Skip to content

Commit

Permalink
Precompile calls were not traced when insuficient gas (hyperledger#7462)
Browse files Browse the repository at this point in the history
* Precompile calls were not traced when insuficient gas

Signed-off-by: Luis Pinto <luis.pinto@consensys.net>

* fixup! Precompile calls were not traced when insuficient gas

 revert RuntimeException in StateDiffGenerator

Signed-off-by: Luis Pinto <luis.pinto@consensys.net>

---------

Signed-off-by: Luis Pinto <luis.pinto@consensys.net>
  • Loading branch information
lu-pinto committed Aug 14, 2024
1 parent adf19af commit b99cdf6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- Correct entrypoint in Docker evmtool [#7430](https://github.com/hyperledger/besu/pull/7430)
- Fix protocol schedule check for devnets [#7429](https://github.com/hyperledger/besu/pull/7429)
- Fix behaviour when starting in a pre-merge network [#7431](https://github.com/hyperledger/besu/pull/7431)
- Fix tracing in precompiled contracts when halting for out of gas [#7318](https://github.com/hyperledger/besu/issues/7318)

## 24.7.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ private void executePrecompile(
if (frame.getRemainingGas() < gasRequirement) {
frame.setExceptionalHaltReason(Optional.of(ExceptionalHaltReason.INSUFFICIENT_GAS));
frame.setState(MessageFrame.State.EXCEPTIONAL_HALT);
operationTracer.tracePrecompileCall(frame, gasRequirement, null);
} else {
frame.decrementRemainingGas(gasRequirement);
final PrecompiledContract.PrecompileContractResult result =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,23 @@
*/
package org.hyperledger.besu.evm.processor;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.evm.EVM;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.precompile.PrecompileContractRegistry;
import org.hyperledger.besu.evm.precompile.PrecompiledContract;
import org.hyperledger.besu.evm.testutils.TestMessageFrameBuilder;
import org.hyperledger.besu.evm.toy.ToyWorld;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
Expand All @@ -28,9 +41,62 @@ class MessageCallProcessorTest extends AbstractMessageProcessorTest<MessageCallP

@Mock EVM evm;
@Mock PrecompileContractRegistry precompileContractRegistry;
@Mock PrecompiledContract contract;

@Override
protected MessageCallProcessor getAbstractMessageProcessor() {
return new MessageCallProcessor(evm, precompileContractRegistry);
}

@Test
public void shouldTracePrecompileContractCall() {
Address sender = Address.ZERO;
Address recipient = Address.fromHexString("0x1");

ToyWorld toyWorld = new ToyWorld();
toyWorld.createAccount(sender);
toyWorld.createAccount(recipient);

final MessageFrame messageFrame =
new TestMessageFrameBuilder()
.worldUpdater(toyWorld)
.sender(sender)
.address(recipient)
.initialGas(20L)
.build();

when(precompileContractRegistry.get(any())).thenReturn(contract);
when(contract.gasRequirement(any())).thenReturn(10L);
when(contract.computePrecompile(any(), eq(messageFrame)))
.thenReturn(PrecompiledContract.PrecompileContractResult.success(any()));

getAbstractMessageProcessor().process(messageFrame, operationTracer);

verify(operationTracer, times(1)).tracePrecompileCall(eq(messageFrame), anyLong(), any());
}

@Test
public void shouldTracePrecompileContractCallOutOfGas() {
Address sender = Address.ZERO;
Address recipient = Address.fromHexString("0x1");

ToyWorld toyWorld = new ToyWorld();
toyWorld.createAccount(sender);
toyWorld.createAccount(recipient);

final MessageFrame messageFrame =
new TestMessageFrameBuilder()
.worldUpdater(toyWorld)
.sender(sender)
.address(recipient)
.initialGas(5L)
.build();

when(precompileContractRegistry.get(any())).thenReturn(contract);
when(contract.gasRequirement(any())).thenReturn(10L);

getAbstractMessageProcessor().process(messageFrame, operationTracer);

verify(operationTracer, times(1)).tracePrecompileCall(eq(messageFrame), anyLong(), any());
}
}

0 comments on commit b99cdf6

Please sign in to comment.