2020-07-24 13:54:03 +00:00
|
|
|
package invoke
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2020-09-03 14:32:37 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2020-07-24 13:54:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-10-29 14:27:13 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-10-30 11:26:57 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
2020-07-24 13:54:03 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
|
|
|
)
|
|
|
|
|
2020-09-03 14:32:37 +00:00
|
|
|
type (
|
|
|
|
UpdatePeerArgs struct {
|
|
|
|
Key *keys.PublicKey
|
2020-11-16 10:26:35 +00:00
|
|
|
Status netmap.NodeState
|
2020-09-03 14:32:37 +00:00
|
|
|
}
|
2020-09-08 08:48:26 +00:00
|
|
|
|
|
|
|
SetConfigArgs struct {
|
2020-11-05 13:41:44 +00:00
|
|
|
ID []byte
|
2020-09-08 08:48:26 +00:00
|
|
|
Key []byte
|
|
|
|
Value []byte
|
|
|
|
}
|
2020-09-03 14:32:37 +00:00
|
|
|
)
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
const (
|
2020-10-29 14:27:13 +00:00
|
|
|
getEpochMethod = "epoch"
|
|
|
|
setNewEpochMethod = "newEpoch"
|
|
|
|
approvePeerMethod = "addPeer"
|
|
|
|
updatePeerStateMethod = "updateState"
|
2021-04-15 13:20:58 +00:00
|
|
|
setConfigMethod = "setConfig"
|
2021-04-29 13:39:09 +00:00
|
|
|
setInnerRingMethod = "updateInnerRing"
|
2020-10-29 14:27:13 +00:00
|
|
|
getNetmapSnapshotMethod = "netmap"
|
2020-07-24 13:54:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Epoch return epoch value from contract.
|
|
|
|
func Epoch(cli *client.Client, con util.Uint160) (int64, error) {
|
|
|
|
if cli == nil {
|
|
|
|
return 0, client.ErrNilClient
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := cli.TestInvoke(con, getEpochMethod)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2020-08-27 11:57:52 +00:00
|
|
|
epoch, err := client.IntFromStackItem(val[0])
|
2020-07-24 13:54:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return epoch, nil
|
|
|
|
}
|
|
|
|
|
2020-09-08 08:48:26 +00:00
|
|
|
// SetNewEpoch invokes newEpoch method.
|
2021-04-29 13:39:09 +00:00
|
|
|
func SetNewEpoch(cli *client.Client, con util.Uint160, fee SideFeeProvider, epoch uint64) error {
|
2020-07-24 13:54:03 +00:00
|
|
|
if cli == nil {
|
|
|
|
return client.ErrNilClient
|
|
|
|
}
|
|
|
|
|
2021-05-21 07:39:21 +00:00
|
|
|
return cli.NotaryInvoke(con, fee.SideChainFee(), setNewEpochMethod, int64(epoch))
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
2020-09-03 14:32:37 +00:00
|
|
|
|
2020-09-08 08:48:26 +00:00
|
|
|
// ApprovePeer invokes addPeer method.
|
2021-04-29 13:39:09 +00:00
|
|
|
func ApprovePeer(cli *client.Client, con util.Uint160, fee SideFeeProvider, peer []byte) error {
|
2020-09-03 14:32:37 +00:00
|
|
|
if cli == nil {
|
|
|
|
return client.ErrNilClient
|
|
|
|
}
|
|
|
|
|
2021-05-21 07:39:21 +00:00
|
|
|
return cli.NotaryInvoke(con, fee.SideChainFee(), approvePeerMethod, peer)
|
2020-09-03 14:32:37 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 08:48:26 +00:00
|
|
|
// UpdatePeerState invokes addPeer method.
|
2021-04-29 13:39:09 +00:00
|
|
|
func UpdatePeerState(cli *client.Client, con util.Uint160, fee SideFeeProvider, args *UpdatePeerArgs) error {
|
2020-09-03 14:32:37 +00:00
|
|
|
if cli == nil {
|
|
|
|
return client.ErrNilClient
|
|
|
|
}
|
|
|
|
|
2021-05-21 07:39:21 +00:00
|
|
|
return cli.NotaryInvoke(con, fee.SideChainFee(), updatePeerStateMethod,
|
2021-01-11 13:27:35 +00:00
|
|
|
int64(args.Status.ToV2()),
|
2020-10-05 15:54:46 +00:00
|
|
|
args.Key.Bytes(),
|
2020-09-03 14:32:37 +00:00
|
|
|
)
|
|
|
|
}
|
2020-09-08 08:48:26 +00:00
|
|
|
|
|
|
|
// SetConfig invokes setConfig method.
|
2021-04-29 13:39:09 +00:00
|
|
|
func SetConfig(cli *client.Client, con util.Uint160, fee SideFeeProvider, args *SetConfigArgs) error {
|
2020-09-08 08:48:26 +00:00
|
|
|
if cli == nil {
|
|
|
|
return client.ErrNilClient
|
|
|
|
}
|
|
|
|
|
2021-05-21 07:39:21 +00:00
|
|
|
return cli.NotaryInvoke(con, fee.SideChainFee(), setConfigMethod,
|
2020-11-05 13:41:44 +00:00
|
|
|
args.ID,
|
2020-09-08 08:48:26 +00:00
|
|
|
args.Key,
|
|
|
|
args.Value,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-29 13:39:09 +00:00
|
|
|
// SetInnerRing invokes update inner ring method. This should be used only
|
|
|
|
// without notary support.
|
|
|
|
func SetInnerRing(cli *client.Client, con util.Uint160, fee SideFeeProvider, list keys.PublicKeys) error {
|
|
|
|
if cli == nil {
|
|
|
|
return client.ErrNilClient
|
|
|
|
}
|
|
|
|
|
2021-05-21 07:39:21 +00:00
|
|
|
return cli.NotaryInvoke(con, fee.SideChainFee(), setInnerRingMethod, list)
|
2021-04-29 13:39:09 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 14:27:13 +00:00
|
|
|
// NetmapSnapshot returns current netmap node infos.
|
2020-10-30 11:26:57 +00:00
|
|
|
// Consider using pkg/morph/client/netmap for this.
|
|
|
|
func NetmapSnapshot(cli *client.Client, con util.Uint160) (*netmap.Netmap, error) {
|
2020-10-29 14:27:13 +00:00
|
|
|
if cli == nil {
|
|
|
|
return nil, client.ErrNilClient
|
|
|
|
}
|
|
|
|
|
|
|
|
rawNetmapStack, err := cli.TestInvoke(con, getNetmapSnapshotMethod)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ln := len(rawNetmapStack); ln != 1 {
|
|
|
|
return nil, errors.New("invalid RPC response")
|
|
|
|
}
|
|
|
|
|
|
|
|
rawNodeInfos, err := client.ArrayFromStackItem(rawNetmapStack[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-05 15:51:43 +00:00
|
|
|
result := make([]netmap.NodeInfo, 0, len(rawNodeInfos))
|
2020-10-29 14:27:13 +00:00
|
|
|
|
|
|
|
for i := range rawNodeInfos {
|
|
|
|
nodeInfo, err := peerInfoFromStackItem(rawNodeInfos[i])
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("invalid RPC response: %w", err)
|
2020-10-29 14:27:13 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 10:26:35 +00:00
|
|
|
result = append(result, *nodeInfo)
|
2020-10-29 14:27:13 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 15:51:43 +00:00
|
|
|
return netmap.NewNetmap(netmap.NodesFromInfo(result))
|
2020-10-29 14:27:13 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 10:26:35 +00:00
|
|
|
func peerInfoFromStackItem(prm stackitem.Item) (*netmap.NodeInfo, error) {
|
|
|
|
node := netmap.NewNodeInfo()
|
2020-10-29 14:27:13 +00:00
|
|
|
|
|
|
|
subItems, err := client.ArrayFromStackItem(prm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if ln := len(subItems); ln != 1 {
|
|
|
|
return nil, errors.New("invalid RPC response")
|
|
|
|
} else if rawNodeInfo, err := client.BytesFromStackItem(subItems[0]); err != nil {
|
|
|
|
return nil, err
|
2020-11-16 10:26:35 +00:00
|
|
|
} else if err = node.Unmarshal(rawNodeInfo); err != nil {
|
2020-10-29 14:27:13 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-16 10:26:35 +00:00
|
|
|
return node, nil
|
2020-10-29 14:27:13 +00:00
|
|
|
}
|