Search in sources :

Example 1 with BlockWithReceipts

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();
    }
}
Also used : InvalidBlockException(org.hyperledger.besu.ethereum.eth.sync.tasks.exceptions.InvalidBlockException) BlockWithReceipts(org.hyperledger.besu.ethereum.core.BlockWithReceipts)

Example 2 with BlockWithReceipts

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();
}
Also used : ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) BlockWithReceipts(org.hyperledger.besu.ethereum.core.BlockWithReceipts) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) FULL(org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode.FULL) LIGHT(org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode.LIGHT) Collections.singletonList(java.util.Collections.singletonList) Mockito.verify(org.mockito.Mockito.verify) ValidationPolicy(org.hyperledger.besu.ethereum.eth.sync.ValidationPolicy) InvalidBlockException(org.hyperledger.besu.ethereum.eth.sync.tasks.exceptions.InvalidBlockException) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) BlockDataGenerator(org.hyperledger.besu.ethereum.core.BlockDataGenerator) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) Block(org.hyperledger.besu.ethereum.core.Block) ProtocolSpec(org.hyperledger.besu.ethereum.mainnet.ProtocolSpec) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) BlockImporter(org.hyperledger.besu.ethereum.core.BlockImporter) Before(org.junit.Before) Block(org.hyperledger.besu.ethereum.core.Block) BlockWithReceipts(org.hyperledger.besu.ethereum.core.BlockWithReceipts) Test(org.junit.Test)

Example 3 with BlockWithReceipts

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);
}
Also used : Block(org.hyperledger.besu.ethereum.core.Block) BlockWithReceipts(org.hyperledger.besu.ethereum.core.BlockWithReceipts) Test(org.junit.Test)

Example 4 with BlockWithReceipts

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));
}
Also used : BlockWithReceipts(org.hyperledger.besu.ethereum.core.BlockWithReceipts)

Example 5 with BlockWithReceipts

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);
    }
}
Also used : Log(org.hyperledger.besu.evm.log.Log) LogResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.LogResult) TransactionReceipt(org.hyperledger.besu.ethereum.core.TransactionReceipt) BlockWithReceipts(org.hyperledger.besu.ethereum.core.BlockWithReceipts) Block(org.hyperledger.besu.ethereum.core.Block) Test(org.junit.Test)

Aggregations

BlockWithReceipts (org.hyperledger.besu.ethereum.core.BlockWithReceipts)20 Block (org.hyperledger.besu.ethereum.core.Block)13 Test (org.junit.Test)13 TransactionReceipt (org.hyperledger.besu.ethereum.core.TransactionReceipt)8 LogResult (org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.LogResult)7 Log (org.hyperledger.besu.evm.log.Log)6 Address (org.hyperledger.besu.datatypes.Address)4 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 NoSuchElementException (java.util.NoSuchElementException)2 Optional (java.util.Optional)2 Collectors.toList (java.util.stream.Collectors.toList)2 Hash (org.hyperledger.besu.datatypes.Hash)2 Transaction (org.hyperledger.besu.ethereum.core.Transaction)2 InvalidBlockException (org.hyperledger.besu.ethereum.eth.sync.tasks.exceptions.InvalidBlockException)2 MetricsSystem (org.hyperledger.besu.plugin.services.MetricsSystem)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1