use of org.hyperledger.besu.datatypes.Wei 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.datatypes.Wei in project besu by hyperledger.
the class EthEstimateGasTest method shouldUseGasPriceParameterWhenIsPresent.
@Test
public void shouldUseGasPriceParameterWhenIsPresent() {
final Wei gasPrice = Wei.of(1000);
final JsonRpcRequestContext request = ethEstimateGasRequest(defaultLegacyTransactionCallParameter(gasPrice));
mockTransientProcessorResultGasEstimate(1L, true, false, gasPrice);
final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(null, Quantity.create(1L));
Assertions.assertThat(method.response(request)).usingRecursiveComparison().isEqualTo(expectedResponse);
}
use of org.hyperledger.besu.datatypes.Wei in project besu by hyperledger.
the class EthGetTransactionReceiptTest method shouldWorkFor1559Txs.
@Test
public void shouldWorkFor1559Txs() {
when(blockchain.headBlockNumber()).thenReturn(1L);
final Transaction transaction1559 = new BlockDataGenerator().transaction(TransactionType.EIP1559);
final Wei baseFee = Wei.ONE;
final TransactionReceiptWithMetadata transactionReceiptWithMetadata = TransactionReceiptWithMetadata.create(statusReceipt, transaction1559, hash, 1, 2, Optional.of(baseFee), blockHash, 4);
when(blockchain.transactionReceiptByTransactionHash(receiptHash)).thenReturn(Optional.of(transactionReceiptWithMetadata));
when(protocolSchedule.getByBlockNumber(1)).thenReturn(rootTransactionTypeSpec);
final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) ethGetTransactionReceipt.response(request);
final TransactionReceiptStatusResult result = (TransactionReceiptStatusResult) response.getResult();
assertThat(result.getStatus()).isEqualTo("0x1");
assertThat(Wei.fromHexString(result.getEffectiveGasPrice())).isEqualTo(UInt256s.min(baseFee.add(transaction1559.getMaxPriorityFeePerGas().get()), transaction1559.getMaxFeePerGas().get()));
}
use of org.hyperledger.besu.datatypes.Wei in project besu by hyperledger.
the class TransactionPoolTest method shouldRejectGoQuorumTransactionWithNonZeroValue.
@Test
public void shouldRejectGoQuorumTransactionWithNonZeroValue() {
when(transactionValidator.getGoQuorumCompatibilityMode()).thenReturn(true);
final EthProtocolManager ethProtocolManager = EthProtocolManagerTestUtil.create();
final EthContext ethContext = ethProtocolManager.ethContext();
final Wei twoEthers = Wei.fromEth(2);
final TransactionPool transactionPool = new TransactionPool(transactions, protocolSchedule, protocolContext, transactionBroadcaster, ethContext, new MiningParameters.Builder().minTransactionGasPrice(Wei.ZERO).build(), metricsSystem, ImmutableTransactionPoolConfiguration.builder().txFeeCap(twoEthers).build());
final Transaction transaction37 = Transaction.builder().v(BigInteger.valueOf(37)).value(Wei.ONE).build();
final Transaction transaction38 = Transaction.builder().v(BigInteger.valueOf(38)).value(Wei.ONE).build();
final ValidationResult<TransactionInvalidReason> result37 = transactionPool.addLocalTransaction(transaction37);
final ValidationResult<TransactionInvalidReason> result38 = transactionPool.addLocalTransaction(transaction38);
assertThat(result37.getInvalidReason()).isEqualTo(TransactionInvalidReason.ETHER_VALUE_NOT_SUPPORTED);
assertThat(result38.getInvalidReason()).isEqualTo(TransactionInvalidReason.ETHER_VALUE_NOT_SUPPORTED);
}
use of org.hyperledger.besu.datatypes.Wei in project besu by hyperledger.
the class RewardTraceGenerator method generateFromBlock.
/**
* Generates a stream of reward {@link Trace} from the passed {@link Block} data.
*
* @param protocolSchedule the {@link ProtocolSchedule} to use
* @param block the current {@link Block} to use
* @return a stream of generated reward traces {@link Trace}
*/
public static Stream<Trace> generateFromBlock(final ProtocolSchedule protocolSchedule, final Block block) {
final List<Trace> flatTraces = new ArrayList<>();
final BlockHeader blockHeader = block.getHeader();
final List<BlockHeader> ommers = block.getBody().getOmmers();
final ProtocolSpec protocolSpec = protocolSchedule.getByBlockNumber(blockHeader.getNumber());
final Wei blockReward = protocolSpec.getBlockReward();
final MiningBeneficiaryCalculator miningBeneficiaryCalculator = protocolSpec.getMiningBeneficiaryCalculator();
final Wei coinbaseReward = protocolSpec.getBlockProcessor().getCoinbaseReward(blockReward, blockHeader.getNumber(), ommers.size());
// add uncle reward traces
ommers.forEach(ommerBlockHeader -> {
final Wei ommerReward = protocolSpec.getBlockProcessor().getOmmerReward(blockReward, blockHeader.getNumber(), ommerBlockHeader.getNumber());
final Action.Builder uncleActionBuilder = Action.builder().author(miningBeneficiaryCalculator.calculateBeneficiary(ommerBlockHeader).toHexString()).rewardType(UNCLE_LABEL).value(ommerReward.toShortHexString());
flatTraces.add(RewardTrace.builder().actionBuilder(uncleActionBuilder).blockHash(block.getHash().toHexString()).blockNumber(blockHeader.getNumber()).type(REWARD_LABEL).build());
});
// add block reward trace
final Action.Builder blockActionBuilder = Action.builder().author(miningBeneficiaryCalculator.calculateBeneficiary(blockHeader).toHexString()).rewardType(BLOCK_LABEL).value(coinbaseReward.toShortHexString());
flatTraces.add(0, RewardTrace.builder().actionBuilder(blockActionBuilder).blockHash(block.getHash().toHexString()).blockNumber(blockHeader.getNumber()).type(REWARD_LABEL).build());
return flatTraces.stream();
}
Aggregations