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