-
Notifications
You must be signed in to change notification settings - Fork 9
/
smt_revert.go
161 lines (151 loc) · 4.09 KB
/
smt_revert.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
/**
* @file
* @copyright defined in aergo/LICENSE.txt
*/
package trie
import (
"bytes"
"fmt"
)
// Revert rewinds the state tree to a previous version
// All the nodes (subtree roots and values) reverted are deleted from the database.
func (s *SMT) Revert(toOldRoot []byte) error {
s.lock.Lock()
defer s.lock.Unlock()
if bytes.Equal(s.Root, toOldRoot) {
return fmt.Errorf("Trying to revert to the same root %x", s.Root)
}
//check if toOldRoot is in s.pastTries
canRevert := false
toIndex := 0
for i, r := range s.pastTries {
if bytes.Equal(r, toOldRoot) {
canRevert = true
toIndex = i
break
}
}
if !canRevert {
return fmt.Errorf("The root is not contained in the cached tries, too old to be reverted : %x", s.Root)
}
// For every node of toOldRoot, compare it to the equivalent node in other pasttries between toOldRoot and current s.Root. If a node is different, delete the one from pasttries
s.db.nodesToRevert = make([][]byte, 0)
for i := toIndex + 1; i < len(s.pastTries); i++ {
ch := make(chan error, 1)
s.maybeDeleteSubTree(toOldRoot, s.pastTries[i], s.TrieHeight, 0, nil, nil, ch)
err := <-ch
if err != nil {
return err
}
}
// NOTE The tx interface doesnt handle ErrTxnTooBig
txn := s.db.store.NewTx(true)
//for _, key := range toBeDeleted {
for _, key := range s.db.nodesToRevert {
txn.Delete(key[:HashLength])
}
txn.Commit()
s.pastTries = s.pastTries[:toIndex+1]
s.Root = toOldRoot
// load default hashes in live cache
s.db.liveCache = make(map[Hash][][]byte)
return nil
}
// maybeDeleteSubTree compares the subtree nodes of 2 tries and keeps only the older one
func (s *SMT) maybeDeleteSubTree(original, maybeDelete []byte, height, iBatch int, batch, batch2 [][]byte, ch chan<- (error)) {
if bytes.Equal(original, maybeDelete) || len(maybeDelete) == 0 {
ch <- nil
return
}
if height == 0 {
ch <- nil
return
}
// if this point os reached, then the root of the batch is same
// so the batch is also same.
batch, iBatch, lnode, rnode, isShortcut, lerr := s.loadChildren(original, height, iBatch, batch)
if lerr != nil {
ch <- lerr
return
}
batch2, _, lnode2, rnode2, isShortcut2, rerr := s.loadChildren(maybeDelete, height, iBatch, batch2)
if rerr != nil {
ch <- rerr
return
}
if isShortcut != isShortcut2 {
if isShortcut {
ch1 := make(chan error, 1)
s.deleteSubTree(maybeDelete, height, iBatch, batch2, ch1)
err := <-ch1
if err != nil {
ch <- err
return
}
} else {
s.maybeDeleteRevertedNode(maybeDelete, iBatch)
}
} else {
if isShortcut {
if !bytes.Equal(lnode, lnode2) || !bytes.Equal(rnode, rnode2) {
s.maybeDeleteRevertedNode(maybeDelete, iBatch)
}
} else {
// Delete subtree if not equal
s.maybeDeleteRevertedNode(maybeDelete, iBatch)
ch1 := make(chan error, 1)
ch2 := make(chan error, 1)
go s.maybeDeleteSubTree(lnode, lnode2, height-1, 2*iBatch+1, batch, batch2, ch1)
go s.maybeDeleteSubTree(rnode, rnode2, height-1, 2*iBatch+2, batch, batch2, ch2)
err1 := <-ch1
err2 := <-ch2
if err1 != nil {
ch <- err1
return
}
if err2 != nil {
ch <- err2
return
}
}
}
ch <- nil
}
// deleteSubTree deletes all the nodes contained in a tree
func (s *SMT) deleteSubTree(root []byte, height, iBatch int, batch [][]byte, ch chan<- (error)) {
if height == 0 || len(root) == 0 {
ch <- nil
return
}
batch, iBatch, lnode, rnode, isShortcut, err := s.loadChildren(root, height, iBatch, batch)
if err != nil {
ch <- err
return
}
if !isShortcut {
ch1 := make(chan error, 1)
ch2 := make(chan error, 1)
go s.deleteSubTree(lnode, height-1, 2*iBatch+1, batch, ch1)
go s.deleteSubTree(rnode, height-1, 2*iBatch+2, batch, ch2)
lerr := <-ch1
rerr := <-ch2
if lerr != nil {
ch <- lerr
return
}
if rerr != nil {
ch <- rerr
return
}
}
s.maybeDeleteRevertedNode(root, iBatch)
ch <- nil
}
// maybeDeleteRevertedNode adds the node to updatedNodes to be reverted
func (s *SMT) maybeDeleteRevertedNode(root []byte, iBatch int) {
if iBatch == 0 {
s.db.revertMux.Lock()
s.db.nodesToRevert = append(s.db.nodesToRevert, root)
s.db.revertMux.Unlock()
}
}