2020-07-24 13:54:03 +00:00
|
|
|
package invoke
|
|
|
|
|
|
|
|
import (
|
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"
|
|
|
|
netmapv2 "github.com/nspcc-dev/neofs-api-go/v2/netmap"
|
|
|
|
netmapgrpc "github.com/nspcc-dev/neofs-api-go/v2/netmap/grpc"
|
2020-07-24 13:54:03 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
2020-10-29 14:27:13 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"google.golang.org/protobuf/proto"
|
2020-07-24 13:54:03 +00:00
|
|
|
)
|
|
|
|
|
2020-09-03 14:32:37 +00:00
|
|
|
type (
|
|
|
|
UpdatePeerArgs struct {
|
|
|
|
Key *keys.PublicKey
|
|
|
|
Status uint32
|
|
|
|
}
|
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"
|
|
|
|
setConfigMethod = "setConfigMethod"
|
|
|
|
updateInnerRingMethod = "updateInnerRingMethod"
|
|
|
|
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.
|
2020-07-24 13:54:03 +00:00
|
|
|
func SetNewEpoch(cli *client.Client, con util.Uint160, epoch uint64) error {
|
|
|
|
if cli == nil {
|
|
|
|
return client.ErrNilClient
|
|
|
|
}
|
|
|
|
|
2020-10-12 11:34:13 +00:00
|
|
|
return cli.Invoke(con, feeOneGas, 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.
|
2020-09-03 14:32:37 +00:00
|
|
|
func ApprovePeer(cli *client.Client, con util.Uint160, peer []byte) error {
|
|
|
|
if cli == nil {
|
|
|
|
return client.ErrNilClient
|
|
|
|
}
|
|
|
|
|
2020-11-13 14:07:02 +00:00
|
|
|
return cli.Invoke(con, feeOneGas, approvePeerMethod, peer)
|
2020-09-03 14:32:37 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 08:48:26 +00:00
|
|
|
// UpdatePeerState invokes addPeer method.
|
2020-09-03 14:32:37 +00:00
|
|
|
func UpdatePeerState(cli *client.Client, con util.Uint160, args *UpdatePeerArgs) error {
|
|
|
|
if cli == nil {
|
|
|
|
return client.ErrNilClient
|
|
|
|
}
|
|
|
|
|
|
|
|
return cli.Invoke(con, extraFee, updatePeerStateMethod,
|
|
|
|
int64(args.Status),
|
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.
|
|
|
|
func SetConfig(cli *client.Client, con util.Uint160, args *SetConfigArgs) error {
|
|
|
|
if cli == nil {
|
|
|
|
return client.ErrNilClient
|
|
|
|
}
|
|
|
|
|
|
|
|
return cli.Invoke(con, extraFee, setConfigMethod,
|
2020-11-05 13:41:44 +00:00
|
|
|
args.ID,
|
2020-09-08 08:48:26 +00:00
|
|
|
args.Key,
|
|
|
|
args.Value,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateInnerRing invokes updateInnerRing method.
|
|
|
|
func UpdateInnerRing(cli *client.Client, con util.Uint160, list []*keys.PublicKey) error {
|
|
|
|
if cli == nil {
|
|
|
|
return client.ErrNilClient
|
|
|
|
}
|
|
|
|
|
|
|
|
rawKeys := make([][]byte, 0, len(list))
|
|
|
|
for i := range list {
|
|
|
|
rawKeys = append(rawKeys, list[i].Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
return cli.Invoke(con, extraFee, updateInnerRingMethod, rawKeys)
|
|
|
|
}
|
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 {
|
|
|
|
return nil, errors.Wrap(err, "invalid RPC response")
|
|
|
|
}
|
|
|
|
|
2020-11-05 15:51:43 +00:00
|
|
|
result = append(result, *netmap.NewNodeInfoFromV2(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-10-30 11:26:57 +00:00
|
|
|
func peerInfoFromStackItem(prm stackitem.Item) (*netmapv2.NodeInfo, error) {
|
|
|
|
node := new(netmapgrpc.NodeInfo)
|
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
|
|
|
|
} else if err = proto.Unmarshal(rawNodeInfo, node); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-30 11:26:57 +00:00
|
|
|
return netmapv2.NodeInfoFromGRPCMessage(node), nil
|
2020-10-29 14:27:13 +00:00
|
|
|
}
|