Search in sources :

Example 1 with ForkChoice

use of tech.pegasys.teku.statetransition.forkchoice.ForkChoice in project teku by ConsenSys.

the class AbstractDataBackedRestAPIIntegrationTest method setupStorage.

private void setupStorage(final StateStorageMode storageMode, final boolean useMockForkChoice, final SpecMilestone specMilestone) {
    this.spec = TestSpecFactory.createMinimal(specMilestone);
    this.specConfig = spec.getGenesisSpecConfig();
    this.storageSystem = InMemoryStorageSystemBuilder.create().specProvider(spec).storageMode(storageMode).build();
    activeValidatorChannel = new ActiveValidatorCache(spec, 10);
    recentChainData = storageSystem.recentChainData();
    chainBuilder = ChainBuilder.create(spec, VALIDATOR_KEYS);
    chainUpdater = new ChainUpdater(recentChainData, chainBuilder, spec);
    forkChoice = useMockForkChoice ? mock(ForkChoice.class) : new ForkChoice(spec, new InlineEventThread(), recentChainData, new StubForkChoiceNotifier(), new MergeTransitionBlockValidator(spec, recentChainData, ExecutionEngineChannel.NOOP));
}
Also used : ActiveValidatorCache(tech.pegasys.teku.statetransition.validatorcache.ActiveValidatorCache) ChainUpdater(tech.pegasys.teku.storage.client.ChainUpdater) MergeTransitionBlockValidator(tech.pegasys.teku.statetransition.forkchoice.MergeTransitionBlockValidator) ForkChoice(tech.pegasys.teku.statetransition.forkchoice.ForkChoice) InlineEventThread(tech.pegasys.teku.infrastructure.async.eventthread.InlineEventThread) StubForkChoiceNotifier(tech.pegasys.teku.statetransition.forkchoice.StubForkChoiceNotifier)

Example 2 with ForkChoice

use of tech.pegasys.teku.statetransition.forkchoice.ForkChoice in project teku by ConsenSys.

the class EpochTransitionBenchmark method init.

@Setup(Level.Trial)
public void init() throws Exception {
    AbstractBlockProcessor.BLS_VERIFY_DEPOSIT = false;
    String blocksFile = "/blocks/blocks_epoch_" + spec.getSlotsPerEpoch(UInt64.ZERO) + "_validators_" + validatorsCount + ".ssz.gz";
    String keysFile = "/bls-key-pairs/bls-key-pairs-200k-seed-0.txt.gz";
    System.out.println("Generating keypairs from " + keysFile);
    List<BLSKeyPair> validatorKeys = BlsKeyPairIO.createReaderForResource(keysFile).readAll(validatorsCount);
    final BlockImportNotifications blockImportNotifications = mock(BlockImportNotifications.class);
    spec = TestSpecFactory.createMainnetPhase0();
    epochProcessor = spec.getGenesisSpec().getEpochProcessor();
    wsValidator = WeakSubjectivityFactory.lenientValidator();
    recentChainData = MemoryOnlyRecentChainData.create(spec);
    final MergeTransitionBlockValidator transitionBlockValidator = new MergeTransitionBlockValidator(spec, recentChainData, ExecutionEngineChannel.NOOP);
    ForkChoice forkChoice = new ForkChoice(spec, new InlineEventThread(), recentChainData, new StubForkChoiceNotifier(), transitionBlockValidator);
    localChain = BeaconChainUtil.create(spec, recentChainData, validatorKeys, false);
    localChain.initializeStorage();
    blockImporter = new BlockImporter(spec, blockImportNotifications, recentChainData, forkChoice, wsValidator, ExecutionEngineChannel.NOOP);
    blockIterator = BlockIO.createResourceReader(spec, blocksFile).iterator();
    System.out.println("Importing 63 blocks from " + blocksFile);
    for (int i = 0; i < 63; i++) {
        SignedBeaconBlock block = blockIterator.next();
        localChain.setSlot(block.getSlot());
        lastResult = blockImporter.importBlock(block).join();
    }
    preEpochTransitionState = safeJoin(recentChainData.getBestState().orElseThrow());
    validatorStatuses = spec.getGenesisSpec().getValidatorStatusFactory().createValidatorStatuses(preEpochTransitionState);
    preEpochTransitionState.updated(mbs -> preEpochTransitionMutableState = mbs);
    attestationDeltas = epochProcessor.getRewardAndPenaltyDeltas(preEpochTransitionState, validatorStatuses);
    System.out.println("Done!");
}
Also used : MergeTransitionBlockValidator(tech.pegasys.teku.statetransition.forkchoice.MergeTransitionBlockValidator) ForkChoice(tech.pegasys.teku.statetransition.forkchoice.ForkChoice) InlineEventThread(tech.pegasys.teku.infrastructure.async.eventthread.InlineEventThread) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) BLSKeyPair(tech.pegasys.teku.bls.BLSKeyPair) StubForkChoiceNotifier(tech.pegasys.teku.statetransition.forkchoice.StubForkChoiceNotifier) BlockImportNotifications(tech.pegasys.teku.statetransition.block.BlockImportNotifications) BlockImporter(tech.pegasys.teku.statetransition.block.BlockImporter) Setup(org.openjdk.jmh.annotations.Setup)

Example 3 with ForkChoice

use of tech.pegasys.teku.statetransition.forkchoice.ForkChoice in project teku by ConsenSys.

the class ForkChoiceTestExecutor method runTest.

@Override
public void runTest(final TestDefinition testDefinition) throws Throwable {
    if (testsToSkip.contains(testDefinition.getTestName())) {
        throw new TestAbortedException("Test " + testDefinition.getDisplayName() + " has been ignored");
    }
    // Note: The fork choice spec says there may be settings in a meta.yaml file but currently no
    // tests actually have one, so we currently don't bother trying to load it.
    final BeaconState anchorState = TestDataUtils.loadStateFromSsz(testDefinition, "anchor_state.ssz_snappy");
    final Spec spec = testDefinition.getSpec();
    final SignedBeaconBlock anchorBlock = loadAnchorBlock(testDefinition);
    final StorageSystem storageSystem = InMemoryStorageSystemBuilder.create().specProvider(spec).build();
    final RecentChainData recentChainData = storageSystem.recentChainData();
    recentChainData.initializeFromAnchorPoint(AnchorPoint.fromInitialBlockAndState(spec, new SignedBlockAndState(anchorBlock, anchorState)), spec.getSlotStartTime(anchorBlock.getSlot(), anchorState.getGenesis_time()));
    final MergeTransitionBlockValidator transitionBlockValidator = new MergeTransitionBlockValidator(spec, recentChainData, ExecutionEngineChannel.NOOP);
    final ForkChoice forkChoice = new ForkChoice(spec, new InlineEventThread(), recentChainData, new StubForkChoiceNotifier(), transitionBlockValidator, true);
    final StubExecutionEngineChannel executionEngine = new StubExecutionEngineChannel(spec);
    runSteps(testDefinition, spec, recentChainData, forkChoice, executionEngine);
}
Also used : RecentChainData(tech.pegasys.teku.storage.client.RecentChainData) MergeTransitionBlockValidator(tech.pegasys.teku.statetransition.forkchoice.MergeTransitionBlockValidator) ForkChoice(tech.pegasys.teku.statetransition.forkchoice.ForkChoice) TestAbortedException(org.opentest4j.TestAbortedException) InlineEventThread(tech.pegasys.teku.infrastructure.async.eventthread.InlineEventThread) Spec(tech.pegasys.teku.spec.Spec) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState) StubExecutionEngineChannel(tech.pegasys.teku.spec.executionengine.StubExecutionEngineChannel) StubForkChoiceNotifier(tech.pegasys.teku.statetransition.forkchoice.StubForkChoiceNotifier) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) StorageSystem(tech.pegasys.teku.storage.storageSystem.StorageSystem)

Example 4 with ForkChoice

use of tech.pegasys.teku.statetransition.forkchoice.ForkChoice in project teku by ConsenSys.

the class BlockImporterTest method importBlock_nonFinalizingChain_skipWSPChecks.

@Test
public void importBlock_nonFinalizingChain_skipWSPChecks() throws Exception {
    final StorageSystem storageSystem = InMemoryStorageSystemBuilder.buildDefault();
    storageSystem.chainUpdater().initializeGenesis();
    final ForkChoice forkChoice = new ForkChoice(spec, new InlineEventThread(), storageSystem.recentChainData(), forkChoiceNotifier, transitionBlockValidator);
    final BlockImporter blockImporter = new BlockImporter(spec, blockImportNotifications, storageSystem.recentChainData(), forkChoice, weakSubjectivityValidator, ExecutionEngineChannel.NOOP);
    // Set current time to be several WSP's ahead of finalized checkpoint
    final UInt64 wsPeriod = UInt64.valueOf(10);
    final UInt64 wsPeriodInSlots = wsPeriod.times(genesisConfig.getSlotsPerEpoch());
    when(weakSubjectivityValidator.getWSPeriod(any())).thenReturn(Optional.of(wsPeriod));
    final UInt64 currentSlot = wsPeriodInSlots.times(3).plus(1);
    storageSystem.chainUpdater().setCurrentSlot(currentSlot);
    // Add a recent block
    final SignedBlockAndState recentBlock = storageSystem.chainUpdater().advanceChain(currentSlot.minus(wsPeriodInSlots));
    storageSystem.chainUpdater().updateBestBlock(recentBlock);
    // Import block new block
    final SignedBlockAndState blockToImport = storageSystem.chainBuilder().generateBlockAtSlot(currentSlot);
    // Import wsBlock
    final BlockImportResult result = blockImporter.importBlock(blockToImport.getBlock()).get();
    assertSuccessfulResult(result);
    // Verify ws period checks were skipped
    verify(weakSubjectivityValidator, never()).validateLatestFinalizedCheckpoint(any(), any());
}
Also used : ForkChoice(tech.pegasys.teku.statetransition.forkchoice.ForkChoice) InlineEventThread(tech.pegasys.teku.infrastructure.async.eventthread.InlineEventThread) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState) BlockImportResult(tech.pegasys.teku.spec.logic.common.statetransition.results.BlockImportResult) StorageSystem(tech.pegasys.teku.storage.storageSystem.StorageSystem) Test(org.junit.jupiter.api.Test)

Example 5 with ForkChoice

use of tech.pegasys.teku.statetransition.forkchoice.ForkChoice in project teku by ConsenSys.

the class ForkChoiceTestExecutor method runForkChoiceTests.

@ParameterizedTest(name = "{index}.{2} fork choice test")
@MethodSource("loadForkChoiceTests")
void runForkChoiceTests(BeaconState genesis, List<Object> steps, String testName, boolean protoArrayFC) {
    RecentChainData storageClient = MemoryOnlyRecentChainData.create(SPEC);
    storageClient.initializeFromGenesis(genesis, UInt64.ZERO);
    final InlineEventThread forkChoiceExecutor = new InlineEventThread();
    final MergeTransitionBlockValidator transitionBlockValidator = new MergeTransitionBlockValidator(SPEC, storageClient, ExecutionEngineChannel.NOOP);
    ForkChoice forkChoice = new ForkChoice(SPEC, forkChoiceExecutor, storageClient, new StubForkChoiceNotifier(), transitionBlockValidator);
    @SuppressWarnings("ModifiedButNotUsed") List<SignedBeaconBlock> blockBuffer = new ArrayList<>();
    @SuppressWarnings("ModifiedButNotUsed") List<Attestation> attestationBuffer = new ArrayList<>();
    for (Object step : steps) {
        blockBuffer.removeIf(block -> processBlock(forkChoice, block));
        attestationBuffer.removeIf(attestation -> processAttestation(forkChoice, attestation));
        if (step instanceof UInt64) {
            UpdatableStore.StoreTransaction transaction = storageClient.startStoreTransaction();
            while (SPEC.getCurrentSlot(transaction).compareTo((UInt64) step) < 0) {
                SPEC.onTick(transaction, transaction.getTime().plus(UInt64.ONE));
            }
            assertEquals(step, SPEC.getCurrentSlot(transaction));
            transaction.commit().join();
        } else if (step instanceof SignedBeaconBlock) {
            for (Attestation attestation : ((SignedBeaconBlock) step).getMessage().getBody().getAttestations()) {
                attestationBuffer.add(attestation);
            }
            if (!processBlock(forkChoice, (SignedBeaconBlock) step)) {
                blockBuffer.add((SignedBeaconBlock) step);
            }
        } else if (step instanceof Attestation) {
            if (!processAttestation(forkChoice, (Attestation) step)) {
                attestationBuffer.add((Attestation) step);
            }
        } else if (step instanceof Map) {
            @SuppressWarnings("unchecked") Map<String, Object> checks = (Map<String, Object>) step;
            for (Map.Entry<String, Object> e : checks.entrySet()) {
                String check = e.getKey();
                switch(check) {
                    case "block_in_store":
                        {
                            Bytes32 root = Bytes32.fromHexString((String) e.getValue());
                            assertTrue(storageClient.retrieveBlockByRoot(root).join().isPresent(), "Block is missing from store :" + root);
                            break;
                        }
                    case "block_not_in_store":
                        {
                            Bytes32 root = Bytes32.fromHexString((String) e.getValue());
                            assertTrue(storageClient.retrieveBlockByRoot(root).join().isEmpty(), "Block should not have been in store :" + root);
                            break;
                        }
                    case "head":
                        {
                            Bytes32 root = Bytes32.fromHexString((String) e.getValue());
                            forkChoice.processHead().join();
                            Bytes32 head = storageClient.getBestBlockRoot().orElseThrow();
                            assertEquals(root, head, "Head does not match expected head: \n head: " + head + "\n expectedHead: " + root);
                            break;
                        }
                    case "justified_checkpoint_epoch":
                        {
                            UInt64 expected = UInt64.valueOf((Integer) e.getValue());
                            UInt64 actual = storageClient.getStore().getJustifiedCheckpoint().getEpoch();
                            assertEquals(expected, actual, "Justified checkpoint epoch does not match expected: \n actual: " + actual + "\n expected: " + expected);
                            break;
                        }
                    default:
                        throw new IllegalArgumentException();
                }
            }
        } else {
            throw new IllegalArgumentException();
        }
    }
}
Also used : MemoryOnlyRecentChainData(tech.pegasys.teku.storage.client.MemoryOnlyRecentChainData) RecentChainData(tech.pegasys.teku.storage.client.RecentChainData) UpdatableStore(tech.pegasys.teku.storage.store.UpdatableStore) ForkChoice(tech.pegasys.teku.statetransition.forkchoice.ForkChoice) ArrayList(java.util.ArrayList) InlineEventThread(tech.pegasys.teku.infrastructure.async.eventthread.InlineEventThread) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) Bytes32(org.apache.tuweni.bytes.Bytes32) MergeTransitionBlockValidator(tech.pegasys.teku.statetransition.forkchoice.MergeTransitionBlockValidator) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) StubForkChoiceNotifier(tech.pegasys.teku.statetransition.forkchoice.StubForkChoiceNotifier) Map(java.util.Map) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

ForkChoice (tech.pegasys.teku.statetransition.forkchoice.ForkChoice)16 InlineEventThread (tech.pegasys.teku.infrastructure.async.eventthread.InlineEventThread)14 MergeTransitionBlockValidator (tech.pegasys.teku.statetransition.forkchoice.MergeTransitionBlockValidator)11 StubForkChoiceNotifier (tech.pegasys.teku.statetransition.forkchoice.StubForkChoiceNotifier)9 SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)8 Test (org.junit.jupiter.api.Test)7 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)7 SignedBlockAndState (tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState)7 RecentChainData (tech.pegasys.teku.storage.client.RecentChainData)7 StorageSystem (tech.pegasys.teku.storage.storageSystem.StorageSystem)7 BlockImportNotifications (tech.pegasys.teku.statetransition.block.BlockImportNotifications)6 BlockImporter (tech.pegasys.teku.statetransition.block.BlockImporter)6 BeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)5 BlockImportResult (tech.pegasys.teku.spec.logic.common.statetransition.results.BlockImportResult)5 Spec (tech.pegasys.teku.spec.Spec)4 CheckpointState (tech.pegasys.teku.spec.datastructures.state.CheckpointState)4 BLSKeyPair (tech.pegasys.teku.bls.BLSKeyPair)3 Checkpoint (tech.pegasys.teku.spec.datastructures.state.Checkpoint)3 BeaconChainUtil (tech.pegasys.teku.statetransition.BeaconChainUtil)3 MemoryOnlyRecentChainData (tech.pegasys.teku.storage.client.MemoryOnlyRecentChainData)3