2020-09-08 08:38:49 +00:00
|
|
|
package neofs
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
|
|
|
|
2021-10-22 10:06:08 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc/response/result/subscriptions"
|
2021-11-10 11:05:51 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-09-08 08:38:49 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
key []byte
|
|
|
|
value []byte
|
2020-11-05 13:26:28 +00:00
|
|
|
id []byte
|
2021-11-10 11:05:51 +00:00
|
|
|
|
|
|
|
// txHash is used in notary environmental
|
|
|
|
// for calculating unique but same for
|
|
|
|
// all notification receivers values.
|
|
|
|
txHash util.Uint256
|
|
|
|
}
|
|
|
|
|
|
|
|
// TxHash returns hash of the TX with new epoch
|
|
|
|
// notification.
|
|
|
|
func (u Config) TxHash() util.Uint256 {
|
|
|
|
return u.txHash
|
2020-09-08 08:38:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MorphEvent implements Neo:Morph Event interface.
|
|
|
|
func (Config) MorphEvent() {}
|
|
|
|
|
2020-11-05 13:26:28 +00:00
|
|
|
func (u Config) ID() []byte { return u.id }
|
|
|
|
|
2020-09-08 08:38:49 +00:00
|
|
|
func (u Config) Key() []byte { return u.key }
|
|
|
|
|
|
|
|
func (u Config) Value() []byte { return u.value }
|
|
|
|
|
2021-10-22 10:06:08 +00:00
|
|
|
func ParseConfig(e *subscriptions.NotificationEvent) (event.Event, error) {
|
2020-09-08 08:38:49 +00:00
|
|
|
var (
|
|
|
|
ev Config
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2021-10-22 10:06:08 +00:00
|
|
|
params, err := event.ParseStackArray(e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not parse stack items from notify event: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-11-05 13:26:28 +00:00
|
|
|
if ln := len(params); ln != 3 {
|
|
|
|
return nil, event.WrongNumberOfParameters(3, ln)
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse id
|
|
|
|
ev.id, err = client.BytesFromStackItem(params[0])
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not get config update id: %w", err)
|
2020-09-08 08:38:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse key
|
2020-11-05 13:26:28 +00:00
|
|
|
ev.key, err = client.BytesFromStackItem(params[1])
|
2020-09-08 08:38:49 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not get config key: %w", err)
|
2020-09-08 08:38:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse value
|
2020-11-05 13:26:28 +00:00
|
|
|
ev.value, err = client.BytesFromStackItem(params[2])
|
2020-09-08 08:38:49 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not get config value: %w", err)
|
2020-09-08 08:38:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 11:05:51 +00:00
|
|
|
ev.txHash = e.Container
|
|
|
|
|
2020-09-08 08:38:49 +00:00
|
|
|
return ev, nil
|
|
|
|
}
|