Skip to content

Commit

Permalink
add gas test for addressInAccessList
Browse files Browse the repository at this point in the history
  • Loading branch information
facuMH committed Sep 5, 2024
1 parent bf927bd commit 96fd6ca
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions go/interpreter/lfvm/gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
package lfvm

import (
"errors"
"testing"

"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 @@ -59,3 +61,102 @@ func TestGas_CallGasCalculation(t *testing.T) {
})
}
}

func TestGas_AddressInAccessList(t *testing.T) {

tests := map[string]struct {
setup func(*context)
accessStatus bool
coldCost tosca.Gas
}{
"istanbul": {
setup: func(c *context) {
c.params.Revision = tosca.R07_Istanbul
},
accessStatus: true,
},
"berlin_warm": {
setup: func(c *context) {
c.params.Revision = tosca.R09_Berlin
c.context.(*tosca.MockRunContext).EXPECT().IsAddressInAccessList(gomock.Any()).Return(true)
c.stack.push(uint256.NewInt(1))
c.stack.push(uint256.NewInt(2))
},
accessStatus: true,
coldCost: ColdAccountAccessCostEIP2929 - WarmStorageReadCostEIP2929,
},
"berlin_cold_enough_gas": {
setup: func(c *context) {
c.params.Revision = tosca.R09_Berlin
c.context.(*tosca.MockRunContext).EXPECT().IsAddressInAccessList(gomock.Any()).Return(false)
c.context.(*tosca.MockRunContext).EXPECT().AccessAccount(gomock.Any()).Return(tosca.ColdAccess)
c.stack.push(uint256.NewInt(1))
c.stack.push(uint256.NewInt(2))
c.gas = ColdAccountAccessCostEIP2929 - WarmStorageReadCostEIP2929
},
accessStatus: false,
coldCost: ColdAccountAccessCostEIP2929 - WarmStorageReadCostEIP2929,
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
ctrl := gomock.NewController(t)
runContext := tosca.NewMockRunContext(ctrl)

ctxt := context{
status: statusRunning,
params: tosca.Parameters{},
context: runContext,
stack: NewStack(),
memory: NewMemory(),
gas: 1 << 20,
}
test.setup(&ctxt)

warmGot, coldCostGot, errGot := addressInAccessList(&ctxt)

if errGot != nil {
t.Errorf("unexpected error, wanted %v, got %v", nil, errGot)
}

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

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

})
}
}

func TestGas_AddressInAccessListReportsOutOfGas(t *testing.T) {

ctrl := gomock.NewController(t)
runContext := tosca.NewMockRunContext(ctrl)
ctxt := context{
status: statusRunning,
params: tosca.Parameters{
BlockParameters: tosca.BlockParameters{
Revision: tosca.R09_Berlin,
},
},
context: runContext,
stack: NewStack(),
memory: NewMemory(),
gas: ColdAccountAccessCostEIP2929 - WarmStorageReadCostEIP2929 - 1,
}

ctxt.context.(*tosca.MockRunContext).EXPECT().IsAddressInAccessList(gomock.Any()).Return(false)
ctxt.context.(*tosca.MockRunContext).EXPECT().AccessAccount(gomock.Any()).Return(tosca.ColdAccess)
ctxt.stack.push(uint256.NewInt(1))
ctxt.stack.push(uint256.NewInt(2))

_, _, errGot := addressInAccessList(&ctxt)
if !errors.Is(errGot, errOutOfGas) {
t.Errorf("unexpected error, wanted %v, got %v", errOutOfGas, errGot)
}

}

0 comments on commit 96fd6ca

Please sign in to comment.