2021-10-23 11:28:03 +00:00
|
|
|
package neotest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-11-16 14:40:01 +00:00
|
|
|
"fmt"
|
2021-12-03 15:45:55 +00:00
|
|
|
"math/big"
|
2021-10-23 11:28:03 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2022-07-22 18:58:05 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core"
|
2021-10-23 11:28:03 +00:00
|
|
|
"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"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2022-07-25 19:07:13 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2021-10-23 11:28:03 +00:00
|
|
|
"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"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2022-07-08 14:28:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/vmstate"
|
2021-10-23 11:28:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Executor is a wrapper over chain state.
|
|
|
|
type Executor struct {
|
2022-07-22 18:58:05 +00:00
|
|
|
Chain *core.Blockchain
|
2021-11-03 12:22:08 +00:00
|
|
|
Validator Signer
|
2021-11-03 10:44:46 +00:00
|
|
|
Committee Signer
|
2021-10-23 11:28:03 +00:00
|
|
|
CommitteeHash util.Uint160
|
|
|
|
Contracts map[string]*Contract
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewExecutor creates a new executor instance from the provided blockchain and committee.
|
2022-07-22 18:58:05 +00:00
|
|
|
func NewExecutor(t testing.TB, bc *core.Blockchain, validator, committee Signer) *Executor {
|
2021-11-03 12:28:29 +00:00
|
|
|
checkMultiSigner(t, validator)
|
|
|
|
checkMultiSigner(t, committee)
|
2021-10-23 11:28:03 +00:00
|
|
|
|
|
|
|
return &Executor{
|
|
|
|
Chain: bc,
|
2021-11-03 12:22:08 +00:00
|
|
|
Validator: validator,
|
2021-10-23 11:28:03 +00:00
|
|
|
Committee: committee,
|
2021-11-03 10:44:46 +00:00
|
|
|
CommitteeHash: committee.ScriptHash(),
|
2021-10-23 11:28:03 +00:00
|
|
|
Contracts: make(map[string]*Contract),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TopBlock returns the block with the highest index.
|
2022-03-18 15:11:24 +00:00
|
|
|
func (e *Executor) TopBlock(t testing.TB) *block.Block {
|
2022-11-18 20:19:50 +00:00
|
|
|
b, err := e.Chain.GetBlock(e.Chain.GetHeaderHash(e.Chain.BlockHeight()))
|
2021-10-23 11:28:03 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NativeHash returns a native contract hash by the name.
|
2022-03-18 15:11:24 +00:00
|
|
|
func (e *Executor) NativeHash(t testing.TB, name string) util.Uint160 {
|
2021-10-23 11:28:03 +00:00
|
|
|
h, err := e.Chain.GetNativeContractScriptHash(name)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// ContractHash returns a contract hash by the ID.
|
2022-03-18 15:11:24 +00:00
|
|
|
func (e *Executor) ContractHash(t testing.TB, id int32) util.Uint160 {
|
2022-03-10 14:12:04 +00:00
|
|
|
h, err := e.Chain.GetContractScriptHash(id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NativeID returns a native contract ID by the name.
|
2022-03-18 15:11:24 +00:00
|
|
|
func (e *Executor) NativeID(t testing.TB, name string) int32 {
|
2021-12-03 15:45:55 +00:00
|
|
|
h := e.NativeHash(t, name)
|
|
|
|
cs := e.Chain.GetContractState(h)
|
|
|
|
require.NotNil(t, cs)
|
|
|
|
return cs.ID
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewUnsignedTx creates a new unsigned transaction which invokes the method of the contract with the hash.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (e *Executor) NewUnsignedTx(t testing.TB, hash util.Uint160, method string, args ...any) *transaction.Transaction {
|
2022-07-25 19:07:13 +00:00
|
|
|
script, err := smartcontract.CreateCallScript(hash, method, args...)
|
|
|
|
require.NoError(t, err)
|
2021-10-23 11:28:03 +00:00
|
|
|
|
|
|
|
tx := transaction.New(script, 0)
|
2021-11-11 17:00:40 +00:00
|
|
|
tx.Nonce = Nonce()
|
2021-10-23 11:28:03 +00:00
|
|
|
tx.ValidUntilBlock = e.Chain.BlockHeight() + 1
|
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewTx creates a new transaction which invokes the contract method.
|
|
|
|
// The transaction is signed by the signers.
|
2022-03-18 15:11:24 +00:00
|
|
|
func (e *Executor) NewTx(t testing.TB, signers []Signer,
|
2023-04-03 10:34:24 +00:00
|
|
|
hash util.Uint160, method string, args ...any) *transaction.Transaction {
|
2021-10-23 11:28:03 +00:00
|
|
|
tx := e.NewUnsignedTx(t, hash, method, args...)
|
2021-11-03 10:44:46 +00:00
|
|
|
return e.SignTx(t, tx, -1, signers...)
|
2021-10-23 11:28:03 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// SignTx signs a transaction using the provided signers.
|
2022-03-18 15:11:24 +00:00
|
|
|
func (e *Executor) SignTx(t testing.TB, tx *transaction.Transaction, sysFee int64, signers ...Signer) *transaction.Transaction {
|
2021-11-03 10:44:46 +00:00
|
|
|
for _, acc := range signers {
|
2021-10-23 11:28:03 +00:00
|
|
|
tx.Signers = append(tx.Signers, transaction.Signer{
|
2021-11-03 10:44:46 +00:00
|
|
|
Account: acc.ScriptHash(),
|
2021-10-23 11:28:03 +00:00
|
|
|
Scopes: transaction.Global,
|
|
|
|
})
|
|
|
|
}
|
2022-04-27 08:58:46 +00:00
|
|
|
AddNetworkFee(e.Chain, tx, signers...)
|
|
|
|
AddSystemFee(e.Chain, tx, sysFee)
|
2021-10-23 11:28:03 +00:00
|
|
|
|
2021-11-03 10:44:46 +00:00
|
|
|
for _, acc := range signers {
|
|
|
|
require.NoError(t, acc.SignTx(e.Chain.GetConfig().Magic, tx))
|
|
|
|
}
|
2021-10-23 11:28:03 +00:00
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewAccount returns a new signer holding 100.0 GAS (or given amount is specified).
|
2021-12-09 14:58:20 +00:00
|
|
|
// This method advances the chain by one block with a transfer transaction.
|
2022-03-18 15:11:24 +00:00
|
|
|
func (e *Executor) NewAccount(t testing.TB, expectedGASBalance ...int64) Signer {
|
2021-10-23 11:28:03 +00:00
|
|
|
acc, err := wallet.NewAccount()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-12-09 14:58:20 +00:00
|
|
|
amount := int64(100_0000_0000)
|
|
|
|
if len(expectedGASBalance) != 0 {
|
|
|
|
amount = expectedGASBalance[0]
|
|
|
|
}
|
|
|
|
tx := e.NewTx(t, []Signer{e.Validator},
|
2021-10-23 11:28:03 +00:00
|
|
|
e.NativeHash(t, nativenames.Gas), "transfer",
|
2021-12-09 14:58:20 +00:00
|
|
|
e.Validator.ScriptHash(), acc.Contract.ScriptHash(), amount, nil)
|
2021-10-23 11:28:03 +00:00
|
|
|
e.AddNewBlock(t, tx)
|
|
|
|
e.CheckHalt(t, tx.Hash())
|
2021-11-03 10:44:46 +00:00
|
|
|
return NewSingleSigner(acc)
|
2021-10-23 11:28:03 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// DeployContract compiles and deploys a contract to the bc. It also checks that
|
|
|
|
// the precalculated contract hash matches the actual one.
|
2021-10-23 11:28:03 +00:00
|
|
|
// data is an optional argument to `_deploy`.
|
2022-04-20 18:30:09 +00:00
|
|
|
// It returns the hash of the deploy transaction.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (e *Executor) DeployContract(t testing.TB, c *Contract, data any) util.Uint256 {
|
2022-03-10 14:12:04 +00:00
|
|
|
return e.DeployContractBy(t, e.Validator, c, data)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// DeployContractBy compiles and deploys a contract to the bc using the provided signer.
|
|
|
|
// It also checks that the precalculated contract hash matches the actual one.
|
2022-03-10 14:12:04 +00:00
|
|
|
// data is an optional argument to `_deploy`.
|
2022-04-20 18:30:09 +00:00
|
|
|
// It returns the hash of the deploy transaction.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (e *Executor) DeployContractBy(t testing.TB, signer Signer, c *Contract, data any) util.Uint256 {
|
2022-03-10 14:12:04 +00:00
|
|
|
tx := NewDeployTxBy(t, e.Chain, signer, c, data)
|
2021-10-23 11:28:03 +00:00
|
|
|
e.AddNewBlock(t, tx)
|
|
|
|
e.CheckHalt(t, tx.Hash())
|
2021-11-16 14:35:39 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Check that the precalculated hash matches the real one.
|
2021-11-16 14:35:39 +00:00
|
|
|
e.CheckTxNotificationEvent(t, tx.Hash(), -1, state.NotificationEvent{
|
|
|
|
ScriptHash: e.NativeHash(t, nativenames.Management),
|
|
|
|
Name: "Deploy",
|
|
|
|
Item: stackitem.NewArray([]stackitem.Item{
|
|
|
|
stackitem.NewByteArray(c.Hash.BytesBE()),
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
|
2021-10-23 11:28:03 +00:00
|
|
|
return tx.Hash()
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// DeployContractCheckFAULT compiles and deploys a contract to the bc using the validator
|
|
|
|
// account. It checks that the deploy transaction FAULTed with the specified error.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (e *Executor) DeployContractCheckFAULT(t testing.TB, c *Contract, data any, errMessage string) {
|
2021-11-18 09:23:22 +00:00
|
|
|
tx := e.NewDeployTx(t, e.Chain, c, data)
|
|
|
|
e.AddNewBlock(t, tx)
|
|
|
|
e.CheckFault(t, tx.Hash(), errMessage)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// InvokeScript adds a transaction with the specified script to the chain and
|
2021-11-18 09:14:53 +00:00
|
|
|
// returns its hash. It does no faults check.
|
2022-03-18 15:11:24 +00:00
|
|
|
func (e *Executor) InvokeScript(t testing.TB, script []byte, signers []Signer) util.Uint256 {
|
2022-03-11 14:47:59 +00:00
|
|
|
tx := e.PrepareInvocation(t, script, signers)
|
|
|
|
e.AddNewBlock(t, tx)
|
|
|
|
return tx.Hash()
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// PrepareInvocation creates a transaction with the specified script and signs it
|
2022-03-11 14:47:59 +00:00
|
|
|
// by the provided signer.
|
|
|
|
func (e *Executor) PrepareInvocation(t testing.TB, script []byte, signers []Signer, validUntilBlock ...uint32) *transaction.Transaction {
|
2021-11-18 09:14:53 +00:00
|
|
|
tx := transaction.New(script, 0)
|
|
|
|
tx.Nonce = Nonce()
|
|
|
|
tx.ValidUntilBlock = e.Chain.BlockHeight() + 1
|
2022-03-11 14:47:59 +00:00
|
|
|
if len(validUntilBlock) != 0 {
|
|
|
|
tx.ValidUntilBlock = validUntilBlock[0]
|
|
|
|
}
|
2021-11-18 09:14:53 +00:00
|
|
|
e.SignTx(t, tx, -1, signers...)
|
2022-03-11 14:47:59 +00:00
|
|
|
return tx
|
2021-11-18 09:14:53 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// InvokeScriptCheckHALT adds a transaction with the specified script to the chain
|
|
|
|
// and checks if it's HALTed with the specified items on stack.
|
2022-03-18 15:11:24 +00:00
|
|
|
func (e *Executor) InvokeScriptCheckHALT(t testing.TB, script []byte, signers []Signer, stack ...stackitem.Item) {
|
2021-11-18 09:14:53 +00:00
|
|
|
hash := e.InvokeScript(t, script, signers)
|
|
|
|
e.CheckHalt(t, hash, stack...)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// InvokeScriptCheckFAULT adds a transaction with the specified script to the
|
|
|
|
// chain and checks if it's FAULTed with the specified error.
|
2022-11-16 09:35:26 +00:00
|
|
|
func (e *Executor) InvokeScriptCheckFAULT(t testing.TB, script []byte, signers []Signer, errMessage string) util.Uint256 {
|
2021-11-18 09:14:53 +00:00
|
|
|
hash := e.InvokeScript(t, script, signers)
|
|
|
|
e.CheckFault(t, hash, errMessage)
|
2022-11-16 09:35:26 +00:00
|
|
|
return hash
|
2021-11-18 09:14:53 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// CheckHalt checks that the transaction is persisted with HALT state.
|
2022-03-18 15:11:24 +00:00
|
|
|
func (e *Executor) CheckHalt(t testing.TB, h util.Uint256, stack ...stackitem.Item) *state.AppExecResult {
|
2021-10-23 11:28:03 +00:00
|
|
|
aer, err := e.Chain.GetAppExecResults(h, trigger.Application)
|
|
|
|
require.NoError(t, err)
|
2022-07-08 14:28:29 +00:00
|
|
|
require.Equal(t, vmstate.Halt, aer[0].VMState, aer[0].FaultException)
|
2021-10-23 11:28:03 +00:00
|
|
|
if len(stack) != 0 {
|
|
|
|
require.Equal(t, stack, aer[0].Stack)
|
|
|
|
}
|
|
|
|
return &aer[0]
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// CheckFault checks that the transaction is persisted with FAULT state.
|
|
|
|
// The raised exception is also checked to contain the s as a substring.
|
2022-03-18 15:11:24 +00:00
|
|
|
func (e *Executor) CheckFault(t testing.TB, h util.Uint256, s string) {
|
2021-10-23 11:28:03 +00:00
|
|
|
aer, err := e.Chain.GetAppExecResults(h, trigger.Application)
|
|
|
|
require.NoError(t, err)
|
2022-07-08 14:28:29 +00:00
|
|
|
require.Equal(t, vmstate.Fault, aer[0].VMState)
|
2021-10-23 11:28:03 +00:00
|
|
|
require.True(t, strings.Contains(aer[0].FaultException, s),
|
|
|
|
"expected: %s, got: %s", s, aer[0].FaultException)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// CheckTxNotificationEvent checks that the specified event was emitted at the specified position
|
2021-11-16 14:40:01 +00:00
|
|
|
// during transaction script execution. Negative index corresponds to backwards enumeration.
|
2022-03-18 15:11:24 +00:00
|
|
|
func (e *Executor) CheckTxNotificationEvent(t testing.TB, h util.Uint256, index int, expected state.NotificationEvent) {
|
2021-11-16 14:40:01 +00:00
|
|
|
aer, err := e.Chain.GetAppExecResults(h, trigger.Application)
|
|
|
|
require.NoError(t, err)
|
|
|
|
l := len(aer[0].Events)
|
|
|
|
if index < 0 {
|
|
|
|
index = l + index
|
|
|
|
}
|
|
|
|
require.True(t, 0 <= index && index < l, fmt.Errorf("notification index is out of range: want %d, len is %d", index, l))
|
|
|
|
require.Equal(t, expected, aer[0].Events[index])
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// CheckGASBalance ensures that the provided account owns the specified amount of GAS.
|
2022-03-18 15:11:24 +00:00
|
|
|
func (e *Executor) CheckGASBalance(t testing.TB, acc util.Uint160, expected *big.Int) {
|
2021-12-03 15:45:55 +00:00
|
|
|
actual := e.Chain.GetUtilityTokenBalance(acc)
|
|
|
|
require.Equal(t, expected, actual, fmt.Errorf("invalid GAS balance: expected %s, got %s", expected.String(), actual.String()))
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// EnsureGASBalance ensures that the provided account owns the amount of GAS that satisfies the provided condition.
|
2022-03-18 15:11:24 +00:00
|
|
|
func (e *Executor) EnsureGASBalance(t testing.TB, acc util.Uint160, isOk func(balance *big.Int) bool) {
|
2022-03-10 14:12:04 +00:00
|
|
|
actual := e.Chain.GetUtilityTokenBalance(acc)
|
|
|
|
require.True(t, isOk(actual), fmt.Errorf("invalid GAS balance: got %s, condition is not satisfied", actual.String()))
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewDeployTx returns a new deployment tx for the contract signed by the committee.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (e *Executor) NewDeployTx(t testing.TB, bc *core.Blockchain, c *Contract, data any) *transaction.Transaction {
|
2022-03-10 14:12:04 +00:00
|
|
|
return NewDeployTxBy(t, bc, e.Validator, c, data)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewDeployTxBy returns a new deployment tx for the contract signed by the specified signer.
|
2023-04-03 10:34:24 +00:00
|
|
|
func NewDeployTxBy(t testing.TB, bc *core.Blockchain, signer Signer, c *Contract, data any) *transaction.Transaction {
|
2021-10-23 11:28:03 +00:00
|
|
|
rawManifest, err := json.Marshal(c.Manifest)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
neb, err := c.NEF.Bytes()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-07-25 19:07:13 +00:00
|
|
|
script, err := smartcontract.CreateCallScript(bc.ManagementContractHash(), "deploy", neb, rawManifest, data)
|
|
|
|
require.NoError(t, err)
|
2021-10-23 11:28:03 +00:00
|
|
|
|
2022-07-25 19:07:13 +00:00
|
|
|
tx := transaction.New(script, 100*native.GASFactor)
|
2021-11-11 17:00:40 +00:00
|
|
|
tx.Nonce = Nonce()
|
2021-10-23 11:28:03 +00:00
|
|
|
tx.ValidUntilBlock = bc.BlockHeight() + 1
|
|
|
|
tx.Signers = []transaction.Signer{{
|
2022-03-10 14:12:04 +00:00
|
|
|
Account: signer.ScriptHash(),
|
2021-10-23 11:28:03 +00:00
|
|
|
Scopes: transaction.Global,
|
|
|
|
}}
|
2022-04-27 08:58:46 +00:00
|
|
|
AddNetworkFee(bc, tx, signer)
|
2022-03-10 14:12:04 +00:00
|
|
|
require.NoError(t, signer.SignTx(netmode.UnitTestNet, tx))
|
2021-10-23 11:28:03 +00:00
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
2022-04-27 08:58:46 +00:00
|
|
|
// AddSystemFee adds system fee to the transaction. If negative value specified,
|
|
|
|
// then system fee is defined by test invocation.
|
2022-07-22 18:58:05 +00:00
|
|
|
func AddSystemFee(bc *core.Blockchain, tx *transaction.Transaction, sysFee int64) {
|
2021-10-23 11:28:03 +00:00
|
|
|
if sysFee >= 0 {
|
|
|
|
tx.SystemFee = sysFee
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v, _ := TestInvoke(bc, tx) // ignore error to support failing transactions
|
|
|
|
tx.SystemFee = v.GasConsumed()
|
|
|
|
}
|
|
|
|
|
2022-04-27 08:58:46 +00:00
|
|
|
// AddNetworkFee adds network fee to the transaction.
|
2022-07-22 18:58:05 +00:00
|
|
|
func AddNetworkFee(bc *core.Blockchain, tx *transaction.Transaction, signers ...Signer) {
|
2022-01-11 21:58:03 +00:00
|
|
|
baseFee := bc.GetBaseExecFee()
|
2021-10-23 11:28:03 +00:00
|
|
|
size := io.GetVarSize(tx)
|
2021-11-03 10:44:46 +00:00
|
|
|
for _, sgr := range signers {
|
|
|
|
netFee, sizeDelta := fee.Calculate(baseFee, sgr.Script())
|
|
|
|
tx.NetworkFee += netFee
|
|
|
|
size += sizeDelta
|
|
|
|
}
|
2021-10-23 11:28:03 +00:00
|
|
|
tx.NetworkFee += int64(size) * bc.FeePerByte()
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewUnsignedBlock creates a new unsigned block from txs.
|
2022-03-18 15:11:24 +00:00
|
|
|
func (e *Executor) NewUnsignedBlock(t testing.TB, txs ...*transaction.Transaction) *block.Block {
|
2021-10-23 11:28:03 +00:00
|
|
|
lastBlock := e.TopBlock(t)
|
|
|
|
b := &block.Block{
|
|
|
|
Header: block.Header{
|
2021-11-03 12:22:08 +00:00
|
|
|
NextConsensus: e.Validator.ScriptHash(),
|
2021-10-23 11:28:03 +00:00
|
|
|
Script: transaction.Witness{
|
2021-11-03 12:22:08 +00:00
|
|
|
VerificationScript: e.Validator.Script(),
|
2021-10-23 11:28:03 +00:00
|
|
|
},
|
|
|
|
Timestamp: lastBlock.Timestamp + 1,
|
|
|
|
},
|
|
|
|
Transactions: txs,
|
|
|
|
}
|
|
|
|
if e.Chain.GetConfig().StateRootInHeader {
|
|
|
|
b.StateRootEnabled = true
|
|
|
|
b.PrevStateRoot = e.Chain.GetStateModule().CurrentLocalStateRoot()
|
|
|
|
}
|
|
|
|
b.PrevHash = lastBlock.Hash()
|
|
|
|
b.Index = e.Chain.BlockHeight() + 1
|
|
|
|
b.RebuildMerkleRoot()
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// AddNewBlock creates a new block from the provided transactions and adds it on the bc.
|
2022-03-18 15:11:24 +00:00
|
|
|
func (e *Executor) AddNewBlock(t testing.TB, txs ...*transaction.Transaction) *block.Block {
|
2021-10-23 11:28:03 +00:00
|
|
|
b := e.NewUnsignedBlock(t, txs...)
|
|
|
|
e.SignBlock(b)
|
|
|
|
require.NoError(t, e.Chain.AddBlock(b))
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GenerateNewBlocks adds the specified number of empty blocks to the chain.
|
2022-03-11 14:47:59 +00:00
|
|
|
func (e *Executor) GenerateNewBlocks(t testing.TB, count int) []*block.Block {
|
|
|
|
blocks := make([]*block.Block, count)
|
2021-12-03 15:45:55 +00:00
|
|
|
for i := 0; i < count; i++ {
|
2022-03-11 14:47:59 +00:00
|
|
|
blocks[i] = e.AddNewBlock(t)
|
2021-12-03 15:45:55 +00:00
|
|
|
}
|
2022-03-11 14:47:59 +00:00
|
|
|
return blocks
|
2021-12-03 15:45:55 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 11:28:03 +00:00
|
|
|
// SignBlock add validators signature to b.
|
|
|
|
func (e *Executor) SignBlock(b *block.Block) *block.Block {
|
2021-11-03 12:22:08 +00:00
|
|
|
invoc := e.Validator.SignHashable(uint32(e.Chain.GetConfig().Magic), b)
|
2021-11-03 10:44:46 +00:00
|
|
|
b.Script.InvocationScript = invoc
|
2021-10-23 11:28:03 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2021-10-23 12:36:26 +00:00
|
|
|
// AddBlockCheckHalt is a convenient wrapper over AddBlock and CheckHalt.
|
2022-03-18 15:11:24 +00:00
|
|
|
func (e *Executor) AddBlockCheckHalt(t testing.TB, txs ...*transaction.Transaction) *block.Block {
|
2021-10-23 11:28:03 +00:00
|
|
|
b := e.AddNewBlock(t, txs...)
|
|
|
|
for _, tx := range txs {
|
|
|
|
e.CheckHalt(t, tx.Hash())
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TestInvoke creates a test VM with a dummy block and executes a transaction in it.
|
2022-07-22 18:58:05 +00:00
|
|
|
func TestInvoke(bc *core.Blockchain, tx *transaction.Transaction) (*vm.VM, error) {
|
2022-11-18 20:19:50 +00:00
|
|
|
lastBlock, err := bc.GetBlock(bc.GetHeaderHash(bc.BlockHeight()))
|
2021-10-23 11:28:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b := &block.Block{
|
|
|
|
Header: block.Header{
|
|
|
|
Index: bc.BlockHeight() + 1,
|
|
|
|
Timestamp: lastBlock.Timestamp + 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// `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.
|
2021-10-23 11:28:03 +00:00
|
|
|
ttx := *tx
|
2022-10-06 10:24:57 +00:00
|
|
|
ic, _ := bc.GetTestVM(trigger.Application, &ttx, b)
|
|
|
|
|
2022-01-12 22:20:08 +00:00
|
|
|
defer ic.Finalize()
|
2021-10-23 11:28:03 +00:00
|
|
|
|
2022-01-12 22:20:08 +00:00
|
|
|
ic.VM.LoadWithFlags(tx.Script, callflag.All)
|
|
|
|
err = ic.VM.Run()
|
|
|
|
return ic.VM, err
|
2021-10-23 11:28:03 +00:00
|
|
|
}
|
2021-12-03 15:45:55 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetTransaction returns a transaction and its height by the specified hash.
|
2022-03-18 15:11:24 +00:00
|
|
|
func (e *Executor) GetTransaction(t testing.TB, h util.Uint256) (*transaction.Transaction, uint32) {
|
2021-12-03 15:45:55 +00:00
|
|
|
tx, height, err := e.Chain.GetTransaction(h)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return tx, height
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetBlockByIndex returns a block by the specified index.
|
2022-11-18 20:19:50 +00:00
|
|
|
func (e *Executor) GetBlockByIndex(t testing.TB, idx uint32) *block.Block {
|
2021-12-03 15:45:55 +00:00
|
|
|
h := e.Chain.GetHeaderHash(idx)
|
|
|
|
require.NotEmpty(t, h)
|
|
|
|
b, err := e.Chain.GetBlock(h)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTxExecResult returns application execution results for the specified transaction.
|
2022-03-18 15:11:24 +00:00
|
|
|
func (e *Executor) GetTxExecResult(t testing.TB, h util.Uint256) *state.AppExecResult {
|
2021-12-03 15:45:55 +00:00
|
|
|
aer, err := e.Chain.GetAppExecResults(h, trigger.Application)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(aer))
|
|
|
|
return &aer[0]
|
|
|
|
}
|