Skip to content

Commit

Permalink
adm: Do not use emit package if possible
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Karpy <[email protected]>
  • Loading branch information
carpawell committed Nov 7, 2023
1 parent 5a0801e commit 472b197
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 81 deletions.
30 changes: 15 additions & 15 deletions cmd/neofs-adm/internal/modules/morph/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
}
Expand Down
18 changes: 9 additions & 9 deletions cmd/neofs-adm/internal/modules/morph/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
50 changes: 30 additions & 20 deletions cmd/neofs-adm/internal/modules/morph/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
}
Expand Down
28 changes: 18 additions & 10 deletions cmd/neofs-adm/internal/modules/morph/dump_hashes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
21 changes: 16 additions & 5 deletions cmd/neofs-adm/internal/modules/morph/initialize_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
15 changes: 8 additions & 7 deletions cmd/neofs-adm/internal/modules/morph/local_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down
15 changes: 9 additions & 6 deletions cmd/neofs-adm/internal/modules/morph/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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 {
Expand All @@ -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
}

Expand Down
Loading

0 comments on commit 472b197

Please sign in to comment.