Search in sources :

Example 1 with BlockResult

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResult in project besu by hyperledger.

the class NewBlockHeadersSubscriptionService method notifySubscribers.

private void notifySubscribers(final Hash newBlockHash) {
    subscriptionManager.notifySubscribersOnWorkerThread(SubscriptionType.NEW_BLOCK_HEADERS, NewBlockHeadersSubscription.class, subscribers -> {
        // memoize
        final Supplier<BlockResult> blockWithTx = Suppliers.memoize(() -> blockWithCompleteTransaction(newBlockHash));
        final Supplier<BlockResult> blockWithoutTx = Suppliers.memoize(() -> blockWithTransactionHash(newBlockHash));
        for (final NewBlockHeadersSubscription subscription : subscribers) {
            final BlockResult newBlock = subscription.getIncludeTransactions() ? blockWithTx.get() : blockWithoutTx.get();
            subscriptionManager.sendMessage(subscription.getSubscriptionId(), newBlock);
        }
    });
}
Also used : BlockResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResult)

Example 2 with BlockResult

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResult in project besu by hyperledger.

the class EthStatsService method sendHistoryReport.

/**
 * Sends a report concerning a set of blocks (range, list of blocks)
 */
private void sendHistoryReport(final List<Long> blocks) {
    final List<BlockResult> blockResults = new ArrayList<>();
    blocks.forEach(blockNumber -> blockchainQueries.blockByNumber(blockNumber).map(tx -> blockResultFactory.transactionComplete(tx, false)).ifPresent(blockResults::add));
    if (!blockResults.isEmpty()) {
        sendMessage(webSocket, new EthStatsRequest(HISTORY, ImmutableHistoryReport.of(enodeURL.getNodeId().toHexString(), blockResults)));
    }
}
Also used : BlockResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResult) EthStatsRequest(org.hyperledger.besu.ethstats.request.EthStatsRequest) ArrayList(java.util.ArrayList)

Example 3 with BlockResult

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResult in project besu by hyperledger.

the class EthGetBlockByNumberLatestDesyncIntegrationTest method shouldReturnCurrentSyncedIfDownloadingWorldState.

@Test
public void shouldReturnCurrentSyncedIfDownloadingWorldState() {
    JsonRpcMethod ethGetBlockNumber = methodsFactoryMidDownload.methods().get("eth_getBlockByNumber");
    Object[] params = { "latest", false };
    JsonRpcRequestContext ctx = new JsonRpcRequestContext(new JsonRpcRequest("2.0", "eth_getBlockByNumber", params));
    Assertions.assertThatNoException().isThrownBy(() -> {
        final JsonRpcResponse resp = ethGetBlockNumber.response(ctx);
        assertThat(resp).isNotNull();
        assertThat(resp).isInstanceOf(JsonRpcSuccessResponse.class);
        Object r = ((JsonRpcSuccessResponse) resp).getResult();
        assertThat(r).isInstanceOf(BlockResult.class);
        BlockResult br = (BlockResult) r;
        assertThat(br.getNumber()).isEqualTo("0x4");
    });
}
Also used : JsonRpcResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) JsonRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest) BlockResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResult) JsonRpcMethod(org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) Test(org.junit.jupiter.api.Test)

Example 4 with BlockResult

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResult in project besu by hyperledger.

the class NewBlockHeadersSubscriptionServiceTest method shouldNotSendMessageWhenBlockAddedIsNotOnCanonicalChain.

@Test
public void shouldNotSendMessageWhenBlockAddedIsNotOnCanonicalChain() {
    final NewBlockHeadersSubscription subscription = createSubscription(false);
    mockSubscriptionManagerNotifyMethod(subscription);
    final Block canonicalBlock = appendBlockWithParent(blockchain, genesisBlock);
    final BlockOptions options = new BlockOptions().setBlockNumber(genesisBlock.getHeader().getNumber() + 1).setParentHash(genesisBlock.getHash()).setDifficulty(genesisBlock.getHeader().getDifficulty().divide(100L));
    appendBlockWithParent(blockchain, options);
    final BlockResult expectedNewBlock = blockResultFactory.transactionHash(blockchainQueriesSpy.blockByHashWithTxHashes(canonicalBlock.getHash()).orElse(null));
    verify(subscriptionManagerSpy, times(1)).notifySubscribersOnWorkerThread(any(), any(), any());
    verify(subscriptionManagerSpy, times(1)).sendMessage(subscriptionIdCaptor.capture(), responseCaptor.capture());
    assertThat(subscriptionIdCaptor.getValue()).isEqualTo(subscription.getSubscriptionId());
    List<JsonRpcResult> capturedNewBlocks = responseCaptor.getAllValues();
    assertThat(capturedNewBlocks.size()).isEqualTo(1);
    assertThat(capturedNewBlocks.get(0)).usingRecursiveComparison().isEqualTo(expectedNewBlock);
}
Also used : BlockOptions(org.hyperledger.besu.ethereum.core.BlockDataGenerator.BlockOptions) BlockResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResult) Block(org.hyperledger.besu.ethereum.core.Block) JsonRpcResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.JsonRpcResult) Test(org.junit.Test)

Example 5 with BlockResult

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResult in project besu by hyperledger.

the class NewBlockHeadersSubscriptionServiceTest method shouldSendMessagesWhenReorgBlockAdded.

@Test
public void shouldSendMessagesWhenReorgBlockAdded() {
    final NewBlockHeadersSubscription subscription = createSubscription(false);
    mockSubscriptionManagerNotifyMethod(subscription);
    final Block canonicalBlock = appendBlockWithParent(blockchain, genesisBlock);
    final BlockOptions options = new BlockOptions().setBlockNumber(genesisBlock.getHeader().getNumber() + 1).setParentHash(genesisBlock.getHash()).setDifficulty(genesisBlock.getHeader().getDifficulty().divide(100L));
    final Block forkBlock = appendBlockWithParent(blockchain, options);
    options.setDifficulty(forkBlock.getHeader().getDifficulty().divide(100L));
    appendBlockWithParent(blockchain, options);
    options.setDifficulty(blockchain.getChainHeadBlock().getHeader().getDifficulty().multiply(2L));
    final Block forkBlock2 = appendBlockWithParent(blockchain, options);
    final BlockResult expectedNewBlock = blockResultFactory.transactionHash(blockchainQueriesSpy.blockByHashWithTxHashes(canonicalBlock.getHash()).orElse(null));
    final BlockResult expectedNewBlock1 = blockResultFactory.transactionHash(blockchainQueriesSpy.blockByHashWithTxHashes(forkBlock2.getHash()).orElse(null));
    verify(subscriptionManagerSpy, times(2)).notifySubscribersOnWorkerThread(any(), any(), any());
    verify(subscriptionManagerSpy, times(2)).sendMessage(subscriptionIdCaptor.capture(), responseCaptor.capture());
    assertThat(subscriptionIdCaptor.getValue()).isEqualTo(subscription.getSubscriptionId());
    List<JsonRpcResult> capturedNewBlocks = responseCaptor.getAllValues();
    assertThat(capturedNewBlocks.size()).isEqualTo(2);
    assertThat(capturedNewBlocks.get(0)).usingRecursiveComparison().isEqualTo(expectedNewBlock);
    assertThat(capturedNewBlocks.get(1)).usingRecursiveComparison().isEqualTo(expectedNewBlock1);
}
Also used : BlockOptions(org.hyperledger.besu.ethereum.core.BlockDataGenerator.BlockOptions) BlockResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResult) Block(org.hyperledger.besu.ethereum.core.Block) JsonRpcResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.JsonRpcResult) Test(org.junit.Test)

Aggregations

BlockResult (org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResult)13 Test (org.junit.Test)7 JsonRpcSuccessResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)6 JsonRpcRequestContext (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext)5 JsonRpcResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse)5 Block (org.hyperledger.besu.ethereum.core.Block)5 JsonRpcRequest (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest)3 JsonRpcMethod (org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod)3 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)3 Test (org.junit.jupiter.api.Test)3 ArrayList (java.util.ArrayList)2 JsonRpcResult (org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.JsonRpcResult)2 BlockOptions (org.hyperledger.besu.ethereum.core.BlockDataGenerator.BlockOptions)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Bytes (org.apache.tuweni.bytes.Bytes)1 Address (org.hyperledger.besu.datatypes.Address)1 Hash (org.hyperledger.besu.datatypes.Hash)1 Wei (org.hyperledger.besu.datatypes.Wei)1 BlockHeaderFunctions (org.hyperledger.besu.ethereum.core.BlockHeaderFunctions)1 Difficulty (org.hyperledger.besu.ethereum.core.Difficulty)1