Search in sources :

Example 1 with ReorgEvent

use of tech.pegasys.teku.storage.api.TrackingChainHeadChannel.ReorgEvent in project teku by ConsenSys.

the class ForkChoiceTest method shouldNotTriggerReorgWhenEmptyHeadSlotFilled.

@Test
void shouldNotTriggerReorgWhenEmptyHeadSlotFilled() {
    // Run fork choice with an empty slot 1
    processHead(ONE);
    assertThat(recentChainData.getBestBlockRoot()).contains(genesis.getRoot());
    // Then rerun with a filled slot 1
    final SignedBlockAndState slot1Block = storageSystem.chainUpdater().advanceChain(ONE);
    processHead(ONE);
    assertThat(recentChainData.getBestBlockRoot()).contains(slot1Block.getRoot());
    final List<ReorgEvent> reorgEvents = storageSystem.chainHeadChannel().getReorgEvents();
    assertThat(reorgEvents).isEmpty();
}
Also used : SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState) ReorgEvent(tech.pegasys.teku.storage.api.TrackingChainHeadChannel.ReorgEvent) Test(org.junit.jupiter.api.Test)

Example 2 with ReorgEvent

use of tech.pegasys.teku.storage.api.TrackingChainHeadChannel.ReorgEvent 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));
}
Also used : ChainBuilder(tech.pegasys.teku.core.ChainBuilder) BlockOptions(tech.pegasys.teku.core.ChainBuilder.BlockOptions) SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState) ReorgEvent(tech.pegasys.teku.storage.api.TrackingChainHeadChannel.ReorgEvent) Test(org.junit.jupiter.api.Test)

Example 3 with ReorgEvent

use of tech.pegasys.teku.storage.api.TrackingChainHeadChannel.ReorgEvent in project teku by ConsenSys.

the class RecentChainDataTest method updateHead_reorgEventWhenChainSwitchesToNewBlockAtLaterSlot.

@Test
public void updateHead_reorgEventWhenChainSwitchesToNewBlockAtLaterSlot() {
    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));
    forkBuilder.generateBlockAtSlot(UInt64.valueOf(2), blockOptions.get(1));
    // Fork extends a slot further
    final SignedBlockAndState latestForkBlockAndState = forkBuilder.generateBlockAtSlot(3);
    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));
}
Also used : ChainBuilder(tech.pegasys.teku.core.ChainBuilder) BlockOptions(tech.pegasys.teku.core.ChainBuilder.BlockOptions) SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState) ReorgEvent(tech.pegasys.teku.storage.api.TrackingChainHeadChannel.ReorgEvent) Test(org.junit.jupiter.api.Test)

Example 4 with ReorgEvent

use of tech.pegasys.teku.storage.api.TrackingChainHeadChannel.ReorgEvent in project teku by ConsenSys.

the class RecentChainDataTest method updateHead_noReorgEventWhenBlockFillsEmptyHeadSlot.

@Test
public void updateHead_noReorgEventWhenBlockFillsEmptyHeadSlot() {
    initPostGenesis();
    final SignedBlockAndState slot1Block = chainBuilder.generateBlockAtSlot(1);
    importBlocksAndStates(recentChainData, chainBuilder);
    recentChainData.updateHead(slot1Block.getRoot(), UInt64.valueOf(2));
    assertThat(storageSystem.chainHeadChannel().getReorgEvents()).isEmpty();
    assertThat(getReorgCountMetric(storageSystem)).isZero();
    final SignedBlockAndState slot2Block = chainBuilder.generateBlockAtSlot(2);
    importBlocksAndStates(recentChainData, chainBuilder);
    recentChainData.updateHead(slot2Block.getRoot(), slot2Block.getSlot());
    final List<ReorgEvent> reorgEvents = storageSystem.chainHeadChannel().getReorgEvents();
    assertThat(reorgEvents).isEmpty();
    assertThat(getReorgCountMetric(storageSystem)).isEqualTo(0);
}
Also used : SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState) ReorgEvent(tech.pegasys.teku.storage.api.TrackingChainHeadChannel.ReorgEvent) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)4 SignedBlockAndState (tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState)4 ReorgEvent (tech.pegasys.teku.storage.api.TrackingChainHeadChannel.ReorgEvent)4 ChainBuilder (tech.pegasys.teku.core.ChainBuilder)2 BlockOptions (tech.pegasys.teku.core.ChainBuilder.BlockOptions)2