Skip to content

Commit a7c40f5

Browse files
authored
Merge pull request #2384 from CortexFoundation/dev
store cache only when preimage is non-nil
2 parents b92bd59 + 9a47bfe commit a7c40f5

3 files changed

Lines changed: 42 additions & 11 deletions

File tree

trie/database.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,19 @@ func (db *Database) Preimage(hash common.Hash) []byte {
541541
return db.preimages.preimage(hash)
542542
}
543543

544+
// InsertPreimage writes pre-images of trie node to the preimage store.
545+
func (db *Database) InsertPreimage(preimages map[common.Hash][]byte) {
546+
if db.preimages == nil {
547+
return
548+
}
549+
db.preimages.insertPreimage(preimages)
550+
}
551+
552+
// PreimageEnabled returns the indicator if the pre-image store is enabled.
553+
func (db *Database) PreimageEnabled() bool {
554+
return db.preimages != nil
555+
}
556+
544557
// Cap iteratively flushes old but still referenced trie nodes until the total
545558
// memory usage goes below the given threshold.
546559
//

trie/secure_trie.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@ func NewStateTrie(id *ID, db *Database) (*StateTrie, error) {
7777
if err != nil {
7878
return nil, err
7979
}
80-
return &StateTrie{trie: *trie, preimages: db.preimages}, nil
80+
81+
tr := &StateTrie{trie: *trie}
82+
if db.PreimageEnabled() {
83+
tr.preimages = db.preimages
84+
}
85+
return tr, nil
8186
}
8287

8388
// Get returns the value for key stored in the trie.
@@ -144,7 +149,9 @@ func (t *StateTrie) UpdateStorage(_ common.Address, key, value []byte) error {
144149
if err != nil {
145150
return err
146151
}
147-
t.getSecKeyCache()[common.Hash(hk)] = common.CopyBytes(key)
152+
if t.preimages != nil {
153+
t.getSecKeyCache()[common.Hash(hk)] = common.CopyBytes(key)
154+
}
148155
return nil
149156
}
150157

@@ -159,7 +166,9 @@ func (t *StateTrie) TryUpdateAccount(key common.Address, acc *types.StateAccount
159166
if err := t.trie.TryUpdate(hk, data); err != nil {
160167
return err
161168
}
162-
t.getSecKeyCache()[common.Hash(hk)] = common.CopyBytes(key.Bytes())
169+
if t.preimages != nil {
170+
t.getSecKeyCache()[common.Hash(hk)] = common.CopyBytes(key.Bytes())
171+
}
163172
return nil
164173
}
165174

@@ -193,7 +202,9 @@ func (t *StateTrie) TryUpdate(key, value []byte) error {
193202
if err != nil {
194203
return err
195204
}
196-
t.getSecKeyCache()[common.Hash(hk)] = common.CopyBytes(key)
205+
if t.preimages != nil {
206+
t.getSecKeyCache()[common.Hash(hk)] = common.CopyBytes(key)
207+
}
197208
return nil
198209
}
199210

@@ -208,26 +219,30 @@ func (t *StateTrie) Delete(key []byte) {
208219
// If a node was not found in the database, a MissingNodeError is returned.
209220
func (t *StateTrie) TryDelete(key []byte) error {
210221
hk := crypto.Keccak256(key)
211-
delete(t.getSecKeyCache(), common.Hash(hk))
222+
if t.preimages != nil {
223+
delete(t.getSecKeyCache(), common.Hash(hk))
224+
}
212225
return t.trie.TryDelete(hk)
213226
}
214227

215228
// TryDeleteAccount abstracts an account deletion from the trie.
216229
func (t *StateTrie) TryDeleteAccount(key []byte) error {
217230
hk := crypto.Keccak256(key)
218-
delete(t.getSecKeyCache(), common.Hash(hk))
231+
if t.preimages != nil {
232+
delete(t.getSecKeyCache(), common.Hash(hk))
233+
}
219234
return t.trie.TryDelete(hk)
220235
}
221236

222237
// GetKey returns the sha3 preimage of a hashed key that was
223238
// previously used to store a value.
224239
func (t *StateTrie) GetKey(shaKey []byte) []byte {
225-
if key, ok := t.getSecKeyCache()[common.Hash(shaKey)]; ok {
226-
return key
227-
}
228240
if t.preimages == nil {
229241
return nil
230242
}
243+
if key, ok := t.getSecKeyCache()[common.Hash(shaKey)]; ok {
244+
return key
245+
}
231246
return t.preimages.preimage(common.BytesToHash(shaKey))
232247
}
233248

trie/secure_trie_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ import (
2828
)
2929

3030
func newEmptySecure() *StateTrie {
31-
trie, _ := NewStateTrie(TrieID(common.Hash{}), NewDatabase(memorydb.New()))
31+
mdb := memorydb.New()
32+
db := NewDatabase(mdb)
33+
db.preimages = newPreimageStore(mdb)
34+
trie, _ := NewStateTrie(TrieID(common.Hash{}), db)
3235
return trie
3336
}
3437

@@ -91,7 +94,7 @@ func TestSecureDelete(t *testing.T) {
9194

9295
func TestSecureGetKey(t *testing.T) {
9396
trie := newEmptySecure()
94-
trie.Update([]byte("foo"), []byte("bar"))
97+
trie.TryUpdate([]byte("foo"), []byte("bar"))
9598

9699
key := []byte("foo")
97100
value := []byte("bar")

0 commit comments

Comments
 (0)