WIP add event handler test
This commit is contained in:
parent
e0a3410f36
commit
1e14395739
9
event/api.go
Normal file
9
event/api.go
Normal file
@ -0,0 +1,9 @@
|
||||
package event
|
||||
|
||||
import (
|
||||
"git.grassecon.net/urdt/ussd/remote"
|
||||
)
|
||||
|
||||
var (
|
||||
Api remote.AccountServiceInterface = &remote.AccountService{}
|
||||
)
|
@ -28,7 +28,7 @@ func asCustodialRegistrationEvent(gev *geEvent.Event) (*eventCustodialRegistrati
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
logg.Debug("parsed ev", "pl", pl, "ev", ev)
|
||||
logg.Debugf("parsed ev", "pl", pl, "ev", ev)
|
||||
return &ev, true
|
||||
}
|
||||
|
||||
|
@ -3,19 +3,18 @@ package nats
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"os"
|
||||
|
||||
nats "github.com/nats-io/nats.go"
|
||||
"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"
|
||||
)
|
||||
|
||||
var (
|
||||
logg = slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug}))
|
||||
logg = logging.NewVanilla().WithDomain("term-nats")
|
||||
)
|
||||
|
||||
type NatsSubscription struct {
|
||||
@ -78,30 +77,30 @@ func(n *NatsSubscription) Close() error {
|
||||
func fail(m jetstream.Msg) {
|
||||
err := m.Nak()
|
||||
if err != nil {
|
||||
logg.Error("nats nak fail", "err", err)
|
||||
logg.Errorf("nats nak fail", "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
func(n *NatsSubscription) handleEvent(m jetstream.Msg) {
|
||||
var ev geEvent.Event
|
||||
|
||||
logg.Debug("have msg", "err", m)
|
||||
logg.DebugCtxf(n.ctx, "have msg", "err", m)
|
||||
b := m.Data()
|
||||
err := json.Unmarshal(b, &ev)
|
||||
if err != nil {
|
||||
logg.Error("nats msg deserialize fail", "err", err)
|
||||
logg.ErrorCtxf(n.ctx, "nats msg deserialize fail", "err", err)
|
||||
//fail(m)
|
||||
} else {
|
||||
err = n.Route(n.ctx, &ev)
|
||||
if err != nil {
|
||||
logg.Error("handler route fail", "err", err)
|
||||
logg.ErrorCtxf(n.ctx, "handler route fail", "err", err)
|
||||
//fail(m)
|
||||
}
|
||||
}
|
||||
err = m.Ack()
|
||||
if err != nil {
|
||||
logg.Error("ack fail", "err", err)
|
||||
logg.ErrorCtxf(n.ctx, "ack fail", "err", err)
|
||||
panic("ack fail")
|
||||
}
|
||||
logg.Debug("handle msg complete")
|
||||
logg.DebugCtxf(n.ctx, "handle msg complete")
|
||||
}
|
||||
|
157
event/nats/nats_test.go
Normal file
157
event/nats/nats_test.go
Normal 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)
|
||||
}
|
@ -3,11 +3,10 @@ package event
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
|
||||
geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event"
|
||||
|
||||
"git.defalsify.org/vise.git/logging"
|
||||
"git.grassecon.net/urdt/ussd/common"
|
||||
)
|
||||
|
||||
@ -17,7 +16,7 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
logg = slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug}))
|
||||
logg = logging.NewVanilla().WithDomain("term-event")
|
||||
)
|
||||
|
||||
type Router struct {
|
||||
@ -25,10 +24,15 @@ type Router struct {
|
||||
}
|
||||
|
||||
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)
|
||||
if ok {
|
||||
return handleCustodialRegistration(ctx, r.Store, evCC)
|
||||
}
|
||||
evTT, ok := asTokenTransferEvent(gev)
|
||||
if ok {
|
||||
return handleTokenTransfer(ctx, r.Store, evTT)
|
||||
}
|
||||
|
||||
return fmt.Errorf("unexpected message")
|
||||
}
|
||||
|
@ -10,7 +10,6 @@ import (
|
||||
|
||||
"git.defalsify.org/vise.git/db"
|
||||
"git.grassecon.net/urdt/ussd/common"
|
||||
"git.grassecon.net/urdt/ussd/remote"
|
||||
"git.grassecon.net/term/lookup"
|
||||
)
|
||||
|
||||
@ -31,10 +30,10 @@ type eventTokenTransfer struct {
|
||||
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
|
||||
|
||||
txs, err := api.FetchTransactions(ctx, identity.ChecksumAddress)
|
||||
txs, err := Api.FetchTransactions(ctx, identity.ChecksumAddress)
|
||||
if err != nil {
|
||||
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))
|
||||
}
|
||||
|
||||
func updateTokenList(ctx context.Context, api remote.AccountServiceInterface, store *common.UserDataStore, identity lookup.Identity) error {
|
||||
holdings, err := api.FetchVouchers(ctx, identity.ChecksumAddress)
|
||||
func updateTokenList(ctx context.Context, store *common.UserDataStore, identity lookup.Identity) error {
|
||||
holdings, err := Api.FetchVouchers(ctx, identity.ChecksumAddress)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -86,16 +85,6 @@ func updateTokenList(ctx context.Context, api remote.AccountServiceInterface, st
|
||||
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 {
|
||||
pfxDb := common.StoreToPrefixDb(store, []byte("vouchers"))
|
||||
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)
|
||||
}
|
||||
|
||||
func updateWait(ctx context.Context, api remote.AccountServiceInterface) error {
|
||||
func updateWait(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func updateToken(ctx context.Context, store *common.UserDataStore, identity lookup.Identity) error {
|
||||
var api remote.AccountService
|
||||
|
||||
err := updateTokenList(ctx, &api, store, identity)
|
||||
err := updateTokenList(ctx, store, identity)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -154,7 +141,7 @@ func asTokenTransferEvent(gev *geEvent.Event) (*eventTokenTransfer, bool) {
|
||||
}
|
||||
ev.TxHash, err = common.NormalizeHex(gev.TxHash)
|
||||
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
|
||||
}
|
||||
|
||||
@ -164,7 +151,7 @@ func asTokenTransferEvent(gev *geEvent.Event) (*eventTokenTransfer, bool) {
|
||||
}
|
||||
ev.Value, err = strconv.Atoi(value)
|
||||
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 &ev, true
|
||||
|
2
go.mod
2
go.mod
@ -4,7 +4,7 @@ go 1.23.2
|
||||
|
||||
require (
|
||||
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/nats-io/nats.go v1.37.0
|
||||
)
|
||||
|
4
go.sum
4
go.sum
@ -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/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-20241102234108-dae12ac4989e/go.mod h1:ADB/wpwvI6umvYzGqpJGm/GYj8msxYGiczzWCCdXegs=
|
||||
git.grassecon.net/urdt/ussd v0.0.0-20241103014457-a237b615f2ee h1:DJI0cfGxMKMoEPeO9IRaBKQzPBJWS00GeQ5derjE+GY=
|
||||
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/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ=
|
||||
github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk=
|
||||
|
14
lookup/db.go
14
lookup/db.go
@ -3,9 +3,15 @@ package lookup
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.defalsify.org/vise.git/db"
|
||||
"git.defalsify.org/vise.git/logging"
|
||||
"git.grassecon.net/urdt/ussd/common"
|
||||
)
|
||||
|
||||
var (
|
||||
logg = logging.NewVanilla().WithDomain("term-lookup")
|
||||
)
|
||||
|
||||
type Identity struct {
|
||||
NormalAddress 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) {
|
||||
|
||||
r, err := store.ReadEntry(ctx, address, common.DATA_PUBLIC_KEY_REVERSE)
|
||||
logg.Debugf("fooar")
|
||||
// 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 {
|
||||
return "", err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user