2021-11-28 15:48:32 +00:00
|
|
|
package morph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/internal"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/rand"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/subnet"
|
|
|
|
subnetid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/subnet/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2021-12-08 11:06:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/flags"
|
|
|
|
"github.com/nspcc-dev/neo-go/cli/input"
|
2022-06-02 14:24:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
2021-11-28 15:48:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2022-07-28 16:22:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc/result"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
|
2022-09-05 12:45:38 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
|
2022-06-02 14:24:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2021-12-08 11:06:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2022-06-02 14:24:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2021-12-08 11:06:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2021-11-28 15:48:32 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
func viperBindFlags(cmd *cobra.Command, flags ...string) {
|
|
|
|
for i := range flags {
|
|
|
|
_ = viper.BindPFlag(flags[i], cmd.Flags().Lookup(flags[i]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// subnet command section.
|
|
|
|
var cmdSubnet = &cobra.Command{
|
|
|
|
Use: "subnet",
|
2023-01-27 10:47:14 +00:00
|
|
|
Short: "FrostFS subnet management",
|
2021-11-30 17:37:13 +00:00
|
|
|
PreRun: func(cmd *cobra.Command, _ []string) {
|
2021-11-28 15:48:32 +00:00
|
|
|
viperBindFlags(cmd,
|
2022-06-02 14:24:03 +00:00
|
|
|
endpointFlag,
|
2021-11-28 15:48:32 +00:00
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// shared flags of cmdSubnet sub-commands.
|
|
|
|
const (
|
2022-10-17 12:03:55 +00:00
|
|
|
flagSubnet = "subnet" // subnet identifier
|
|
|
|
flagSubnetGroup = "group" // subnet client group ID
|
|
|
|
flagSubnetWallet = "wallet" // filepath to wallet
|
|
|
|
flagSubnetAddress = "address" // address in the wallet, optional
|
2021-11-28 15:48:32 +00:00
|
|
|
)
|
|
|
|
|
2021-12-08 11:06:35 +00:00
|
|
|
// reads wallet from the filepath configured in flagSubnetWallet flag,
|
|
|
|
// looks for address specified in flagSubnetAddress flag (uses default
|
|
|
|
// address if flag is empty) and decrypts private key.
|
2021-11-28 15:48:32 +00:00
|
|
|
func readSubnetKey(key *keys.PrivateKey) error {
|
2021-12-08 11:06:35 +00:00
|
|
|
// read wallet from file
|
|
|
|
|
|
|
|
walletPath := viper.GetString(flagSubnetWallet)
|
|
|
|
if walletPath == "" {
|
|
|
|
return errors.New("missing path to wallet")
|
|
|
|
}
|
|
|
|
|
|
|
|
w, err := wallet.NewWalletFromFile(walletPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("read wallet from file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// read account from the wallet
|
|
|
|
|
|
|
|
var (
|
|
|
|
addr util.Uint160
|
|
|
|
addrStr = viper.GetString(flagSubnetAddress)
|
|
|
|
)
|
|
|
|
|
|
|
|
if addrStr == "" {
|
|
|
|
addr = w.GetChangeAddress()
|
|
|
|
} else {
|
|
|
|
addr, err = flags.ParseAddress(addrStr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("read wallet address: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
acc := w.GetAccount(addr)
|
|
|
|
if acc == nil {
|
|
|
|
return fmt.Errorf("address %s not found in %s", addrStr, walletPath)
|
2021-11-28 15:48:32 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 11:06:35 +00:00
|
|
|
// read password
|
|
|
|
pass, err := input.ReadPassword("Enter password > ")
|
2021-11-28 15:48:32 +00:00
|
|
|
if err != nil {
|
2021-12-08 11:06:35 +00:00
|
|
|
return fmt.Errorf("read password: %w", err)
|
2021-11-28 15:48:32 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 11:06:35 +00:00
|
|
|
// decrypt with just read password
|
|
|
|
err = acc.Decrypt(pass, keys.NEP2ScryptParams())
|
2021-11-28 15:48:32 +00:00
|
|
|
if err != nil {
|
2021-12-08 11:06:35 +00:00
|
|
|
return fmt.Errorf("decrypt wallet: %w", err)
|
2021-11-28 15:48:32 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 11:06:35 +00:00
|
|
|
*key = *acc.PrivateKey()
|
2021-11-28 15:48:32 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// create subnet command.
|
|
|
|
var cmdSubnetCreate = &cobra.Command{
|
|
|
|
Use: "create",
|
2023-01-27 10:47:14 +00:00
|
|
|
Short: "Create FrostFS subnet",
|
2021-12-01 10:37:07 +00:00
|
|
|
PreRun: func(cmd *cobra.Command, _ []string) {
|
|
|
|
viperBindFlags(cmd,
|
2021-12-08 09:57:29 +00:00
|
|
|
flagSubnetWallet,
|
|
|
|
flagSubnetAddress,
|
2021-12-01 10:37:07 +00:00
|
|
|
)
|
|
|
|
},
|
2021-11-28 15:48:32 +00:00
|
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
|
|
// read private key
|
|
|
|
var key keys.PrivateKey
|
|
|
|
|
|
|
|
err := readSubnetKey(&key)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("read private key: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate subnet ID and marshal it
|
|
|
|
var (
|
|
|
|
id subnetid.ID
|
|
|
|
num uint32
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
2022-01-11 14:07:59 +00:00
|
|
|
num = rand.Uint32()
|
2021-11-28 15:48:32 +00:00
|
|
|
|
2022-06-15 08:08:10 +00:00
|
|
|
id.SetNumeric(num)
|
2021-11-28 15:48:32 +00:00
|
|
|
|
|
|
|
if !subnetid.IsZero(id) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// declare creator ID and encode it
|
2022-05-17 13:59:46 +00:00
|
|
|
var creator user.ID
|
|
|
|
user.IDFromKey(&creator, key.PrivateKey.PublicKey)
|
2021-11-28 15:48:32 +00:00
|
|
|
|
|
|
|
// fill subnet info and encode it
|
|
|
|
var info subnet.Info
|
|
|
|
|
|
|
|
info.SetID(id)
|
|
|
|
info.SetOwner(creator)
|
|
|
|
|
2022-06-15 08:08:10 +00:00
|
|
|
err = invokeMethod(key, true, "put", id.Marshal(), key.PublicKey().Bytes(), info.Marshal())
|
2021-11-28 15:48:32 +00:00
|
|
|
if err != nil {
|
2022-06-02 14:24:03 +00:00
|
|
|
return fmt.Errorf("morph invocation: %w", err)
|
2021-11-28 15:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Printf("Create subnet request sent successfully. ID: %s.\n", &id)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// cmdSubnetRemove flags.
|
|
|
|
const (
|
2022-10-17 12:03:55 +00:00
|
|
|
// subnet ID to be removed.
|
2021-11-28 15:48:32 +00:00
|
|
|
flagSubnetRemoveID = flagSubnet
|
|
|
|
)
|
|
|
|
|
|
|
|
// errZeroSubnet is returned on attempts to work with zero subnet which is virtual.
|
|
|
|
var errZeroSubnet = errors.New("zero subnet")
|
|
|
|
|
|
|
|
// remove subnet command.
|
|
|
|
var cmdSubnetRemove = &cobra.Command{
|
|
|
|
Use: "remove",
|
2023-01-27 10:47:14 +00:00
|
|
|
Short: "Remove FrostFS subnet",
|
2021-11-28 15:48:32 +00:00
|
|
|
PreRun: func(cmd *cobra.Command, _ []string) {
|
|
|
|
viperBindFlags(cmd,
|
2021-12-08 09:57:29 +00:00
|
|
|
flagSubnetWallet,
|
|
|
|
flagSubnetAddress,
|
2021-11-28 15:48:32 +00:00
|
|
|
flagSubnetRemoveID,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
|
|
// read private key
|
|
|
|
var key keys.PrivateKey
|
|
|
|
|
|
|
|
err := readSubnetKey(&key)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("read private key: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// read ID and encode it
|
|
|
|
var id subnetid.ID
|
|
|
|
|
2022-06-15 08:08:10 +00:00
|
|
|
err = id.DecodeString(viper.GetString(flagSubnetRemoveID))
|
2021-11-28 15:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decode ID text: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if subnetid.IsZero(id) {
|
|
|
|
return errZeroSubnet
|
|
|
|
}
|
|
|
|
|
2022-06-15 08:08:10 +00:00
|
|
|
err = invokeMethod(key, false, "delete", id.Marshal())
|
2021-11-28 15:48:32 +00:00
|
|
|
if err != nil {
|
2022-06-02 14:24:03 +00:00
|
|
|
return fmt.Errorf("morph invocation: %w", err)
|
2021-11-28 15:48:32 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 06:03:52 +00:00
|
|
|
cmd.Println("Remove subnet request sent successfully")
|
2021-11-28 15:48:32 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// cmdSubnetGet flags.
|
|
|
|
const (
|
2022-10-17 12:03:55 +00:00
|
|
|
// subnet ID to be read.
|
2021-11-28 15:48:32 +00:00
|
|
|
flagSubnetGetID = flagSubnet
|
|
|
|
)
|
|
|
|
|
|
|
|
// get subnet command.
|
|
|
|
var cmdSubnetGet = &cobra.Command{
|
|
|
|
Use: "get",
|
2023-01-27 10:47:14 +00:00
|
|
|
Short: "Read information about the FrostFS subnet",
|
2021-11-28 15:48:32 +00:00
|
|
|
PreRun: func(cmd *cobra.Command, _ []string) {
|
|
|
|
viperBindFlags(cmd,
|
|
|
|
flagSubnetGetID,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
|
|
// read ID and encode it
|
|
|
|
var id subnetid.ID
|
|
|
|
|
2022-06-15 08:08:10 +00:00
|
|
|
err := id.DecodeString(viper.GetString(flagSubnetGetID))
|
2021-11-28 15:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decode ID text: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if subnetid.IsZero(id) {
|
|
|
|
return errZeroSubnet
|
|
|
|
}
|
|
|
|
|
2021-12-08 09:57:29 +00:00
|
|
|
// use random key to fetch the data
|
|
|
|
// we could use raw neo-go client to perform testInvoke
|
|
|
|
// without keys, as it is done in other commands
|
|
|
|
key, err := keys.NewPrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("init subnet client: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-06-15 08:08:10 +00:00
|
|
|
res, err := testInvokeMethod(*key, "get", id.Marshal())
|
2021-11-28 15:48:32 +00:00
|
|
|
if err != nil {
|
2022-06-02 14:24:03 +00:00
|
|
|
return fmt.Errorf("morph invocation: %w", err)
|
2021-11-28 15:48:32 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 14:24:03 +00:00
|
|
|
if len(res) == 0 {
|
|
|
|
return errors.New("subnet does not exist")
|
|
|
|
}
|
2021-11-28 15:48:32 +00:00
|
|
|
|
2022-06-02 14:24:03 +00:00
|
|
|
data, err := client.BytesFromStackItem(res[0])
|
2021-11-28 15:48:32 +00:00
|
|
|
if err != nil {
|
2022-06-02 14:24:03 +00:00
|
|
|
return fmt.Errorf("decoding contract response: %w", err)
|
2021-11-28 15:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// decode info
|
|
|
|
var info subnet.Info
|
2022-06-02 14:24:03 +00:00
|
|
|
if err = info.Unmarshal(data); err != nil {
|
2021-11-28 15:48:32 +00:00
|
|
|
return fmt.Errorf("decode subnet info: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// print information
|
2022-06-15 08:08:10 +00:00
|
|
|
cmd.Printf("Owner: %s\n", info.Owner())
|
2021-11-28 15:48:32 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// cmdSubnetAdmin subnet flags.
|
|
|
|
const (
|
2022-10-17 12:03:55 +00:00
|
|
|
flagSubnetAdminSubnet = flagSubnet // subnet ID to be managed
|
|
|
|
flagSubnetAdminID = "admin" // admin public key
|
|
|
|
flagSubnetAdminClient = "client" // manage client admins instead of node ones
|
2021-11-28 15:48:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// command to manage subnet admins.
|
|
|
|
var cmdSubnetAdmin = &cobra.Command{
|
|
|
|
Use: "admin",
|
2023-01-27 10:47:14 +00:00
|
|
|
Short: "Manage administrators of the FrostFS subnet",
|
2021-11-30 17:37:13 +00:00
|
|
|
PreRun: func(cmd *cobra.Command, args []string) {
|
2021-11-28 15:48:32 +00:00
|
|
|
viperBindFlags(cmd,
|
2021-12-08 09:57:29 +00:00
|
|
|
flagSubnetWallet,
|
|
|
|
flagSubnetAddress,
|
2021-11-28 15:48:32 +00:00
|
|
|
flagSubnetAdminSubnet,
|
|
|
|
flagSubnetAdminID,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// cmdSubnetAdminAdd flags.
|
|
|
|
const (
|
2022-10-17 12:03:55 +00:00
|
|
|
flagSubnetAdminAddGroup = flagSubnetGroup // client group ID
|
2021-11-28 15:48:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// common executor cmdSubnetAdminAdd and cmdSubnetAdminRemove commands.
|
|
|
|
func manageSubnetAdmins(cmd *cobra.Command, rm bool) error {
|
|
|
|
// read private key
|
|
|
|
var key keys.PrivateKey
|
|
|
|
|
|
|
|
err := readSubnetKey(&key)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("read private key: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// read ID and encode it
|
|
|
|
var id subnetid.ID
|
|
|
|
|
2022-06-15 08:08:10 +00:00
|
|
|
err = id.DecodeString(viper.GetString(flagSubnetAdminSubnet))
|
2021-11-28 15:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decode ID text: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if subnetid.IsZero(id) {
|
|
|
|
return errZeroSubnet
|
|
|
|
}
|
|
|
|
|
|
|
|
// read admin key and decode it
|
|
|
|
binAdminKey, err := hex.DecodeString(viper.GetString(flagSubnetAdminID))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decode admin key text: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var pubkey keys.PublicKey
|
|
|
|
if err = pubkey.DecodeBytes(binAdminKey); err != nil {
|
|
|
|
return fmt.Errorf("admin key format: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-03-23 07:04:20 +00:00
|
|
|
return invokeMethodWithParams(cmd, id, rm, binAdminKey, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func invokeMethodWithParams(cmd *cobra.Command, id subnetid.ID, rm bool, binAdminKey []byte, key keys.PrivateKey) error {
|
2023-02-21 11:42:45 +00:00
|
|
|
prm := make([]any, 0, 3)
|
2022-06-15 08:08:10 +00:00
|
|
|
prm = append(prm, id.Marshal())
|
2022-06-02 14:24:03 +00:00
|
|
|
|
|
|
|
var method string
|
2021-11-28 15:48:32 +00:00
|
|
|
|
|
|
|
if viper.GetBool(flagSubnetAdminClient) {
|
|
|
|
var groupID internal.SubnetClientGroupID
|
|
|
|
|
2023-03-23 07:04:20 +00:00
|
|
|
err := groupID.UnmarshalText([]byte(viper.GetString(flagSubnetAdminAddGroup)))
|
2021-11-28 15:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decode group ID text: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
binGroupID, err := groupID.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("marshal group ID: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-06-02 14:24:03 +00:00
|
|
|
if rm {
|
|
|
|
method = "removeClientAdmin"
|
|
|
|
} else {
|
|
|
|
method = "addClientAdmin"
|
|
|
|
}
|
2021-11-28 15:48:32 +00:00
|
|
|
|
2022-06-02 14:24:03 +00:00
|
|
|
prm = append(prm, binGroupID)
|
|
|
|
} else {
|
|
|
|
if rm {
|
|
|
|
method = "removeNodeAdmin"
|
|
|
|
} else {
|
|
|
|
method = "addNodeAdmin"
|
|
|
|
}
|
2021-11-28 15:48:32 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 14:24:03 +00:00
|
|
|
prm = append(prm, binAdminKey)
|
2021-11-28 15:48:32 +00:00
|
|
|
|
2023-03-23 07:04:20 +00:00
|
|
|
err := invokeMethod(key, false, method, prm...)
|
2021-11-28 15:48:32 +00:00
|
|
|
if err != nil {
|
2022-06-02 14:24:03 +00:00
|
|
|
return fmt.Errorf("morph invocation: %w", err)
|
2021-11-28 15:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var op string
|
|
|
|
|
|
|
|
if rm {
|
|
|
|
op = "Remove"
|
|
|
|
} else {
|
|
|
|
op = "Add"
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Printf("%s admin request sent successfully.\n", op)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// command to add subnet admin.
|
|
|
|
var cmdSubnetAdminAdd = &cobra.Command{
|
|
|
|
Use: "add",
|
2023-01-27 10:47:14 +00:00
|
|
|
Short: "Add admin to the FrostFS subnet",
|
2021-11-28 15:48:32 +00:00
|
|
|
PreRun: func(cmd *cobra.Command, _ []string) {
|
|
|
|
viperBindFlags(cmd,
|
|
|
|
flagSubnetAdminAddGroup,
|
|
|
|
flagSubnetAdminClient,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
|
|
return manageSubnetAdmins(cmd, false)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// command to remove subnet admin.
|
|
|
|
var cmdSubnetAdminRemove = &cobra.Command{
|
|
|
|
Use: "remove",
|
2023-01-27 10:47:14 +00:00
|
|
|
Short: "Remove admin of the FrostFS subnet",
|
2021-11-28 15:48:32 +00:00
|
|
|
PreRun: func(cmd *cobra.Command, _ []string) {
|
|
|
|
viperBindFlags(cmd,
|
|
|
|
flagSubnetAdminClient,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
|
|
return manageSubnetAdmins(cmd, true)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// cmdSubnetClient flags.
|
|
|
|
const (
|
2022-10-17 12:03:55 +00:00
|
|
|
flagSubnetClientSubnet = flagSubnet // ID of the subnet to be managed
|
|
|
|
flagSubnetClientID = flagSubnetAdminClient // client's NeoFS ID
|
|
|
|
flagSubnetClientGroup = flagSubnetGroup // ID of the subnet client group
|
2021-11-28 15:48:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// command to manage subnet clients.
|
|
|
|
var cmdSubnetClient = &cobra.Command{
|
|
|
|
Use: "client",
|
2023-01-27 10:47:14 +00:00
|
|
|
Short: "Manage clients of the FrostFS subnet",
|
2021-11-28 15:48:32 +00:00
|
|
|
PreRun: func(cmd *cobra.Command, _ []string) {
|
|
|
|
viperBindFlags(cmd,
|
2021-12-08 09:57:29 +00:00
|
|
|
flagSubnetWallet,
|
|
|
|
flagSubnetAddress,
|
2021-11-28 15:48:32 +00:00
|
|
|
flagSubnetClientSubnet,
|
|
|
|
flagSubnetClientID,
|
|
|
|
flagSubnetClientGroup,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// common executor cmdSubnetClientAdd and cmdSubnetClientRemove commands.
|
|
|
|
func manageSubnetClients(cmd *cobra.Command, rm bool) error {
|
|
|
|
// read private key
|
|
|
|
var key keys.PrivateKey
|
|
|
|
|
|
|
|
err := readSubnetKey(&key)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("read private key: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// read ID and encode it
|
|
|
|
var id subnetid.ID
|
|
|
|
|
2022-06-15 08:08:10 +00:00
|
|
|
err = id.DecodeString(viper.GetString(flagSubnetClientSubnet))
|
2021-11-28 15:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decode ID text: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if subnetid.IsZero(id) {
|
|
|
|
return errZeroSubnet
|
|
|
|
}
|
|
|
|
|
|
|
|
// read client ID and encode it
|
2022-05-17 13:59:46 +00:00
|
|
|
var clientID user.ID
|
2021-11-28 15:48:32 +00:00
|
|
|
|
2022-05-17 13:59:46 +00:00
|
|
|
err = clientID.DecodeString(viper.GetString(flagSubnetClientID))
|
2021-11-28 15:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decode client ID text: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-12-01 13:48:24 +00:00
|
|
|
// read group ID and encode it
|
|
|
|
var groupID internal.SubnetClientGroupID
|
2021-11-28 15:48:32 +00:00
|
|
|
|
2021-12-01 13:48:24 +00:00
|
|
|
err = groupID.UnmarshalText([]byte(viper.GetString(flagSubnetAdminAddGroup)))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decode group ID text: %w", err)
|
|
|
|
}
|
2021-11-28 15:48:32 +00:00
|
|
|
|
2021-12-01 13:48:24 +00:00
|
|
|
binGroupID, err := groupID.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("marshal group ID: %w", err)
|
2021-11-28 15:48:32 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 14:24:03 +00:00
|
|
|
var method string
|
2021-11-28 15:48:32 +00:00
|
|
|
if rm {
|
2022-06-02 14:24:03 +00:00
|
|
|
method = "removeUser"
|
|
|
|
} else {
|
|
|
|
method = "addUser"
|
2021-11-28 15:48:32 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 08:08:10 +00:00
|
|
|
err = invokeMethod(key, false, method, id.Marshal(), binGroupID, clientID.WalletBytes())
|
2021-11-28 15:48:32 +00:00
|
|
|
if err != nil {
|
2022-06-02 14:24:03 +00:00
|
|
|
return fmt.Errorf("morph invocation: %w", err)
|
2021-11-28 15:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var op string
|
|
|
|
|
|
|
|
if rm {
|
|
|
|
op = "Remove"
|
|
|
|
} else {
|
|
|
|
op = "Add"
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Printf("%s client request sent successfully.\n", op)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// command to add subnet client.
|
|
|
|
var cmdSubnetClientAdd = &cobra.Command{
|
|
|
|
Use: "add",
|
2023-01-27 10:47:14 +00:00
|
|
|
Short: "Add client to the FrostFS subnet",
|
2021-11-28 15:48:32 +00:00
|
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
|
|
return manageSubnetClients(cmd, false)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// command to remove subnet client.
|
|
|
|
var cmdSubnetClientRemove = &cobra.Command{
|
|
|
|
Use: "remove",
|
2023-01-27 10:47:14 +00:00
|
|
|
Short: "Remove client of the FrostFS subnet",
|
2021-11-28 15:48:32 +00:00
|
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
|
|
return manageSubnetClients(cmd, true)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-12-01 09:14:10 +00:00
|
|
|
// cmdSubnetNode flags.
|
|
|
|
const (
|
2022-10-17 12:03:55 +00:00
|
|
|
flagSubnetNode = "node" // node ID
|
|
|
|
flagSubnetNodeSubnet = flagSubnet // ID of the subnet to be managed
|
2021-12-01 09:14:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// common executor cmdSubnetNodeAdd and cmdSubnetNodeRemove commands.
|
|
|
|
func manageSubnetNodes(cmd *cobra.Command, rm bool) error {
|
|
|
|
// read private key
|
|
|
|
var key keys.PrivateKey
|
|
|
|
|
|
|
|
err := readSubnetKey(&key)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("read private key: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// read ID and encode it
|
|
|
|
var id subnetid.ID
|
|
|
|
|
2022-06-15 08:08:10 +00:00
|
|
|
err = id.DecodeString(viper.GetString(flagSubnetNodeSubnet))
|
2021-12-01 09:14:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decode ID text: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if subnetid.IsZero(id) {
|
|
|
|
return errZeroSubnet
|
|
|
|
}
|
|
|
|
|
|
|
|
// read node ID and encode it
|
|
|
|
binNodeID, err := hex.DecodeString(viper.GetString(flagSubnetNode))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decode node ID text: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var pubkey keys.PublicKey
|
|
|
|
if err = pubkey.DecodeBytes(binNodeID); err != nil {
|
|
|
|
return fmt.Errorf("node ID format: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-06-02 14:24:03 +00:00
|
|
|
var method string
|
2021-12-01 09:14:10 +00:00
|
|
|
if rm {
|
2022-06-02 14:24:03 +00:00
|
|
|
method = "removeNode"
|
|
|
|
} else {
|
|
|
|
method = "addNode"
|
2021-12-01 09:14:10 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 08:08:10 +00:00
|
|
|
err = invokeMethod(key, false, method, id.Marshal(), binNodeID)
|
2021-12-01 09:14:10 +00:00
|
|
|
if err != nil {
|
2022-06-02 14:24:03 +00:00
|
|
|
return fmt.Errorf("morph invocation: %w", err)
|
2021-12-01 09:14:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var op string
|
|
|
|
|
|
|
|
if rm {
|
|
|
|
op = "Remove"
|
|
|
|
} else {
|
|
|
|
op = "Add"
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Printf("%s node request sent successfully.\n", op)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// command to manage subnet nodes.
|
|
|
|
var cmdSubnetNode = &cobra.Command{
|
|
|
|
Use: "node",
|
2023-01-27 10:47:14 +00:00
|
|
|
Short: "Manage nodes of the FrostFS subnet",
|
2021-12-01 09:14:10 +00:00
|
|
|
PreRun: func(cmd *cobra.Command, _ []string) {
|
|
|
|
viperBindFlags(cmd,
|
2022-03-09 10:49:35 +00:00
|
|
|
flagSubnetWallet,
|
2021-12-01 09:14:10 +00:00
|
|
|
flagSubnetNode,
|
|
|
|
flagSubnetNodeSubnet,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// command to add subnet node.
|
|
|
|
var cmdSubnetNodeAdd = &cobra.Command{
|
|
|
|
Use: "add",
|
2023-01-27 10:47:14 +00:00
|
|
|
Short: "Add node to the FrostFS subnet",
|
2021-12-01 09:14:10 +00:00
|
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
|
|
return manageSubnetNodes(cmd, false)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// command to remove subnet node.
|
|
|
|
var cmdSubnetNodeRemove = &cobra.Command{
|
|
|
|
Use: "remove",
|
2023-01-27 10:47:14 +00:00
|
|
|
Short: "Remove node from the FrostFS subnet",
|
2021-12-01 09:14:10 +00:00
|
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
|
|
return manageSubnetNodes(cmd, true)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-11-30 17:37:13 +00:00
|
|
|
// returns function which calls PreRun on parent if it exists.
|
|
|
|
func inheritPreRun(preRun func(*cobra.Command, []string)) func(*cobra.Command, []string) {
|
|
|
|
return func(cmd *cobra.Command, args []string) {
|
|
|
|
par := cmd.Parent()
|
|
|
|
if par != nil && par.PreRun != nil {
|
|
|
|
par.PreRun(par, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
if preRun != nil {
|
|
|
|
preRun(cmd, args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// inherits PreRun function of parent command in all sub-commands and
|
|
|
|
// adds them to the parent.
|
|
|
|
func addCommandInheritPreRun(par *cobra.Command, subs ...*cobra.Command) {
|
|
|
|
for _, sub := range subs {
|
|
|
|
sub.PreRun = inheritPreRun(sub.PreRun)
|
|
|
|
}
|
|
|
|
|
|
|
|
par.AddCommand(subs...)
|
|
|
|
}
|
|
|
|
|
2021-11-28 15:48:32 +00:00
|
|
|
// registers flags and binds sub-commands for subnet commands.
|
2023-03-20 07:01:27 +00:00
|
|
|
//
|
|
|
|
// nolint: funlen
|
2021-11-28 15:48:32 +00:00
|
|
|
func init() {
|
2021-12-08 09:57:29 +00:00
|
|
|
cmdSubnetCreate.Flags().StringP(flagSubnetWallet, "w", "", "Path to file with wallet")
|
|
|
|
_ = cmdSubnetCreate.MarkFlagRequired(flagSubnetWallet)
|
|
|
|
cmdSubnetCreate.Flags().StringP(flagSubnetAddress, "a", "", "Address in the wallet, optional")
|
2021-12-01 10:37:07 +00:00
|
|
|
|
2021-11-28 15:48:32 +00:00
|
|
|
// get subnet flags
|
|
|
|
cmdSubnetGet.Flags().String(flagSubnetGetID, "", "ID of the subnet to read")
|
|
|
|
_ = cmdSubnetAdminAdd.MarkFlagRequired(flagSubnetGetID)
|
|
|
|
|
|
|
|
// remove subnet flags
|
|
|
|
cmdSubnetRemove.Flags().String(flagSubnetRemoveID, "", "ID of the subnet to remove")
|
|
|
|
_ = cmdSubnetRemove.MarkFlagRequired(flagSubnetRemoveID)
|
2021-12-08 09:57:29 +00:00
|
|
|
cmdSubnetRemove.Flags().StringP(flagSubnetWallet, "w", "", "Path to file with wallet")
|
|
|
|
_ = cmdSubnetRemove.MarkFlagRequired(flagSubnetWallet)
|
|
|
|
cmdSubnetRemove.Flags().StringP(flagSubnetAddress, "a", "", "Address in the wallet, optional")
|
2021-11-28 15:48:32 +00:00
|
|
|
|
|
|
|
// subnet administer flags
|
2021-11-30 11:58:15 +00:00
|
|
|
adminFlags := cmdSubnetAdmin.PersistentFlags()
|
2021-11-28 15:48:32 +00:00
|
|
|
adminFlags.String(flagSubnetAdminSubnet, "", "ID of the subnet to manage administrators")
|
|
|
|
_ = cmdSubnetAdmin.MarkFlagRequired(flagSubnetAdminSubnet)
|
|
|
|
adminFlags.String(flagSubnetAdminID, "", "Hex-encoded public key of the admin")
|
|
|
|
_ = cmdSubnetAdmin.MarkFlagRequired(flagSubnetAdminID)
|
2021-12-08 09:57:29 +00:00
|
|
|
adminFlags.StringP(flagSubnetWallet, "w", "", "Path to file with wallet")
|
|
|
|
_ = cmdSubnetAdmin.MarkFlagRequired(flagSubnetWallet)
|
|
|
|
adminFlags.StringP(flagSubnetAddress, "a", "", "Address in the wallet, optional")
|
2021-11-28 15:48:32 +00:00
|
|
|
|
|
|
|
// add admin flags
|
|
|
|
cmdSubnetAdminAddFlags := cmdSubnetAdminAdd.Flags()
|
|
|
|
cmdSubnetAdminAddFlags.String(flagSubnetAdminAddGroup, "", fmt.Sprintf(
|
|
|
|
"Client group ID in text format (needed with --%s only)", flagSubnetAdminClient))
|
|
|
|
cmdSubnetAdminAddFlags.Bool(flagSubnetAdminClient, false, "Add client admin instead of node one")
|
|
|
|
|
|
|
|
// remove admin flags
|
|
|
|
cmdSubnetAdminRemoveFlags := cmdSubnetAdminRemove.Flags()
|
|
|
|
cmdSubnetAdminRemoveFlags.Bool(flagSubnetAdminClient, false, "Remove client admin instead of node one")
|
|
|
|
|
|
|
|
// client managements flags
|
2021-11-30 11:58:15 +00:00
|
|
|
clientFlags := cmdSubnetClient.PersistentFlags()
|
2021-11-28 15:48:32 +00:00
|
|
|
clientFlags.String(flagSubnetClientSubnet, "", "ID of the subnet to be managed")
|
|
|
|
_ = cmdSubnetClient.MarkFlagRequired(flagSubnetClientSubnet)
|
|
|
|
clientFlags.String(flagSubnetClientGroup, "", "ID of the client group to work with")
|
|
|
|
_ = cmdSubnetClient.MarkFlagRequired(flagSubnetClientGroup)
|
2023-01-27 10:47:14 +00:00
|
|
|
clientFlags.String(flagSubnetClientID, "", "Client's user ID in FrostFS system in text format")
|
2021-11-28 15:48:32 +00:00
|
|
|
_ = cmdSubnetClient.MarkFlagRequired(flagSubnetClientID)
|
2021-12-08 09:57:29 +00:00
|
|
|
clientFlags.StringP(flagSubnetWallet, "w", "", "Path to file with wallet")
|
|
|
|
_ = cmdSubnetClient.MarkFlagRequired(flagSubnetWallet)
|
|
|
|
clientFlags.StringP(flagSubnetAddress, "a", "", "Address in the wallet, optional")
|
2021-11-28 15:48:32 +00:00
|
|
|
|
|
|
|
// add all admin managing commands to corresponding command section
|
2021-11-30 17:37:13 +00:00
|
|
|
addCommandInheritPreRun(cmdSubnetAdmin,
|
2021-11-28 15:48:32 +00:00
|
|
|
cmdSubnetAdminAdd,
|
|
|
|
cmdSubnetAdminRemove,
|
|
|
|
)
|
|
|
|
|
|
|
|
// add all client managing commands to corresponding command section
|
2021-11-30 17:37:13 +00:00
|
|
|
addCommandInheritPreRun(cmdSubnetClient,
|
2021-11-28 15:48:32 +00:00
|
|
|
cmdSubnetClientAdd,
|
|
|
|
cmdSubnetClientRemove,
|
|
|
|
)
|
|
|
|
|
2021-12-01 09:14:10 +00:00
|
|
|
// subnet node flags
|
|
|
|
nodeFlags := cmdSubnetNode.PersistentFlags()
|
2022-03-09 10:49:35 +00:00
|
|
|
nodeFlags.StringP(flagSubnetWallet, "w", "", "Path to file with wallet")
|
|
|
|
_ = cmdSubnetNode.MarkFlagRequired(flagSubnetWallet)
|
2021-12-01 09:14:10 +00:00
|
|
|
nodeFlags.String(flagSubnetNode, "", "Hex-encoded public key of the node")
|
2022-03-09 10:49:35 +00:00
|
|
|
_ = cmdSubnetNode.MarkFlagRequired(flagSubnetNode)
|
2021-12-01 09:14:10 +00:00
|
|
|
nodeFlags.String(flagSubnetNodeSubnet, "", "ID of the subnet to manage nodes")
|
|
|
|
_ = cmdSubnetNode.MarkFlagRequired(flagSubnetNodeSubnet)
|
|
|
|
|
|
|
|
// add all node managing commands to corresponding command section
|
|
|
|
addCommandInheritPreRun(cmdSubnetNode,
|
|
|
|
cmdSubnetNodeAdd,
|
|
|
|
cmdSubnetNodeRemove,
|
|
|
|
)
|
|
|
|
|
2021-11-28 15:48:32 +00:00
|
|
|
// subnet global flags
|
2021-11-30 11:58:15 +00:00
|
|
|
cmdSubnetFlags := cmdSubnet.PersistentFlags()
|
2022-06-02 14:24:03 +00:00
|
|
|
cmdSubnetFlags.StringP(endpointFlag, "r", "", "N3 RPC node endpoint")
|
|
|
|
_ = cmdSubnet.MarkFlagRequired(endpointFlag)
|
2021-11-28 15:48:32 +00:00
|
|
|
|
|
|
|
// add all subnet commands to corresponding command section
|
2021-11-30 17:37:13 +00:00
|
|
|
addCommandInheritPreRun(cmdSubnet,
|
2021-11-28 15:48:32 +00:00
|
|
|
cmdSubnetCreate,
|
|
|
|
cmdSubnetRemove,
|
|
|
|
cmdSubnetGet,
|
|
|
|
cmdSubnetAdmin,
|
|
|
|
cmdSubnetClient,
|
2021-12-01 09:14:10 +00:00
|
|
|
cmdSubnetNode,
|
2021-11-28 15:48:32 +00:00
|
|
|
)
|
|
|
|
}
|
2022-06-02 14:24:03 +00:00
|
|
|
|
2023-02-21 11:42:45 +00:00
|
|
|
func testInvokeMethod(key keys.PrivateKey, method string, args ...any) ([]stackitem.Item, error) {
|
2022-06-02 14:24:03 +00:00
|
|
|
c, err := getN3Client(viper.GetViper())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("morph client creation: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nnsCs, err := c.GetContractStateByID(1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("NNS contract resolving: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cosigner := []transaction.Signer{
|
|
|
|
{
|
|
|
|
Account: key.PublicKey().GetScriptHash(),
|
|
|
|
Scopes: transaction.Global,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-09-05 12:45:38 +00:00
|
|
|
inv := invoker.New(c, cosigner)
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
subnetHash, err := nnsResolveHash(inv, nnsCs.Hash, subnetContract+".frostfs")
|
2022-09-05 12:45:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("subnet hash resolving: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := inv.Call(subnetHash, method, args...)
|
2022-06-02 14:24:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invocation parameters prepararion: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = checkInvocationResults(res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Stack, nil
|
|
|
|
}
|
|
|
|
|
2023-02-21 11:42:45 +00:00
|
|
|
func invokeMethod(key keys.PrivateKey, tryNotary bool, method string, args ...any) error {
|
2022-06-02 14:24:03 +00:00
|
|
|
c, err := getN3Client(viper.GetViper())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("morph client creation: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tryNotary {
|
|
|
|
cc, err := c.GetNativeContracts()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("native hashes: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var notary bool
|
|
|
|
var notaryHash util.Uint160
|
|
|
|
for _, c := range cc {
|
|
|
|
if c.Manifest.Name == nativenames.Notary {
|
|
|
|
notary = len(c.UpdateHistory) > 0
|
|
|
|
notaryHash = c.Hash
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if notary {
|
|
|
|
err = invokeNotary(c, key, method, notaryHash, args...)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("notary invocation: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = invokeNonNotary(c, key, method, args...)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("non-notary invocation: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-21 11:42:45 +00:00
|
|
|
func invokeNonNotary(c Client, key keys.PrivateKey, method string, args ...any) error {
|
2022-06-02 14:24:03 +00:00
|
|
|
nnsCs, err := c.GetContractStateByID(1)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("NNS contract resolving: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
acc := wallet.NewAccountFromPrivateKey(&key)
|
|
|
|
|
|
|
|
cosigner := []transaction.Signer{
|
|
|
|
{
|
|
|
|
Account: key.PublicKey().GetScriptHash(),
|
|
|
|
Scopes: transaction.Global,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-07-28 16:22:32 +00:00
|
|
|
cosignerAcc := []rpcclient.SignerAccount{
|
2022-06-02 14:24:03 +00:00
|
|
|
{
|
|
|
|
Signer: cosigner[0],
|
|
|
|
Account: acc,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-09-05 12:45:38 +00:00
|
|
|
inv := invoker.New(c, cosigner)
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
subnetHash, err := nnsResolveHash(inv, nnsCs.Hash, subnetContract+".frostfs")
|
2022-09-05 12:45:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("subnet hash resolving: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
test, err := inv.Call(subnetHash, method, args...)
|
2022-06-02 14:24:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("test invocation: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = checkInvocationResults(test)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.SignAndPushInvocationTx(test.Script, acc, test.GasConsumed, 0, cosignerAcc)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("sending transaction: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-20 07:01:27 +00:00
|
|
|
// nolint: funlen
|
2023-02-21 11:42:45 +00:00
|
|
|
func invokeNotary(c Client, key keys.PrivateKey, method string, notaryHash util.Uint160, args ...any) error {
|
2022-06-02 14:24:03 +00:00
|
|
|
nnsCs, err := c.GetContractStateByID(1)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("NNS contract resolving: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
alphabet, err := c.GetCommittee()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("alphabet list: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
multisigScript, err := smartcontract.CreateDefaultMultiSigRedeemScript(alphabet)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("alphabet multi-signature script: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cosigners, err := notaryCosigners(c, notaryHash, nnsCs, key, hash.Hash160(multisigScript))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cosigners collecting: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-09-05 12:45:38 +00:00
|
|
|
inv := invoker.New(c, cosigners)
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
subnetHash, err := nnsResolveHash(inv, nnsCs.Hash, subnetContract+".frostfs")
|
2022-09-05 12:45:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("subnet hash resolving: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-06-02 14:24:03 +00:00
|
|
|
// make test invocation of the method
|
2022-09-05 12:45:38 +00:00
|
|
|
test, err := inv.Call(subnetHash, method, args...)
|
2022-06-02 14:24:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("test invocation: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = checkInvocationResults(test)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
multisigAccount := &wallet.Account{
|
|
|
|
Contract: &wallet.Contract{
|
|
|
|
Script: multisigScript,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
bc, err := c.GetBlockCount()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("blockchain height: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
signersNumber := uint8(smartcontract.GetDefaultHonestNodeCount(len(alphabet)) + 1) // alphabet multisig + key signature
|
|
|
|
|
|
|
|
// notaryRequestValidity is number of blocks during
|
|
|
|
// witch notary request is considered valid
|
|
|
|
const notaryRequestValidity = 100
|
|
|
|
|
|
|
|
mainTx := &transaction.Transaction{
|
|
|
|
Nonce: rand.Uint32(),
|
|
|
|
SystemFee: test.GasConsumed,
|
|
|
|
ValidUntilBlock: bc + notaryRequestValidity,
|
|
|
|
Script: test.Script,
|
|
|
|
Attributes: []transaction.Attribute{
|
|
|
|
{
|
|
|
|
Type: transaction.NotaryAssistedT,
|
|
|
|
Value: &transaction.NotaryAssisted{NKeys: signersNumber},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Signers: cosigners,
|
|
|
|
}
|
|
|
|
|
|
|
|
notaryFee, err := c.CalculateNotaryFee(signersNumber)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
acc := wallet.NewAccountFromPrivateKey(&key)
|
|
|
|
aa := notaryAccounts(multisigAccount, acc)
|
|
|
|
|
|
|
|
err = c.AddNetworkFee(mainTx, notaryFee, aa...)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("notary network fee adding: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mainTx.Scripts = notaryWitnesses(c, multisigAccount, acc, mainTx)
|
|
|
|
|
|
|
|
_, err = c.SignAndPushP2PNotaryRequest(mainTx,
|
|
|
|
[]byte{byte(opcode.RET)},
|
|
|
|
-1,
|
|
|
|
0,
|
|
|
|
40,
|
|
|
|
acc)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("sending notary request: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func notaryCosigners(c Client, notaryHash util.Uint160, nnsCs *state.Contract,
|
|
|
|
key keys.PrivateKey, alphabetAccount util.Uint160) ([]transaction.Signer, error) {
|
2022-12-23 17:35:35 +00:00
|
|
|
proxyHash, err := nnsResolveHash(invoker.New(c, nil), nnsCs.Hash, proxyContract+".frostfs")
|
2022-06-02 14:24:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("proxy hash resolving: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return []transaction.Signer{
|
|
|
|
{
|
|
|
|
Account: proxyHash,
|
|
|
|
Scopes: transaction.None,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Account: alphabetAccount,
|
|
|
|
Scopes: transaction.Global,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Account: hash.Hash160(key.PublicKey().GetVerificationScript()),
|
|
|
|
Scopes: transaction.Global,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Account: notaryHash,
|
|
|
|
Scopes: transaction.None,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func notaryAccounts(alphabet, acc *wallet.Account) []*wallet.Account {
|
|
|
|
return []*wallet.Account{
|
|
|
|
// proxy
|
|
|
|
{
|
|
|
|
Contract: &wallet.Contract{
|
|
|
|
Deployed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
alphabet,
|
|
|
|
// caller's account
|
|
|
|
acc,
|
|
|
|
// last one is a placeholder for notary contract account
|
|
|
|
{
|
|
|
|
Contract: &wallet.Contract{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func notaryWitnesses(c Client, alphabet, acc *wallet.Account, tx *transaction.Transaction) []transaction.Witness {
|
|
|
|
ww := make([]transaction.Witness, 0, 4)
|
|
|
|
|
|
|
|
// empty proxy contract witness
|
|
|
|
ww = append(ww, transaction.Witness{
|
|
|
|
InvocationScript: []byte{},
|
|
|
|
VerificationScript: []byte{},
|
|
|
|
})
|
|
|
|
|
|
|
|
// alphabet multi-address witness
|
|
|
|
ww = append(ww, transaction.Witness{
|
|
|
|
InvocationScript: append(
|
|
|
|
[]byte{byte(opcode.PUSHDATA1), 64},
|
|
|
|
make([]byte, 64)...,
|
|
|
|
),
|
|
|
|
VerificationScript: alphabet.GetVerificationScript(),
|
|
|
|
})
|
|
|
|
|
|
|
|
magicNumber, _ := c.GetNetwork()
|
|
|
|
|
|
|
|
// caller's witness
|
|
|
|
ww = append(ww, transaction.Witness{
|
|
|
|
InvocationScript: append(
|
|
|
|
[]byte{byte(opcode.PUSHDATA1), 64},
|
|
|
|
acc.PrivateKey().SignHashable(uint32(magicNumber), tx)...),
|
|
|
|
VerificationScript: acc.GetVerificationScript(),
|
|
|
|
})
|
|
|
|
|
|
|
|
// notary contract witness
|
|
|
|
ww = append(ww, transaction.Witness{
|
|
|
|
InvocationScript: append(
|
|
|
|
[]byte{byte(opcode.PUSHDATA1), 64},
|
|
|
|
make([]byte, 64)...,
|
|
|
|
),
|
|
|
|
VerificationScript: []byte{},
|
|
|
|
})
|
|
|
|
|
|
|
|
return ww
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkInvocationResults(res *result.Invoke) error {
|
|
|
|
if res.State != "HALT" {
|
|
|
|
return fmt.Errorf("test invocation state: %s, exception %s: ", res.State, res.FaultException)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(res.Script) == 0 {
|
|
|
|
return errors.New("empty invocation script")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|