forked from Fantom-foundation/substate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubstate_db.go
576 lines (485 loc) · 13.4 KB
/
substate_db.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
package substate
import (
"encoding/binary"
"fmt"
"io"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/rlp"
)
const (
Stage1SubstatePrefix = "1s" // Stage1SubstatePrefix + block (64-bit) + tx (64-bit) -> substateRLP
Stage1CodePrefix = "1c" // Stage1CodePrefix + codeHash (256-bit) -> code
)
func Stage1SubstateKey(block uint64, tx int) []byte {
prefix := []byte(Stage1SubstatePrefix)
blockTx := make([]byte, 16)
binary.BigEndian.PutUint64(blockTx[0:8], block)
binary.BigEndian.PutUint64(blockTx[8:16], uint64(tx))
return append(prefix, blockTx...)
}
func DecodeStage1SubstateKey(key []byte) (block uint64, tx int, err error) {
prefix := Stage1SubstatePrefix
if len(key) != len(prefix)+8+8 {
err = fmt.Errorf("invalid length of stage1 substate key: %v", len(key))
return
}
if p := string(key[:len(prefix)]); p != prefix {
err = fmt.Errorf("invalid prefix of stage1 substate key: %#x", p)
return
}
blockTx := key[len(prefix):]
block = binary.BigEndian.Uint64(blockTx[0:8])
tx = int(binary.BigEndian.Uint64(blockTx[8:16]))
return
}
func Stage1SubstateBlockPrefix(block uint64) []byte {
return append([]byte(Stage1SubstatePrefix), BlockToBytes(block)...)
}
func BlockToBytes(block uint64) []byte {
blockBytes := make([]byte, 8)
binary.BigEndian.PutUint64(blockBytes[0:8], block)
return blockBytes
}
func Stage1CodeKey(codeHash common.Hash) []byte {
prefix := []byte(Stage1CodePrefix)
return append(prefix, codeHash.Bytes()...)
}
func DecodeStage1CodeKey(key []byte) (codeHash common.Hash, err error) {
prefix := Stage1CodePrefix
if len(key) != len(prefix)+32 {
err = fmt.Errorf("invalid length of stage1 code key: %v", len(key))
return
}
if p := string(key[:2]); p != prefix {
err = fmt.Errorf("invalid prefix of stage1 code key: %#x", p)
return
}
codeHash = common.BytesToHash(key[len(prefix):])
return
}
type BackendDatabase interface {
ethdb.KeyValueReader
ethdb.KeyValueWriter
ethdb.Batcher
ethdb.Iteratee
ethdb.Stater
ethdb.Compacter
io.Closer
}
type DB struct {
backend BackendDatabase
}
// Deprecated: This function will be private in the future. Please use NewDb, MakeDb or NewInMemoryDb instead.
func NewSubstateDB(backend BackendDatabase) *DB {
return &DB{backend: backend}
}
func newSubstateDB(backend BackendDatabase) *DB {
return &DB{backend: backend}
}
func (db *DB) Compact(start []byte, limit []byte) error {
return db.backend.Compact(start, limit)
}
func (db *DB) Close() error {
return db.backend.Close()
}
func CodeHash(code []byte) common.Hash {
return crypto.Keccak256Hash(code)
}
var EmptyCodeHash = CodeHash(nil)
func (db *DB) HasCode(codeHash common.Hash) bool {
if codeHash == EmptyCodeHash {
return false
}
key := Stage1CodeKey(codeHash)
has, err := db.backend.Has(key)
if err != nil {
panic(fmt.Errorf("record-replay: error checking bytecode for codeHash %s: %v", codeHash.Hex(), err))
}
return has
}
func (db *DB) GetCode(codeHash common.Hash) []byte {
if codeHash == EmptyCodeHash {
return nil
}
key := Stage1CodeKey(codeHash)
code, err := db.backend.Get(key)
if err != nil {
panic(fmt.Errorf("record-replay: error getting code %s: %v", codeHash.Hex(), err))
}
return code
}
func (db *DB) PutCode(code []byte) {
if len(code) == 0 {
return
}
codeHash := crypto.Keccak256Hash(code)
key := Stage1CodeKey(codeHash)
err := db.backend.Put(key, code)
if err != nil {
panic(fmt.Errorf("record-replay: error putting code %s: %v", codeHash.Hex(), err))
}
}
func (db *DB) HasSubstate(block uint64, tx int) bool {
key := Stage1SubstateKey(block, tx)
has, _ := db.backend.Has(key)
return has
}
func (db *DB) GetSubstate(block uint64, tx int) *Substate {
var err error
key := Stage1SubstateKey(block, tx)
value, err := db.backend.Get(key)
if err != nil {
panic(fmt.Errorf("record-replay: error getting substate %v_%v from substate DB: %v,", block, tx, err))
}
// try decoding as substates from latest hard forks
substateRLP := SubstateRLP{}
err = rlp.DecodeBytes(value, &substateRLP)
if err != nil {
// try decoding as legacy substates between Berlin and London hard forks
berlinRLP := berlinSubstateRLP{}
err = rlp.DecodeBytes(value, &berlinRLP)
if err == nil {
substateRLP.setBerlinRLP(&berlinRLP)
}
}
if err != nil {
// try decoding as legacy substates before Berlin hard fork
legacyRLP := legacySubstateRLP{}
err = rlp.DecodeBytes(value, &legacyRLP)
if err != nil {
panic(fmt.Errorf("error decoding substateRLP %v_%v: %v", block, tx, err))
}
substateRLP.setLegacyRLP(&legacyRLP)
}
substate := Substate{}
substate.SetRLP(&substateRLP, db)
return &substate
}
func (db *DB) GetBlockSubstates(block uint64) map[int]*Substate {
var err error
txSubstate := make(map[int]*Substate)
prefix := Stage1SubstateBlockPrefix(block)
iter := db.backend.NewIterator(prefix, nil)
for iter.Next() {
key := iter.Key()
value := iter.Value()
b, tx, err := DecodeStage1SubstateKey(key)
if err != nil {
panic(fmt.Errorf("record-replay: invalid substate key found for block %v: %v", block, err))
}
if block != b {
panic(fmt.Errorf("record-replay: GetBlockSubstates(%v) iterated substates from block %v", block, b))
}
// try decoding as substates from latest hard forks
substateRLP := SubstateRLP{}
err = rlp.DecodeBytes(value, &substateRLP)
if err != nil {
// try decoding as legacy substates between Berlin and London hard forks
berlinRLP := berlinSubstateRLP{}
err = rlp.DecodeBytes(value, &berlinRLP)
if err == nil {
substateRLP.setBerlinRLP(&berlinRLP)
}
}
if err != nil {
// try decoding as legacy substates before Berlin hard fork
legacyRLP := legacySubstateRLP{}
err = rlp.DecodeBytes(value, &legacyRLP)
if err != nil {
panic(fmt.Errorf("error decoding substateRLP %v_%v: %v", block, tx, err))
}
substateRLP.setLegacyRLP(&legacyRLP)
}
substate := Substate{}
substate.SetRLP(&substateRLP, db)
txSubstate[tx] = &substate
}
iter.Release()
err = iter.Error()
if err != nil {
panic(err)
}
return txSubstate
}
func (db *DB) PutSubstate(block uint64, tx int, substate *Substate) {
var err error
// put deployed/creation code
for _, account := range substate.InputAlloc {
db.PutCode(account.Code)
}
for _, account := range substate.OutputAlloc {
db.PutCode(account.Code)
}
if msg := substate.Message; msg.To == nil {
db.PutCode(msg.Data)
}
key := Stage1SubstateKey(block, tx)
defer func() {
if err != nil {
panic(fmt.Errorf("record-replay: error putting substate %v_%v into substate DB: %v", block, tx, err))
}
}()
substateRLP := NewSubstateRLP(substate)
value, err := rlp.EncodeToBytes(substateRLP)
if err != nil {
panic(err)
}
err = db.backend.Put(key, value)
if err != nil {
panic(err)
}
}
func (db *DB) DeleteSubstate(block uint64, tx int) {
key := Stage1SubstateKey(block, tx)
err := db.backend.Delete(key)
if err != nil {
panic(err)
}
}
type Transaction struct {
Block uint64
Transaction int
Substate *Substate
}
type rawEntry struct {
key []byte
value []byte
}
func parseTransaction(db *DB, data rawEntry) *Transaction {
key := data.key
value := data.value
block, tx, err := DecodeStage1SubstateKey(data.key)
if err != nil {
panic(fmt.Errorf("record-replay: invalid substate key found: %v - issue: %v", key, err))
}
// try decoding as substates from latest hard forks
substateRLP := SubstateRLP{}
err = rlp.DecodeBytes(value, &substateRLP)
if err != nil {
// try decoding as legacy substates between Berlin and London hard forks
berlinRLP := berlinSubstateRLP{}
err = rlp.DecodeBytes(value, &berlinRLP)
if err == nil {
substateRLP.setBerlinRLP(&berlinRLP)
}
}
if err != nil {
// try decoding as legacy substates before Berlin hard fork
legacyRLP := legacySubstateRLP{}
err = rlp.DecodeBytes(value, &legacyRLP)
if err != nil {
panic(fmt.Errorf("error decoding substateRLP %v_%v: %v", block, tx, err))
}
substateRLP.setLegacyRLP(&legacyRLP)
}
substate := &Substate{}
substate.SetRLP(&substateRLP, db)
return &Transaction{
Block: block,
Transaction: tx,
Substate: substate,
}
}
func (db *DB) GetFirstSubstate() *Substate {
iter := NewSubstateIterator(0, 1)
defer iter.Release()
// start with writing first block
if iter.Next() {
return iter.Value().Substate
} else {
return nil
}
}
// GetLastSubstate returns substate of the highest transaction index in the last block
func (db *DB) GetLastSubstate() (*Substate, error) {
block, err := db.GetLastBlock()
if err != nil {
return nil, err
}
substates := db.GetBlockSubstates(block)
if len(substates) == 0 {
return nil, fmt.Errorf("block %v doesn't have any substates.", block)
}
maxTx := 0
for txIdx, _ := range substates {
if txIdx > maxTx {
maxTx = txIdx
}
}
return substates[maxTx], nil
}
// GetLastSubstate searches for last substate
func (db *DB) GetLastBlock() (uint64, error) {
zeroBytes, err := db.getLongestEncodedKeyZeroPrefixLength()
if err != nil {
return 0, err
}
var lastKeyPrefix []byte
if zeroBytes > 0 {
blockBytes := make([]byte, zeroBytes)
lastKeyPrefix = append([]byte(Stage1SubstatePrefix), blockBytes...)
} else {
lastKeyPrefix = []byte(Stage1SubstatePrefix)
}
substatePrefixSize := len([]byte(Stage1SubstatePrefix))
// binary search for biggest key
for {
nextBiggestPrefixValue, err := db.binarySearchForLastPrefixKey(lastKeyPrefix)
if err != nil {
return 0, err
}
lastKeyPrefix = append(lastKeyPrefix, nextBiggestPrefixValue)
// we have all 8 bytes of uint64 encoded block
if len(lastKeyPrefix) == (substatePrefixSize + 8) {
// full key is already found
substateBlockValue := lastKeyPrefix[substatePrefixSize:]
if len(substateBlockValue) != 8 {
return 0, fmt.Errorf("undefined behaviour in GetLastSubstate search; retrieved block bytes can't be converted")
}
return binary.BigEndian.Uint64(substateBlockValue), nil
}
}
}
func (db *DB) binarySearchForLastPrefixKey(lastKeyPrefix []byte) (byte, error) {
var min uint16 = 0
var max uint16 = 255
startIndex := make([]byte, 1)
for max-min > 1 {
searchHalf := (max + min) / 2
startIndex[0] = byte(searchHalf)
if db.HasKeyValuesFor(lastKeyPrefix, startIndex) {
min = searchHalf
} else {
max = searchHalf
}
}
// shouldn't occure
if max-min == 0 {
return 0, fmt.Errorf("undefined behaviour in GetLastSubstate search; max - min == 0")
}
startIndex[0] = byte(min)
if db.HasKeyValuesFor(lastKeyPrefix, startIndex) {
startIndex[0] = byte(max)
if db.HasKeyValuesFor(lastKeyPrefix, startIndex) {
return byte(max), nil
} else {
return byte(min), nil
}
} else {
return 0, fmt.Errorf("undefined behaviour in GetLastSubstate search")
}
}
// getLongestEncodedValue returns longest index of biggest block number to be search for in its search
func (db *DB) getLongestEncodedKeyZeroPrefixLength() (byte, error) {
var i byte
for i = 0; i < 8; i++ {
startingIndex := make([]byte, 8)
startingIndex[i] = 1
if db.HasKeyValuesFor([]byte(Stage1SubstatePrefix), startingIndex) {
return i, nil
}
}
return 0, fmt.Errorf("unable to find prefix of substate with biggest block")
}
func (db *DB) HasKeyValuesFor(prefix []byte, start []byte) bool {
iter := db.backend.NewIterator(prefix, start)
defer iter.Release()
return iter.Next()
}
type SubstateIterator struct {
db *DB
iter ethdb.Iterator
cur *Transaction
// Connections to parsing pipeline
source <-chan *Transaction
done chan<- int
}
func NewSubstateIterator(start_block uint64, num_workers int) SubstateIterator {
db := staticSubstateDB
start := BlockToBytes(start_block)
// substate prefix is already in start
iter := db.backend.NewIterator([]byte(Stage1SubstatePrefix), start)
// Create channels
done := make(chan int)
raw_data := make([]chan rawEntry, num_workers)
results := make([]chan *Transaction, num_workers)
result := make(chan *Transaction, 10)
for i := 0; i < num_workers; i++ {
raw_data[i] = make(chan rawEntry, 10)
results[i] = make(chan *Transaction, 10)
}
// Start iter => raw data stage
go func() {
defer func() {
for _, c := range raw_data {
close(c)
}
}()
step := 0
for {
if !iter.Next() {
return
}
key := make([]byte, len(iter.Key()))
copy(key, iter.Key())
value := make([]byte, len(iter.Value()))
copy(value, iter.Value())
res := rawEntry{key, value}
select {
case <-done:
return
case raw_data[step] <- res: // fall-through
}
step = (step + 1) % num_workers
}
}()
// Start raw data => parsed transaction stage (parallel)
for i := 0; i < num_workers; i++ {
id := i
go func() {
defer close(results[id])
for raw := range raw_data[id] {
results[id] <- parseTransaction(db, raw)
}
}()
}
// Start the go routine moving transactions from parsers to sink in order
go func() {
defer close(result)
step := 0
for open_producers := num_workers; open_producers > 0; {
next := <-results[step%num_workers]
if next != nil {
result <- next
} else {
open_producers--
}
step++
}
}()
return SubstateIterator{
db: db,
iter: iter,
source: result,
done: done,
}
}
func (i *SubstateIterator) Release() {
close(i.done)
// drain pipeline until the result channel is closed
for open := true; open; _, open = <-i.source {
}
i.iter.Release()
}
func (i *SubstateIterator) Next() bool {
if i.iter == nil {
return false
}
i.cur = <-i.source
return i.cur != nil
}
func (i *SubstateIterator) Value() *Transaction {
return i.cur
}