Use storage service in events updater

This commit is contained in:
lash 2025-01-13 21:39:15 +00:00
parent fcce60e031
commit 63eb803022
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
6 changed files with 75 additions and 41 deletions

View File

@ -19,7 +19,6 @@ import (
apievent "git.grassecon.net/grassrootseconomics/sarafu-api/event" apievent "git.grassecon.net/grassrootseconomics/sarafu-api/event"
"git.grassecon.net/grassrootseconomics/sarafu-vise/args" "git.grassecon.net/grassrootseconomics/sarafu-vise/args"
"git.grassecon.net/grassrootseconomics/sarafu-vise/handlers" "git.grassecon.net/grassrootseconomics/sarafu-vise/handlers"
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
"git.grassecon.net/grassrootseconomics/sarafu-vise/handlers/event" "git.grassecon.net/grassrootseconomics/sarafu-vise/handlers/event"
) )
@ -158,10 +157,7 @@ func main() {
if fakeDir != "" { if fakeDir != "" {
svc := devremote.NewDevAccountService(ctx, fakeDir).WithAutoVoucher(ctx, "FOO", 42) svc := devremote.NewDevAccountService(ctx, fakeDir).WithAutoVoucher(ctx, "FOO", 42)
userStore := &store.UserDataStore{ eu := event.NewEventsUpdater(svc, menuStorageService)
Db: userdatastore,
}
eu := event.NewEventsUpdater(svc, userStore, pe)
emitter := &devEmitter{ emitter := &devEmitter{
h: eu.ToEventsHandler(), h: eu.ToEventsHandler(),
} }

4
go.mod
View File

@ -5,8 +5,8 @@ go 1.23.4
require ( require (
git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d
git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05 git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05
git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250113201755-37bb46056123 git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250113213645-7c697394b5e7
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113103142-5bf0a0e85893 git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113213325-5228aef0889b
git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250113103030-f0b2056fd87d git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250113103030-f0b2056fd87d
github.com/alecthomas/assert/v2 v2.2.2 github.com/alecthomas/assert/v2 v2.2.2
github.com/gofrs/uuid v4.4.0+incompatible github.com/gofrs/uuid v4.4.0+incompatible

8
go.sum
View File

@ -2,10 +2,10 @@ git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d h1:bPAOVZOX4frSG
git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck= git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck=
git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05 h1:BenzGx6aDHKDwE23/mWIFD2InYIXyzHroZWV3MF5WUk= git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05 h1:BenzGx6aDHKDwE23/mWIFD2InYIXyzHroZWV3MF5WUk=
git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05/go.mod h1:wgQJZGIS6QuNLHqDhcsvehsbn5PvgV7aziRebMnJi60= git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05/go.mod h1:wgQJZGIS6QuNLHqDhcsvehsbn5PvgV7aziRebMnJi60=
git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250113201755-37bb46056123 h1:O8+f/S0cST2EX/mSsoBM+Jja2QdIyawJSTWCy6oiBF8= git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250113213645-7c697394b5e7 h1:P+Bi5jcQbnCrLnKNC5k8XDTBsocyZTVifqDyZY6d0w8=
git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250113201755-37bb46056123/go.mod h1:e9Damfk0euyjWzn+CE8uxgg8p4ggd07wp99SOyEhGXY= git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250113213645-7c697394b5e7/go.mod h1:X9aQF93xw3vcW2QftJfzLewvbotRM0U00DRtdmFw294=
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113103142-5bf0a0e85893 h1:MyDINzwY1sjfXkIFoc+6T5lXF/1xdFV6yjHrpSNZzWM= git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113213325-5228aef0889b h1:6SieNUSEKbkjzquuwazs/lVG56zdEWF10zQQEMRJfMs=
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113103142-5bf0a0e85893/go.mod h1:E6W7ZOa7ZvVr0Bc5ot0LNSwpSPYq4hXlAIvEPy3AJ7U= git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113213325-5228aef0889b/go.mod h1:E6W7ZOa7ZvVr0Bc5ot0LNSwpSPYq4hXlAIvEPy3AJ7U=
git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250113103030-f0b2056fd87d h1:q/NO1rEgK3pia32D/tCq5hXfEuJp84COZRwceFvy/MM= git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250113103030-f0b2056fd87d h1:q/NO1rEgK3pia32D/tCq5hXfEuJp84COZRwceFvy/MM=
git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250113103030-f0b2056fd87d/go.mod h1:AH15xABcvaJr1TCGlih3oGSuwWC0E5IdbHQwuu+E1KI= git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250113103030-f0b2056fd87d/go.mod h1:AH15xABcvaJr1TCGlih3oGSuwWC0E5IdbHQwuu+E1KI=
github.com/alecthomas/assert/v2 v2.2.2 h1:Z/iVC0xZfWTaFNE6bA3z07T86hd45Xe2eLt6WVy2bbk= github.com/alecthomas/assert/v2 v2.2.2 h1:Z/iVC0xZfWTaFNE6bA3z07T86hd45Xe2eLt6WVy2bbk=

View File

@ -26,16 +26,20 @@ func (eh *EventsUpdater) handleCustodialRegistration(ctx context.Context, ev any
return eh.HandleCustodialRegistration(ctx, o) return eh.HandleCustodialRegistration(ctx, o)
} }
func (eh *EventsUpdater) HandleCustodialRegistration(ctx context.Context, ev *apievent.EventCustodialRegistration) error { func (eu *EventsUpdater) HandleCustodialRegistration(ctx context.Context, ev *apievent.EventCustodialRegistration) error {
identity, err := store.IdentityFromAddress(ctx, eh.store, ev.Account) pe, userStore, err := eu.getStore(ctx)
if err != nil { if err != nil {
return err return err
} }
err = eh.pe.Load(identity.SessionId) identity, err := store.IdentityFromAddress(ctx, userStore, ev.Account)
if err != nil { if err != nil {
return err return err
} }
st := eh.pe.GetState() err = pe.Load(identity.SessionId)
if err != nil {
return err
}
st := pe.GetState()
st.SetFlag(accountCreatedFlag) st.SetFlag(accountCreatedFlag)
return eh.pe.Save(identity.SessionId) return pe.Save(identity.SessionId)
} }

View File

@ -1,29 +1,34 @@
package event package event
import ( import (
"context"
"fmt" "fmt"
"git.defalsify.org/vise.git/persist" "git.defalsify.org/vise.git/persist"
"git.defalsify.org/vise.git/logging"
"git.grassecon.net/grassrootseconomics/visedriver/storage"
"git.grassecon.net/grassrootseconomics/sarafu-vise/store" "git.grassecon.net/grassrootseconomics/sarafu-vise/store"
"git.grassecon.net/grassrootseconomics/sarafu-api/remote" "git.grassecon.net/grassrootseconomics/sarafu-api/remote"
apievent "git.grassecon.net/grassrootseconomics/sarafu-api/event" apievent "git.grassecon.net/grassrootseconomics/sarafu-api/event"
) )
var (
logg = logging.NewVanilla().WithDomain("sarafu-vise.handlers.event")
)
type EventsUpdater struct { type EventsUpdater struct {
api remote.AccountService api remote.AccountService
formatFunc func(string, int, any) string formatFunc func(string, int, any) string
store *store.UserDataStore store storage.StorageService
pe *persist.Persister
} }
func NewEventsUpdater(api remote.AccountService, store *store.UserDataStore, pe *persist.Persister) *EventsUpdater { func NewEventsUpdater(api remote.AccountService, store storage.StorageService) *EventsUpdater {
return &EventsUpdater{ return &EventsUpdater{
api: api, api: api,
formatFunc: func(tag string, i int, o any) string { formatFunc: func(tag string, i int, o any) string {
return fmt.Sprintf("%d %v", i, o) return fmt.Sprintf("%d %v", i, o)
}, },
store: store, store: store,
pe: pe,
} }
} }
@ -31,5 +36,26 @@ func (eu *EventsUpdater) ToEventsHandler() *apievent.EventsHandler {
eh := apievent.NewEventsHandler() eh := apievent.NewEventsHandler()
eh = eh.WithHandler(apievent.EventTokenTransferTag, eu.handleTokenTransfer) eh = eh.WithHandler(apievent.EventTokenTransferTag, eu.handleTokenTransfer)
eh = eh.WithHandler(apievent.EventRegistrationTag, eu.handleCustodialRegistration) eh = eh.WithHandler(apievent.EventRegistrationTag, eu.handleCustodialRegistration)
eh = eh.WithHandler(apievent.EventTokenMintTag, eu.handleNoop)
return eh return eh
} }
func (eu *EventsUpdater) handleNoop(ctx context.Context, ev any) error {
logg.WarnCtxf(ctx, "noop event handler")
return nil
}
func (eu *EventsUpdater) getStore(ctx context.Context) (*persist.Persister, *store.UserDataStore, error) {
userDb, err := eu.store.GetUserdataDb(ctx)
if err != nil {
return nil, nil, err
}
userStore := &store.UserDataStore{
Db: userDb,
}
pr, err := eu.store.GetPersister(ctx)
if err != nil {
return nil, nil, err
}
return pr, userStore, nil
}

View File

@ -13,14 +13,14 @@ import (
) )
// execute all // execute all
func (eu *EventsUpdater) updateToken(ctx context.Context, identity identity.Identity, tokenAddress string) error { func (eu *EventsUpdater) updateToken(ctx context.Context, identity identity.Identity, userStore *store.UserDataStore, tokenAddress string) error {
err := eu.updateTokenList(ctx, identity) err := eu.updateTokenList(ctx, identity, userStore)
if err != nil { if err != nil {
return err return err
} }
eu.store.Db.SetSession(identity.SessionId) userStore.Db.SetSession(identity.SessionId)
activeSym, err := eu.store.ReadEntry(ctx, identity.SessionId, storedb.DATA_ACTIVE_SYM) activeSym, err := userStore.ReadEntry(ctx, identity.SessionId, storedb.DATA_ACTIVE_SYM)
if err == nil { if err == nil {
return nil return nil
} }
@ -34,12 +34,12 @@ func (eu *EventsUpdater) updateToken(ctx context.Context, identity identity.Iden
} }
} }
err = eu.updateDefaultToken(ctx, identity, string(activeSym)) err = eu.updateDefaultToken(ctx, identity, userStore, string(activeSym))
if err != nil { if err != nil {
return err return err
} }
err = eu.updateTokenTransferList(ctx, identity) err = eu.updateTokenTransferList(ctx, identity, userStore)
if err != nil { if err != nil {
return err return err
} }
@ -49,14 +49,14 @@ func (eu *EventsUpdater) updateToken(ctx context.Context, identity identity.Iden
// set default token to given symbol. // set default token to given symbol.
func (eu *EventsUpdater) updateDefaultToken(ctx context.Context, identity identity.Identity, activeSym string) error { func (eu *EventsUpdater) updateDefaultToken(ctx context.Context, identity identity.Identity, userStore *store.UserDataStore, activeSym string) error {
pfxDb := toPrefixDb(eu.store, identity.SessionId) pfxDb := toPrefixDb(userStore, identity.SessionId)
// TODO: the activeSym input should instead be newline separated list? // TODO: the activeSym input should instead be newline separated list?
tokenData, err := store.GetVoucherData(ctx, pfxDb, activeSym) tokenData, err := store.GetVoucherData(ctx, pfxDb, activeSym)
if err != nil { if err != nil {
return err return err
} }
return store.UpdateVoucherData(ctx, eu.store, identity.SessionId, tokenData) return store.UpdateVoucherData(ctx, userStore, identity.SessionId, tokenData)
} }
@ -71,26 +71,30 @@ func (eh *EventsUpdater) handleTokenTransfer(ctx context.Context, ev any) error
return eh.HandleTokenTransfer(ctx, o) return eh.HandleTokenTransfer(ctx, o)
} }
func (eu *EventsUpdater) HandleTokenTransfer(ctx context.Context, ev *apievent.EventTokenTransfer) error { func (eu *EventsUpdater) HandleTokenTransfer(ctx context.Context, ev *apievent.EventTokenTransfer) error {
identity, err := store.IdentityFromAddress(ctx, eu.store, ev.From) _, userStore, err := eu.getStore(ctx)
if err != nil {
return err
}
identity, err := store.IdentityFromAddress(ctx, userStore, ev.From)
if err != nil { if err != nil {
if !db.IsNotFound(err) { if !db.IsNotFound(err) {
return err return err
} }
} else { } else {
err = eu.updateToken(ctx, identity, ev.VoucherAddress) err = eu.updateToken(ctx, identity, userStore, ev.VoucherAddress)
if err != nil { if err != nil {
return err return err
} }
} }
if strings.Compare(ev.To, ev.From) != 0 { if strings.Compare(ev.To, ev.From) != 0 {
identity, err = store.IdentityFromAddress(ctx, eu.store, ev.To) identity, err = store.IdentityFromAddress(ctx, userStore, ev.To)
if err != nil { if err != nil {
if !db.IsNotFound(err) { if !db.IsNotFound(err) {
return err return err
} }
} else { } else {
err = eu.updateToken(ctx, identity, ev.VoucherAddress) err = eu.updateToken(ctx, identity, userStore, ev.VoucherAddress)
if err != nil { if err != nil {
return err return err
} }
@ -102,13 +106,17 @@ func (eu *EventsUpdater) HandleTokenTransfer(ctx context.Context, ev *apievent.E
// handle token mint. // handle token mint.
func (eu *EventsUpdater) HandleTokenMint(ctx context.Context, ev *apievent.EventTokenMint) error { func (eu *EventsUpdater) HandleTokenMint(ctx context.Context, ev *apievent.EventTokenMint) error {
identity, err := store.IdentityFromAddress(ctx, eu.store, ev.To) _, userStore, err := eu.getStore(ctx)
if err != nil {
return err
}
identity, err := store.IdentityFromAddress(ctx, userStore, ev.To)
if err != nil { if err != nil {
if !db.IsNotFound(err) { if !db.IsNotFound(err) {
return err return err
} }
} else { } else {
err = eu.updateToken(ctx, identity, ev.VoucherAddress) err = eu.updateToken(ctx, identity, userStore, ev.VoucherAddress)
if err != nil { if err != nil {
return err return err
} }
@ -117,8 +125,8 @@ func (eu *EventsUpdater) HandleTokenMint(ctx context.Context, ev *apievent.Event
} }
// use api to resolve address to token symbol. // use api to resolve address to token symbol.
func (ev *EventsUpdater) toSym(ctx context.Context, address string) ([]byte, error) { func (eu *EventsUpdater) toSym(ctx context.Context, address string) ([]byte, error) {
voucherData, err := ev.api.VoucherData(ctx, address) voucherData, err := eu.api.VoucherData(ctx, address)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -126,7 +134,7 @@ func (ev *EventsUpdater) toSym(ctx context.Context, address string) ([]byte, err
} }
// refresh and store token list. // refresh and store token list.
func (eu *EventsUpdater) updateTokenList(ctx context.Context, identity identity.Identity) error { func (eu *EventsUpdater) updateTokenList(ctx context.Context, identity identity.Identity, userStore *store.UserDataStore) error {
holdings, err := eu.api.FetchVouchers(ctx, identity.ChecksumAddress) holdings, err := eu.api.FetchVouchers(ctx, identity.ChecksumAddress)
if err != nil { if err != nil {
return err return err
@ -136,7 +144,7 @@ func (eu *EventsUpdater) updateTokenList(ctx context.Context, identity identity.
// TODO: make sure subprefixdb is thread safe when using gdbm // TODO: make sure subprefixdb is thread safe when using gdbm
// TODO: why is address session here unless explicitly set // TODO: why is address session here unless explicitly set
pfxDb := toPrefixDb(eu.store, identity.SessionId) pfxDb := toPrefixDb(userStore, identity.SessionId)
typ := storedb.ToBytes(storedb.DATA_VOUCHER_SYMBOLS) typ := storedb.ToBytes(storedb.DATA_VOUCHER_SYMBOLS)
err = pfxDb.Put(ctx, typ, []byte(metadata.Symbols)) err = pfxDb.Put(ctx, typ, []byte(metadata.Symbols))
@ -166,7 +174,7 @@ func (eu *EventsUpdater) updateTokenList(ctx context.Context, identity identity.
} }
// refresh and store transaction history. // refresh and store transaction history.
func (eu *EventsUpdater) updateTokenTransferList(ctx context.Context, identity identity.Identity) error { func (eu *EventsUpdater) updateTokenTransferList(ctx context.Context, identity identity.Identity, userStore *store.UserDataStore) error {
var r []string var r []string
txs, err := eu.api.FetchTransactions(ctx, identity.ChecksumAddress) txs, err := eu.api.FetchTransactions(ctx, identity.ChecksumAddress)
@ -180,7 +188,7 @@ func (eu *EventsUpdater) updateTokenTransferList(ctx context.Context, identity i
s := strings.Join(r, "\n") s := strings.Join(r, "\n")
return eu.store.WriteEntry(ctx, identity.SessionId, storedb.DATA_TRANSACTIONS, []byte(s)) return userStore.WriteEntry(ctx, identity.SessionId, storedb.DATA_TRANSACTIONS, []byte(s))
} }
func toPrefixDb(userStore *store.UserDataStore, sessionId string) storedb.PrefixDb { func toPrefixDb(userStore *store.UserDataStore, sessionId string) storedb.PrefixDb {