Move out event handler encapsulation to api module

This commit is contained in:
lash 2025-01-13 20:19:34 +00:00
parent 0d47c096e0
commit fcce60e031
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
6 changed files with 82 additions and 56 deletions

View File

@ -10,7 +10,6 @@ import (
"git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/logging"
"git.defalsify.org/vise.git/resource"
"git.defalsify.org/vise.git/persist"
"git.defalsify.org/vise.git/lang"
"git.grassecon.net/grassrootseconomics/sarafu-vise/config"
"git.grassecon.net/grassrootseconomics/visedriver/storage"
@ -31,12 +30,9 @@ var (
)
type devEmitter struct {
h *event.EventsHandler
store *store.UserDataStore
pe *persist.Persister
h *apievent.EventsHandler
}
// WIP: placeholder emitter, should perform same action as events
func (d *devEmitter) emit(ctx context.Context, msg apievent.Msg) error {
var err error
if msg.Typ == apievent.EventTokenTransferTag {
@ -46,7 +42,7 @@ func (d *devEmitter) emit(ctx context.Context, msg apievent.Msg) error {
}
logg.InfoCtxf(ctx, "tx emit", "tx", tx)
ev := tx.ToTransferEvent()
err = d.h.HandleTokenTransfer(ctx, d.store, &ev)
err = d.h.Handle(ctx, apievent.EventTokenTransferTag, &ev)
} else if msg.Typ == apievent.EventRegistrationTag {
acc, ok := msg.Item.(devremote.Account)
if !ok {
@ -54,7 +50,7 @@ func (d *devEmitter) emit(ctx context.Context, msg apievent.Msg) error {
}
logg.InfoCtxf(ctx, "account emit", "account", acc)
ev := acc.ToRegistrationEvent()
err = d.h.HandleCustodialRegistration(ctx, d.store, d.pe, &ev)
err = d.h.Handle(ctx, apievent.EventRegistrationTag, &ev)
}
return err
}
@ -162,11 +158,12 @@ func main() {
if fakeDir != "" {
svc := devremote.NewDevAccountService(ctx, fakeDir).WithAutoVoucher(ctx, "FOO", 42)
emitter := &devEmitter{
h: event.NewEventsHandler(svc),
store: &store.UserDataStore{
userStore := &store.UserDataStore{
Db: userdatastore,
},
}
eu := event.NewEventsUpdater(svc, userStore, pe)
emitter := &devEmitter{
h: eu.ToEventsHandler(),
}
svc = svc.WithEmitter(emitter.emit)
svc.AddVoucher(ctx, "BAR")

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.23.4
require (
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/sarafu-api v0.0.0-20250113181351-551195543805
git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250113201755-37bb46056123
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113103142-5bf0a0e85893
git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250113103030-f0b2056fd87d
github.com/alecthomas/assert/v2 v2.2.2

4
go.sum
View File

@ -2,8 +2,8 @@ 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.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/sarafu-api v0.0.0-20250113181351-551195543805 h1:Okih7rAAjD8cpv7pi8QQc36mmp7SRBnOc7dc125GP/E=
git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250113181351-551195543805/go.mod h1:e9Damfk0euyjWzn+CE8uxgg8p4ggd07wp99SOyEhGXY=
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-20250113201755-37bb46056123/go.mod h1:e9Damfk0euyjWzn+CE8uxgg8p4ggd07wp99SOyEhGXY=
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.20250113103142-5bf0a0e85893/go.mod h1:E6W7ZOa7ZvVr0Bc5ot0LNSwpSPYq4hXlAIvEPy3AJ7U=
git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250113103030-f0b2056fd87d h1:q/NO1rEgK3pia32D/tCq5hXfEuJp84COZRwceFvy/MM=

View File

@ -2,8 +2,8 @@ package event
import (
"context"
"fmt"
"git.defalsify.org/vise.git/persist"
apievent "git.grassecon.net/grassrootseconomics/sarafu-api/event"
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
)
@ -18,16 +18,24 @@ const (
// TODO: implement account created in userstore instead, so that
// the need for persister and state use here is eliminated (it
// introduces concurrency risks)
func (eh *EventsHandler) HandleCustodialRegistration(ctx context.Context, userStore *store.UserDataStore, pr *persist.Persister, ev *apievent.EventCustodialRegistration) error {
identity, err := store.IdentityFromAddress(ctx, userStore, ev.Account)
if err != nil {
return err
func (eh *EventsUpdater) handleCustodialRegistration(ctx context.Context, ev any) error {
o, ok := ev.(*apievent.EventCustodialRegistration)
if !ok {
fmt.Errorf("invalid event for custodial registration")
}
err = pr.Load(identity.SessionId)
if err != nil {
return err
}
st := pr.GetState()
st.SetFlag(accountCreatedFlag)
return pr.Save(identity.SessionId)
return eh.HandleCustodialRegistration(ctx, o)
}
func (eh *EventsUpdater) HandleCustodialRegistration(ctx context.Context, ev *apievent.EventCustodialRegistration) error {
identity, err := store.IdentityFromAddress(ctx, eh.store, ev.Account)
if err != nil {
return err
}
err = eh.pe.Load(identity.SessionId)
if err != nil {
return err
}
st := eh.pe.GetState()
st.SetFlag(accountCreatedFlag)
return eh.pe.Save(identity.SessionId)
}

View File

@ -3,19 +3,33 @@ package event
import (
"fmt"
"git.defalsify.org/vise.git/persist"
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
"git.grassecon.net/grassrootseconomics/sarafu-api/remote"
apievent "git.grassecon.net/grassrootseconomics/sarafu-api/event"
)
type EventsHandler struct {
type EventsUpdater struct {
api remote.AccountService
formatFunc func(string, int, any) string
store *store.UserDataStore
pe *persist.Persister
}
func NewEventsHandler(api remote.AccountService) *EventsHandler {
return &EventsHandler{
func NewEventsUpdater(api remote.AccountService, store *store.UserDataStore, pe *persist.Persister) *EventsUpdater {
return &EventsUpdater{
api: api,
formatFunc: func(tag string, i int, o any) string {
return fmt.Sprintf("%d %v", i, o)
},
store: store,
pe: pe,
}
}
func (eu *EventsUpdater) ToEventsHandler() *apievent.EventsHandler {
eh := apievent.NewEventsHandler()
eh = eh.WithHandler(apievent.EventTokenTransferTag, eu.handleTokenTransfer)
eh = eh.WithHandler(apievent.EventRegistrationTag, eu.handleCustodialRegistration)
return eh
}

View File

@ -2,6 +2,7 @@ package event
import (
"context"
"fmt"
"strings"
"git.defalsify.org/vise.git/db"
@ -12,14 +13,14 @@ import (
)
// execute all
func (eh *EventsHandler) updateToken(ctx context.Context, userStore *store.UserDataStore, identity identity.Identity, tokenAddress string) error {
err := eh.updateTokenList(ctx, userStore, identity)
func (eu *EventsUpdater) updateToken(ctx context.Context, identity identity.Identity, tokenAddress string) error {
err := eu.updateTokenList(ctx, identity)
if err != nil {
return err
}
userStore.Db.SetSession(identity.SessionId)
activeSym, err := userStore.ReadEntry(ctx, identity.SessionId, storedb.DATA_ACTIVE_SYM)
eu.store.Db.SetSession(identity.SessionId)
activeSym, err := eu.store.ReadEntry(ctx, identity.SessionId, storedb.DATA_ACTIVE_SYM)
if err == nil {
return nil
}
@ -27,18 +28,18 @@ func (eh *EventsHandler) updateToken(ctx context.Context, userStore *store.UserD
return err
}
if activeSym == nil {
activeSym, err = eh.toSym(ctx, tokenAddress)
activeSym, err = eu.toSym(ctx, tokenAddress)
if err != nil {
return err
}
}
err = updateDefaultToken(ctx, userStore, identity, string(activeSym))
err = eu.updateDefaultToken(ctx, identity, string(activeSym))
if err != nil {
return err
}
err = eh.updateTokenTransferList(ctx, userStore, identity)
err = eu.updateTokenTransferList(ctx, identity)
if err != nil {
return err
}
@ -48,41 +49,48 @@ func (eh *EventsHandler) updateToken(ctx context.Context, userStore *store.UserD
// set default token to given symbol.
func updateDefaultToken(ctx context.Context, userStore *store.UserDataStore, identity identity.Identity, activeSym string) error {
pfxDb := toPrefixDb(userStore, identity.SessionId)
func (eu *EventsUpdater) updateDefaultToken(ctx context.Context, identity identity.Identity, activeSym string) error {
pfxDb := toPrefixDb(eu.store, identity.SessionId)
// TODO: the activeSym input should instead be newline separated list?
tokenData, err := store.GetVoucherData(ctx, pfxDb, activeSym)
if err != nil {
return err
}
return store.UpdateVoucherData(ctx, userStore, identity.SessionId, tokenData)
return store.UpdateVoucherData(ctx, eu.store, identity.SessionId, tokenData)
}
// handle token transfer.
//
// if from and to are NOT the same, handle code will be executed once for each side of the transfer.
func (eh *EventsHandler) HandleTokenTransfer(ctx context.Context, userStore *store.UserDataStore, ev *apievent.EventTokenTransfer) error {
identity, err := store.IdentityFromAddress(ctx, userStore, ev.From)
func (eh *EventsUpdater) handleTokenTransfer(ctx context.Context, ev any) error {
o, ok := ev.(*apievent.EventTokenTransfer)
if !ok {
fmt.Errorf("invalid event for custodial registration")
}
return eh.HandleTokenTransfer(ctx, o)
}
func (eu *EventsUpdater) HandleTokenTransfer(ctx context.Context, ev *apievent.EventTokenTransfer) error {
identity, err := store.IdentityFromAddress(ctx, eu.store, ev.From)
if err != nil {
if !db.IsNotFound(err) {
return err
}
} else {
err = eh.updateToken(ctx, userStore, identity, ev.VoucherAddress)
err = eu.updateToken(ctx, identity, ev.VoucherAddress)
if err != nil {
return err
}
}
if strings.Compare(ev.To, ev.From) != 0 {
identity, err = store.IdentityFromAddress(ctx, userStore, ev.To)
identity, err = store.IdentityFromAddress(ctx, eu.store, ev.To)
if err != nil {
if !db.IsNotFound(err) {
return err
}
} else {
err = eh.updateToken(ctx, userStore, identity, ev.VoucherAddress)
err = eu.updateToken(ctx, identity, ev.VoucherAddress)
if err != nil {
return err
}
@ -93,14 +101,14 @@ func (eh *EventsHandler) HandleTokenTransfer(ctx context.Context, userStore *sto
}
// handle token mint.
func (eh *EventsHandler) HandleTokenMint(ctx context.Context, userStore *store.UserDataStore, ev *apievent.EventTokenMint) error {
identity, err := store.IdentityFromAddress(ctx, userStore, ev.To)
func (eu *EventsUpdater) HandleTokenMint(ctx context.Context, ev *apievent.EventTokenMint) error {
identity, err := store.IdentityFromAddress(ctx, eu.store, ev.To)
if err != nil {
if !db.IsNotFound(err) {
return err
}
} else {
err = eh.updateToken(ctx, userStore, identity, ev.VoucherAddress)
err = eu.updateToken(ctx, identity, ev.VoucherAddress)
if err != nil {
return err
}
@ -109,7 +117,7 @@ func (eh *EventsHandler) HandleTokenMint(ctx context.Context, userStore *store.U
}
// use api to resolve address to token symbol.
func (ev *EventsHandler) toSym(ctx context.Context, address string) ([]byte, error) {
func (ev *EventsUpdater) toSym(ctx context.Context, address string) ([]byte, error) {
voucherData, err := ev.api.VoucherData(ctx, address)
if err != nil {
return nil, err
@ -118,8 +126,8 @@ func (ev *EventsHandler) toSym(ctx context.Context, address string) ([]byte, err
}
// refresh and store token list.
func (eh *EventsHandler) updateTokenList(ctx context.Context, userStore *store.UserDataStore, identity identity.Identity) error {
holdings, err := eh.api.FetchVouchers(ctx, identity.ChecksumAddress)
func (eu *EventsUpdater) updateTokenList(ctx context.Context, identity identity.Identity) error {
holdings, err := eu.api.FetchVouchers(ctx, identity.ChecksumAddress)
if err != nil {
return err
}
@ -128,7 +136,7 @@ func (eh *EventsHandler) updateTokenList(ctx context.Context, userStore *store.U
// TODO: make sure subprefixdb is thread safe when using gdbm
// TODO: why is address session here unless explicitly set
pfxDb := toPrefixDb(userStore, identity.SessionId)
pfxDb := toPrefixDb(eu.store, identity.SessionId)
typ := storedb.ToBytes(storedb.DATA_VOUCHER_SYMBOLS)
err = pfxDb.Put(ctx, typ, []byte(metadata.Symbols))
@ -158,22 +166,21 @@ func (eh *EventsHandler) updateTokenList(ctx context.Context, userStore *store.U
}
// refresh and store transaction history.
func (eh *EventsHandler) updateTokenTransferList(ctx context.Context, userStore *store.UserDataStore, identity identity.Identity) error {
func (eu *EventsUpdater) updateTokenTransferList(ctx context.Context, identity identity.Identity) error {
var r []string
txs, err := eh.api.FetchTransactions(ctx, identity.ChecksumAddress)
txs, err := eu.api.FetchTransactions(ctx, identity.ChecksumAddress)
if err != nil {
return err
}
for i, tx := range(txs) {
//r = append(r, formatTransaction(i, tx))
r = append(r, eh.formatFunc(apievent.EventTokenTransferTag, i, tx))
r = append(r, eu.formatFunc(apievent.EventTokenTransferTag, i, tx))
}
s := strings.Join(r, "\n")
return userStore.WriteEntry(ctx, identity.SessionId, storedb.DATA_TRANSACTIONS, []byte(s))
return eu.store.WriteEntry(ctx, identity.SessionId, storedb.DATA_TRANSACTIONS, []byte(s))
}
func toPrefixDb(userStore *store.UserDataStore, sessionId string) storedb.PrefixDb {