2021-11-26 12:19:20 +00:00
|
|
|
package subnetevents
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-07-28 16:22:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2021-11-26 12:19:20 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/payload"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Put structures information about the notification generated by Put method of Subnet contract.
|
|
|
|
type Put struct {
|
|
|
|
notaryRequest *payload.P2PNotaryRequest
|
|
|
|
|
|
|
|
txHash util.Uint256
|
|
|
|
|
|
|
|
id []byte
|
|
|
|
|
2021-11-28 09:43:03 +00:00
|
|
|
owner []byte
|
2021-11-26 12:19:20 +00:00
|
|
|
|
|
|
|
info []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// MorphEvent implements Neo:Morph Event interface.
|
|
|
|
func (Put) MorphEvent() {}
|
|
|
|
|
|
|
|
// ID returns identifier of the creating subnet in a binary format of NeoFS API protocol.
|
|
|
|
func (x Put) ID() []byte {
|
|
|
|
return x.id
|
|
|
|
}
|
|
|
|
|
2021-11-28 09:43:03 +00:00
|
|
|
// Owner returns subnet owner's public key in a binary format.
|
2021-11-26 12:19:20 +00:00
|
|
|
func (x Put) Owner() []byte {
|
2021-11-28 09:43:03 +00:00
|
|
|
return x.owner
|
2021-11-26 12:19:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Info returns information about the subnet in a binary format of NeoFS API protocol.
|
|
|
|
func (x Put) Info() []byte {
|
|
|
|
return x.info
|
|
|
|
}
|
|
|
|
|
|
|
|
// TxHash returns hash of the transaction which thrown the notification event.
|
2021-11-28 09:43:03 +00:00
|
|
|
// Makes sense only in notary environments.
|
2021-11-26 12:19:20 +00:00
|
|
|
func (x Put) TxHash() util.Uint256 {
|
|
|
|
return x.txHash
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotaryMainTx returns main transaction of the request in the Notary service.
|
|
|
|
// Returns nil in non-notary environments.
|
|
|
|
func (x Put) NotaryMainTx() *transaction.Transaction {
|
|
|
|
if x.notaryRequest != nil {
|
|
|
|
return x.notaryRequest.MainTransaction
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// number of items in notification about subnet creation.
|
|
|
|
const itemNumPut = 3
|
|
|
|
|
|
|
|
// ParsePut parses the notification about the creation of a subnet which has been thrown
|
|
|
|
// by the appropriate method of the subnet contract.
|
|
|
|
//
|
|
|
|
// Resulting event is of Put type.
|
2022-07-28 16:22:32 +00:00
|
|
|
func ParsePut(e *state.ContainedNotificationEvent) (event.Event, error) {
|
2021-11-26 12:19:20 +00:00
|
|
|
var (
|
|
|
|
put Put
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
items, err := event.ParseStackArray(e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("parse stack array: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ln := len(items); ln != itemNumPut {
|
|
|
|
return nil, event.WrongNumberOfParameters(itemNumPut, ln)
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse ID
|
|
|
|
put.id, err = client.BytesFromStackItem(items[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("id item: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse owner
|
2021-11-28 09:43:03 +00:00
|
|
|
put.owner, err = client.BytesFromStackItem(items[1])
|
2021-11-26 12:19:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("owner item: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-11-28 09:43:03 +00:00
|
|
|
// parse info about subnet
|
2021-11-26 12:19:20 +00:00
|
|
|
put.info, err = client.BytesFromStackItem(items[2])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("info item: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
put.txHash = e.Container
|
|
|
|
|
|
|
|
return put, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseNotaryPut parses the notary notification about the creation of a subnet which has been
|
|
|
|
// thrown by the appropriate method of the subnet contract.
|
|
|
|
//
|
|
|
|
// Resulting event is of Put type.
|
|
|
|
func ParseNotaryPut(e event.NotaryEvent) (event.Event, error) {
|
|
|
|
var put Put
|
|
|
|
|
|
|
|
put.notaryRequest = e.Raw()
|
|
|
|
if put.notaryRequest == nil {
|
|
|
|
panic(fmt.Sprintf("nil %T in notary environment", put.notaryRequest))
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
|
|
|
|
prms = e.Params()
|
|
|
|
)
|
|
|
|
|
|
|
|
if ln := len(prms); ln != itemNumPut {
|
|
|
|
return nil, event.WrongNumberOfParameters(itemNumPut, ln)
|
|
|
|
}
|
|
|
|
|
2021-11-28 09:43:03 +00:00
|
|
|
// parse info about subnet
|
2021-11-26 12:19:20 +00:00
|
|
|
put.info, err = event.BytesFromOpcode(prms[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("info param: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-11-28 09:43:03 +00:00
|
|
|
// parse owner
|
|
|
|
put.owner, err = event.BytesFromOpcode(prms[1])
|
2021-11-26 12:19:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("creator param: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-11-28 09:43:03 +00:00
|
|
|
// parse ID
|
2021-11-26 12:19:20 +00:00
|
|
|
put.id, err = event.BytesFromOpcode(prms[2])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("id param: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return put, nil
|
|
|
|
}
|