-
Notifications
You must be signed in to change notification settings - Fork 20.8k
/
Copy pathstates_test.go
479 lines (454 loc) · 12.8 KB
/
states_test.go
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// Copyright 2024 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package pathdb
import (
"bytes"
"reflect"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
)
func TestStatesMerge(t *testing.T) {
a := newStates(
map[common.Hash][]byte{
{0xa}: {0xa0},
{0xb}: {0xb0},
{0xc}: {0xc0},
},
map[common.Hash]map[common.Hash][]byte{
{0xa}: {
common.Hash{0x1}: {0x10},
common.Hash{0x2}: {0x20},
},
{0xb}: {
common.Hash{0x1}: {0x10},
},
{0xc}: {
common.Hash{0x1}: {0x10},
},
},
false,
)
b := newStates(
map[common.Hash][]byte{
{0xa}: {0xa1},
{0xb}: {0xb1},
{0xc}: nil, // delete account
},
map[common.Hash]map[common.Hash][]byte{
{0xa}: {
common.Hash{0x1}: {0x11},
common.Hash{0x2}: nil, // delete slot
common.Hash{0x3}: {0x31},
},
{0xb}: {
common.Hash{0x1}: {0x11},
},
{0xc}: {
common.Hash{0x1}: nil, // delete slot
},
},
false,
)
a.merge(b)
blob, exist := a.account(common.Hash{0xa})
if !exist || !bytes.Equal(blob, []byte{0xa1}) {
t.Error("Unexpected value for account a")
}
blob, exist = a.account(common.Hash{0xb})
if !exist || !bytes.Equal(blob, []byte{0xb1}) {
t.Error("Unexpected value for account b")
}
blob, exist = a.account(common.Hash{0xc})
if !exist || len(blob) != 0 {
t.Error("Unexpected value for account c")
}
// unknown account
blob, exist = a.account(common.Hash{0xd})
if exist || len(blob) != 0 {
t.Error("Unexpected value for account d")
}
blob, exist = a.storage(common.Hash{0xa}, common.Hash{0x1})
if !exist || !bytes.Equal(blob, []byte{0x11}) {
t.Error("Unexpected value for a's storage")
}
blob, exist = a.storage(common.Hash{0xa}, common.Hash{0x2})
if !exist || len(blob) != 0 {
t.Error("Unexpected value for a's storage")
}
blob, exist = a.storage(common.Hash{0xa}, common.Hash{0x3})
if !exist || !bytes.Equal(blob, []byte{0x31}) {
t.Error("Unexpected value for a's storage")
}
blob, exist = a.storage(common.Hash{0xb}, common.Hash{0x1})
if !exist || !bytes.Equal(blob, []byte{0x11}) {
t.Error("Unexpected value for b's storage")
}
blob, exist = a.storage(common.Hash{0xc}, common.Hash{0x1})
if !exist || len(blob) != 0 {
t.Error("Unexpected value for c's storage")
}
// unknown storage slots
blob, exist = a.storage(common.Hash{0xd}, common.Hash{0x1})
if exist || len(blob) != 0 {
t.Error("Unexpected value for d's storage")
}
}
func TestStatesRevert(t *testing.T) {
a := newStates(
map[common.Hash][]byte{
{0xa}: {0xa0},
{0xb}: {0xb0},
{0xc}: {0xc0},
},
map[common.Hash]map[common.Hash][]byte{
{0xa}: {
common.Hash{0x1}: {0x10},
common.Hash{0x2}: {0x20},
},
{0xb}: {
common.Hash{0x1}: {0x10},
},
{0xc}: {
common.Hash{0x1}: {0x10},
},
},
false,
)
b := newStates(
map[common.Hash][]byte{
{0xa}: {0xa1},
{0xb}: {0xb1},
{0xc}: nil,
},
map[common.Hash]map[common.Hash][]byte{
{0xa}: {
common.Hash{0x1}: {0x11},
common.Hash{0x2}: nil,
common.Hash{0x3}: {0x31},
},
{0xb}: {
common.Hash{0x1}: {0x11},
},
{0xc}: {
common.Hash{0x1}: nil,
},
},
false,
)
a.merge(b)
a.revertTo(
map[common.Hash][]byte{
{0xa}: {0xa0},
{0xb}: {0xb0},
{0xc}: {0xc0},
},
map[common.Hash]map[common.Hash][]byte{
{0xa}: {
common.Hash{0x1}: {0x10},
common.Hash{0x2}: {0x20},
common.Hash{0x3}: nil,
},
{0xb}: {
common.Hash{0x1}: {0x10},
},
{0xc}: {
common.Hash{0x1}: {0x10},
},
},
)
blob, exist := a.account(common.Hash{0xa})
if !exist || !bytes.Equal(blob, []byte{0xa0}) {
t.Error("Unexpected value for account a")
}
blob, exist = a.account(common.Hash{0xb})
if !exist || !bytes.Equal(blob, []byte{0xb0}) {
t.Error("Unexpected value for account b")
}
blob, exist = a.account(common.Hash{0xc})
if !exist || !bytes.Equal(blob, []byte{0xc0}) {
t.Error("Unexpected value for account c")
}
// unknown account
blob, exist = a.account(common.Hash{0xd})
if exist || len(blob) != 0 {
t.Error("Unexpected value for account d")
}
blob, exist = a.storage(common.Hash{0xa}, common.Hash{0x1})
if !exist || !bytes.Equal(blob, []byte{0x10}) {
t.Error("Unexpected value for a's storage")
}
blob, exist = a.storage(common.Hash{0xa}, common.Hash{0x2})
if !exist || !bytes.Equal(blob, []byte{0x20}) {
t.Error("Unexpected value for a's storage")
}
blob, exist = a.storage(common.Hash{0xa}, common.Hash{0x3})
if !exist || len(blob) != 0 {
t.Error("Unexpected value for a's storage")
}
blob, exist = a.storage(common.Hash{0xb}, common.Hash{0x1})
if !exist || !bytes.Equal(blob, []byte{0x10}) {
t.Error("Unexpected value for b's storage")
}
blob, exist = a.storage(common.Hash{0xc}, common.Hash{0x1})
if !exist || !bytes.Equal(blob, []byte{0x10}) {
t.Error("Unexpected value for c's storage")
}
// unknown storage slots
blob, exist = a.storage(common.Hash{0xd}, common.Hash{0x1})
if exist || len(blob) != 0 {
t.Error("Unexpected value for d's storage")
}
}
// TestStateRevertAccountNullMarker tests the scenario that account x did not exist
// before and was created during transition w, reverting w will retain an x=nil
// entry in the set.
func TestStateRevertAccountNullMarker(t *testing.T) {
a := newStates(nil, nil, false) // empty initial state
b := newStates(
map[common.Hash][]byte{
{0xa}: {0xa},
},
nil,
false,
)
a.merge(b) // create account 0xa
a.revertTo(
map[common.Hash][]byte{
{0xa}: nil,
},
nil,
) // revert the transition b
blob, exist := a.account(common.Hash{0xa})
if !exist {
t.Fatal("null marker is not found")
}
if len(blob) != 0 {
t.Fatalf("Unexpected value for account, %v", blob)
}
}
// TestStateRevertStorageNullMarker tests the scenario that slot x did not exist
// before and was created during transition w, reverting w will retain an x=nil
// entry in the set.
func TestStateRevertStorageNullMarker(t *testing.T) {
a := newStates(map[common.Hash][]byte{
{0xa}: {0xa},
}, nil, false) // initial state with account 0xa
b := newStates(
nil,
map[common.Hash]map[common.Hash][]byte{
{0xa}: {
common.Hash{0x1}: {0x1},
},
},
false,
)
a.merge(b) // create slot 0x1
a.revertTo(
nil,
map[common.Hash]map[common.Hash][]byte{
{0xa}: {
common.Hash{0x1}: nil,
},
},
) // revert the transition b
blob, exist := a.storage(common.Hash{0xa}, common.Hash{0x1})
if !exist {
t.Fatal("null marker is not found")
}
if len(blob) != 0 {
t.Fatalf("Unexpected value for storage slot, %v", blob)
}
}
func TestStatesEncode(t *testing.T) {
testStatesEncode(t, false)
testStatesEncode(t, true)
}
func testStatesEncode(t *testing.T, rawStorageKey bool) {
s := newStates(
map[common.Hash][]byte{
{0x1}: {0x1},
},
map[common.Hash]map[common.Hash][]byte{
{0x1}: {
common.Hash{0x1}: {0x1},
},
},
rawStorageKey,
)
buf := bytes.NewBuffer(nil)
if err := s.encode(buf); err != nil {
t.Fatalf("Failed to encode states, %v", err)
}
var dec stateSet
if err := dec.decode(rlp.NewStream(buf, 0)); err != nil {
t.Fatalf("Failed to decode states, %v", err)
}
if !reflect.DeepEqual(s.accountData, dec.accountData) {
t.Fatal("Unexpected account data")
}
if !reflect.DeepEqual(s.storageData, dec.storageData) {
t.Fatal("Unexpected storage data")
}
if s.rawStorageKey != dec.rawStorageKey {
t.Fatal("Unexpected rawStorageKey flag")
}
}
func TestStateWithOriginEncode(t *testing.T) {
testStateWithOriginEncode(t, false)
testStateWithOriginEncode(t, true)
}
func testStateWithOriginEncode(t *testing.T, rawStorageKey bool) {
s := NewStateSetWithOrigin(
map[common.Hash][]byte{
{0x1}: {0x1},
},
map[common.Hash]map[common.Hash][]byte{
{0x1}: {
common.Hash{0x1}: {0x1},
},
},
map[common.Address][]byte{
{0x1}: {0x1},
},
map[common.Address]map[common.Hash][]byte{
{0x1}: {
common.Hash{0x1}: {0x1},
},
},
rawStorageKey,
)
buf := bytes.NewBuffer(nil)
if err := s.encode(buf); err != nil {
t.Fatalf("Failed to encode states, %v", err)
}
var dec StateSetWithOrigin
if err := dec.decode(rlp.NewStream(buf, 0)); err != nil {
t.Fatalf("Failed to decode states, %v", err)
}
if !reflect.DeepEqual(s.accountData, dec.accountData) {
t.Fatal("Unexpected account data")
}
if !reflect.DeepEqual(s.storageData, dec.storageData) {
t.Fatal("Unexpected storage data")
}
if !reflect.DeepEqual(s.accountOrigin, dec.accountOrigin) {
t.Fatal("Unexpected account origin data")
}
if !reflect.DeepEqual(s.storageOrigin, dec.storageOrigin) {
t.Fatal("Unexpected storage origin data")
}
if s.rawStorageKey != dec.rawStorageKey {
t.Fatal("Unexpected rawStorageKey flag")
}
}
func TestStateSizeTracking(t *testing.T) {
expSizeA := 3*(common.HashLength+1) + /* account data */
2*(2*common.HashLength+1) + /* storage data of 0xa */
2*common.HashLength + 3 + /* storage data of 0xb */
2*common.HashLength + 1 /* storage data of 0xc */
a := newStates(
map[common.Hash][]byte{
{0xa}: {0xa0}, // common.HashLength+1
{0xb}: {0xb0}, // common.HashLength+1
{0xc}: {0xc0}, // common.HashLength+1
},
map[common.Hash]map[common.Hash][]byte{
{0xa}: {
common.Hash{0x1}: {0x10}, // 2*common.HashLength+1
common.Hash{0x2}: {0x20}, // 2*common.HashLength+1
},
{0xb}: {
common.Hash{0x1}: {0x10, 0x11, 0x12}, // 2*common.HashLength+3
},
{0xc}: {
common.Hash{0x1}: {0x10}, // 2*common.HashLength+1
},
},
false,
)
if a.size != uint64(expSizeA) {
t.Fatalf("Unexpected size, want: %d, got: %d", expSizeA, a.size)
}
expSizeB := common.HashLength + 2 + common.HashLength + 3 + common.HashLength + /* account data */
2*common.HashLength + 3 + 2*common.HashLength + 2 + /* storage data of 0xa */
2*common.HashLength + 2 + 2*common.HashLength + 2 + /* storage data of 0xb */
3*2*common.HashLength /* storage data of 0xc */
b := newStates(
map[common.Hash][]byte{
{0xa}: {0xa1, 0xa1}, // common.HashLength+2
{0xb}: {0xb1, 0xb1, 0xb1}, // common.HashLength+3
{0xc}: nil, // common.HashLength, account deletion
},
map[common.Hash]map[common.Hash][]byte{
{0xa}: {
common.Hash{0x1}: {0x11, 0x11, 0x11}, // 2*common.HashLength+3
common.Hash{0x3}: {0x31, 0x31}, // 2*common.HashLength+2, slot creation
},
{0xb}: {
common.Hash{0x1}: {0x11, 0x11}, // 2*common.HashLength+2
common.Hash{0x2}: {0x22, 0x22}, // 2*common.HashLength+2, slot creation
},
// The storage of 0xc is entirely removed
{0xc}: {
common.Hash{0x1}: nil, // 2*common.HashLength, slot deletion
common.Hash{0x2}: nil, // 2*common.HashLength, slot deletion
common.Hash{0x3}: nil, // 2*common.HashLength, slot deletion
},
},
false,
)
if b.size != uint64(expSizeB) {
t.Fatalf("Unexpected size, want: %d, got: %d", expSizeB, b.size)
}
a.merge(b)
mergeSize := expSizeA + 1 /* account a data change */ + 2 /* account b data change */ - 1 /* account c data change */
mergeSize += 2*common.HashLength + 2 + 2 /* storage a change */
mergeSize += 2*common.HashLength + 2 - 1 /* storage b change */
mergeSize += 2*2*common.HashLength - 1 /* storage data removal of 0xc */
if a.size != uint64(mergeSize) {
t.Fatalf("Unexpected size, want: %d, got: %d", mergeSize, a.size)
}
// Revert the set to original status
a.revertTo(
map[common.Hash][]byte{
{0xa}: {0xa0},
{0xb}: {0xb0},
{0xc}: {0xc0},
},
map[common.Hash]map[common.Hash][]byte{
{0xa}: {
common.Hash{0x1}: {0x10},
common.Hash{0x2}: {0x20},
common.Hash{0x3}: nil, // revert slot creation
},
{0xb}: {
common.Hash{0x1}: {0x10, 0x11, 0x12},
common.Hash{0x2}: nil, // revert slot creation
},
{0xc}: {
common.Hash{0x1}: {0x10},
common.Hash{0x2}: {0x20}, // resurrected slot
common.Hash{0x3}: {0x30}, // resurrected slot
},
},
)
revertSize := expSizeA + 2*common.HashLength + 2*common.HashLength // delete-marker of a.3 and b.2 slot
revertSize += 2 * (2*common.HashLength + 1) // resurrected slot, c.2, c.3
if a.size != uint64(revertSize) {
t.Fatalf("Unexpected size, want: %d, got: %d", revertSize, a.size)
}
}