use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameter in project besu by hyperledger.
the class QbftGetValidatorsByBlockNumberTest method blockParameterIsParameter0.
@Test
public void blockParameterIsParameter0() {
request = new JsonRpcRequestContext(new JsonRpcRequest("?", "ignore", new String[] { "0x1245" }));
BlockParameter blockParameter = method.blockParameter(request);
assertThat(blockParameter.getNumber()).isPresent();
assertThat(blockParameter.getNumber().get()).isEqualTo(0x1245);
}
use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameter in project besu by hyperledger.
the class EthFeeHistory method response.
@Override
public JsonRpcResponse response(final JsonRpcRequestContext request) {
final Object requestId = request.getRequest().getId();
final long blockCount = Optional.of(request.getRequiredParameter(0, UnsignedLongParameter.class)).map(UnsignedLongParameter::getValue).orElse(0L);
if (blockCount < 1 || blockCount > 1024) {
return new JsonRpcErrorResponse(requestId, JsonRpcError.INVALID_PARAMS);
}
final BlockParameter highestBlock = request.getRequiredParameter(1, BlockParameter.class);
final Optional<List<Double>> maybeRewardPercentiles = request.getOptionalParameter(2, Double[].class).map(Arrays::asList);
final long chainHeadBlockNumber = blockchain.getChainHeadBlockNumber();
final long resolvedHighestBlockNumber = highestBlock.getNumber().orElse(chainHeadBlockNumber);
if (resolvedHighestBlockNumber > chainHeadBlockNumber) {
return new JsonRpcErrorResponse(requestId, JsonRpcError.INVALID_PARAMS);
}
final long oldestBlock = Math.max(0, resolvedHighestBlockNumber - (blockCount - 1));
final List<BlockHeader> blockHeaders = LongStream.range(oldestBlock, oldestBlock + blockCount).mapToObj(blockchain::getBlockHeader).flatMap(Optional::stream).collect(toUnmodifiableList());
// we return the base fees for the blocks requested and 1 more because we can always compute it
final List<Wei> explicitlyRequestedBaseFees = blockHeaders.stream().map(blockHeader -> blockHeader.getBaseFee().orElse(Wei.ZERO)).collect(toUnmodifiableList());
final long nextBlockNumber = resolvedHighestBlockNumber + 1;
final Wei nextBaseFee = blockchain.getBlockHeader(nextBlockNumber).map(blockHeader -> blockHeader.getBaseFee().orElse(Wei.ZERO)).orElseGet(() -> Optional.of(protocolSchedule.getByBlockNumber(nextBlockNumber).getFeeMarket()).filter(FeeMarket::implementsBaseFee).map(BaseFeeMarket.class::cast).map(feeMarket -> {
final BlockHeader lastBlockHeader = blockHeaders.get(blockHeaders.size() - 1);
return feeMarket.computeBaseFee(nextBlockNumber, explicitlyRequestedBaseFees.get(explicitlyRequestedBaseFees.size() - 1), lastBlockHeader.getGasUsed(), feeMarket.targetGasUsed(lastBlockHeader));
}).orElse(Wei.ZERO));
final List<Double> gasUsedRatios = blockHeaders.stream().map(blockHeader -> blockHeader.getGasUsed() / (double) blockHeader.getGasLimit()).collect(toUnmodifiableList());
final Optional<List<List<Wei>>> maybeRewards = maybeRewardPercentiles.map(rewardPercentiles -> LongStream.range(oldestBlock, oldestBlock + blockCount).mapToObj(blockchain::getBlockByNumber).flatMap(Optional::stream).map(block -> computeRewards(rewardPercentiles.stream().sorted().collect(toUnmodifiableList()), block)).collect(toUnmodifiableList()));
return new JsonRpcSuccessResponse(requestId, FeeHistory.FeeHistoryResult.from(ImmutableFeeHistory.builder().oldestBlock(oldestBlock).baseFeePerGas(Stream.concat(explicitlyRequestedBaseFees.stream(), Stream.of(nextBaseFee)).collect(toUnmodifiableList())).gasUsedRatio(gasUsedRatios).reward(maybeRewards).build()));
}
use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameter in project besu by hyperledger.
the class FilterManagerLogFilterTest method shouldNotReturnLogOnNewBlockIfToBlockIsInPast.
@Test
public void shouldNotReturnLogOnNewBlockIfToBlockIsInPast() {
final String filterId = filterManager.installLogFilter(new BlockParameter("0"), new BlockParameter("1"), logsQuery());
recordBlockEvents(1).get(0);
final List<LogWithMetadata> retrievedLogs = filterManager.logsChanges(filterId);
assertThat(retrievedLogs).isEqualTo(emptyList());
}
use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameter in project besu by hyperledger.
the class IbftGetValidatorsByBlockNumberTest method blockParameterIsParameter0.
@Test
public void blockParameterIsParameter0() {
request = new JsonRpcRequestContext(new JsonRpcRequest("?", "ignore", new String[] { "0x1245" }));
BlockParameter blockParameter = method.blockParameter(request);
assertThat(blockParameter.getNumber()).isPresent();
assertThat(blockParameter.getNumber().get()).isEqualTo(0x1245);
}
use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameter in project besu by hyperledger.
the class AbstractBlockParameterMethod method findResultByParamType.
protected Object findResultByParamType(final JsonRpcRequestContext request) {
final BlockParameter blockParam = blockParameter(request);
final Object result;
final Optional<Long> blockNumber = blockParam.getNumber();
if (blockNumber.isPresent()) {
result = resultByBlockNumber(request, blockNumber.get());
} else if (blockParam.isLatest()) {
result = latestResult(request);
} else {
// If block parameter is not numeric or latest, it is pending.
result = pendingResult(request);
}
return result;
}
Aggregations