diff --git a/examples/gno.land/p/demo/teritori/worx/worxs.gno b/examples/gno.land/p/demo/teritori/worx/worxs.gno index eaa2835133d..fb2dfae7e87 100644 --- a/examples/gno.land/p/demo/teritori/worx/worxs.gno +++ b/examples/gno.land/p/demo/teritori/worx/worxs.gno @@ -3,10 +3,11 @@ package worx import ( "gno.land/p/demo/avl" "gno.land/p/demo/ufmt" + "time" ) type WorxKeeper struct { - worxs []*Worx + worxs *avl.Tree } const dayToSeconds = 1 @@ -18,7 +19,16 @@ func NewWorxKeeper() *WorxKeeper { } func (keeper *WorxKeeper) Store(worx *Worx) { - keeper.worxs = append(keeper.worxs, worx) + storekey := ufmt.Sprintf("%d", time.Now().Unix()) + worxs, ok:= keeper.worxs.Get(storekey) + if ok { + worxsOnDay := worxs.([]*Worx) + worxsOnDay = append(worxsOnDay, worx) + keeper.worxs.Set(storekey, worxsOnDay) + return + } + + keeper.worxs.Set(storekey, []*Worx{worx}) } func (keeper *WorxKeeper) Get() []*Worx { 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..eaf8d58efe3 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) @@ -31,13 +36,13 @@ func getKeeper(addr std.Address) *worx.WorxKeeper { if ok { return data.(*worx.WorxKeeper) } - return &worx.WorxKeeper{} + return worx.NewWorxKeeper() } 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) +}