From e9cbdc4a0ddad38f9461890df8bec2cadda17ce3 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 1 Feb 2021 15:33:58 +0300 Subject: [PATCH] [#328] cmd/node: Add the ability to listen Container contract's events Signed-off-by: Leonard Lyubich --- cmd/neofs-node/config.go | 3 +++ cmd/neofs-node/container.go | 21 +++++++++++++++++++++ cmd/neofs-node/morph.go | 1 + 3 files changed, 25 insertions(+) diff --git a/cmd/neofs-node/config.go b/cmd/neofs-node/config.go index 3bfb4bfb2..a7afaa6d5 100644 --- a/cmd/neofs-node/config.go +++ b/cmd/neofs-node/config.go @@ -219,6 +219,9 @@ type cfgContainer struct { scriptHash util.Uint160 fee fixedn.Fixed8 + + parsers map[event.Type]event.Parser + subscribers map[event.Type][]event.Handler } type cfgNetmap struct { diff --git a/cmd/neofs-node/container.go b/cmd/neofs-node/container.go index 17f5bfc34..4ee5a6c7d 100644 --- a/cmd/neofs-node/container.go +++ b/cmd/neofs-node/container.go @@ -17,6 +17,7 @@ import ( "github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/client/container" "github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper" + "github.com/nspcc-dev/neofs-node/pkg/morph/event" "github.com/nspcc-dev/neofs-node/pkg/network" "github.com/nspcc-dev/neofs-node/pkg/network/cache" containerTransportGRPC "github.com/nspcc-dev/neofs-node/pkg/network/transport/container/grpc" @@ -61,6 +62,26 @@ func initContainerService(c *cfg) { ) } +func addContainerNotificationHandler(c *cfg, sTyp string, h event.Handler) { + typ := event.TypeFromString(sTyp) + + if c.cfgContainer.subscribers == nil { + c.cfgContainer.subscribers = make(map[event.Type][]event.Handler, 1) + } + + c.cfgContainer.subscribers[typ] = append(c.cfgContainer.subscribers[typ], h) +} + +func setContainerNotificationParser(c *cfg, sTyp string, p event.Parser) { + typ := event.TypeFromString(sTyp) + + if c.cfgContainer.parsers == nil { + c.cfgContainer.parsers = make(map[event.Type]event.Parser, 1) + } + + c.cfgContainer.parsers[typ] = p +} + type morphLoadWriter struct { log *logger.Logger diff --git a/cmd/neofs-node/morph.go b/cmd/neofs-node/morph.go index a0197645d..68b58e767 100644 --- a/cmd/neofs-node/morph.go +++ b/cmd/neofs-node/morph.go @@ -119,6 +119,7 @@ func listenMorphNotifications(c *cfg) { setNetmapNotificationParser(c, newEpochNotification, netmapEvent.ParseNewEpoch) registerNotificationHandlers(c.cfgNetmap.scriptHash, lis, c.cfgNetmap.parsers, c.cfgNetmap.subscribers) + registerNotificationHandlers(c.cfgContainer.scriptHash, lis, c.cfgContainer.parsers, c.cfgContainer.subscribers) } func registerNotificationHandlers(scHash util.Uint160, lis event.Listener, parsers map[event.Type]event.Parser,