Compare commits

..

2 Commits

Author SHA1 Message Date
lash
a4443cbe4c
Add voucherdata resolve 2024-11-03 14:35:37 +00:00
lash
1e14395739
WIP add event handler test 2024-11-03 02:00:21 +00:00
9 changed files with 243 additions and 45 deletions

View File

@ -28,7 +28,7 @@ func asCustodialRegistrationEvent(gev *geEvent.Event) (*eventCustodialRegistrati
if !ok { if !ok {
return nil, false return nil, false
} }
logg.Debug("parsed ev", "pl", pl, "ev", ev) logg.Debugf("parsed ev", "pl", pl, "ev", ev)
return &ev, true return &ev, true
} }

View File

@ -3,19 +3,18 @@ package nats
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"log/slog"
"os"
nats "github.com/nats-io/nats.go" nats "github.com/nats-io/nats.go"
"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/db" "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"
) )
var ( var (
logg = slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug})) logg = logging.NewVanilla().WithDomain("term-nats")
) )
type NatsSubscription struct { type NatsSubscription struct {
@ -78,30 +77,30 @@ func(n *NatsSubscription) Close() error {
func fail(m jetstream.Msg) { func fail(m jetstream.Msg) {
err := m.Nak() err := m.Nak()
if err != nil { if err != nil {
logg.Error("nats nak fail", "err", err) logg.Errorf("nats nak fail", "err", err)
} }
} }
func(n *NatsSubscription) handleEvent(m jetstream.Msg) { func(n *NatsSubscription) handleEvent(m jetstream.Msg) {
var ev geEvent.Event var ev geEvent.Event
logg.Debug("have msg", "err", m) logg.DebugCtxf(n.ctx, "have msg", "err", m)
b := m.Data() b := m.Data()
err := json.Unmarshal(b, &ev) err := json.Unmarshal(b, &ev)
if err != nil { if err != nil {
logg.Error("nats msg deserialize fail", "err", err) logg.ErrorCtxf(n.ctx, "nats msg deserialize fail", "err", err)
//fail(m) //fail(m)
} else { } else {
err = n.Route(n.ctx, &ev) err = n.Route(n.ctx, &ev)
if err != nil { if err != nil {
logg.Error("handler route fail", "err", err) logg.ErrorCtxf(n.ctx, "handler route fail", "err", err)
//fail(m) //fail(m)
} }
} }
err = m.Ack() err = m.Ack()
if err != nil { if err != nil {
logg.Error("ack fail", "err", err) logg.ErrorCtxf(n.ctx, "ack fail", "err", err)
panic("ack fail") panic("ack fail")
} }
logg.Debug("handle msg complete") logg.DebugCtxf(n.ctx, "handle msg complete")
} }

166
event/nats/nats_test.go Normal file
View File

@ -0,0 +1,166 @@
package nats
import (
"context"
"testing"
"time"
nats "github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
"git.defalsify.org/vise.git/db"
memdb "git.defalsify.org/vise.git/db/mem"
"git.grassecon.net/urdt/ussd/common"
"git.grassecon.net/urdt/ussd/config"
"git.grassecon.net/urdt/ussd/models"
"git.grassecon.net/term/lookup"
)
func init() {
}
const (
aliceChecksum = "0xeae046BF396e91f5A8D74f863dC57c107c8a4a70"
)
// TODO: jetstream, would have been nice of you to provide an easier way to make a mock msg
type testMsg struct {
data []byte
}
func(m *testMsg) Ack() error {
return nil
}
func(m *testMsg) Nak() error {
return nil
}
func(m *testMsg) NakWithDelay(time.Duration) error {
return nil
}
func(m *testMsg) Data() []byte {
return m.data
}
func(m *testMsg) Reply() string {
return ""
}
func(m *testMsg) Subject() string {
return ""
}
func(m *testMsg) Term() error {
return nil
}
func(m *testMsg) TermWithReason(string) error {
return nil
}
func(m *testMsg) DoubleAck(ctx context.Context) error {
return nil
}
func(m *testMsg) Headers() nats.Header {
return nats.Header{}
}
func(m *testMsg) InProgress() error {
return nil
}
func(m *testMsg) Metadata() (*jetstream.MsgMetadata, error) {
return nil, nil
}
type mockApi struct {
}
func(m mockApi) CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResult, error) {
return nil, nil
}
func(m mockApi) CreateAccount(ctx context.Context) (*models.AccountResult, error) {
return nil, nil
}
func(m mockApi) TrackAccountStatus(ctx context.Context, publicKey string) (*models.TrackStatusResult, error) {
return nil, nil
}
func(m mockApi) FetchVouchers(ctx context.Context, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
logg.DebugCtxf(ctx, "mockapi fetchvouchers", "key", publicKey)
return []dataserviceapi.TokenHoldings{
dataserviceapi.TokenHoldings{
ContractAddress: "0xeE0A29AE1BB7a033c8277C04780c4aBcf4388E93",
TokenSymbol: "FOO",
TokenDecimals: "6",
Balance: "362436",
},
}, nil
}
func(m mockApi) FetchTransactions(ctx context.Context, publicKey string) ([]dataserviceapi.Last10TxResponse, error) {
return nil, nil
}
func(m mockApi) VoucherData(ctx context.Context, address string) (*models.VoucherDataResult, error) {
return &models.VoucherDataResult{
TokenSymbol: "FOO",
TokenName: "Foo Token",
TokenDecimals: "6",
SinkAddress: "0xb42C5920014eE152F2225285219407938469BBfA",
}, nil
}
func TestHandleMsg(t *testing.T) {
err := config.LoadConfig()
if err != nil {
t.Fatal(err)
}
lookup.Api = mockApi{}
ctx := context.Background()
userDb := memdb.NewMemDb()
err = userDb.Connect(ctx, "")
if err != nil {
panic(err)
}
alice, err := common.NormalizeHex(aliceChecksum)
if err != nil {
t.Fatal(err)
}
aliceSession := "5553425"
userDb.SetSession(alice)
userDb.SetPrefix(db.DATATYPE_USERDATA)
err = userDb.Put(ctx, common.PackKey(common.DATA_PUBLIC_KEY_REVERSE, []byte{}), []byte(aliceSession))
if err != nil {
t.Fatal(err)
}
sub := NewNatsSubscription(userDb)
data := `{
"block": 42,
"contractAddress": "0x765DE816845861e75A25fCA122bb6898B8B1282a",
"success": true,
"timestamp": 1730592500,
"transactionHash": "0xabcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789",
"transactionType": "TOKEN_TRANSFER",
"payload": {
"from": "0xeae046BF396e91f5A8D74f863dC57c107c8a4a70",
"to": "B3117202371853e24B725d4169D87616A7dDb127",
"value": "1337"
}
}`
msg := &testMsg{
data: []byte(data),
}
sub.handleEvent(msg)
}

View File

@ -3,11 +3,10 @@ package event
import ( import (
"context" "context"
"fmt" "fmt"
"log/slog"
"os"
geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event" geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event"
"git.defalsify.org/vise.git/logging"
"git.grassecon.net/urdt/ussd/common" "git.grassecon.net/urdt/ussd/common"
) )
@ -17,7 +16,7 @@ const (
) )
var ( var (
logg = slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug})) logg = logging.NewVanilla().WithDomain("term-event")
) )
type Router struct { type Router struct {
@ -25,10 +24,15 @@ type Router struct {
} }
func(r *Router) Route(ctx context.Context, gev *geEvent.Event) error { func(r *Router) Route(ctx context.Context, gev *geEvent.Event) error {
logg.Debug("have event", "ev", gev) logg.DebugCtxf(ctx, "have event", "ev", gev)
evCC, ok := asCustodialRegistrationEvent(gev) evCC, ok := asCustodialRegistrationEvent(gev)
if ok { if ok {
return handleCustodialRegistration(ctx, r.Store, evCC) return handleCustodialRegistration(ctx, r.Store, evCC)
} }
evTT, ok := asTokenTransferEvent(gev)
if ok {
return handleTokenTransfer(ctx, r.Store, evTT)
}
return fmt.Errorf("unexpected message") return fmt.Errorf("unexpected message")
} }

View File

@ -10,7 +10,6 @@ import (
"git.defalsify.org/vise.git/db" "git.defalsify.org/vise.git/db"
"git.grassecon.net/urdt/ussd/common" "git.grassecon.net/urdt/ussd/common"
"git.grassecon.net/urdt/ussd/remote"
"git.grassecon.net/term/lookup" "git.grassecon.net/term/lookup"
) )
@ -29,12 +28,13 @@ type eventTokenTransfer struct {
To string To string
Value int Value int
TxHash string TxHash string
VoucherAddress string
} }
func updateTokenTransferList(ctx context.Context, api remote.AccountServiceInterface, store common.UserDataStore, identity lookup.Identity) error { func updateTokenTransferList(ctx context.Context, store common.UserDataStore, identity lookup.Identity) error {
var r []string var r []string
txs, err := api.FetchTransactions(ctx, identity.ChecksumAddress) txs, err := lookup.Api.FetchTransactions(ctx, identity.ChecksumAddress)
if err != nil { if err != nil {
return err return err
} }
@ -47,8 +47,8 @@ func updateTokenTransferList(ctx context.Context, api remote.AccountServiceInter
return store.WriteEntry(ctx, identity.SessionId, common.DATA_TRANSACTIONS, []byte(s)) return store.WriteEntry(ctx, identity.SessionId, common.DATA_TRANSACTIONS, []byte(s))
} }
func updateTokenList(ctx context.Context, api remote.AccountServiceInterface, store *common.UserDataStore, identity lookup.Identity) error { func updateTokenList(ctx context.Context, store *common.UserDataStore, identity lookup.Identity) error {
holdings, err := api.FetchVouchers(ctx, identity.ChecksumAddress) holdings, err := lookup.Api.FetchVouchers(ctx, identity.ChecksumAddress)
if err != nil { if err != nil {
return err return err
} }
@ -57,6 +57,8 @@ func updateTokenList(ctx context.Context, api remote.AccountServiceInterface, st
// TODO: export subprefixdb and use that instead // TODO: export subprefixdb and use that instead
// TODO: make sure subprefixdb is thread safe when using gdbm // TODO: make sure subprefixdb is thread safe when using gdbm
// TODO: why is address session here unless explicitly set
store.Db.SetSession(identity.SessionId)
store.Db.SetPrefix(DATATYPE_USERSUB) store.Db.SetPrefix(DATATYPE_USERSUB)
k := append([]byte("vouchers"), []byte("sym")...) k := append([]byte("vouchers"), []byte("sym")...)
@ -64,7 +66,7 @@ func updateTokenList(ctx context.Context, api remote.AccountServiceInterface, st
if err != nil { if err != nil {
return err return err
} }
logg.TraceCtxf(ctx, "processvoucher", "key", k)
k = append([]byte("vouchers"), []byte("bal")...) k = append([]byte("vouchers"), []byte("bal")...)
err = store.Db.Put(ctx, k, []byte(metadata.Balances)) err = store.Db.Put(ctx, k, []byte(metadata.Balances))
if err != nil { if err != nil {
@ -86,44 +88,50 @@ func updateTokenList(ctx context.Context, api remote.AccountServiceInterface, st
return nil return nil
} }
//func updateTokenBalance(ctx context.Context, api remote.AccountServiceInterface, store common.UserDataStore, sessionId string) error {
// r, err := api.CheckBalance(ctx, sessionId)
// if err != nil {
// return err
// }
// //store.WriteEntry()
// return nil
//}
//
func updateDefaultToken(ctx context.Context, store *common.UserDataStore, identity lookup.Identity, activeSym string) error { func updateDefaultToken(ctx context.Context, store *common.UserDataStore, identity lookup.Identity, activeSym string) error {
pfxDb := common.StoreToPrefixDb(store, []byte("vouchers")) pfxDb := common.StoreToPrefixDb(store, []byte("vouchers"))
// TODO: the activeSym input should instead be newline separated list?
tokenData, err := common.GetVoucherData(ctx, pfxDb, activeSym) tokenData, err := common.GetVoucherData(ctx, pfxDb, activeSym)
if err != nil { if err != nil {
return err return err
} }
logg.TraceCtxf(ctx, "tokendaa", "d", tokenData)
return common.UpdateVoucherData(ctx, store, identity.SessionId, tokenData) return common.UpdateVoucherData(ctx, store, identity.SessionId, tokenData)
} }
func updateWait(ctx context.Context, api remote.AccountServiceInterface) error { func updateWait(ctx context.Context) error {
return nil return nil
} }
func updateToken(ctx context.Context, store *common.UserDataStore, identity lookup.Identity) error { func toSym(ctx context.Context, address string) ([]byte, error) {
var api remote.AccountService voucherData, err := lookup.Api.VoucherData(ctx, address)
if err != nil {
return nil, err
}
return []byte(voucherData.TokenSymbol), nil
}
err := updateTokenList(ctx, &api, store, identity) func updateToken(ctx context.Context, store *common.UserDataStore, identity lookup.Identity, tokenAddress string) error {
err := updateTokenList(ctx, store, identity)
if err != nil { if err != nil {
return err return err
} }
activeSym, err := store.ReadEntry(ctx, identity.SessionId, common.DATA_ACTIVE_ADDRESS) store.Db.SetSession(identity.SessionId)
activeSym, err := store.ReadEntry(ctx, identity.SessionId, common.DATA_ACTIVE_SYM)
if err == nil { if err == nil {
return nil return nil
} }
if !db.IsNotFound(err) { if !db.IsNotFound(err) {
return err return err
} }
if activeSym == nil {
activeSym, err = toSym(ctx, tokenAddress)
if err != nil {
return err
}
}
logg.Debugf("barfoo")
err = updateDefaultToken(ctx, store, identity, string(activeSym)) err = updateDefaultToken(ctx, store, identity, string(activeSym))
if err != nil { if err != nil {
@ -154,7 +162,7 @@ func asTokenTransferEvent(gev *geEvent.Event) (*eventTokenTransfer, bool) {
} }
ev.TxHash, err = common.NormalizeHex(gev.TxHash) ev.TxHash, err = common.NormalizeHex(gev.TxHash)
if err != nil { if err != nil {
logg.Error("could not decode tx hash", "tx", gev.TxHash, "err", err) logg.Errorf("could not decode tx hash", "tx", gev.TxHash, "err", err)
return nil, false return nil, false
} }
@ -164,9 +172,12 @@ func asTokenTransferEvent(gev *geEvent.Event) (*eventTokenTransfer, bool) {
} }
ev.Value, err = strconv.Atoi(value) ev.Value, err = strconv.Atoi(value)
if err != nil { if err != nil {
logg.Error("could not decode value", "value", value, "err", err) logg.Errorf("could not decode value", "value", value, "err", err)
return nil, false return nil, false
} }
ev.VoucherAddress = gev.ContractAddress
return &ev, true return &ev, true
} }
@ -177,7 +188,7 @@ func handleTokenTransfer(ctx context.Context, store *common.UserDataStore, ev *e
return err return err
} }
} else { } else {
err = updateToken(ctx, store, identity) err = updateToken(ctx, store, identity, ev.VoucherAddress)
if err != nil { if err != nil {
return err return err
} }
@ -188,7 +199,7 @@ func handleTokenTransfer(ctx context.Context, store *common.UserDataStore, ev *e
return err return err
} }
} else { } else {
err = updateToken(ctx, store, identity) err = updateToken(ctx, store, identity, ev.VoucherAddress)
if err != nil { if err != nil {
return err return err
} }

4
go.mod
View File

@ -4,8 +4,9 @@ go 1.23.2
require ( require (
git.defalsify.org/vise.git v0.2.1-0.20241031204035-b588301738ed git.defalsify.org/vise.git v0.2.1-0.20241031204035-b588301738ed
git.grassecon.net/urdt/ussd v0.0.0-20241102234108-dae12ac4989e git.grassecon.net/urdt/ussd v0.0.0-20241103143426-0506a8c452f1
github.com/grassrootseconomics/eth-tracker v1.3.0-rc github.com/grassrootseconomics/eth-tracker v1.3.0-rc
github.com/grassrootseconomics/ussd-data-service v0.0.0-20241003123429-4904b4438a3a
github.com/nats-io/nats.go v1.37.0 github.com/nats-io/nats.go v1.37.0
) )
@ -13,7 +14,6 @@ require (
github.com/barbashov/iso639-3 v0.0.0-20211020172741-1f4ffb2d8d1c // indirect github.com/barbashov/iso639-3 v0.0.0-20211020172741-1f4ffb2d8d1c // indirect
github.com/fxamacker/cbor/v2 v2.4.0 // indirect github.com/fxamacker/cbor/v2 v2.4.0 // indirect
github.com/grassrootseconomics/eth-custodial v1.3.0-beta // indirect github.com/grassrootseconomics/eth-custodial v1.3.0-beta // indirect
github.com/grassrootseconomics/ussd-data-service v0.0.0-20241003123429-4904b4438a3a // indirect
github.com/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4 // indirect github.com/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect

4
go.sum
View File

@ -1,7 +1,7 @@
git.defalsify.org/vise.git v0.2.1-0.20241031204035-b588301738ed h1:4TrsfbK7NKgsa7KjMPlnV/tjYTkAAXP5PWAZzUfzCdI= git.defalsify.org/vise.git v0.2.1-0.20241031204035-b588301738ed h1:4TrsfbK7NKgsa7KjMPlnV/tjYTkAAXP5PWAZzUfzCdI=
git.defalsify.org/vise.git v0.2.1-0.20241031204035-b588301738ed/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck= git.defalsify.org/vise.git v0.2.1-0.20241031204035-b588301738ed/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck=
git.grassecon.net/urdt/ussd v0.0.0-20241102234108-dae12ac4989e h1:YOEAjTrlyDRm0E63fEq8HbMp7oUCyc3vbAPOIw0xNWA= git.grassecon.net/urdt/ussd v0.0.0-20241103143426-0506a8c452f1 h1:uTscFuyKCqWshcN+pgoJiE0jIVzRrUrgBfI/RsiM7qE=
git.grassecon.net/urdt/ussd v0.0.0-20241102234108-dae12ac4989e/go.mod h1:ADB/wpwvI6umvYzGqpJGm/GYj8msxYGiczzWCCdXegs= git.grassecon.net/urdt/ussd v0.0.0-20241103143426-0506a8c452f1/go.mod h1:ADB/wpwvI6umvYzGqpJGm/GYj8msxYGiczzWCCdXegs=
github.com/alecthomas/assert/v2 v2.2.2 h1:Z/iVC0xZfWTaFNE6bA3z07T86hd45Xe2eLt6WVy2bbk= github.com/alecthomas/assert/v2 v2.2.2 h1:Z/iVC0xZfWTaFNE6bA3z07T86hd45Xe2eLt6WVy2bbk=
github.com/alecthomas/assert/v2 v2.2.2/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ= github.com/alecthomas/assert/v2 v2.2.2/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ=
github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk= github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk=

View File

@ -3,9 +3,15 @@ package lookup
import ( import (
"context" "context"
"git.defalsify.org/vise.git/db"
"git.defalsify.org/vise.git/logging"
"git.grassecon.net/urdt/ussd/common" "git.grassecon.net/urdt/ussd/common"
) )
var (
logg = logging.NewVanilla().WithDomain("term-lookup")
)
type Identity struct { type Identity struct {
NormalAddress string NormalAddress string
ChecksumAddress string ChecksumAddress string
@ -29,8 +35,11 @@ func IdentityFromAddress(ctx context.Context, store *common.UserDataStore, addre
} }
func getSessionIdByAddress(ctx context.Context, store *common.UserDataStore, address string) (string, error) { func getSessionIdByAddress(ctx context.Context, store *common.UserDataStore, address string) (string, error) {
// TODO: replace with userdatastore when double sessionid issue fixed
r, err := store.ReadEntry(ctx, address, common.DATA_PUBLIC_KEY_REVERSE) //r, err := store.ReadEntry(ctx, address, common.DATA_PUBLIC_KEY_REVERSE)
store.Db.SetPrefix(db.DATATYPE_USERDATA)
store.Db.SetSession(address)
r, err := store.Db.Get(ctx, common.PackKey(common.DATA_PUBLIC_KEY_REVERSE, []byte{}))
if err != nil { if err != nil {
return "", err return "", err
} }

9
lookup/token.go Normal file
View File

@ -0,0 +1,9 @@
package lookup
import (
"git.grassecon.net/urdt/ussd/remote"
)
var (
Api remote.AccountServiceInterface = &remote.AccountService{}
)