Search in sources :

Example 1 with MergeTransitionBlockValidator

use of tech.pegasys.teku.statetransition.forkchoice.MergeTransitionBlockValidator 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 MergeTransitionBlockValidator

use of tech.pegasys.teku.statetransition.forkchoice.MergeTransitionBlockValidator 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 MergeTransitionBlockValidator

use of tech.pegasys.teku.statetransition.forkchoice.MergeTransitionBlockValidator 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 MergeTransitionBlockValidator

use of tech.pegasys.teku.statetransition.forkchoice.MergeTransitionBlockValidator 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)

Example 5 with MergeTransitionBlockValidator

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

the class SyncingNodeManager method create.

@SuppressWarnings("FutureReturnValueIgnored")
public static SyncingNodeManager create(final AsyncRunner asyncRunner, Eth2P2PNetworkFactory networkFactory, final List<BLSKeyPair> validatorKeys, Consumer<Eth2P2PNetworkBuilder> configureNetwork) throws Exception {
    final Spec spec = TestSpecFactory.createMinimalPhase0();
    final EventChannels eventChannels = EventChannels.createSyncChannels(TEST_EXCEPTION_HANDLER, new NoOpMetricsSystem());
    final RecentChainData recentChainData = MemoryOnlyRecentChainData.create(spec);
    final BeaconChainUtil chainUtil = BeaconChainUtil.create(spec, recentChainData, validatorKeys);
    chainUtil.initializeStorage();
    final MergeTransitionBlockValidator transitionBlockValidator = new MergeTransitionBlockValidator(spec, recentChainData, ExecutionEngineChannel.NOOP);
    ForkChoice forkChoice = new ForkChoice(spec, new InlineEventThread(), recentChainData, new StubForkChoiceNotifier(), transitionBlockValidator);
    BlockImporter blockImporter = new BlockImporter(spec, eventChannels.getPublisher(BlockImportNotifications.class), recentChainData, forkChoice, WeakSubjectivityFactory.lenientValidator(), new StubExecutionEngineChannel(spec));
    BlockValidator blockValidator = new BlockValidator(spec, recentChainData);
    final PendingPool<SignedBeaconBlock> pendingBlocks = PendingPool.createForBlocks(spec);
    final FutureItems<SignedBeaconBlock> futureBlocks = FutureItems.create(SignedBeaconBlock::getSlot);
    BlockManager blockManager = new BlockManager(recentChainData, blockImporter, pendingBlocks, futureBlocks, blockValidator);
    eventChannels.subscribe(SlotEventsChannel.class, blockManager).subscribe(BlockImportChannel.class, blockManager).subscribe(BlockImportNotifications.class, blockManager).subscribe(FinalizedCheckpointChannel.class, pendingBlocks);
    final Eth2P2PNetworkBuilder networkBuilder = networkFactory.builder().spec(spec).eventChannels(eventChannels).recentChainData(recentChainData).gossipedBlockProcessor(blockManager::validateAndImportBlock);
    configureNetwork.accept(networkBuilder);
    final Eth2P2PNetwork eth2P2PNetwork = networkBuilder.startNetwork();
    SyncManager syncManager = SyncManager.create(asyncRunner, eth2P2PNetwork, recentChainData, blockImporter, new NoOpMetricsSystem(), spec);
    ForwardSyncService syncService = new SinglePeerSyncService(syncManager, recentChainData);
    final FetchRecentBlocksService recentBlockFetcher = FetchRecentBlocksService.create(asyncRunner, eth2P2PNetwork, pendingBlocks, syncService);
    recentBlockFetcher.subscribeBlockFetched(blockManager::importBlock);
    blockManager.subscribeToReceivedBlocks((block, executionOptimistic) -> recentBlockFetcher.cancelRecentBlockRequest(block.getRoot()));
    recentBlockFetcher.start().join();
    blockManager.start().join();
    syncService.start().join();
    return new SyncingNodeManager(eventChannels, recentChainData, chainUtil, eth2P2PNetwork, syncService);
}
Also used : MemoryOnlyRecentChainData(tech.pegasys.teku.storage.client.MemoryOnlyRecentChainData) RecentChainData(tech.pegasys.teku.storage.client.RecentChainData) SlotEventsChannel(tech.pegasys.teku.ethereum.events.SlotEventsChannel) Eth2P2PNetworkBuilder(tech.pegasys.teku.networking.eth2.Eth2P2PNetworkFactory.Eth2P2PNetworkBuilder) BlockImporter(tech.pegasys.teku.statetransition.block.BlockImporter) BlockValidator(tech.pegasys.teku.statetransition.validation.BlockValidator) MergeTransitionBlockValidator(tech.pegasys.teku.statetransition.forkchoice.MergeTransitionBlockValidator) FetchRecentBlocksService(tech.pegasys.teku.beacon.sync.gossip.FetchRecentBlocksService) EventChannels(tech.pegasys.teku.infrastructure.events.EventChannels) MergeTransitionBlockValidator(tech.pegasys.teku.statetransition.forkchoice.MergeTransitionBlockValidator) SinglePeerSyncService(tech.pegasys.teku.beacon.sync.forward.singlepeer.SinglePeerSyncService) StubForkChoiceNotifier(tech.pegasys.teku.statetransition.forkchoice.StubForkChoiceNotifier) BlockImportNotifications(tech.pegasys.teku.statetransition.block.BlockImportNotifications) ForkChoice(tech.pegasys.teku.statetransition.forkchoice.ForkChoice) SyncManager(tech.pegasys.teku.beacon.sync.forward.singlepeer.SyncManager) InlineEventThread(tech.pegasys.teku.infrastructure.async.eventthread.InlineEventThread) StubExecutionEngineChannel(tech.pegasys.teku.spec.executionengine.StubExecutionEngineChannel) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) ForwardSyncService(tech.pegasys.teku.beacon.sync.forward.ForwardSyncService) BlockManager(tech.pegasys.teku.statetransition.block.BlockManager) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) BeaconChainUtil(tech.pegasys.teku.statetransition.BeaconChainUtil) Spec(tech.pegasys.teku.spec.Spec) Eth2P2PNetwork(tech.pegasys.teku.networking.eth2.Eth2P2PNetwork)

Aggregations

ForkChoice (tech.pegasys.teku.statetransition.forkchoice.ForkChoice)9 MergeTransitionBlockValidator (tech.pegasys.teku.statetransition.forkchoice.MergeTransitionBlockValidator)9 InlineEventThread (tech.pegasys.teku.infrastructure.async.eventthread.InlineEventThread)8 StubForkChoiceNotifier (tech.pegasys.teku.statetransition.forkchoice.StubForkChoiceNotifier)8 SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)6 BlockImportNotifications (tech.pegasys.teku.statetransition.block.BlockImportNotifications)5 BlockImporter (tech.pegasys.teku.statetransition.block.BlockImporter)5 RecentChainData (tech.pegasys.teku.storage.client.RecentChainData)5 BLSKeyPair (tech.pegasys.teku.bls.BLSKeyPair)4 MemoryOnlyRecentChainData (tech.pegasys.teku.storage.client.MemoryOnlyRecentChainData)4 BeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)3 BeaconChainUtil (tech.pegasys.teku.statetransition.BeaconChainUtil)3 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 Disabled (org.junit.jupiter.api.Disabled)2 Test (org.junit.jupiter.api.Test)2 Setup (org.openjdk.jmh.annotations.Setup)2 Reader (tech.pegasys.teku.benchmarks.gen.BlockIO.Reader)2 Spec (tech.pegasys.teku.spec.Spec)2 StubExecutionEngineChannel (tech.pegasys.teku.spec.executionengine.StubExecutionEngineChannel)2