use of org.hyperledger.besu.ethereum.core.BlockWithReceipts in project besu by hyperledger.
the class FastImportBlocksStep method accept.
@Override
public void accept(final List<BlockWithReceipts> blocksWithReceipts) {
final long startTime = System.nanoTime();
for (final BlockWithReceipts blockWithReceipts : blocksWithReceipts) {
if (!importBlock(blockWithReceipts)) {
throw new InvalidBlockException("Failed to import block", blockWithReceipts.getHeader().getNumber(), blockWithReceipts.getHash());
}
traceLambda(LOG, "Imported block {}", blockWithReceipts.getBlock()::toLogString);
}
if (logStartBlock.isEmpty()) {
logStartBlock = OptionalLong.of(blocksWithReceipts.get(0).getNumber());
}
final long lastBlock = blocksWithReceipts.get(blocksWithReceipts.size() - 1).getNumber();
// ethContext is not available in tests
int peerCount = -1;
if (ethContext != null && ethContext.getEthPeers().peerCount() >= 0) {
peerCount = ethContext.getEthPeers().peerCount();
}
final long endTime = System.nanoTime();
accumulatedTime += TimeUnit.MILLISECONDS.convert(endTime - startTime, TimeUnit.NANOSECONDS);
if (accumulatedTime > TEN_SECONDS) {
LOG.info("Completed importing chain segment {} to {} ({} blocks in {}ms), Peers: {}", logStartBlock.getAsLong(), lastBlock, lastBlock - logStartBlock.getAsLong() + 1, accumulatedTime, peerCount);
accumulatedTime = 0L;
logStartBlock = OptionalLong.empty();
}
}
use of org.hyperledger.besu.ethereum.core.BlockWithReceipts in project besu by hyperledger.
the class FastImportBlocksStepTest method shouldImportBlocks.
@Test
public void shouldImportBlocks() {
final List<Block> blocks = gen.blockSequence(5);
final List<BlockWithReceipts> blocksWithReceipts = blocks.stream().map(block -> new BlockWithReceipts(block, gen.receipts(block))).collect(toList());
for (final BlockWithReceipts blockWithReceipts : blocksWithReceipts) {
when(blockImporter.fastImportBlock(protocolContext, blockWithReceipts.getBlock(), blockWithReceipts.getReceipts(), FULL, LIGHT)).thenReturn(true);
}
importBlocksStep.accept(blocksWithReceipts);
for (final BlockWithReceipts blockWithReceipts : blocksWithReceipts) {
verify(protocolSchedule).getByBlockNumber(blockWithReceipts.getNumber());
}
verify(validationPolicy, times(blocks.size())).getValidationModeForNextBlock();
}
use of org.hyperledger.besu.ethereum.core.BlockWithReceipts in project besu by hyperledger.
the class FastImportBlocksStepTest method shouldThrowExceptionWhenValidationFails.
@Test
public void shouldThrowExceptionWhenValidationFails() {
final Block block = gen.block();
final BlockWithReceipts blockWithReceipts = new BlockWithReceipts(block, gen.receipts(block));
when(blockImporter.fastImportBlock(protocolContext, block, blockWithReceipts.getReceipts(), FULL, LIGHT)).thenReturn(false);
assertThatThrownBy(() -> importBlocksStep.accept(singletonList(blockWithReceipts))).isInstanceOf(InvalidBlockException.class);
}
use of org.hyperledger.besu.ethereum.core.BlockWithReceipts in project besu by hyperledger.
the class DefaultBlockchain method appendBlock.
@Override
public synchronized void appendBlock(final Block block, final List<TransactionReceipt> receipts) {
checkArgument(block.getBody().getTransactions().size() == receipts.size(), "Supplied receipts do not match block transactions.");
if (blockIsAlreadyTracked(block)) {
return;
}
checkArgument(blockIsConnected(block), "Attempt to append non-connected block.");
final BlockAddedEvent blockAddedEvent = appendBlockHelper(new BlockWithReceipts(block, receipts));
blockAddedObservers.forEach(observer -> observer.onBlockAdded(blockAddedEvent));
}
use of org.hyperledger.besu.ethereum.core.BlockWithReceipts in project besu by hyperledger.
the class LogsSubscriptionServiceTest method multipleSubscriptionsForSingleMatchingLog.
@Test
public void multipleSubscriptionsForSingleMatchingLog() {
final BlockWithReceipts blockWithReceipts = generateBlock(2, 2, 2);
final Block block = blockWithReceipts.getBlock();
final List<TransactionReceipt> receipts = blockWithReceipts.getReceipts();
final int txIndex = 1;
final int logIndex = 1;
final Log targetLog = receipts.get(txIndex).getLogsList().get(logIndex);
final List<LogsSubscription> subscriptions = Stream.generate(() -> createSubscription(targetLog.getLogger())).limit(3).collect(Collectors.toList());
registerSubscriptions(subscriptions);
blockchain.appendBlock(blockWithReceipts.getBlock(), blockWithReceipts.getReceipts());
for (LogsSubscription subscription : subscriptions) {
final ArgumentCaptor<LogResult> captor = ArgumentCaptor.forClass(LogResult.class);
verify(subscriptionManager).sendMessage(eq(subscription.getSubscriptionId()), captor.capture());
final List<LogResult> logResults = captor.getAllValues();
assertThat(logResults).hasSize(1);
final LogResult result = logResults.get(0);
assertLogResultMatches(result, block, receipts, txIndex, logIndex, false);
}
}
Aggregations