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/p/demo/teritori/worx/worxs_test.gno b/examples/gno.land/p/demo/teritori/worx/worxs_test.gno new file mode 100644 index 00000000000..ec3efab0e82 --- /dev/null +++ b/examples/gno.land/p/demo/teritori/worx/worxs_test.gno @@ -0,0 +1,63 @@ +package worx + +import ( + "testing" + "gno.land/p/demo/rand" + "gno.land/p/demo/testutils" + "gno.land/p/demo/ufmt" + "std" +) + +func TestAddGet(t *testing.T) { + keeper := NewWorxKeeper() + user1 := testutils.TestAddress("user1") + if len(keeper.Get()) != 0{ + t.Fatalf("Keeper is not empty initialized") + } + + fillRandomWorx(keeper, 10123423, user1) + if len(keeper.Get()) != 1{ + t.Fatalf("Keeper Worx was not added to keeper 1") + } + + fillRandomWorx(keeper, 10123423, user1) + if len(keeper.Get()) != 2{ + t.Fatalf("Keeper Worx was not added to keeper 2") + } +} + +func TestGetFromDate(t *testing.T) { + keeper := NewWorxKeeper() + user1 := testutils.TestAddress("user1") + if len(keeper.Get()) != 0{ + t.Fatalf("Keeper is not empty initialized") + } + + for i:=0; i < 100; i++ { + fillRandomWorx(keeper, int64(i*10), user1) + } + + if len(keeper.Get()) != 100{ + t.Fatalf("Keeper Worx was not totally added") + } + + if len(keeper.GetFromDate(1003)) != 0 { + t.Fatalf("Get From Date Should have found 0 registers") + } + + if len(keeper.GetFromDate(903)) != 9 { + t.Fatalf("Get From Date Should have found 9 registers") + } + +} + +func fillRandomWorx(keeper *WorxKeeper, timestamp int64, address std.Address){ + r := rand.New() + keeper.Store(NewWorx( + r.Intn(25), + "somekey", + address, + r.Intn(100), + timestamp, + )) +} \ No newline at end of file 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) +}