diff --git a/examples/gno.land/r/demo/teritori/worx_aggregator/admin.gno b/examples/gno.land/r/demo/teritori/worx_aggregator/admin.gno new file mode 100644 index 00000000000..bbc740e2124 --- /dev/null +++ b/examples/gno.land/r/demo/teritori/worx_aggregator/admin.gno @@ -0,0 +1,22 @@ +package worx_aggregator + +import ( + "std" +) + +var adminAddr std.Address + +func assertIsAdmin() { + if std.PrevRealm().Addr() != adminAddr { + panic("restricted area") + } +} + +func setAdminAddress(address std.Address) { + adminAddr = address +} + +func SetAdminAddress(address std.Address) { + assertIsAdmin() + setAdminAddress(address) +} diff --git a/examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno b/examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno index 99432f3c1bd..8b4260614be 100644 --- a/examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno +++ b/examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno @@ -8,11 +8,16 @@ import ( ) var ( - admin std.Address - registeredProviders avl.Tree - worxByAddress avl.Tree + registeredProviders *avl.Tree + worxByAddress *avl.Tree ) +func init() { + registeredProviders = avl.NewTree() + worxByAddress = avl.NewTree() + setAdminAddress(std.PrevRealm().Addr()) +} + func Push(hours int, metadata string, addr std.Address, points int, timestamp int64) { assertRegistered(addr) @@ -35,9 +40,9 @@ func getKeeper(addr std.Address) *worx.WorxKeeper { } func RegisterDataProvider(addr std.Address) { - assertAdmin() + assertIsAdmin() _, ok := registeredProviders.Get(string(addr)) - if !ok { + if ok { panic("Data provider already registered") } registeredProviders.Set(string(addr), 0) @@ -50,9 +55,3 @@ func assertRegistered(addr std.Address) { panic("caller realm is not registered as provider") } } - -func assertAdmin() { - if std.PrevRealm().Addr() != admin { - panic("unathorized") - } -} diff --git a/examples/gno.land/r/demo/teritori/worx_aggregator/worx_test.gno b/examples/gno.land/r/demo/teritori/worx_aggregator/worx_test.gno index 21ce9b75639..70ed2873666 100644 --- a/examples/gno.land/r/demo/teritori/worx_aggregator/worx_test.gno +++ b/examples/gno.land/r/demo/teritori/worx_aggregator/worx_test.gno @@ -1,11 +1,14 @@ package worx_aggregator import ( + "std" "testing" + + "gno.land/p/demo/avl" "gno.land/p/demo/testutils" ) -func TestPushCallerNotRegistered(t * testing.T){ +func TestPushCallerNotRegistered(t *testing.T) { user1 := testutils.TestAddress("user1") defer func() { if v := recover(); v == nil { @@ -14,3 +17,22 @@ func TestPushCallerNotRegistered(t * testing.T){ }() Push(1, "", user1, 3, 10) } + +func TestHappyPath(t *testing.T) { + registeredProviders = avl.NewTree() + worxByAddress = avl.NewTree() + + realm := testutils.TestAddress("realm") + admin := testutils.TestAddress("admin") + user1 := testutils.TestAddress("user1") + + setAdminAddress(admin) + + std.TestSetOrigCaller(admin) + + RegisterDataProvider(realm) + + std.TestSetOrigCaller(realm) + + Push(1, "", user1, 3, 10) +}