-
Notifications
You must be signed in to change notification settings - Fork 199
/
patriciaMerkleTrie.go
632 lines (510 loc) · 13.7 KB
/
patriciaMerkleTrie.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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
package trie
import (
"bytes"
"context"
"encoding/hex"
"fmt"
"strings"
"sync"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go-core/hashing"
"github.com/ElrondNetwork/elrond-go-core/marshal"
logger "github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/common"
"github.com/ElrondNetwork/elrond-go/dataRetriever"
"github.com/ElrondNetwork/elrond-go/storage"
)
var log = logger.GetOrCreate("trie")
var _ dataRetriever.TrieDataGetter = (*patriciaMerkleTrie)(nil)
const (
extension = iota
leaf
branch
)
// EmptyTrieHash returns the value with empty trie hash
var EmptyTrieHash = make([]byte, 32)
type patriciaMerkleTrie struct {
root node
trieStorage common.StorageManager
marshalizer marshal.Marshalizer
hasher hashing.Hasher
mutOperation sync.RWMutex
oldHashes [][]byte
oldRoot []byte
maxTrieLevelInMemory uint
chanClose chan struct{}
}
// NewTrie creates a new Patricia Merkle Trie
func NewTrie(
trieStorage common.StorageManager,
msh marshal.Marshalizer,
hsh hashing.Hasher,
maxTrieLevelInMemory uint,
) (*patriciaMerkleTrie, error) {
if check.IfNil(trieStorage) {
return nil, ErrNilTrieStorage
}
if check.IfNil(msh) {
return nil, ErrNilMarshalizer
}
if check.IfNil(hsh) {
return nil, ErrNilHasher
}
if maxTrieLevelInMemory == 0 {
return nil, ErrInvalidLevelValue
}
log.Trace("created new trie", "max trie level in memory", maxTrieLevelInMemory)
return &patriciaMerkleTrie{
trieStorage: trieStorage,
marshalizer: msh,
hasher: hsh,
oldHashes: make([][]byte, 0),
oldRoot: make([]byte, 0),
maxTrieLevelInMemory: maxTrieLevelInMemory,
chanClose: make(chan struct{}),
}, nil
}
// Get starts at the root and searches for the given key.
// If the key is present in the tree, it returns the corresponding value
func (tr *patriciaMerkleTrie) Get(key []byte) ([]byte, error) {
tr.mutOperation.Lock()
defer tr.mutOperation.Unlock()
if tr.root == nil {
return nil, nil
}
hexKey := keyBytesToHex(key)
val, err := tr.root.tryGet(hexKey, tr.trieStorage)
if err != nil {
err = fmt.Errorf("trie get error: %w, for key %v", err, hex.EncodeToString(key))
return nil, err
}
return val, nil
}
// Update updates the value at the given key.
// If the key is not in the trie, it will be added.
// If the value is empty, the key will be removed from the trie
func (tr *patriciaMerkleTrie) Update(key, value []byte) error {
tr.mutOperation.Lock()
defer tr.mutOperation.Unlock()
log.Trace("update trie", "key", hex.EncodeToString(key), "val", hex.EncodeToString(value))
hexKey := keyBytesToHex(key)
newLn, err := newLeafNode(hexKey, value, tr.marshalizer, tr.hasher)
if err != nil {
return err
}
var newRoot node
var oldHashes [][]byte
if len(value) != 0 {
if tr.root == nil {
newRoot, err = newLeafNode(hexKey, value, tr.marshalizer, tr.hasher)
if err != nil {
return err
}
tr.root = newRoot
return nil
}
if !tr.root.isDirty() {
tr.oldRoot = tr.root.getHash()
}
newRoot, oldHashes, err = tr.root.insert(newLn, tr.trieStorage)
if err != nil {
return err
}
if check.IfNil(newRoot) {
return nil
}
tr.root = newRoot
tr.oldHashes = append(tr.oldHashes, oldHashes...)
logArrayWithTrace("oldHashes after insert", "hash", oldHashes)
} else {
if tr.root == nil {
return nil
}
if !tr.root.isDirty() {
tr.oldRoot = tr.root.getHash()
}
_, newRoot, oldHashes, err = tr.root.delete(hexKey, tr.trieStorage)
if err != nil {
return err
}
tr.root = newRoot
tr.oldHashes = append(tr.oldHashes, oldHashes...)
logArrayWithTrace("oldHashes after delete", "hash", oldHashes)
}
return nil
}
// Delete removes the node that has the given key from the tree
func (tr *patriciaMerkleTrie) Delete(key []byte) error {
tr.mutOperation.Lock()
defer tr.mutOperation.Unlock()
hexKey := keyBytesToHex(key)
if tr.root == nil {
return nil
}
if !tr.root.isDirty() {
tr.oldRoot = tr.root.getHash()
}
_, newRoot, oldHashes, err := tr.root.delete(hexKey, tr.trieStorage)
if err != nil {
return err
}
tr.root = newRoot
tr.oldHashes = append(tr.oldHashes, oldHashes...)
return nil
}
// RootHash returns the hash of the root node
func (tr *patriciaMerkleTrie) RootHash() ([]byte, error) {
tr.mutOperation.Lock()
defer tr.mutOperation.Unlock()
return tr.getRootHash()
}
func (tr *patriciaMerkleTrie) getRootHash() ([]byte, error) {
if tr.root == nil {
return EmptyTrieHash, nil
}
hash := tr.root.getHash()
if hash != nil {
return hash, nil
}
err := tr.root.setRootHash()
if err != nil {
return nil, err
}
return tr.root.getHash(), nil
}
// Commit adds all the dirty nodes to the database
func (tr *patriciaMerkleTrie) Commit() error {
tr.mutOperation.Lock()
defer tr.mutOperation.Unlock()
if tr.root == nil {
return nil
}
if !tr.root.isDirty() {
return nil
}
err := tr.root.setRootHash()
if err != nil {
return err
}
tr.oldRoot = make([]byte, 0)
tr.oldHashes = make([][]byte, 0)
if log.GetLevel() == logger.LogTrace {
log.Trace("started committing trie", "trie", tr.root.getHash())
}
err = tr.root.commitDirty(0, tr.maxTrieLevelInMemory, tr.trieStorage, tr.trieStorage)
if err != nil {
return err
}
return nil
}
// Recreate returns a new trie that has the given root hash and database
func (tr *patriciaMerkleTrie) Recreate(root []byte) (common.Trie, error) {
tr.mutOperation.Lock()
defer tr.mutOperation.Unlock()
return tr.recreate(root)
}
func (tr *patriciaMerkleTrie) recreate(root []byte) (*patriciaMerkleTrie, error) {
if emptyTrie(root) {
return NewTrie(
tr.trieStorage,
tr.marshalizer,
tr.hasher,
tr.maxTrieLevelInMemory,
)
}
_, err := tr.trieStorage.Get(root)
if err != nil {
return nil, err
}
newTr, _, err := tr.recreateFromDb(root, tr.trieStorage)
if err != nil {
if err == storage.ErrDBIsClosed || strings.Contains(err.Error(), storage.ErrDBIsClosed.Error()) {
log.Debug("could not recreate", "rootHash", root, "error", err)
return nil, err
}
log.Warn("trie recreate error:", "error", err, "root", hex.EncodeToString(root))
return nil, err
}
return newTr, nil
}
// String outputs a graphical view of the trie. Mainly used in tests/debugging
func (tr *patriciaMerkleTrie) String() string {
writer := bytes.NewBuffer(make([]byte, 0))
if tr.root == nil {
_, _ = fmt.Fprintln(writer, "*** EMPTY TRIE ***")
} else {
tr.root.print(writer, 0, tr.trieStorage)
}
return writer.String()
}
// IsInterfaceNil returns true if there is no value under the interface
func (tr *patriciaMerkleTrie) IsInterfaceNil() bool {
return tr == nil
}
func emptyTrie(root []byte) bool {
if len(root) == 0 {
return true
}
if bytes.Equal(root, EmptyTrieHash) {
return true
}
return false
}
// GetObsoleteHashes resets the oldHashes and oldRoot variables and returns the old hashes
func (tr *patriciaMerkleTrie) GetObsoleteHashes() [][]byte {
tr.mutOperation.Lock()
oldHashes := tr.oldHashes
logArrayWithTrace("old trie hash", "hash", oldHashes)
tr.mutOperation.Unlock()
return oldHashes
}
// GetDirtyHashes returns all the dirty hashes from the trie
func (tr *patriciaMerkleTrie) GetDirtyHashes() (common.ModifiedHashes, error) {
tr.mutOperation.Lock()
defer tr.mutOperation.Unlock()
if tr.root == nil {
return nil, nil
}
err := tr.root.setRootHash()
if err != nil {
return nil, err
}
dirtyHashes := make(common.ModifiedHashes)
err = tr.root.getDirtyHashes(dirtyHashes)
if err != nil {
return nil, err
}
logMapWithTrace("new trie hash", "hash", dirtyHashes)
return dirtyHashes, nil
}
func (tr *patriciaMerkleTrie) recreateFromDb(rootHash []byte, tsm common.StorageManager) (*patriciaMerkleTrie, snapshotNode, error) {
newTr, err := NewTrie(
tsm,
tr.marshalizer,
tr.hasher,
tr.maxTrieLevelInMemory,
)
if err != nil {
return nil, nil, err
}
newRoot, err := getNodeFromDBAndDecode(rootHash, tsm, tr.marshalizer, tr.hasher)
if err != nil {
return nil, nil, err
}
newRoot.setGivenHash(rootHash)
newTr.root = newRoot
return newTr, newRoot, nil
}
// GetSerializedNode returns the serialized node (if existing) provided the node's hash
func (tr *patriciaMerkleTrie) GetSerializedNode(hash []byte) ([]byte, error) {
tr.mutOperation.Lock()
defer tr.mutOperation.Unlock()
log.Trace("GetSerializedNode", "hash", hash)
return tr.trieStorage.Get(hash)
}
// GetSerializedNodes returns a batch of serialized nodes from the trie, starting from the given hash
func (tr *patriciaMerkleTrie) GetSerializedNodes(rootHash []byte, maxBuffToSend uint64) ([][]byte, uint64, error) {
tr.mutOperation.Lock()
defer tr.mutOperation.Unlock()
log.Trace("GetSerializedNodes", "rootHash", rootHash)
size := uint64(0)
newTr, _, err := tr.recreateFromDb(rootHash, tr.trieStorage)
if err != nil {
return nil, 0, err
}
it, err := NewIterator(newTr)
if err != nil {
return nil, 0, err
}
encNode, err := it.MarshalizedNode()
if err != nil {
return nil, 0, err
}
nodes := make([][]byte, 0)
nodes = append(nodes, encNode)
size += uint64(len(encNode))
for it.HasNext() {
err = it.Next()
if err != nil {
return nil, 0, err
}
encNode, err = it.MarshalizedNode()
if err != nil {
return nil, 0, err
}
if size+uint64(len(encNode)) > maxBuffToSend {
return nodes, 0, nil
}
nodes = append(nodes, encNode)
size += uint64(len(encNode))
}
remainingSpace := maxBuffToSend - size
return nodes, remainingSpace, nil
}
// GetAllLeavesOnChannel adds all the trie leaves to the given channel
func (tr *patriciaMerkleTrie) GetAllLeavesOnChannel(
leavesChannel chan core.KeyValueHolder,
ctx context.Context,
rootHash []byte,
) error {
tr.mutOperation.RLock()
newTrie, err := tr.recreate(rootHash)
if err != nil {
tr.mutOperation.RUnlock()
close(leavesChannel)
return err
}
if check.IfNil(newTrie) || newTrie.root == nil {
tr.mutOperation.RUnlock()
close(leavesChannel)
return nil
}
tr.trieStorage.EnterPruningBufferingMode()
tr.mutOperation.RUnlock()
go func() {
err = newTrie.root.getAllLeavesOnChannel(
leavesChannel,
[]byte{},
tr.trieStorage,
tr.marshalizer,
tr.chanClose,
ctx,
)
if err != nil {
log.Error("could not get all trie leaves: ", "error", err)
}
tr.mutOperation.Lock()
tr.trieStorage.ExitPruningBufferingMode()
tr.mutOperation.Unlock()
close(leavesChannel)
}()
return nil
}
// GetAllHashes returns all the hashes from the trie
func (tr *patriciaMerkleTrie) GetAllHashes() ([][]byte, error) {
tr.mutOperation.Lock()
defer tr.mutOperation.Unlock()
hashes := make([][]byte, 0)
if tr.root == nil {
return hashes, nil
}
err := tr.root.setRootHash()
if err != nil {
return nil, err
}
hashes, err = tr.root.getAllHashes(tr.trieStorage)
if err != nil {
return nil, err
}
return hashes, nil
}
func logArrayWithTrace(message string, paramName string, hashes [][]byte) {
if log.GetLevel() == logger.LogTrace {
for _, hash := range hashes {
log.Trace(message, paramName, hash)
}
}
}
func logMapWithTrace(message string, paramName string, hashes common.ModifiedHashes) {
if log.GetLevel() == logger.LogTrace {
for key := range hashes {
log.Trace(message, paramName, []byte(key))
}
}
}
// GetProof computes a Merkle proof for the node that is present at the given key
func (tr *patriciaMerkleTrie) GetProof(key []byte) ([][]byte, []byte, error) {
tr.mutOperation.Lock()
defer tr.mutOperation.Unlock()
if tr.root == nil {
return nil, nil, ErrNilNode
}
var proof [][]byte
hexKey := keyBytesToHex(key)
currentNode := tr.root
err := currentNode.setRootHash()
if err != nil {
return nil, nil, err
}
for {
encodedNode, errGet := currentNode.getEncodedNode()
if errGet != nil {
return nil, nil, errGet
}
proof = append(proof, encodedNode)
value := currentNode.getValue()
currentNode, hexKey, errGet = currentNode.getNext(hexKey, tr.trieStorage)
if errGet != nil {
return nil, nil, errGet
}
if currentNode == nil {
return proof, value, nil
}
}
}
// VerifyProof verifies the given Merkle proof
func (tr *patriciaMerkleTrie) VerifyProof(rootHash []byte, key []byte, proof [][]byte) (bool, error) {
tr.mutOperation.Lock()
defer tr.mutOperation.Unlock()
wantHash := rootHash
key = keyBytesToHex(key)
for _, encodedNode := range proof {
if encodedNode == nil {
return false, nil
}
hash := tr.hasher.Compute(string(encodedNode))
if !bytes.Equal(wantHash, hash) {
return false, nil
}
n, errDecode := decodeNode(encodedNode, tr.marshalizer, tr.hasher)
if errDecode != nil {
return false, errDecode
}
var proofVerified bool
proofVerified, wantHash, key = n.getNextHashAndKey(key)
if proofVerified {
return true, nil
}
}
return false, nil
}
// GetNumNodes will return the trie nodes statistics DTO
func (tr *patriciaMerkleTrie) GetNumNodes() common.NumNodesDTO {
tr.mutOperation.Lock()
defer tr.mutOperation.Unlock()
n := tr.root
if check.IfNil(n) {
return common.NumNodesDTO{}
}
return n.getNumNodes()
}
// GetStorageManager returns the storage manager for the trie
func (tr *patriciaMerkleTrie) GetStorageManager() common.StorageManager {
tr.mutOperation.Lock()
defer tr.mutOperation.Unlock()
return tr.trieStorage
}
// GetOldRoot returns the rootHash of the trie before the latest changes
func (tr *patriciaMerkleTrie) GetOldRoot() []byte {
tr.mutOperation.Lock()
defer tr.mutOperation.Unlock()
return tr.oldRoot
}
// Close stops all the active goroutines started by the trie
func (tr *patriciaMerkleTrie) Close() error {
tr.mutOperation.Lock()
defer tr.mutOperation.Unlock()
if !isChannelClosed(tr.chanClose) {
close(tr.chanClose)
}
return nil
}
func isChannelClosed(ch chan struct{}) bool {
select {
case <-ch:
return true
default:
}
return false
}