diff --git a/cmd/neofs-adm/internal/modules/morph/balance.go b/cmd/neofs-adm/internal/modules/morph/balance.go index fa56c4d4c4c..74ece05e0a9 100644 --- a/cmd/neofs-adm/internal/modules/morph/balance.go +++ b/cmd/neofs-adm/internal/modules/morph/balance.go @@ -11,14 +11,12 @@ import ( "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" "github.com/nspcc-dev/neo-go/pkg/rpcclient/gas" "github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker" "github.com/nspcc-dev/neo-go/pkg/rpcclient/rolemgmt" "github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap" - "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" + "github.com/nspcc-dev/neo-go/pkg/smartcontract" "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" "github.com/nspcc-dev/neo-go/pkg/vm/vmstate" "github.com/nspcc-dev/neofs-contract/nns" @@ -126,17 +124,17 @@ func dumpBalances(cmd *cobra.Command, _ []string) error { if dumpAlphabet { alphaList := make([]accBalancePair, len(irList)) - w := io.NewBufBinWriter() + b := smartcontract.NewBuilder() for i := range alphaList { - emit.AppCall(w.BinWriter, nnsCs.Hash, "resolve", callflag.ReadOnly, - getAlphabetNNSDomain(i), - int64(nns.TXT)) + b.InvokeMethod(nnsCs.Hash, "resolve", getAlphabetNNSDomain(i), int64(nns.TXT)) } - if w.Err != nil { - panic(w.Err) + + script, err := b.Script() + if err != nil { + return fmt.Errorf("resolbing alphabet hashes script: %w", err) } - alphaRes, err := c.InvokeScript(w.Bytes(), nil) + alphaRes, err := c.InvokeScript(script, nil) if err != nil { return fmt.Errorf("can't fetch info from NNS: %w", err) } @@ -194,15 +192,17 @@ func printBalances(cmd *cobra.Command, prefix string, accounts []accBalancePair) } func fetchBalances(c *invoker.Invoker, gasHash util.Uint160, accounts []accBalancePair) error { - w := io.NewBufBinWriter() + b := smartcontract.NewBuilder() for i := range accounts { - emit.AppCall(w.BinWriter, gasHash, "balanceOf", callflag.ReadStates, accounts[i].scriptHash) + b.InvokeMethod(gasHash, "balanceOf", accounts[i].scriptHash) } - if w.Err != nil { - panic(w.Err) + + script, err := b.Script() + if err != nil { + return fmt.Errorf("reading balances script: %w", err) } - res, err := c.Run(w.Bytes()) + res, err := c.Run(script) if err != nil || res.State != vmstate.Halt.String() || len(res.Stack) != len(accounts) { return errors.New("can't fetch account balances") } diff --git a/cmd/neofs-adm/internal/modules/morph/config.go b/cmd/neofs-adm/internal/modules/morph/config.go index bf9b2ee9673..3b96c05110d 100644 --- a/cmd/neofs-adm/internal/modules/morph/config.go +++ b/cmd/neofs-adm/internal/modules/morph/config.go @@ -10,11 +10,9 @@ import ( "strings" "text/tabwriter" - "github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker" "github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap" - "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" - "github.com/nspcc-dev/neo-go/pkg/vm/emit" + "github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -116,7 +114,7 @@ func setConfigCmd(cmd *cobra.Command, args []string) error { forceFlag, _ := cmd.Flags().GetBool(forceConfigSet) - bw := io.NewBufBinWriter() + b := smartcontract.NewBuilder() for _, arg := range args { k, v, err := parseConfigPair(arg, forceFlag) if err != nil { @@ -126,13 +124,15 @@ func setConfigCmd(cmd *cobra.Command, args []string) error { // In NeoFS this is done via Notary contract. Here, however, we can form the // transaction locally. The first `nil` argument is required only for notary // disabled environment which is not supported by that command. - emit.AppCall(bw.BinWriter, nmHash, "setConfig", callflag.All, nil, k, v) - if bw.Err != nil { - return fmt.Errorf("can't form raw transaction: %w", bw.Err) - } + b.InvokeMethod(nmHash, "setConfig", nil, k, v) + } + + script, err := b.Script() + if err != nil { + return fmt.Errorf("config setting script: %w", err) } - err = wCtx.sendConsensusTx(bw.Bytes()) + err = wCtx.sendConsensusTx(script) if err != nil { return err } diff --git a/cmd/neofs-adm/internal/modules/morph/container.go b/cmd/neofs-adm/internal/modules/morph/container.go index 811d71be0d9..caa3100deee 100644 --- a/cmd/neofs-adm/internal/modules/morph/container.go +++ b/cmd/neofs-adm/internal/modules/morph/container.go @@ -8,12 +8,10 @@ import ( "sort" "github.com/nspcc-dev/neo-go/pkg/crypto/hash" - "github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker" "github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap" - "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" + "github.com/nspcc-dev/neo-go/pkg/smartcontract" "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" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" "github.com/spf13/cobra" @@ -82,15 +80,21 @@ func dumpContainers(cmd *cobra.Command, _ []string) error { } var containers []*Container - bw := io.NewBufBinWriter() + b := smartcontract.NewBuilder() for _, id := range cids { if !isOK(id) { continue } - bw.Reset() - emit.AppCall(bw.BinWriter, ch, "get", callflag.All, id) - emit.AppCall(bw.BinWriter, ch, "eACL", callflag.All, id) - res, err := inv.Run(bw.Bytes()) + b.Reset() + b.InvokeMethod(ch, "get", id) + b.InvokeMethod(ch, "eACL", id) + + script, err := b.Script() + if err != nil { + return fmt.Errorf("dumping '%s' container script: %w", id, err) + } + + res, err := inv.Run(script) if err != nil { return fmt.Errorf("can't get container info: %w", err) } @@ -190,15 +194,21 @@ func restoreContainers(cmd *cobra.Command, _ []string) error { return err } - bw := io.NewBufBinWriter() + b := smartcontract.NewBuilder() for _, cnt := range containers { hv := hash.Sha256(cnt.Value) if !isOK(hv[:]) { continue } - bw.Reset() - emit.AppCall(bw.BinWriter, ch, "get", callflag.All, hv.BytesBE()) - res, err := wCtx.Client.InvokeScript(bw.Bytes(), nil) + b.Reset() + b.InvokeMethod(ch, "get", hv.BytesBE()) + + script, err := b.Script() + if err != nil { + return fmt.Errorf("reading container script: %w", err) + } + + res, err := wCtx.Client.InvokeScript(script, nil) if err != nil { return fmt.Errorf("can't check if container is already restored: %w", err) } @@ -217,18 +227,18 @@ func restoreContainers(cmd *cobra.Command, _ []string) error { continue } - bw.Reset() - emit.AppCall(bw.BinWriter, ch, "put", callflag.All, - cnt.Value, cnt.Signature, cnt.PublicKey, cnt.Token) + b.Reset() + b.InvokeMethod(ch, "put", cnt.Value, cnt.Signature, cnt.PublicKey, cnt.Token) if ea := cnt.EACL; ea != nil { - emit.AppCall(bw.BinWriter, ch, "setEACL", callflag.All, - ea.Value, ea.Signature, ea.PublicKey, ea.Token) + b.InvokeMethod(ch, "setEACL", ea.Value, ea.Signature, ea.PublicKey, ea.Token) } - if bw.Err != nil { - panic(bw.Err) + + script, err = b.Script() + if err != nil { + return fmt.Errorf("container update script: %w", err) } - if err := wCtx.sendConsensusTx(bw.Bytes()); err != nil { + if err := wCtx.sendConsensusTx(script); err != nil { return err } } diff --git a/cmd/neofs-adm/internal/modules/morph/dump_hashes.go b/cmd/neofs-adm/internal/modules/morph/dump_hashes.go index 9614aab411a..b4175d371b9 100644 --- a/cmd/neofs-adm/internal/modules/morph/dump_hashes.go +++ b/cmd/neofs-adm/internal/modules/morph/dump_hashes.go @@ -12,6 +12,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker" "github.com/nspcc-dev/neo-go/pkg/rpcclient/nep11" "github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap" + "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" @@ -60,17 +61,20 @@ func dumpContractHashes(cmd *cobra.Command, _ []string) error { } } - bw := io.NewBufBinWriter() + b := smartcontract.NewBuilder() if irSize != 0 { - bw.Reset() + b.Reset() for i := 0; i < irSize; i++ { - emit.AppCall(bw.BinWriter, cs.Hash, "resolve", callflag.ReadOnly, - getAlphabetNNSDomain(i), - int64(nns.TXT)) + b.InvokeMethod(cs.Hash, "resolve", getAlphabetNNSDomain(i), int64(nns.TXT)) + } + + script, err := b.Script() + if err != nil { + return fmt.Errorf("resolving alphabet hashes script: %w", err) } - alphaRes, err := c.InvokeScript(bw.Bytes(), nil) + alphaRes, err := c.InvokeScript(script, nil) if err != nil { return fmt.Errorf("can't fetch info from NNS: %w", err) } @@ -85,11 +89,15 @@ func dumpContractHashes(cmd *cobra.Command, _ []string) error { } for _, ctrName := range contractList { - bw.Reset() - emit.AppCall(bw.BinWriter, cs.Hash, "resolve", callflag.ReadOnly, - ctrName+".neofs", int64(nns.TXT)) + b.Reset() + b.InvokeMethod(cs.Hash, "resolve", ctrName+".neofs", int64(nns.TXT)) + + script, err := b.Script() + if err != nil { + return fmt.Errorf("resolving neofs contract hashes script: %w", err) + } - res, err := c.InvokeScript(bw.Bytes(), nil) + res, err := c.InvokeScript(script, nil) if err != nil { return fmt.Errorf("can't fetch info from NNS: %w", err) } diff --git a/cmd/neofs-adm/internal/modules/morph/initialize_register.go b/cmd/neofs-adm/internal/modules/morph/initialize_register.go index d28299e4d3d..2989c4ddbeb 100644 --- a/cmd/neofs-adm/internal/modules/morph/initialize_register.go +++ b/cmd/neofs-adm/internal/modules/morph/initialize_register.go @@ -3,6 +3,7 @@ package morph import ( "errors" "fmt" + "math/big" "github.com/nspcc-dev/neo-go/pkg/core/native" "github.com/nspcc-dev/neo-go/pkg/core/state" @@ -12,6 +13,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/rpcclient/actor" "github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker" "github.com/nspcc-dev/neo-go/pkg/rpcclient/neo" + "github.com/nspcc-dev/neo-go/pkg/rpcclient/nep17" "github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap" "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" "github.com/nspcc-dev/neo-go/pkg/util" @@ -100,15 +102,24 @@ func (c *initializeContext) transferNEOToAlphabetContracts() error { cs := c.getContract(alphabetContract) amount := initialAlphabetNEOAmount / len(c.Wallets) - bw := io.NewBufBinWriter() + tNeo := nep17.New(c.CommitteeAct, neo.Hash) + pp := make([]nep17.TransferParameters, 0, len(c.Accounts)) + for _, acc := range c.Accounts { h := state.CreateContractHash(acc.Contract.ScriptHash(), cs.NEF.Checksum, cs.Manifest.Name) - emit.AppCall(bw.BinWriter, neo.Hash, "transfer", callflag.All, - c.CommitteeAcc.Contract.ScriptHash(), h, int64(amount), nil) - emit.Opcodes(bw.BinWriter, opcode.ASSERT) + + pp = append(pp, nep17.TransferParameters{ + To: h, + Amount: big.NewInt(int64(amount)), + }) + } + + tx, err := tNeo.MultiTransferUnsigned(pp) + if err != nil { + return fmt.Errorf("multi transfer script: %w", err) } - if err := c.sendCommitteeTx(bw.Bytes(), false); err != nil { + if err := c.multiSignAndSend(tx, committeeAccountName); err != nil { return err } diff --git a/cmd/neofs-adm/internal/modules/morph/local_client.go b/cmd/neofs-adm/internal/modules/morph/local_client.go index 5c98c00bdb8..fb2dcfdd100 100644 --- a/cmd/neofs-adm/internal/modules/morph/local_client.go +++ b/cmd/neofs-adm/internal/modules/morph/local_client.go @@ -27,7 +27,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" "github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger" "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/opcode" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neo-go/pkg/wallet" @@ -315,13 +314,15 @@ func (l *localClient) putTransactions() error { } func invokeFunction(c Client, h util.Uint160, method string, parameters []interface{}, signers []transaction.Signer) (*result.Invoke, error) { - w := io.NewBufBinWriter() - emit.Array(w.BinWriter, parameters...) - emit.AppCallNoArgs(w.BinWriter, h, method, callflag.All) - if w.Err != nil { - panic(fmt.Sprintf("BUG: invalid parameters for '%s': %v", method, w.Err)) + b := smartcontract.NewBuilder() + b.InvokeMethod(h, method, parameters...) + + script, err := b.Script() + if err != nil { + return nil, fmt.Errorf("BUG: invalid parameters for '%s': %v", method, err) } - return c.InvokeScript(w.Bytes(), signers) + + return c.InvokeScript(script, signers) } var errGetDesignatedByRoleResponse = errors.New("`getDesignatedByRole`: invalid response") diff --git a/cmd/neofs-adm/internal/modules/morph/policy.go b/cmd/neofs-adm/internal/modules/morph/policy.go index b00fa9c7d4d..82a6d616161 100644 --- a/cmd/neofs-adm/internal/modules/morph/policy.go +++ b/cmd/neofs-adm/internal/modules/morph/policy.go @@ -5,10 +5,8 @@ import ( "strconv" "strings" - "github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/rpcclient/policy" - "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" - "github.com/nspcc-dev/neo-go/pkg/vm/emit" + "github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -25,7 +23,7 @@ func setPolicyCmd(cmd *cobra.Command, args []string) error { return fmt.Errorf("can't to initialize context: %w", err) } - bw := io.NewBufBinWriter() + b := smartcontract.NewBuilder() for i := range args { kv := strings.SplitN(args[i], "=", 2) if len(kv) != 2 { @@ -43,10 +41,15 @@ func setPolicyCmd(cmd *cobra.Command, args []string) error { return fmt.Errorf("can't parse parameter value '%s': %w", args[1], err) } - emit.AppCall(bw.BinWriter, policy.Hash, "set"+kv[0], callflag.All, int64(value)) + b.InvokeMethod(policy.Hash, "set"+kv[0], int64(value)) } - if err := wCtx.sendCommitteeTx(bw.Bytes(), false); err != nil { + script, err := b.Script() + if err != nil { + return fmt.Errorf("policy setting script: %w", err) + } + + if err := wCtx.sendCommitteeTx(script, false); err != nil { return err } diff --git a/cmd/neofs-adm/internal/modules/morph/renew_domain.go b/cmd/neofs-adm/internal/modules/morph/renew_domain.go index dfbb073ad11..af7855434dd 100644 --- a/cmd/neofs-adm/internal/modules/morph/renew_domain.go +++ b/cmd/neofs-adm/internal/modules/morph/renew_domain.go @@ -2,13 +2,12 @@ package morph import ( "errors" + "fmt" "strings" - "github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/rpcclient/nep11" "github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap" - "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" - "github.com/nspcc-dev/neo-go/pkg/vm/emit" + "github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -56,18 +55,21 @@ func renewDomain(cmd *cobra.Command, _ []string) error { domains = append(domains, dom) } - bw := io.NewBufBinWriter() + b := smartcontract.NewBuilder() for i := range domains { - emit.AppCall(bw.BinWriter, nns.Hash, "renew", callflag.All, domains[i]) - if bw.Err != nil { - return bw.Err + b.InvokeMethod(nns.Hash, "renew", domains[i]) + + script, err := b.Script() + if err != nil { + return fmt.Errorf("renew script: %w", err) } + // Default registration price is 10 GAS, adding more domains // into the script makes test execution to fail. - if err := wCtx.sendConsensusTx(bw.Bytes()); err != nil { + if err := wCtx.sendConsensusTx(script); err != nil { return err } - bw.Reset() + b.Reset() } return wCtx.awaitTx() }