[#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 <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-08-25 14:35:17 +03:00 committed by Pavel Karpy
parent 9921358f09
commit 13f1273e82
3 changed files with 60 additions and 9 deletions

View file

@ -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()),

View file

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

View file

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