From cab491f0d9f0b77fbe9f6d61fb3b41b9bd06ecb0 Mon Sep 17 00:00:00 2001 From: lash Date: Thu, 24 Oct 2024 16:00:46 +0100 Subject: [PATCH] Add custodial registration handler --- event/custodial_registration.go | 31 +++++++++++++++++++++++++ event/nats/nats.go | 41 +++++++++++++++------------------ event/route.go | 22 ++++++++++++++++-- 3 files changed, 69 insertions(+), 25 deletions(-) create mode 100644 event/custodial_registration.go diff --git a/event/custodial_registration.go b/event/custodial_registration.go new file mode 100644 index 0000000..68f3ca7 --- /dev/null +++ b/event/custodial_registration.go @@ -0,0 +1,31 @@ +package event + +import ( + geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event" +) + +const ( + evReg = "CUSTODIAL_REGISTRATION" +) + +type eventCustodialRegistration struct { + account string +} + +func asCustodialRegistrationEvent(gev *geEvent.Event) (*eventCustodialRegistration, bool) { + var ok bool + var ev eventCustodialRegistration + if gev.TxType != evReg { + return nil, false + } + pl := gev.Payload + ev.account, ok = pl["account"].(string) + if !ok { + return nil, false + } + return &ev, true +} + +func handleCustodialRegistration(ev *eventCustodialRegistration) error { + return nil +} diff --git a/event/nats/nats.go b/event/nats/nats.go index a6ebe06..a79e57b 100644 --- a/event/nats/nats.go +++ b/event/nats/nats.go @@ -3,24 +3,19 @@ package nats import ( "context" "encoding/json" + "log/slog" + "os" nats "github.com/nats-io/nats.go" "github.com/nats-io/nats.go/jetstream" - "git.defalsify.org/vise.git/logging" - geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event" "git.grassecon.net/term/event" ) -const ( - evGive = "TRACKER.FAUCET_GIVE" - evReg = "TRACKER.CUSTODIAL_REGISTRATION" -) - var ( - logg = logging.NewVanilla().WithDomain("nats") + logg = slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug})) ) type NatsSubscription struct { @@ -75,31 +70,31 @@ func(n *NatsSubscription) Close() error { func fail(m jetstream.Msg) { err := m.Nak() if err != nil { - logg.Errorf("nats nak fail", "err", err) + logg.Error("nats nak fail", "err", err) } } func(n *NatsSubscription) handleEvent(m jetstream.Msg) { var ev geEvent.Event - logg.Tracef("have msg", "err", m) + logg.Debug("have msg", "err", m) b := m.Data() err := json.Unmarshal(b, &ev) if err != nil { - logg.Errorf("nats msg deserialize fail", "err", err) - fail(m) - return + logg.Error("nats msg deserialize fail", "err", err) + //fail(m) + } else { + err = n.Route(&ev) + if err != nil { + logg.Error("handler route fail", "err", err) + fail(m) + return + } } - err = n.Route(&ev) + err = m.Term() if err != nil { - logg.Errorf("handler route fail", "err", err) - fail(m) - return - } - err = m.Ack() -// err = m.DoubleAck(n.ctx) - if err != nil { - logg.Errorf("ack fail", "err", err) - panic("ack fail") + logg.Error("term fail", "err", err) + panic("term fail") } + logg.Debug("handle msg complete") } diff --git a/event/route.go b/event/route.go index cf61b66..068244d 100644 --- a/event/route.go +++ b/event/route.go @@ -1,12 +1,30 @@ package event import ( + "fmt" + "log/slog" + "os" + geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event" ) +// TODO: this vocabulary should be public in and provided by the eth-tracker repo +const ( + evGive = "FAUCET_GIVE" +) + +var ( + logg = slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug})) +) + type Router struct { } -func(r *Router) Route(ev *geEvent.Event) error { - return nil +func(r *Router) Route(gev *geEvent.Event) error { + logg.Debug("have event", "ev", gev) + evCC, ok := asCustodialRegistrationEvent(gev) + if !ok { + return handleCustodialRegistration(evCC) + } + return fmt.Errorf("unexpected message") }