Skip to content

Commit

Permalink
implement storageexist
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Ballet <3272758+gballet@users.noreply.github.com>
  • Loading branch information
gballet committed Nov 5, 2024
1 parent 544b460 commit f4dcec5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
7 changes: 7 additions & 0 deletions core/state/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ func (s *stateObject) GetState(key common.Hash) common.Hash {
return s.GetCommittedState(key)
}

func (s *stateObject) GetOriginState(key common.Hash) common.Hash {
if value, cached := s.originStorage[key]; cached {
return value
}
return common.Hash{}
}

// GetCommittedState retrieves a value from the committed account storage trie.
func (s *stateObject) GetCommittedState(key common.Hash) common.Hash {
// If we have a pending write or clean cached, return that
Expand Down
17 changes: 17 additions & 0 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,23 @@ func (s *StateDB) SubRefund(gas uint64) {
func (s *StateDB) Exist(addr common.Address) bool {
return s.getStateObject(addr) != nil
}
func (s *StateDB) StorageExist(addr common.Address, slot common.Hash) bool {
so := s.getStateObject(addr)
if so == nil {
return false
}
val := so.GetOriginState(slot)
if val == (common.Hash{}) {
// We got a 0, check if there was something in the tree
vtr := s.GetTrie().(*trie.VerkleTrie)
v, err := vtr.GetStorage(addr, slot[:])
if err != nil {
panic(err)
}
return v == nil
}
return false
}

// Empty returns whether the state object is either non-existent
// or empty according to the EIP161 specification (balance = nonce = code = 0)
Expand Down
1 change: 1 addition & 0 deletions core/vm/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type StateDB interface {
// Exist reports whether the given account exists in state.
// Notably this should also return true for self-destructed accounts.
Exist(common.Address) bool
StorageExist(common.Address, common.Hash) bool
// Empty returns whether the given account is empty. Empty
// is defined according to EIP161 (balance = nonce = code = 0).
Empty(common.Address) bool
Expand Down
4 changes: 3 additions & 1 deletion core/vm/operations_verkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import (
)

func gasSStore4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
return evm.Accesses.TouchSlotAndChargeGas(contract.Address().Bytes(), common.Hash(stack.peek().Bytes32()), true, true /* XXX needs a missing API */, contract.Gas, true), nil
addr := contract.Address()
slot := common.Hash(stack.peek().Bytes32())
return evm.Accesses.TouchSlotAndChargeGas(addr.Bytes(), slot, true, evm.StateDB.StorageExist(addr, slot), contract.Gas, true), nil
}

func gasSLoad4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
Expand Down

0 comments on commit f4dcec5

Please sign in to comment.