2022-12-23 17:35:35 +00:00
|
|
|
package frostfs
|
2020-09-08 08:37:58 +00:00
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2020-09-08 08:37:58 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
|
2022-07-28 16:22:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2021-11-11 08:59:14 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-10-26 14:46:15 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-09-08 08:37:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Bind struct {
|
2023-04-26 13:25:50 +00:00
|
|
|
BindCommon
|
2021-05-31 18:09:02 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 13:25:50 +00:00
|
|
|
type BindCommon struct {
|
|
|
|
UserValue []byte
|
|
|
|
KeysValue [][]byte
|
2021-11-11 08:59:14 +00:00
|
|
|
|
2023-04-26 13:25:50 +00:00
|
|
|
// TxHashValue is used in notary environmental
|
2021-11-11 08:59:14 +00:00
|
|
|
// for calculating unique but same for
|
|
|
|
// all notification receivers values.
|
2023-04-26 13:25:50 +00:00
|
|
|
TxHashValue util.Uint256
|
2021-11-11 08:59:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TxHash returns hash of the TX with new epoch
|
|
|
|
// notification.
|
2023-04-26 13:25:50 +00:00
|
|
|
func (b BindCommon) TxHash() util.Uint256 {
|
|
|
|
return b.TxHashValue
|
2020-09-08 08:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MorphEvent implements Neo:Morph Event interface.
|
2023-04-26 13:25:50 +00:00
|
|
|
func (BindCommon) MorphEvent() {}
|
2020-09-08 08:37:58 +00:00
|
|
|
|
2023-04-26 13:25:50 +00:00
|
|
|
func (b BindCommon) Keys() [][]byte { return b.KeysValue }
|
2020-09-08 08:37:58 +00:00
|
|
|
|
2023-04-26 13:25:50 +00:00
|
|
|
func (b BindCommon) User() []byte { return b.UserValue }
|
2020-09-08 08:37:58 +00:00
|
|
|
|
2022-07-28 16:22:32 +00:00
|
|
|
func ParseBind(e *state.ContainedNotificationEvent) (event.Event, error) {
|
2020-09-08 08:37:58 +00:00
|
|
|
var (
|
|
|
|
ev Bind
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-04-26 13:25:50 +00:00
|
|
|
err = parseBind(&ev.BindCommon, params)
|
2021-05-31 18:09:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-09-08 08:37:58 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 13:25:50 +00:00
|
|
|
ev.TxHashValue = e.Container
|
2021-11-11 08:59:14 +00:00
|
|
|
|
2021-05-31 18:09:02 +00:00
|
|
|
return ev, nil
|
|
|
|
}
|
|
|
|
|
2023-04-26 13:25:50 +00:00
|
|
|
func parseBind(dst *BindCommon, params []stackitem.Item) error {
|
2021-05-31 18:09:02 +00:00
|
|
|
if ln := len(params); ln != 2 {
|
|
|
|
return event.WrongNumberOfParameters(2, ln)
|
2020-09-08 08:37:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 18:09:02 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
// parse user
|
2023-04-26 13:25:50 +00:00
|
|
|
dst.UserValue, err = client.BytesFromStackItem(params[0])
|
2020-09-08 08:37:58 +00:00
|
|
|
if err != nil {
|
2021-05-31 18:09:02 +00:00
|
|
|
return fmt.Errorf("could not get bind user: %w", err)
|
2020-09-08 08:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse keys
|
2020-10-26 14:46:15 +00:00
|
|
|
bindKeys, err := client.ArrayFromStackItem(params[1])
|
2020-09-08 08:37:58 +00:00
|
|
|
if err != nil {
|
2021-05-31 18:09:02 +00:00
|
|
|
return fmt.Errorf("could not get bind keys: %w", err)
|
2020-09-08 08:37:58 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 13:25:50 +00:00
|
|
|
dst.KeysValue = make([][]byte, 0, len(bindKeys))
|
2021-05-31 18:09:02 +00:00
|
|
|
|
2020-09-08 08:37:58 +00:00
|
|
|
for i := range bindKeys {
|
2020-10-26 14:46:15 +00:00
|
|
|
rawKey, err := client.BytesFromStackItem(bindKeys[i])
|
2020-09-08 08:37:58 +00:00
|
|
|
if err != nil {
|
2021-05-31 18:09:02 +00:00
|
|
|
return fmt.Errorf("could not get bind public key: %w", err)
|
2020-09-08 08:37:58 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 13:25:50 +00:00
|
|
|
dst.KeysValue = append(dst.KeysValue, rawKey)
|
2020-09-08 08:37:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 18:09:02 +00:00
|
|
|
return nil
|
2020-09-08 08:37:58 +00:00
|
|
|
}
|