Compare commits
3 Commits
7c7eff2aff
...
6f352e7cf2
Author | SHA1 | Date | |
---|---|---|---|
|
6f352e7cf2 | ||
|
6c4f91b0b8 | ||
|
f441047fa1 |
25
cmd/main.go
25
cmd/main.go
@ -2,13 +2,14 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"git.defalsify.org/vise.git/db/mem"
|
||||
"git.grassecon.net/urdt/ussd/initializers"
|
||||
"git.grassecon.net/urdt/ussd/common"
|
||||
"git.grassecon.net/term/config"
|
||||
"git.grassecon.net/term/event/nats"
|
||||
)
|
||||
@ -18,14 +19,28 @@ func init() {
|
||||
}
|
||||
|
||||
func main() {
|
||||
config.LoadConfig()
|
||||
|
||||
var dbDir string
|
||||
flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from")
|
||||
flag.Parse()
|
||||
|
||||
ctx := context.Background()
|
||||
db := mem.NewMemDb()
|
||||
err := db.Connect(ctx, "")
|
||||
// db := mem.NewMemDb()
|
||||
// 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 {
|
||||
fmt.Fprintf(os.Stderr, "Db connect err: %v", err)
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
n := nats.NewNatsSubscription(db)
|
||||
|
||||
n := nats.NewNatsSubscription(menuStorageService)
|
||||
err = n.Connect(ctx, config.JetstreamURL)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Stream connect err: %v", err)
|
||||
|
@ -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
62
event/custodial_test.go
Normal 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)
|
||||
}
|
||||
|
||||
}
|
@ -9,7 +9,6 @@ import (
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event"
|
||||
"git.defalsify.org/vise.git/logging"
|
||||
"git.defalsify.org/vise.git/db"
|
||||
"git.grassecon.net/urdt/ussd/common"
|
||||
"git.grassecon.net/term/event"
|
||||
"git.grassecon.net/term/config"
|
||||
@ -32,12 +31,10 @@ type NatsSubscription struct {
|
||||
}
|
||||
|
||||
// NewNatsSubscription creates a new NatsSubscription with the given user store.
|
||||
func NewNatsSubscription(store db.Db) *NatsSubscription {
|
||||
func NewNatsSubscription(store common.StorageServices) *NatsSubscription {
|
||||
return &NatsSubscription{
|
||||
Router: event.Router{
|
||||
Store: &common.UserDataStore{
|
||||
Db: store,
|
||||
},
|
||||
Store: store,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -141,8 +141,10 @@ func TestHandleMsg(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
||||
sub := NewNatsSubscription(userDb)
|
||||
storageService := &testutil.TestStorageService{
|
||||
Store: userDb,
|
||||
}
|
||||
sub := NewNatsSubscription(storageService)
|
||||
|
||||
data := fmt.Sprintf(`{
|
||||
"block": %d,
|
||||
|
@ -16,8 +16,7 @@ var (
|
||||
|
||||
// Router is responsible for invoking handlers corresponding to events.
|
||||
type Router struct {
|
||||
// User data store abstraction over application data.
|
||||
Store *common.UserDataStore
|
||||
Store common.StorageServices
|
||||
}
|
||||
|
||||
// Route parses an event from the event stream, and resolves the handler
|
||||
@ -27,13 +26,24 @@ type Router struct {
|
||||
// handler fails to successfully execute.
|
||||
func(r *Router) Route(ctx context.Context, gev *geEvent.Event) error {
|
||||
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)
|
||||
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)
|
||||
if ok {
|
||||
return handleTokenTransfer(ctx, r.Store, evTT)
|
||||
return handleTokenTransfer(ctx, userStore, evTT)
|
||||
}
|
||||
|
||||
return fmt.Errorf("unexpected message")
|
||||
|
@ -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) {
|
||||
|
35
internal/testutil/service.go
Normal file
35
internal/testutil/service.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user