Search in sources :

Example 1 with BaseUInt256Value

use of org.apache.tuweni.units.bigints.BaseUInt256Value in project besu by hyperledger.

the class EthGetMinerDataByBlockHash method createMinerDataResult.

public static MinerDataResult createMinerDataResult(final BlockWithMetadata<TransactionWithMetadata, Hash> block, final ProtocolSchedule protocolSchedule, final BlockchainQueries blockchainQueries) {
    final BlockHeader blockHeader = block.getHeader();
    final ProtocolSpec protocolSpec = protocolSchedule.getByBlockNumber(blockHeader.getNumber());
    final Wei staticBlockReward = protocolSpec.getBlockReward();
    final Wei transactionFee = block.getTransactions().stream().map(t -> blockchainQueries.transactionReceiptByTransactionHash(t.getTransaction().getHash()).map(receipt -> receipt.getTransaction().getEffectiveGasPrice(receipt.getBaseFee()).multiply(receipt.getGasUsed())).orElse(Wei.ZERO)).reduce(Wei.ZERO, BaseUInt256Value::add);
    final Wei uncleInclusionReward = staticBlockReward.multiply(block.getOmmers().size()).divide(32);
    final Wei netBlockReward = staticBlockReward.add(transactionFee).add(uncleInclusionReward);
    final List<UncleRewardResult> uncleRewards = new ArrayList<>();
    blockchainQueries.getBlockchain().getBlockByNumber(block.getHeader().getNumber()).ifPresent(blockBody -> blockBody.getBody().getOmmers().forEach(header -> uncleRewards.add(ImmutableUncleRewardResult.builder().hash(header.getHash().toHexString()).coinbase(header.getCoinbase().toHexString()).build())));
    return ImmutableMinerDataResult.builder().netBlockReward(netBlockReward.toHexString()).staticBlockReward(staticBlockReward.toHexString()).transactionFee(transactionFee.toHexString()).uncleInclusionReward(uncleInclusionReward.toHexString()).uncleRewards(uncleRewards).coinbase(blockHeader.getCoinbase().toHexString()).extraData(blockHeader.getExtraData().toHexString()).difficulty(blockHeader.getDifficulty().toHexString()).totalDifficulty(block.getTotalDifficulty().toHexString()).build();
}
Also used : ImmutableMinerDataResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.ImmutableMinerDataResult) BaseUInt256Value(org.apache.tuweni.units.bigints.BaseUInt256Value) MinerDataResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.MinerDataResult) ImmutableUncleRewardResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.ImmutableUncleRewardResult) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) JsonRpcResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse) BlockchainQueries(org.hyperledger.besu.ethereum.api.query.BlockchainQueries) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) Supplier(java.util.function.Supplier) UncleRewardResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.MinerDataResult.UncleRewardResult) ArrayList(java.util.ArrayList) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) List(java.util.List) RpcMethod(org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod) BlockWithMetadata(org.hyperledger.besu.ethereum.api.query.BlockWithMetadata) TransactionWithMetadata(org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata) Wei(org.hyperledger.besu.datatypes.Wei) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) Suppliers(com.google.common.base.Suppliers) ProtocolSpec(org.hyperledger.besu.ethereum.mainnet.ProtocolSpec) Hash(org.hyperledger.besu.datatypes.Hash) BaseUInt256Value(org.apache.tuweni.units.bigints.BaseUInt256Value) ImmutableUncleRewardResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.ImmutableUncleRewardResult) UncleRewardResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.MinerDataResult.UncleRewardResult) 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

Suppliers (com.google.common.base.Suppliers)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Supplier (java.util.function.Supplier)1 BaseUInt256Value (org.apache.tuweni.units.bigints.BaseUInt256Value)1 Hash (org.hyperledger.besu.datatypes.Hash)1 Wei (org.hyperledger.besu.datatypes.Wei)1 RpcMethod (org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod)1 JsonRpcRequestContext (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext)1 JsonRpcResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse)1 JsonRpcSuccessResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)1 ImmutableMinerDataResult (org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.ImmutableMinerDataResult)1 ImmutableUncleRewardResult (org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.ImmutableUncleRewardResult)1 MinerDataResult (org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.MinerDataResult)1 UncleRewardResult (org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.MinerDataResult.UncleRewardResult)1 BlockWithMetadata (org.hyperledger.besu.ethereum.api.query.BlockWithMetadata)1 BlockchainQueries (org.hyperledger.besu.ethereum.api.query.BlockchainQueries)1 TransactionWithMetadata (org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata)1 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)1 ProtocolSchedule (org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule)1