Compare commits
No commits in common. "6f352e7cf279b38681265be7351d226de99cad1a" and "7c7eff2affd3b22cfe3ea2e41ae564754eea9e49" have entirely different histories.
6f352e7cf2
...
7c7eff2aff
25
cmd/main.go
25
cmd/main.go
@ -2,14 +2,13 @@ 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"
|
||||
)
|
||||
@ -19,28 +18,14 @@ 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, "")
|
||||
// if err != nil {
|
||||
// fmt.Fprintf(os.Stderr, "Db connect err: %v", err)
|
||||
// os.Exit(1)
|
||||
// }
|
||||
|
||||
menuStorageService := common.NewStorageService(dbDir)
|
||||
err := menuStorageService.EnsureDbDir()
|
||||
db := mem.NewMemDb()
|
||||
err := db.Connect(ctx, "")
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
fmt.Fprintf(os.Stderr, "Db connect err: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
n := nats.NewNatsSubscription(menuStorageService)
|
||||
n := nats.NewNatsSubscription(db)
|
||||
err = n.Connect(ctx, config.JetstreamURL)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Stream connect err: %v", err)
|
||||
|
@ -5,14 +5,12 @@ 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.
|
||||
@ -37,20 +35,11 @@ func asCustodialRegistrationEvent(gev *geEvent.Event) (*eventCustodialRegistrati
|
||||
}
|
||||
|
||||
// handle custodial registration.
|
||||
//
|
||||
// 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 {
|
||||
func handleCustodialRegistration(ctx context.Context, store *common.UserDataStore, ev *eventCustodialRegistration) error {
|
||||
identity, err := lookup.IdentityFromAddress(ctx, store, ev.Account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = pr.Load(identity.SessionId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
st := pr.GetState()
|
||||
st.SetFlag(accountCreatedFlag)
|
||||
return pr.Save(identity.SessionId)
|
||||
_ = identity
|
||||
return nil
|
||||
}
|
||||
|
@ -1,62 +0,0 @@
|
||||
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,6 +9,7 @@ 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"
|
||||
@ -31,10 +32,12 @@ type NatsSubscription struct {
|
||||
}
|
||||
|
||||
// NewNatsSubscription creates a new NatsSubscription with the given user store.
|
||||
func NewNatsSubscription(store common.StorageServices) *NatsSubscription {
|
||||
func NewNatsSubscription(store db.Db) *NatsSubscription {
|
||||
return &NatsSubscription{
|
||||
Router: event.Router{
|
||||
Store: store,
|
||||
Store: &common.UserDataStore{
|
||||
Db: store,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -141,10 +141,8 @@ func TestHandleMsg(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
storageService := &testutil.TestStorageService{
|
||||
Store: userDb,
|
||||
}
|
||||
sub := NewNatsSubscription(storageService)
|
||||
|
||||
sub := NewNatsSubscription(userDb)
|
||||
|
||||
data := fmt.Sprintf(`{
|
||||
"block": %d,
|
||||
|
@ -16,7 +16,8 @@ var (
|
||||
|
||||
// Router is responsible for invoking handlers corresponding to events.
|
||||
type Router struct {
|
||||
Store common.StorageServices
|
||||
// User data store abstraction over application data.
|
||||
Store *common.UserDataStore
|
||||
}
|
||||
|
||||
// Route parses an event from the event stream, and resolves the handler
|
||||
@ -26,24 +27,13 @@ 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 {
|
||||
pr, err := r.Store.GetPersister(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return handleCustodialRegistration(ctx, userStore, pr, evCC)
|
||||
return handleCustodialRegistration(ctx, r.Store, evCC)
|
||||
}
|
||||
evTT, ok := asTokenTransferEvent(gev)
|
||||
if ok {
|
||||
return handleTokenTransfer(ctx, userStore, evTT)
|
||||
return handleTokenTransfer(ctx, r.Store, evTT)
|
||||
}
|
||||
|
||||
return fmt.Errorf("unexpected message")
|
||||
|
@ -78,7 +78,6 @@ 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))
|
||||
@ -98,43 +97,6 @@ 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) {
|
||||
|
@ -1,35 +0,0 @@
|
||||
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