2020-09-03 14:04:04 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/elliptic"
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2020-09-03 14:04:04 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2020-10-26 14:46:15 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-11-16 10:26:35 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
|
|
|
v2netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap"
|
2020-09-03 14:04:04 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UpdatePeer struct {
|
|
|
|
publicKey *keys.PublicKey
|
2020-11-16 10:26:35 +00:00
|
|
|
status netmap.NodeState
|
2020-09-03 14:04:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MorphEvent implements Neo:Morph Event interface.
|
|
|
|
func (UpdatePeer) MorphEvent() {}
|
|
|
|
|
2020-11-16 10:26:35 +00:00
|
|
|
func (s UpdatePeer) Status() netmap.NodeState {
|
2020-09-03 14:04:04 +00:00
|
|
|
return s.status
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s UpdatePeer) PublicKey() *keys.PublicKey {
|
|
|
|
return s.publicKey
|
|
|
|
}
|
|
|
|
|
2020-10-26 14:46:15 +00:00
|
|
|
func ParseUpdatePeer(prms []stackitem.Item) (event.Event, error) {
|
2020-09-03 14:04:04 +00:00
|
|
|
var (
|
|
|
|
ev UpdatePeer
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if ln := len(prms); ln != 2 {
|
|
|
|
return nil, event.WrongNumberOfParameters(2, ln)
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse public key
|
2021-01-11 11:52:48 +00:00
|
|
|
key, err := client.BytesFromStackItem(prms[1])
|
2020-09-03 14:04:04 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not get public key: %w", err)
|
2020-09-03 14:04:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ev.publicKey, err = keys.NewPublicKeyFromBytes(key, elliptic.P256())
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not parse public key: %w", err)
|
2020-09-03 14:04:04 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 14:46:15 +00:00
|
|
|
// parse node status
|
2021-01-11 11:52:48 +00:00
|
|
|
st, err := client.IntFromStackItem(prms[0])
|
2020-10-26 14:46:15 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not get node status: %w", err)
|
2020-10-26 14:46:15 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 10:26:35 +00:00
|
|
|
ev.status = netmap.NodeStateFromV2(v2netmap.NodeState(st))
|
2020-10-26 14:46:15 +00:00
|
|
|
|
2020-09-03 14:04:04 +00:00
|
|
|
return ev, nil
|
|
|
|
}
|