Search in sources :

Example 1 with BlockWithMetadata

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);
}
Also used : Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) BlockWithMetadata(org.hyperledger.besu.ethereum.api.query.BlockWithMetadata) Hash(org.hyperledger.besu.datatypes.Hash)

Example 2 with BlockWithMetadata

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);
}
Also used : Address(org.hyperledger.besu.datatypes.Address) EmptyAccountAdapter(org.hyperledger.besu.ethereum.api.graphql.internal.pojoadapter.EmptyAccountAdapter) BlockWithMetadata(org.hyperledger.besu.ethereum.api.query.BlockWithMetadata) NormalBlockAdapter(org.hyperledger.besu.ethereum.api.graphql.internal.pojoadapter.NormalBlockAdapter) Test(org.junit.Test)

Example 3 with BlockWithMetadata

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

Example 4 with BlockWithMetadata

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)));
}
Also used : WorldState(org.hyperledger.besu.evm.worldstate.WorldState) Account(org.hyperledger.besu.evm.account.Account) AccountStorageEntry(org.hyperledger.besu.evm.account.AccountStorageEntry) 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) NavigableMap(java.util.NavigableMap) Address(org.hyperledger.besu.datatypes.Address) Supplier(java.util.function.Supplier) RpcMethod(org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod) BlockWithMetadata(org.hyperledger.besu.ethereum.api.query.BlockWithMetadata) TransactionWithMetadata(org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) BlockParameterOrBlockHash(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameterOrBlockHash) Optional(java.util.Optional) Suppliers(com.google.common.base.Suppliers) DebugStorageRangeAtResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.DebugStorageRangeAtResult) Collections(java.util.Collections) Bytes32(org.apache.tuweni.bytes.Bytes32) Hash(org.hyperledger.besu.datatypes.Hash) BlockReplay(org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockReplay) Address(org.hyperledger.besu.datatypes.Address) TransactionWithMetadata(org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata) BlockParameterOrBlockHash(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameterOrBlockHash) BlockWithMetadata(org.hyperledger.besu.ethereum.api.query.BlockWithMetadata) BlockParameterOrBlockHash(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameterOrBlockHash) Hash(org.hyperledger.besu.datatypes.Hash) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader)

Example 5 with BlockWithMetadata

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");
}
Also used : JsonRpcResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse) JsonRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) TransactionWithMetadata(org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata) BlockWithMetadata(org.hyperledger.besu.ethereum.api.query.BlockWithMetadata) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Hash(org.hyperledger.besu.datatypes.Hash) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) Test(org.junit.Test)

Aggregations

BlockWithMetadata (org.hyperledger.besu.ethereum.api.query.BlockWithMetadata)14 Hash (org.hyperledger.besu.datatypes.Hash)12 TransactionWithMetadata (org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata)9 JsonRpcRequestContext (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext)8 JsonRpcSuccessResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)7 ArrayList (java.util.ArrayList)5 JsonRpcResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse)5 BlockchainQueries (org.hyperledger.besu.ethereum.api.query.BlockchainQueries)5 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)5 Test (org.junit.Test)5 Supplier (java.util.function.Supplier)4 Address (org.hyperledger.besu.datatypes.Address)4 JsonRpcRequest (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest)4 Suppliers (com.google.common.base.Suppliers)3 Collections (java.util.Collections)3 List (java.util.List)3 Optional (java.util.Optional)3 Bytes32 (org.apache.tuweni.bytes.Bytes32)3 RpcMethod (org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod)3 BlockParameterOrBlockHash (org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameterOrBlockHash)3