forked from TrueCloudLab/neoneo-go
112 lines
3.4 KiB
Go
112 lines
3.4 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)
|
|
})
|
|
}
|