From d40f898ee52be0e9ef4e601573630c9fe67f3f5d Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Wed, 25 Aug 2021 14:29:58 +0300 Subject: [PATCH] [#770] morph/event/container: Add notary `Put` parser Signed-off-by: Pavel Karpy --- pkg/morph/event/container/put_notary.go | 73 +++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 pkg/morph/event/container/put_notary.go diff --git a/pkg/morph/event/container/put_notary.go b/pkg/morph/event/container/put_notary.go new file mode 100644 index 00000000..14008b5a --- /dev/null +++ b/pkg/morph/event/container/put_notary.go @@ -0,0 +1,73 @@ +package container + +import ( + "fmt" + + "github.com/nspcc-dev/neo-go/pkg/vm/opcode" + "github.com/nspcc-dev/neofs-node/pkg/morph/event" +) + +func (p *Put) setRawContainer(v []byte) { + if v != nil { + p.rawContainer = v + } +} + +func (p *Put) setSignature(v []byte) { + if v != nil { + p.signature = v + } +} + +func (p *Put) setPublicKey(v []byte) { + if v != nil { + p.publicKey = v + } +} + +func (p *Put) setToken(v []byte) { + if v != nil { + p.token = v + } +} + +var fieldSetters = []func(*Put, []byte){ + // order on stack is reversed + (*Put).setToken, + (*Put).setPublicKey, + (*Put).setSignature, + (*Put).setRawContainer, +} + +const ( + // PutNotaryEvent is method name for container put operations + // in `Container` contract. Is used as identificator for notary + // put container requests. + PutNotaryEvent = "put" +) + +var errUnexpectedArgumentAmount = fmt.Errorf("unexpected arguments amount in %s call", PutNotaryEvent) + +// ParsePutNotary from NotaryEvent into container event structure. +func ParsePutNotary(ne event.NotaryEvent) (event.Event, error) { + var ev Put + + fieldNum := 0 + + for _, op := range ne.Params() { + switch op.Code() { + case opcode.PUSHDATA1, opcode.PUSHDATA2, opcode.PUSHDATA4: + if fieldNum > expectedItemNumPut { + return nil, errUnexpectedArgumentAmount + } + + fieldSetters[fieldNum](&ev, op.Param()) + fieldNum++ + default: + } + } + + ev.notaryRequest = ne.Raw() + + return ev, nil +}