From 5c85e262ff760975f2a09f44662618bffe724395 Mon Sep 17 00:00:00 2001 From: facuMH Date: Thu, 12 Sep 2024 15:41:11 +0200 Subject: [PATCH] add test ensuring genericCall charges for warm/cold access --- go/interpreter/lfvm/instructions.go | 1 - go/interpreter/lfvm/instructions_test.go | 38 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/go/interpreter/lfvm/instructions.go b/go/interpreter/lfvm/instructions.go index 52be30e22..b565191ce 100644 --- a/go/interpreter/lfvm/instructions.go +++ b/go/interpreter/lfvm/instructions.go @@ -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) diff --git a/go/interpreter/lfvm/instructions_test.go b/go/interpreter/lfvm/instructions_test.go index 54139d716..98e92e5c4 100644 --- a/go/interpreter/lfvm/instructions_test.go +++ b/go/interpreter/lfvm/instructions_test.go @@ -920,3 +920,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) + } + } +}