diff options
author | Péter Szilágyi <peterke@gmail.com> | 2016-02-03 16:56:21 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2016-02-03 16:56:21 +0800 |
commit | a50bccc642d079899feb8cf5781331bb10174a77 (patch) | |
tree | 7123612a7882218052972261854b5b018958a034 /trie | |
parent | fbbedb4c03e0ac386787030c8f7731d8a123d2b4 (diff) | |
parent | f3d4ce0d164f7b17a143304e2b94421573d596a4 (diff) | |
download | go-tangerine-a50bccc642d079899feb8cf5781331bb10174a77.tar go-tangerine-a50bccc642d079899feb8cf5781331bb10174a77.tar.gz go-tangerine-a50bccc642d079899feb8cf5781331bb10174a77.tar.bz2 go-tangerine-a50bccc642d079899feb8cf5781331bb10174a77.tar.lz go-tangerine-a50bccc642d079899feb8cf5781331bb10174a77.tar.xz go-tangerine-a50bccc642d079899feb8cf5781331bb10174a77.tar.zst go-tangerine-a50bccc642d079899feb8cf5781331bb10174a77.zip |
Merge pull request #2134 from karalabe/save-state-writes
core/state, trie: don't leak database writes before commit
Diffstat (limited to 'trie')
-rw-r--r-- | trie/secure_trie.go | 53 |
1 files changed, 47 insertions, 6 deletions
diff --git a/trie/secure_trie.go b/trie/secure_trie.go index caeef3c3a..be7defe83 100644 --- a/trie/secure_trie.go +++ b/trie/secure_trie.go @@ -40,9 +40,10 @@ var secureKeyPrefix = []byte("secure-key-") type SecureTrie struct { *Trie - hash hash.Hash - secKeyBuf []byte - hashKeyBuf []byte + hash hash.Hash + hashKeyBuf []byte + secKeyBuf []byte + secKeyCache map[string][]byte } // NewSecure creates a trie with an existing root node from db. @@ -59,7 +60,10 @@ func NewSecure(root common.Hash, db Database) (*SecureTrie, error) { if err != nil { return nil, err } - return &SecureTrie{Trie: trie}, nil + return &SecureTrie{ + Trie: trie, + secKeyCache: make(map[string][]byte), + }, nil } // Get returns the value for key stored in the trie. @@ -105,7 +109,7 @@ func (t *SecureTrie) TryUpdate(key, value []byte) error { if err != nil { return err } - t.Trie.db.Put(t.secKey(hk), key) + t.secKeyCache[string(hk)] = common.CopyBytes(key) return nil } @@ -119,16 +123,53 @@ func (t *SecureTrie) Delete(key []byte) { // TryDelete removes any existing value for key from the trie. // If a node was not found in the database, a MissingNodeError is returned. func (t *SecureTrie) TryDelete(key []byte) error { - return t.Trie.TryDelete(t.hashKey(key)) + hk := t.hashKey(key) + delete(t.secKeyCache, string(hk)) + return t.Trie.TryDelete(hk) } // GetKey returns the sha3 preimage of a hashed key that was // previously used to store a value. func (t *SecureTrie) GetKey(shaKey []byte) []byte { + if key, ok := t.secKeyCache[string(shaKey)]; ok { + return key + } key, _ := t.Trie.db.Get(t.secKey(shaKey)) return key } +// Commit writes all nodes and the secure hash pre-images to the trie's database. +// Nodes are stored with their sha3 hash as the key. +// +// Committing flushes nodes from memory. Subsequent Get calls will load nodes +// from the database. +func (t *SecureTrie) Commit() (root common.Hash, err error) { + return t.CommitTo(t.db) +} + +// CommitTo writes all nodes and the secure hash pre-images to the given database. +// Nodes are stored with their sha3 hash as the key. +// +// Committing flushes nodes from memory. Subsequent Get calls will load nodes from +// the trie's database. Calling code must ensure that the changes made to db are +// written back to the trie's attached database before using the trie. +func (t *SecureTrie) CommitTo(db DatabaseWriter) (root common.Hash, err error) { + if len(t.secKeyCache) > 0 { + for hk, key := range t.secKeyCache { + if err := db.Put(t.secKey([]byte(hk)), key); err != nil { + return common.Hash{}, err + } + } + t.secKeyCache = make(map[string][]byte) + } + n, err := t.hashRoot(db) + if err != nil { + return (common.Hash{}), err + } + t.root = n + return common.BytesToHash(n.(hashNode)), nil +} + func (t *SecureTrie) secKey(key []byte) []byte { t.secKeyBuf = append(t.secKeyBuf[:0], secureKeyPrefix...) t.secKeyBuf = append(t.secKeyBuf, key...) |