Skip to content

Commit

Permalink
[#722] neofs-adm: Allow to initialize local dump
Browse files Browse the repository at this point in the history
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
  • Loading branch information
fyrchik authored and cthulhu-rider committed May 16, 2022
1 parent c55950b commit efb6545
Show file tree
Hide file tree
Showing 14 changed files with 500 additions and 134 deletions.
25 changes: 16 additions & 9 deletions cmd/neofs-adm/internal/modules/morph/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"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"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"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"
Expand Down Expand Up @@ -57,11 +56,19 @@ func dumpBalances(cmd *cobra.Command, _ []string) error {
}

ns, err := getNativeHashes(c)
if err != nil || ns[nativenames.Gas].Equals(util.Uint160{}) {
return errors.New("can't fetch hash of the GAS contract")
if err != nil {
return fmt.Errorf("can't fetch the list of native contracts: %w", err)
}

gasHash, ok := ns[nativenames.Gas]
if !ok {
return fmt.Errorf("can't find the %s native contract hash", nativenames.Gas)
}

gasHash := ns[nativenames.Gas]
desigHash, ok := ns[nativenames.Designation]
if !ok {
return fmt.Errorf("can't find the %s native contract hash", nativenames.Designation)
}

if !notaryEnabled || dumpStorage || dumpAlphabet || dumpProxy {
nnsCs, err = c.GetContractStateByID(1)
Expand All @@ -75,7 +82,7 @@ func dumpBalances(cmd *cobra.Command, _ []string) error {
}
}

irList, err := fetchIRNodes(c, nmHash)
irList, err := fetchIRNodes(c, nmHash, desigHash)
if err != nil {
return err
}
Expand All @@ -86,7 +93,7 @@ func dumpBalances(cmd *cobra.Command, _ []string) error {
printBalances(cmd, "Inner ring nodes balances:", irList)

if dumpStorage {
res, err := c.InvokeFunction(nmHash, "netmap", []smartcontract.Parameter{}, nil)
res, err := invokeFunction(c, nmHash, "netmap", []interface{}{}, nil)
if err != nil || res.State != vm.HaltState.String() || len(res.Stack) == 0 {
return errors.New("can't fetch the list of storage nodes")
}
Expand Down Expand Up @@ -170,7 +177,7 @@ func dumpBalances(cmd *cobra.Command, _ []string) error {
return nil
}

func fetchIRNodes(c Client, nmHash util.Uint160) ([]accBalancePair, error) {
func fetchIRNodes(c Client, nmHash, desigHash util.Uint160) ([]accBalancePair, error) {
var irList []accBalancePair

if notaryEnabled {
Expand All @@ -179,7 +186,7 @@ func fetchIRNodes(c Client, nmHash util.Uint160) ([]accBalancePair, error) {
return nil, fmt.Errorf("can't get block height: %w", err)
}

arr, err := c.GetDesignatedByRole(noderoles.NeoFSAlphabet, height)
arr, err := getDesignatedByRole(c, desigHash, noderoles.NeoFSAlphabet, height)
if err != nil {
return nil, errors.New("can't fetch list of IR nodes from the netmap contract")
}
Expand All @@ -189,7 +196,7 @@ func fetchIRNodes(c Client, nmHash util.Uint160) ([]accBalancePair, error) {
irList[i].scriptHash = arr[i].GetScriptHash()
}
} else {
res, err := c.InvokeFunction(nmHash, "innerRingList", []smartcontract.Parameter{}, nil)
res, err := invokeFunction(c, nmHash, "innerRingList", []interface{}{}, nil)
if err != nil || res.State != vm.HaltState.String() || len(res.Stack) == 0 {
return nil, errors.New("can't fetch list of IR nodes from the netmap contract")
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/neofs-adm/internal/modules/morph/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"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"
Expand Down Expand Up @@ -49,8 +48,7 @@ func dumpContainers(cmd *cobra.Command, _ []string) error {
}
}

res, err := c.InvokeFunction(ch, "list",
[]smartcontract.Parameter{{Type: smartcontract.StringType, Value: ""}}, nil)
res, err := invokeFunction(c, ch, "list", []interface{}{""}, nil)
if err != nil {
return fmt.Errorf("%w: %v", errInvalidContainerResponse, err)
}
Expand Down
5 changes: 1 addition & 4 deletions cmd/neofs-adm/internal/modules/morph/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import (
"fmt"
"text/tabwriter"

"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"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"
Expand Down Expand Up @@ -160,8 +158,7 @@ func dumpNetworkConfig(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("can't get netmap contract hash: %w", err)
}

res, err := c.InvokeFunction(nmHash, "listConfig",
[]smartcontract.Parameter{}, []transaction.Signer{{}})
res, err := invokeFunction(c, nmHash, "listConfig", nil, nil)
if err != nil || res.State != vm.HaltState.String() || len(res.Stack) == 0 {
return errors.New("can't fetch list of network config keys from the netmap contract")
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/neofs-adm/internal/modules/morph/epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"

"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
Expand All @@ -29,7 +28,7 @@ func forceNewEpochCmd(cmd *cobra.Command, args []string) error {
return fmt.Errorf("can't get netmap contract hash: %w", err)
}

res, err := wCtx.Client.InvokeFunction(nmHash, "epoch", []smartcontract.Parameter{}, nil)
res, err := invokeFunction(wCtx.Client, nmHash, "epoch", nil, nil)
if err != nil || res.State != vm.HaltState.String() || len(res.Stack) == 0 {
return errors.New("can't fetch current epoch from the netmap contract")
}
Expand Down
38 changes: 33 additions & 5 deletions cmd/neofs-adm/internal/modules/morph/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func initializeSideChainCmd(cmd *cobra.Command, args []string) error {
if err != nil {
return fmt.Errorf("initialization error: %w", err)
}
defer initCtx.close()

// 1. Transfer funds to committee accounts.
cmd.Println("Stage 1: transfer GAS to alphabet nodes.")
Expand Down Expand Up @@ -98,6 +99,16 @@ func initializeSideChainCmd(cmd *cobra.Command, args []string) error {
return nil
}

func (c *initializeContext) close() {
if local, ok := c.Client.(*localClient); ok {
err := local.dump()
if err != nil {
c.Command.PrintErrf("Can't write dump: %v\n", err)
os.Exit(1)
}
}
}

func newInitializeContext(cmd *cobra.Command, v *viper.Viper) (*initializeContext, error) {
walletDir := config.ResolveHomePath(viper.GetString(alphabetWalletsFlag))
wallets, err := openAlphabetWallets(walletDir)
Expand All @@ -110,7 +121,18 @@ func newInitializeContext(cmd *cobra.Command, v *viper.Viper) (*initializeContex
return nil, err
}

c, err := getN3Client(v)
var c Client
if v.GetString(localDumpFlag) != "" {
if cmd.Name() != "init" {
return nil, errors.New("dump creation is only supported for `init` command")
}
if v.GetString(endpointFlag) != "" {
return nil, fmt.Errorf("`%s` and `%s` flags are mutually exclusive", endpointFlag, localDumpFlag)
}
c, err = newLocalClient(v, wallets)
} else {
c, err = getN3Client(v)
}
if err != nil {
return nil, fmt.Errorf("can't create N3 client: %w", err)
}
Expand Down Expand Up @@ -287,6 +309,12 @@ func (c *clientContext) awaitTx(cmd *cobra.Command) error {
return nil
}

if local, ok := c.Client.(*localClient); ok {
if err := local.putTransactions(); err != nil {
return fmt.Errorf("can't persist transactions: %w", err)
}
}

cmd.Println("Waiting for transactions to persist...")

tick := time.NewTicker(c.PollInterval)
Expand All @@ -304,8 +332,8 @@ loop:
res, err := c.Client.GetApplicationLog(c.Hashes[i], &at)
if err == nil {
if retErr == nil && len(res.Executions) > 0 && res.Executions[0].VMState != vm.HaltState {
retErr = fmt.Errorf("tx persisted in %s state: %s",
res.Executions[0].VMState, res.Executions[0].FaultException)
retErr = fmt.Errorf("tx %d persisted in %s state: %s",
i, res.Executions[0].VMState, res.Executions[0].FaultException)
}
continue loop
}
Expand All @@ -315,8 +343,8 @@ loop:
res, err := c.Client.GetApplicationLog(c.Hashes[i], &at)
if err == nil {
if retErr == nil && len(res.Executions) > 0 && res.Executions[0].VMState != vm.HaltState {
retErr = fmt.Errorf("tx persisted in %s state: %s",
res.Executions[0].VMState, res.Executions[0].FaultException)
retErr = fmt.Errorf("tx %d persisted in %s state: %s",
i, res.Executions[0].VMState, res.Executions[0].FaultException)
}
continue loop
}
Expand Down
Loading

0 comments on commit efb6545

Please sign in to comment.