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);
}
});
}
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)));
}
}
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");
});
}
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);
}
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);
}
Aggregations