Fixed state problem
This commit is contained in:
@ -73,3 +73,13 @@ func BinaryLength(num int) int {
|
||||
|
||||
return 1 + BinaryLength(num>>8)
|
||||
}
|
||||
|
||||
// Copy bytes
|
||||
//
|
||||
// Returns an exact copy of the provided bytes
|
||||
func CopyBytes(b []byte) (copiedBytes []byte) {
|
||||
copiedBytes = make([]byte, len(b))
|
||||
copy(copiedBytes, b)
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -119,14 +119,29 @@ type Trie struct {
|
||||
cache *Cache
|
||||
}
|
||||
|
||||
func copyRoot(root interface{}) interface{} {
|
||||
var prevRootCopy interface{}
|
||||
if b, ok := root.([]byte); ok {
|
||||
prevRootCopy = CopyBytes(b)
|
||||
} else {
|
||||
prevRootCopy = root
|
||||
}
|
||||
|
||||
return prevRootCopy
|
||||
}
|
||||
|
||||
func NewTrie(db Database, Root interface{}) *Trie {
|
||||
return &Trie{cache: NewCache(db), Root: Root, prevRoot: Root}
|
||||
// Make absolute sure the root is copied
|
||||
r := copyRoot(Root)
|
||||
p := copyRoot(Root)
|
||||
|
||||
return &Trie{cache: NewCache(db), Root: r, prevRoot: p}
|
||||
}
|
||||
|
||||
// Save the cached value to the database.
|
||||
func (t *Trie) Sync() {
|
||||
t.cache.Commit()
|
||||
t.prevRoot = t.Root
|
||||
t.prevRoot = copyRoot(t.Root)
|
||||
}
|
||||
|
||||
func (t *Trie) Undo() {
|
||||
|
Reference in New Issue
Block a user