Search in sources :

Example 1 with BlockParameter

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);
}
Also used : BlockParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameter) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) JsonRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest) Test(org.junit.Test)

Example 2 with BlockParameter

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()));
}
Also used : Arrays(java.util.Arrays) JsonRpcError(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError) ImmutableFeeHistory(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.ImmutableFeeHistory) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) FeeMarket(org.hyperledger.besu.ethereum.mainnet.feemarket.FeeMarket) Collectors.toUnmodifiableList(java.util.stream.Collectors.toUnmodifiableList) FeeHistory(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.FeeHistory) ArrayList(java.util.ArrayList) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) UnsignedLongParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.UnsignedLongParameter) TransactionReceipt(org.hyperledger.besu.ethereum.core.TransactionReceipt) Map(java.util.Map) BlockParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameter) BaseFeeMarket(org.hyperledger.besu.ethereum.mainnet.feemarket.BaseFeeMarket) Wei(org.hyperledger.besu.datatypes.Wei) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) Block(org.hyperledger.besu.ethereum.core.Block) LongStream(java.util.stream.LongStream) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) JsonRpcResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse) Streams(com.google.common.collect.Streams) AbstractMap(java.util.AbstractMap) List(java.util.List) RpcMethod(org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod) Stream(java.util.stream.Stream) Optional(java.util.Optional) Comparator(java.util.Comparator) Transaction(org.hyperledger.besu.ethereum.core.Transaction) UnsignedLongParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.UnsignedLongParameter) FeeMarket(org.hyperledger.besu.ethereum.mainnet.feemarket.FeeMarket) BaseFeeMarket(org.hyperledger.besu.ethereum.mainnet.feemarket.BaseFeeMarket) BlockParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameter) Collectors.toUnmodifiableList(java.util.stream.Collectors.toUnmodifiableList) ArrayList(java.util.ArrayList) List(java.util.List) Wei(org.hyperledger.besu.datatypes.Wei) Arrays(java.util.Arrays) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)

Example 3 with BlockParameter

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());
}
Also used : BlockParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameter) LogWithMetadata(org.hyperledger.besu.ethereum.core.LogWithMetadata) Test(org.junit.Test)

Example 4 with BlockParameter

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);
}
Also used : BlockParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameter) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) JsonRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest) Test(org.junit.Test)

Example 5 with BlockParameter

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;
}
Also used : BlockParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameter)

Aggregations

BlockParameter (org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameter)7 JsonRpcRequestContext (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext)3 JsonRpcErrorResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)3 JsonRpcSuccessResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)3 Test (org.junit.Test)3 JsonRpcRequest (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest)2 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)2 Streams (com.google.common.collect.Streams)1 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Collectors.toUnmodifiableList (java.util.stream.Collectors.toUnmodifiableList)1 LongStream (java.util.stream.LongStream)1 Stream (java.util.stream.Stream)1 Address (org.hyperledger.besu.datatypes.Address)1