Skip to content

Commit

Permalink
Adjust overriding the state
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio Di Fabio <[email protected]>
  • Loading branch information
fab-10 committed Nov 28, 2024
1 parent fad0f1b commit 665f9b6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonCallParameterUtil.validateAndGetCallParams;

import org.hyperledger.besu.datatypes.AccountOverrideMap;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcErrorConverter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcRequestException;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonCallParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter;
Expand All @@ -41,6 +43,8 @@

import java.util.Optional;

import com.google.common.annotations.VisibleForTesting;

public abstract class AbstractEstimateGas extends AbstractBlockParameterMethod {
protected static final TransactionValidationParams ALLOW_EXCEEDING_BALANCE_VALIDATION_PARAMETERS =
ImmutableTransactionValidationParams.builder()
Expand Down Expand Up @@ -78,11 +82,12 @@ protected abstract Object simulate(
protected Object pendingResult(final JsonRpcRequestContext requestContext) {
final JsonCallParameter jsonCallParameter = validateAndGetCallParams(requestContext);
final var validationParams = getTransactionValidationParams(jsonCallParameter);
final var maybeStateOverrides = getAddressAccountOverrideMap(requestContext);
final var pendingBlockHeader = transactionSimulator.simulatePendingBlockHeader();
final TransactionSimulationFunction simulationFunction =
(cp, op) ->
transactionSimulator.processOnPending(
cp, Optional.empty(), validationParams, op, pendingBlockHeader);
cp, maybeStateOverrides, validationParams, op, pendingBlockHeader);
return simulate(
requestContext, jsonCallParameter, pendingBlockHeader.getGasLimit(), simulationFunction);
}
Expand All @@ -104,10 +109,11 @@ private Object resultByBlockHeader(
final JsonCallParameter jsonCallParameter,
final BlockHeader blockHeader) {
final var validationParams = getTransactionValidationParams(jsonCallParameter);
final var maybeStateOverrides = getAddressAccountOverrideMap(requestContext);
final TransactionSimulationFunction simulationFunction =
(cp, op) ->
transactionSimulator.processOnPending(
cp, Optional.empty(), validationParams, op, blockHeader);
transactionSimulator.process(
cp, maybeStateOverrides, validationParams, op, blockHeader);
return simulate(
requestContext, jsonCallParameter, blockHeader.getGasLimit(), simulationFunction);
}
Expand Down Expand Up @@ -214,8 +220,19 @@ protected static TransactionValidationParams getTransactionValidationParams(
return TransactionValidationParams.transactionSimulator();
}

@VisibleForTesting
protected Optional<AccountOverrideMap> getAddressAccountOverrideMap(
final JsonRpcRequestContext request) {
try {
return request.getOptionalParameter(2, AccountOverrideMap.class);
} catch (JsonRpcParameter.JsonRpcParameterException e) {
throw new InvalidJsonRpcRequestException(
"Invalid account overrides parameter (index 2)", RpcErrorType.INVALID_CALL_PARAMS, e);
}
}

protected interface TransactionSimulationFunction {
Optional<TransactionSimulatorResult> simulate(
final CallParameter callParams, final OperationTracer operationTracer);
CallParameter callParams, OperationTracer operationTracer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected Object simulate(

final AccessListOperationTracer tracer = AccessListOperationTracer.create();
final Optional<TransactionSimulatorResult> firstResult =
simulationFunction.simulate(callParams, tracer);
simulationFunction.simulate(overrideGasLimit(callParams, gasLimit), tracer);

// if the call accessList is different from the simulation result, calculate gas and return
if (shouldProcessWithAccessListOverride(callParams, tracer)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods;

import org.hyperledger.besu.datatypes.AccountOverrideMap;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcRequestException;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.Quantity;
Expand All @@ -30,7 +27,6 @@

import java.util.Optional;

import com.google.common.annotations.VisibleForTesting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -109,15 +105,4 @@ private Optional<JsonRpcErrorResponse> validateSimulationResult(
}
return Optional.empty();
}

@VisibleForTesting
protected Optional<AccountOverrideMap> getAddressAccountOverrideMap(
final JsonRpcRequestContext request) {
try {
return request.getOptionalParameter(2, AccountOverrideMap.class);
} catch (JsonRpcParameter.JsonRpcParameterException e) {
throw new InvalidJsonRpcRequestException(
"Invalid account overrides parameter (index 2)", RpcErrorType.INVALID_CALL_PARAMS, e);
}
}
}

0 comments on commit 665f9b6

Please sign in to comment.