Compare commits

..

3 Commits

Author SHA1 Message Date
lash
6f352e7cf2
Complete custodial registration hack 2024-11-03 23:53:36 +00:00
lash
6c4f91b0b8
Implement storageservice in nats subscription object 2024-11-03 23:10:44 +00:00
lash
f441047fa1
Implement storageservice in nats subscription object 2024-11-03 23:08:50 +00:00
8 changed files with 189 additions and 19 deletions

View File

@ -2,13 +2,14 @@ package main
import ( import (
"context" "context"
"flag"
"fmt" "fmt"
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
"git.defalsify.org/vise.git/db/mem"
"git.grassecon.net/urdt/ussd/initializers" "git.grassecon.net/urdt/ussd/initializers"
"git.grassecon.net/urdt/ussd/common"
"git.grassecon.net/term/config" "git.grassecon.net/term/config"
"git.grassecon.net/term/event/nats" "git.grassecon.net/term/event/nats"
) )
@ -18,14 +19,28 @@ func init() {
} }
func main() { func main() {
config.LoadConfig()
var dbDir string
flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from")
flag.Parse()
ctx := context.Background() ctx := context.Background()
db := mem.NewMemDb() // db := mem.NewMemDb()
err := db.Connect(ctx, "") // err := db.Connect(ctx, "")
// if err != nil {
// fmt.Fprintf(os.Stderr, "Db connect err: %v", err)
// os.Exit(1)
// }
menuStorageService := common.NewStorageService(dbDir)
err := menuStorageService.EnsureDbDir()
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Db connect err: %v", err) fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)
} }
n := nats.NewNatsSubscription(db)
n := nats.NewNatsSubscription(menuStorageService)
err = n.Connect(ctx, config.JetstreamURL) err = n.Connect(ctx, config.JetstreamURL)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Stream connect err: %v", err) fmt.Fprintf(os.Stderr, "Stream connect err: %v", err)

View File

@ -5,12 +5,14 @@ import (
geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event" geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event"
"git.defalsify.org/vise.git/persist"
"git.grassecon.net/urdt/ussd/common" "git.grassecon.net/urdt/ussd/common"
"git.grassecon.net/term/lookup" "git.grassecon.net/term/lookup"
) )
const ( const (
evReg = "CUSTODIAL_REGISTRATION" evReg = "CUSTODIAL_REGISTRATION"
accountCreatedFlag = 9
) )
// fields used for handling custodial registration event. // fields used for handling custodial registration event.
@ -35,11 +37,20 @@ func asCustodialRegistrationEvent(gev *geEvent.Event) (*eventCustodialRegistrati
} }
// handle custodial registration. // handle custodial registration.
func handleCustodialRegistration(ctx context.Context, store *common.UserDataStore, ev *eventCustodialRegistration) error { //
// TODO: implement account created in userstore instead, so that
// the need for persister and state use here is eliminated (it
// introduces concurrency risks)
func handleCustodialRegistration(ctx context.Context, store *common.UserDataStore, pr *persist.Persister, ev *eventCustodialRegistration) error {
identity, err := lookup.IdentityFromAddress(ctx, store, ev.Account) identity, err := lookup.IdentityFromAddress(ctx, store, ev.Account)
if err != nil { if err != nil {
return err return err
} }
_ = identity err = pr.Load(identity.SessionId)
return nil if err != nil {
return err
}
st := pr.GetState()
st.SetFlag(accountCreatedFlag)
return pr.Save(identity.SessionId)
} }

62
event/custodial_test.go Normal file
View File

@ -0,0 +1,62 @@
package event
import (
"context"
"testing"
memdb "git.defalsify.org/vise.git/db/mem"
"git.defalsify.org/vise.git/db"
"git.defalsify.org/vise.git/persist"
"git.defalsify.org/vise.git/state"
"git.defalsify.org/vise.git/cache"
"git.grassecon.net/urdt/ussd/config"
"git.grassecon.net/urdt/ussd/common"
"git.grassecon.net/term/internal/testutil"
)
func TestCustodialRegistration(t *testing.T) {
err := config.LoadConfig()
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
userDb := memdb.NewMemDb()
err = userDb.Connect(ctx, "")
if err != nil {
panic(err)
}
alice, err := common.NormalizeHex(testutil.AliceChecksum)
if err != nil {
t.Fatal(err)
}
userDb.SetSession(alice)
userDb.SetPrefix(db.DATATYPE_USERDATA)
err = userDb.Put(ctx, common.PackKey(common.DATA_PUBLIC_KEY_REVERSE, []byte{}), []byte(testutil.AliceSession))
if err != nil {
t.Fatal(err)
}
store := common.UserDataStore{
Db: userDb,
}
st := state.NewState(248)
ca := cache.NewCache()
pr := persist.NewPersister(userDb)
pr = pr.WithContent(st, ca)
err = pr.Save(testutil.AliceSession)
if err != nil {
t.Fatal(err)
}
ev := &eventCustodialRegistration{
Account: testutil.AliceChecksum,
}
err = handleCustodialRegistration(ctx, &store, pr, ev)
if err != nil {
t.Fatal(err)
}
}

View File

@ -9,7 +9,6 @@ import (
"github.com/nats-io/nats.go/jetstream" "github.com/nats-io/nats.go/jetstream"
geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event" geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event"
"git.defalsify.org/vise.git/logging" "git.defalsify.org/vise.git/logging"
"git.defalsify.org/vise.git/db"
"git.grassecon.net/urdt/ussd/common" "git.grassecon.net/urdt/ussd/common"
"git.grassecon.net/term/event" "git.grassecon.net/term/event"
"git.grassecon.net/term/config" "git.grassecon.net/term/config"
@ -32,12 +31,10 @@ type NatsSubscription struct {
} }
// NewNatsSubscription creates a new NatsSubscription with the given user store. // NewNatsSubscription creates a new NatsSubscription with the given user store.
func NewNatsSubscription(store db.Db) *NatsSubscription { func NewNatsSubscription(store common.StorageServices) *NatsSubscription {
return &NatsSubscription{ return &NatsSubscription{
Router: event.Router{ Router: event.Router{
Store: &common.UserDataStore{ Store: store,
Db: store,
},
}, },
} }
} }

View File

@ -141,8 +141,10 @@ func TestHandleMsg(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
storageService := &testutil.TestStorageService{
sub := NewNatsSubscription(userDb) Store: userDb,
}
sub := NewNatsSubscription(storageService)
data := fmt.Sprintf(`{ data := fmt.Sprintf(`{
"block": %d, "block": %d,

View File

@ -16,8 +16,7 @@ var (
// Router is responsible for invoking handlers corresponding to events. // Router is responsible for invoking handlers corresponding to events.
type Router struct { type Router struct {
// User data store abstraction over application data. Store common.StorageServices
Store *common.UserDataStore
} }
// Route parses an event from the event stream, and resolves the handler // Route parses an event from the event stream, and resolves the handler
@ -27,13 +26,24 @@ type Router struct {
// handler fails to successfully execute. // handler fails to successfully execute.
func(r *Router) Route(ctx context.Context, gev *geEvent.Event) error { func(r *Router) Route(ctx context.Context, gev *geEvent.Event) error {
logg.DebugCtxf(ctx, "have event", "ev", gev) logg.DebugCtxf(ctx, "have event", "ev", gev)
store, err := r.Store.GetUserdataDb(ctx)
if err != nil {
return err
}
userStore := &common.UserDataStore{
Db: store,
}
evCC, ok := asCustodialRegistrationEvent(gev) evCC, ok := asCustodialRegistrationEvent(gev)
if ok { if ok {
return handleCustodialRegistration(ctx, r.Store, evCC) pr, err := r.Store.GetPersister(ctx)
if err != nil {
return err
}
return handleCustodialRegistration(ctx, userStore, pr, evCC)
} }
evTT, ok := asTokenTransferEvent(gev) evTT, ok := asTokenTransferEvent(gev)
if ok { if ok {
return handleTokenTransfer(ctx, r.Store, evTT) return handleTokenTransfer(ctx, userStore, evTT)
} }
return fmt.Errorf("unexpected message") return fmt.Errorf("unexpected message")

View File

@ -78,6 +78,7 @@ func TestTokenTransfer(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
// TODO: deduplicate test setup
userDb.SetSession(alice) userDb.SetSession(alice)
userDb.SetPrefix(db.DATATYPE_USERDATA) userDb.SetPrefix(db.DATATYPE_USERDATA)
err = userDb.Put(ctx, common.PackKey(common.DATA_PUBLIC_KEY_REVERSE, []byte{}), []byte(testutil.AliceSession)) err = userDb.Put(ctx, common.PackKey(common.DATA_PUBLIC_KEY_REVERSE, []byte{}), []byte(testutil.AliceSession))
@ -97,6 +98,43 @@ func TestTokenTransfer(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
v, err := store.ReadEntry(ctx, testutil.AliceSession, common.DATA_ACTIVE_SYM)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(v, []byte(tokenSymbol)) {
t.Fatalf("expected '%s', got %s", tokenSymbol, v)
}
v, err = store.ReadEntry(ctx, testutil.AliceSession, common.DATA_ACTIVE_BAL)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(v, []byte(strconv.Itoa(tokenBalance))) {
t.Fatalf("expected '%d', got %s", tokenBalance, v)
}
v, err = store.ReadEntry(ctx, testutil.AliceSession, common.DATA_TRANSACTIONS)
if err != nil {
t.Fatal(err)
}
if !bytes.Contains(v, []byte("abcdef")) {
t.Fatal("no transaction data")
}
userDb.SetPrefix(DATATYPE_USERSUB)
userDb.SetSession(testutil.AliceSession)
k := append([]byte("vouchers"), []byte("sym")...)
v, err = userDb.Get(ctx, k)
if err != nil {
t.Fatal(err)
}
if !bytes.Contains(v, []byte(fmt.Sprintf("1:%s", tokenSymbol))) {
t.Fatalf("expected '1:%s', got %s", tokenSymbol, v)
}
} }
func TestTokenMint(t *testing.T) { func TestTokenMint(t *testing.T) {

View File

@ -0,0 +1,35 @@
package testutil
import (
"context"
"errors"
"git.defalsify.org/vise.git/persist"
"git.defalsify.org/vise.git/resource"
"git.defalsify.org/vise.git/db"
)
// TestStorageService wraps db for nats subscription.
type TestStorageService struct {
Store db.Db
}
// GetUserdataDb implements urdt/ussd/common.StorageServices.
func(ss *TestStorageService) GetUserdataDb(ctx context.Context) (db.Db, error) {
return ss.Store, nil
}
// GetPersister implements urdt/ussd/common.StorageServices.
func(ts *TestStorageService) GetPersister(ctx context.Context) (*persist.Persister, error) {
return persist.NewPersister(ts.Store), nil
}
// GetResource implements urdt/ussd/common.StorageServices.
func(ts *TestStorageService) GetResource(ctx context.Context) (resource.Resource, error) {
return nil, errors.New("not implemented")
}
// EnsureDbDir implements urdt/ussd/common.StorageServices.
func(ss *TestStorageService) EnsureDbDir() error {
return nil
}