From 6f352e7cf279b38681265be7351d226de99cad1a Mon Sep 17 00:00:00 2001 From: lash Date: Sun, 3 Nov 2024 23:53:36 +0000 Subject: [PATCH] Complete custodial registration hack --- event/custodial_registration.go | 17 +++++++-- event/custodial_test.go | 62 +++++++++++++++++++++++++++++++++ event/route.go | 6 +++- event/token_test.go | 38 ++++++++++++++++++++ internal/testutil/service.go | 2 +- 5 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 event/custodial_test.go diff --git a/event/custodial_registration.go b/event/custodial_registration.go index ba5cca2..f8f49dc 100644 --- a/event/custodial_registration.go +++ b/event/custodial_registration.go @@ -5,12 +5,14 @@ import ( geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event" + "git.defalsify.org/vise.git/persist" "git.grassecon.net/urdt/ussd/common" "git.grassecon.net/term/lookup" ) const ( evReg = "CUSTODIAL_REGISTRATION" + accountCreatedFlag = 9 ) // fields used for handling custodial registration event. @@ -35,11 +37,20 @@ func asCustodialRegistrationEvent(gev *geEvent.Event) (*eventCustodialRegistrati } // 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) if err != nil { return err } - _ = identity - return nil + err = pr.Load(identity.SessionId) + if err != nil { + return err + } + st := pr.GetState() + st.SetFlag(accountCreatedFlag) + return pr.Save(identity.SessionId) } diff --git a/event/custodial_test.go b/event/custodial_test.go new file mode 100644 index 0000000..c3891e1 --- /dev/null +++ b/event/custodial_test.go @@ -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) + } + +} diff --git a/event/route.go b/event/route.go index d99bdb2..a614b9f 100644 --- a/event/route.go +++ b/event/route.go @@ -35,7 +35,11 @@ func(r *Router) Route(ctx context.Context, gev *geEvent.Event) error { } evCC, ok := asCustodialRegistrationEvent(gev) if ok { - return handleCustodialRegistration(ctx, userStore, evCC) + pr, err := r.Store.GetPersister(ctx) + if err != nil { + return err + } + return handleCustodialRegistration(ctx, userStore, pr, evCC) } evTT, ok := asTokenTransferEvent(gev) if ok { diff --git a/event/token_test.go b/event/token_test.go index 634ccf2..dc37848 100644 --- a/event/token_test.go +++ b/event/token_test.go @@ -78,6 +78,7 @@ func TestTokenTransfer(t *testing.T) { t.Fatal(err) } + // TODO: deduplicate test setup userDb.SetSession(alice) userDb.SetPrefix(db.DATATYPE_USERDATA) 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 { 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) { diff --git a/internal/testutil/service.go b/internal/testutil/service.go index a7c26a1..340abad 100644 --- a/internal/testutil/service.go +++ b/internal/testutil/service.go @@ -21,7 +21,7 @@ func(ss *TestStorageService) GetUserdataDb(ctx context.Context) (db.Db, error) { // GetPersister implements urdt/ussd/common.StorageServices. func(ts *TestStorageService) GetPersister(ctx context.Context) (*persist.Persister, error) { - return nil, errors.New("not implemented") + return persist.NewPersister(ts.Store), nil } // GetResource implements urdt/ussd/common.StorageServices.