use of tech.pegasys.teku.statetransition.forkchoice.StubForkChoiceNotifier 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));
}
use of tech.pegasys.teku.statetransition.forkchoice.StubForkChoiceNotifier 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!");
}
use of tech.pegasys.teku.statetransition.forkchoice.StubForkChoiceNotifier 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);
}
use of tech.pegasys.teku.statetransition.forkchoice.StubForkChoiceNotifier 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();
}
}
}
use of tech.pegasys.teku.statetransition.forkchoice.StubForkChoiceNotifier 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);
}
Aggregations