2022-04-11 14:25:58 +00:00
|
|
|
package morph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/elliptic"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/nns"
|
2024-01-31 14:26:26 +00:00
|
|
|
morphUtil "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/util"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
2022-04-11 14:25:58 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/noderoles"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2022-11-23 14:44:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/gas"
|
2022-09-05 12:45:38 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
|
2023-10-24 10:06:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/management"
|
2022-11-23 14:44:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/rolemgmt"
|
2022-09-05 12:45:38 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"
|
2022-04-11 14:25:58 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2022-07-18 08:33:53 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/vmstate"
|
2022-04-11 14:25:58 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
type accBalancePair struct {
|
|
|
|
scriptHash util.Uint160
|
|
|
|
balance *big.Int
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
dumpBalancesStorageFlag = "storage"
|
|
|
|
dumpBalancesAlphabetFlag = "alphabet"
|
|
|
|
dumpBalancesProxyFlag = "proxy"
|
|
|
|
dumpBalancesUseScriptHashFlag = "script-hash"
|
|
|
|
)
|
|
|
|
|
|
|
|
func dumpBalances(cmd *cobra.Command, _ []string) error {
|
|
|
|
var (
|
|
|
|
dumpStorage, _ = cmd.Flags().GetBool(dumpBalancesStorageFlag)
|
|
|
|
dumpAlphabet, _ = cmd.Flags().GetBool(dumpBalancesAlphabetFlag)
|
|
|
|
dumpProxy, _ = cmd.Flags().GetBool(dumpBalancesProxyFlag)
|
|
|
|
nnsCs *state.Contract
|
|
|
|
nmHash util.Uint160
|
|
|
|
)
|
|
|
|
|
2024-01-31 14:26:26 +00:00
|
|
|
c, err := morphUtil.GetN3Client(viper.GetViper())
|
2022-04-11 14:25:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-05 12:45:38 +00:00
|
|
|
inv := invoker.New(c, nil)
|
|
|
|
|
2023-05-15 12:14:56 +00:00
|
|
|
if dumpStorage || dumpAlphabet || dumpProxy {
|
2023-10-24 10:06:30 +00:00
|
|
|
r := management.NewReader(inv)
|
|
|
|
nnsCs, err = r.GetContractByID(1)
|
2022-04-11 14:25:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't get NNS contract info: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-12-21 10:59:46 +00:00
|
|
|
nmHash, err = nnsResolveHash(inv, nnsCs.Hash, domainOf(netmapContract))
|
2022-04-11 14:25:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't get netmap contract hash: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-15 12:14:56 +00:00
|
|
|
irList, err := fetchIRNodes(c, rolemgmt.Hash)
|
2022-04-11 14:25:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-23 14:44:03 +00:00
|
|
|
if err := fetchBalances(inv, gas.Hash, irList); err != nil {
|
2022-04-11 14:25:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
printBalances(cmd, "Inner ring nodes balances:", irList)
|
|
|
|
|
|
|
|
if dumpStorage {
|
2023-03-21 14:36:14 +00:00
|
|
|
if err := printStorageNodeBalances(cmd, inv, nmHash); err != nil {
|
2022-04-11 14:25:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if dumpProxy {
|
2023-03-21 14:36:14 +00:00
|
|
|
if err := printProxyContractBalance(cmd, inv, nnsCs.Hash); err != nil {
|
|
|
|
return err
|
2022-04-11 14:25:58 +00:00
|
|
|
}
|
2023-03-21 14:36:14 +00:00
|
|
|
}
|
2022-04-11 14:25:58 +00:00
|
|
|
|
2023-03-21 14:36:14 +00:00
|
|
|
if dumpAlphabet {
|
|
|
|
if err := printAlphabetContractBalances(cmd, c, inv, len(irList), nnsCs.Hash); err != nil {
|
2022-04-11 14:25:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 14:36:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-04-11 14:25:58 +00:00
|
|
|
|
2023-03-21 14:36:14 +00:00
|
|
|
func printStorageNodeBalances(cmd *cobra.Command, inv *invoker.Invoker, nmHash util.Uint160) error {
|
|
|
|
arr, err := unwrap.Array(inv.Call(nmHash, "netmap"))
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("can't fetch the list of storage nodes")
|
|
|
|
}
|
|
|
|
|
|
|
|
snList := make([]accBalancePair, len(arr))
|
|
|
|
for i := range arr {
|
|
|
|
node, ok := arr[i].Value().([]stackitem.Item)
|
|
|
|
if !ok || len(node) == 0 {
|
|
|
|
return errors.New("can't parse the list of storage nodes")
|
2022-04-11 14:25:58 +00:00
|
|
|
}
|
2023-03-21 14:36:14 +00:00
|
|
|
bs, err := node[0].TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("can't parse the list of storage nodes")
|
2022-04-11 14:25:58 +00:00
|
|
|
}
|
2023-03-21 14:36:14 +00:00
|
|
|
var ni netmap.NodeInfo
|
|
|
|
if err := ni.Unmarshal(bs); err != nil {
|
|
|
|
return fmt.Errorf("can't parse the list of storage nodes: %w", err)
|
|
|
|
}
|
|
|
|
pub, err := keys.NewPublicKeyFromBytes(ni.PublicKey(), elliptic.P256())
|
2022-04-11 14:25:58 +00:00
|
|
|
if err != nil {
|
2023-03-21 14:36:14 +00:00
|
|
|
return fmt.Errorf("can't parse storage node public key: %w", err)
|
2022-04-11 14:25:58 +00:00
|
|
|
}
|
2023-03-21 14:36:14 +00:00
|
|
|
snList[i].scriptHash = pub.GetScriptHash()
|
|
|
|
}
|
2022-04-11 14:25:58 +00:00
|
|
|
|
2023-03-21 14:36:14 +00:00
|
|
|
if err := fetchBalances(inv, gas.Hash, snList); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-11 14:25:58 +00:00
|
|
|
|
2023-03-21 14:36:14 +00:00
|
|
|
printBalances(cmd, "\nStorage node balances:", snList)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func printProxyContractBalance(cmd *cobra.Command, inv *invoker.Invoker, nnsHash util.Uint160) error {
|
2023-12-21 10:59:46 +00:00
|
|
|
h, err := nnsResolveHash(inv, nnsHash, domainOf(proxyContract))
|
2023-03-21 14:36:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't get hash of the proxy contract: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
proxyList := []accBalancePair{{scriptHash: h}}
|
|
|
|
if err := fetchBalances(inv, gas.Hash, proxyList); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
printBalances(cmd, "\nProxy contract balance:", proxyList)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-31 14:26:26 +00:00
|
|
|
func printAlphabetContractBalances(cmd *cobra.Command, c morphUtil.Client, inv *invoker.Invoker, count int, nnsHash util.Uint160) error {
|
2023-03-21 14:36:14 +00:00
|
|
|
alphaList := make([]accBalancePair, count)
|
|
|
|
|
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
for i := range alphaList {
|
|
|
|
emit.AppCall(w.BinWriter, nnsHash, "resolve", callflag.ReadOnly,
|
|
|
|
getAlphabetNNSDomain(i),
|
|
|
|
int64(nns.TXT))
|
|
|
|
}
|
|
|
|
if w.Err != nil {
|
|
|
|
panic(w.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
alphaRes, err := c.InvokeScript(w.Bytes(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't fetch info from NNS: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range alphaList {
|
|
|
|
h, err := parseNNSResolveResult(alphaRes.Stack[i])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't fetch the alphabet contract #%d hash: %w", i, err)
|
2022-04-11 14:25:58 +00:00
|
|
|
}
|
2023-03-21 14:36:14 +00:00
|
|
|
alphaList[i].scriptHash = h
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := fetchBalances(inv, gas.Hash, alphaList); err != nil {
|
|
|
|
return err
|
2022-04-11 14:25:58 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 14:36:14 +00:00
|
|
|
printBalances(cmd, "\nAlphabet contracts balances:", alphaList)
|
2022-04-11 14:25:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-31 14:26:26 +00:00
|
|
|
func fetchIRNodes(c morphUtil.Client, desigHash util.Uint160) ([]accBalancePair, error) {
|
2022-09-05 12:45:38 +00:00
|
|
|
inv := invoker.New(c, nil)
|
|
|
|
|
2023-05-15 12:14:56 +00:00
|
|
|
height, err := c.GetBlockCount()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't get block height: %w", err)
|
|
|
|
}
|
2022-04-11 14:25:58 +00:00
|
|
|
|
2024-01-31 14:26:26 +00:00
|
|
|
arr, err := morphUtil.GetDesignatedByRole(inv, desigHash, noderoles.NeoFSAlphabet, height)
|
2023-05-15 12:14:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("can't fetch list of IR nodes from the netmap contract")
|
|
|
|
}
|
2022-04-11 14:25:58 +00:00
|
|
|
|
2023-05-15 12:14:56 +00:00
|
|
|
irList := make([]accBalancePair, len(arr))
|
|
|
|
for i := range arr {
|
|
|
|
irList[i].scriptHash = arr[i].GetScriptHash()
|
2022-04-11 14:25:58 +00:00
|
|
|
}
|
|
|
|
return irList, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func printBalances(cmd *cobra.Command, prefix string, accounts []accBalancePair) {
|
|
|
|
useScriptHash, _ := cmd.Flags().GetBool(dumpBalancesUseScriptHashFlag)
|
|
|
|
|
|
|
|
cmd.Println(prefix)
|
|
|
|
for i := range accounts {
|
|
|
|
var addr string
|
|
|
|
if useScriptHash {
|
|
|
|
addr = accounts[i].scriptHash.StringLE()
|
|
|
|
} else {
|
|
|
|
addr = address.Uint160ToString(accounts[i].scriptHash)
|
|
|
|
}
|
|
|
|
cmd.Printf("%s: %s\n", addr, fixedn.ToString(accounts[i].balance, 8))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 12:45:38 +00:00
|
|
|
func fetchBalances(c *invoker.Invoker, gasHash util.Uint160, accounts []accBalancePair) error {
|
2022-04-11 14:25:58 +00:00
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
for i := range accounts {
|
|
|
|
emit.AppCall(w.BinWriter, gasHash, "balanceOf", callflag.ReadStates, accounts[i].scriptHash)
|
|
|
|
}
|
|
|
|
if w.Err != nil {
|
|
|
|
panic(w.Err)
|
|
|
|
}
|
|
|
|
|
2022-09-05 12:45:38 +00:00
|
|
|
res, err := c.Run(w.Bytes())
|
2022-07-18 08:33:53 +00:00
|
|
|
if err != nil || res.State != vmstate.Halt.String() || len(res.Stack) != len(accounts) {
|
2022-04-11 14:25:58 +00:00
|
|
|
return errors.New("can't fetch account balances")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range accounts {
|
|
|
|
bal, err := res.Stack[i].TryInteger()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't parse account balance: %w", err)
|
|
|
|
}
|
|
|
|
accounts[i].balance = bal
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|