aboutsummaryrefslogtreecommitdiffstats
path: root/core/vm/sqlvm/common/storage_test.go
blob: 42b8c2298b6cc37b6b6e3a13bc83f4193d21fa01 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package common

import (
    "bytes"
    "fmt"
    "testing"

    "github.com/stretchr/testify/suite"
    "golang.org/x/crypto/sha3"

    "github.com/dexon-foundation/dexon/common"
    "github.com/dexon-foundation/dexon/core/state"
    "github.com/dexon-foundation/dexon/crypto"
    "github.com/dexon-foundation/dexon/ethdb"
    "github.com/dexon-foundation/dexon/rlp"
)

type StorageTestSuite struct{ suite.Suite }

func (s *StorageTestSuite) TestGetPrimaryKeyHash() {
    id := uint64(555666)
    table := []byte("TABLE_A")
    key := [][]byte{
        []byte("tables"),
        table,
        []byte("primary"),
        convertIDtoBytes(id),
    }
    hw := sha3.NewLegacyKeccak256()
    rlp.Encode(hw, key)
    bytes := hw.Sum(nil)
    storage := Storage{}
    result := storage.GetPrimaryKeyHash(table, id)
    s.Require().Equal(bytes, result[:])
}

type decodeTestCase struct {
    name     string
    slotData common.Hash
    result   []byte
}

func (s *StorageTestSuite) TestDecodeDByte() {
    db := ethdb.NewMemDatabase()
    state, _ := state.New(common.Hash{}, state.NewDatabase(db))
    storage := NewStorage(state)
    address := common.BytesToAddress([]byte("123"))
    head := common.HexToHash("0x5566")
    testcase := []decodeTestCase{
        {
            name:     "small size",
            slotData: common.HexToHash("0x48656c6c6f2c20776f726c64210000000000000000000000000000000000001a"),
            result:   common.FromHex("0x48656c6c6f2c20776f726c6421"),
        },
        {
            name:     "32 byte case",
            slotData: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000041"),
            result:   []byte("Hello world. Hello DEXON, SQLVM."),
        },
        {
            name:     "large size",
            slotData: common.HexToHash("0x000000000000000000000000000000000000000000000000000000000000047D"),
            result:   []byte("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."),
        },
        {
            name:     "empty",
            slotData: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"),
            result:   []byte(""),
        },
    }
    SetDataToStateDB(head, storage, address, testcase)
    for i, t := range testcase {
        slot := storage.ShiftHashUint64(head, uint64(i))
        result := storage.DecodeDByteBySlot(address, slot)
        s.Require().Truef(bytes.Equal(result, t.result), fmt.Sprintf("name %v", t.name))
    }
}

func SetDataToStateDB(head common.Hash, storage Storage, addr common.Address,
    testcase []decodeTestCase) {
    for i, t := range testcase {
        slot := storage.ShiftHashUint64(head, uint64(i))
        storage.SetState(addr, slot, t.slotData)
        b := t.slotData.Bytes()
        if b[len(b)-1]&0x1 != 0 {
            length := len(t.result)
            slotNum := (length-1)/32 + 1
            ptr := crypto.Keccak256Hash(slot.Bytes())
            for s := 0; s < slotNum; s++ {
                start := s * 32
                end := (s + 1) * 32
                if end > len(t.result) {
                    end = len(t.result)
                }
                hash := common.Hash{}
                copy(hash[:], t.result[start:end])
                storage.SetState(addr, ptr, hash)
                ptr = storage.ShiftHashUint64(ptr, 1)
            }
        }
    }
    storage.Commit(false)
}

func TestStorage(t *testing.T) {
    suite.Run(t, new(StorageTestSuite))
}