use of tech.pegasys.teku.spec.util.DataStructureUtil in project teku by ConsenSys.
the class ExternalSignerBlockRequestProviderTest method phase0BlockGeneratesCorrectSignTypeAndMetadata.
@Test
void phase0BlockGeneratesCorrectSignTypeAndMetadata() {
final Spec spec = TestSpecFactory.createMinimalPhase0();
final BeaconBlock block = new DataStructureUtil(spec).randomBeaconBlock(10);
final ExternalSignerBlockRequestProvider externalSignerBlockRequestProvider = new ExternalSignerBlockRequestProvider(spec, block);
final SignType signType = externalSignerBlockRequestProvider.getSignType();
final Map<String, Object> blockMetadata = externalSignerBlockRequestProvider.getBlockMetadata(Map.of());
assertThat(signType).isEqualTo(SignType.BLOCK);
assertThat(blockMetadata).containsKey("block");
}
use of tech.pegasys.teku.spec.util.DataStructureUtil 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.spec.util.DataStructureUtil in project teku by ConsenSys.
the class ProfilingRun method runSszDeserialize.
@Disabled
@Test
void runSszDeserialize() {
BLSPublicKey publicKey = BLSTestUtil.randomPublicKey(1);
System.out.println("Generating state...");
BeaconState beaconState = new DataStructureUtil(1, spec).withPubKeyGenerator(() -> publicKey).randomBeaconState(100_000);
final BeaconStateSchema<?, ?> stateSchema = spec.atSlot(beaconState.getSlot()).getSchemaDefinitions().getBeaconStateSchema();
System.out.println("Serializing...");
Bytes bytes = beaconState.sszSerialize();
System.out.println("Deserializing...");
while (true) {
long s = System.currentTimeMillis();
long sum = 0;
for (int i = 0; i < 1; i++) {
BeaconState state = stateSchema.sszDeserialize(bytes);
blackHole.accept(state);
for (Validator validator : state.getValidators()) {
sum += validator.getEffective_balance().longValue();
}
}
System.out.println("Time: " + (System.currentTimeMillis() - s) + ", sum = " + sum);
}
}
use of tech.pegasys.teku.spec.util.DataStructureUtil in project teku by ConsenSys.
the class PostSyncCommitteesIntegrationTest method phase0SlotCausesBadRequest.
@Test
void phase0SlotCausesBadRequest() throws IOException {
startRestAPIAtGenesis(SpecMilestone.PHASE0);
spec = TestSpecFactory.createMinimalPhase0();
DataStructureUtil dataStructureUtil = new DataStructureUtil(spec);
final List<SyncCommitteeMessage> requestBody = List.of(new SyncCommitteeMessage(UInt64.ONE, dataStructureUtil.randomBytes32(), dataStructureUtil.randomUInt64(), new BLSSignature(dataStructureUtil.randomSignature())));
Response response = post(PostSyncCommittees.ROUTE, jsonProvider.objectToJSON(requestBody));
assertThat(response.code()).isEqualTo(SC_BAD_REQUEST);
assertThat(response.body().string()).contains("Could not create sync committee signature at phase0 slot 1");
}
use of tech.pegasys.teku.spec.util.DataStructureUtil in project teku by ConsenSys.
the class PostSyncCommitteesIntegrationTest method shouldGet200OkWhenThereAreNoErrors.
@Test
void shouldGet200OkWhenThereAreNoErrors() throws Exception {
spec = TestSpecFactory.createMinimalAltair();
DataStructureUtil dataStructureUtil = new DataStructureUtil(spec);
startRestAPIAtGenesis(SpecMilestone.ALTAIR);
final List<SyncCommitteeMessage> requestBody = List.of(new SyncCommitteeMessage(UInt64.ONE, dataStructureUtil.randomBytes32(), dataStructureUtil.randomUInt64(), new BLSSignature(dataStructureUtil.randomSignature())));
final SafeFuture<List<SubmitDataError>> future = SafeFuture.completedFuture(Collections.emptyList());
when(validatorApiChannel.sendSyncCommitteeMessages(any())).thenReturn(future);
Response response = post(PostSyncCommittees.ROUTE, jsonProvider.objectToJSON(requestBody));
assertThat(response.code()).isEqualTo(SC_OK);
assertThat(response.body().string()).isEmpty();
}
Aggregations