use of tech.pegasys.teku.core.ChainBuilder in project teku by ConsenSys.
the class RecentChainDataTest method updateHead_reorgEventWhenChainSwitchesToNewBlockAtSameSlot.
@Test
public void updateHead_reorgEventWhenChainSwitchesToNewBlockAtSameSlot() {
initPreGenesis();
final ChainBuilder chainBuilder = ChainBuilder.create(spec, BLSKeyGenerator.generateKeyPairs(16));
final SignedBlockAndState genesis = chainBuilder.generateGenesis();
recentChainData.initializeFromGenesis(genesis.getState(), UInt64.ZERO);
assertThat(storageSystem.chainHeadChannel().getReorgEvents()).isEmpty();
chainBuilder.generateBlockAtSlot(1);
// Set target slot at which to create duplicate blocks
// and generate block options to make each block unique
final List<BlockOptions> blockOptions = chainBuilder.streamValidAttestationsForBlockAtSlot(ONE).map(attestation -> BlockOptions.create().addAttestation(attestation)).limit(2).collect(toList());
final ChainBuilder forkBuilder = chainBuilder.fork();
final SignedBlockAndState latestBlockAndState = chainBuilder.generateBlockAtSlot(UInt64.valueOf(2), blockOptions.get(0));
final SignedBlockAndState latestForkBlockAndState = forkBuilder.generateBlockAtSlot(UInt64.valueOf(2), blockOptions.get(1));
importBlocksAndStates(recentChainData, chainBuilder, forkBuilder);
// Update to head block of original chain.
recentChainData.updateHead(latestBlockAndState.getRoot(), latestBlockAndState.getSlot());
assertThat(storageSystem.chainHeadChannel().getReorgEvents()).isEmpty();
// Switch to fork.
recentChainData.updateHead(latestForkBlockAndState.getRoot(), latestForkBlockAndState.getSlot());
// Check reorg event
assertThat(storageSystem.chainHeadChannel().getReorgEvents()).containsExactly(new ReorgEvent(latestForkBlockAndState.getRoot(), latestForkBlockAndState.getSlot(), latestForkBlockAndState.getStateRoot(), latestBlockAndState.getRoot(), latestBlockAndState.getStateRoot(), ONE));
}
use of tech.pegasys.teku.core.ChainBuilder in project teku by ConsenSys.
the class RecentChainDataTest method getBlockRootBySlotWithHeadRoot_withForkRoot.
@Test
public void getBlockRootBySlotWithHeadRoot_withForkRoot() {
initPostGenesis();
// Build small chain
for (int i = 0; i < 5; i++) {
advanceChain(recentChainData);
}
// Split the chain
final ChainBuilder fork = chainBuilder.fork();
final UInt64 chainSplitSlot = chainBuilder.getLatestSlot();
for (int i = 0; i < 5; i++) {
final UInt64 canonicalBlockSlot = chainSplitSlot.plus(i * 2 + 2);
final UInt64 forkSlot = chainSplitSlot.plus(i * 2 + 1);
updateHead(recentChainData, chainBuilder.generateBlockAtSlot(canonicalBlockSlot));
saveBlock(recentChainData, fork.generateBlockAtSlot(forkSlot));
}
final Bytes32 headRoot = fork.getLatestBlockAndState().getRoot();
for (int i = 0; i < fork.getLatestSlot().intValue(); i++) {
final UInt64 targetSlot = UInt64.valueOf(i);
final SignedBlockAndState expectedBlock = fork.getLatestBlockAndStateAtSlot(targetSlot);
if (targetSlot.compareTo(chainSplitSlot) > 0) {
// Sanity check that fork differs from main chain
assertThat(expectedBlock).isNotEqualTo(chainBuilder.getLatestBlockAndStateAtSlot(targetSlot));
}
assertThat(recentChainData.getBlockRootBySlot(targetSlot, headRoot)).contains(expectedBlock.getRoot());
}
}
use of tech.pegasys.teku.core.ChainBuilder in project teku by ConsenSys.
the class RecentChainDataTest method initializeFromGenesis_withTimeGreaterThanGenesisTime.
@Test
public void initializeFromGenesis_withTimeGreaterThanGenesisTime() {
initPreGenesis();
final ChainBuilder chainBuilder = ChainBuilder.create(spec);
final UInt64 genesisTime = UInt64.valueOf(5000);
final UInt64 time = genesisTime.plus(100);
final SignedBlockAndState genesis = chainBuilder.generateGenesis(genesisTime, false);
recentChainData.initializeFromGenesis(genesis.getState(), time);
assertThat(recentChainData.getStore().getTime()).isEqualTo(time);
}
use of tech.pegasys.teku.core.ChainBuilder in project teku by ConsenSys.
the class RecentChainDataTest method getAncestorsOnFork.
@Test
public void getAncestorsOnFork() {
initPreGenesis();
final ChainBuilder chainBuilder = ChainBuilder.create(BLSKeyGenerator.generateKeyPairs(16));
final SignedBlockAndState genesis = chainBuilder.generateGenesis();
recentChainData.initializeFromGenesis(genesis.getState(), UInt64.ZERO);
chainBuilder.generateBlockAtSlot(1);
final List<BlockOptions> blockOptions = chainBuilder.streamValidAttestationsForBlockAtSlot(ONE).map(attestation -> BlockOptions.create().addAttestation(attestation)).limit(2).collect(toList());
final ChainBuilder forkBuilder = chainBuilder.fork();
final SignedBlockAndState firstBlockAndState = chainBuilder.generateBlockAtSlot(UInt64.valueOf(2), blockOptions.get(0));
final SignedBlockAndState latestBlockAndState = chainBuilder.generateBlockAtSlot(UInt64.valueOf(3));
final SignedBlockAndState forkFirstBlockAndState = forkBuilder.generateBlockAtSlot(UInt64.valueOf(2), blockOptions.get(1));
final SignedBlockAndState latestForkBlockAndState = forkBuilder.generateBlockAtSlot(UInt64.valueOf(3), blockOptions.get(1));
importBlocksAndStates(recentChainData, chainBuilder, forkBuilder);
assertThat(recentChainData.getAncestorsOnFork(UInt64.valueOf(1), latestBlockAndState.getRoot())).containsOnly(Map.entry(UInt64.valueOf(2), firstBlockAndState.getRoot()), Map.entry(UInt64.valueOf(3), latestBlockAndState.getRoot()));
assertThat(recentChainData.getAncestorsOnFork(UInt64.valueOf(1), latestForkBlockAndState.getRoot())).containsOnly(Map.entry(UInt64.valueOf(2), forkFirstBlockAndState.getRoot()), Map.entry(UInt64.valueOf(3), latestForkBlockAndState.getRoot()));
}
use of tech.pegasys.teku.core.ChainBuilder in project teku by ConsenSys.
the class HistoricalBatchFetcherTest method run_requestBatchWithSkippedSlots.
@Test
public void run_requestBatchWithSkippedSlots() {
final ChainBuilder chain = ChainBuilder.create(spec);
final int batchSize = 20;
final SignedBlockAndState genesis = chain.generateGenesis();
chain.generateBlockAtSlot(5);
chain.generateBlocksUpToSlot(10);
chain.generateBlockAtSlot(15);
chain.generateBlocksUpToSlot(30);
final SignedBeaconBlock latestBlock = chain.getBlockAtSlot(19);
final List<SignedBeaconBlock> targetBatch = chain.streamBlocksAndStates(0, latestBlock.getSlot().longValue()).map(SignedBlockAndState::getBlock).collect(Collectors.toList());
peer = RespondingEth2Peer.create(spec, chain);
fetcher = new HistoricalBatchFetcher(storageUpdateChannel, signatureVerifier, chainDataClient, spec, peer, latestBlock.getSlot(), latestBlock.getRoot(), UInt64.valueOf(batchSize), maxRequests);
assertThat(peer.getOutstandingRequests()).isEqualTo(0);
final SafeFuture<BeaconBlockSummary> future = fetcher.run();
assertThat(peer.getOutstandingRequests()).isEqualTo(1);
peer.completePendingRequests();
assertThat(peer.getOutstandingRequests()).isEqualTo(0);
assertThat(future).isCompletedWithValue(genesis.getBlock());
verify(storageUpdateChannel).onFinalizedBlocks(blockCaptor.capture());
assertThat(blockCaptor.getValue()).containsExactlyElementsOf(targetBatch);
}
Aggregations