@@ -144,7 +144,9 @@ func (t *StateTrie) UpdateStorage(_ common.Address, key, value []byte) error {
144144 if err != nil {
145145 return err
146146 }
147- t .getSecKeyCache ()[common .Hash (hk )] = common .CopyBytes (key )
147+ if t .preimages != nil {
148+ t .getSecKeyCache ()[common .Hash (hk )] = common .CopyBytes (key )
149+ }
148150 return nil
149151}
150152
@@ -159,7 +161,9 @@ func (t *StateTrie) TryUpdateAccount(key common.Address, acc *types.StateAccount
159161 if err := t .trie .TryUpdate (hk , data ); err != nil {
160162 return err
161163 }
162- t .getSecKeyCache ()[common .Hash (hk )] = common .CopyBytes (key .Bytes ())
164+ if t .preimages != nil {
165+ t .getSecKeyCache ()[common .Hash (hk )] = common .CopyBytes (key .Bytes ())
166+ }
163167 return nil
164168}
165169
@@ -193,7 +197,9 @@ func (t *StateTrie) TryUpdate(key, value []byte) error {
193197 if err != nil {
194198 return err
195199 }
196- t .getSecKeyCache ()[common .Hash (hk )] = common .CopyBytes (key )
200+ if t .preimages != nil {
201+ t .getSecKeyCache ()[common .Hash (hk )] = common .CopyBytes (key )
202+ }
197203 return nil
198204}
199205
@@ -208,26 +214,30 @@ func (t *StateTrie) Delete(key []byte) {
208214// If a node was not found in the database, a MissingNodeError is returned.
209215func (t * StateTrie ) TryDelete (key []byte ) error {
210216 hk := crypto .Keccak256 (key )
211- delete (t .getSecKeyCache (), common .Hash (hk ))
217+ if t .preimages != nil {
218+ delete (t .getSecKeyCache (), common .Hash (hk ))
219+ }
212220 return t .trie .TryDelete (hk )
213221}
214222
215223// TryDeleteAccount abstracts an account deletion from the trie.
216224func (t * StateTrie ) TryDeleteAccount (key []byte ) error {
217225 hk := crypto .Keccak256 (key )
218- delete (t .getSecKeyCache (), common .Hash (hk ))
226+ if t .preimages != nil {
227+ delete (t .getSecKeyCache (), common .Hash (hk ))
228+ }
219229 return t .trie .TryDelete (hk )
220230}
221231
222232// GetKey returns the sha3 preimage of a hashed key that was
223233// previously used to store a value.
224234func (t * StateTrie ) GetKey (shaKey []byte ) []byte {
225- if key , ok := t .getSecKeyCache ()[common .Hash (shaKey )]; ok {
226- return key
227- }
228235 if t .preimages == nil {
229236 return nil
230237 }
238+ if key , ok := t .getSecKeyCache ()[common .Hash (shaKey )]; ok {
239+ return key
240+ }
231241 return t .preimages .preimage (common .BytesToHash (shaKey ))
232242}
233243
0 commit comments