Skip to content

Instrument LOG0-LOG4 gas function #492

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

Merged
merged 2 commits into from
Jul 22, 2025
Merged
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
19 changes: 13 additions & 6 deletions core/vm/gas_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,27 +258,34 @@ func makeGasLog(n uint64) gasFunc {
return multigas.ZeroGas(), 0, ErrGasUintOverflow
}

multiGas, gas, err := memoryGasCost(mem, memorySize)
multiGas, _, err := memoryGasCost(mem, memorySize)
if err != nil {
return multigas.ZeroGas(), 0, err
}

// TODO(NIT-3484): Update multi dimensional gas here
if gas, overflow = math.SafeAdd(gas, params.LogGas); overflow {
// Base LOG operation considered as computation.
// See rationale in: https://github.com/OffchainLabs/nitro/blob/master/docs/decisions/0002-multi-dimensional-gas-metering.md
if overflow = multiGas.SafeIncrement(multigas.ResourceKindComputation, params.LogGas); overflow {
return multigas.ZeroGas(), 0, ErrGasUintOverflow
}
if gas, overflow = math.SafeAdd(gas, n*params.LogTopicGas); overflow {

// LOG topic operations considered as computation.
// See rationale in: https://github.com/OffchainLabs/nitro/blob/master/docs/decisions/0002-multi-dimensional-gas-metering.md
if overflow = multiGas.SafeIncrement(multigas.ResourceKindComputation, n*params.LogTopicGas); overflow {
return multigas.ZeroGas(), 0, ErrGasUintOverflow
}

var memorySizeGas uint64
if memorySizeGas, overflow = math.SafeMul(requestedSize, params.LogDataGas); overflow {
return multigas.ZeroGas(), 0, ErrGasUintOverflow
}
if gas, overflow = math.SafeAdd(gas, memorySizeGas); overflow {
// Event log data considered as history growth.
// See rationale in: https://github.com/OffchainLabs/nitro/blob/master/docs/decisions/0002-multi-dimensional-gas-metering.md
if overflow = multiGas.SafeIncrement(multigas.ResourceKindHistoryGrowth, memorySizeGas); overflow {
return multigas.ZeroGas(), 0, ErrGasUintOverflow
}
return multiGas, gas, nil
singleGas, _ := multiGas.SingleGas()
return multiGas, singleGas, nil
}
}

Expand Down
44 changes: 44 additions & 0 deletions core/vm/operations_gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1260,3 +1260,47 @@ func TestGasSelfdestructEIP4762(t *testing.T) {
t.Errorf("Expected multi gas %d, got %d", expectedMultiGas, multiGas)
}
}

// Base LOG0-LOG4 gas function test
func TestMakeGasLog(t *testing.T) {
for n := uint64(0); n <= 4; n++ {
statedb, _ := state.New(types.EmptyRootHash, state.NewDatabaseForTesting())
evm := NewEVM(BlockContext{}, statedb, params.TestChainConfig, 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 stack for LOG operation
requestedSize := uint64(32) // requestedSize = 32 bytes for data
stack.push(new(uint256.Int).SetUint64(requestedSize)) // stack.Back(1)
stack.push(new(uint256.Int)) // dummy top (stack.Back(0))

// memorySize is arbitrary, only affects memory cost component
memorySize := uint64(64)

_, memorySingleGas, err := memoryGasCost(memForExpected, memorySize)
if err != nil {
t.Fatalf("Failed memoryGasCost: %v", err)
}

expectedComputation := memorySingleGas + params.LogGas + n*params.LogTopicGas
expectedHistory := requestedSize * params.LogDataGas

expectedMultiGas := multigas.ComputationGas(expectedComputation).Set(multigas.ResourceKindHistoryGrowth, expectedHistory)

multiGas, _, err := makeGasLog(n)(evm, contract, stack, mem, memorySize)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

if *multiGas != *expectedMultiGas {
t.Errorf("Expected multi gas %d, got %d", expectedMultiGas, multiGas)
}
}
}
Loading