diff options
author | obscuren <geffobscura@gmail.com> | 2014-04-29 18:36:27 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-04-29 18:36:27 +0800 |
commit | 38d6b67b5cfbfb63620a244ea01b5b534917128f (patch) | |
tree | 42b4f55e4cd5c2f2a2c28f8551d8b92686abf567 /ethutil | |
parent | 5516efdfa0494e028fc3649e4a38da81c56ed598 (diff) | |
download | dexon-38d6b67b5cfbfb63620a244ea01b5b534917128f.tar.gz dexon-38d6b67b5cfbfb63620a244ea01b5b534917128f.tar.zst dexon-38d6b67b5cfbfb63620a244ea01b5b534917128f.zip |
Fixed state problem
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() { |