Skip to content

Commit

Permalink
Fix: rpc gas cap option should not inconditionally override smaller t…
Browse files Browse the repository at this point in the history
…x gas limit

Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
  • Loading branch information
fab-10 committed Sep 30, 2024
1 parent c3aa3f4 commit 513ce1f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,16 +230,23 @@ public Optional<TransactionSimulatorResult> processWithWorldUpdater(
final Account sender = updater.get(senderAddress);
final long nonce = sender != null ? sender.getNonce() : 0L;

long gasLimit =
final long txGasLimit =
callParams.getGasLimit() >= 0
? callParams.getGasLimit()
: blockHeaderToProcess.getGasLimit();

final long simulationGasLimit;
if (rpcGasCap > 0) {
gasLimit = rpcGasCap;
simulationGasLimit = Math.min(txGasLimit, rpcGasCap);
LOG.trace(
"Gas limit capped at {} for transaction simulation due to provided RPC gas cap.",
"Gas limit capped at {} for transaction simulation, tx gas limit is {} and provided RPC gas cap is {}",
simulationGasLimit,
txGasLimit,
rpcGasCap);
} else {
simulationGasLimit = txGasLimit;
}

final Wei value = callParams.getValue() != null ? callParams.getValue() : Wei.ZERO;
final Bytes payload = callParams.getPayload() != null ? callParams.getPayload() : Bytes.EMPTY;

Expand All @@ -265,7 +272,7 @@ public Optional<TransactionSimulatorResult> processWithWorldUpdater(
header,
senderAddress,
nonce,
gasLimit,
simulationGasLimit,
value,
payload,
blobGasPrice);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class TransactionSimulatorTest {

private static final Address DEFAULT_FROM =
Address.fromHexString("0x0000000000000000000000000000000000000000");
private static final long GASCAP = 500L;
private static final long GAS_CAP = 50000L;
private TransactionSimulator transactionSimulator;
private TransactionSimulator cappedTransactionSimulator;

Expand All @@ -96,7 +96,7 @@ public void setUp() {
this.transactionSimulator =
new TransactionSimulator(blockchain, worldStateArchive, protocolSchedule, 0);
this.cappedTransactionSimulator =
new TransactionSimulator(blockchain, worldStateArchive, protocolSchedule, GASCAP);
new TransactionSimulator(blockchain, worldStateArchive, protocolSchedule, GAS_CAP);
}

@Test
Expand Down Expand Up @@ -530,7 +530,7 @@ public void shouldReturnSuccessfulResultWhenEip1559TransactionProcessingIsSucces
@Test
public void shouldCapGasLimitWhenOriginalTransactionExceedsGasCap() {
final CallParameter callParameter =
eip1559TransactionCallParameter(Wei.ZERO, Wei.ZERO, GASCAP + 1);
eip1559TransactionCallParameter(Wei.ZERO, Wei.ZERO, GAS_CAP + 1);

final BlockHeader blockHeader = mockBlockHeader(Hash.ZERO, 1L, Wei.ONE);

Expand All @@ -542,7 +542,7 @@ public void shouldCapGasLimitWhenOriginalTransactionExceedsGasCap() {
.type(TransactionType.EIP1559)
.chainId(BigInteger.ONE)
.nonce(1L)
.gasLimit(GASCAP)
.gasLimit(GAS_CAP)
.maxFeePerGas(callParameter.getMaxFeePerGas().orElseThrow())
.maxPriorityFeePerGas(callParameter.getMaxPriorityFeePerGas().orElseThrow())
.to(callParameter.getTo())
Expand All @@ -566,11 +566,11 @@ public void shouldCapGasLimitWhenOriginalTransactionExceedsGasCap() {
}

@Test
public void shouldUseRpcGasCapWhenCapIsHigherThanGasLimit() {
public void rpcGasCapShouldNotOverrideExplicitTxGasLimit() {
// generate a transaction with a gas limit that is lower than the gas cap,
// expect the gas cap to override parameter gas limit
// expect the gas cap to not override tx gas limit
final CallParameter callParameter =
eip1559TransactionCallParameter(Wei.ZERO, Wei.ZERO, GASCAP - 1);
eip1559TransactionCallParameter(Wei.ZERO, Wei.ZERO, GAS_CAP - 1);

final BlockHeader blockHeader = mockBlockHeader(Hash.ZERO, 1L, Wei.ONE);

Expand All @@ -591,7 +591,7 @@ public void shouldUseRpcGasCapWhenCapIsHigherThanGasLimit() {
.value(callParameter.getValue())
.payload(callParameter.getPayload())
.signature(FAKE_SIGNATURE)
.gasLimit(GASCAP)
.gasLimit(GAS_CAP - 1)
.build();

// call process with original transaction
Expand Down

0 comments on commit 513ce1f

Please sign in to comment.