From b5d5963b38796372cdcb1f43a15acc70ee29e063 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 6 Oct 2021 14:29:36 +0300 Subject: [PATCH] [#135] tests: cache compiled contracts Speedup tests a bit. Signed-off-by: Evgenii Stratonikov --- tests/basic.go | 37 +++----------------------- tests/compile.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 33 deletions(-) create mode 100644 tests/compile.go diff --git a/tests/basic.go b/tests/basic.go index 674017a..851ac15 100644 --- a/tests/basic.go +++ b/tests/basic.go @@ -3,26 +3,20 @@ package tests import ( "encoding/hex" "encoding/json" - "path" "strings" "testing" - "github.com/nspcc-dev/neo-go/cli/smartcontract" - "github.com/nspcc-dev/neo-go/pkg/compiler" "github.com/nspcc-dev/neo-go/pkg/config" "github.com/nspcc-dev/neo-go/pkg/config/netmode" "github.com/nspcc-dev/neo-go/pkg/core" "github.com/nspcc-dev/neo-go/pkg/core/block" "github.com/nspcc-dev/neo-go/pkg/core/fee" "github.com/nspcc-dev/neo-go/pkg/core/native" - "github.com/nspcc-dev/neo-go/pkg/core/state" "github.com/nspcc-dev/neo-go/pkg/core/storage" "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/io" "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/smartcontract/trigger" "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/vm" @@ -164,34 +158,13 @@ func CheckFault(t *testing.T, bc *core.Blockchain, h util.Uint256, s string) { // newDeployTx returns new deployment tx for contract. func newDeployTx(t *testing.T, bc *core.Blockchain, ctrPath string, data interface{}) (*transaction.Transaction, util.Uint160) { - // nef.NewFile() cares about version a lot. - config.Version = "0.90.0-test" - - avm, di, err := compiler.CompileWithDebugInfo(ctrPath, nil) + c, err := ContractInfo(CommitteeAcc.Contract.ScriptHash(), ctrPath) require.NoError(t, err) - ne, err := nef.NewFile(avm) + rawManifest, err := json.Marshal(c.Manifest) require.NoError(t, err) - conf, err := smartcontract.ParseContractConfig(path.Join(ctrPath, "config.yml")) - require.NoError(t, err) - - o := &compiler.Options{} - o.Name = conf.Name - o.ContractEvents = conf.Events - o.ContractSupportedStandards = conf.SupportedStandards - o.Permissions = make([]manifest.Permission, len(conf.Permissions)) - for i := range conf.Permissions { - o.Permissions[i] = manifest.Permission(conf.Permissions[i]) - } - o.SafeMethods = conf.SafeMethods - m, err := compiler.CreateManifest(di, o) - require.NoError(t, err) - - rawManifest, err := json.Marshal(m) - require.NoError(t, err) - - neb, err := ne.Bytes() + neb, err := c.NEF.Bytes() require.NoError(t, err) buf := io.NewBufBinWriter() @@ -204,9 +177,7 @@ func newDeployTx(t *testing.T, bc *core.Blockchain, ctrPath string, data interfa tx.Signers = []transaction.Signer{{Account: CommitteeAcc.Contract.ScriptHash()}} require.NoError(t, addNetworkFee(bc, tx, CommitteeAcc)) require.NoError(t, CommitteeAcc.SignTx(netmode.UnitTestNet, tx)) - - h := state.CreateContractHash(tx.Sender(), ne.Checksum, m.Name) - return tx, h + return tx, c.Hash } func addSystemFee(bc *core.Blockchain, tx *transaction.Transaction) error { diff --git a/tests/compile.go b/tests/compile.go new file mode 100644 index 0000000..8f4aa8a --- /dev/null +++ b/tests/compile.go @@ -0,0 +1,69 @@ +package tests + +import ( + "path" + + "github.com/nspcc-dev/neo-go/cli/smartcontract" + "github.com/nspcc-dev/neo-go/pkg/compiler" + "github.com/nspcc-dev/neo-go/pkg/config" + "github.com/nspcc-dev/neo-go/pkg/core/state" + "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" +) + +// Contract contains contract info for deployment. +type Contract struct { + Hash util.Uint160 + NEF *nef.File + Manifest *manifest.Manifest +} + +var contracts = map[string]*Contract{} + +// ContractInfo compiles contract and returns it's NEF, manifest and hash. +func ContractInfo(sender util.Uint160, ctrPath string) (*Contract, error) { + if c, ok := contracts[ctrPath]; ok { + return c, nil + } + + // nef.NewFile() cares about version a lot. + config.Version = "0.90.0-test" + + avm, di, err := compiler.CompileWithDebugInfo(ctrPath, nil) + if err != nil { + return nil, err + } + + ne, err := nef.NewFile(avm) + if err != nil { + return nil, err + } + + conf, err := smartcontract.ParseContractConfig(path.Join(ctrPath, "config.yml")) + if err != nil { + return nil, err + } + + o := &compiler.Options{} + o.Name = conf.Name + o.ContractEvents = conf.Events + o.ContractSupportedStandards = conf.SupportedStandards + o.Permissions = make([]manifest.Permission, len(conf.Permissions)) + for i := range conf.Permissions { + o.Permissions[i] = manifest.Permission(conf.Permissions[i]) + } + o.SafeMethods = conf.SafeMethods + m, err := compiler.CreateManifest(di, o) + if err != nil { + return nil, err + } + + c := &Contract{ + Hash: state.CreateContractHash(sender, ne.Checksum, m.Name), + NEF: ne, + Manifest: m, + } + contracts[ctrPath] = c + return c, nil +}