Skip to content

Commit

Permalink
Fix linea_estimateGas not allowing exceeding balance during simulation (
Browse files Browse the repository at this point in the history
#126)

Signed-off-by: Fabio Di Fabio <[email protected]>
  • Loading branch information
fab-10 authored Dec 18, 2024
1 parent be18e11 commit 574fbb1
Showing 1 changed file with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,15 @@ public LineaEstimateGas.Response execute(final PluginRpcRequest request) {
final var maybeStateOverrides = getAddressAccountOverrideMap(request.getParams());
final var minGasPrice = besuConfiguration.getMinGasPrice();
final var gasLimitUpperBound = calculateGasLimitUpperBound(callParameters, logId);
final var transaction = createTransactionForSimulation(callParameters, gasLimitUpperBound);
final Wei baseFee =
blockchainService
.getNextBlockBaseFee()
.orElseThrow(
() ->
new PluginRpcEndpointException(
RpcErrorType.INVALID_REQUEST, "Not on a baseFee market"));
final var transaction =
createTransactionForSimulation(callParameters, gasLimitUpperBound, baseFee);
log.atDebug()
.setMessage("[{}] Parsed call parameters: {}; Transaction: {}; Gas limit upper bound {}")
.addArgument(logId)
Expand All @@ -161,15 +169,7 @@ public LineaEstimateGas.Response execute(final PluginRpcRequest request) {
.addArgument(gasLimitUpperBound)
.log();
final var estimatedGasUsed =
estimateGasUsed(callParameters, maybeStateOverrides, transaction, logId);

final Wei baseFee =
blockchainService
.getNextBlockBaseFee()
.orElseThrow(
() ->
new PluginRpcEndpointException(
RpcErrorType.INVALID_REQUEST, "Not on a baseFee market"));
estimateGasUsed(callParameters, maybeStateOverrides, transaction, baseFee, logId);

final Wei estimatedPriorityFee =
getEstimatedPriorityFee(transaction, baseFee, minGasPrice, estimatedGasUsed);
Expand Down Expand Up @@ -270,6 +270,7 @@ private Long estimateGasUsed(
final JsonCallParameter callParameters,
final Optional<AccountOverrideMap> maybeStateOverrides,
final Transaction transaction,
final Wei baseFee,
final long logId) {

final var estimateGasTracer = new EstimateGasOperationTracer();
Expand Down Expand Up @@ -325,11 +326,11 @@ private Long estimateGasUsed(
final var lowGasEstimation = r.result().getEstimateGasUsedByTransaction();
final var lowResult =
transactionSimulationService.simulate(
createTransactionForSimulation(callParameters, lowGasEstimation),
createTransactionForSimulation(callParameters, lowGasEstimation, baseFee),
maybeStateOverrides,
Optional.empty(),
estimateGasTracer,
true);
false);

return lowResult
.map(
Expand Down Expand Up @@ -361,11 +362,11 @@ private Long estimateGasUsed(

final var binarySearchResult =
transactionSimulationService.simulate(
createTransactionForSimulation(callParameters, mid),
createTransactionForSimulation(callParameters, mid, baseFee),
maybeStateOverrides,
Optional.empty(),
estimateGasTracer,
true);
false);

if (binarySearchResult.isEmpty()
|| !binarySearchResult.get().isSuccessful()) {
Expand All @@ -387,7 +388,7 @@ private Long estimateGasUsed(
} else {
log.atTrace()
.setMessage(
"[{}]-[{}} Binary gas estimation search low={},mid={},high={}, successful")
"[{}]-[{}] Binary gas estimation search low={},mid={},high={}, successful")
.addArgument(logId)
.addArgument(iterations)
.addArgument(low)
Expand Down Expand Up @@ -478,7 +479,7 @@ private long highGasEstimation(
}

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

final var txBuilder =
Transaction.builder()
Expand All @@ -495,9 +496,7 @@ private Transaction createTransactionForSimulation(
txBuilder.maxPriorityFeePerGas(callParameters.getMaxPriorityFeePerGas().orElse(Wei.ZERO));
} else {
txBuilder.gasPrice(
callParameters.getGasPrice() != null
? callParameters.getGasPrice()
: blockchainService.getNextBlockBaseFee().orElse(Wei.ZERO));
callParameters.getGasPrice() != null ? callParameters.getGasPrice() : baseFee);
}

callParameters.getAccessList().ifPresent(txBuilder::accessList);
Expand Down

0 comments on commit 574fbb1

Please sign in to comment.