Search in sources :

Example 11 with Wei

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()));
}
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 12 with Wei

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);
}
Also used : JsonRpcResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) Wei(org.hyperledger.besu.datatypes.Wei) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) Test(org.junit.Test)

Example 13 with Wei

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()));
}
Also used : TransactionReceiptStatusResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.TransactionReceiptStatusResult) Transaction(org.hyperledger.besu.ethereum.core.Transaction) TransactionReceiptWithMetadata(org.hyperledger.besu.ethereum.api.query.TransactionReceiptWithMetadata) Wei(org.hyperledger.besu.datatypes.Wei) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) BlockDataGenerator(org.hyperledger.besu.ethereum.core.BlockDataGenerator) Test(org.junit.Test)

Example 14 with Wei

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);
}
Also used : EthProtocolManager(org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager) EthContext(org.hyperledger.besu.ethereum.eth.manager.EthContext) MiningParameters(org.hyperledger.besu.ethereum.core.MiningParameters) Transaction(org.hyperledger.besu.ethereum.core.Transaction) TransactionInvalidReason(org.hyperledger.besu.ethereum.transaction.TransactionInvalidReason) Wei(org.hyperledger.besu.datatypes.Wei) Test(org.junit.Test)

Example 15 with Wei

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();
}
Also used : Trace(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.Trace) MiningBeneficiaryCalculator(org.hyperledger.besu.ethereum.mainnet.MiningBeneficiaryCalculator) ProtocolSpec(org.hyperledger.besu.ethereum.mainnet.ProtocolSpec) ArrayList(java.util.ArrayList) Wei(org.hyperledger.besu.datatypes.Wei) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader)

Aggregations

Wei (org.hyperledger.besu.datatypes.Wei)69 Address (org.hyperledger.besu.datatypes.Address)24 Test (org.junit.Test)20 Transaction (org.hyperledger.besu.ethereum.core.Transaction)18 Bytes (org.apache.tuweni.bytes.Bytes)16 Hash (org.hyperledger.besu.datatypes.Hash)14 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)13 MutableAccount (org.hyperledger.besu.evm.account.MutableAccount)12 Account (org.hyperledger.besu.evm.account.Account)9 WorldUpdater (org.hyperledger.besu.evm.worldstate.WorldUpdater)9 Optional (java.util.Optional)8 TransactionInvalidReason (org.hyperledger.besu.ethereum.transaction.TransactionInvalidReason)7 ArrayList (java.util.ArrayList)6 JsonRpcSuccessResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)6 ProtocolSchedule (org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule)6 JsonObject (io.vertx.core.json.JsonObject)5 Response (okhttp3.Response)5 Bytes32 (org.apache.tuweni.bytes.Bytes32)5 MessageFrame (org.hyperledger.besu.evm.frame.MessageFrame)5 List (java.util.List)4