use of tech.pegasys.teku.statetransition.BeaconChainUtil in project teku by ConsenSys.
the class Generator method generateBlocks.
@Disabled
@Test
public void generateBlocks() throws Exception {
final Spec spec = TestSpecFactory.createMainnetAltair();
AbstractBlockProcessor.BLS_VERIFY_DEPOSIT = false;
System.out.println("Generating keypairs...");
int validatorsCount = 32 * 1024;
List<BLSKeyPair> validatorKeys = BlsKeyPairIO.createReaderForResource("/bls-key-pairs/bls-key-pairs-200k-seed-0.txt.gz").readAll(validatorsCount);
System.out.println("Keypairs done.");
RecentChainData localStorage = MemoryOnlyRecentChainData.create(spec);
BeaconChainUtil localChain = BeaconChainUtil.builder().specProvider(spec).recentChainData(localStorage).validatorKeys(validatorKeys).signDeposits(false).build();
localChain.initializeStorage();
AttestationGenerator attestationGenerator = new AttestationGenerator(spec, validatorKeys);
UInt64 currentSlot = localStorage.getHeadSlot();
List<Attestation> attestations = Collections.emptyList();
final int slotsPerEpoch = spec.getGenesisSpecConfig().getSlotsPerEpoch();
String blocksFile = "blocks_epoch_" + slotsPerEpoch + "_validators_" + validatorsCount + ".ssz.gz";
try (Writer writer = BlockIO.createFileWriter(blocksFile)) {
for (int j = 0; j < 50; j++) {
for (int i = 0; i < slotsPerEpoch; i++) {
long s = System.currentTimeMillis();
currentSlot = currentSlot.plus(UInt64.ONE);
final SignedBeaconBlock block = localChain.createAndImportBlockAtSlotWithAttestations(currentSlot, AttestationGenerator.groupAndAggregateAttestations(attestations));
writer.accept(block);
final StateAndBlockSummary postState = localStorage.getStore().retrieveStateAndBlockSummary(block.getMessage().hashTreeRoot()).join().orElseThrow();
attestations = UInt64.ONE.equals(currentSlot) ? Collections.emptyList() : attestationGenerator.getAttestationsForSlot(postState, currentSlot);
System.out.println("Processed: " + currentSlot + ", " + getCommittees(spec, postState.getState()) + ", " + (System.currentTimeMillis() - s) + " ms");
}
Optional<BeaconState> bestState = localStorage.retrieveBlockState(localStorage.getBestBlockRoot().orElse(null)).join();
System.out.println("Epoch done: " + bestState);
}
}
}
use of tech.pegasys.teku.statetransition.BeaconChainUtil 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);
}
use of tech.pegasys.teku.statetransition.BeaconChainUtil in project teku by ConsenSys.
the class BlockFactoryTest method assertBlockCreated.
private BeaconBlock assertBlockCreated(final int blockSlot, final Spec spec) throws EpochProcessingException, SlotProcessingException, StateTransitionException {
final UInt64 newSlot = UInt64.valueOf(blockSlot);
final DataStructureUtil dataStructureUtil = new DataStructureUtil(spec);
final BeaconBlockBodyLists blockBodyLists = BeaconBlockBodyLists.ofSpec(spec);
final RecentChainData recentChainData = MemoryOnlyRecentChainData.create(spec);
final BeaconChainUtil beaconChainUtil = BeaconChainUtil.create(spec, 1, recentChainData);
final SszList<Deposit> deposits = blockBodyLists.createDeposits();
final SszList<Attestation> attestations = blockBodyLists.createAttestations();
final SszList<AttesterSlashing> attesterSlashings = blockBodyLists.createAttesterSlashings();
final SszList<ProposerSlashing> proposerSlashings = blockBodyLists.createProposerSlashings();
final SszList<SignedVoluntaryExit> voluntaryExits = blockBodyLists.createVoluntaryExits();
if (spec.getGenesisSpec().getMilestone().isGreaterThanOrEqualTo(SpecMilestone.BELLATRIX)) {
executionPayload = SchemaDefinitionsBellatrix.required(spec.getGenesisSpec().getSchemaDefinitions()).getExecutionPayloadSchema().getDefault();
} else {
executionPayload = null;
}
final Bytes32 graffiti = dataStructureUtil.randomBytes32();
final BlockFactory blockFactory = new BlockFactory(spec, new BlockOperationSelectorFactory(spec, attestationsPool, attesterSlashingPool, proposerSlashingPool, voluntaryExitPool, syncCommitteeContributionPool, depositProvider, eth1DataCache, graffiti, forkChoiceNotifier, executionEngine));
when(depositProvider.getDeposits(any(), any())).thenReturn(deposits);
when(attestationsPool.getAttestationsForBlock(any(), any(), any())).thenReturn(attestations);
when(attesterSlashingPool.getItemsForBlock(any(), any(), any())).thenReturn(attesterSlashings);
when(proposerSlashingPool.getItemsForBlock(any(), any(), any())).thenReturn(proposerSlashings);
when(voluntaryExitPool.getItemsForBlock(any(), any(), any())).thenReturn(voluntaryExits);
when(eth1DataCache.getEth1Vote(any())).thenReturn(ETH1_DATA);
when(forkChoiceNotifier.getPayloadId(any(), any())).thenReturn(SafeFuture.completedFuture(Optional.of(Bytes8.fromHexStringLenient("0x0"))));
when(executionEngine.getPayload(any(), any())).thenReturn(SafeFuture.completedFuture(executionPayload));
beaconChainUtil.initializeStorage();
final BLSSignature randaoReveal = dataStructureUtil.randomSignature();
final Bytes32 bestBlockRoot = recentChainData.getBestBlockRoot().orElseThrow();
final BeaconState blockSlotState = recentChainData.retrieveStateAtSlot(new SlotAndBlockRoot(UInt64.valueOf(blockSlot), bestBlockRoot)).join().orElseThrow();
when(syncCommitteeContributionPool.createSyncAggregateForBlock(newSlot, bestBlockRoot)).thenAnswer(invocation -> createEmptySyncAggregate(spec));
final BeaconBlock block = blockFactory.createUnsignedBlock(blockSlotState, newSlot, randaoReveal, Optional.empty());
assertThat(block).isNotNull();
assertThat(block.getSlot()).isEqualTo(newSlot);
assertThat(block.getBody().getRandaoReveal()).isEqualTo(randaoReveal);
assertThat(block.getBody().getEth1Data()).isEqualTo(ETH1_DATA);
assertThat(block.getBody().getDeposits()).isEqualTo(deposits);
assertThat(block.getBody().getAttestations()).isEqualTo(attestations);
assertThat(block.getBody().getAttesterSlashings()).isEqualTo(attesterSlashings);
assertThat(block.getBody().getProposerSlashings()).isEqualTo(proposerSlashings);
assertThat(block.getBody().getVoluntaryExits()).isEqualTo(voluntaryExits);
assertThat(block.getBody().getGraffiti()).isEqualTo(graffiti);
return block;
}
use of tech.pegasys.teku.statetransition.BeaconChainUtil in project teku by ConsenSys.
the class ProfilingRun method importBlocks.
@Disabled
@Test
public void importBlocks() throws Exception {
AbstractBlockProcessor.BLS_VERIFY_DEPOSIT = false;
int validatorsCount = 32 * 1024;
int iterationBlockLimit = 1024;
String blocksFile = "/blocks/blocks_epoch_" + spec.getSlotsPerEpoch(UInt64.ZERO) + "_validators_" + validatorsCount + ".ssz.gz";
System.out.println("Generating keypairs...");
List<BLSKeyPair> validatorKeys = BlsKeyPairIO.createReaderForResource("/bls-key-pairs/bls-key-pairs-200k-seed-0.txt.gz").readAll(validatorsCount);
BeaconState initialState = InteropStartupUtil.createMockedStartInitialBeaconState(spec, 0, validatorKeys, false);
final WeakSubjectivityValidator wsValidator = WeakSubjectivityFactory.lenientValidator();
while (true) {
final BlockImportNotifications blockImportNotifications = mock(BlockImportNotifications.class);
RecentChainData recentChainData = MemoryOnlyRecentChainData.create(spec);
recentChainData.initializeFromGenesis(initialState, UInt64.ZERO);
final MergeTransitionBlockValidator transitionBlockValidator = new MergeTransitionBlockValidator(spec, recentChainData, ExecutionEngineChannel.NOOP);
ForkChoice forkChoice = new ForkChoice(spec, new InlineEventThread(), recentChainData, new StubForkChoiceNotifier(), transitionBlockValidator);
BeaconChainUtil localChain = BeaconChainUtil.create(spec, recentChainData, validatorKeys, false);
BlockImporter blockImporter = new BlockImporter(spec, blockImportNotifications, recentChainData, forkChoice, wsValidator, ExecutionEngineChannel.NOOP);
System.out.println("Start blocks import from " + blocksFile);
int blockCount = 0;
int measuredBlockCount = 0;
long totalS = 0;
try (Reader blockReader = BlockIO.createResourceReader(spec, blocksFile)) {
for (SignedBeaconBlock block : blockReader) {
if (block.getSlot().intValue() == 65) {
totalS = System.currentTimeMillis();
measuredBlockCount = 0;
}
long s = System.currentTimeMillis();
localChain.setSlot(block.getSlot());
BlockImportResult result = blockImporter.importBlock(block).join();
System.out.println("Imported block at #" + block.getSlot() + " in " + (System.currentTimeMillis() - s) + " ms: " + result);
blockCount++;
measuredBlockCount++;
if (blockCount > iterationBlockLimit)
break;
}
}
long totalT = System.currentTimeMillis() - totalS;
System.out.printf("############# Total: %f.2 blocks/sec\n", measuredBlockCount / (totalT / 1000.0));
}
}
use of tech.pegasys.teku.statetransition.BeaconChainUtil in project teku by ConsenSys.
the class ProfilingRun method importBlocksMemProfiling.
@Disabled
@Test
public void importBlocksMemProfiling() throws Exception {
AbstractBlockProcessor.BLS_VERIFY_DEPOSIT = false;
int validatorsCount = 32 * 1024;
String blocksFile = "/blocks/blocks_epoch_" + spec.getSlotsPerEpoch(UInt64.ZERO) + "_validators_" + validatorsCount + ".ssz.gz";
System.out.println("Generating keypairs...");
List<BLSKeyPair> validatorKeys = BlsKeyPairIO.createReaderForResource("/bls-key-pairs/bls-key-pairs-200k-seed-0.txt.gz").readAll(validatorsCount);
BeaconState initialState = InteropStartupUtil.createMockedStartInitialBeaconState(spec, 0, validatorKeys, false);
final WeakSubjectivityValidator wsValidator = WeakSubjectivityFactory.lenientValidator();
while (true) {
final BlockImportNotifications blockImportNotifications = mock(BlockImportNotifications.class);
RecentChainData recentChainData = MemoryOnlyRecentChainData.create();
BeaconChainUtil localChain = BeaconChainUtil.create(recentChainData, validatorKeys, false);
recentChainData.initializeFromGenesis(initialState, UInt64.ZERO);
initialState = null;
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, blockImportNotifications, recentChainData, forkChoice, wsValidator, ExecutionEngineChannel.NOOP);
System.out.println("Start blocks import from " + blocksFile);
int counter = 1;
try (Reader blockReader = BlockIO.createResourceReader(spec, blocksFile)) {
for (SignedBeaconBlock block : blockReader) {
long s = System.currentTimeMillis();
localChain.setSlot(block.getSlot());
BlockImportResult result = blockImporter.importBlock(block).join();
System.out.println("Imported block at #" + block.getSlot() + " in " + (System.currentTimeMillis() - s) + " ms: " + result);
if (--counter == 0) {
// recreate View validator caches for older state
// traverseViewHierarchy(statesList.get(statesList.size() - 2), v ->
// blackHole.accept(v));
System.out.println("Press enter: ");
String line = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8)).readLine();
try {
counter = Integer.parseInt(line);
} catch (NumberFormatException e) {
counter = 1;
}
}
}
}
}
}
Aggregations