From 00caed8d3d56f1ce7875c1b912ad1e95c657f948 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Wed, 8 Sep 2021 16:54:45 +0300 Subject: [PATCH] [#815] morph/event/netmap: Add `addPeer` notary notification support Signed-off-by: Pavel Karpy --- pkg/innerring/innerring.go | 1 + pkg/innerring/processors/netmap/processor.go | 79 +++++++++++++++----- pkg/morph/event/netmap/add_peer.go | 6 +- pkg/morph/event/netmap/add_peer_notary.go | 47 ++++++++++++ 4 files changed, 113 insertions(+), 20 deletions(-) create mode 100644 pkg/morph/event/netmap/add_peer_notary.go diff --git a/pkg/innerring/innerring.go b/pkg/innerring/innerring.go index f1cb00829..060d694a6 100644 --- a/pkg/innerring/innerring.go +++ b/pkg/innerring/innerring.go @@ -668,6 +668,7 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error addrvalidator.New(), locodeValidator, ), + NotaryDisabled: server.sideNotaryConfig.disabled, }) if err != nil { return nil, err diff --git a/pkg/innerring/processors/netmap/processor.go b/pkg/innerring/processors/netmap/processor.go index ba6e767b0..67410b458 100644 --- a/pkg/innerring/processors/netmap/processor.go +++ b/pkg/innerring/processors/netmap/processor.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" + "github.com/nspcc-dev/neo-go/pkg/core/mempoolevent" "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neofs-api-go/pkg/netmap" container "github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper" @@ -66,6 +67,8 @@ type ( handleAlphabetSync event.Handler nodeValidator NodeValidator + + notaryDisabled bool } // Params of the processor constructor. @@ -87,6 +90,8 @@ type ( AlphabetSyncHandler event.Handler NodeValidator NodeValidator + + NotaryDisabled bool } ) @@ -143,12 +148,14 @@ func New(p *Params) (*Processor, error) { handleAlphabetSync: p.AlphabetSyncHandler, nodeValidator: p.NodeValidator, + + notaryDisabled: p.NotaryDisabled, }, nil } // ListenerNotificationParsers for the 'event.Listener' event producer. func (np *Processor) ListenerNotificationParsers() []event.NotificationParserInfo { - var parsers []event.NotificationParserInfo + parsers := make([]event.NotificationParserInfo, 0, 3) // new epoch event newEpoch := event.NotificationParserInfo{} @@ -157,13 +164,6 @@ func (np *Processor) ListenerNotificationParsers() []event.NotificationParserInf newEpoch.SetParser(netmapEvent.ParseNewEpoch) parsers = append(parsers, newEpoch) - // new peer event - addPeer := event.NotificationParserInfo{} - addPeer.SetType(addPeerNotification) - addPeer.SetScriptHash(np.netmapContract) - addPeer.SetParser(netmapEvent.ParseAddPeer) - parsers = append(parsers, addPeer) - // update peer event updatePeer := event.NotificationParserInfo{} updatePeer.SetType(updatePeerStateNotification) @@ -171,12 +171,23 @@ func (np *Processor) ListenerNotificationParsers() []event.NotificationParserInf updatePeer.SetParser(netmapEvent.ParseUpdatePeer) parsers = append(parsers, updatePeer) + if !np.notaryDisabled { + return parsers + } + + // new peer event + addPeer := event.NotificationParserInfo{} + addPeer.SetType(addPeerNotification) + addPeer.SetScriptHash(np.netmapContract) + addPeer.SetParser(netmapEvent.ParseAddPeer) + parsers = append(parsers, addPeer) + return parsers } // ListenerNotificationHandlers for the 'event.Listener' event producer. func (np *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo { - var handlers []event.NotificationHandlerInfo + handlers := make([]event.NotificationHandlerInfo, 0, 3) // new epoch handler newEpoch := event.NotificationHandlerInfo{} @@ -185,13 +196,6 @@ func (np *Processor) ListenerNotificationHandlers() []event.NotificationHandlerI newEpoch.SetHandler(np.handleNewEpoch) handlers = append(handlers, newEpoch) - // new peer handler - addPeer := event.NotificationHandlerInfo{} - addPeer.SetType(addPeerNotification) - addPeer.SetScriptHash(np.netmapContract) - addPeer.SetHandler(np.handleAddPeer) - handlers = append(handlers, addPeer) - // update peer handler updatePeer := event.NotificationHandlerInfo{} updatePeer.SetType(updatePeerStateNotification) @@ -199,17 +203,56 @@ func (np *Processor) ListenerNotificationHandlers() []event.NotificationHandlerI updatePeer.SetHandler(np.handleUpdateState) handlers = append(handlers, updatePeer) + if !np.notaryDisabled { + return handlers + } + + // new peer handler + addPeer := event.NotificationHandlerInfo{} + addPeer.SetType(addPeerNotification) + addPeer.SetScriptHash(np.netmapContract) + addPeer.SetHandler(np.handleAddPeer) + handlers = append(handlers, addPeer) + return handlers } // ListenerNotaryParsers for the 'event.Listener' event producer. func (np *Processor) ListenerNotaryParsers() []event.NotaryParserInfo { - return nil + var ( + p event.NotaryParserInfo + + pp = make([]event.NotaryParserInfo, 0, 2) + ) + + p.SetMempoolType(mempoolevent.TransactionAdded) + p.SetScriptHash(np.netmapContract) + + // new peer + p.SetRequestType(netmapEvent.AddPeerNotaryEvent) + p.SetParser(netmapEvent.ParseAddPeerNotary) + pp = append(pp, p) + + return pp } // ListenerNotaryHandlers for the 'event.Listener' event producer. func (np *Processor) ListenerNotaryHandlers() []event.NotaryHandlerInfo { - return nil + var ( + h event.NotaryHandlerInfo + + hh = make([]event.NotaryHandlerInfo, 0, 2) + ) + + h.SetMempoolType(mempoolevent.TransactionAdded) + h.SetScriptHash(np.netmapContract) + + // new peer + h.SetRequestType(netmapEvent.AddPeerNotaryEvent) + h.SetHandler(np.handleAddPeer) + hh = append(hh, h) + + return hh } // TimersHandlers for the 'Timers' event producer. diff --git a/pkg/morph/event/netmap/add_peer.go b/pkg/morph/event/netmap/add_peer.go index 75327c1d1..e04a8979a 100644 --- a/pkg/morph/event/netmap/add_peer.go +++ b/pkg/morph/event/netmap/add_peer.go @@ -19,14 +19,16 @@ func (s AddPeer) Node() []byte { return s.node } +const expectedItemNumAddPeer = 1 + func ParseAddPeer(prms []stackitem.Item) (event.Event, error) { var ( ev AddPeer err error ) - if ln := len(prms); ln != 1 { - return nil, event.WrongNumberOfParameters(1, ln) + if ln := len(prms); ln != expectedItemNumAddPeer { + return nil, event.WrongNumberOfParameters(expectedItemNumAddPeer, ln) } ev.node, err = client.BytesFromStackItem(prms[0]) diff --git a/pkg/morph/event/netmap/add_peer_notary.go b/pkg/morph/event/netmap/add_peer_notary.go new file mode 100644 index 000000000..cdc6961a1 --- /dev/null +++ b/pkg/morph/event/netmap/add_peer_notary.go @@ -0,0 +1,47 @@ +package netmap + +import ( + "github.com/nspcc-dev/neo-go/pkg/vm/opcode" + "github.com/nspcc-dev/neofs-node/pkg/morph/event" +) + +func (s *AddPeer) setNode(v []byte) { + if v != nil { + s.node = v + } +} + +const ( + // AddPeerNotaryEvent is method name for netmap `addPeer` operation + // in `Netmap` contract. Is used as identificator for notary + // peer addition requests. + AddPeerNotaryEvent = "addPeer" +) + +// ParseAddPeerNotary from NotaryEvent into netmap event structure. +func ParseAddPeerNotary(ne event.NotaryEvent) (event.Event, error) { + var ( + ev AddPeer + currentOp opcode.Opcode + ) + + fieldNum := 0 + + for _, op := range ne.Params() { + currentOp = op.Code() + + switch { + case opcode.PUSHDATA1 <= currentOp && currentOp <= opcode.PUSHDATA4: + if fieldNum == expectedItemNumAddPeer { + return nil, event.UnexpectedArgNumErr(AddPeerNotaryEvent) + } + + ev.setNode(op.Param()) + fieldNum++ + default: + return nil, event.UnexpectedOpcode(AddPeerNotaryEvent, currentOp) + } + } + + return ev, nil +}