use of tech.pegasys.teku.spec.datastructures.forkchoice.ReadOnlyForkChoiceStrategy in project teku by ConsenSys.
the class ForkChoiceTest method assertForkChoiceUpdateNotification.
private void assertForkChoiceUpdateNotification(final SignedBlockAndState blockAndState, final boolean optimisticHead, final VerificationMode mode) {
final ReadOnlyForkChoiceStrategy forkChoiceStrategy = recentChainData.getForkChoiceStrategy().orElseThrow();
final Bytes32 headExecutionHash = forkChoiceStrategy.executionBlockHash(blockAndState.getRoot()).orElseThrow();
final Bytes32 finalizedExecutionHash = forkChoiceStrategy.executionBlockHash(recentChainData.getFinalizedCheckpoint().orElseThrow().getRoot()).orElseThrow();
verify(forkChoiceNotifier, mode).onForkChoiceUpdated(new ForkChoiceState(blockAndState.getRoot(), blockAndState.getSlot(), headExecutionHash, headExecutionHash, finalizedExecutionHash, optimisticHead));
}
use of tech.pegasys.teku.spec.datastructures.forkchoice.ReadOnlyForkChoiceStrategy in project teku by ConsenSys.
the class RecentChainData method computeReorgContext.
private Optional<ReorgContext> computeReorgContext(final ReadOnlyForkChoiceStrategy forkChoiceStrategy, final Optional<ChainHead> originalChainHead, final ChainHead newChainHead) {
final Optional<ReorgContext> optionalReorgContext;
if (originalChainHead.map(head -> hasReorgedFrom(head.getRoot(), head.getSlot())).orElse(false)) {
final ChainHead previousChainHead = originalChainHead.get();
final SlotAndBlockRoot commonAncestorSlotAndBlockRoot = forkChoiceStrategy.findCommonAncestor(previousChainHead.getRoot(), newChainHead.getRoot()).orElseGet(() -> store.getFinalizedCheckpoint().toSlotAndBlockRoot(spec));
reorgCounter.inc();
optionalReorgContext = ReorgContext.of(previousChainHead.getRoot(), previousChainHead.getSlot(), previousChainHead.getStateRoot(), commonAncestorSlotAndBlockRoot.getSlot(), commonAncestorSlotAndBlockRoot.getBlockRoot());
} else {
optionalReorgContext = ReorgContext.empty();
}
return optionalReorgContext;
}
use of tech.pegasys.teku.spec.datastructures.forkchoice.ReadOnlyForkChoiceStrategy in project teku by ConsenSys.
the class ForkChoiceStrategyTest method executionBlockHash_shouldGetExecutionRootForKnownBlock.
@Test
void executionBlockHash_shouldGetExecutionRootForKnownBlock() {
final StorageSystem storageSystem = initStorageSystem(TestSpecFactory.createMinimalBellatrix());
final SignedBlockAndState block1 = storageSystem.chainBuilder().generateBlockAtSlot(1, BlockOptions.create().setTransactions());
storageSystem.chainUpdater().saveBlock(block1);
assertThat(block1.getExecutionBlockHash()).isNotEmpty();
final ReadOnlyForkChoiceStrategy strategy = storageSystem.recentChainData().getForkChoiceStrategy().orElseThrow();
assertThat(strategy.executionBlockHash(block1.getRoot())).isEqualTo(block1.getExecutionBlockHash());
storageSystem.restarted();
assertThat(storageSystem.recentChainData().getForkChoiceStrategy().orElseThrow().executionBlockHash(block1.getRoot())).isEqualTo(block1.getExecutionBlockHash());
}
use of tech.pegasys.teku.spec.datastructures.forkchoice.ReadOnlyForkChoiceStrategy in project teku by ConsenSys.
the class ForkChoiceUtil method validateOnAttestation.
private AttestationProcessingResult validateOnAttestation(final ReadOnlyStore store, final AttestationData attestationData) {
UInt64 currentEpoch = miscHelpers.computeEpochAtSlot(getCurrentSlot(store));
final ReadOnlyForkChoiceStrategy forkChoiceStrategy = store.getForkChoiceStrategy();
return validateOnAttestation(forkChoiceStrategy, currentEpoch, attestationData);
}
use of tech.pegasys.teku.spec.datastructures.forkchoice.ReadOnlyForkChoiceStrategy in project teku by ConsenSys.
the class ForkChoiceUtil method validateOnAttestation.
public AttestationProcessingResult validateOnAttestation(final ReadOnlyForkChoiceStrategy forkChoiceStrategy, final UInt64 currentEpoch, final AttestationData attestationData) {
final Checkpoint target = attestationData.getTarget();
// Use GENESIS_EPOCH for previous when genesis to avoid underflow
final UInt64 previousEpoch = currentEpoch.isGreaterThan(SpecConfig.GENESIS_EPOCH) ? currentEpoch.minus(UInt64.ONE) : SpecConfig.GENESIS_EPOCH;
if (!target.getEpoch().equals(previousEpoch) && !target.getEpoch().equals(currentEpoch)) {
return AttestationProcessingResult.invalid("Attestations must be from the current or previous epoch");
}
if (!target.getEpoch().equals(miscHelpers.computeEpochAtSlot(attestationData.getSlot()))) {
return AttestationProcessingResult.invalid("Attestation slot must be within specified epoch");
}
if (!forkChoiceStrategy.contains(target.getRoot())) {
// consideration until the block is found
return AttestationProcessingResult.UNKNOWN_BLOCK;
}
final Optional<UInt64> blockSlot = forkChoiceStrategy.blockSlot(attestationData.getBeacon_block_root());
if (blockSlot.isEmpty()) {
// block is found
return AttestationProcessingResult.UNKNOWN_BLOCK;
}
if (blockSlot.get().compareTo(attestationData.getSlot()) > 0) {
return AttestationProcessingResult.invalid("Attestations must not be for blocks in the future. If not, the attestation should not be considered");
}
// LMD vote must be consistent with FFG vote target
final UInt64 targetSlot = miscHelpers.computeStartSlotAtEpoch(target.getEpoch());
if (getAncestor(forkChoiceStrategy, attestationData.getBeacon_block_root(), targetSlot).map(ancestorRoot -> !ancestorRoot.equals(target.getRoot())).orElse(true)) {
return AttestationProcessingResult.invalid("LMD vote must be consistent with FFG vote target");
}
return AttestationProcessingResult.SUCCESSFUL;
}
Aggregations