Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement FILL_COSTS on top of devnet7 #524

Draft
wants to merge 3 commits into
base: kaustinen-with-shapella
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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{}
}
Comment on lines +170 to +175
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be somehow better signal that originStorage is a cache and not a fully reliable source of truth? Maybe renaming to originStorageCache and also rename the method to GetOriginStateCache or something?

I don't like the rename to GetOriginStateCache but L309 of core/state/statedb.go was quite surprising to me, and then I realized it's because it's only asking for a cache.

And even a more important reason to be super-clear about this GetOriginState being a cache, is that this method is public. If I see that name in an API and returns common.Hash{} I'd assume it doesn't exist, not that "potentially I don't know".


// 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
8 changes: 5 additions & 3 deletions core/state_processor_test.go
jsign marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -507,9 +507,11 @@ func TestProcessVerkle(t *testing.T) {
contractCreationCost := intrinsicContractCreationGas +
params.WitnessChunkReadCost + params.WitnessChunkWriteCost + params.WitnessBranchReadCost + params.WitnessBranchWriteCost + params.WitnessChunkFillCost + /* creation */
params.WitnessChunkReadCost + params.WitnessChunkWriteCost + params.WitnessChunkFillCost + /* creation with value */
739 /* execution costs */
params.WitnessChunkReadCost + params.WitnessChunkWriteCost + params.WitnessChunkFillCost + /* code chunk #0 */
39 /* execution costs */
codeWithExtCodeCopyGas := intrinsicCodeWithExtCodeCopyGas +
params.WitnessChunkReadCost + params.WitnessChunkWriteCost + params.WitnessBranchReadCost + params.WitnessBranchWriteCost + params.WitnessChunkFillCost + /* creation (tx) */
params.WitnessChunkReadCost + params.WitnessChunkWriteCost + params.WitnessChunkFillCost + /* write code hash */
params.WitnessChunkReadCost + params.WitnessChunkWriteCost + params.WitnessBranchReadCost + params.WitnessBranchWriteCost + params.WitnessChunkFillCost + /* creation (CREATE at pc=0x20) */
params.WitnessChunkReadCost + params.WitnessChunkWriteCost + params.WitnessChunkFillCost + /* write code hash */
params.WitnessChunkReadCost + params.WitnessChunkWriteCost + params.WitnessChunkFillCost + /* code chunk #0 */
Expand All @@ -520,7 +522,7 @@ func TestProcessVerkle(t *testing.T) {
params.WitnessChunkReadCost + params.WitnessChunkWriteCost + params.WitnessChunkFillCost + /* code chunk #5 */
params.WitnessChunkReadCost + /* SLOAD in constructor */
params.WitnessChunkWriteCost + params.WitnessChunkFillCost + /* SSTORE in constructor */
params.WitnessChunkReadCost + params.WitnessChunkWriteCost + params.WitnessBranchReadCost + params.WitnessBranchWriteCost + /* creation (CREATE at PC=0x121) */
params.WitnessChunkReadCost + params.WitnessChunkWriteCost + params.WitnessBranchReadCost + params.WitnessBranchWriteCost + params.WitnessChunkFillCost + /* creation (CREATE at PC=0x121) */
params.WitnessChunkReadCost + params.WitnessChunkWriteCost + params.WitnessChunkFillCost + /* write code hash */
params.WitnessChunkReadCost + params.WitnessChunkWriteCost + params.WitnessChunkFillCost + /* code chunk #0 */
params.WitnessChunkReadCost + params.WitnessChunkWriteCost + params.WitnessChunkFillCost + /* code chunk #1 */
Expand All @@ -546,7 +548,7 @@ func TestProcessVerkle(t *testing.T) {
params.WitnessChunkReadCost + params.WitnessChunkWriteCost + params.WitnessChunkFillCost + /* code chunk #12 */
params.WitnessChunkReadCost + params.WitnessChunkWriteCost + params.WitnessChunkFillCost + /* code chunk #13 */
params.WitnessChunkReadCost + params.WitnessChunkWriteCost + params.WitnessChunkFillCost + /* code chunk #14 */
4844 /* execution costs */
4144 /* execution costs */
blockGasUsagesExpected := []uint64{
txCost1*2 + txCost2,
txCost1*2 + txCost2 + contractCreationCost + codeWithExtCodeCopyGas,
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
Loading