Search in sources :

Example 26 with DataStructureUtil

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");
}
Also used : BeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock) Spec(tech.pegasys.teku.spec.Spec) DataStructureUtil(tech.pegasys.teku.spec.util.DataStructureUtil) Test(org.junit.jupiter.api.Test)

Example 27 with DataStructureUtil

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;
}
Also used : MemoryOnlyRecentChainData(tech.pegasys.teku.storage.client.MemoryOnlyRecentChainData) RecentChainData(tech.pegasys.teku.storage.client.RecentChainData) Deposit(tech.pegasys.teku.spec.datastructures.operations.Deposit) SignedVoluntaryExit(tech.pegasys.teku.spec.datastructures.operations.SignedVoluntaryExit) AttesterSlashing(tech.pegasys.teku.spec.datastructures.operations.AttesterSlashing) BeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock) BeaconBlockBodyLists(tech.pegasys.teku.spec.datastructures.util.BeaconBlockBodyLists) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) Bytes32(org.apache.tuweni.bytes.Bytes32) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) SlotAndBlockRoot(tech.pegasys.teku.spec.datastructures.blocks.SlotAndBlockRoot) ProposerSlashing(tech.pegasys.teku.spec.datastructures.operations.ProposerSlashing) BeaconChainUtil(tech.pegasys.teku.statetransition.BeaconChainUtil) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) BLSSignature(tech.pegasys.teku.bls.BLSSignature) DataStructureUtil(tech.pegasys.teku.spec.util.DataStructureUtil)

Example 28 with DataStructureUtil

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);
    }
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) WeakSubjectivityValidator(tech.pegasys.teku.weaksubjectivity.WeakSubjectivityValidator) Validator(tech.pegasys.teku.spec.datastructures.state.Validator) MergeTransitionBlockValidator(tech.pegasys.teku.statetransition.forkchoice.MergeTransitionBlockValidator) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) DataStructureUtil(tech.pegasys.teku.spec.util.DataStructureUtil) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 29 with DataStructureUtil

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");
}
Also used : PostDataFailureResponse(tech.pegasys.teku.api.response.v1.beacon.PostDataFailureResponse) Response(okhttp3.Response) SyncCommitteeMessage(tech.pegasys.teku.api.schema.altair.SyncCommitteeMessage) BLSSignature(tech.pegasys.teku.api.schema.BLSSignature) DataStructureUtil(tech.pegasys.teku.spec.util.DataStructureUtil) Test(org.junit.jupiter.api.Test) AbstractDataBackedRestAPIIntegrationTest(tech.pegasys.teku.beaconrestapi.AbstractDataBackedRestAPIIntegrationTest)

Example 30 with DataStructureUtil

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();
}
Also used : PostDataFailureResponse(tech.pegasys.teku.api.response.v1.beacon.PostDataFailureResponse) Response(okhttp3.Response) List(java.util.List) SyncCommitteeMessage(tech.pegasys.teku.api.schema.altair.SyncCommitteeMessage) BLSSignature(tech.pegasys.teku.api.schema.BLSSignature) DataStructureUtil(tech.pegasys.teku.spec.util.DataStructureUtil) Test(org.junit.jupiter.api.Test) AbstractDataBackedRestAPIIntegrationTest(tech.pegasys.teku.beaconrestapi.AbstractDataBackedRestAPIIntegrationTest)

Aggregations

DataStructureUtil (tech.pegasys.teku.spec.util.DataStructureUtil)45 Test (org.junit.jupiter.api.Test)33 Spec (tech.pegasys.teku.spec.Spec)11 Checkpoint (tech.pegasys.teku.spec.datastructures.state.Checkpoint)8 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)7 Bytes32 (org.apache.tuweni.bytes.Bytes32)6 List (java.util.List)4 Bytes (org.apache.tuweni.bytes.Bytes)4 StubMetricsSystem (tech.pegasys.teku.infrastructure.metrics.StubMetricsSystem)4 Attestation (tech.pegasys.teku.spec.datastructures.operations.Attestation)4 AnchorPoint (tech.pegasys.teku.spec.datastructures.state.AnchorPoint)4 Response (okhttp3.Response)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 PostDataFailureResponse (tech.pegasys.teku.api.response.v1.beacon.PostDataFailureResponse)3 BLSSignature (tech.pegasys.teku.api.schema.BLSSignature)3 SyncCommitteeMessage (tech.pegasys.teku.api.schema.altair.SyncCommitteeMessage)3 AbstractDataBackedRestAPIIntegrationTest (tech.pegasys.teku.beaconrestapi.AbstractDataBackedRestAPIIntegrationTest)3 BLSSignature (tech.pegasys.teku.bls.BLSSignature)3 BeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)3 WeakSubjectivityConfig (tech.pegasys.teku.weaksubjectivity.config.WeakSubjectivityConfig)3