mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2025-05-05 23:55:11 +00:00
core: add tests for (*Blockchain).verifyTx
This commit is contained in:
parent
95d86b67c3
commit
e8cf4d96ce
2 changed files with 155 additions and 10 deletions
|
@ -1,7 +1,10 @@
|
|||
package core
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"math/rand"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
@ -11,11 +14,14 @@ import (
|
|||
"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/hash"
|
||||
"github.com/nspcc-dev/neo-go/pkg/internal/testchain"
|
||||
"github.com/nspcc-dev/neo-go/pkg/io"
|
||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||
"github.com/nspcc-dev/neo-go/pkg/vm"
|
||||
"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"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
@ -144,6 +150,129 @@ func TestGetBlock(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func (bc *Blockchain) newTestTx(h util.Uint160, script []byte) *transaction.Transaction {
|
||||
tx := transaction.New(testchain.Network(), script, 1_000_000)
|
||||
tx.Nonce = rand.Uint32()
|
||||
tx.ValidUntilBlock = 100
|
||||
tx.Signers = []transaction.Signer{{
|
||||
Account: h,
|
||||
Scopes: transaction.CalledByEntry,
|
||||
}}
|
||||
tx.NetworkFee = int64(io.GetVarSize(tx)+200 /* witness */) * bc.FeePerByte()
|
||||
tx.NetworkFee += 1_000_000 // verification cost
|
||||
return tx
|
||||
}
|
||||
|
||||
func TestVerifyTx(t *testing.T) {
|
||||
bc := newTestChain(t)
|
||||
defer bc.Close()
|
||||
|
||||
accs := make([]*wallet.Account, 2)
|
||||
for i := range accs {
|
||||
var err error
|
||||
accs[i], err = wallet.NewAccount()
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
neoHash := bc.contracts.NEO.Hash
|
||||
gasHash := bc.contracts.GAS.Hash
|
||||
w := io.NewBufBinWriter()
|
||||
for _, sc := range []util.Uint160{neoHash, gasHash} {
|
||||
for _, a := range accs {
|
||||
amount := int64(1_000_000)
|
||||
if sc.Equals(gasHash) {
|
||||
amount = 1_000_000_000
|
||||
}
|
||||
emit.AppCallWithOperationAndArgs(w.BinWriter, sc, "transfer",
|
||||
neoOwner, a.PrivateKey().GetScriptHash(), amount)
|
||||
emit.Opcode(w.BinWriter, opcode.ASSERT)
|
||||
}
|
||||
}
|
||||
require.NoError(t, w.Err)
|
||||
|
||||
txMove := bc.newTestTx(neoOwner, w.Bytes())
|
||||
txMove.SystemFee = 1_000_000_000
|
||||
require.NoError(t, signTx(bc, txMove))
|
||||
b := bc.newBlock(txMove)
|
||||
require.NoError(t, bc.AddBlock(b))
|
||||
|
||||
aer, err := bc.GetAppExecResult(txMove.Hash())
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, aer.VMState, vm.HaltState)
|
||||
|
||||
res, err := invokeNativePolicyMethod(bc, "blockAccount", accs[1].PrivateKey().GetScriptHash().BytesBE())
|
||||
require.NoError(t, err)
|
||||
checkResult(t, res, stackitem.NewBool(true))
|
||||
|
||||
checkErr := func(t *testing.T, expectedErr error, tx *transaction.Transaction) {
|
||||
err := bc.verifyTx(tx, nil)
|
||||
fmt.Println(err)
|
||||
require.True(t, errors.Is(err, expectedErr))
|
||||
}
|
||||
|
||||
testScript := []byte{byte(opcode.PUSH1)}
|
||||
h := accs[0].PrivateKey().GetScriptHash()
|
||||
t.Run("Expired", func(t *testing.T) {
|
||||
tx := bc.newTestTx(h, testScript)
|
||||
tx.ValidUntilBlock = 1
|
||||
require.NoError(t, accs[0].SignTx(tx))
|
||||
checkErr(t, ErrTxExpired, tx)
|
||||
})
|
||||
t.Run("BlockedAccount", func(t *testing.T) {
|
||||
tx := bc.newTestTx(accs[1].PrivateKey().GetScriptHash(), testScript)
|
||||
require.NoError(t, accs[1].SignTx(tx))
|
||||
err := bc.verifyTx(tx, nil)
|
||||
require.True(t, errors.Is(err, ErrPolicy))
|
||||
})
|
||||
t.Run("InsufficientGas", func(t *testing.T) {
|
||||
balance := bc.GetUtilityTokenBalance(h)
|
||||
tx := bc.newTestTx(h, testScript)
|
||||
tx.SystemFee = balance.Int64() + 1
|
||||
require.NoError(t, accs[0].SignTx(tx))
|
||||
checkErr(t, ErrInsufficientFunds, tx)
|
||||
})
|
||||
t.Run("TooBigTx", func(t *testing.T) {
|
||||
script := make([]byte, transaction.MaxTransactionSize)
|
||||
tx := bc.newTestTx(h, script)
|
||||
require.NoError(t, accs[0].SignTx(tx))
|
||||
checkErr(t, ErrTxTooBig, tx)
|
||||
})
|
||||
t.Run("SmallNetworkFee", func(t *testing.T) {
|
||||
tx := bc.newTestTx(h, testScript)
|
||||
tx.NetworkFee = 1
|
||||
require.NoError(t, accs[0].SignTx(tx))
|
||||
checkErr(t, ErrTxSmallNetworkFee, tx)
|
||||
})
|
||||
t.Run("Conflict", func(t *testing.T) {
|
||||
balance := bc.GetUtilityTokenBalance(h).Int64()
|
||||
tx := bc.newTestTx(h, testScript)
|
||||
tx.NetworkFee = balance / 2
|
||||
require.NoError(t, accs[0].SignTx(tx))
|
||||
checkErr(t, nil, tx)
|
||||
|
||||
tx2 := bc.newTestTx(h, testScript)
|
||||
tx2.NetworkFee = balance / 2
|
||||
require.NoError(t, bc.memPool.Add(tx2, bc))
|
||||
checkErr(t, ErrMemPoolConflict, tx)
|
||||
})
|
||||
t.Run("NotEnoughWitnesses", func(t *testing.T) {
|
||||
tx := bc.newTestTx(h, testScript)
|
||||
checkErr(t, ErrTxInvalidWitnessNum, tx)
|
||||
})
|
||||
t.Run("InvalidWitnessHash", func(t *testing.T) {
|
||||
tx := bc.newTestTx(h, testScript)
|
||||
require.NoError(t, accs[0].SignTx(tx))
|
||||
tx.Scripts[0].VerificationScript = []byte{byte(opcode.PUSHT)}
|
||||
checkErr(t, ErrWitnessHashMismatch, tx)
|
||||
})
|
||||
t.Run("InvalidWitnessSignature", func(t *testing.T) {
|
||||
tx := bc.newTestTx(h, testScript)
|
||||
require.NoError(t, accs[0].SignTx(tx))
|
||||
tx.Scripts[0].InvocationScript[10] = ^tx.Scripts[0].InvocationScript[10]
|
||||
checkErr(t, ErrVerificationFailed, tx)
|
||||
})
|
||||
}
|
||||
|
||||
func TestHasBlock(t *testing.T) {
|
||||
bc := newTestChain(t)
|
||||
blocks, err := bc.genBlocks(50)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue