Skip to content

Commit 9a3ffad

Browse files
authored
Problem: no efficient GetStorageRoot impl in cosmos/evm (#9)
Solution: - create a new method IsStorageEmpty to use in core evm
1 parent 2fc7571 commit 9a3ffad

File tree

4 files changed

+13
-2
lines changed

4 files changed

+13
-2
lines changed

core/state/statedb.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,12 @@ func (s *StateDB) GetStorageRoot(addr common.Address) common.Hash {
335335
return common.Hash{}
336336
}
337337

338+
// IsStorageEmpty returns if the contract storage is empty
339+
func (s *StateDB) IsStorageEmpty(addr common.Address) bool {
340+
storageRoot := s.GetStorageRoot(addr)
341+
return storageRoot == (common.Hash{}) || storageRoot == types.EmptyRootHash
342+
}
343+
338344
// TxIndex returns the current transaction index set by SetTxContext.
339345
func (s *StateDB) TxIndex() int {
340346
return s.txIndex

core/state/statedb_hooked.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ func (s *hookedStateDB) GetStorageRoot(addr common.Address) common.Hash {
9797
return s.inner.GetStorageRoot(addr)
9898
}
9999

100+
func (s *hookedStateDB) IsStorageEmpty(addr common.Address) bool {
101+
return s.inner.IsStorageEmpty(addr)
102+
}
103+
100104
func (s *hookedStateDB) GetTransientState(addr common.Address, key common.Hash) common.Hash {
101105
return s.inner.GetTransientState(addr, key)
102106
}

core/vm/evm.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,10 +499,10 @@ func (evm *EVM) create(caller common.Address, code []byte, gas uint64, value *ui
499499
// - the code is non-empty
500500
// - the storage is non-empty
501501
contractHash := evm.StateDB.GetCodeHash(address)
502-
storageRoot := evm.StateDB.GetStorageRoot(address)
502+
isStorageEmpty := evm.StateDB.IsStorageEmpty(address)
503503
if evm.StateDB.GetNonce(address) != 0 ||
504504
(contractHash != (common.Hash{}) && contractHash != types.EmptyCodeHash) || // non-empty code
505-
(storageRoot != (common.Hash{}) && storageRoot != types.EmptyRootHash) { // non-empty storage
505+
!isStorageEmpty { // non-empty storage
506506
if evm.Config.Tracer != nil && evm.Config.Tracer.OnGasChange != nil {
507507
evm.Config.Tracer.OnGasChange(gas, 0, tracing.GasChangeCallFailedExecution)
508508
}

core/vm/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ type StateDB interface {
5454
GetState(common.Address, common.Hash) common.Hash
5555
SetState(common.Address, common.Hash, common.Hash) common.Hash
5656
GetStorageRoot(addr common.Address) common.Hash
57+
IsStorageEmpty(addr common.Address) bool
5758

5859
GetTransientState(addr common.Address, key common.Hash) common.Hash
5960
SetTransientState(addr common.Address, key, value common.Hash)

0 commit comments

Comments
 (0)