From 073754c66f664ce5744ac07caf00c0f2ce1a3059 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 29 Aug 2023 18:08:17 +0400 Subject: [PATCH] sidechain/deploy: Drop unused utilities working with contract versions Signed-off-by: Leonard Lyubich --- go.mod | 2 - go.sum | 4 - pkg/morph/deploy/util.go | 131 --------------------------- pkg/morph/deploy/util_test.go | 163 ---------------------------------- 4 files changed, 300 deletions(-) delete mode 100644 pkg/morph/deploy/util_test.go diff --git a/go.mod b/go.mod index 8832982e77..4cf076350f 100644 --- a/go.mod +++ b/go.mod @@ -98,11 +98,9 @@ require ( go.uber.org/multierr v1.10.0 // indirect golang.org/x/crypto v0.14.0 // indirect golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect - golang.org/x/mod v0.12.0 // indirect golang.org/x/net v0.17.0 // indirect golang.org/x/sync v0.3.0 // indirect golang.org/x/text v0.13.0 // indirect - golang.org/x/tools v0.13.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20230920204549-e6e6cdab5c13 // indirect gopkg.in/ini.v1 v1.67.0 // indirect lukechampine.com/blake3 v1.1.7 // indirect diff --git a/go.sum b/go.sum index a542684354..d648ca538b 100644 --- a/go.sum +++ b/go.sum @@ -186,7 +186,6 @@ github.com/ipfs/go-cid v0.3.2 h1:OGgOd+JCFM+y1DjWPmVH+2/4POtpDzwcr7VgnB7mZXc= github.com/ipfs/go-cid v0.3.2/go.mod h1:gQ8pKqT/sUxGY+tIwy1RPpAojYu7jAyCp5Tz1svoupw= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= -github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= @@ -368,7 +367,6 @@ go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk= -go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ= go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= @@ -421,7 +419,6 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= -golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -595,7 +592,6 @@ golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4f golang.org/x/tools v0.0.0-20210108195828-e2f9c7f1fc8e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ= -golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/pkg/morph/deploy/util.go b/pkg/morph/deploy/util.go index 027e9d5633..bd01dab17e 100644 --- a/pkg/morph/deploy/util.go +++ b/pkg/morph/deploy/util.go @@ -2,7 +2,6 @@ package deploy import ( "context" - "encoding/json" "errors" "fmt" "strings" @@ -10,25 +9,14 @@ import ( "time" "github.com/nspcc-dev/neo-go/pkg/core/block" - "github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames" "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" "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/io" "github.com/nspcc-dev/neo-go/pkg/neorpc" - "github.com/nspcc-dev/neo-go/pkg/neorpc/result" "github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker" - "github.com/nspcc-dev/neo-go/pkg/rpcclient/management" - "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/smartcontract/manifest" "github.com/nspcc-dev/neo-go/pkg/smartcontract/nef" "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/neofs-contract/common" "go.uber.org/zap" ) @@ -181,125 +169,6 @@ func readNNSOnChainState(b Blockchain) (*state.Contract, error) { return res, nil } -// contractVersion describes versioning of NeoFS smart contracts. -type contractVersion struct{ major, minor, patch uint64 } - -// space sizes for major and minor versions of the NeoFS contracts. -const majorSpace, minorSpace = 1e6, 1e3 - -// equals checks if contractVersion equals to the specified SemVer version. -// -//nolint:unused -func (x contractVersion) equals(major, minor, patch uint64) bool { - return x.major == major && x.minor == minor && x.patch == patch -} - -// returns contractVersion as single integer. -func (x contractVersion) toUint64() uint64 { - return x.major*majorSpace + x.minor*minorSpace + x.patch -} - -// cmp compares x and y and returns: -// -// -1 if x < y -// 0 if x == y -// +1 if x > y -func (x contractVersion) cmp(y contractVersion) int { - xN := x.toUint64() - yN := y.toUint64() - if xN < yN { - return -1 - } else if xN == yN { - return 0 - } - return 1 -} - -func (x contractVersion) String() string { - const sep = "." - return fmt.Sprintf("%d%s%d%s%d", x.major, sep, x.minor, sep, x.patch) -} - -// parses contractVersion from the invocation result of methodVersion method. -func parseContractVersionFromInvocationResult(res *result.Invoke) (contractVersion, error) { - bigVersionOnChain, err := unwrap.BigInt(res, nil) - if err != nil { - return contractVersion{}, fmt.Errorf("unwrap big integer from '%s' method return: %w", methodVersion, err) - } else if !bigVersionOnChain.IsUint64() { - return contractVersion{}, fmt.Errorf("invalid/unsupported format of the '%s' method return: expected uint64, got %v", methodVersion, bigVersionOnChain) - } - - n := bigVersionOnChain.Uint64() - - mjr := n / majorSpace - - return contractVersion{ - major: mjr, - minor: (n - mjr*majorSpace) / minorSpace, - patch: n % minorSpace, - }, nil -} - -// readContractOnChainVersion returns current version of the smart contract -// presented in given Blockchain with specified address. -func readContractOnChainVersion(b Blockchain, onChainAddress util.Uint160) (contractVersion, error) { - res, err := invoker.New(b, nil).Call(onChainAddress, methodVersion) - if err != nil { - return contractVersion{}, fmt.Errorf("call '%s' contract method: %w", methodVersion, err) - } - - return parseContractVersionFromInvocationResult(res) -} - -// readContractLocalVersion returns version of the local smart contract -// represented by its compiled artifacts. Deployment is tested using provided -// invoker on behalf of the committee. -func readContractLocalVersion(rpc invoker.RPCInvoke, committee keys.PublicKeys, localNEF nef.File, localManifest manifest.Manifest, deployArgs ...interface{}) (contractVersion, error) { - multiSigScript, err := smartcontract.CreateMultiSigRedeemScript(smartcontract.GetMajorityHonestNodeCount(len(committee)), committee) - if err != nil { - return contractVersion{}, fmt.Errorf("create committee multi-signature verification script: %w", err) - } - - jManifest, err := json.Marshal(localManifest) - if err != nil { - return contractVersion{}, fmt.Errorf("encode manifest into JSON: %w", err) - } - - bNEF, err := localNEF.Bytes() - if err != nil { - return contractVersion{}, fmt.Errorf("encode NEF into binary: %w", err) - } - - var deployData interface{} - if len(deployArgs) > 0 { - deployData = deployArgs - } - - script := io.NewBufBinWriter() - emit.Opcodes(script.BinWriter, opcode.NEWARRAY0) - emit.Int(script.BinWriter, int64(callflag.All)) - emit.String(script.BinWriter, methodVersion) - emit.AppCall(script.BinWriter, management.Hash, "deploy", callflag.All, bNEF, jManifest, deployData) - emit.Opcodes(script.BinWriter, opcode.PUSH2, opcode.PICKITEM) - emit.Syscall(script.BinWriter, interopnames.SystemContractCall) - - res, err := invoker.New(rpc, []transaction.Signer{ - { - Account: util.Uint160{}, // zero hash to avoid 'contract already exists' case - Scopes: transaction.None, - }, - { - Account: hash.Hash160(multiSigScript), - Scopes: transaction.Global, - }, - }).Run(script.Bytes()) - if err != nil { - return contractVersion{}, fmt.Errorf("run test script deploying contract and calling its '%s' method: %w", methodVersion, err) - } - - return parseContractVersionFromInvocationResult(res) -} - type transactionGroupWaiter interface { WaitAny(ctx context.Context, vub uint32, hashes ...util.Uint256) (*state.AppExecResult, error) } diff --git a/pkg/morph/deploy/util_test.go b/pkg/morph/deploy/util_test.go deleted file mode 100644 index ce9ab09e54..0000000000 --- a/pkg/morph/deploy/util_test.go +++ /dev/null @@ -1,163 +0,0 @@ -package deploy - -import ( - "math" - "math/big" - "strconv" - "strings" - "testing" - - "github.com/nspcc-dev/neo-go/pkg/compiler" - "github.com/nspcc-dev/neo-go/pkg/core/transaction" - "github.com/nspcc-dev/neo-go/pkg/crypto/keys" - "github.com/nspcc-dev/neo-go/pkg/neorpc/result" - "github.com/nspcc-dev/neo-go/pkg/neotest" - "github.com/nspcc-dev/neo-go/pkg/neotest/chain" - "github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker" - "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/vm/stackitem" - "github.com/nspcc-dev/neo-go/pkg/vm/vmstate" - "github.com/stretchr/testify/require" -) - -func TestVersionCmp(t *testing.T) { - for _, tc := range []struct { - xmjr, xmnr, xpatch uint64 - ymjr, ymnr, ypatch uint64 - expected int - }{ - { - 1, 2, 3, - 1, 2, 3, - 0, - }, - { - 0, 1, 1, - 0, 2, 0, - -1, - }, - { - 1, 2, 2, - 1, 2, 3, - -1, - }, - { - 1, 2, 4, - 1, 2, 3, - 1, - }, - { - 0, 10, 0, - 1, 2, 3, - -1, - }, - { - 2, 0, 0, - 1, 2, 3, - 1, - }, - } { - x := contractVersion{tc.xmjr, tc.xmnr, tc.xpatch} - y := contractVersion{tc.ymjr, tc.ymnr, tc.ypatch} - require.Equal(t, tc.expected, x.cmp(y), tc) - } -} - -func TestParseContractVersionFromInvocationResult(t *testing.T) { - var err error - var res result.Invoke - - // non-HALT state - _, err = parseContractVersionFromInvocationResult(&res) - require.Error(t, err) - - res.State = vmstate.Halt.String() - - // empty stack - _, err = parseContractVersionFromInvocationResult(&res) - require.Error(t, err) - - // invalid item - res.Stack = []stackitem.Item{stackitem.Null{}} - - _, err = parseContractVersionFromInvocationResult(&res) - require.Error(t, err) - - // correct - ver := contractVersion{1, 2, 3} - i := new(big.Int) - - res.Stack = []stackitem.Item{stackitem.NewBigInteger(i.SetUint64(ver.toUint64()))} - - // overflow uint64 - i.SetUint64(math.MaxUint64).Add(i, big.NewInt(1)) - - _, err = parseContractVersionFromInvocationResult(&res) - require.Error(t, err) -} - -type testRPCInvoker struct { - invoker.RPCInvoke - tb testing.TB - exec *neotest.Executor -} - -func newTestRPCInvoker(tb testing.TB, exec *neotest.Executor) *testRPCInvoker { - return &testRPCInvoker{ - tb: tb, - exec: exec, - } -} - -func (x *testRPCInvoker) InvokeScript(script []byte, _ []transaction.Signer) (*result.Invoke, error) { - tx := transaction.New(script, 0) - tx.Nonce = neotest.Nonce() - tx.ValidUntilBlock = x.exec.Chain.BlockHeight() + 1 - tx.Signers = []transaction.Signer{{Account: x.exec.Committee.ScriptHash()}} - - b := x.exec.NewUnsignedBlock(x.tb, tx) - ic, err := x.exec.Chain.GetTestVM(trigger.Application, tx, b) - if err != nil { - return nil, err - } - x.tb.Cleanup(ic.Finalize) - - ic.VM.LoadWithFlags(tx.Script, callflag.All) - err = ic.VM.Run() - if err != nil { - return nil, err - } - - return &result.Invoke{ - State: vmstate.Halt.String(), - Stack: ic.VM.Estack().ToArray(), - }, nil -} - -func TestReadContractLocalVersion(t *testing.T) { - const version = 1_002_003 - - bc, acc := chain.NewSingle(t) - e := neotest.NewExecutor(t, bc, acc, acc) - - src := `package foo - const version = ` + strconv.Itoa(version) + ` - func Version() int { - return version - }` - - ctr := neotest.CompileSource(t, e.CommitteeHash, strings.NewReader(src), &compiler.Options{Name: "Helper"}) - - var committeeSigner neotest.SingleSigner - - if single, ok := acc.(neotest.SingleSigner); ok { - committeeSigner = single - } else { - committeeSigner = acc.(neotest.MultiSigner).Single(0) - } - - res, err := readContractLocalVersion(newTestRPCInvoker(t, e), keys.PublicKeys{committeeSigner.Account().PublicKey()}, *ctr.NEF, *ctr.Manifest) - require.NoError(t, err) - require.EqualValues(t, version, res.toUint64()) -}