-
Notifications
You must be signed in to change notification settings - Fork 9
/
smt_tools.go
105 lines (96 loc) · 2.77 KB
/
smt_tools.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
/**
* @file
* @copyright defined in aergo/LICENSE.txt
*/
package trie
// The Package Trie implements a sparse merkle trie.
import (
"bytes"
"fmt"
)
// Get fetches the value of a key by going down the current trie root.
func (s *SMT) Get(key []byte) ([]byte, error) {
s.lock.RLock()
defer s.lock.RUnlock()
s.atomicUpdate = false
return s.get(s.Root, key, nil, 0, s.TrieHeight)
}
// get fetches the value of a key given a trie root
func (s *SMT) get(root []byte, key []byte, batch [][]byte, iBatch, height int) ([]byte, error) {
if len(root) == 0 {
return nil, nil
}
if height == 0 {
return root[:HashLength], nil
}
// Fetch the children of the node
batch, iBatch, lnode, rnode, isShortcut, err := s.loadChildren(root, height, iBatch, batch)
if err != nil {
return nil, err
}
if isShortcut {
if bytes.Equal(lnode[:HashLength], key) {
return rnode[:HashLength], nil
}
return nil, nil
}
if bitIsSet(key, s.TrieHeight-height) {
return s.get(rnode, key, batch, 2*iBatch+2, height-1)
}
return s.get(lnode, key, batch, 2*iBatch+1, height-1)
}
// DefaultHash is a getter for the defaultHashes array
func (s *SMT) DefaultHash(height int) []byte {
return s.defaultHashes[height]
}
// CheckRoot returns true if the root exists in Database.
func (s *SMT) CheckRoot(root []byte) bool {
s.db.lock.RLock()
dbval := s.db.store.Get(root)
s.db.lock.RUnlock()
if len(dbval) != 0 {
return true
}
return false
}
// Commit stores the updated nodes to disk
// Commit should be called for every block otherwise past tries are not recorded and it is not possible to revert to them
func (s *SMT) Commit() error {
s.lock.RLock()
defer s.lock.RUnlock()
if s.db.store == nil {
return fmt.Errorf("DB not connected to trie")
}
// Commit the new nodes to database, clear updatedNodes and store the Root in history for reverts.
if !s.atomicUpdate {
if len(s.pastTries) >= maxPastTries {
copy(s.pastTries, s.pastTries[1:])
s.pastTries[len(s.pastTries)-1] = s.Root
} else {
s.pastTries = append(s.pastTries, s.Root)
}
}
s.db.commit()
s.db.updatedNodes = make(map[Hash][][]byte)
s.prevRoot = s.Root
return nil
}
// Stash rolls back the changes made by previous updates made without commit
// and loads the cache from before the rollback.
func (s *SMT) Stash() {
// Making a temporary liveCache requires it to be copied, so it's quicker
// to just load the cache if a block state root was incorrect.
s.Root = s.prevRoot
s.db.liveCache = make(map[Hash][][]byte)
s.db.updatedNodes = make(map[Hash][][]byte)
// TODO add LoadCache()
// also stash past tries created by Atomic update
for i := len(s.pastTries) - 1; i >= 0; i-- {
if bytes.Equal(s.pastTries[i], s.Root) {
break
} else {
// remove from past tries
s.pastTries = s.pastTries[:len(s.pastTries)-1]
}
}
}