@@ -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.
209220func (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.
216229func (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.
224239func (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
0 commit comments