Search in sources :

Example 1 with SszUInt64List

use of tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List in project teku by ConsenSys.

the class DataStructureUtil method randomIndexedAttestation.

public IndexedAttestation randomIndexedAttestation(final UInt64... attestingIndices) {
    final IndexedAttestationSchema indexedAttestationSchema = spec.getGenesisSchemaDefinitions().getIndexedAttestationSchema();
    SszUInt64List attesting_indices = indexedAttestationSchema.getAttestingIndicesSchema().of(attestingIndices);
    return indexedAttestationSchema.create(attesting_indices, randomAttestationData(), randomSignature());
}
Also used : IndexedAttestationSchema(tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation.IndexedAttestationSchema) SszUInt64List(tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List)

Example 2 with SszUInt64List

use of tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List in project teku by ConsenSys.

the class ActiveValidatorCacheTest method shouldAcceptAttestations.

@Test
void shouldAcceptAttestations() {
    final Attestation attestation = mock(Attestation.class);
    final AttestationData attestationData = mock(AttestationData.class);
    final IndexedAttestation indexedAttestation = mock(IndexedAttestation.class);
    final ValidateableAttestation validateableAttestation = ValidateableAttestation.from(spec, attestation);
    validateableAttestation.setIndexedAttestation(indexedAttestation);
    when(indexedAttestation.getData()).thenReturn(attestationData);
    when(attestationData.getSlot()).thenReturn(UInt64.valueOf(8), UInt64.valueOf(16), UInt64.valueOf(24));
    final SszUInt64List validators = SszUInt64ListSchema.create(2).of(UInt64.valueOf(11), UInt64.valueOf(21));
    when(indexedAttestation.getAttesting_indices()).thenReturn(validators);
    // each attestation will have 2 validators.
    // getSlot will return 8, then 16, then 24; and each validator will be processed
    cache.onAttestation(validateableAttestation);
    cache.onAttestation(validateableAttestation);
    cache.onAttestation(validateableAttestation);
    assertThat(cache.getValidatorEpochs(UInt64.valueOf(11))).containsExactly(THREE, ONE, TWO);
    assertThat(cache.getValidatorEpochs(UInt64.valueOf(21))).containsExactly(THREE, ONE, TWO);
}
Also used : AttestationData(tech.pegasys.teku.spec.datastructures.operations.AttestationData) IndexedAttestation(tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation) SszUInt64List(tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) IndexedAttestation(tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) Test(org.junit.jupiter.api.Test)

Example 3 with SszUInt64List

use of tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List in project teku by ConsenSys.

the class ExpectedDeltas method getDeltas.

public RewardAndPenaltyDeltas getDeltas() {
    final SszUInt64List rewards = getField0();
    final SszUInt64List penalties = getField1();
    final RewardAndPenaltyDeltas deltas = new RewardAndPenaltyDeltas(rewards.size());
    for (int i = 0; i < rewards.size(); i++) {
        deltas.getDelta(i).reward(rewards.get(i).get());
        deltas.getDelta(i).penalize(penalties.get(i).get());
    }
    return deltas;
}
Also used : SszUInt64List(tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List) RewardAndPenaltyDeltas(tech.pegasys.teku.spec.logic.common.statetransition.epoch.RewardAndPenaltyDeltas)

Example 4 with SszUInt64List

use of tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List in project teku by ConsenSys.

the class BlockProcessorAltair method processAttestation.

@Override
protected void processAttestation(final MutableBeaconState genericState, final Attestation attestation, final IndexedAttestationProvider indexedAttestationProvider) {
    final MutableBeaconStateAltair state = MutableBeaconStateAltair.required(genericState);
    final AttestationData data = attestation.getData();
    final List<Integer> participationFlagIndices = beaconStateAccessorsAltair.getAttestationParticipationFlagIndices(state, data, state.getSlot().minus(data.getSlot()));
    // Update epoch participation flags
    final SszMutableList<SszByte> epochParticipation;
    if (data.getTarget().getEpoch().equals(beaconStateAccessors.getCurrentEpoch(state))) {
        epochParticipation = state.getCurrentEpochParticipation();
    } else {
        epochParticipation = state.getPreviousEpochParticipation();
    }
    UInt64 proposerRewardNumerator = UInt64.ZERO;
    final SszUInt64List attestingIndices = indexedAttestationProvider.getIndexedAttestation(attestation).getAttesting_indices();
    for (SszUInt64 attestingIndex : attestingIndices) {
        final int index = attestingIndex.get().intValue();
        byte participationFlags = epochParticipation.get(index).get();
        final UInt64 baseReward = beaconStateAccessorsAltair.getBaseReward(state, index);
        boolean shouldUpdate = false;
        for (int flagIndex = 0; flagIndex < PARTICIPATION_FLAG_WEIGHTS.size(); flagIndex++) {
            final UInt64 weight = PARTICIPATION_FLAG_WEIGHTS.get(flagIndex);
            if (participationFlagIndices.contains(flagIndex) && !miscHelpersAltair.hasFlag(participationFlags, flagIndex)) {
                shouldUpdate = true;
                participationFlags = miscHelpersAltair.addFlag(participationFlags, flagIndex);
                proposerRewardNumerator = proposerRewardNumerator.plus(baseReward.times(weight));
            }
        }
        if (shouldUpdate) {
            epochParticipation.set(index, SszByte.of(participationFlags));
        }
    }
    if (!proposerRewardNumerator.isZero()) {
        final int proposerIndex = beaconStateAccessors.getBeaconProposerIndex(state);
        final UInt64 proposerRewardDenominator = WEIGHT_DENOMINATOR.minus(PROPOSER_WEIGHT).times(WEIGHT_DENOMINATOR).dividedBy(PROPOSER_WEIGHT);
        final UInt64 proposerReward = proposerRewardNumerator.dividedBy(proposerRewardDenominator);
        beaconStateMutators.increaseBalance(state, proposerIndex, proposerReward);
    }
}
Also used : AttestationData(tech.pegasys.teku.spec.datastructures.operations.AttestationData) SszUInt64(tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64) SszUInt64List(tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List) SszByte(tech.pegasys.teku.infrastructure.ssz.primitive.SszByte) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) SszUInt64(tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64) MutableBeaconStateAltair(tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.MutableBeaconStateAltair)

Example 5 with SszUInt64List

use of tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List in project teku by ConsenSys.

the class AttestationUtil method isValidIndexedAttestationAsync.

public SafeFuture<AttestationProcessingResult> isValidIndexedAttestationAsync(Fork fork, BeaconState state, IndexedAttestation indexed_attestation, AsyncBLSSignatureVerifier signatureVerifier) {
    SszUInt64List indices = indexed_attestation.getAttesting_indices();
    if (indices.isEmpty() || !Comparators.isInStrictOrder(indices.asListUnboxed(), Comparator.naturalOrder())) {
        return completedFuture(AttestationProcessingResult.invalid("Attesting indices are not sorted"));
    }
    List<BLSPublicKey> pubkeys = indices.streamUnboxed().flatMap(i -> beaconStateAccessors.getValidatorPubKey(state, i).stream()).collect(toList());
    if (pubkeys.size() < indices.size()) {
        return completedFuture(AttestationProcessingResult.invalid("Attesting indices include non-existent validator"));
    }
    BLSSignature signature = indexed_attestation.getSignature();
    Bytes32 domain = beaconStateAccessors.getDomain(Domain.BEACON_ATTESTER, indexed_attestation.getData().getTarget().getEpoch(), fork, state.getGenesis_validators_root());
    Bytes signing_root = miscHelpers.computeSigningRoot(indexed_attestation.getData(), domain);
    return signatureVerifier.verify(pubkeys, signing_root, signature).thenApply(isValidSignature -> {
        if (isValidSignature) {
            return AttestationProcessingResult.SUCCESSFUL;
        } else {
            LOG.debug("AttestationUtil.is_valid_indexed_attestation: Verify aggregate signature");
            return AttestationProcessingResult.invalid("Signature is invalid");
        }
    });
}
Also used : IntStream(java.util.stream.IntStream) AttestationProcessingResult(tech.pegasys.teku.spec.datastructures.util.AttestationProcessingResult) Comparators(com.google.common.collect.Comparators) BeaconBlockSummary(tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockSummary) SszBitlist(tech.pegasys.teku.infrastructure.ssz.collections.SszBitlist) IndexedAttestation(tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) BeaconStateAccessors(tech.pegasys.teku.spec.logic.common.helpers.BeaconStateAccessors) Bytes(org.apache.tuweni.bytes.Bytes) Fork(tech.pegasys.teku.spec.datastructures.state.Fork) SafeFuture.completedFuture(tech.pegasys.teku.infrastructure.async.SafeFuture.completedFuture) BLSSignatureVerifier(tech.pegasys.teku.bls.BLSSignatureVerifier) IndexedAttestationSchema(tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation.IndexedAttestationSchema) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) AttestationData(tech.pegasys.teku.spec.datastructures.operations.AttestationData) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Bytes32(org.apache.tuweni.bytes.Bytes32) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) Domain(tech.pegasys.teku.spec.constants.Domain) MiscHelpers(tech.pegasys.teku.spec.logic.common.helpers.MiscHelpers) BLSSignature(tech.pegasys.teku.bls.BLSSignature) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) SszUInt64List(tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List) SchemaDefinitions(tech.pegasys.teku.spec.schemas.SchemaDefinitions) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) IntList(it.unimi.dsi.fastutil.ints.IntList) Logger(org.apache.logging.log4j.Logger) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) Comparator(java.util.Comparator) LogManager(org.apache.logging.log4j.LogManager) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) Bytes(org.apache.tuweni.bytes.Bytes) SszUInt64List(tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) Bytes32(org.apache.tuweni.bytes.Bytes32) BLSSignature(tech.pegasys.teku.bls.BLSSignature)

Aggregations

SszUInt64List (tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List)6 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)3 AttestationData (tech.pegasys.teku.spec.datastructures.operations.AttestationData)3 ValidateableAttestation (tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation)2 Attestation (tech.pegasys.teku.spec.datastructures.operations.Attestation)2 IndexedAttestation (tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation)2 IndexedAttestationSchema (tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation.IndexedAttestationSchema)2 Checkpoint (tech.pegasys.teku.spec.datastructures.state.Checkpoint)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Comparators (com.google.common.collect.Comparators)1 IntList (it.unimi.dsi.fastutil.ints.IntList)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Collectors.toList (java.util.stream.Collectors.toList)1 IntStream (java.util.stream.IntStream)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 Bytes (org.apache.tuweni.bytes.Bytes)1 Bytes32 (org.apache.tuweni.bytes.Bytes32)1 Test (org.junit.jupiter.api.Test)1