Search in sources :

Example 1 with ReadOnlyForkChoiceStrategy

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));
}
Also used : ReadOnlyForkChoiceStrategy(tech.pegasys.teku.spec.datastructures.forkchoice.ReadOnlyForkChoiceStrategy) ForkChoiceState(tech.pegasys.teku.spec.executionengine.ForkChoiceState) Bytes32(org.apache.tuweni.bytes.Bytes32)

Example 2 with ReadOnlyForkChoiceStrategy

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;
}
Also used : SpecConfigBellatrix(tech.pegasys.teku.spec.config.SpecConfigBellatrix) ProtoNodeData(tech.pegasys.teku.spec.datastructures.forkchoice.ProtoNodeData) StoreConfig(tech.pegasys.teku.storage.store.StoreConfig) BlockProvider(tech.pegasys.teku.dataproviders.lookup.BlockProvider) BeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock) StateAndBlockSummary(tech.pegasys.teku.spec.datastructures.blocks.StateAndBlockSummary) ReadOnlyStore(tech.pegasys.teku.spec.datastructures.forkchoice.ReadOnlyStore) Map(java.util.Map) ForkInfo(tech.pegasys.teku.spec.datastructures.state.ForkInfo) Bytes32(org.apache.tuweni.bytes.Bytes32) GenesisData(tech.pegasys.teku.spec.datastructures.genesis.GenesisData) AsyncRunner(tech.pegasys.teku.infrastructure.async.AsyncRunner) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) NavigableMap(java.util.NavigableMap) Logger(org.apache.logging.log4j.Logger) Optional(java.util.Optional) VoteUpdateChannel(tech.pegasys.teku.storage.api.VoteUpdateChannel) MetricsSystem(org.hyperledger.besu.plugin.services.MetricsSystem) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) Counter(org.hyperledger.besu.plugin.services.metrics.Counter) ChainHeadChannel(tech.pegasys.teku.storage.api.ChainHeadChannel) StoreUpdateHandler(tech.pegasys.teku.storage.store.UpdatableStore.StoreUpdateHandler) ReadOnlyForkChoiceStrategy(tech.pegasys.teku.spec.datastructures.forkchoice.ReadOnlyForkChoiceStrategy) UpdatableStore(tech.pegasys.teku.storage.store.UpdatableStore) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) MinimalBeaconBlockSummary(tech.pegasys.teku.spec.datastructures.blocks.MinimalBeaconBlockSummary) Fork(tech.pegasys.teku.spec.datastructures.state.Fork) StoreBuilder(tech.pegasys.teku.storage.store.StoreBuilder) SpecVersion(tech.pegasys.teku.spec.SpecVersion) VoteUpdater(tech.pegasys.teku.spec.datastructures.forkchoice.VoteUpdater) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Spec(tech.pegasys.teku.spec.Spec) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) SlotAndBlockRoot(tech.pegasys.teku.spec.datastructures.blocks.SlotAndBlockRoot) BeaconStateUtil(tech.pegasys.teku.spec.logic.common.util.BeaconStateUtil) FinalizedCheckpointChannel(tech.pegasys.teku.storage.api.FinalizedCheckpointChannel) EmptyStoreResults(tech.pegasys.teku.storage.store.EmptyStoreResults) ReorgContext(tech.pegasys.teku.storage.api.ReorgContext) StorageUpdateChannel(tech.pegasys.teku.storage.api.StorageUpdateChannel) StateAndBlockSummaryProvider(tech.pegasys.teku.dataproviders.lookup.StateAndBlockSummaryProvider) StoreTransaction(tech.pegasys.teku.storage.store.UpdatableStore.StoreTransaction) ForkChoiceStrategy(tech.pegasys.teku.ethereum.forkchoice.ForkChoiceStrategy) TekuMetricCategory(tech.pegasys.teku.infrastructure.metrics.TekuMetricCategory) TreeMap(java.util.TreeMap) AnchorPoint(tech.pegasys.teku.spec.datastructures.state.AnchorPoint) Bytes4(tech.pegasys.teku.infrastructure.bytes.Bytes4) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) SpecMilestone(tech.pegasys.teku.spec.SpecMilestone) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) SlotAndBlockRoot(tech.pegasys.teku.spec.datastructures.blocks.SlotAndBlockRoot) ReorgContext(tech.pegasys.teku.storage.api.ReorgContext)

Example 3 with ReadOnlyForkChoiceStrategy

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());
}
Also used : ReadOnlyForkChoiceStrategy(tech.pegasys.teku.spec.datastructures.forkchoice.ReadOnlyForkChoiceStrategy) SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState) StorageSystem(tech.pegasys.teku.storage.storageSystem.StorageSystem) Test(org.junit.jupiter.api.Test)

Example 4 with ReadOnlyForkChoiceStrategy

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);
}
Also used : ReadOnlyForkChoiceStrategy(tech.pegasys.teku.spec.datastructures.forkchoice.ReadOnlyForkChoiceStrategy) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64)

Example 5 with ReadOnlyForkChoiceStrategy

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;
}
Also used : AttestationProcessingResult(tech.pegasys.teku.spec.datastructures.util.AttestationProcessingResult) BeaconBlockSummary(tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockSummary) MutableStore(tech.pegasys.teku.spec.datastructures.forkchoice.MutableStore) MiscHelpers(tech.pegasys.teku.spec.logic.common.helpers.MiscHelpers) SpecConfig(tech.pegasys.teku.spec.config.SpecConfig) ReadOnlyForkChoiceStrategy(tech.pegasys.teku.spec.datastructures.forkchoice.ReadOnlyForkChoiceStrategy) BeaconStateAccessors(tech.pegasys.teku.spec.logic.common.helpers.BeaconStateAccessors) Fork(tech.pegasys.teku.spec.datastructures.state.Fork) Instant(java.time.Instant) NavigableMap(java.util.NavigableMap) CheckReturnValue(javax.annotation.CheckReturnValue) ReadOnlyStore(tech.pegasys.teku.spec.datastructures.forkchoice.ReadOnlyStore) TreeMap(java.util.TreeMap) AttestationData(tech.pegasys.teku.spec.datastructures.operations.AttestationData) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) Optional(java.util.Optional) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Bytes32(org.apache.tuweni.bytes.Bytes32) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) BlockImportResult(tech.pegasys.teku.spec.logic.common.statetransition.results.BlockImportResult) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64)

Aggregations

ReadOnlyForkChoiceStrategy (tech.pegasys.teku.spec.datastructures.forkchoice.ReadOnlyForkChoiceStrategy)8 Bytes32 (org.apache.tuweni.bytes.Bytes32)6 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)5 Optional (java.util.Optional)4 SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)4 Checkpoint (tech.pegasys.teku.spec.datastructures.state.Checkpoint)4 NavigableMap (java.util.NavigableMap)3 TreeMap (java.util.TreeMap)3 LogManager (org.apache.logging.log4j.LogManager)3 Logger (org.apache.logging.log4j.Logger)3 SafeFuture (tech.pegasys.teku.infrastructure.async.SafeFuture)3 Spec (tech.pegasys.teku.spec.Spec)3 ReadOnlyStore (tech.pegasys.teku.spec.datastructures.forkchoice.ReadOnlyStore)3 Fork (tech.pegasys.teku.spec.datastructures.state.Fork)3 BeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)3 Collections (java.util.Collections)2 Map (java.util.Map)2 Set (java.util.Set)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2