Search in sources :

Example 1 with AttestationData

use of tech.pegasys.teku.spec.datastructures.operations.AttestationData in project teku by ConsenSys.

the class ValidatorApiHandlerTest method createAttestationData_shouldUseCorrectSourceWhenEpochTransitionRequired.

@Test
public void createAttestationData_shouldUseCorrectSourceWhenEpochTransitionRequired() {
    final UInt64 slot = spec.computeStartSlotAtEpoch(EPOCH);
    when(chainDataClient.getCurrentSlot()).thenReturn(slot);
    // Slot is from before the current epoch, so we need to ensure we process the epoch transition
    final UInt64 blockSlot = slot.minus(1);
    final BeaconState wrongState = createStateWithActiveValidators(blockSlot);
    final BeaconState rightState = createStateWithActiveValidators(slot);
    final SignedBeaconBlock block = dataStructureUtil.randomSignedBeaconBlock(wrongState.getSlot(), wrongState);
    final SignedBlockAndState blockAndState = new SignedBlockAndState(block, wrongState);
    final SafeFuture<Optional<SignedBlockAndState>> blockAndStateResult = completedFuture(Optional.of(blockAndState));
    when(chainDataClient.getSignedBlockAndStateInEffectAtSlot(slot)).thenReturn(blockAndStateResult);
    when(chainDataClient.getCheckpointState(EPOCH, blockAndState)).thenReturn(SafeFuture.completedFuture(CheckpointState.create(spec, new Checkpoint(EPOCH, block.getRoot()), block, rightState)));
    when(forkChoiceTrigger.prepareForAttestationProduction(slot)).thenReturn(SafeFuture.COMPLETE);
    final int committeeIndex = 0;
    final SafeFuture<Optional<AttestationData>> result = validatorApiHandler.createAttestationData(slot, committeeIndex);
    assertThat(result).isCompleted();
    final Optional<AttestationData> maybeAttestation = result.join();
    assertThat(maybeAttestation).isPresent();
    final AttestationData attestationData = maybeAttestation.orElseThrow();
    assertThat(attestationData).isEqualTo(spec.getGenericAttestationData(slot, rightState, block.getMessage(), UInt64.valueOf(committeeIndex)));
    assertThat(attestationData.getSlot()).isEqualTo(slot);
}
Also used : AttestationData(tech.pegasys.teku.spec.datastructures.operations.AttestationData) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) Optional(java.util.Optional) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) Test(org.junit.jupiter.api.Test)

Example 2 with AttestationData

use of tech.pegasys.teku.spec.datastructures.operations.AttestationData in project teku by ConsenSys.

the class LocalSignerTest method shouldSignAttestationData.

@Test
public void shouldSignAttestationData() {
    final AttestationData attestationData = dataStructureUtil.randomAttestationData();
    final BLSSignature expectedSignature = BLSSignature.fromBytesCompressed(Bytes.fromBase64String("l1DUv3fmbvZanhCaaraMk2PKAl+33sf3UHMbxkv18CKILzzIz+Hr6hnLXCHqWQYEGKTtLcf6OLV7Z+Y21BW2bBtJHXJqqzvWkec/j0X0hWaEoWOSAs20sipO1WSIUY2m"));
    final SafeFuture<BLSSignature> result = signer.signAttestationData(attestationData, fork);
    asyncRunner.executeQueuedActions();
    assertThat(result).isCompletedWithValue(expectedSignature);
}
Also used : AttestationData(tech.pegasys.teku.spec.datastructures.operations.AttestationData) BLSSignature(tech.pegasys.teku.bls.BLSSignature) Test(org.junit.jupiter.api.Test)

Example 3 with AttestationData

use of tech.pegasys.teku.spec.datastructures.operations.AttestationData in project teku by ConsenSys.

the class SlashingProtectedSignerTest method signAttestationData_shouldNotSignWhenSlashingProtectionRejects.

@Test
void signAttestationData_shouldNotSignWhenSlashingProtectionRejects() {
    final AttestationData attestationData = dataStructureUtil.randomAttestationData();
    when(slashingProtector.maySignAttestation(publicKey, forkInfo.getGenesisValidatorsRoot(), attestationData.getSource().getEpoch(), attestationData.getTarget().getEpoch())).thenReturn(SafeFuture.completedFuture(false));
    when(delegate.signAttestationData(attestationData, forkInfo)).thenReturn(signatureFuture);
    assertThatSafeFuture(signer.signAttestationData(attestationData, forkInfo)).isCompletedExceptionallyWith(SlashableConditionException.class);
}
Also used : AttestationData(tech.pegasys.teku.spec.datastructures.operations.AttestationData) Test(org.junit.jupiter.api.Test)

Example 4 with AttestationData

use of tech.pegasys.teku.spec.datastructures.operations.AttestationData in project teku by ConsenSys.

the class AttestationUtil method getGenericAttestationData.

// Get attestation data that does not include attester specific shard or crosslink information
public AttestationData getGenericAttestationData(UInt64 slot, BeaconState state, BeaconBlockSummary block, final UInt64 committeeIndex) {
    UInt64 epoch = miscHelpers.computeEpochAtSlot(slot);
    // Get variables necessary that can be shared among Attestations of all validators
    Bytes32 beacon_block_root = block.getRoot();
    UInt64 start_slot = miscHelpers.computeStartSlotAtEpoch(epoch);
    Bytes32 epoch_boundary_block_root = start_slot.compareTo(slot) == 0 || state.getSlot().compareTo(start_slot) <= 0 ? block.getRoot() : beaconStateAccessors.getBlockRootAtSlot(state, start_slot);
    Checkpoint source = state.getCurrent_justified_checkpoint();
    Checkpoint target = new Checkpoint(epoch, epoch_boundary_block_root);
    // Set attestation data
    return new AttestationData(slot, committeeIndex, beacon_block_root, source, target);
}
Also used : AttestationData(tech.pegasys.teku.spec.datastructures.operations.AttestationData) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Bytes32(org.apache.tuweni.bytes.Bytes32)

Example 5 with AttestationData

use of tech.pegasys.teku.spec.datastructures.operations.AttestationData in project teku by ConsenSys.

the class AltairStateUpgrade method translateParticipation.

private void translateParticipation(final MutableBeaconStateAltair state, final SszList<PendingAttestation> pendingAttestations) {
    for (PendingAttestation attestation : pendingAttestations) {
        final AttestationData data = attestation.getData();
        final UInt64 inclusionDelay = attestation.getInclusion_delay();
        // Translate attestation inclusion info to flag indices
        final List<Integer> participationFlagIndices = beaconStateAccessors.getAttestationParticipationFlagIndices(state, data, inclusionDelay);
        // Apply flags to all attesting validators
        final SszMutableList<SszByte> epochParticipation = state.getPreviousEpochParticipation();
        attestationUtil.streamAttestingIndices(state, data, attestation.getAggregation_bits()).forEach(index -> {
            final byte previousFlags = epochParticipation.get(index).get();
            byte newFlags = previousFlags;
            for (int flagIndex : participationFlagIndices) {
                newFlags = miscHelpersAltair.addFlag(newFlags, flagIndex);
            }
            if (previousFlags != newFlags) {
                epochParticipation.set(index, SszByte.of(newFlags));
            }
        });
    }
}
Also used : PendingAttestation(tech.pegasys.teku.spec.datastructures.state.PendingAttestation) AttestationData(tech.pegasys.teku.spec.datastructures.operations.AttestationData) SszByte(tech.pegasys.teku.infrastructure.ssz.primitive.SszByte) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) SszUInt64(tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64)

Aggregations

AttestationData (tech.pegasys.teku.spec.datastructures.operations.AttestationData)73 Test (org.junit.jupiter.api.Test)58 Attestation (tech.pegasys.teku.spec.datastructures.operations.Attestation)38 ValidateableAttestation (tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation)27 Optional (java.util.Optional)18 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)15 Checkpoint (tech.pegasys.teku.spec.datastructures.state.Checkpoint)14 BeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)14 BLSSignature (tech.pegasys.teku.bls.BLSSignature)10 List (java.util.List)8 SignedAggregateAndProof (tech.pegasys.teku.spec.datastructures.operations.SignedAggregateAndProof)7 Validator (tech.pegasys.teku.validator.client.Validator)7 Bytes32 (org.apache.tuweni.bytes.Bytes32)6 SignedBlockAndState (tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState)6 Spec (tech.pegasys.teku.spec.Spec)5 IntList (it.unimi.dsi.fastutil.ints.IntList)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Mockito.mock (org.mockito.Mockito.mock)4 Mockito.when (org.mockito.Mockito.when)4 SafeFuture (tech.pegasys.teku.infrastructure.async.SafeFuture)4