Skip to content

Commit a260a06

Browse files
Instrument LOG0-LOG4 gas function
1 parent 589dae4 commit a260a06

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

core/vm/gas_table.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,27 +256,27 @@ func makeGasLog(n uint64) gasFunc {
256256
return multigas.ZeroGas(), 0, ErrGasUintOverflow
257257
}
258258

259-
multiGas, gas, err := memoryGasCost(mem, memorySize)
259+
multiGas, _, err := memoryGasCost(mem, memorySize)
260260
if err != nil {
261261
return multigas.ZeroGas(), 0, err
262262
}
263263

264-
// TODO(NIT-3484): Update multi dimensional gas here
265-
if gas, overflow = math.SafeAdd(gas, params.LogGas); overflow {
264+
if overflow = multiGas.SafeIncrement(multigas.ResourceKindComputation, params.LogGas); overflow {
266265
return multigas.ZeroGas(), 0, ErrGasUintOverflow
267266
}
268-
if gas, overflow = math.SafeAdd(gas, n*params.LogTopicGas); overflow {
267+
if overflow = multiGas.SafeIncrement(multigas.ResourceKindComputation, n*params.LogTopicGas); overflow {
269268
return multigas.ZeroGas(), 0, ErrGasUintOverflow
270269
}
271270

272271
var memorySizeGas uint64
273272
if memorySizeGas, overflow = math.SafeMul(requestedSize, params.LogDataGas); overflow {
274273
return multigas.ZeroGas(), 0, ErrGasUintOverflow
275274
}
276-
if gas, overflow = math.SafeAdd(gas, memorySizeGas); overflow {
275+
if overflow = multiGas.SafeIncrement(multigas.ResourceKindHistoryGrowth, memorySizeGas); overflow {
277276
return multigas.ZeroGas(), 0, ErrGasUintOverflow
278277
}
279-
return multiGas, gas, nil
278+
singleGas, _ := multiGas.SingleGas()
279+
return multiGas, singleGas, nil
280280
}
281281
}
282282

core/vm/operaions_gas_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,41 @@ func TestGasSStore4762(t *testing.T) {
449449
t.Errorf("Expected single gas %d, got %d", expectedSingleGas, singleGas)
450450
}
451451
}
452+
453+
// Base LOG0-LOG4 gas function test
454+
func TestMakeGasLog(t *testing.T) {
455+
for n := uint64(0); n <= 4; n++ {
456+
statedb, _ := state.New(types.EmptyRootHash, state.NewDatabaseForTesting())
457+
evm := NewEVM(BlockContext{}, statedb, params.TestChainConfig, Config{})
458+
459+
caller := common.Address{}
460+
contractAddr := common.Address{1}
461+
contractGas := uint64(100000)
462+
contract := NewContract(caller, contractAddr, new(uint256.Int), contractGas, nil)
463+
464+
stack := newstack()
465+
mem := NewMemory()
466+
467+
// Set up stack for LOG operation
468+
requestedSize := uint64(32) // requestedSize = 32 bytes for data
469+
stack.push(new(uint256.Int).SetUint64(requestedSize)) // stack.Back(1)
470+
stack.push(new(uint256.Int)) // dummy top (stack.Back(0))
471+
472+
// memorySize is arbitrary, only affects memory cost component
473+
memorySize := uint64(64)
474+
475+
expectedComputation := params.LogGas + n*params.LogTopicGas
476+
expectedHistory := requestedSize * params.LogDataGas
477+
478+
expectedMultiGas := multigas.ComputationGas(expectedComputation).Set(multigas.ResourceKindHistoryGrowth, expectedHistory)
479+
480+
multiGas, _, err := makeGasLog(n)(evm, contract, stack, mem, memorySize)
481+
if err != nil {
482+
t.Fatalf("Unexpected error: %v", err)
483+
}
484+
485+
if *multiGas != *expectedMultiGas {
486+
t.Errorf("Expected multi gas %d, got %d", expectedMultiGas, multiGas)
487+
}
488+
}
489+
}

0 commit comments

Comments
 (0)