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()));
}
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);
}
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();
});
}
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());
}
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);
}
Aggregations