use of org.hyperledger.besu.ethereum.api.query.BlockWithMetadata in project besu by hyperledger.
the class JsonRpcHttpServiceTest method blockWithMetadataAndTxHashes.
public BlockWithMetadata<Hash, Hash> blockWithMetadataAndTxHashes(final Block block) {
final Difficulty td = block.getHeader().getDifficulty().add(10L);
final int size = block.calculateSize();
final List<Hash> txs = block.getBody().getTransactions().stream().map(Transaction::getHash).collect(Collectors.toList());
final List<Hash> ommers = block.getBody().getOmmers().stream().map(BlockHeader::getHash).collect(Collectors.toList());
return new BlockWithMetadata<>(block.getHeader(), txs, ommers, td, size);
}
use of org.hyperledger.besu.ethereum.api.query.BlockWithMetadata in project besu by hyperledger.
the class BlockDataFetcherTest method ibftMiner.
@Test
public void ibftMiner() throws Exception {
// IBFT can mine blocks with a coinbase that is an empty account, hence not stored and returned
// as null. The compromise is to report zeros and empty on query from a block.
final Address testAddress = Address.fromHexString("0xdeadbeef");
when(environment.getArgument(ArgumentMatchers.eq("number"))).thenReturn(1L);
when(environment.getArgument(ArgumentMatchers.eq("hash"))).thenReturn(null);
when(environment.getGraphQlContext()).thenReturn(graphQLContext);
when(graphQLContext.get(GraphQLContextType.BLOCKCHAIN_QUERIES)).thenReturn(query);
when(query.blockByNumber(ArgumentMatchers.anyLong())).thenReturn(Optional.of(new BlockWithMetadata<>(header, null, null, null, 0)));
when(header.getCoinbase()).thenReturn(testAddress);
when(query.getWorldState(anyLong())).thenReturn(Optional.of(mutableWorldState));
final Optional<NormalBlockAdapter> maybeBlock = fetcher.get(environment);
assertThat(maybeBlock).isPresent();
assertThat(maybeBlock.get().getMiner(environment)).isPresent();
assertThat(((EmptyAccountAdapter) maybeBlock.get().getMiner(environment).get()).getBalance()).isPresent();
assertThat(((EmptyAccountAdapter) maybeBlock.get().getMiner(environment).get()).getAddress()).contains(testAddress);
}
use of org.hyperledger.besu.ethereum.api.query.BlockWithMetadata 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();
}
use of org.hyperledger.besu.ethereum.api.query.BlockWithMetadata in project besu by hyperledger.
the class DebugStorageRangeAt method response.
@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
final BlockParameterOrBlockHash blockParameterOrBlockHash = requestContext.getRequiredParameter(0, BlockParameterOrBlockHash.class);
final int transactionIndex = requestContext.getRequiredParameter(1, Integer.class);
final Address accountAddress = requestContext.getRequiredParameter(2, Address.class);
final Hash startKey = Hash.fromHexStringLenient(requestContext.getRequiredParameter(3, String.class));
final int limit = requestContext.getRequiredParameter(4, Integer.class);
final Optional<Hash> blockHashOptional = hashFromParameter(blockParameterOrBlockHash);
if (blockHashOptional.isEmpty()) {
return emptyResponse(requestContext);
}
final Hash blockHash = blockHashOptional.get();
final Optional<BlockHeader> blockHeaderOptional = blockchainQueries.get().blockByHash(blockHash).map(BlockWithMetadata::getHeader);
if (blockHeaderOptional.isEmpty()) {
return emptyResponse(requestContext);
}
final Optional<TransactionWithMetadata> optional = blockchainQueries.get().transactionByBlockHashAndIndex(blockHash, transactionIndex);
return optional.map(transactionWithMetadata -> (blockReplay.get().afterTransactionInBlock(blockHash, transactionWithMetadata.getTransaction().getHash(), (transaction, blockHeader, blockchain, worldState, transactionProcessor) -> extractStorageAt(requestContext, accountAddress, startKey, limit, worldState)).orElseGet(() -> emptyResponse(requestContext)))).orElseGet(() -> blockchainQueries.get().getWorldState(blockHeaderOptional.get().getNumber()).map(worldState -> extractStorageAt(requestContext, accountAddress, startKey, limit, worldState)).orElseGet(() -> emptyResponse(requestContext)));
}
use of org.hyperledger.besu.ethereum.api.query.BlockWithMetadata in project besu by hyperledger.
the class EthGetMinerDataByBlockHashTest method successTest.
@Test
public void successTest() {
final BlockHeader header = blockHeaderTestFixture.buildHeader();
final BlockWithMetadata<TransactionWithMetadata, Hash> blockWithMetadata = new BlockWithMetadata<>(header, Collections.emptyList(), Collections.emptyList(), Difficulty.of(100L), 5);
when(blockchainQueries.blockByHash(any())).thenReturn(Optional.of(blockWithMetadata));
when(protocolSchedule.getByBlockNumber(header.getNumber())).thenReturn(protocolSpec);
when(protocolSpec.getBlockReward()).thenReturn(Wei.fromEth(2));
when(blockchainQueries.getBlockchain()).thenReturn(blockChain);
JsonRpcRequest request = new JsonRpcRequest("2.0", ETH_METHOD, Arrays.array("0x1349e5d4002e72615ae371dc173ba530bf98a7bef886d5b3b00ca5f217565039"));
JsonRpcRequestContext requestContext = new JsonRpcRequestContext(request);
JsonRpcResponse response = method.response(requestContext);
assertThat(response).isNotNull().isInstanceOf(JsonRpcSuccessResponse.class);
assertThat(((JsonRpcSuccessResponse) response).getResult()).isNotNull();
assertThat(((JsonRpcSuccessResponse) response).getResult()).hasFieldOrProperty("netBlockReward").hasFieldOrProperty("staticBlockReward").hasFieldOrProperty("transactionFee").hasFieldOrProperty("uncleInclusionReward").hasFieldOrProperty("uncleRewards").hasFieldOrProperty("coinbase").hasFieldOrProperty("extraData").hasFieldOrProperty("difficulty").hasFieldOrProperty("totalDifficulty");
}
Aggregations