diff options
Diffstat (limited to 'ethutil')
-rw-r--r-- | ethutil/bytes.go | 10 | ||||
-rw-r--r-- | ethutil/trie.go | 19 |
2 files changed, 27 insertions, 2 deletions
diff --git a/ethutil/bytes.go b/ethutil/bytes.go index 957fa254a..500368017 100644 --- a/ethutil/bytes.go +++ b/ethutil/bytes.go @@ -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 +} diff --git a/ethutil/trie.go b/ethutil/trie.go index c67f750bc..4d088ccff 100644 --- a/ethutil/trie.go +++ b/ethutil/trie.go @@ -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() { |