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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
package common
import (
"bytes"
"fmt"
"math"
"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/core/vm/sqlvm/schema"
"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) TestUint64ToBytes() {
testcases := []uint64{1, 65535, math.MaxUint64}
for _, i := range testcases {
s.Require().Equal(i, bytesToUint64(uint64ToBytes(i)))
}
}
func (s *StorageTestSuite) TestGetRowAddress() {
id := uint64(555666)
table := schema.TableRef(1)
key := [][]byte{
[]byte("tables"),
{uint8(table)},
[]byte("primary"),
uint64ToBytes(id),
}
hw := sha3.NewLegacyKeccak256()
rlp.Encode(hw, key)
bytes := hw.Sum(nil)
storage := &Storage{}
result := storage.GetRowPathHash(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(""),
},
}
SetDataToStorage(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 SetDataToStorage(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)/common.HashLength + 1
ptr := crypto.Keccak256Hash(slot.Bytes())
for s := 0; s < slotNum; s++ {
start := s * common.HashLength
end := (s + 1) * common.HashLength
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)
}
}
}
}
func (s *StorageTestSuite) TestOwner() {
db := ethdb.NewMemDatabase()
state, _ := state.New(common.Hash{}, state.NewDatabase(db))
storage := NewStorage(state)
contractA := common.BytesToAddress([]byte("I'm sad."))
ownerA := common.BytesToAddress([]byte{5, 5, 6, 6})
contractB := common.BytesToAddress([]byte{9, 5, 2, 7})
ownerB := common.BytesToAddress([]byte("Tong Pak-Fu"))
storage.StoreOwner(contractA, ownerA)
storage.StoreOwner(contractB, ownerB)
s.Require().Equal(ownerA, storage.LoadOwner(contractA))
s.Require().Equal(ownerB, storage.LoadOwner(contractB))
storage.StoreOwner(contractA, ownerB)
s.Require().Equal(ownerB, storage.LoadOwner(contractA))
}
func (s *StorageTestSuite) TestTableWriter() {
db := ethdb.NewMemDatabase()
state, _ := state.New(common.Hash{}, state.NewDatabase(db))
storage := NewStorage(state)
table1 := schema.TableRef(0)
table2 := schema.TableRef(1)
contractA := common.BytesToAddress([]byte("A"))
contractB := common.BytesToAddress([]byte("B"))
addrs := []common.Address{
common.BytesToAddress([]byte("addr1")),
common.BytesToAddress([]byte("addr2")),
common.BytesToAddress([]byte("addr3")),
}
// Genesis.
s.Require().Len(storage.LoadTableWriters(contractA, table1), 0)
s.Require().Len(storage.LoadTableWriters(contractB, table1), 0)
// Check writer list.
storage.InsertTableWriter(contractA, table1, addrs[0])
storage.InsertTableWriter(contractA, table1, addrs[1])
storage.InsertTableWriter(contractA, table1, addrs[2])
storage.InsertTableWriter(contractB, table2, addrs[0])
s.Require().Equal(addrs, storage.LoadTableWriters(contractA, table1))
s.Require().Len(storage.LoadTableWriters(contractA, table2), 0)
s.Require().Len(storage.LoadTableWriters(contractB, table1), 0)
s.Require().Equal([]common.Address{addrs[0]},
storage.LoadTableWriters(contractB, table2))
// Insert duplicate.
storage.InsertTableWriter(contractA, table1, addrs[0])
storage.InsertTableWriter(contractA, table1, addrs[1])
storage.InsertTableWriter(contractA, table1, addrs[2])
s.Require().Equal(addrs, storage.LoadTableWriters(contractA, table1))
// Delete some writer.
storage.DeleteTableWriter(contractA, table1, addrs[0])
storage.DeleteTableWriter(contractA, table2, addrs[0])
storage.DeleteTableWriter(contractB, table2, addrs[0])
s.Require().Equal([]common.Address{addrs[2], addrs[1]},
storage.LoadTableWriters(contractA, table1))
s.Require().Len(storage.LoadTableWriters(contractA, table2), 0)
s.Require().Len(storage.LoadTableWriters(contractB, table1), 0)
s.Require().Len(storage.LoadTableWriters(contractB, table2), 0)
// Delete again.
storage.DeleteTableWriter(contractA, table1, addrs[2])
s.Require().Equal([]common.Address{addrs[1]},
storage.LoadTableWriters(contractA, table1))
// Check writer.
s.Require().False(storage.IsTableWriter(contractA, table1, addrs[0]))
s.Require().True(storage.IsTableWriter(contractA, table1, addrs[1]))
s.Require().False(storage.IsTableWriter(contractA, table1, addrs[2]))
s.Require().False(storage.IsTableWriter(contractA, table2, addrs[0]))
s.Require().False(storage.IsTableWriter(contractB, table2, addrs[0]))
}
func (s *StorageTestSuite) TestSequence() {
db := ethdb.NewMemDatabase()
state, _ := state.New(common.Hash{}, state.NewDatabase(db))
storage := NewStorage(state)
table1 := schema.TableRef(0)
table2 := schema.TableRef(1)
contract := common.BytesToAddress([]byte("A"))
s.Require().Equal(uint64(0), storage.IncSequence(contract, table1, 0, 2))
s.Require().Equal(uint64(2), storage.IncSequence(contract, table1, 0, 1))
s.Require().Equal(uint64(3), storage.IncSequence(contract, table1, 0, 1))
// Repeat on another sequence.
s.Require().Equal(uint64(0), storage.IncSequence(contract, table1, 1, 1))
s.Require().Equal(uint64(1), storage.IncSequence(contract, table1, 1, 2))
s.Require().Equal(uint64(3), storage.IncSequence(contract, table1, 1, 3))
// Repeat on another table.
s.Require().Equal(uint64(0), storage.IncSequence(contract, table2, 0, 3))
s.Require().Equal(uint64(3), storage.IncSequence(contract, table2, 0, 4))
s.Require().Equal(uint64(7), storage.IncSequence(contract, table2, 0, 5))
}
func TestStorage(t *testing.T) {
suite.Run(t, new(StorageTestSuite))
}
|