Search in sources :

Example 1 with BeaconState

use of tech.pegasys.teku.api.schema.BeaconState in project teku by ConsenSys.

the class TekuNode method waitForAttestationBeingGossiped.

public void waitForAttestationBeingGossiped(int validatorSeparationIndex, int totalValidatorCount) {
    List<UInt64> node1Validators = IntStream.range(0, validatorSeparationIndex).mapToObj(UInt64::valueOf).collect(toList());
    List<UInt64> node2Validators = IntStream.range(validatorSeparationIndex, totalValidatorCount).mapToObj(UInt64::valueOf).collect(toList());
    waitFor(() -> {
        final Optional<SignedBlock> maybeBlock = fetchHeadBlock();
        final Optional<BeaconState> maybeState = fetchHeadState();
        assertThat(maybeBlock).isPresent();
        assertThat(maybeState).isPresent();
        SignedBeaconBlock block = (SignedBeaconBlock) maybeBlock.get();
        BeaconState state = maybeState.get();
        // Check that the fetched block and state are in sync
        assertThat(state.latest_block_header.parent_root).isEqualTo(block.getMessage().parent_root);
        tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState internalBeaconState = state.asInternalBeaconState(spec);
        UInt64 proposerIndex = block.getMessage().proposer_index;
        Set<UInt64> attesterIndicesInAttestations = block.getMessage().getBody().attestations.stream().map(a -> spec.getAttestingIndices(internalBeaconState, a.asInternalAttestation(spec).getData(), a.asInternalAttestation(spec).getAggregationBits())).flatMap(Collection::stream).map(UInt64::valueOf).collect(toSet());
        if (node1Validators.contains(proposerIndex)) {
            assertThat(attesterIndicesInAttestations.stream().anyMatch(node2Validators::contains)).isTrue();
        } else if (node2Validators.contains(proposerIndex)) {
            assertThat(attesterIndicesInAttestations.stream().anyMatch(node1Validators::contains)).isTrue();
        } else {
            throw new IllegalStateException("Proposer index greater than total validator count");
        }
    }, 2, MINUTES);
}
Also used : SignedBeaconBlockAltair(tech.pegasys.teku.api.schema.altair.SignedBeaconBlockAltair) Arrays(java.util.Arrays) SszBitvectorSchema(tech.pegasys.teku.infrastructure.ssz.schema.collections.SszBitvectorSchema) ValidatorKeystores(tech.pegasys.teku.test.acceptance.dsl.tools.deposits.ValidatorKeystores) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) PostValidatorLivenessResponse(tech.pegasys.teku.api.response.v1.validator.PostValidatorLivenessResponse) Network(org.testcontainers.containers.Network) SszBitvector(tech.pegasys.teku.infrastructure.ssz.collections.SszBitvector) GetStateResponseV2(tech.pegasys.teku.api.response.v2.debug.GetStateResponseV2) SpecFactory(tech.pegasys.teku.spec.SpecFactory) Duration(java.time.Duration) Map(java.util.Map) FinalityCheckpointsResponse(tech.pegasys.teku.api.response.v1.beacon.FinalityCheckpointsResponse) KEY_TYPE(io.libp2p.core.crypto.KEY_TYPE) URI(java.net.URI) SignedBlock(tech.pegasys.teku.api.schema.interfaces.SignedBlock) Bytes32(org.apache.tuweni.bytes.Bytes32) Collectors.toSet(java.util.stream.Collectors.toSet) HttpWaitStrategy(org.testcontainers.containers.wait.strategy.HttpWaitStrategy) PrivKey(io.libp2p.core.crypto.PrivKey) EventType(tech.pegasys.teku.api.response.v1.EventType) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) ValidatorLivenessRequest(tech.pegasys.teku.api.request.v1.validator.ValidatorLivenessRequest) Logger(org.apache.logging.log4j.Logger) SignedBeaconBlock(tech.pegasys.teku.api.schema.SignedBeaconBlock) MountableFile(org.testcontainers.utility.MountableFile) Optional(java.util.Optional) IntStream(java.util.stream.IntStream) IOUtils(org.testcontainers.shaded.org.apache.commons.io.IOUtils) GenesisStateConfig(tech.pegasys.teku.test.acceptance.dsl.tools.GenesisStateConfig) GetGenesisResponse(tech.pegasys.teku.api.response.v1.beacon.GetGenesisResponse) MINUTES(java.util.concurrent.TimeUnit.MINUTES) HashMap(java.util.HashMap) Bytes(org.apache.tuweni.bytes.Bytes) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) GetBlockResponseV2(tech.pegasys.teku.api.response.v2.beacon.GetBlockResponseV2) SignedContributionAndProof(tech.pegasys.teku.api.schema.altair.SignedContributionAndProof) PeerId(io.libp2p.core.PeerId) ValidatorLivenessAtEpoch(tech.pegasys.teku.api.response.v1.validator.ValidatorLivenessAtEpoch) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) KeyKt(io.libp2p.core.crypto.KeyKt) Spec(tech.pegasys.teku.spec.Spec) GetStateFinalityCheckpointsResponse(tech.pegasys.teku.api.response.v1.beacon.GetStateFinalityCheckpointsResponse) GenesisStateGenerator(tech.pegasys.teku.test.acceptance.dsl.tools.GenesisStateGenerator) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) HeadEvent(tech.pegasys.teku.api.response.v1.HeadEvent) FileOutputStream(java.io.FileOutputStream) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) File(java.io.File) BeaconState(tech.pegasys.teku.api.schema.BeaconState) Consumer(java.util.function.Consumer) Collectors.toList(java.util.stream.Collectors.toList) GetBlockRootResponse(tech.pegasys.teku.api.response.v1.beacon.GetBlockRootResponse) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) InputStream(java.io.InputStream) SignedBeaconBlock(tech.pegasys.teku.api.schema.SignedBeaconBlock) SignedBlock(tech.pegasys.teku.api.schema.interfaces.SignedBlock) BeaconState(tech.pegasys.teku.api.schema.BeaconState) Collection(java.util.Collection) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64)

Example 2 with BeaconState

use of tech.pegasys.teku.api.schema.BeaconState in project teku by ConsenSys.

the class TekuNode method waitForValidators.

public void waitForValidators(int numberOfValidators) {
    waitFor(() -> {
        Optional<BeaconState> maybeState = fetchHeadState();
        assertThat(maybeState).isPresent();
        BeaconState state = maybeState.get();
        assertThat(state.asInternalBeaconState(spec).getValidators().size()).isEqualTo(numberOfValidators);
    });
}
Also used : BeaconState(tech.pegasys.teku.api.schema.BeaconState)

Example 3 with BeaconState

use of tech.pegasys.teku.api.schema.BeaconState in project teku by ConsenSys.

the class JsonProviderTest method beaconStateJsonTest.

@Test
void beaconStateJsonTest() throws JsonProcessingException {
    tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState stateInternal = dataStructureUtil.randomBeaconState(UInt64.valueOf(16));
    BeaconState state = new BeaconStatePhase0(stateInternal);
    String jsonState = jsonProvider.objectToJSON(state);
    assertTrue(jsonState.length() > 0);
}
Also used : BeaconStatePhase0(tech.pegasys.teku.api.schema.phase0.BeaconStatePhase0) BeaconState(tech.pegasys.teku.api.schema.BeaconState) Test(org.junit.jupiter.api.Test)

Example 4 with BeaconState

use of tech.pegasys.teku.api.schema.BeaconState in project teku by ConsenSys.

the class GetStateResponseV2Deserializer method deserialize.

@Override
public GetStateResponseV2 deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException {
    JsonNode node = jp.getCodec().readTree(jp);
    final Version version = Version.valueOf(node.findValue("version").asText().toLowerCase(Locale.ROOT));
    final JsonNode executionOptimisticNode = node.findValue("execution_optimistic");
    final Boolean executionOptimistic = executionOptimisticNode != null ? executionOptimisticNode.asBoolean() : null;
    final BeaconState state;
    switch(version) {
        case altair:
            state = mapper.treeToValue(node.findValue("data"), BeaconStateAltair.class);
            break;
        case phase0:
            state = mapper.treeToValue(node.findValue("data"), BeaconStatePhase0.class);
            break;
        default:
            throw new IOException("Milestone was not able to be decoded");
    }
    return new GetStateResponseV2(version, executionOptimistic, state);
}
Also used : GetStateResponseV2(tech.pegasys.teku.api.response.v2.debug.GetStateResponseV2) Version(tech.pegasys.teku.api.schema.Version) JsonNode(com.fasterxml.jackson.databind.JsonNode) BeaconStatePhase0(tech.pegasys.teku.api.schema.phase0.BeaconStatePhase0) IOException(java.io.IOException) BeaconStateAltair(tech.pegasys.teku.api.schema.altair.BeaconStateAltair) BeaconState(tech.pegasys.teku.api.schema.BeaconState)

Aggregations

BeaconState (tech.pegasys.teku.api.schema.BeaconState)4 IOException (java.io.IOException)2 BeaconStatePhase0 (tech.pegasys.teku.api.schema.phase0.BeaconStatePhase0)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 PeerId (io.libp2p.core.PeerId)1 KEY_TYPE (io.libp2p.core.crypto.KEY_TYPE)1 KeyKt (io.libp2p.core.crypto.KeyKt)1 PrivKey (io.libp2p.core.crypto.PrivKey)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1 Files (java.nio.file.Files)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1