[#815] morph/event/netmap: Add addPeer notary notification support

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-09-08 16:54:45 +03:00 committed by Alex Vanin
parent 01df4ffa61
commit 00caed8d3d
4 changed files with 113 additions and 20 deletions

View file

@ -668,6 +668,7 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error
addrvalidator.New(), addrvalidator.New(),
locodeValidator, locodeValidator,
), ),
NotaryDisabled: server.sideNotaryConfig.disabled,
}) })
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/nspcc-dev/neo-go/pkg/core/mempoolevent"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neofs-api-go/pkg/netmap" "github.com/nspcc-dev/neofs-api-go/pkg/netmap"
container "github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper" container "github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper"
@ -66,6 +67,8 @@ type (
handleAlphabetSync event.Handler handleAlphabetSync event.Handler
nodeValidator NodeValidator nodeValidator NodeValidator
notaryDisabled bool
} }
// Params of the processor constructor. // Params of the processor constructor.
@ -87,6 +90,8 @@ type (
AlphabetSyncHandler event.Handler AlphabetSyncHandler event.Handler
NodeValidator NodeValidator NodeValidator NodeValidator
NotaryDisabled bool
} }
) )
@ -143,12 +148,14 @@ func New(p *Params) (*Processor, error) {
handleAlphabetSync: p.AlphabetSyncHandler, handleAlphabetSync: p.AlphabetSyncHandler,
nodeValidator: p.NodeValidator, nodeValidator: p.NodeValidator,
notaryDisabled: p.NotaryDisabled,
}, nil }, nil
} }
// ListenerNotificationParsers for the 'event.Listener' event producer. // ListenerNotificationParsers for the 'event.Listener' event producer.
func (np *Processor) ListenerNotificationParsers() []event.NotificationParserInfo { func (np *Processor) ListenerNotificationParsers() []event.NotificationParserInfo {
var parsers []event.NotificationParserInfo parsers := make([]event.NotificationParserInfo, 0, 3)
// new epoch event // new epoch event
newEpoch := event.NotificationParserInfo{} newEpoch := event.NotificationParserInfo{}
@ -157,13 +164,6 @@ func (np *Processor) ListenerNotificationParsers() []event.NotificationParserInf
newEpoch.SetParser(netmapEvent.ParseNewEpoch) newEpoch.SetParser(netmapEvent.ParseNewEpoch)
parsers = append(parsers, newEpoch) 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 // update peer event
updatePeer := event.NotificationParserInfo{} updatePeer := event.NotificationParserInfo{}
updatePeer.SetType(updatePeerStateNotification) updatePeer.SetType(updatePeerStateNotification)
@ -171,12 +171,23 @@ func (np *Processor) ListenerNotificationParsers() []event.NotificationParserInf
updatePeer.SetParser(netmapEvent.ParseUpdatePeer) updatePeer.SetParser(netmapEvent.ParseUpdatePeer)
parsers = append(parsers, updatePeer) 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 return parsers
} }
// ListenerNotificationHandlers for the 'event.Listener' event producer. // ListenerNotificationHandlers for the 'event.Listener' event producer.
func (np *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo { func (np *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo {
var handlers []event.NotificationHandlerInfo handlers := make([]event.NotificationHandlerInfo, 0, 3)
// new epoch handler // new epoch handler
newEpoch := event.NotificationHandlerInfo{} newEpoch := event.NotificationHandlerInfo{}
@ -185,13 +196,6 @@ func (np *Processor) ListenerNotificationHandlers() []event.NotificationHandlerI
newEpoch.SetHandler(np.handleNewEpoch) newEpoch.SetHandler(np.handleNewEpoch)
handlers = append(handlers, newEpoch) 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 // update peer handler
updatePeer := event.NotificationHandlerInfo{} updatePeer := event.NotificationHandlerInfo{}
updatePeer.SetType(updatePeerStateNotification) updatePeer.SetType(updatePeerStateNotification)
@ -199,17 +203,56 @@ func (np *Processor) ListenerNotificationHandlers() []event.NotificationHandlerI
updatePeer.SetHandler(np.handleUpdateState) updatePeer.SetHandler(np.handleUpdateState)
handlers = append(handlers, updatePeer) 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 return handlers
} }
// ListenerNotaryParsers for the 'event.Listener' event producer. // ListenerNotaryParsers for the 'event.Listener' event producer.
func (np *Processor) ListenerNotaryParsers() []event.NotaryParserInfo { 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. // ListenerNotaryHandlers for the 'event.Listener' event producer.
func (np *Processor) ListenerNotaryHandlers() []event.NotaryHandlerInfo { 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. // TimersHandlers for the 'Timers' event producer.

View file

@ -19,14 +19,16 @@ func (s AddPeer) Node() []byte {
return s.node return s.node
} }
const expectedItemNumAddPeer = 1
func ParseAddPeer(prms []stackitem.Item) (event.Event, error) { func ParseAddPeer(prms []stackitem.Item) (event.Event, error) {
var ( var (
ev AddPeer ev AddPeer
err error err error
) )
if ln := len(prms); ln != 1 { if ln := len(prms); ln != expectedItemNumAddPeer {
return nil, event.WrongNumberOfParameters(1, ln) return nil, event.WrongNumberOfParameters(expectedItemNumAddPeer, ln)
} }
ev.node, err = client.BytesFromStackItem(prms[0]) ev.node, err = client.BytesFromStackItem(prms[0])

View file

@ -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
}