Skip to content

Commit

Permalink
remove calculateAccessCost function and move it into genericCall
Browse files Browse the repository at this point in the history
  • Loading branch information
facuMH committed Sep 6, 2024
1 parent fce7093 commit 2c6e0e6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 77 deletions.
13 changes: 0 additions & 13 deletions go/interpreter/lfvm/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,19 +457,6 @@ func gasEip2929AccountCheck(c *context, address tosca.Address) error {
return nil
}

func calculateAccessCost(address tosca.Address, revision tosca.Revision, runContext tosca.RunContext) (isWarm bool, accessCost tosca.Gas) {
isWarm = true
if revision >= tosca.R09_Berlin {
// Check slot presence in the access list
//lint:ignore SA1019 deprecated functions to be migrated in #616
isWarm = runContext.IsAddressInAccessList(address)
// The WarmStorageReadCostEIP2929 (100) is already deducted in the form of a constant cost, so
// the cost to charge for cold access, if any, is Cold - Warm
accessCost = ColdAccountAccessCostEIP2929 - WarmStorageReadCostEIP2929
}
return isWarm, accessCost
}

func gasSelfdestruct(c *context) tosca.Gas {
gas := SelfdestructGasEIP150
var address = tosca.Address(c.stack.peekN(0).Bytes20())
Expand Down
49 changes: 0 additions & 49 deletions go/interpreter/lfvm/gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

"github.com/Fantom-foundation/Tosca/go/tosca"
"github.com/holiman/uint256"
"go.uber.org/mock/gomock"
)

func TestGas_CallGasCalculation(t *testing.T) {
Expand Down Expand Up @@ -60,51 +59,3 @@ func TestGas_CallGasCalculation(t *testing.T) {
})
}
}

func TestGas_calculateAccessCost(t *testing.T) {

tests := map[string]struct {
revision tosca.Revision
warmAccess bool
coldCost tosca.Gas
}{
"istanbul": {
revision: tosca.R07_Istanbul,
warmAccess: true,
},
"berlin_warm": {
revision: tosca.R09_Berlin,
warmAccess: true,
coldCost: ColdAccountAccessCostEIP2929 - WarmStorageReadCostEIP2929,
},
"berlin_cold_enough_gas": {
revision: tosca.R09_Berlin,
warmAccess: false,
coldCost: ColdAccountAccessCostEIP2929 - WarmStorageReadCostEIP2929,
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
ctrl := gomock.NewController(t)
mockRunContext := tosca.NewMockRunContext(ctrl)
mockRunContext.EXPECT().IsAddressInAccessList(gomock.Any()).Return(test.warmAccess).AnyTimes()
mockRunContext.EXPECT().AccessAccount(gomock.Any()).Return(tosca.ColdAccess).AnyTimes()

warmGot, coldCostGot := calculateAccessCost(
tosca.Address{1},
test.revision,
mockRunContext,
)

if warmGot != test.warmAccess {
t.Errorf("unexpected warm access status, wanted %v, got %v", test.warmAccess, warmGot)
}

if coldCostGot != test.coldCost {
t.Errorf("unexpected cold cost, wanted %d, got %d", test.coldCost, coldCostGot)
}

})
}
}
26 changes: 11 additions & 15 deletions go/interpreter/lfvm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1076,16 +1076,20 @@ func genericCall(c *context, kind tosca.CallKind) {
needed_memory_size = ret_memory_size
}

isWarm, accessCost := calculateAccessCost(toAddr, c.params.Revision, c.context)
if !isWarm {
c.context.AccessAccount(toAddr)
if !c.useGas(accessCost) {
c.status = statusOutOfGas
return
baseGas := c.memory.ExpansionCosts(needed_memory_size)

accessCost := tosca.Gas(0)
if c.isAtLeast(tosca.R09_Berlin) {
// Check address presence in the access list
isWarm := c.context.AccessAccount(toAddr)
// The WarmStorageReadCostEIP2929 (100) is already deducted in the form of a constant cost, so
// the cost to charge for cold access, if any, is Cold - Warm
if !isWarm {
accessCost = ColdAccountAccessCostEIP2929 - WarmStorageReadCostEIP2929
}
}

baseGas := c.memory.ExpansionCosts(needed_memory_size)
baseGas += accessCost
checkGas := func(cost tosca.Gas) bool {
return 0 <= cost && cost <= c.gas
}
Expand Down Expand Up @@ -1115,14 +1119,6 @@ func genericCall(c *context, kind tosca.CallKind) {
}

cost := callGas(c.gas, baseGas, provided_gas)
if !isWarm {
// In case of a cold access, we temporarily add the cold charge back, and also
// add it to the returned gas. By adding it to the return, it will be charged
// outside of this function, as part of the dynamic gas, and that will make it
// also become correctly reported to tracers.
c.gas += accessCost
baseGas += accessCost
}
if !c.useGas(baseGas + cost) {
return
}
Expand Down

0 comments on commit 2c6e0e6

Please sign in to comment.