From 13f1273e829a29e87ef054839c2ad2f699be5a1c Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Wed, 25 Aug 2021 14:35:17 +0300 Subject: [PATCH] [#770] ir/container: Support notary `Put` Implement `NotaryContractProcessor` by IR container processor. Add support for notary `put` container operation. Do not parse `put` non-notary notifications in notary enabled environment. Signed-off-by: Pavel Karpy --- .../processors/container/process_container.go | 10 +++- .../processors/container/processor.go | 48 +++++++++++++++---- pkg/morph/event/container/put.go | 11 +++++ 3 files changed, 60 insertions(+), 9 deletions(-) diff --git a/pkg/innerring/processors/container/process_container.go b/pkg/innerring/processors/container/process_container.go index c5f82ac4..d5ca7ff5 100644 --- a/pkg/innerring/processors/container/process_container.go +++ b/pkg/innerring/processors/container/process_container.go @@ -86,7 +86,15 @@ func (cp *Processor) checkPutContainer(e *containerEvent.Put) error { } func (cp *Processor) approvePutContainer(e *containerEvent.Put) { - err := cp.cnrClient.Put(e.Container(), e.PublicKey(), e.Signature(), e.SessionToken()) + var err error + + if nr := e.NotaryRequest(); nr != nil { + // put event was received via Notary service + err = cp.cnrClient.Morph().NotarySignAndInvokeTX(nr.MainTransaction) + } else { + // put event was received via notification service + err = cp.cnrClient.Put(e.Container(), e.PublicKey(), e.Signature(), e.SessionToken()) + } if err != nil { cp.log.Error("could not approve put container", zap.String("error", err.Error()), diff --git a/pkg/innerring/processors/container/processor.go b/pkg/innerring/processors/container/processor.go index 66be56ca..b36bed1f 100644 --- a/pkg/innerring/processors/container/processor.go +++ b/pkg/innerring/processors/container/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-node/pkg/morph/client/container/wrapper" neofsid "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofsid/wrapper" @@ -28,6 +29,7 @@ type ( cnrClient *wrapper.Wrapper // notary must be enabled idClient *neofsid.ClientWrapper netState NetworkState + notaryDisabled bool } // Params of the processor constructor. @@ -39,6 +41,7 @@ type ( ContainerClient *wrapper.Wrapper NeoFSIDClient *neofsid.ClientWrapper NetworkState NetworkState + NotaryDisabled bool } ) @@ -89,6 +92,7 @@ func New(p *Params) (*Processor, error) { cnrClient: p.ContainerClient, idClient: p.NeoFSIDClient, netState: p.NetworkState, + notaryDisabled: p.NotaryDisabled, }, nil } @@ -102,10 +106,12 @@ func (cp *Processor) ListenerNotificationParsers() []event.NotificationParserInf p.SetScriptHash(cp.containerContract) - // container put - p.SetType(event.TypeFromString(putNotification)) - p.SetParser(containerEvent.ParsePut) - parsers = append(parsers, p) + if cp.notaryDisabled { + // container put + p.SetType(event.TypeFromString(putNotification)) + p.SetParser(containerEvent.ParsePut) + parsers = append(parsers, p) + } // container delete p.SetType(event.TypeFromString(deleteNotification)) @@ -130,10 +136,12 @@ func (cp *Processor) ListenerNotificationHandlers() []event.NotificationHandlerI h.SetScriptHash(cp.containerContract) - // container put - h.SetType(event.TypeFromString(putNotification)) - h.SetHandler(cp.handlePut) - handlers = append(handlers, h) + if cp.notaryDisabled { + // container put + h.SetType(event.TypeFromString(putNotification)) + h.SetHandler(cp.handlePut) + handlers = append(handlers, h) + } // container delete h.SetType(event.TypeFromString(deleteNotification)) @@ -148,6 +156,30 @@ func (cp *Processor) ListenerNotificationHandlers() []event.NotificationHandlerI return handlers } +// ListenerNotaryParsers for the 'event.Listener' notary event producer. +func (cp *Processor) ListenerNotaryParsers() []event.NotaryParserInfo { + var p event.NotaryParserInfo + + p.SetMempoolType(mempoolevent.TransactionAdded) + p.SetRequestType(containerEvent.PutNotaryEvent) + p.SetScriptHash(cp.containerContract) + p.SetParser(containerEvent.ParsePutNotary) + + return []event.NotaryParserInfo{p} +} + +// ListenerNotaryHandlers for the 'event.Listener' notary event producer. +func (cp *Processor) ListenerNotaryHandlers() []event.NotaryHandlerInfo { + var h event.NotaryHandlerInfo + + h.SetMempoolType(mempoolevent.TransactionAdded) + h.SetRequestType(containerEvent.PutNotaryEvent) + h.SetScriptHash(cp.containerContract) + h.SetHandler(cp.handlePut) + + return []event.NotaryHandlerInfo{h} +} + // TimersHandlers for the 'Timers' event producer. func (cp *Processor) TimersHandlers() []event.NotificationHandlerInfo { return nil diff --git a/pkg/morph/event/container/put.go b/pkg/morph/event/container/put.go index da10caca..c5af119c 100644 --- a/pkg/morph/event/container/put.go +++ b/pkg/morph/event/container/put.go @@ -3,6 +3,7 @@ package container import ( "fmt" + "github.com/nspcc-dev/neo-go/pkg/network/payload" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/event" @@ -14,6 +15,10 @@ type Put struct { signature []byte publicKey []byte token []byte + + // For notary notifications only. + // Contains raw transactions of notary request. + notaryRequest *payload.P2PNotaryRequest } const expectedItemNumPut = 4 @@ -36,6 +41,12 @@ func (p Put) SessionToken() []byte { return p.token } +// NotaryRequest returns raw notary request if notification +// was received via notary service. Otherwise, returns nil. +func (p Put) NotaryRequest() *payload.P2PNotaryRequest { + return p.notaryRequest +} + // ParsePut from notification into container event structure. func ParsePut(params []stackitem.Item) (event.Event, error) { var (