Skip to content

Instrument CREATE and CREATE2 gas func #489

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

Open
wants to merge 1 commit into
base: instrument_call_gas_func
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
24 changes: 12 additions & 12 deletions core/vm/gas_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ var (
)

func gasCreate2(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (*multigas.MultiGas, uint64, error) {
multiGas, gas, err := memoryGasCost(mem, memorySize)
multiGas, _, err := memoryGasCost(mem, memorySize)
if err != nil {
return multigas.ZeroGas(), 0, err
}
Expand All @@ -327,15 +327,15 @@ func gasCreate2(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memoryS
if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Keccak256WordGas); overflow {
return multigas.ZeroGas(), 0, ErrGasUintOverflow
}
// TODO(NIT-3484): Update multi dimensional gas here
if gas, overflow = math.SafeAdd(gas, wordGas); overflow {
if overflow = multiGas.SafeIncrement(multigas.ResourceKindComputation, wordGas); overflow {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

CREATE / CREATE2 are pure computation cost:

Computation: initCodeCost + memExpansionCost + params.CreateGas + hashCost,

Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be an inline comment rather than a PR conversation comment?
Like we want the reasoning to be available to folks reading the code.

return multigas.ZeroGas(), 0, ErrGasUintOverflow
}
return multiGas, gas, nil
singleGas, _ := multiGas.SingleGas()
return multiGas, singleGas, nil
}

func gasCreateEip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (*multigas.MultiGas, uint64, error) {
multiGas, gas, err := memoryGasCost(mem, memorySize)
multiGas, _, err := memoryGasCost(mem, memorySize)
if err != nil {
return multigas.ZeroGas(), 0, err
}
Expand All @@ -346,16 +346,16 @@ func gasCreateEip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m
if size > evm.chainConfig.MaxInitCodeSize() {
return multigas.ZeroGas(), 0, fmt.Errorf("%w: size %d", ErrMaxInitCodeSizeExceeded, size)
}
// TODO(NIT-3484): Update multi dimensional gas here
// Since size <= params.MaxInitCodeSize, these multiplication cannot overflow
moreGas := params.InitCodeWordGas * ((size + 31) / 32)
if gas, overflow = math.SafeAdd(gas, moreGas); overflow {
if overflow = multiGas.SafeIncrement(multigas.ResourceKindComputation, moreGas); overflow {
return multigas.ZeroGas(), 0, ErrGasUintOverflow
}
return multiGas, gas, nil
singleGas, _ := multiGas.SingleGas()
return multiGas, singleGas, nil
}
func gasCreate2Eip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (*multigas.MultiGas, uint64, error) {
multiGas, gas, err := memoryGasCost(mem, memorySize)
multiGas, _, err := memoryGasCost(mem, memorySize)
if err != nil {
return multigas.ZeroGas(), 0, err
}
Expand All @@ -366,13 +366,13 @@ func gasCreate2Eip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory,
if size > evm.chainConfig.MaxInitCodeSize() {
return multigas.ZeroGas(), 0, fmt.Errorf("%w: size %d", ErrMaxInitCodeSizeExceeded, size)
}
// TODO(NIT-3484): Update multi dimensional gas here
// Since size <= params.MaxInitCodeSize, these multiplication cannot overflow
moreGas := (params.InitCodeWordGas + params.Keccak256WordGas) * ((size + 31) / 32)
if gas, overflow = math.SafeAdd(gas, moreGas); overflow {
if overflow = multiGas.SafeIncrement(multigas.ResourceKindComputation, moreGas); overflow {
return multigas.ZeroGas(), 0, ErrGasUintOverflow
}
return multiGas, gas, nil
singleGas, _ := multiGas.SingleGas()
return multiGas, singleGas, nil
}

func gasExpFrontier(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (*multigas.MultiGas, uint64, error) {
Expand Down
129 changes: 129 additions & 0 deletions core/vm/operations_gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -953,3 +953,132 @@ func TestGasDelegateCall(t *testing.T) {
func TestGasStaticCall(t *testing.T) {
testGasDelegateOrStaticCall(t, gasStaticCall)
}

func testGasCreateFunc(t *testing.T, gasImplFunc gasFunc, includeHashCost bool, enableEip3860 bool) {
t.Helper()

statedb, _ := state.New(types.EmptyRootHash, state.NewDatabaseForTesting())

chainConfig := params.TestChainConfig
if enableEip3860 {
chainConfig.ShanghaiTime = new(uint64) // Enable EIP-3860
*chainConfig.ShanghaiTime = 0
}

evm := NewEVM(BlockContext{}, statedb, chainConfig, Config{})

caller := common.Address{}
contractAddr := common.Address{1}
contractGas := uint64(100000)
contract := NewContract(caller, contractAddr, new(uint256.Int), contractGas, nil)

stack := newstack()
mem := NewMemory()
memForExpected := NewMemory()

// Set up test cases based on EIP-3860 requirement
var testCases []struct {
name string
initSize uint64
shouldFail bool
}

if enableEip3860 {
// Test with different init code sizes (within limit) and boundary conditions
maxInitCodeSize := evm.chainConfig.MaxInitCodeSize()
testCases = []struct {
name string
initSize uint64
shouldFail bool
}{
{"small init code", 32, false},
{"medium init code", 1024, false},
{"max init code", maxInitCodeSize, false},
{"over max init code", maxInitCodeSize + 1, true},
}
} else {
// Test with different init code sizes (no limit)
testCases = []struct {
name string
initSize uint64
shouldFail bool
}{
{"small init code", 32, false},
{"medium init code", 1024, false},
{"large init code", 32768, false},
}
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Setup stack
stack = newstack()
stack.push(new(uint256.Int).SetUint64(tc.initSize)) // size (stack position 2)
stack.push(new(uint256.Int).SetUint64(0)) // offset (stack position 1)
stack.push(new(uint256.Int).SetUint64(0)) // value (stack position 0)

memorySize := uint64(64)

// Call gasImplFunc
multiGas, singleGas, err := gasImplFunc(evm, contract, stack, mem, memorySize)

if tc.shouldFail {
if err == nil {
t.Fatalf("Expected error for init code size %d, but got none", tc.initSize)
}
return
}

if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

// Calculate expected multi-dimensional gas
expectedMultiGas, memorySingleGas, err := memoryGasCost(memForExpected, memorySize)
if err != nil {
t.Fatalf("Failed memoryGasCost: %v", err)
}
// Calculate expected multi-dimensional gas

wordSize := (tc.initSize + 31) / 32
var totalComputationCost uint64
if enableEip3860 {
// EIP-3860 functions calculate init code cost
initCodeCost := params.InitCodeWordGas * wordSize
totalComputationCost = initCodeCost
if includeHashCost {
hashCost := params.Keccak256WordGas * wordSize
totalComputationCost += hashCost
}
} else {
// Regular gasCreate2 only calculates hash cost
if includeHashCost {
hashCost := params.Keccak256WordGas * wordSize
totalComputationCost = hashCost
}
}

expectedMultiGas.SafeIncrement(multigas.ResourceKindComputation, totalComputationCost)
expectedSingleGas := memorySingleGas + totalComputationCost

if *multiGas != *expectedMultiGas {
t.Errorf("Expected multi gas %+v, got %+v", expectedMultiGas, multiGas)
}
if singleGas != expectedSingleGas {
t.Errorf("Expected single gas %d, got %d", expectedSingleGas, singleGas)
}
})
}
}

func TestGasCreate2(t *testing.T) {
testGasCreateFunc(t, gasCreate2, true, false)
}

func TestGasCreateEip3860(t *testing.T) {
testGasCreateFunc(t, gasCreateEip3860, false, true)
}

func TestGasCreate2Eip3860(t *testing.T) {
testGasCreateFunc(t, gasCreate2Eip3860, true, true)
}
Loading