Skip to content
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

Align linea_estimateGas behavior to geth #25

Merged
merged 1 commit into from
Jun 25, 2024
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
3 changes: 2 additions & 1 deletion PLUGINS.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ The validators are in the package `net.consensys.linea.sequencer.txpoolvalidatio
### Linea Estimate Gas
#### `linea_estimateGas`

This endpoint simulates a transaction, including line count limit validation, and returns the estimated gas used ( as the standard `eth_estimateGas`) plus the estimated gas price to be used when submitting the tx.
This endpoint simulates a transaction, including line count limit validation, and returns the estimated gas used
(as the standard `eth_estimateGas` with `strict=true`) plus the estimated gas price to be used when submitting the tx.

#### Parameters
same as `eth_estimateGas`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,14 @@ public LineaEstimateGas.Response execute(final PluginRpcRequest request) {
final var minGasPrice = besuConfiguration.getMinGasPrice();

final var transaction =
createTransactionForSimulation(
callParameters, txValidatorConf.maxTxGasLimit(), minGasPrice);
createTransactionForSimulation(callParameters, txValidatorConf.maxTxGasLimit());
log.atDebug()
.setMessage("[{}] Parsed call parameters: {}; Transaction: {}")
.addArgument(LOG_SEQUENCE::get)
.addArgument(callParameters)
.addArgument(transaction::toTraceLog)
.log();
final var estimatedGasUsed = estimateGasUsed(callParameters, transaction, minGasPrice);
final var estimatedGasUsed = estimateGasUsed(callParameters, transaction);

final Wei baseFee =
blockchainService
Expand Down Expand Up @@ -205,9 +204,7 @@ private Wei getEstimatedPriorityFee(
}

private Long estimateGasUsed(
final JsonCallParameter callParameters,
final Transaction transaction,
final Wei minGasPrice) {
final JsonCallParameter callParameters, final Transaction transaction) {

final var estimateGasTracer = new EstimateGasOperationTracer();
final var chainHeadHeader = blockchainService.getChainHeadHeader();
Expand All @@ -216,7 +213,7 @@ private Long estimateGasUsed(

final var chainHeadHash = chainHeadHeader.getBlockHash();
final var maybeSimulationResults =
transactionSimulationService.simulate(transaction, chainHeadHash, zkAndGasTracer, true);
transactionSimulationService.simulate(transaction, chainHeadHash, zkAndGasTracer, false);

ModuleLimitsValidationResult moduleLimit =
moduleLineCountValidator.validate(zkTracer.getModulesLineCount());
Expand Down Expand Up @@ -262,7 +259,7 @@ private Long estimateGasUsed(
final var lowGasEstimation = r.result().getEstimateGasUsedByTransaction();
final var lowResult =
transactionSimulationService.simulate(
createTransactionForSimulation(callParameters, lowGasEstimation, minGasPrice),
createTransactionForSimulation(callParameters, lowGasEstimation),
chainHeadHash,
estimateGasTracer,
true);
Expand Down Expand Up @@ -297,8 +294,7 @@ private Long estimateGasUsed(

final var binarySearchResult =
transactionSimulationService.simulate(
createTransactionForSimulation(
callParameters, mid, minGasPrice),
createTransactionForSimulation(callParameters, mid),
chainHeadHash,
estimateGasTracer,
true);
Expand Down Expand Up @@ -361,10 +357,7 @@ private JsonCallParameter parseRequest(final Object[] params) {
}

private void validateParameters(final JsonCallParameter callParameters) {
if (callParameters.getGasPrice() != null
&& (callParameters.getMaxFeePerGas().isPresent()
|| callParameters.getMaxPriorityFeePerGas().isPresent()
|| callParameters.getMaxFeePerBlobGas().isPresent())) {
if (callParameters.getGasPrice() != null && isBaseFeeMarket(callParameters)) {
throw new InvalidJsonRpcParameters(
"gasPrice cannot be used with maxFeePerGas or maxPriorityFeePerGas or maxFeePerBlobGas");
}
Expand All @@ -376,6 +369,12 @@ private void validateParameters(final JsonCallParameter callParameters) {
}
}

private boolean isBaseFeeMarket(final JsonCallParameter callParameters) {
return (callParameters.getMaxFeePerGas().isPresent()
|| callParameters.getMaxPriorityFeePerGas().isPresent()
|| callParameters.getMaxFeePerBlobGas().isPresent());
}

/**
* Estimate gas by adding minimum gas remaining for some operation and the necessary gas for sub
* calls
Expand All @@ -396,7 +395,7 @@ private long highGasEstimation(
}

private Transaction createTransactionForSimulation(
final JsonCallParameter callParameters, final long maxTxGasLimit, final Wei minGasPrice) {
final JsonCallParameter callParameters, final long maxTxGasLimit) {

final var txBuilder =
Transaction.builder()
Expand All @@ -405,11 +404,12 @@ private Transaction createTransactionForSimulation(
.gasLimit(maxTxGasLimit)
.payload(
callParameters.getPayload() == null ? Bytes.EMPTY : callParameters.getPayload())
.gasPrice(
callParameters.getGasPrice() == null ? minGasPrice : callParameters.getGasPrice())
.value(callParameters.getValue() == null ? Wei.ZERO : callParameters.getValue())
.signature(FAKE_SIGNATURE_FOR_SIZE_CALCULATION);

if (!isBaseFeeMarket(callParameters) && callParameters.getGasPrice() == null) {
txBuilder.gasPrice(blockchainService.getNextBlockBaseFee().orElse(Wei.ZERO));
}
callParameters.getMaxFeePerGas().ifPresent(txBuilder::maxFeePerGas);
callParameters.getMaxPriorityFeePerGas().ifPresent(txBuilder::maxPriorityFeePerGas);
callParameters.getAccessList().ifPresent(txBuilder::accessList);
Expand Down
Loading