Skip to content

Commit

Permalink
fix(r/faucet): Render call panic (gnolang#628)
Browse files Browse the repository at this point in the history
Co-authored-by: Morgan <git@howl.moe>
Co-authored-by: Manfred Touron <94029+moul@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 3, 2023
1 parent aa730e3 commit f8231e0
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/gno.land/r/gnoland/faucet/admin.gno
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func AdminAddController(addr std.Address) string {

size := gControllers.Size()

if size >= gControllersSize {
if size >= gControllersMaxSize {
return "can not add more controllers than allowed"
}

Expand Down
16 changes: 8 additions & 8 deletions examples/gno.land/r/gnoland/faucet/faucet.gno
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (

var (
// configurable by admin.
gAdminAddr std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
gControllers = avl.NewTree()
gControllersSize = 10 // limit it to 10
gInPause = false
gMessage = "# Community Faucet.\n\n"
gAdminAddr std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
gControllers = avl.NewTree()
gControllersMaxSize = 10 // limit it to 10
gInPause = false
gMessage = "# Community Faucet.\n\n"

// internal vars, for stats.
gTotalTransferred std.Coins
Expand Down Expand Up @@ -52,11 +52,11 @@ func GetPerTransferLimit() int64 {
return gLimit.Amount
}

func Render(path string) string {
func Render(_ string) string {
banker := std.GetBanker(std.BankerTypeRealmSend)
balance := banker.GetCoins(std.GetOrigPkgAddr())

output := path + gMessage
output := gMessage
if gInPause {
output += "Status: inactive.\n"
} else {
Expand All @@ -69,7 +69,7 @@ func Render(path string) string {
output += ufmt.Sprintf("Admin: %s\n\n ", gAdminAddr.String())
output += ufmt.Sprintf("Controllers:\n\n ")

for i := 0; i < gControllersSize; i++ {
for i := 0; i < gControllers.Size(); i++ {
_, v := gControllers.GetByIndex(i)
output += ufmt.Sprintf("%s ", v.(std.Address))
}
Expand Down
27 changes: 27 additions & 0 deletions examples/gno.land/r/gnoland/faucet/z0_filetest.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"gno.land/r/gnoland/faucet"
)

// assert render with empty path and no controllers
func main() {
println(faucet.Render(""))
}

// Output:
// # Community Faucet.
//
// Status: active.
// Balance: 200000000ugnot.
// Total transfers: (in 0 times).
//
// Package address: g17rgsdnfxzza0sdfsdma37sdwxagsz378833ca4
//
// Admin: g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5
//
// Controllers:
//
//
//
// Per request limit: 350000000ugnot
27 changes: 27 additions & 0 deletions examples/gno.land/r/gnoland/faucet/z1_filetest.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"gno.land/r/gnoland/faucet"
)

// assert render with a path and no controllers
func main() {
println(faucet.Render("path"))
}

// Output:
// # Community Faucet.
//
// Status: active.
// Balance: 200000000ugnot.
// Total transfers: (in 0 times).
//
// Package address: g17rgsdnfxzza0sdfsdma37sdwxagsz378833ca4
//
// Admin: g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5
//
// Controllers:
//
//
//
// Per request limit: 350000000ugnot
44 changes: 44 additions & 0 deletions examples/gno.land/r/gnoland/faucet/z2_filetest.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"std"

"gno.land/p/demo/testutils"
"gno.land/r/gnoland/faucet"
)

// assert render with empty path and 2 controllers
func main() {
var (
adminaddr = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
controlleraddr1 = testutils.TestAddress("controller1")
controlleraddr2 = testutils.TestAddress("controller2")
)
std.TestSetOrigCaller(adminaddr)
err := faucet.AdminAddController(controlleraddr1)
if err != nil {
panic(err)
}
err = faucet.AdminAddController(controlleraddr2)
if err != nil {
panic(err)
}
println(faucet.Render(""))
}

// Output:
// # Community Faucet.
//
// Status: active.
// Balance: 200000000ugnot.
// Total transfers: (in 0 times).
//
// Package address: g17rgsdnfxzza0sdfsdma37sdwxagsz378833ca4
//
// Admin: g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5
//
// Controllers:
//
// g1vdhkuarjdakxcetjx9047h6lta047h6lsdacav g1vdhkuarjdakxcetjxf047h6lta047h6lnrev3v
//
// Per request limit: 350000000ugnot
56 changes: 56 additions & 0 deletions examples/gno.land/r/gnoland/faucet/z3_filetest.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"std"

"gno.land/p/demo/testutils"
"gno.land/r/gnoland/faucet"
)

// assert render with 2 controllers and 2 transfers
func main() {
var (
adminaddr = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
controlleraddr1 = testutils.TestAddress("controller1")
controlleraddr2 = testutils.TestAddress("controller2")
testaddr1 = testutils.TestAddress("test1")
testaddr2 = testutils.TestAddress("test2")
)
std.TestSetOrigCaller(adminaddr)
err := faucet.AdminAddController(controlleraddr1)
if err != nil {
panic(err)
}
err = faucet.AdminAddController(controlleraddr2)
if err != nil {
panic(err)
}
std.TestSetOrigCaller(controlleraddr1)
err = faucet.Transfer(testaddr1, 1000000)
if err != nil {
panic(err)
}
std.TestSetOrigCaller(controlleraddr2)
err = faucet.Transfer(testaddr1, 2000000)
if err != nil {
panic(err)
}
println(faucet.Render(""))
}

// Output:
// # Community Faucet.
//
// Status: active.
// Balance: 197000000ugnot.
// Total transfers: 3000000ugnot (in 2 times).
//
// Package address: g17rgsdnfxzza0sdfsdma37sdwxagsz378833ca4
//
// Admin: g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5
//
// Controllers:
//
// g1vdhkuarjdakxcetjx9047h6lta047h6lsdacav g1vdhkuarjdakxcetjxf047h6lta047h6lnrev3v
//
// Per request limit: 350000000ugnot
18 changes: 17 additions & 1 deletion gnovm/tests/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tests
import (
"bytes"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io"
Expand Down Expand Up @@ -347,7 +348,7 @@ func wantedFromComment(p string) (directives []string, pkgPath, res, err, rops s
return
}
for _, comments := range f.Comments {
text := comments.Text()
text := readComments(comments)
if strings.HasPrefix(text, "PKGPATH:") {
line := strings.SplitN(text, "\n", 2)[0]
pkgPath = strings.TrimSpace(strings.TrimPrefix(line, "PKGPATH:"))
Expand Down Expand Up @@ -386,6 +387,21 @@ func wantedFromComment(p string) (directives []string, pkgPath, res, err, rops s
return
}

// readComments returns //-style comments from cg, but without truncating empty
// lines like cg.Text().
func readComments(cg *ast.CommentGroup) string {
var b strings.Builder
for _, c := range cg.List {
if len(c.Text) < 2 || c.Text[:2] != "//" {
// ignore no //-style comment
break
}
s := strings.TrimPrefix(c.Text[2:], " ")
b.WriteString(s + "\n")
}
return b.String()
}

// Replace comment in file with given output given directive.
func replaceWantedInPlace(path string, directive string, output string) {
bz := osm.MustReadFile(path)
Expand Down

0 comments on commit f8231e0

Please sign in to comment.