From b5f83065dc8207e679f3df53067b97a3607634b5 Mon Sep 17 00:00:00 2001 From: Omar Sy Date: Sun, 21 Apr 2024 18:27:58 +0200 Subject: [PATCH] feat: add aggregator --- .../gno.land/p/demo/teritori/worx/worxs.gno | 11 ++- .../teritori/worx_aggregator/aggregator.gno | 17 ++++ .../r/demo/teritori/worx_aggregator/gno.mod | 5 ++ .../r/demo/teritori/worx_aggregator/worx.gno | 84 ++++++++++--------- 4 files changed, 73 insertions(+), 44 deletions(-) create mode 100644 examples/gno.land/r/demo/teritori/worx_aggregator/aggregator.gno create mode 100644 examples/gno.land/r/demo/teritori/worx_aggregator/gno.mod diff --git a/examples/gno.land/p/demo/teritori/worx/worxs.gno b/examples/gno.land/p/demo/teritori/worx/worxs.gno index 06ab66eec3d..725841afb44 100644 --- a/examples/gno.land/p/demo/teritori/worx/worxs.gno +++ b/examples/gno.land/p/demo/teritori/worx/worxs.gno @@ -1,5 +1,6 @@ package worx -import( + +import ( "gno.land/p/demo/avl" "gno.land/p/demo/ufmt" ) @@ -7,13 +8,15 @@ import( type WorxKeeper struct { worxs *avl.Tree } + const dayToSeconds = 1 -func NewWorxKeeper() *WorxKeeper{ +func NewWorxKeeper() *WorxKeeper { return &WorxKeeper{ worxs: avl.NewTree(), } } + func (keeper *WorxKeeper) Store(worx *Worx) { key := ufmt.Sprintf("%d", worx.Timestamp) worxsInterface, exists := keeper.worxs.Get(key) @@ -44,7 +47,7 @@ func (keeper *WorxKeeper) GetFromDate(date int64) []*Worx { keeper.worxs.Iterate(start, "", func(key string, value interface{}) bool { // here we account for string comparation "95" > "105" because comparing first character 9 > 1 (Comparation of lenght) // and simply comparing same lenght string 25 > 12 this in order to stop tree for iterating over lower dates leaves - if len(start) > len(key) || start > key { + if len(start) > len(key) || start > key { return true } worxByDay := value.([]*Worx) @@ -53,4 +56,4 @@ func (keeper *WorxKeeper) GetFromDate(date int64) []*Worx { }) return totalWorx -} \ No newline at end of file +} diff --git a/examples/gno.land/r/demo/teritori/worx_aggregator/aggregator.gno b/examples/gno.land/r/demo/teritori/worx_aggregator/aggregator.gno new file mode 100644 index 00000000000..07c5727f76a --- /dev/null +++ b/examples/gno.land/r/demo/teritori/worx_aggregator/aggregator.gno @@ -0,0 +1,17 @@ +package worx_aggregator + +import ( + "std" + + "gno.land/p/demo/teritori/worx" +) + +func GetWorx(addr std.Address) []*worx.Worx { + keeper := getKeeper(addr) + return keeper.Get() +} + +func GetWorxFromDate(addr std.Address, date int64) []*worx.Worx { + keeper := getKeeper(addr) + return keeper.GetFromDate(date) +} diff --git a/examples/gno.land/r/demo/teritori/worx_aggregator/gno.mod b/examples/gno.land/r/demo/teritori/worx_aggregator/gno.mod new file mode 100644 index 00000000000..701b2f6b6cd --- /dev/null +++ b/examples/gno.land/r/demo/teritori/worx_aggregator/gno.mod @@ -0,0 +1,5 @@ +module gno.land/r/demo/teritori/worx_aggregator + +require ( + gno.land/p/demo/teritori/worx v0.0.0-latest +) 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 3b853bc6d11..99432f3c1bd 100644 --- a/examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno +++ b/examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno @@ -1,54 +1,58 @@ package worx_aggregator -import( +import ( + "std" + "gno.land/p/demo/avl" - "gno.land/p/demo/teritori/worx" - "std" + "gno.land/p/demo/teritori/worx" +) + +var ( + admin std.Address + registeredProviders avl.Tree + worxByAddress avl.Tree ) -var admin std.Address -var registeredProviders avl.Tree -var worxByAddress avl.Tree func Push(hours int, metadata string, addr std.Address, points int, timestamp int64) { - prevRealm := std.PrevRealm().Addr() - dataProviders, ok := registeredProviders.Get(string(prevRealm)) - if !ok { - panic("caller realm is not registered as provider") - } - keeper := getKeeper(addr) - keeper.Store(worx.NewWorx(hours, metadata, addr, points, timestamp)) - worxByAddress.Set(string(addr), keeper) - - std.Emit("worx_added", - "addr",string(addr), - "metadata", metadata, - ) -} + assertRegistered(addr) -func getKeeper(addr std.Address) *worx.WorxKeeper{ - data, ok := worxByAddress.Get(string(addr)) - if ok { - return data.(*worx.WorxKeeper) - } - return &worx.WorxKeeper{} -} + keeper := getKeeper(addr) + keeper.Store(worx.NewWorx(hours, metadata, addr, points, timestamp)) + worxByAddress.Set(string(addr), keeper) -func Get(dataType string, addr std.Address) []*worx.Worx { - return getKeeper(addr).Get() + std.Emit("worx_added", + "addr", string(addr), + "metadata", metadata, + ) } +func getKeeper(addr std.Address) *worx.WorxKeeper { + data, ok := worxByAddress.Get(string(addr)) + if ok { + return data.(*worx.WorxKeeper) + } + return &worx.WorxKeeper{} +} func RegisterDataProvider(addr std.Address) { - assertAdmin() - _, ok :=registeredProviders.Get(string(addr)) - if !ok { - panic("Data provider already registered") - } - registeredProviders.Set(string(addr), 0) + assertAdmin() + _, ok := registeredProviders.Get(string(addr)) + if !ok { + panic("Data provider already registered") + } + registeredProviders.Set(string(addr), 0) +} + +func assertRegistered(addr std.Address) { + prevRealm := std.PrevRealm().Addr() + dataProviders, ok := registeredProviders.Get(string(prevRealm)) + if !ok { + panic("caller realm is not registered as provider") + } } -func assertAdmin(){ - if (std.PrevRealm().Addr() != admin) { - panic("unathorized") - } -} \ No newline at end of file +func assertAdmin() { + if std.PrevRealm().Addr() != admin { + panic("unathorized") + } +}