[#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 <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-10-12 17:43:34 +03:00 committed by Alex Vanin
parent a9494412da
commit 36c5e4c527

View file

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