2021-08-25 11:29:58 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2021-10-12 14:43:34 +00:00
|
|
|
"fmt"
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/morph/event"
|
2021-10-12 14:43:34 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/payload"
|
2021-08-25 11:29:58 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 08:17:19 +00:00
|
|
|
var putFieldSetters = []func(*Put, []byte){
|
2021-08-25 11:29:58 +00:00
|
|
|
// 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"
|
2021-10-12 14:43:34 +00:00
|
|
|
|
|
|
|
// PutNotaryEvent is an ID of notary "put named container" notification.
|
|
|
|
PutNamedNotaryEvent = "putNamed"
|
2021-08-25 11:29:58 +00:00
|
|
|
)
|
|
|
|
|
2021-10-12 14:43:34 +00:00
|
|
|
func parsePutNotary(ev *Put, raw *payload.P2PNotaryRequest, ops []event.Op) error {
|
2021-09-09 11:41:04 +00:00
|
|
|
var (
|
|
|
|
currentOp opcode.Opcode
|
2021-10-12 14:43:34 +00:00
|
|
|
fieldNum = 0
|
2021-09-09 11:41:04 +00:00
|
|
|
)
|
2021-08-25 11:29:58 +00:00
|
|
|
|
2021-10-12 14:43:34 +00:00
|
|
|
for _, op := range ops {
|
2021-09-09 11:41:04 +00:00
|
|
|
currentOp = op.Code()
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case opcode.PUSHDATA1 <= currentOp && currentOp <= opcode.PUSHDATA4:
|
2021-09-09 08:48:13 +00:00
|
|
|
if fieldNum == expectedItemNumPut {
|
2021-10-12 14:43:34 +00:00
|
|
|
return event.UnexpectedArgNumErr(PutNotaryEvent)
|
2021-08-25 11:29:58 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 14:43:34 +00:00
|
|
|
putFieldSetters[fieldNum](ev, op.Param())
|
2021-08-25 11:29:58 +00:00
|
|
|
fieldNum++
|
|
|
|
default:
|
2021-10-12 14:43:34 +00:00
|
|
|
return event.UnexpectedOpcode(PutNotaryEvent, op.Code())
|
2021-08-25 11:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-12 14:43:34 +00:00
|
|
|
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
|
|
|
|
}
|
2021-08-25 11:29:58 +00:00
|
|
|
|
|
|
|
return ev, nil
|
|
|
|
}
|