Complete custodial registration hack

This commit is contained in:
lash 2024-11-03 23:53:36 +00:00
parent 6c4f91b0b8
commit 6f352e7cf2
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
5 changed files with 120 additions and 5 deletions

View File

@ -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)
}

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

@ -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 {

View File

@ -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) {

View File

@ -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.