From 80b2d01ad13e6e1cb3b14012f716de971d30e0d7 Mon Sep 17 00:00:00 2001 From: piux2 <> Date: Sat, 26 Nov 2022 20:05:40 -0800 Subject: [PATCH 1/7] update package location and number of controllers --- examples/gno.land/r/gnoland/faucet/admin.gno | 73 ++++++++++++---- examples/gno.land/r/gnoland/faucet/faucet.gno | 67 ++++++++++----- .../r/gnoland/faucet/faucet_filetest.gno | 75 ---------------- .../gno.land/r/gnoland/faucet/faucet_test.gno | 86 +++++++++++++++---- 4 files changed, 171 insertions(+), 130 deletions(-) delete mode 100644 examples/gno.land/r/gnoland/faucet/faucet_filetest.gno diff --git a/examples/gno.land/r/gnoland/faucet/admin.gno b/examples/gno.land/r/gnoland/faucet/admin.gno index 03798ba92e7..13672911eac 100644 --- a/examples/gno.land/r/gnoland/faucet/admin.gno +++ b/examples/gno.land/r/gnoland/faucet/admin.gno @@ -5,44 +5,83 @@ import ( "std" ) -func AdminSetInPause(inPause bool) error { +func AdminSetInPause(inPause bool) string { if err := assertIsAdmin(); err != nil { - return err + return err.Error() } gInPause = inPause - return nil + return "" } -func AdminSetMessage(message string) error { +func AdminSetMessage(message string) string { if err := assertIsAdmin(); err != nil { - return err + return err.Error() } gMessage = message - return nil + return "" } -func AdminSetPerTransferSend(send std.Coins) error { +func AdminSetTransferLimit(amount int64, denom string) string { if err := assertIsAdmin(); err != nil { - return err + return err.Error() } - gPerTransferSend = send - return nil + gLimit = std.Coins{std.Coin{Denom: denom, Amount: amount}} + return "" } -func AdminSetAdminAddr(addr std.Address) error { +func AdminSetAdminAddr(addr std.Address) string { if err := assertIsAdmin(); err != nil { - return err + return err.Error() } gAdminAddr = addr - return nil + return "" } -func AdminSetControllerAddr(addr std.Address) error { +func AdminAddController(addr std.Address) string { if err := assertIsAdmin(); err != nil { - return err + return err.Error() } - gControllerAddr = addr - return nil + + k := -1 + for _, v := range gControllers { + if v == addr { + return addr.String() + " exists, no need to add." + } + } + for i, v := range gControllers { + if v == "" { + + k = i + break + } + } + if k < 0 { + return "can not add more controllers than allowed" + } + + gControllers[k] = addr + + return "" +} + +func AdminRemoveController(addr std.Address) string { + if err := assertIsAdmin(); err != nil { + return err.Error() + } + + removed := false + for i, v := range gControllers { + if v == addr { + + gControllers[i] = "" + removed = true + } + } + + if removed == false { + return addr.String() + " is not on the controller list" + } + return "" } func assertIsAdmin() error { diff --git a/examples/gno.land/r/gnoland/faucet/faucet.gno b/examples/gno.land/r/gnoland/faucet/faucet.gno index 0d6c8ee37f5..4b31d246e3a 100644 --- a/examples/gno.land/r/gnoland/faucet/faucet.gno +++ b/examples/gno.land/r/gnoland/faucet/faucet.gno @@ -9,60 +9,85 @@ import ( var ( // configurable by admin. - gAdminAddr std.Address = "g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq" - gControllerAddr std.Address = "g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq" - gPerTransferSend = std.Coins{std.Coin{"ugnot", 1000000}} - gInPause = false - gMessage = "# Community Faucet.\n\n" + gAdminAddr std.Address = "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" + + gControllers [10]std.Address = [10]std.Address{} // limit it to 10 + + gInPause = false + gMessage = "# Community Faucet.\n\n" // internal vars, for stats. gTotalTransferred std.Coins gTotalTransfers = uint(0) + + // per request limit, 350 gnot + gLimit std.Coins = std.Coins{std.Coin{"ugnot", 350000000}} ) -func Transfer(to std.Address) error { +func Transfer(to std.Address, send int64) string { if err := assertIsController(); err != nil { - return err + return err.Error() } if gInPause { - return errors.New("faucet in pause") + return errors.New("faucet in pause").Error() } - send := gPerTransferSend + var sendCoins std.Coins + + // limit the pre request + if send <= gLimit.AmountOf("ugnot") { + sendCoins = std.Coins{std.Coin{Denom: "ugnot", Amount: send}} + } else { + return errors.New("Per request limit " + gLimit.String() + " exceed").Error() + } - gTotalTransferred = gTotalTransferred.Add(send) + gTotalTransferred = gTotalTransferred.Add(sendCoins) gTotalTransfers++ - banker := std.GetBanker(std.BankerTypeOrigSend) + banker := std.GetBanker(std.BankerTypeRealmSend) pkgaddr := std.GetOrigPkgAddr() - banker.SendCoins(pkgaddr, to, send) - return nil + banker.SendCoins(pkgaddr, to, sendCoins) + return "" +} + +func GetPerTransferLimit() int64 { + return gLimit.AmountOf("ugnot") } func Render(path string) string { - banker := std.GetBanker(std.BankerTypeOrigSend) + banker := std.GetBanker(std.BankerTypeRealmSend) balance := banker.GetCoins(std.GetOrigPkgAddr()) - output := gMessage + output := path + gMessage if gInPause { output += "Status: inactive.\n" } else { output += "Status: active.\n" } output += ufmt.Sprintf("Balance: %s.\n", balance.String()) - output += ufmt.Sprintf("Rewarded: %s (in %d times).\n", gTotalTransferred.String(), gTotalTransfers) + output += ufmt.Sprintf("Total transfers: %s (in %d times).\n\n", gTotalTransferred.String(), gTotalTransfers) + + output += "Package address: " + std.GetOrigPkgAddr().String() + "\n\n" + output += ufmt.Sprintf("Admin: %s\n\n ", gAdminAddr.String()) + output += ufmt.Sprintf("Controllers:\n\n ") - if path == "?debug" { - output += ufmt.Sprintf("Admin: %s, Controller: %s\n", gAdminAddr.String(), gControllerAddr.String()) + for _, v := range gControllers { + output += ufmt.Sprintf("%s ", v.String()) } + output += "\n\n" + output += ufmt.Sprintf("Per request limit: %s\n\n", gLimit.String()) + return output } func assertIsController() error { caller := std.GetOrigCaller() - if caller != gControllerAddr { - return errors.New("restricted for controller") + + for _, v := range gControllers { + if caller == v { + return nil + } } - return nil + return errors.New(caller.String() + " is not on the controller list") } diff --git a/examples/gno.land/r/gnoland/faucet/faucet_filetest.gno b/examples/gno.land/r/gnoland/faucet/faucet_filetest.gno deleted file mode 100644 index d3e4b5a0ca9..00000000000 --- a/examples/gno.land/r/gnoland/faucet/faucet_filetest.gno +++ /dev/null @@ -1,75 +0,0 @@ -package main - -import ( - "fmt" - "std" - - "gno.land/p/demo/testutils" - "gno.land/r/gnoland/faucet" -) - -func main() { - var ( - adminaddr = std.Address("g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq") - controlleraddr = testutils.TestAddress("controller") - mainaddr = std.TestDerivePkgAddr("main") - faucetaddr = std.TestDerivePkgAddr("gno.land/r/faucet") - test1addr = testutils.TestAddress("test1") - test2addr = testutils.TestAddress("test2") - test3addr = testutils.TestAddress("test3") - ) - - banker := std.GetBanker(std.BankerTypeReadonly) - std.TestIssueCoins(faucetaddr, std.Coins{{"ugnot", 1000000000}}) - std.TestSetOrigPkgAddr(faucetaddr) - - showBalances := func() string { - var ( - mainbal = banker.GetCoins(mainaddr) - faucetbal = banker.GetCoins(faucetaddr) - test1bal = banker.GetCoins(test1addr) - test2bal = banker.GetCoins(test2addr) - test3bal = banker.GetCoins(test3addr) - controllerbal = banker.GetCoins(controlleraddr) - adminbal = banker.GetCoins(adminaddr) - ) - return fmt.Sprintf("main=%q, faucet=%q, test1=%q, test2=%q test3=%q controller=%q admin=%q", - mainbal.String(), faucetbal.String(), test1bal.String(), test2bal.String(), - test3bal.String(), controllerbal.String(), adminbal.String()) - } - - println("before:", showBalances()) - - // simulate a Deposit call. - std.TestSetOrigSend(std.Coins{{"ugnot", 2000000000}}, nil) - std.TestSetOrigCaller(adminaddr) - faucet.AdminSetControllerAddr(controlleraddr) - std.TestSetOrigCaller(controlleraddr) - faucet.Transfer(test1addr) - faucet.Transfer(test2addr) - faucet.Transfer(test1addr) - - println("after: ", showBalances()) - - // simulate a Render(). - ret := faucet.Render("") - println("---") - println("Render: ", ret) - println("---") - println("done") -} - -// Output: -// before: main="200000000ugnot", faucet="1000000000ugnot", test1="", test2="" test3="" controller="" admin="" -// after: main="200000000ugnot", faucet="997000000ugnot", test1="2000000ugnot", test2="1000000ugnot" test3="" controller="" admin="" -// --- -// Render: # Community Faucet. -// -// Status: active. -// Balance: 997000000ugnot. -// Rewarded: 3000000ugnot (in 3 times). -// -// --- -// done - -// Realm: diff --git a/examples/gno.land/r/gnoland/faucet/faucet_test.gno b/examples/gno.land/r/gnoland/faucet/faucet_test.gno index 54214dd0ede..47d2a65ecf3 100644 --- a/examples/gno.land/r/gnoland/faucet/faucet_test.gno +++ b/examples/gno.land/r/gnoland/faucet/faucet_test.gno @@ -11,42 +11,94 @@ import ( func TestPackage(t *testing.T) { var ( - adminaddr = std.Address("g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq") - controlleraddr = testutils.TestAddress("controller") - test1addr = testutils.TestAddress("test1") + adminaddr = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + faucetaddr = std.TestDerivePkgAddr("gno.land/r/faucet") + controlleraddr1 = testutils.TestAddress("controller1") + controlleraddr2 = testutils.TestAddress("controller2") + controlleraddr3 = testutils.TestAddress("controller3") + controlleraddr4 = testutils.TestAddress("controller4") + controlleraddr5 = testutils.TestAddress("controller5") + controlleraddr6 = testutils.TestAddress("controller6") + controlleraddr7 = testutils.TestAddress("controller7") + controlleraddr8 = testutils.TestAddress("controller8") + controlleraddr9 = testutils.TestAddress("controller9") + controlleraddr10 = testutils.TestAddress("controller10") + controlleraddr11 = testutils.TestAddress("controller11") + + test1addr = testutils.TestAddress("test1") ) + // deposit 1000gnot to faucet contract + + std.TestIssueCoins(faucetaddr, std.Coins{{"ugnot", 1000000000}}) + std.TestSetOrigPkgAddr(faucetaddr) + assertBalance(t, faucetaddr, 1000000000) // by default, balance is empty, and as a user I cannot call Transfer, or Admin commands. - std.TestSetOrigSend(std.Coins{{"ugnot", 2000000000}}, nil) + assertBalance(t, test1addr, 0) - assertErr(t, faucet.Transfer(test1addr)) - assertErr(t, faucet.AdminSetControllerAddr(controlleraddr)) - std.TestSetOrigCaller(controlleraddr) - assertErr(t, faucet.Transfer(test1addr)) + assertErr(t, faucet.Transfer(test1addr, 1000000)) + assertErr(t, faucet.AdminAddController(controlleraddr1)) + std.TestSetOrigCaller(controlleraddr1) + assertErr(t, faucet.Transfer(test1addr, 1000000)) - // as an admin, set the controller to controlleraddr. + // as an admin, add the controller to contract and deposit more 2000gnot to contract std.TestSetOrigCaller(adminaddr) - assertNoErr(t, faucet.AdminSetControllerAddr(controlleraddr)) + assertNoErr(t, faucet.AdminAddController(controlleraddr1)) + assertBalance(t, faucetaddr, 1000000000) // now, send some tokens as controller. - std.TestSetOrigCaller(controlleraddr) - assertNoErr(t, faucet.Transfer(test1addr)) + std.TestSetOrigCaller(controlleraddr1) + assertNoErr(t, faucet.Transfer(test1addr, 1000000)) assertBalance(t, test1addr, 1000000) - assertNoErr(t, faucet.Transfer(test1addr)) + assertNoErr(t, faucet.Transfer(test1addr, 1000000)) assertBalance(t, test1addr, 2000000) + assertBalance(t, faucetaddr, 998000000) + + // remove controller + // as an admin, remove controller + std.TestSetOrigCaller(adminaddr) + assertNoErr(t, faucet.AdminRemoveController(controlleraddr1)) + std.TestSetOrigCaller(controlleraddr1) + assertErr(t, faucet.Transfer(test1addr, 1000000)) + + // duplicate controller + std.TestSetOrigCaller(adminaddr) + assertNoErr(t, faucet.AdminAddController(controlleraddr1)) + assertErr(t, faucet.AdminAddController(controlleraddr1)) + // add more than more than allowed controllers + assertNoErr(t, faucet.AdminAddController(controlleraddr2)) + assertNoErr(t, faucet.AdminAddController(controlleraddr3)) + assertNoErr(t, faucet.AdminAddController(controlleraddr4)) + assertNoErr(t, faucet.AdminAddController(controlleraddr5)) + assertNoErr(t, faucet.AdminAddController(controlleraddr6)) + assertNoErr(t, faucet.AdminAddController(controlleraddr7)) + assertNoErr(t, faucet.AdminAddController(controlleraddr8)) + assertNoErr(t, faucet.AdminAddController(controlleraddr9)) + assertNoErr(t, faucet.AdminAddController(controlleraddr10)) + assertErr(t, faucet.AdminAddController(controlleraddr11)) + + // send more than per transfer limit + std.TestSetOrigCaller(adminaddr) + faucet.AdminSetTransferLimit(300000000, "ugnot") + std.TestSetOrigCaller(controlleraddr1) + assertErr(t, faucet.Transfer(test1addr, 301000000)) + + // block transefer from the address not on the controllers list. + std.TestSetOrigCaller(controlleraddr11) + assertErr(t, faucet.Transfer(test1addr, 1000000)) } -func assertErr(t *testing.T, err error) { +func assertErr(t *testing.T, err string) { t.Helper() - if err == nil { + if err == "" { t.Logf("info: got err: %v", err) t.Errorf("expected an error, got nil.") } } -func assertNoErr(t *testing.T, err error) { +func assertNoErr(t *testing.T, err string) { t.Helper() - if err != nil { + if err != "" { t.Errorf("got err: %v.", err) } } From 65bb40f0f6630765d852d58d3e064e33a7ee23fa Mon Sep 17 00:00:00 2001 From: piux2 <90544084+piux2@users.noreply.github.com> Date: Mon, 28 Nov 2022 12:13:50 -0800 Subject: [PATCH 2/7] Update examples/gno.land/r/gnoland/faucet/faucet.gno Co-authored-by: Manfred Touron <94029+moul@users.noreply.github.com> --- examples/gno.land/r/gnoland/faucet/faucet.gno | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/examples/gno.land/r/gnoland/faucet/faucet.gno b/examples/gno.land/r/gnoland/faucet/faucet.gno index 4b31d246e3a..9e504364dda 100644 --- a/examples/gno.land/r/gnoland/faucet/faucet.gno +++ b/examples/gno.land/r/gnoland/faucet/faucet.gno @@ -33,14 +33,11 @@ func Transfer(to std.Address, send int64) string { return errors.New("faucet in pause").Error() } - var sendCoins std.Coins - - // limit the pre request - if send <= gLimit.AmountOf("ugnot") { - sendCoins = std.Coins{std.Coin{Denom: "ugnot", Amount: send}} - } else { + // limit the per request + if send > gLimit.AmountOf("ugnot") { return errors.New("Per request limit " + gLimit.String() + " exceed").Error() } + sendCoins := std.Coins{std.Coin{Denom: "ugnot", Amount: send}} gTotalTransferred = gTotalTransferred.Add(sendCoins) gTotalTransfers++ From 26590e488218ed75433d75213661a626cac84e2d Mon Sep 17 00:00:00 2001 From: piux2 <> Date: Thu, 1 Dec 2022 17:38:49 -0800 Subject: [PATCH 3/7] replaced array with avl as the global controllers storage --- examples/gno.land/r/gnoland/faucet/admin.gno | 45 +++++++++---------- examples/gno.land/r/gnoland/faucet/faucet.gno | 38 +++++++++++----- 2 files changed, 48 insertions(+), 35 deletions(-) diff --git a/examples/gno.land/r/gnoland/faucet/admin.gno b/examples/gno.land/r/gnoland/faucet/admin.gno index 13672911eac..bb691644baa 100644 --- a/examples/gno.land/r/gnoland/faucet/admin.gno +++ b/examples/gno.land/r/gnoland/faucet/admin.gno @@ -21,11 +21,11 @@ func AdminSetMessage(message string) string { return "" } -func AdminSetTransferLimit(amount int64, denom string) string { +func AdminSetTransferLimit(amount int64) string { if err := assertIsAdmin(); err != nil { return err.Error() } - gLimit = std.Coins{std.Coin{Denom: denom, Amount: amount}} + gLimit = std.Coin{Denom: "ugnot", Amount: amount} return "" } @@ -42,24 +42,19 @@ func AdminAddController(addr std.Address) string { return err.Error() } - k := -1 - for _, v := range gControllers { - if v == addr { - return addr.String() + " exists, no need to add." - } - } - for i, v := range gControllers { - if v == "" { + size := gControllers.Size() - k = i - break - } - } - if k < 0 { + if size >= gControllersSize { return "can not add more controllers than allowed" } - gControllers[k] = addr + if gControllers.Has(addr.String()) { + + return addr.String() + " exists, no need to add." + + } + + gControllers.Set(addr.String(), addr) return "" } @@ -69,18 +64,22 @@ func AdminRemoveController(addr std.Address) string { return err.Error() } - removed := false - for i, v := range gControllers { - if v == addr { + if !gControllers.Has(addr.String()) { + + return addr.String() + " is not on the controller list" - gControllers[i] = "" - removed = true - } } - if removed == false { + _, ok := gControllers.Remove(addr.String()) + + // it not should happen. + // we will check anyway to prevent issues in the underline implementation. + + if !ok { + return addr.String() + " is not on the controller list" } + return "" } diff --git a/examples/gno.land/r/gnoland/faucet/faucet.gno b/examples/gno.land/r/gnoland/faucet/faucet.gno index 9e504364dda..d60235e9bf2 100644 --- a/examples/gno.land/r/gnoland/faucet/faucet.gno +++ b/examples/gno.land/r/gnoland/faucet/faucet.gno @@ -4,16 +4,19 @@ import ( "errors" "std" + "gno.land/p/demo/avl" "gno.land/p/demo/ufmt" ) var ( // configurable by admin. - gAdminAddr std.Address = "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" + gAdminAddr std.Address - gControllers [10]std.Address = [10]std.Address{} // limit it to 10 + gControllers = avl.NewMutTree() + gControllersSize = 10 // limit it to 10 gInPause = false + gMessage = "# Community Faucet.\n\n" // internal vars, for stats. @@ -21,9 +24,15 @@ var ( gTotalTransfers = uint(0) // per request limit, 350 gnot - gLimit std.Coins = std.Coins{std.Coin{"ugnot", 350000000}} + gLimit std.Coin = std.Coin{"ugnot", 350000000} ) +func init() { + + gAdminAddr = std.GetOrigCaller() + +} + func Transfer(to std.Address, send int64) string { if err := assertIsController(); err != nil { return err.Error() @@ -34,7 +43,7 @@ func Transfer(to std.Address, send int64) string { } // limit the per request - if send > gLimit.AmountOf("ugnot") { + if send > gLimit.Amount { return errors.New("Per request limit " + gLimit.String() + " exceed").Error() } sendCoins := std.Coins{std.Coin{Denom: "ugnot", Amount: send}} @@ -49,7 +58,7 @@ func Transfer(to std.Address, send int64) string { } func GetPerTransferLimit() int64 { - return gLimit.AmountOf("ugnot") + return gLimit.Amount } func Render(path string) string { @@ -69,9 +78,14 @@ func Render(path string) string { output += ufmt.Sprintf("Admin: %s\n\n ", gAdminAddr.String()) output += ufmt.Sprintf("Controllers:\n\n ") - for _, v := range gControllers { - output += ufmt.Sprintf("%s ", v.String()) + for i := 0; i < gControllersSize; i++ { + + _, v := gControllers.GetByIndex(i) + + output += ufmt.Sprintf("%s ", v.(std.Address)) + } + output += "\n\n" output += ufmt.Sprintf("Per request limit: %s\n\n", gLimit.String()) @@ -81,10 +95,10 @@ func Render(path string) string { func assertIsController() error { caller := std.GetOrigCaller() - for _, v := range gControllers { - if caller == v { - return nil - } + ok := gControllers.Has(caller.String()) + + if !ok { + errors.New(caller.String() + " is not on the controller list") } - return errors.New(caller.String() + " is not on the controller list") + return nil } From 598d6417d2ea15ef7cfae18c74aaf22a358d3d0b Mon Sep 17 00:00:00 2001 From: piux2 <> Date: Sat, 3 Dec 2022 15:53:10 -0800 Subject: [PATCH 4/7] use val tree --- examples/gno.land/r/gnoland/faucet/admin.gno | 5 ----- examples/gno.land/r/gnoland/faucet/faucet.gno | 10 ++-------- examples/gno.land/r/gnoland/faucet/faucet_test.gno | 6 ++++-- 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/examples/gno.land/r/gnoland/faucet/admin.gno b/examples/gno.land/r/gnoland/faucet/admin.gno index bb691644baa..000adb8f310 100644 --- a/examples/gno.land/r/gnoland/faucet/admin.gno +++ b/examples/gno.land/r/gnoland/faucet/admin.gno @@ -49,9 +49,7 @@ func AdminAddController(addr std.Address) string { } if gControllers.Has(addr.String()) { - return addr.String() + " exists, no need to add." - } gControllers.Set(addr.String(), addr) @@ -65,9 +63,7 @@ func AdminRemoveController(addr std.Address) string { } if !gControllers.Has(addr.String()) { - return addr.String() + " is not on the controller list" - } _, ok := gControllers.Remove(addr.String()) @@ -76,7 +72,6 @@ func AdminRemoveController(addr std.Address) string { // we will check anyway to prevent issues in the underline implementation. if !ok { - return addr.String() + " is not on the controller list" } diff --git a/examples/gno.land/r/gnoland/faucet/faucet.gno b/examples/gno.land/r/gnoland/faucet/faucet.gno index d60235e9bf2..2e4daa7214a 100644 --- a/examples/gno.land/r/gnoland/faucet/faucet.gno +++ b/examples/gno.land/r/gnoland/faucet/faucet.gno @@ -10,7 +10,7 @@ import ( var ( // configurable by admin. - gAdminAddr std.Address + gAdminAddr std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") gControllers = avl.NewMutTree() gControllersSize = 10 // limit it to 10 @@ -27,12 +27,6 @@ var ( gLimit std.Coin = std.Coin{"ugnot", 350000000} ) -func init() { - - gAdminAddr = std.GetOrigCaller() - -} - func Transfer(to std.Address, send int64) string { if err := assertIsController(); err != nil { return err.Error() @@ -98,7 +92,7 @@ func assertIsController() error { ok := gControllers.Has(caller.String()) if !ok { - errors.New(caller.String() + " is not on the controller list") + return errors.New(caller.String() + " is not on the controller list") } return nil } diff --git a/examples/gno.land/r/gnoland/faucet/faucet_test.gno b/examples/gno.land/r/gnoland/faucet/faucet_test.gno index 47d2a65ecf3..822dc169045 100644 --- a/examples/gno.land/r/gnoland/faucet/faucet_test.gno +++ b/examples/gno.land/r/gnoland/faucet/faucet_test.gno @@ -36,7 +36,9 @@ func TestPackage(t *testing.T) { // by default, balance is empty, and as a user I cannot call Transfer, or Admin commands. assertBalance(t, test1addr, 0) + std.TestSetOrigCaller(test1addr) assertErr(t, faucet.Transfer(test1addr, 1000000)) + assertErr(t, faucet.AdminAddController(controlleraddr1)) std.TestSetOrigCaller(controlleraddr1) assertErr(t, faucet.Transfer(test1addr, 1000000)) @@ -79,7 +81,7 @@ func TestPackage(t *testing.T) { // send more than per transfer limit std.TestSetOrigCaller(adminaddr) - faucet.AdminSetTransferLimit(300000000, "ugnot") + faucet.AdminSetTransferLimit(300000000) std.TestSetOrigCaller(controlleraddr1) assertErr(t, faucet.Transfer(test1addr, 301000000)) @@ -90,6 +92,7 @@ func TestPackage(t *testing.T) { func assertErr(t *testing.T, err string) { t.Helper() + if err == "" { t.Logf("info: got err: %v", err) t.Errorf("expected an error, got nil.") @@ -105,7 +108,6 @@ func assertNoErr(t *testing.T, err string) { func assertBalance(t *testing.T, addr std.Address, expectedBal int64) { t.Helper() - banker := std.GetBanker(std.BankerTypeReadonly) coins := banker.GetCoins(addr) got := coins.AmountOf("ugnot") From bf486d47f7331a6cafe41aa55abc841c96ef06dc Mon Sep 17 00:00:00 2001 From: piux2 <> Date: Sat, 3 Dec 2022 15:53:10 -0800 Subject: [PATCH 5/7] replace array with avl tree as the global storage --- examples/gno.land/r/gnoland/faucet/admin.gno | 5 ----- examples/gno.land/r/gnoland/faucet/faucet.gno | 10 ++-------- examples/gno.land/r/gnoland/faucet/faucet_test.gno | 6 ++++-- 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/examples/gno.land/r/gnoland/faucet/admin.gno b/examples/gno.land/r/gnoland/faucet/admin.gno index bb691644baa..000adb8f310 100644 --- a/examples/gno.land/r/gnoland/faucet/admin.gno +++ b/examples/gno.land/r/gnoland/faucet/admin.gno @@ -49,9 +49,7 @@ func AdminAddController(addr std.Address) string { } if gControllers.Has(addr.String()) { - return addr.String() + " exists, no need to add." - } gControllers.Set(addr.String(), addr) @@ -65,9 +63,7 @@ func AdminRemoveController(addr std.Address) string { } if !gControllers.Has(addr.String()) { - return addr.String() + " is not on the controller list" - } _, ok := gControllers.Remove(addr.String()) @@ -76,7 +72,6 @@ func AdminRemoveController(addr std.Address) string { // we will check anyway to prevent issues in the underline implementation. if !ok { - return addr.String() + " is not on the controller list" } diff --git a/examples/gno.land/r/gnoland/faucet/faucet.gno b/examples/gno.land/r/gnoland/faucet/faucet.gno index d60235e9bf2..2e4daa7214a 100644 --- a/examples/gno.land/r/gnoland/faucet/faucet.gno +++ b/examples/gno.land/r/gnoland/faucet/faucet.gno @@ -10,7 +10,7 @@ import ( var ( // configurable by admin. - gAdminAddr std.Address + gAdminAddr std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") gControllers = avl.NewMutTree() gControllersSize = 10 // limit it to 10 @@ -27,12 +27,6 @@ var ( gLimit std.Coin = std.Coin{"ugnot", 350000000} ) -func init() { - - gAdminAddr = std.GetOrigCaller() - -} - func Transfer(to std.Address, send int64) string { if err := assertIsController(); err != nil { return err.Error() @@ -98,7 +92,7 @@ func assertIsController() error { ok := gControllers.Has(caller.String()) if !ok { - errors.New(caller.String() + " is not on the controller list") + return errors.New(caller.String() + " is not on the controller list") } return nil } diff --git a/examples/gno.land/r/gnoland/faucet/faucet_test.gno b/examples/gno.land/r/gnoland/faucet/faucet_test.gno index 47d2a65ecf3..822dc169045 100644 --- a/examples/gno.land/r/gnoland/faucet/faucet_test.gno +++ b/examples/gno.land/r/gnoland/faucet/faucet_test.gno @@ -36,7 +36,9 @@ func TestPackage(t *testing.T) { // by default, balance is empty, and as a user I cannot call Transfer, or Admin commands. assertBalance(t, test1addr, 0) + std.TestSetOrigCaller(test1addr) assertErr(t, faucet.Transfer(test1addr, 1000000)) + assertErr(t, faucet.AdminAddController(controlleraddr1)) std.TestSetOrigCaller(controlleraddr1) assertErr(t, faucet.Transfer(test1addr, 1000000)) @@ -79,7 +81,7 @@ func TestPackage(t *testing.T) { // send more than per transfer limit std.TestSetOrigCaller(adminaddr) - faucet.AdminSetTransferLimit(300000000, "ugnot") + faucet.AdminSetTransferLimit(300000000) std.TestSetOrigCaller(controlleraddr1) assertErr(t, faucet.Transfer(test1addr, 301000000)) @@ -90,6 +92,7 @@ func TestPackage(t *testing.T) { func assertErr(t *testing.T, err string) { t.Helper() + if err == "" { t.Logf("info: got err: %v", err) t.Errorf("expected an error, got nil.") @@ -105,7 +108,6 @@ func assertNoErr(t *testing.T, err string) { func assertBalance(t *testing.T, addr std.Address, expectedBal int64) { t.Helper() - banker := std.GetBanker(std.BankerTypeReadonly) coins := banker.GetCoins(addr) got := coins.AmountOf("ugnot") From 470e0e557fcebb0b2eb2d8d0606b1f9a15e76de8 Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Thu, 19 Jan 2023 19:28:06 +0100 Subject: [PATCH 6/7] chore: cleanup styling --- examples/gno.land/r/gnoland/faucet/faucet.gno | 7 ------- 1 file changed, 7 deletions(-) diff --git a/examples/gno.land/r/gnoland/faucet/faucet.gno b/examples/gno.land/r/gnoland/faucet/faucet.gno index 2e4daa7214a..6adb38154bc 100644 --- a/examples/gno.land/r/gnoland/faucet/faucet.gno +++ b/examples/gno.land/r/gnoland/faucet/faucet.gno @@ -11,12 +11,9 @@ import ( var ( // configurable by admin. gAdminAddr std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") - gControllers = avl.NewMutTree() gControllersSize = 10 // limit it to 10 - gInPause = false - gMessage = "# Community Faucet.\n\n" // internal vars, for stats. @@ -73,11 +70,8 @@ func Render(path string) string { output += ufmt.Sprintf("Controllers:\n\n ") for i := 0; i < gControllersSize; i++ { - _, v := gControllers.GetByIndex(i) - output += ufmt.Sprintf("%s ", v.(std.Address)) - } output += "\n\n" @@ -90,7 +84,6 @@ func assertIsController() error { caller := std.GetOrigCaller() ok := gControllers.Has(caller.String()) - if !ok { return errors.New(caller.String() + " is not on the controller list") } From 65e28704b13741b6799eb410ccc8ab1c279298ae Mon Sep 17 00:00:00 2001 From: piux2 <> Date: Fri, 20 Jan 2023 19:42:29 -0800 Subject: [PATCH 7/7] use updated val tree --- examples/gno.land/r/gnoland/faucet/faucet.gno | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/gno.land/r/gnoland/faucet/faucet.gno b/examples/gno.land/r/gnoland/faucet/faucet.gno index 6adb38154bc..c68f6d203f0 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.NewMutTree() - gControllersSize = 10 // limit it to 10 - gInPause = false - gMessage = "# Community Faucet.\n\n" + gAdminAddr std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + gControllers = avl.NewTree() + gControllersSize = 10 // limit it to 10 + gInPause = false + gMessage = "# Community Faucet.\n\n" // internal vars, for stats. gTotalTransferred std.Coins