From 5799397034095d5dbe2ae26d7bdc9e921f1121fc Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Fri, 9 Aug 2024 11:49:31 +0300 Subject: [PATCH 1/2] neotest: bind AddSystemFee and TestInvoke to Executor These methods need Executor's context to properly process coverage, thus these methods are not independent anymore. Ref. https://github.com/nspcc-dev/neo-go/pull/3462#pullrequestreview-2219332436. Signed-off-by: Anna Shaleva --- .../native_test/cryptolib_verification_test.go | 4 ++-- pkg/core/native/native_test/ledger_test.go | 2 +- pkg/neotest/basic.go | 14 +++++++------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/pkg/core/native/native_test/cryptolib_verification_test.go b/pkg/core/native/native_test/cryptolib_verification_test.go index 2143a1217..0e0dbd10d 100644 --- a/pkg/core/native/native_test/cryptolib_verification_test.go +++ b/pkg/core/native/native_test/cryptolib_verification_test.go @@ -67,7 +67,7 @@ func TestCryptoLib_KoblitzVerificationScript(t *testing.T) { }, } neotest.AddNetworkFee(t, e.Chain, tx) - neotest.AddSystemFee(e.Chain, tx, -1) + e.AddSystemFee(tx, -1) // Add some more network fee to pay for the witness verification. This value may be calculated precisely, // but let's keep some inaccurate value for the test. @@ -631,7 +631,7 @@ func TestCryptoLib_KoblitzMultisigVerificationScript(t *testing.T) { }, } neotest.AddNetworkFee(t, e.Chain, tx) - neotest.AddSystemFee(e.Chain, tx, -1) + e.AddSystemFee(tx, -1) // Add some more network fee to pay for the witness verification. This value may be calculated precisely, // but let's keep some inaccurate value for the test. diff --git a/pkg/core/native/native_test/ledger_test.go b/pkg/core/native/native_test/ledger_test.go index 308120b21..cd65d5796 100644 --- a/pkg/core/native/native_test/ledger_test.go +++ b/pkg/core/native/native_test/ledger_test.go @@ -227,7 +227,7 @@ func TestLedger_GetTransactionSignersInteropAPI(t *testing.T) { }, }} neotest.AddNetworkFee(t, e.Chain, tx, c.Committee) - neotest.AddSystemFee(e.Chain, tx, -1) + e.AddSystemFee(tx, -1) require.NoError(t, c.Committee.SignTx(e.Chain.GetConfig().Magic, tx)) c.AddNewBlock(t, tx) c.CheckHalt(t, tx.Hash(), stackitem.Make(e.Chain.BlockHeight()-1)) diff --git a/pkg/neotest/basic.go b/pkg/neotest/basic.go index c789d7ae6..ad0b1ceb2 100644 --- a/pkg/neotest/basic.go +++ b/pkg/neotest/basic.go @@ -107,7 +107,7 @@ func (e *Executor) SignTx(t testing.TB, tx *transaction.Transaction, sysFee int6 }) } AddNetworkFee(t, e.Chain, tx, signers...) - AddSystemFee(e.Chain, tx, sysFee) + e.AddSystemFee(tx, sysFee) for _, acc := range signers { require.NoError(t, acc.SignTx(e.Chain.GetConfig().Magic, tx)) @@ -287,12 +287,12 @@ func NewDeployTxBy(t testing.TB, bc *core.Blockchain, signer Signer, c *Contract // AddSystemFee adds system fee to the transaction. If negative value specified, // then system fee is defined by test invocation. -func AddSystemFee(bc *core.Blockchain, tx *transaction.Transaction, sysFee int64) { +func (e *Executor) AddSystemFee(tx *transaction.Transaction, sysFee int64) { if sysFee >= 0 { tx.SystemFee = sysFee return } - v, _ := TestInvoke(bc, tx) // ignore error to support failing transactions + v, _ := e.TestInvoke(tx) // ignore error to support failing transactions tx.SystemFee = v.GasConsumed() } @@ -384,14 +384,14 @@ func (e *Executor) AddBlockCheckHalt(t testing.TB, txs ...*transaction.Transacti } // TestInvoke creates a test VM with a dummy block and executes a transaction in it. -func TestInvoke(bc *core.Blockchain, tx *transaction.Transaction) (*vm.VM, error) { - lastBlock, err := bc.GetBlock(bc.GetHeaderHash(bc.BlockHeight())) +func (e *Executor) TestInvoke(tx *transaction.Transaction) (*vm.VM, error) { + lastBlock, err := e.Chain.GetBlock(e.Chain.GetHeaderHash(e.Chain.BlockHeight())) if err != nil { return nil, err } b := &block.Block{ Header: block.Header{ - Index: bc.BlockHeight() + 1, + Index: e.Chain.BlockHeight() + 1, Timestamp: lastBlock.Timestamp + 1, }, } @@ -399,7 +399,7 @@ func TestInvoke(bc *core.Blockchain, tx *transaction.Transaction) (*vm.VM, error // `GetTestVM` as well as `Run` can use a transaction hash which will set a cached value. // This is unwanted behavior, so we explicitly copy the transaction to perform execution. ttx := *tx - ic, _ := bc.GetTestVM(trigger.Application, &ttx, b) + ic, _ := e.Chain.GetTestVM(trigger.Application, &ttx, b) defer ic.Finalize() From 72d1427109125b1afe622a6aa3630688d403e1f2 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Fri, 9 Aug 2024 11:55:22 +0300 Subject: [PATCH 2/2] neotest: calculate system fee for deployment transactions 1. Bind NewDeployTxBy to Executor to be able to use (*Executor).AddSystemFee. 2. Replace pre-defined constant deployment fee by calculated one. This change is needed to be able to properly collect coverage for _deploy method of a contract via neotest coverage. Ref. https://github.com/nspcc-dev/neo-go/pull/3462#pullrequestreview-2229601870. Signed-off-by: Anna Shaleva --- pkg/core/native/management_neotest_test.go | 2 +- pkg/neotest/basic.go | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/pkg/core/native/management_neotest_test.go b/pkg/core/native/management_neotest_test.go index d8d8e0eac..f470f3a25 100644 --- a/pkg/core/native/management_neotest_test.go +++ b/pkg/core/native/management_neotest_test.go @@ -127,7 +127,7 @@ func TestManagement_CallInTheSameBlock(t *testing.T) { m.Name = "another contract" h := state.CreateContractHash(e.Validator.ScriptHash(), ne.Checksum, m.Name) - txDeploy := e.NewDeployTx(t, e.Chain, &neotest.Contract{Hash: h, NEF: ne, Manifest: m}, nil) + txDeploy := e.NewDeployTx(t, &neotest.Contract{Hash: h, NEF: ne, Manifest: m}, nil) txHasMethod := e.NewTx(t, []neotest.Signer{e.Validator}, bc.ManagementContractHash(), "hasMethod", h, "main", 0) txCall := e.NewUnsignedTx(t, h, "main") // Test invocation doesn't give true GAS cost before deployment. diff --git a/pkg/neotest/basic.go b/pkg/neotest/basic.go index ad0b1ceb2..6acc8dbc2 100644 --- a/pkg/neotest/basic.go +++ b/pkg/neotest/basic.go @@ -11,7 +11,6 @@ import ( "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/native/nativenames" "github.com/nspcc-dev/neo-go/pkg/core/state" "github.com/nspcc-dev/neo-go/pkg/core/transaction" @@ -146,7 +145,7 @@ func (e *Executor) DeployContract(t testing.TB, c *Contract, data any) util.Uint // data is an optional argument to `_deploy`. // It returns the hash of the deploy transaction. func (e *Executor) DeployContractBy(t testing.TB, signer Signer, c *Contract, data any) util.Uint256 { - tx := NewDeployTxBy(t, e.Chain, signer, c, data) + tx := e.NewDeployTxBy(t, signer, c, data) e.AddNewBlock(t, tx) e.CheckHalt(t, tx.Hash()) @@ -165,7 +164,7 @@ func (e *Executor) DeployContractBy(t testing.TB, signer Signer, c *Contract, da // DeployContractCheckFAULT compiles and deploys a contract to the bc using the validator // account. It checks that the deploy transaction FAULTed with the specified error. func (e *Executor) DeployContractCheckFAULT(t testing.TB, c *Contract, data any, errMessage string) { - tx := e.NewDeployTx(t, e.Chain, c, data) + tx := e.NewDeployTx(t, c, data) e.AddNewBlock(t, tx) e.CheckFault(t, tx.Hash(), errMessage) } @@ -258,29 +257,30 @@ func (e *Executor) EnsureGASBalance(t testing.TB, acc util.Uint160, isOk func(ba } // NewDeployTx returns a new deployment tx for the contract signed by the committee. -func (e *Executor) NewDeployTx(t testing.TB, bc *core.Blockchain, c *Contract, data any) *transaction.Transaction { - return NewDeployTxBy(t, bc, e.Validator, c, data) +func (e *Executor) NewDeployTx(t testing.TB, c *Contract, data any) *transaction.Transaction { + return e.NewDeployTxBy(t, e.Validator, c, data) } // NewDeployTxBy returns a new deployment tx for the contract signed by the specified signer. -func NewDeployTxBy(t testing.TB, bc *core.Blockchain, signer Signer, c *Contract, data any) *transaction.Transaction { +func (e *Executor) NewDeployTxBy(t testing.TB, signer Signer, c *Contract, data any) *transaction.Transaction { rawManifest, err := json.Marshal(c.Manifest) require.NoError(t, err) neb, err := c.NEF.Bytes() require.NoError(t, err) - script, err := smartcontract.CreateCallScript(bc.ManagementContractHash(), "deploy", neb, rawManifest, data) + script, err := smartcontract.CreateCallScript(e.Chain.ManagementContractHash(), "deploy", neb, rawManifest, data) require.NoError(t, err) - tx := transaction.New(script, 100*native.GASFactor) + tx := transaction.New(script, 0) tx.Nonce = Nonce() - tx.ValidUntilBlock = bc.BlockHeight() + 1 + tx.ValidUntilBlock = e.Chain.BlockHeight() + 1 tx.Signers = []transaction.Signer{{ Account: signer.ScriptHash(), Scopes: transaction.Global, }} - AddNetworkFee(t, bc, tx, signer) + AddNetworkFee(t, e.Chain, tx, signer) + e.AddSystemFee(tx, -1) require.NoError(t, signer.SignTx(netmode.UnitTestNet, tx)) return tx }