2019-11-18 16:31:27 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2021-01-12 12:32:27 +00:00
|
|
|
"math/big"
|
2019-11-18 16:31:27 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-06-18 09:00:51 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
2020-04-07 09:41:12 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/dao"
|
2020-04-08 10:35:39 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
2020-04-08 11:08:43 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/iterator"
|
2021-01-12 10:39:31 +00:00
|
|
|
istorage "github.com/nspcc-dev/neo-go/pkg/core/interop/storage"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2020-11-18 20:10:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
2020-06-09 09:12:56 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
2021-01-13 12:34:10 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
|
2020-03-03 14:21:42 +00:00
|
|
|
"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/opcode"
|
2020-06-03 12:55:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2019-11-18 16:31:27 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
/* Missing tests:
|
|
|
|
* TestTxGetWitnesses
|
|
|
|
* TestAccountIsStandard
|
|
|
|
* TestCreateContractStateFromVM
|
|
|
|
* TestContractCreate
|
|
|
|
* TestContractMigrate
|
|
|
|
* TestRuntimeSerialize
|
|
|
|
* TestRuntimeDeserialize
|
|
|
|
*/
|
|
|
|
|
2019-12-26 11:34:38 +00:00
|
|
|
func TestStorageFind(t *testing.T) {
|
2020-01-10 08:47:55 +00:00
|
|
|
v, contractState, context, chain := createVMAndContractState(t)
|
|
|
|
defer chain.Close()
|
2019-12-26 11:34:38 +00:00
|
|
|
|
2021-01-12 12:32:27 +00:00
|
|
|
arr := []stackitem.Item{
|
|
|
|
stackitem.NewBigInteger(big.NewInt(42)),
|
|
|
|
stackitem.NewByteArray([]byte("second")),
|
|
|
|
stackitem.Null{},
|
|
|
|
}
|
|
|
|
rawArr, err := stackitem.SerializeItem(stackitem.NewArray(arr))
|
|
|
|
require.NoError(t, err)
|
|
|
|
rawArr0, err := stackitem.SerializeItem(stackitem.NewArray(arr[:0]))
|
|
|
|
require.NoError(t, err)
|
|
|
|
rawArr1, err := stackitem.SerializeItem(stackitem.NewArray(arr[:1]))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-01-12 12:09:05 +00:00
|
|
|
skeys := [][]byte{{0x01, 0x02}, {0x02, 0x01}, {0x01, 0x01},
|
2021-01-12 12:32:27 +00:00
|
|
|
{0x04, 0x00}, {0x05, 0x00}, {0x06}, {0x07}, {0x08}}
|
2019-12-26 11:34:38 +00:00
|
|
|
items := []*state.StorageItem{
|
|
|
|
{
|
|
|
|
Value: []byte{0x01, 0x02, 0x03, 0x04},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: []byte{0x04, 0x03, 0x02, 0x01},
|
|
|
|
},
|
2020-05-29 08:02:26 +00:00
|
|
|
{
|
|
|
|
Value: []byte{0x03, 0x04, 0x05, 0x06},
|
|
|
|
},
|
2021-01-12 12:09:05 +00:00
|
|
|
{
|
|
|
|
Value: []byte{byte(stackitem.ByteArrayT), 2, 0xCA, 0xFE},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: []byte{0xFF, 0xFF},
|
|
|
|
},
|
2021-01-12 12:32:27 +00:00
|
|
|
{
|
|
|
|
Value: rawArr,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: rawArr0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: rawArr1,
|
|
|
|
},
|
2019-12-26 11:34:38 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 15:26:35 +00:00
|
|
|
require.NoError(t, chain.contracts.Management.PutContractState(chain.dao, contractState))
|
2019-12-26 11:34:38 +00:00
|
|
|
|
2020-06-18 10:50:30 +00:00
|
|
|
id := contractState.ID
|
2019-12-26 11:34:38 +00:00
|
|
|
|
|
|
|
for i := range skeys {
|
2020-06-18 10:50:30 +00:00
|
|
|
err := context.DAO.PutStorageItem(id, skeys[i], items[i])
|
2019-12-26 11:34:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-01-12 10:39:31 +00:00
|
|
|
testFind := func(t *testing.T, prefix byte, opts int64, expected []stackitem.Item) {
|
|
|
|
v.Estack().PushVal(opts)
|
|
|
|
v.Estack().PushVal([]byte{prefix})
|
2020-06-18 10:50:30 +00:00
|
|
|
v.Estack().PushVal(stackitem.NewInterop(&StorageContext{ID: id}))
|
2019-12-26 11:34:38 +00:00
|
|
|
|
2020-08-07 11:37:49 +00:00
|
|
|
err := storageFind(context)
|
2019-12-26 11:34:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
var iter *stackitem.Interop
|
2021-01-12 09:30:21 +00:00
|
|
|
require.NotPanics(t, func() { iter = v.Estack().Pop().Interop() })
|
2019-12-26 11:34:38 +00:00
|
|
|
|
2021-01-12 10:39:31 +00:00
|
|
|
for i := range expected { // sorted indices with mathing prefix
|
2021-01-12 09:30:21 +00:00
|
|
|
v.Estack().PushVal(iter)
|
|
|
|
require.NoError(t, iterator.Next(context))
|
|
|
|
require.True(t, v.Estack().Pop().Bool())
|
2019-12-26 11:34:38 +00:00
|
|
|
|
2021-01-12 09:30:21 +00:00
|
|
|
v.Estack().PushVal(iter)
|
2021-01-12 12:32:27 +00:00
|
|
|
if expected[i] == nil {
|
|
|
|
require.Panics(t, func() { _ = iterator.Value(context) })
|
|
|
|
return
|
|
|
|
}
|
2021-01-12 09:30:21 +00:00
|
|
|
require.NoError(t, iterator.Value(context))
|
2021-01-12 10:39:31 +00:00
|
|
|
require.Equal(t, expected[i], v.Estack().Pop().Item())
|
2021-01-12 09:30:21 +00:00
|
|
|
}
|
2019-12-26 11:34:38 +00:00
|
|
|
|
|
|
|
v.Estack().PushVal(iter)
|
2021-01-12 09:30:21 +00:00
|
|
|
require.NoError(t, iterator.Next(context))
|
2019-12-26 11:34:38 +00:00
|
|
|
require.False(t, v.Estack().Pop().Bool())
|
2021-01-12 10:39:31 +00:00
|
|
|
}
|
2019-12-26 11:34:38 +00:00
|
|
|
|
2021-01-12 10:39:31 +00:00
|
|
|
t.Run("normal invocation", func(t *testing.T) {
|
|
|
|
testFind(t, 0x01, istorage.FindDefault, []stackitem.Item{
|
|
|
|
stackitem.NewStruct([]stackitem.Item{
|
|
|
|
stackitem.NewByteArray(skeys[2]),
|
|
|
|
stackitem.NewByteArray(items[2].Value),
|
|
|
|
}),
|
|
|
|
stackitem.NewStruct([]stackitem.Item{
|
|
|
|
stackitem.NewByteArray(skeys[0]),
|
|
|
|
stackitem.NewByteArray(items[0].Value),
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
})
|
2020-05-29 08:02:26 +00:00
|
|
|
|
2021-01-12 10:39:31 +00:00
|
|
|
t.Run("keys only", func(t *testing.T) {
|
|
|
|
testFind(t, 0x01, istorage.FindKeysOnly, []stackitem.Item{
|
|
|
|
stackitem.NewByteArray(skeys[2]),
|
|
|
|
stackitem.NewByteArray(skeys[0]),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("remove prefix", func(t *testing.T) {
|
|
|
|
testFind(t, 0x01, istorage.FindKeysOnly|istorage.FindRemovePrefix, []stackitem.Item{
|
|
|
|
stackitem.NewByteArray(skeys[2][1:]),
|
|
|
|
stackitem.NewByteArray(skeys[0][1:]),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("values only", func(t *testing.T) {
|
|
|
|
testFind(t, 0x01, istorage.FindValuesOnly, []stackitem.Item{
|
|
|
|
stackitem.NewByteArray(items[2].Value),
|
|
|
|
stackitem.NewByteArray(items[0].Value),
|
|
|
|
})
|
|
|
|
})
|
2021-01-12 12:09:05 +00:00
|
|
|
t.Run("deserialize values", func(t *testing.T) {
|
|
|
|
testFind(t, 0x04, istorage.FindValuesOnly|istorage.FindDeserialize, []stackitem.Item{
|
|
|
|
stackitem.NewByteArray(items[3].Value[2:]),
|
|
|
|
})
|
|
|
|
t.Run("invalid", func(t *testing.T) {
|
|
|
|
v.Estack().PushVal(istorage.FindDeserialize)
|
|
|
|
v.Estack().PushVal([]byte{0x05})
|
|
|
|
v.Estack().PushVal(stackitem.NewInterop(&StorageContext{ID: id}))
|
|
|
|
err := storageFind(context)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var iter *stackitem.Interop
|
|
|
|
require.NotPanics(t, func() { iter = v.Estack().Pop().Interop() })
|
|
|
|
|
|
|
|
v.Estack().PushVal(iter)
|
|
|
|
require.NoError(t, iterator.Next(context))
|
|
|
|
|
|
|
|
v.Estack().PushVal(iter)
|
|
|
|
require.Panics(t, func() { _ = iterator.Value(context) })
|
|
|
|
})
|
|
|
|
})
|
2021-01-12 12:32:27 +00:00
|
|
|
t.Run("PickN", func(t *testing.T) {
|
|
|
|
testFind(t, 0x06, istorage.FindPick0|istorage.FindValuesOnly|istorage.FindDeserialize, arr[:1])
|
|
|
|
testFind(t, 0x06, istorage.FindPick1|istorage.FindValuesOnly|istorage.FindDeserialize, arr[1:2])
|
|
|
|
// Array with 0 elements.
|
|
|
|
testFind(t, 0x07, istorage.FindPick0|istorage.FindValuesOnly|istorage.FindDeserialize,
|
|
|
|
[]stackitem.Item{nil})
|
|
|
|
// Array with 1 element.
|
|
|
|
testFind(t, 0x08, istorage.FindPick1|istorage.FindValuesOnly|istorage.FindDeserialize,
|
|
|
|
[]stackitem.Item{nil})
|
|
|
|
// Not an array, but serialized ByteArray.
|
|
|
|
testFind(t, 0x04, istorage.FindPick1|istorage.FindValuesOnly|istorage.FindDeserialize,
|
|
|
|
[]stackitem.Item{nil})
|
|
|
|
})
|
2020-05-29 08:02:26 +00:00
|
|
|
|
2021-01-12 10:39:31 +00:00
|
|
|
t.Run("normal invocation, empty result", func(t *testing.T) {
|
|
|
|
testFind(t, 0x03, istorage.FindDefault, nil)
|
2020-05-29 08:02:26 +00:00
|
|
|
})
|
|
|
|
|
2021-01-12 10:39:31 +00:00
|
|
|
t.Run("invalid options", func(t *testing.T) {
|
|
|
|
invalid := []int64{
|
|
|
|
istorage.FindKeysOnly | istorage.FindValuesOnly,
|
|
|
|
^istorage.FindAll,
|
2021-01-12 12:09:05 +00:00
|
|
|
istorage.FindKeysOnly | istorage.FindDeserialize,
|
2021-01-12 12:32:27 +00:00
|
|
|
istorage.FindPick0,
|
|
|
|
istorage.FindPick0 | istorage.FindPick1 | istorage.FindDeserialize,
|
|
|
|
istorage.FindPick0 | istorage.FindPick1,
|
2021-01-12 10:39:31 +00:00
|
|
|
}
|
|
|
|
for _, opts := range invalid {
|
|
|
|
v.Estack().PushVal(opts)
|
|
|
|
v.Estack().PushVal([]byte{0x01})
|
|
|
|
v.Estack().PushVal(stackitem.NewInterop(&StorageContext{ID: id}))
|
|
|
|
require.Error(t, storageFind(context))
|
|
|
|
}
|
|
|
|
})
|
2019-12-26 11:34:38 +00:00
|
|
|
t.Run("invalid type for StorageContext", func(t *testing.T) {
|
2021-01-12 10:39:31 +00:00
|
|
|
v.Estack().PushVal(istorage.FindDefault)
|
2019-12-26 11:34:38 +00:00
|
|
|
v.Estack().PushVal([]byte{0x01})
|
2020-06-03 12:55:06 +00:00
|
|
|
v.Estack().PushVal(stackitem.NewInterop(nil))
|
2019-12-26 11:34:38 +00:00
|
|
|
|
2020-08-07 11:37:49 +00:00
|
|
|
require.Error(t, storageFind(context))
|
2019-12-26 11:34:38 +00:00
|
|
|
})
|
|
|
|
|
2020-06-18 10:50:30 +00:00
|
|
|
t.Run("invalid id", func(t *testing.T) {
|
|
|
|
invalidID := id + 1
|
2019-12-26 11:34:38 +00:00
|
|
|
|
2021-01-12 10:39:31 +00:00
|
|
|
v.Estack().PushVal(istorage.FindDefault)
|
2019-12-26 11:34:38 +00:00
|
|
|
v.Estack().PushVal([]byte{0x01})
|
2020-06-18 10:50:30 +00:00
|
|
|
v.Estack().PushVal(stackitem.NewInterop(&StorageContext{ID: invalidID}))
|
2019-12-26 11:34:38 +00:00
|
|
|
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, storageFind(context))
|
2021-01-12 09:30:21 +00:00
|
|
|
require.NoError(t, iterator.Next(context))
|
2020-06-18 10:50:30 +00:00
|
|
|
require.False(t, v.Estack().Pop().Bool())
|
2019-12-26 11:34:38 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-05 15:42:58 +00:00
|
|
|
// Helper functions to create VM, InteropContext, TX, Account, Contract.
|
2019-11-18 16:31:27 +00:00
|
|
|
|
2020-06-09 10:18:32 +00:00
|
|
|
func createVM(t *testing.T) (*vm.VM, *interop.Context, *Blockchain) {
|
|
|
|
chain := newTestChain(t)
|
|
|
|
context := chain.newInteropContext(trigger.Application,
|
2020-11-17 12:57:50 +00:00
|
|
|
dao.NewSimple(storage.NewMemoryStore(), netmode.UnitTestNet, chain.config.StateRootInHeader), nil, nil)
|
2020-08-07 11:37:49 +00:00
|
|
|
v := context.SpawnVM()
|
2020-06-09 10:18:32 +00:00
|
|
|
return v, context, chain
|
|
|
|
}
|
|
|
|
|
2020-04-08 10:35:39 +00:00
|
|
|
func createVMAndPushBlock(t *testing.T) (*vm.VM, *block.Block, *interop.Context, *Blockchain) {
|
2020-06-09 09:18:08 +00:00
|
|
|
v, block, context, chain := createVMAndBlock(t)
|
|
|
|
v.Estack().PushVal(stackitem.NewInterop(block))
|
|
|
|
return v, block, context, chain
|
|
|
|
}
|
|
|
|
|
|
|
|
func createVMAndBlock(t *testing.T) (*vm.VM, *block.Block, *interop.Context, *Blockchain) {
|
2019-11-18 16:31:27 +00:00
|
|
|
block := newDumbBlock()
|
2020-01-10 08:47:55 +00:00
|
|
|
chain := newTestChain(t)
|
2020-11-17 12:57:50 +00:00
|
|
|
d := dao.NewSimple(storage.NewMemoryStore(), netmode.UnitTestNet, chain.GetConfig().StateRootInHeader)
|
|
|
|
context := chain.newInteropContext(trigger.Application, d, block, nil)
|
2020-08-07 11:37:49 +00:00
|
|
|
v := context.SpawnVM()
|
2020-01-10 08:47:55 +00:00
|
|
|
return v, block, context, chain
|
2019-11-18 16:31:27 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 10:35:39 +00:00
|
|
|
func createVMAndPushTX(t *testing.T) (*vm.VM, *transaction.Transaction, *interop.Context, *Blockchain) {
|
2020-01-10 08:47:55 +00:00
|
|
|
v, tx, context, chain := createVMAndTX(t)
|
2020-06-03 12:55:06 +00:00
|
|
|
v.Estack().PushVal(stackitem.NewInterop(tx))
|
2020-01-10 08:47:55 +00:00
|
|
|
return v, tx, context, chain
|
2019-11-18 16:31:27 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 10:35:39 +00:00
|
|
|
func createVMAndContractState(t *testing.T) (*vm.VM, *state.Contract, *interop.Context, *Blockchain) {
|
2020-06-09 09:12:56 +00:00
|
|
|
script := []byte("testscript")
|
2020-11-18 09:43:51 +00:00
|
|
|
m := manifest.NewManifest("Test")
|
2021-01-13 12:34:10 +00:00
|
|
|
ne, err := nef.NewFile(script)
|
|
|
|
require.NoError(t, err)
|
2019-11-28 16:06:09 +00:00
|
|
|
contractState := &state.Contract{
|
2021-02-09 09:02:38 +00:00
|
|
|
ContractBase: state.ContractBase{
|
|
|
|
NEF: *ne,
|
|
|
|
Hash: hash.Hash160(script),
|
|
|
|
Manifest: *m,
|
|
|
|
ID: 123,
|
|
|
|
},
|
2019-11-18 16:31:27 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 08:47:55 +00:00
|
|
|
chain := newTestChain(t)
|
2020-11-17 12:57:50 +00:00
|
|
|
d := dao.NewSimple(storage.NewMemoryStore(), netmode.UnitTestNet, chain.config.StateRootInHeader)
|
|
|
|
context := chain.newInteropContext(trigger.Application, d, nil, nil)
|
2020-08-07 11:37:49 +00:00
|
|
|
v := context.SpawnVM()
|
2020-01-10 08:47:55 +00:00
|
|
|
return v, contractState, context, chain
|
2019-11-18 16:31:27 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 10:35:39 +00:00
|
|
|
func createVMAndTX(t *testing.T) (*vm.VM, *transaction.Transaction, *interop.Context, *Blockchain) {
|
2019-12-23 12:04:23 +00:00
|
|
|
script := []byte{byte(opcode.PUSH1), byte(opcode.RET)}
|
2020-06-18 09:00:51 +00:00
|
|
|
tx := transaction.New(netmode.UnitTestNet, script, 0)
|
2020-07-29 16:57:38 +00:00
|
|
|
tx.Signers = []transaction.Signer{{Account: util.Uint160{1, 2, 3, 4}}}
|
2021-02-15 13:40:42 +00:00
|
|
|
tx.Scripts = []transaction.Witness{{InvocationScript: []byte{}, VerificationScript: []byte{}}}
|
2020-01-10 08:47:55 +00:00
|
|
|
chain := newTestChain(t)
|
2020-11-17 12:57:50 +00:00
|
|
|
d := dao.NewSimple(storage.NewMemoryStore(), netmode.UnitTestNet, chain.config.StateRootInHeader)
|
|
|
|
context := chain.newInteropContext(trigger.Application, d, nil, tx)
|
2020-08-07 11:37:49 +00:00
|
|
|
v := context.SpawnVM()
|
2020-01-10 08:47:55 +00:00
|
|
|
return v, tx, context, chain
|
2019-11-18 16:31:27 +00:00
|
|
|
}
|