neoneo-go/pkg/core/interop_system_test.go
Anna Shaleva 7a2d37cf7e core: update System.Blockchain.GetBlock interop
closes #1025

Now we put on stack stackitem.Array instead of Interop, so we're able to
use all available block properties without extra interop getters.
Removed Neo.Blockchain.GetBlock interop as we don't need it anymore.
2020-06-09 23:24:04 +03:00

147 lines
4.6 KiB
Go

package core
import (
"math/big"
"testing"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/stretchr/testify/require"
)
func TestBCGetTransaction(t *testing.T) {
v, tx, context, chain := createVMAndTX(t)
defer chain.Close()
t.Run("success", func(t *testing.T) {
require.NoError(t, context.DAO.StoreAsTransaction(tx, 0))
v.Estack().PushVal(tx.Hash().BytesBE())
err := bcGetTransaction(context, v)
require.NoError(t, err)
value := v.Estack().Pop().Value()
actual, ok := value.([]stackitem.Item)
require.True(t, ok)
require.Equal(t, 8, len(actual))
require.Equal(t, tx.Hash().BytesBE(), actual[0].Value().([]byte))
require.Equal(t, int64(tx.Version), actual[1].Value().(*big.Int).Int64())
require.Equal(t, int64(tx.Nonce), actual[2].Value().(*big.Int).Int64())
require.Equal(t, tx.Sender.BytesBE(), actual[3].Value().([]byte))
require.Equal(t, int64(tx.SystemFee), actual[4].Value().(*big.Int).Int64())
require.Equal(t, int64(tx.NetworkFee), actual[5].Value().(*big.Int).Int64())
require.Equal(t, int64(tx.ValidUntilBlock), actual[6].Value().(*big.Int).Int64())
require.Equal(t, tx.Script, actual[7].Value().([]byte))
})
t.Run("isn't traceable", func(t *testing.T) {
require.NoError(t, context.DAO.StoreAsTransaction(tx, 1))
v.Estack().PushVal(tx.Hash().BytesBE())
err := bcGetTransaction(context, v)
require.NoError(t, err)
_, ok := v.Estack().Pop().Item().(stackitem.Null)
require.True(t, ok)
})
t.Run("bad hash", func(t *testing.T) {
require.NoError(t, context.DAO.StoreAsTransaction(tx, 1))
v.Estack().PushVal(tx.Hash().BytesLE())
err := bcGetTransaction(context, v)
require.NoError(t, err)
_, ok := v.Estack().Pop().Item().(stackitem.Null)
require.True(t, ok)
})
}
func TestBCGetTransactionFromBlock(t *testing.T) {
v, block, context, chain := createVMAndBlock(t)
defer chain.Close()
require.NoError(t, chain.AddBlock(chain.newBlock()))
require.NoError(t, context.DAO.StoreAsBlock(block))
t.Run("success", func(t *testing.T) {
v.Estack().PushVal(0)
v.Estack().PushVal(block.Hash().BytesBE())
err := bcGetTransactionFromBlock(context, v)
require.NoError(t, err)
value := v.Estack().Pop().Value()
actual, ok := value.([]byte)
require.True(t, ok)
require.Equal(t, block.Transactions[0].Hash().BytesBE(), actual)
})
t.Run("invalid block hash", func(t *testing.T) {
v.Estack().PushVal(0)
v.Estack().PushVal(block.Hash().BytesBE()[:10])
err := bcGetTransactionFromBlock(context, v)
require.Error(t, err)
})
t.Run("isn't traceable", func(t *testing.T) {
block.Index = 2
require.NoError(t, context.DAO.StoreAsBlock(block))
v.Estack().PushVal(0)
v.Estack().PushVal(block.Hash().BytesBE())
err := bcGetTransactionFromBlock(context, v)
require.NoError(t, err)
_, ok := v.Estack().Pop().Item().(stackitem.Null)
require.True(t, ok)
})
t.Run("bad block hash", func(t *testing.T) {
block.Index = 1
require.NoError(t, context.DAO.StoreAsBlock(block))
v.Estack().PushVal(0)
v.Estack().PushVal(block.Hash().BytesLE())
err := bcGetTransactionFromBlock(context, v)
require.NoError(t, err)
_, ok := v.Estack().Pop().Item().(stackitem.Null)
require.True(t, ok)
})
t.Run("bad transaction index", func(t *testing.T) {
require.NoError(t, context.DAO.StoreAsBlock(block))
v.Estack().PushVal(1)
v.Estack().PushVal(block.Hash().BytesBE())
err := bcGetTransactionFromBlock(context, v)
require.Error(t, err)
})
}
func TestBCGetBlock(t *testing.T) {
v, context, chain := createVM(t)
defer chain.Close()
block := chain.newBlock()
require.NoError(t, chain.AddBlock(block))
t.Run("success", func(t *testing.T) {
v.Estack().PushVal(block.Hash().BytesBE())
err := bcGetBlock(context, v)
require.NoError(t, err)
value := v.Estack().Pop().Value()
actual, ok := value.([]stackitem.Item)
require.True(t, ok)
require.Equal(t, 8, len(actual))
require.Equal(t, block.Hash().BytesBE(), actual[0].Value().([]byte))
require.Equal(t, int64(block.Version), actual[1].Value().(*big.Int).Int64())
require.Equal(t, block.PrevHash.BytesBE(), actual[2].Value().([]byte))
require.Equal(t, block.MerkleRoot.BytesBE(), actual[3].Value().([]byte))
require.Equal(t, int64(block.Timestamp), actual[4].Value().(*big.Int).Int64())
require.Equal(t, int64(block.Index), actual[5].Value().(*big.Int).Int64())
require.Equal(t, block.NextConsensus.BytesBE(), actual[6].Value().([]byte))
require.Equal(t, int64(len(block.Transactions)), actual[7].Value().(*big.Int).Int64())
})
t.Run("bad hash", func(t *testing.T) {
v.Estack().PushVal(block.Hash().BytesLE())
err := bcGetTransaction(context, v)
require.NoError(t, err)
_, ok := v.Estack().Pop().Item().(stackitem.Null)
require.True(t, ok)
})
}