2020-04-07 09:41:12 +00:00
|
|
|
package dao
|
2019-11-27 10:29:54 +00:00
|
|
|
|
|
|
|
import (
|
2020-06-18 10:50:30 +00:00
|
|
|
"encoding/binary"
|
2019-11-27 10:29:54 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-11-23 11:09:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/random"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
|
|
|
"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"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2020-11-11 15:43:28 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2020-07-31 12:48:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2019-11-27 10:29:54 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPutGetAndDecode(t *testing.T) {
|
2021-03-25 19:32:16 +00:00
|
|
|
dao := NewSimple(storage.NewMemoryStore(), false)
|
2019-12-04 12:24:49 +00:00
|
|
|
serializable := &TestSerializable{field: random.String(4)}
|
2019-11-27 10:29:54 +00:00
|
|
|
hash := []byte{1}
|
|
|
|
err := dao.Put(serializable, hash)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
gotAndDecoded := &TestSerializable{}
|
|
|
|
err = dao.GetAndDecode(gotAndDecoded, hash)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestSerializable structure used in testing.
|
|
|
|
type TestSerializable struct {
|
|
|
|
field string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TestSerializable) EncodeBinary(writer *io.BinWriter) {
|
|
|
|
writer.WriteString(t.field)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TestSerializable) DecodeBinary(reader *io.BinReader) {
|
|
|
|
t.field = reader.ReadString()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPutGetAppExecResult(t *testing.T) {
|
2021-03-25 19:32:16 +00:00
|
|
|
dao := NewSimple(storage.NewMemoryStore(), false)
|
2019-12-04 12:24:49 +00:00
|
|
|
hash := random.Uint256()
|
2020-03-23 12:03:37 +00:00
|
|
|
appExecResult := &state.AppExecResult{
|
2020-11-11 15:43:28 +00:00
|
|
|
Container: hash,
|
|
|
|
Execution: state.Execution{
|
|
|
|
Trigger: trigger.Application,
|
|
|
|
Events: []state.NotificationEvent{},
|
|
|
|
Stack: []stackitem.Item{},
|
|
|
|
},
|
2020-03-23 12:03:37 +00:00
|
|
|
}
|
2020-11-11 15:43:28 +00:00
|
|
|
err := dao.AppendAppExecResult(appExecResult, nil)
|
2019-11-27 10:29:54 +00:00
|
|
|
require.NoError(t, err)
|
2020-11-11 15:43:28 +00:00
|
|
|
gotAppExecResult, err := dao.GetAppExecResults(hash, trigger.All)
|
2019-11-27 10:29:54 +00:00
|
|
|
require.NoError(t, err)
|
2020-11-11 15:43:28 +00:00
|
|
|
require.Equal(t, []state.AppExecResult{*appExecResult}, gotAppExecResult)
|
2019-11-27 10:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPutGetStorageItem(t *testing.T) {
|
2021-03-25 19:32:16 +00:00
|
|
|
dao := NewSimple(storage.NewMemoryStore(), false)
|
2020-06-18 10:50:30 +00:00
|
|
|
id := int32(random.Int(0, 1024))
|
2019-11-27 10:29:54 +00:00
|
|
|
key := []byte{0}
|
2021-03-05 14:06:54 +00:00
|
|
|
storageItem := state.StorageItem{}
|
2020-06-18 10:50:30 +00:00
|
|
|
err := dao.PutStorageItem(id, key, storageItem)
|
2019-11-27 10:29:54 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-18 10:50:30 +00:00
|
|
|
gotStorageItem := dao.GetStorageItem(id, key)
|
2019-11-27 10:29:54 +00:00
|
|
|
require.Equal(t, storageItem, gotStorageItem)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteStorageItem(t *testing.T) {
|
2021-03-25 19:32:16 +00:00
|
|
|
dao := NewSimple(storage.NewMemoryStore(), false)
|
2020-06-18 10:50:30 +00:00
|
|
|
id := int32(random.Int(0, 1024))
|
2019-11-27 10:29:54 +00:00
|
|
|
key := []byte{0}
|
2021-03-05 14:06:54 +00:00
|
|
|
storageItem := state.StorageItem{}
|
2020-06-18 10:50:30 +00:00
|
|
|
err := dao.PutStorageItem(id, key, storageItem)
|
2019-11-27 10:29:54 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-18 10:50:30 +00:00
|
|
|
err = dao.DeleteStorageItem(id, key)
|
2019-11-27 10:29:54 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-18 10:50:30 +00:00
|
|
|
gotStorageItem := dao.GetStorageItem(id, key)
|
2019-11-27 10:29:54 +00:00
|
|
|
require.Nil(t, gotStorageItem)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBlock_NotExists(t *testing.T) {
|
2021-03-25 19:32:16 +00:00
|
|
|
dao := NewSimple(storage.NewMemoryStore(), false)
|
2019-12-04 12:24:49 +00:00
|
|
|
hash := random.Uint256()
|
2020-06-04 19:59:34 +00:00
|
|
|
block, err := dao.GetBlock(hash)
|
2019-11-27 10:29:54 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, block)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPutGetBlock(t *testing.T) {
|
2021-03-25 19:32:16 +00:00
|
|
|
dao := NewSimple(storage.NewMemoryStore(), false)
|
2020-01-14 12:32:07 +00:00
|
|
|
b := &block.Block{
|
2021-03-01 13:44:47 +00:00
|
|
|
Header: block.Header{
|
2019-11-27 10:29:54 +00:00
|
|
|
Script: transaction.Witness{
|
|
|
|
VerificationScript: []byte{byte(opcode.PUSH1)},
|
|
|
|
InvocationScript: []byte{byte(opcode.NOP)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-01-14 12:32:07 +00:00
|
|
|
hash := b.Hash()
|
2020-08-31 19:11:49 +00:00
|
|
|
err := dao.StoreAsBlock(b, nil)
|
2019-11-27 10:29:54 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-04 19:59:34 +00:00
|
|
|
gotBlock, err := dao.GetBlock(hash)
|
2019-11-27 10:29:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, gotBlock)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetVersion_NoVersion(t *testing.T) {
|
2021-03-25 19:32:16 +00:00
|
|
|
dao := NewSimple(storage.NewMemoryStore(), false)
|
2019-11-27 10:29:54 +00:00
|
|
|
version, err := dao.GetVersion()
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, "", version)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetVersion(t *testing.T) {
|
2021-03-25 19:32:16 +00:00
|
|
|
dao := NewSimple(storage.NewMemoryStore(), false)
|
2019-11-27 10:29:54 +00:00
|
|
|
err := dao.PutVersion("testVersion")
|
|
|
|
require.NoError(t, err)
|
|
|
|
version, err := dao.GetVersion()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, version)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetCurrentHeaderHeight_NoHeader(t *testing.T) {
|
2021-03-25 19:32:16 +00:00
|
|
|
dao := NewSimple(storage.NewMemoryStore(), false)
|
2019-11-27 10:29:54 +00:00
|
|
|
height, err := dao.GetCurrentBlockHeight()
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, uint32(0), height)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetCurrentHeaderHeight_Store(t *testing.T) {
|
2021-03-25 19:32:16 +00:00
|
|
|
dao := NewSimple(storage.NewMemoryStore(), false)
|
2020-01-14 12:32:07 +00:00
|
|
|
b := &block.Block{
|
2021-03-01 13:44:47 +00:00
|
|
|
Header: block.Header{
|
2019-11-27 10:29:54 +00:00
|
|
|
Script: transaction.Witness{
|
|
|
|
VerificationScript: []byte{byte(opcode.PUSH1)},
|
|
|
|
InvocationScript: []byte{byte(opcode.NOP)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-08-31 19:11:49 +00:00
|
|
|
err := dao.StoreAsCurrentBlock(b, nil)
|
2019-11-27 10:29:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
height, err := dao.GetCurrentBlockHeight()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uint32(0), height)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStoreAsTransaction(t *testing.T) {
|
2021-03-25 19:32:16 +00:00
|
|
|
dao := NewSimple(storage.NewMemoryStore(), false)
|
2021-03-25 16:18:01 +00:00
|
|
|
tx := transaction.New([]byte{byte(opcode.PUSH1)}, 1)
|
2019-11-27 10:29:54 +00:00
|
|
|
hash := tx.Hash()
|
2020-08-31 19:11:49 +00:00
|
|
|
err := dao.StoreAsTransaction(tx, 0, nil)
|
2019-11-27 10:29:54 +00:00
|
|
|
require.NoError(t, err)
|
2020-10-15 11:45:29 +00:00
|
|
|
err = dao.HasTransaction(hash)
|
|
|
|
require.NotNil(t, err)
|
2019-11-27 10:29:54 +00:00
|
|
|
}
|
2020-06-18 10:50:30 +00:00
|
|
|
|
|
|
|
func TestMakeStorageItemKey(t *testing.T) {
|
|
|
|
var id int32 = 5
|
|
|
|
|
|
|
|
expected := []byte{byte(storage.STStorage), 0, 0, 0, 0, 1, 2, 3}
|
|
|
|
binary.LittleEndian.PutUint32(expected[1:5], uint32(id))
|
|
|
|
actual := makeStorageItemKey(id, []byte{1, 2, 3})
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
|
|
|
|
expected = expected[0:5]
|
|
|
|
actual = makeStorageItemKey(id, nil)
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
}
|