From 15d9f81d6fddd3ab96de6993de7947c4aee9d665 Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Tue, 21 Mar 2023 11:40:23 +0100 Subject: [PATCH 1/5] fix(r/faucet): Render call panic Fix #621 `Render` prints the controllers in a loop but relies on `gControllersSize` rather than `gControllers.Size()` to determine the number of controllers. The fix simply replaces `gControllersSize` by `gControllers.Size()`. Fix #627 filetest `Output:` directive doesn't work when the output contains multiple consecutive empty lines, because the method used to collect those comments truncate them (see issue and `ast.CommentGroup.Text()`). The fix replaces this method by a custom one that doesn't truncate. --- examples/gno.land/r/gnoland/faucet/faucet.gno | 2 +- .../gno.land/r/gnoland/faucet/z0_filetest.gno | 27 +++++++++ .../gno.land/r/gnoland/faucet/z1_filetest.gno | 27 +++++++++ .../gno.land/r/gnoland/faucet/z2_filetest.gno | 44 +++++++++++++++ .../gno.land/r/gnoland/faucet/z3_filetest.gno | 56 +++++++++++++++++++ gnovm/tests/file.go | 21 ++++++- 6 files changed, 175 insertions(+), 2 deletions(-) create mode 100644 examples/gno.land/r/gnoland/faucet/z0_filetest.gno create mode 100644 examples/gno.land/r/gnoland/faucet/z1_filetest.gno create mode 100644 examples/gno.land/r/gnoland/faucet/z2_filetest.gno create mode 100644 examples/gno.land/r/gnoland/faucet/z3_filetest.gno diff --git a/examples/gno.land/r/gnoland/faucet/faucet.gno b/examples/gno.land/r/gnoland/faucet/faucet.gno index c68f6d203f0..61b08bb7aa1 100644 --- a/examples/gno.land/r/gnoland/faucet/faucet.gno +++ b/examples/gno.land/r/gnoland/faucet/faucet.gno @@ -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)) } diff --git a/examples/gno.land/r/gnoland/faucet/z0_filetest.gno b/examples/gno.land/r/gnoland/faucet/z0_filetest.gno new file mode 100644 index 00000000000..d6c3273497f --- /dev/null +++ b/examples/gno.land/r/gnoland/faucet/z0_filetest.gno @@ -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 diff --git a/examples/gno.land/r/gnoland/faucet/z1_filetest.gno b/examples/gno.land/r/gnoland/faucet/z1_filetest.gno new file mode 100644 index 00000000000..5615e54f3fb --- /dev/null +++ b/examples/gno.land/r/gnoland/faucet/z1_filetest.gno @@ -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: +// path# Community Faucet. +// +// Status: active. +// Balance: 200000000ugnot. +// Total transfers: (in 0 times). +// +// Package address: g17rgsdnfxzza0sdfsdma37sdwxagsz378833ca4 +// +// Admin: g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 +// +// Controllers: +// +// +// +// Per request limit: 350000000ugnot diff --git a/examples/gno.land/r/gnoland/faucet/z2_filetest.gno b/examples/gno.land/r/gnoland/faucet/z2_filetest.gno new file mode 100644 index 00000000000..93c0aa33a9f --- /dev/null +++ b/examples/gno.land/r/gnoland/faucet/z2_filetest.gno @@ -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: +// +// (unhandled) (unhandled) +// +// Per request limit: 350000000ugnot diff --git a/examples/gno.land/r/gnoland/faucet/z3_filetest.gno b/examples/gno.land/r/gnoland/faucet/z3_filetest.gno new file mode 100644 index 00000000000..269d5c93680 --- /dev/null +++ b/examples/gno.land/r/gnoland/faucet/z3_filetest.gno @@ -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: +// +// (unhandled) (unhandled) +// +// Per request limit: 350000000ugnot diff --git a/gnovm/tests/file.go b/gnovm/tests/file.go index 10eb0ddb62c..5d53f9fa021 100644 --- a/gnovm/tests/file.go +++ b/gnovm/tests/file.go @@ -3,6 +3,7 @@ package tests import ( "bytes" "fmt" + "go/ast" "go/parser" "go/token" "io" @@ -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:")) @@ -386,6 +387,24 @@ 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 := c.Text[2:] + if len(s) > 0 && s[0] == ' ' { + s = s[1:] + } + 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) From 25080e05995f7ac44d2e9ec7cb5dc54490871ddb Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Fri, 24 Mar 2023 16:28:03 +0100 Subject: [PATCH 2/5] Update tests/file.go Co-authored-by: Morgan --- gnovm/tests/file.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/gnovm/tests/file.go b/gnovm/tests/file.go index 5d53f9fa021..a9f15ebcab3 100644 --- a/gnovm/tests/file.go +++ b/gnovm/tests/file.go @@ -396,10 +396,7 @@ func readComments(cg *ast.CommentGroup) string { // ignore no //-style comment break } - s := c.Text[2:] - if len(s) > 0 && s[0] == ' ' { - s = s[1:] - } + s := strings.TrimPrefix(c.Text[2:], " ") b.WriteString(s + "\n") } return b.String() From a008d41ffc790644db09e00e8af025cff11dd2bb Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Fri, 24 Mar 2023 17:27:56 +0100 Subject: [PATCH 3/5] rename gControllersSize to gControllersMaxSize --- examples/gno.land/r/gnoland/faucet/admin.gno | 2 +- examples/gno.land/r/gnoland/faucet/faucet.gno | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/gno.land/r/gnoland/faucet/admin.gno b/examples/gno.land/r/gnoland/faucet/admin.gno index 000adb8f310..a4916c5af02 100644 --- a/examples/gno.land/r/gnoland/faucet/admin.gno +++ b/examples/gno.land/r/gnoland/faucet/admin.gno @@ -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" } diff --git a/examples/gno.land/r/gnoland/faucet/faucet.gno b/examples/gno.land/r/gnoland/faucet/faucet.gno index 61b08bb7aa1..b198fa4c5f7 100644 --- a/examples/gno.land/r/gnoland/faucet/faucet.gno +++ b/examples/gno.land/r/gnoland/faucet/faucet.gno @@ -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 From 8e256a96b3aa55b15bcb054dcd67bce3cacab6f5 Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Mon, 27 Mar 2023 16:45:12 +0200 Subject: [PATCH 4/5] fix(faucet): remove path from Render output --- examples/gno.land/r/gnoland/faucet/faucet.gno | 4 ++-- examples/gno.land/r/gnoland/faucet/z1_filetest.gno | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/gno.land/r/gnoland/faucet/faucet.gno b/examples/gno.land/r/gnoland/faucet/faucet.gno index b198fa4c5f7..c9d8ed503d0 100644 --- a/examples/gno.land/r/gnoland/faucet/faucet.gno +++ b/examples/gno.land/r/gnoland/faucet/faucet.gno @@ -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 { diff --git a/examples/gno.land/r/gnoland/faucet/z1_filetest.gno b/examples/gno.land/r/gnoland/faucet/z1_filetest.gno index 5615e54f3fb..54fa750ba5e 100644 --- a/examples/gno.land/r/gnoland/faucet/z1_filetest.gno +++ b/examples/gno.land/r/gnoland/faucet/z1_filetest.gno @@ -10,7 +10,7 @@ func main() { } // Output: -// path# Community Faucet. +// # Community Faucet. // // Status: active. // Balance: 200000000ugnot. From 24920e0b979dfa65f03cc42c5131327c34bb2691 Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Mon, 27 Mar 2023 17:04:46 +0200 Subject: [PATCH 5/5] fix after merge with master and ufmt+stringer --- examples/gno.land/r/gnoland/faucet/z2_filetest.gno | 2 +- examples/gno.land/r/gnoland/faucet/z3_filetest.gno | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/gno.land/r/gnoland/faucet/z2_filetest.gno b/examples/gno.land/r/gnoland/faucet/z2_filetest.gno index 93c0aa33a9f..1791cd91989 100644 --- a/examples/gno.land/r/gnoland/faucet/z2_filetest.gno +++ b/examples/gno.land/r/gnoland/faucet/z2_filetest.gno @@ -39,6 +39,6 @@ func main() { // // Controllers: // -// (unhandled) (unhandled) +// g1vdhkuarjdakxcetjx9047h6lta047h6lsdacav g1vdhkuarjdakxcetjxf047h6lta047h6lnrev3v // // Per request limit: 350000000ugnot diff --git a/examples/gno.land/r/gnoland/faucet/z3_filetest.gno b/examples/gno.land/r/gnoland/faucet/z3_filetest.gno index 269d5c93680..1dca56811a9 100644 --- a/examples/gno.land/r/gnoland/faucet/z3_filetest.gno +++ b/examples/gno.land/r/gnoland/faucet/z3_filetest.gno @@ -51,6 +51,6 @@ func main() { // // Controllers: // -// (unhandled) (unhandled) +// g1vdhkuarjdakxcetjx9047h6lta047h6lsdacav g1vdhkuarjdakxcetjxf047h6lta047h6lnrev3v // // Per request limit: 350000000ugnot