Search in sources :

Example 1 with UnsignedLongParameter

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.UnsignedLongParameter 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 2 with UnsignedLongParameter

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.UnsignedLongParameter in project besu by hyperledger.

the class EngineExchangeTransitionConfigurationTest method shouldReturnConfigurationOnConfigurationMatch.

@Test
public void shouldReturnConfigurationOnConfigurationMatch() {
    final BlockHeader fakeBlockHeader = createBlockHeader(Hash.fromHexStringLenient("0x01"), 42);
    when(mergeContext.getTerminalPoWBlock()).thenReturn(Optional.of(fakeBlockHeader));
    when(mergeContext.getTerminalTotalDifficulty()).thenReturn(Difficulty.of(24));
    var response = resp(new EngineExchangeTransitionConfigurationParameter("24", Hash.fromHexStringLenient("0x01").toHexString(), new UnsignedLongParameter(0)));
    var result = fromSuccessResp(response);
    assertThat(result.getTerminalTotalDifficulty()).isEqualTo(Difficulty.of(24));
    assertThat(result.getTerminalBlockHash()).isEqualTo(Hash.fromHexStringLenient("0x01"));
    assertThat(result.getTerminalBlockNumber()).isEqualTo(42);
}
Also used : UnsignedLongParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.UnsignedLongParameter) EngineExchangeTransitionConfigurationParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.EngineExchangeTransitionConfigurationParameter) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Test(org.junit.Test)

Example 3 with UnsignedLongParameter

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.UnsignedLongParameter in project besu by hyperledger.

the class EngineExchangeTransitionConfigurationTest method shouldNotWarnWhenExchangeConfigurationCalledWithinTimeout.

@Test
public void shouldNotWarnWhenExchangeConfigurationCalledWithinTimeout(final TestContext ctx) {
    final long TEST_QOS_TIMEOUT = 75L;
    final Async async = ctx.async();
    final AtomicInteger logCounter = new AtomicInteger(0);
    final var spyMethod = spy(method);
    final var spyTimer = spy(new QosTimer(vertx, TEST_QOS_TIMEOUT, z -> logCounter.incrementAndGet()));
    when(mergeContext.getTerminalPoWBlock()).thenReturn(Optional.empty());
    when(mergeContext.getTerminalTotalDifficulty()).thenReturn(Difficulty.of(1337L));
    when(spyMethod.getQosTimer()).thenReturn(spyTimer);
    spyTimer.resetTimer();
    // call exchangeTransitionConfiguration 50 milliseconds hence to reset our QoS timer
    vertx.setTimer(50L, z -> spyMethod.syncResponse(new JsonRpcRequestContext(new JsonRpcRequest("2.0", RpcMethod.ENGINE_EXCHANGE_TRANSITION_CONFIGURATION.getMethodName(), new Object[] { new EngineExchangeTransitionConfigurationParameter("24", Hash.fromHexStringLenient("0x01").toHexString(), new UnsignedLongParameter(0)) }))));
    vertx.setTimer(100L, z -> {
        try {
            // once on construction, once on call:
            verify(spyTimer, times(2)).resetTimer();
        } catch (Exception ex) {
            ctx.fail(ex);
        }
        // should not warn
        ctx.assertEquals(0, logCounter.get());
        async.complete();
    });
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) JsonRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest) RunWith(org.junit.runner.RunWith) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) Bytes(org.apache.tuweni.bytes.Bytes) Mockito.spy(org.mockito.Mockito.spy) MergeContext(org.hyperledger.besu.consensus.merge.MergeContext) JsonRpcResponseType(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponseType) Address(org.hyperledger.besu.datatypes.Address) UnsignedLongParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.UnsignedLongParameter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Wei(org.hyperledger.besu.datatypes.Wei) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) Bytes32(org.apache.tuweni.bytes.Bytes32) Before(org.junit.Before) Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) JsonRpcResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Vertx(io.vertx.core.Vertx) LogsBloomFilter(org.hyperledger.besu.evm.log.LogsBloomFilter) Mockito.times(org.mockito.Mockito.times) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) Mockito.verify(org.mockito.Mockito.verify) QosTimer(org.hyperledger.besu.ethereum.api.jsonrpc.internal.QosTimer) Mockito(org.mockito.Mockito) RpcMethod(org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod) ParsedExtraData(org.hyperledger.besu.ethereum.core.ParsedExtraData) EngineExchangeTransitionConfigurationParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.EngineExchangeTransitionConfigurationParameter) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) Optional(java.util.Optional) BlockHeaderFunctions(org.hyperledger.besu.ethereum.core.BlockHeaderFunctions) Mockito.mock(org.mockito.Mockito.mock) Hash(org.hyperledger.besu.datatypes.Hash) EngineExchangeTransitionConfigurationResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.EngineExchangeTransitionConfigurationResult) QosTimer(org.hyperledger.besu.ethereum.api.jsonrpc.internal.QosTimer) UnsignedLongParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.UnsignedLongParameter) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) JsonRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EngineExchangeTransitionConfigurationParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.EngineExchangeTransitionConfigurationParameter) Async(io.vertx.ext.unit.Async) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.Test)

Example 4 with UnsignedLongParameter

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.UnsignedLongParameter in project besu by hyperledger.

the class EngineExchangeTransitionConfigurationTest method shouldNotReturnInvalidParamsOnTerminalBlockNumberNotZero.

@Test
public void shouldNotReturnInvalidParamsOnTerminalBlockNumberNotZero() {
    var mockBlockHeader = new BlockHeaderTestFixture().difficulty(Difficulty.of(1339L)).number(420).buildHeader();
    when(mergeContext.getTerminalPoWBlock()).thenReturn(Optional.of(mockBlockHeader));
    when(mergeContext.getTerminalTotalDifficulty()).thenReturn(Difficulty.of(1337L));
    var response = resp(new EngineExchangeTransitionConfigurationParameter("0", Hash.ZERO.toHexString(), new UnsignedLongParameter(1L)));
    var result = fromSuccessResp(response);
    assertThat(result.getTerminalTotalDifficulty()).isEqualTo(Difficulty.of(1337L));
    assertThat(result.getTerminalBlockHash()).isEqualTo(mockBlockHeader.getHash());
    assertThat(result.getTerminalBlockNumber()).isEqualTo(mockBlockHeader.getNumber());
}
Also used : BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) UnsignedLongParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.UnsignedLongParameter) EngineExchangeTransitionConfigurationParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.EngineExchangeTransitionConfigurationParameter) Test(org.junit.Test)

Example 5 with UnsignedLongParameter

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.UnsignedLongParameter in project besu by hyperledger.

the class EngineExchangeTransitionConfigurationTest method shouldReturnConfigurationOnConfigurationMisMatch.

@Test
public void shouldReturnConfigurationOnConfigurationMisMatch() {
    final BlockHeader fakeBlockHeader = createBlockHeader(Hash.fromHexStringLenient("0x01"), 42);
    when(mergeContext.getTerminalPoWBlock()).thenReturn(Optional.of(fakeBlockHeader));
    when(mergeContext.getTerminalTotalDifficulty()).thenReturn(Difficulty.of(24));
    var response = resp(new EngineExchangeTransitionConfigurationParameter("1", Hash.fromHexStringLenient("0xff").toHexString(), new UnsignedLongParameter(0L)));
    var result = fromSuccessResp(response);
    assertThat(result.getTerminalTotalDifficulty()).isEqualTo(Difficulty.of(24));
    assertThat(result.getTerminalBlockHash()).isEqualTo(Hash.fromHexStringLenient("0x01"));
    assertThat(result.getTerminalBlockNumber()).isEqualTo(42);
}
Also used : UnsignedLongParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.UnsignedLongParameter) EngineExchangeTransitionConfigurationParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.EngineExchangeTransitionConfigurationParameter) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Test(org.junit.Test)

Aggregations

UnsignedLongParameter (org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.UnsignedLongParameter)6 EngineExchangeTransitionConfigurationParameter (org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.EngineExchangeTransitionConfigurationParameter)5 Test (org.junit.Test)5 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)4 Map (java.util.Map)2 Optional (java.util.Optional)2 Wei (org.hyperledger.besu.datatypes.Wei)2 RpcMethod (org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod)2 JsonRpcRequestContext (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext)2 JsonRpcResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse)2 JsonRpcSuccessResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)2 BlockHeaderTestFixture (org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Streams (com.google.common.collect.Streams)1 Vertx (io.vertx.core.Vertx)1 Async (io.vertx.ext.unit.Async)1 TestContext (io.vertx.ext.unit.TestContext)1 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)1 AbstractMap (java.util.AbstractMap)1