From 36c5e4c5274023f9d446da050b16ed9a82e636d8 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 12 Oct 2021 17:43:34 +0300 Subject: [PATCH] [#907] morph/event/container: Parse PutNamed event Implement `ParsePutNamedNotary` function which parses `PutNamed` structure from `event.NotaryEvent`. Share common code with `ParsePutNotary` function. Signed-off-by: Leonard Lyubich --- pkg/morph/event/container/put_notary.go | 68 +++++++++++++++++++++---- 1 file changed, 58 insertions(+), 10 deletions(-) diff --git a/pkg/morph/event/container/put_notary.go b/pkg/morph/event/container/put_notary.go index d95806c7..f489537f 100644 --- a/pkg/morph/event/container/put_notary.go +++ b/pkg/morph/event/container/put_notary.go @@ -1,6 +1,9 @@ package container import ( + "fmt" + + "github.com/nspcc-dev/neo-go/pkg/network/payload" "github.com/nspcc-dev/neo-go/pkg/vm/opcode" "github.com/nspcc-dev/neofs-node/pkg/morph/event" ) @@ -42,34 +45,79 @@ const ( // in `Container` contract. Is used as identificator for notary // put container requests. PutNotaryEvent = "put" + + // PutNotaryEvent is an ID of notary "put named container" notification. + PutNamedNotaryEvent = "putNamed" ) -// ParsePutNotary from NotaryEvent into container event structure. -func ParsePutNotary(ne event.NotaryEvent) (event.Event, error) { +func parsePutNotary(ev *Put, raw *payload.P2PNotaryRequest, ops []event.Op) error { var ( - ev Put currentOp opcode.Opcode + fieldNum = 0 ) - fieldNum := 0 - - for _, op := range ne.Params() { + for _, op := range ops { currentOp = op.Code() switch { case opcode.PUSHDATA1 <= currentOp && currentOp <= opcode.PUSHDATA4: if fieldNum == expectedItemNumPut { - return nil, event.UnexpectedArgNumErr(PutNotaryEvent) + return event.UnexpectedArgNumErr(PutNotaryEvent) } - putFieldSetters[fieldNum](&ev, op.Param()) + putFieldSetters[fieldNum](ev, op.Param()) fieldNum++ default: - return nil, event.UnexpectedOpcode(PutNotaryEvent, op.Code()) + return event.UnexpectedOpcode(PutNotaryEvent, op.Code()) } } - ev.notaryRequest = ne.Raw() + ev.notaryRequest = raw + + return nil +} + +// ParsePutNotary from NotaryEvent into container event structure. +func ParsePutNotary(ne event.NotaryEvent) (event.Event, error) { + var ev Put + + err := parsePutNotary(&ev, ne.Raw(), ne.Params()) + if err != nil { + return nil, err + } + + return ev, nil +} + +// ParsePutNamedNotary parses PutNamed event structure from generic event.NotaryEvent. +func ParsePutNamedNotary(ne event.NotaryEvent) (event.Event, error) { + ops := ne.Params() + + const putNamedAdditionalArgs = 2 // PutNamed has same args as Put + (name, zone) (2) + + if len(ops) != expectedItemNumPut+putNamedAdditionalArgs { + return nil, event.UnexpectedArgNumErr(PutNamedNotaryEvent) + } + + var ( + ev PutNamed + err error + ) + + ev.zone, err = event.StringFromOpcode(ops[0]) + if err != nil { + return nil, fmt.Errorf("parse arg zone: %w", err) + } + + ev.name, err = event.StringFromOpcode(ops[1]) + if err != nil { + return nil, fmt.Errorf("parse arg name: %w", err) + } + + err = parsePutNotary(&ev.Put, ne.Raw(), ops[putNamedAdditionalArgs:]) + if err != nil { + return nil, err + } return ev, nil }