Skip to content

Commit

Permalink
Enable Limit On Range Of JSON-RPC API trace_filter Method (#5971)
Browse files Browse the repository at this point in the history
Enable a limit on the range of blocks that can be supplied to the
JSON-RPC trace_filter method.

The limit has a default value and can be overridden with a command
line option at start up.

Signed-off-by: alyokaz <alyoshakaz@live.co.uk>
  • Loading branch information
alyokaz committed Jan 22, 2024
1 parent 921bc17 commit d743aaf
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
9 changes: 8 additions & 1 deletion besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,12 @@ static class PermissionsOptionGroup {
description = "Specifies the number of last blocks to cache (default: ${DEFAULT-VALUE})")
private final Integer numberOfblocksToCache = 0;

@Option(
names = {"--rpc-max-trace-range"},
description =
"Specifies the maximum number of blocks for the trace_filter method (default: $DEFAULT-VALUE)")
private final Long maxTraceRange = 1000L;

@Mixin private P2PTLSConfigOptions p2pTLSConfigOptions;

@Mixin private PkiBlockCreationOptions pkiBlockCreationOptions;
Expand Down Expand Up @@ -2526,7 +2532,8 @@ private ApiConfiguration apiConfiguration() {
.gasPriceMax(apiGasPriceMax)
.maxLogsRange(rpcMaxLogsRange)
.gasCap(rpcGasCap)
.isGasAndPriorityFeeLimitingEnabled(apiGasAndPriorityFeeLimitingEnabled);
.isGasAndPriorityFeeLimitingEnabled(apiGasAndPriorityFeeLimitingEnabled)
.maxTraceRange(maxTraceRange);
if (apiGasAndPriorityFeeLimitingEnabled) {
if (apiGasAndPriorityFeeLowerBoundCoefficient > apiGasAndPriorityFeeUpperBoundCoefficient) {
throw new ParameterException(
Expand Down
3 changes: 2 additions & 1 deletion besu/src/test/resources/everything_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ rpc-max-logs-range=100
json-pretty-print-enabled=false
cache-last-blocks=512
rpc-gas-cap = 50000000
rpc-max-trace-range=100

# PRIVACY TLS
privacy-tls-enabled=false
Expand Down Expand Up @@ -232,4 +233,4 @@ Xp2p-tls-crl-file="none.file"
Xp2p-tls-clienthello-sni=false

#contracts
Xevm-jumpdest-cache-weight-kb=32000
Xevm-jumpdest-cache-weight-kb=32000
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,9 @@ public Long getLowerBoundGasAndPriorityFeeCoefficient() {
public Long getUpperBoundGasAndPriorityFeeCoefficient() {
return DEFAULT_UPPER_BOUND_GAS_AND_PRIORITY_FEE_COEFFICIENT;
}

@Value.Default
public Long getMaxTraceRange() {
return 1000L;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockTracer;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.Tracer;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.flat.FlatTrace;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.flat.RewardTraceGenerator;
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
Expand Down Expand Up @@ -66,12 +68,15 @@
public class TraceFilter extends TraceBlock {

private static final Logger LOG = LoggerFactory.getLogger(TraceFilter.class);
private final Long maxRange;

public TraceFilter(
final Supplier<BlockTracer> blockTracerSupplier,
final ProtocolSchedule protocolSchedule,
final BlockchainQueries blockchainQueries) {
final BlockchainQueries blockchainQueries,
final Long maxRange) {
super(protocolSchedule, blockchainQueries);
this.maxRange = maxRange;
}

@Override
Expand All @@ -88,6 +93,19 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
final long toBlock = resolveBlockNumber(filterParameter.getToBlock());
LOG.trace("Received RPC rpcName={} fromBlock={} toBlock={}", getName(), fromBlock, toBlock);

try {
if (maxRange > 0 && toBlock - fromBlock > maxRange)
throw new IllegalArgumentException(RpcErrorType.EXCEEDS_RPC_MAX_BLOCK_RANGE.getMessage());
} catch (IllegalArgumentException ex) {
LOG.atDebug()
.setMessage("eth_getLogs request {} failed:")
.addArgument(requestContext.getRequest())
.setCause(ex.getCause())
.log();
return new JsonRpcErrorResponse(
requestContext.getRequest().getId(), RpcErrorType.EXCEEDS_RPC_MAX_BLOCK_RANGE);
}

final ObjectMapper mapper = new ObjectMapper();
final ArrayNodeWrapper resultArrayNode =
new ArrayNodeWrapper(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ protected Map<String, JsonRpcMethod> create() {
new BlockReplay(protocolSchedule, blockchainQueries.getBlockchain());
return mapOf(
new TraceReplayBlockTransactions(protocolSchedule, blockchainQueries),
new TraceFilter(() -> new BlockTracer(blockReplay), protocolSchedule, blockchainQueries),
new TraceFilter(
() -> new BlockTracer(blockReplay),
protocolSchedule,
blockchainQueries,
apiConfiguration.getMaxTraceRange()),
new TraceGet(() -> new BlockTracer(blockReplay), blockchainQueries, protocolSchedule),
new TraceTransaction(
() -> new BlockTracer(blockReplay), protocolSchedule, blockchainQueries),
Expand Down

0 comments on commit d743aaf

Please sign in to comment.