Search in sources :

Example 1 with BlockOptions

use of org.hyperledger.besu.ethereum.core.BlockDataGenerator.BlockOptions in project besu by hyperledger.

the class BlockchainQueriesTest method logsShouldBeFlaggedAsRemovedWhenBlockIsNotInCanonicalChain.

@Test
public void logsShouldBeFlaggedAsRemovedWhenBlockIsNotInCanonicalChain() {
    // create initial blockchain
    final BlockchainWithData data = setupBlockchain(3);
    final Block targetBlock = data.blockData.get(data.blockData.size() - 1).block;
    // check that logs have removed = false
    List<LogWithMetadata> logs = data.blockchainQueries.matchingLogs(targetBlock.getHash(), new LogsQuery.Builder().build(), () -> true);
    assertThat(logs).isNotEmpty();
    assertThat(logs).allMatch(l -> !l.isRemoved());
    // Create parallel fork of length 1
    final int forkBlock = 2;
    final int commonAncestor = 1;
    final BlockOptions options = new BlockOptions().setParentHash(data.blockchain.getBlockHashByNumber(commonAncestor).get()).setBlockNumber(forkBlock).setDifficulty(data.blockchain.getBlockHeader(forkBlock).get().getDifficulty().add(10L));
    final Block fork = gen.block(options);
    final List<TransactionReceipt> forkReceipts = gen.receipts(fork);
    // Add fork
    data.blockchain.appendBlock(fork, forkReceipts);
    // check that logs have removed = true
    logs = data.blockchainQueries.matchingLogs(targetBlock.getHash(), new LogsQuery.Builder().build(), () -> true);
    assertThat(logs).isNotEmpty();
    assertThat(logs).allMatch(LogWithMetadata::isRemoved);
}
Also used : BlockOptions(org.hyperledger.besu.ethereum.core.BlockDataGenerator.BlockOptions) LogWithMetadata(org.hyperledger.besu.ethereum.core.LogWithMetadata) TransactionReceipt(org.hyperledger.besu.ethereum.core.TransactionReceipt) Block(org.hyperledger.besu.ethereum.core.Block) Test(org.junit.Test)

Example 2 with BlockOptions

use of org.hyperledger.besu.ethereum.core.BlockDataGenerator.BlockOptions in project besu by hyperledger.

the class ProposedBlockHelpers method createProposalBlock.

public static Block createProposalBlock(final List<Address> validators, final ConsensusRoundIdentifier roundId, final BftExtraDataCodec bftExtraDataCodec) {
    final Bytes extraData = bftExtraDataCodec.encode(new BftExtraData(Bytes.wrap(new byte[32]), Collections.emptyList(), Optional.empty(), roundId.getRoundNumber(), validators));
    final BlockOptions blockOptions = BlockOptions.create().setExtraData(extraData).setBlockNumber(roundId.getSequenceNumber()).setBlockHeaderFunctions(BftBlockHeaderFunctions.forCommittedSeal(bftExtraDataCodec)).hasOmmers(false).hasTransactions(false);
    if (validators.size() > 0) {
        blockOptions.setCoinbase(validators.get(0));
    }
    return new BlockDataGenerator().block(blockOptions);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) BlockOptions(org.hyperledger.besu.ethereum.core.BlockDataGenerator.BlockOptions) BlockDataGenerator(org.hyperledger.besu.ethereum.core.BlockDataGenerator)

Example 3 with BlockOptions

use of org.hyperledger.besu.ethereum.core.BlockDataGenerator.BlockOptions in project besu by hyperledger.

the class PrunerTest method appendBlockWithParent.

private Block appendBlockWithParent(final MutableBlockchain blockchain, final Block parent) {
    BlockOptions options = new BlockOptions().setBlockNumber(parent.getHeader().getNumber() + 1).setParentHash(parent.getHash());
    final Block newBlock = gen.block(options);
    final List<TransactionReceipt> receipts = gen.receipts(newBlock);
    blockchain.appendBlock(newBlock, receipts);
    return newBlock;
}
Also used : BlockOptions(org.hyperledger.besu.ethereum.core.BlockDataGenerator.BlockOptions) TransactionReceipt(org.hyperledger.besu.ethereum.core.TransactionReceipt) Block(org.hyperledger.besu.ethereum.core.Block)

Example 4 with BlockOptions

use of org.hyperledger.besu.ethereum.core.BlockDataGenerator.BlockOptions 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 BlockOptions

use of org.hyperledger.besu.ethereum.core.BlockDataGenerator.BlockOptions 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

BlockOptions (org.hyperledger.besu.ethereum.core.BlockDataGenerator.BlockOptions)8 Block (org.hyperledger.besu.ethereum.core.Block)6 BlockDataGenerator (org.hyperledger.besu.ethereum.core.BlockDataGenerator)3 TransactionReceipt (org.hyperledger.besu.ethereum.core.TransactionReceipt)3 Test (org.junit.Test)3 Bytes (org.apache.tuweni.bytes.Bytes)2 BlockResult (org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResult)2 JsonRpcResult (org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.JsonRpcResult)2 ArrayList (java.util.ArrayList)1 PkiQbftExtraData (org.hyperledger.besu.consensus.qbft.pki.PkiQbftExtraData)1 BlockWithReceipts (org.hyperledger.besu.ethereum.core.BlockWithReceipts)1 LogWithMetadata (org.hyperledger.besu.ethereum.core.LogWithMetadata)1 Transaction (org.hyperledger.besu.ethereum.core.Transaction)1 Log (org.hyperledger.besu.evm.log.Log)1