WIP add event handler test

This commit is contained in:
lash 2024-11-03 00:34:28 +00:00
parent e0a3410f36
commit 1e14395739
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
9 changed files with 206 additions and 40 deletions

9
event/api.go Normal file
View File

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

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

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

@ -0,0 +1,157 @@
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/event"
)
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 TestHandleMsg(t *testing.T) {
err := config.LoadConfig()
if err != nil {
t.Fatal(err)
}
event.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"
) )
@ -31,10 +30,10 @@ type eventTokenTransfer struct {
TxHash string TxHash 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 := Api.FetchTransactions(ctx, identity.ChecksumAddress)
if err != nil { if err != nil {
return err return err
} }
@ -47,8 +46,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 := Api.FetchVouchers(ctx, identity.ChecksumAddress)
if err != nil { if err != nil {
return err return err
} }
@ -86,16 +85,6 @@ 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"))
tokenData, err := common.GetVoucherData(ctx, pfxDb, activeSym) tokenData, err := common.GetVoucherData(ctx, pfxDb, activeSym)
@ -105,14 +94,12 @@ func updateDefaultToken(ctx context.Context, store *common.UserDataStore, identi
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 updateToken(ctx context.Context, store *common.UserDataStore, identity lookup.Identity) error {
var api remote.AccountService err := updateTokenList(ctx, store, identity)
err := updateTokenList(ctx, &api, store, identity)
if err != nil { if err != nil {
return err return err
} }
@ -154,7 +141,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,7 +151,7 @@ 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
} }
return &ev, true return &ev, true

2
go.mod
View File

@ -4,7 +4,7 @@ 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-20241103014457-a237b615f2ee
github.com/grassrootseconomics/eth-tracker v1.3.0-rc github.com/grassrootseconomics/eth-tracker v1.3.0-rc
github.com/nats-io/nats.go v1.37.0 github.com/nats-io/nats.go v1.37.0
) )

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-20241103014457-a237b615f2ee h1:DJI0cfGxMKMoEPeO9IRaBKQzPBJWS00GeQ5derjE+GY=
git.grassecon.net/urdt/ussd v0.0.0-20241102234108-dae12ac4989e/go.mod h1:ADB/wpwvI6umvYzGqpJGm/GYj8msxYGiczzWCCdXegs= git.grassecon.net/urdt/ussd v0.0.0-20241103014457-a237b615f2ee/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,12 @@ 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) {
logg.Debugf("fooar")
r, err := store.ReadEntry(ctx, address, common.DATA_PUBLIC_KEY_REVERSE) // TODO: replace with userdatastore when double sessionid issue fixed
//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
} }