Skip to content

Commit

Permalink
add test ensuring genericCall charges for warm/cold access
Browse files Browse the repository at this point in the history
  • Loading branch information
facuMH committed Sep 13, 2024
1 parent 313a711 commit 9358579
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
1 change: 0 additions & 1 deletion go/interpreter/lfvm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,6 @@ func neededMemorySize(c *context, offset, size *uint256.Int) (uint64, error) {

func getAccessCost(accessStatus tosca.AccessStatus) tosca.Gas {
// EIP-2929 says that cold access cost is 2600 and warm is 100.
// however, the warm access cost is charged as part of the base gas cost.
// (https://eips.ethereum.org/EIPS/eip-2929)
if accessStatus == tosca.ColdAccess {
return tosca.Gas(2600)
Expand Down
38 changes: 38 additions & 0 deletions go/interpreter/lfvm/instructions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -918,3 +918,41 @@ func TestCall_ChargesNothingForColdAccessBeforeBerlin(t *testing.T) {
t.Errorf("unexpected status, wanted RUNNING, got %v", ctxt.status)
}
}

func TestCall_ChargesForAccessAfterBerlin(t *testing.T) {

for _, accessStatus := range []tosca.AccessStatus{tosca.WarmAccess, tosca.ColdAccess} {

ctrl := gomock.NewController(t)
runContext := tosca.NewMockRunContext(ctrl)
runContext.EXPECT().AccessAccount(gomock.Any()).Return(accessStatus)
runContext.EXPECT().Call(tosca.Call, gomock.Any()).Return(tosca.CallResult{}, nil)
delta := tosca.Gas(1)
ctxt := context{
params: tosca.Parameters{
BlockParameters: tosca.BlockParameters{
Revision: tosca.R09_Berlin,
},
},
stack: NewStack(),
memory: NewMemory(),
context: runContext,
gas: 2600 + delta,
}
ctxt.stack.stackPointer = 8

genericCall(&ctxt, tosca.Call)

want := tosca.Gas(delta)
if accessStatus == tosca.WarmAccess {
want = 2500 + delta
}
if ctxt.gas != want {
t.Errorf("unexpected gas cost, wanted %v, got %v", want, ctxt.gas)
}

if ctxt.status != statusRunning {
t.Errorf("unexpected status, wanted RUNNING, got %v", ctxt.status)
}
}
}

0 comments on commit 9358579

Please sign in to comment.