mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-09 03:17:55 +00:00
adding tests and payload changes
This commit is contained in:
@@ -19,5 +19,6 @@ types = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
beacon_chain = { workspace = true }
|
||||
bls = { workspace = true }
|
||||
store = { workspace = true }
|
||||
tokio = { workspace = true }
|
||||
|
||||
@@ -249,7 +249,6 @@ pub struct QueuedAttestation {
|
||||
attesting_indices: Vec<u64>,
|
||||
block_root: Hash256,
|
||||
target_epoch: Epoch,
|
||||
payload_present: bool,
|
||||
}
|
||||
|
||||
impl<'a, E: EthSpec> From<IndexedAttestationRef<'a, E>> for QueuedAttestation {
|
||||
@@ -259,11 +258,22 @@ impl<'a, E: EthSpec> From<IndexedAttestationRef<'a, E>> for QueuedAttestation {
|
||||
attesting_indices: a.attesting_indices_to_vec(),
|
||||
block_root: a.data().beacon_block_root,
|
||||
target_epoch: a.data().target.epoch,
|
||||
payload_present: a.data().index == 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Used for queuing payload attestations (PTC votes) from the current slot.
|
||||
/// Payload attestations have different dequeue timing than regular attestations:
|
||||
/// non-block payload attestations need an extra slot of delay (slot + 1 < current_slot).
|
||||
#[derive(Clone, PartialEq, Encode, Decode)]
|
||||
pub struct QueuedPayloadAttestation {
|
||||
slot: Slot,
|
||||
attesting_indices: Vec<u64>,
|
||||
block_root: Hash256,
|
||||
payload_present: bool,
|
||||
blob_data_available: bool,
|
||||
}
|
||||
|
||||
/// Returns all values in `self.queued_attestations` that have a slot that is earlier than the
|
||||
/// current slot. Also removes those values from `self.queued_attestations`.
|
||||
fn dequeue_attestations(
|
||||
@@ -285,6 +295,22 @@ fn dequeue_attestations(
|
||||
std::mem::replace(queued_attestations, remaining)
|
||||
}
|
||||
|
||||
/// Returns all values in `queued` that have `slot + 1 < current_slot`.
|
||||
/// Payload attestations need an extra slot of delay compared to regular attestations.
|
||||
fn dequeue_payload_attestations(
|
||||
current_slot: Slot,
|
||||
queued: &mut Vec<QueuedPayloadAttestation>,
|
||||
) -> Vec<QueuedPayloadAttestation> {
|
||||
let remaining = queued.split_off(
|
||||
queued
|
||||
.iter()
|
||||
.position(|a| a.slot.saturating_add(1_u64) >= current_slot)
|
||||
.unwrap_or(queued.len()),
|
||||
);
|
||||
|
||||
std::mem::replace(queued, remaining)
|
||||
}
|
||||
|
||||
/// Denotes whether an attestation we are processing was received from a block or from gossip.
|
||||
/// Equivalent to the `is_from_block` `bool` in:
|
||||
///
|
||||
@@ -329,6 +355,9 @@ pub struct ForkChoice<T, E> {
|
||||
proto_array: ProtoArrayForkChoice,
|
||||
/// Attestations that arrived at the current slot and must be queued for later processing.
|
||||
queued_attestations: Vec<QueuedAttestation>,
|
||||
/// Payload attestations (PTC votes) that must be queued for later processing.
|
||||
/// These have different dequeue timing than regular attestations.
|
||||
queued_payload_attestations: Vec<QueuedPayloadAttestation>,
|
||||
/// Stores a cache of the values required to be sent to the execution layer.
|
||||
forkchoice_update_parameters: ForkchoiceUpdateParameters,
|
||||
_phantom: PhantomData<E>,
|
||||
@@ -343,6 +372,7 @@ where
|
||||
self.fc_store == other.fc_store
|
||||
&& self.proto_array == other.proto_array
|
||||
&& self.queued_attestations == other.queued_attestations
|
||||
&& self.queued_payload_attestations == other.queued_payload_attestations
|
||||
}
|
||||
}
|
||||
|
||||
@@ -414,6 +444,7 @@ where
|
||||
fc_store,
|
||||
proto_array,
|
||||
queued_attestations: vec![],
|
||||
queued_payload_attestations: vec![],
|
||||
// This will be updated during the next call to `Self::get_head`.
|
||||
forkchoice_update_parameters: ForkchoiceUpdateParameters {
|
||||
head_hash: None,
|
||||
@@ -1120,7 +1151,7 @@ where
|
||||
});
|
||||
}
|
||||
|
||||
if indexed_payload_attestation.data.slot == block.slot
|
||||
if self.fc_store.get_current_slot() == block.slot
|
||||
&& indexed_payload_attestation.data.payload_present
|
||||
{
|
||||
return Err(InvalidAttestation::PayloadAttestationDuringSameSlot { slot: block.slot });
|
||||
@@ -1177,12 +1208,10 @@ where
|
||||
|
||||
if attestation.data().slot < self.fc_store.get_current_slot() {
|
||||
for validator_index in attestation.attesting_indices_iter() {
|
||||
let payload_present = attestation.data().index == 1;
|
||||
self.proto_array.process_attestation(
|
||||
*validator_index as usize,
|
||||
attestation.data().beacon_block_root,
|
||||
attestation.data().slot,
|
||||
payload_present,
|
||||
)?;
|
||||
}
|
||||
} else {
|
||||
@@ -1214,23 +1243,33 @@ where
|
||||
|
||||
self.validate_on_payload_attestation(attestation, is_from_block)?;
|
||||
|
||||
if attestation.data.slot < self.fc_store.get_current_slot() {
|
||||
let processing_slot = self.fc_store.get_current_slot();
|
||||
// Payload attestations from blocks can be applied in the next slot (S+1 for data.slot=S),
|
||||
// while non-block payload attestations are delayed one extra slot.
|
||||
let should_process_now = match is_from_block {
|
||||
AttestationFromBlock::True => attestation.data.slot < processing_slot,
|
||||
AttestationFromBlock::False => attestation.data.slot + 1_u64 < processing_slot,
|
||||
};
|
||||
|
||||
if should_process_now {
|
||||
for validator_index in attestation.attesting_indices_iter() {
|
||||
self.proto_array.process_attestation(
|
||||
self.proto_array.process_payload_attestation(
|
||||
*validator_index as usize,
|
||||
attestation.data.beacon_block_root,
|
||||
attestation.data.slot,
|
||||
processing_slot,
|
||||
attestation.data.payload_present,
|
||||
attestation.data.blob_data_available,
|
||||
)?;
|
||||
}
|
||||
} else {
|
||||
self.queued_attestations.push(QueuedAttestation {
|
||||
slot: attestation.data.slot,
|
||||
attesting_indices: attestation.attesting_indices.iter().copied().collect(),
|
||||
block_root: attestation.data.beacon_block_root,
|
||||
target_epoch: attestation.data.slot.epoch(E::slots_per_epoch()),
|
||||
payload_present: attestation.data.payload_present,
|
||||
});
|
||||
self.queued_payload_attestations
|
||||
.push(QueuedPayloadAttestation {
|
||||
slot: attestation.data.slot,
|
||||
attesting_indices: attestation.attesting_indices.iter().copied().collect(),
|
||||
block_root: attestation.data.beacon_block_root,
|
||||
payload_present: attestation.data.payload_present,
|
||||
blob_data_available: attestation.data.blob_data_available,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -1265,6 +1304,7 @@ where
|
||||
|
||||
// Process any attestations that might now be eligible.
|
||||
self.process_attestation_queue()?;
|
||||
self.process_payload_attestation_queue()?;
|
||||
|
||||
Ok(self.fc_store.get_current_slot())
|
||||
}
|
||||
@@ -1339,12 +1379,31 @@ where
|
||||
&mut self.queued_attestations,
|
||||
) {
|
||||
for validator_index in attestation.attesting_indices.iter() {
|
||||
// FIXME(sproul): backwards compat/fork abstraction
|
||||
self.proto_array.process_attestation(
|
||||
*validator_index as usize,
|
||||
attestation.block_root,
|
||||
attestation.slot,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Processes and removes from the queue any queued payload attestations which may now be
|
||||
/// eligible for processing. Payload attestations use `slot + 1 < current_slot` timing.
|
||||
fn process_payload_attestation_queue(&mut self) -> Result<(), Error<T::Error>> {
|
||||
let current_slot = self.fc_store.get_current_slot();
|
||||
for attestation in
|
||||
dequeue_payload_attestations(current_slot, &mut self.queued_payload_attestations)
|
||||
{
|
||||
for validator_index in attestation.attesting_indices.iter() {
|
||||
self.proto_array.process_payload_attestation(
|
||||
*validator_index as usize,
|
||||
attestation.block_root,
|
||||
current_slot,
|
||||
attestation.payload_present,
|
||||
attestation.blob_data_available,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
@@ -1507,6 +1566,11 @@ where
|
||||
&self.queued_attestations
|
||||
}
|
||||
|
||||
/// Returns a reference to the currently queued payload attestations.
|
||||
pub fn queued_payload_attestations(&self) -> &[QueuedPayloadAttestation] {
|
||||
&self.queued_payload_attestations
|
||||
}
|
||||
|
||||
/// Returns the store's `proposer_boost_root`.
|
||||
pub fn proposer_boost_root(&self) -> Hash256 {
|
||||
self.fc_store.proposer_boost_root()
|
||||
@@ -1591,6 +1655,7 @@ where
|
||||
fc_store,
|
||||
proto_array,
|
||||
queued_attestations: persisted.queued_attestations,
|
||||
queued_payload_attestations: persisted.queued_payload_attestations,
|
||||
// Will be updated in the following call to `Self::get_head`.
|
||||
forkchoice_update_parameters: ForkchoiceUpdateParameters {
|
||||
head_hash: None,
|
||||
@@ -1633,6 +1698,7 @@ where
|
||||
.proto_array()
|
||||
.as_ssz_container(self.justified_checkpoint(), self.finalized_checkpoint()),
|
||||
queued_attestations: self.queued_attestations().to_vec(),
|
||||
queued_payload_attestations: self.queued_payload_attestations.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1658,6 +1724,8 @@ pub struct PersistedForkChoice {
|
||||
#[superstruct(only(V29))]
|
||||
pub proto_array: proto_array::core::SszContainerV29,
|
||||
pub queued_attestations: Vec<QueuedAttestation>,
|
||||
#[superstruct(only(V29))]
|
||||
pub queued_payload_attestations: Vec<QueuedPayloadAttestation>,
|
||||
}
|
||||
|
||||
pub type PersistedForkChoice = PersistedForkChoiceV29;
|
||||
@@ -1682,6 +1750,7 @@ impl From<PersistedForkChoiceV28> for PersistedForkChoiceV29 {
|
||||
Self {
|
||||
proto_array: v28.proto_array_v28.into(),
|
||||
queued_attestations: v28.queued_attestations,
|
||||
queued_payload_attestations: vec![],
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1734,7 +1803,6 @@ mod tests {
|
||||
attesting_indices: vec![],
|
||||
block_root: Hash256::zero(),
|
||||
target_epoch: Epoch::new(0),
|
||||
payload_present: false,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ pub use crate::fork_choice::{
|
||||
AttestationFromBlock, Error, ForkChoice, ForkChoiceView, ForkchoiceUpdateParameters,
|
||||
InvalidAttestation, InvalidBlock, PayloadVerificationStatus, PersistedForkChoice,
|
||||
PersistedForkChoiceV17, PersistedForkChoiceV28, PersistedForkChoiceV29, QueuedAttestation,
|
||||
ResetPayloadStatuses,
|
||||
QueuedPayloadAttestation, ResetPayloadStatuses,
|
||||
};
|
||||
pub use fork_choice_store::ForkChoiceStore;
|
||||
pub use proto_array::{
|
||||
|
||||
@@ -7,9 +7,11 @@ use beacon_chain::{
|
||||
BeaconChain, BeaconChainError, BeaconForkChoiceStore, ChainConfig, ForkChoiceError,
|
||||
StateSkipConfig, WhenSlotSkipped,
|
||||
};
|
||||
use bls::AggregateSignature;
|
||||
use fixed_bytes::FixedBytesExtended;
|
||||
use fork_choice::{
|
||||
ForkChoiceStore, InvalidAttestation, InvalidBlock, PayloadVerificationStatus, QueuedAttestation,
|
||||
AttestationFromBlock, ForkChoiceStore, InvalidAttestation, InvalidBlock,
|
||||
PayloadVerificationStatus, QueuedAttestation, QueuedPayloadAttestation,
|
||||
};
|
||||
use state_processing::state_advance::complete_state_advance;
|
||||
use std::fmt;
|
||||
@@ -19,8 +21,8 @@ use store::MemoryStore;
|
||||
use types::SingleAttestation;
|
||||
use types::{
|
||||
BeaconBlockRef, BeaconState, ChainSpec, Checkpoint, Epoch, EthSpec, ForkName, Hash256,
|
||||
IndexedAttestation, MainnetEthSpec, RelativeEpoch, SignedBeaconBlock, Slot, SubnetId,
|
||||
test_utils::generate_deterministic_keypair,
|
||||
IndexedAttestation, IndexedPayloadAttestation, MainnetEthSpec, PayloadAttestationData,
|
||||
RelativeEpoch, SignedBeaconBlock, Slot, SubnetId, test_utils::generate_deterministic_keypair,
|
||||
};
|
||||
|
||||
pub type E = MainnetEthSpec;
|
||||
@@ -154,6 +156,28 @@ impl ForkChoiceTest {
|
||||
self
|
||||
}
|
||||
|
||||
/// Inspect the queued payload attestations in fork choice.
|
||||
#[allow(dead_code)]
|
||||
pub fn inspect_queued_payload_attestations<F>(self, mut func: F) -> Self
|
||||
where
|
||||
F: FnMut(&[QueuedPayloadAttestation]),
|
||||
{
|
||||
self.harness
|
||||
.chain
|
||||
.canonical_head
|
||||
.fork_choice_write_lock()
|
||||
.update_time(self.harness.chain.slot().unwrap())
|
||||
.unwrap();
|
||||
func(
|
||||
self.harness
|
||||
.chain
|
||||
.canonical_head
|
||||
.fork_choice_read_lock()
|
||||
.queued_payload_attestations(),
|
||||
);
|
||||
self
|
||||
}
|
||||
|
||||
/// Skip a slot, without producing a block.
|
||||
pub fn skip_slot(self) -> Self {
|
||||
self.harness.advance_slot();
|
||||
@@ -953,6 +977,119 @@ async fn invalid_attestation_payload_during_same_slot() {
|
||||
.await;
|
||||
}
|
||||
|
||||
/// A payload attestation for block A at slot S should be accepted when processed at slot S+1.
|
||||
#[tokio::test]
|
||||
async fn payload_attestation_for_previous_slot_is_accepted_at_next_slot() {
|
||||
let test = ForkChoiceTest::new()
|
||||
.apply_blocks_without_new_attestations(1)
|
||||
.await;
|
||||
|
||||
let chain = &test.harness.chain;
|
||||
let block_a = chain
|
||||
.block_at_slot(Slot::new(1), WhenSlotSkipped::Prev)
|
||||
.expect("lookup should succeed")
|
||||
.expect("block A should exist");
|
||||
let block_a_root = block_a.canonical_root();
|
||||
let current_slot = block_a.slot().saturating_add(1_u64);
|
||||
|
||||
let payload_attestation = IndexedPayloadAttestation::<E> {
|
||||
attesting_indices: vec![0_u64].try_into().expect("valid attesting indices"),
|
||||
data: PayloadAttestationData {
|
||||
beacon_block_root: block_a_root,
|
||||
slot: Slot::new(1),
|
||||
payload_present: true,
|
||||
blob_data_available: true,
|
||||
},
|
||||
signature: AggregateSignature::empty(),
|
||||
};
|
||||
|
||||
let result = chain
|
||||
.canonical_head
|
||||
.fork_choice_write_lock()
|
||||
.on_payload_attestation(
|
||||
current_slot,
|
||||
&payload_attestation,
|
||||
AttestationFromBlock::True,
|
||||
);
|
||||
|
||||
assert!(
|
||||
result.is_ok(),
|
||||
"payload attestation at slot S should be accepted at S+1, got: {:?}",
|
||||
result
|
||||
);
|
||||
|
||||
let latest_message = chain
|
||||
.canonical_head
|
||||
.fork_choice_read_lock()
|
||||
.latest_message(0)
|
||||
.expect("latest message should exist");
|
||||
assert_eq!(latest_message.slot, current_slot);
|
||||
assert!(latest_message.payload_present);
|
||||
}
|
||||
|
||||
/// Non-block payload attestations at slot S+1 for data.slot S are delayed; they are not applied
|
||||
/// until a later slot.
|
||||
#[tokio::test]
|
||||
async fn non_block_payload_attestation_at_next_slot_is_delayed() {
|
||||
let test = ForkChoiceTest::new()
|
||||
.apply_blocks_without_new_attestations(1)
|
||||
.await;
|
||||
|
||||
let chain = &test.harness.chain;
|
||||
let block_a = chain
|
||||
.block_at_slot(Slot::new(1), WhenSlotSkipped::Prev)
|
||||
.expect("lookup should succeed")
|
||||
.expect("block A should exist");
|
||||
let block_a_root = block_a.canonical_root();
|
||||
let s_plus_1 = block_a.slot().saturating_add(1_u64);
|
||||
let s_plus_2 = block_a.slot().saturating_add(2_u64);
|
||||
|
||||
let payload_attestation = IndexedPayloadAttestation::<E> {
|
||||
attesting_indices: vec![0_u64].try_into().expect("valid attesting indices"),
|
||||
data: PayloadAttestationData {
|
||||
beacon_block_root: block_a_root,
|
||||
slot: Slot::new(1),
|
||||
payload_present: true,
|
||||
blob_data_available: true,
|
||||
},
|
||||
signature: AggregateSignature::empty(),
|
||||
};
|
||||
|
||||
let result = chain
|
||||
.canonical_head
|
||||
.fork_choice_write_lock()
|
||||
.on_payload_attestation(s_plus_1, &payload_attestation, AttestationFromBlock::False);
|
||||
assert!(
|
||||
result.is_ok(),
|
||||
"payload attestation should be accepted for queueing"
|
||||
);
|
||||
|
||||
// Vote should not be applied yet; message remains unset.
|
||||
let latest_before = chain
|
||||
.canonical_head
|
||||
.fork_choice_read_lock()
|
||||
.latest_message(0);
|
||||
assert!(
|
||||
latest_before.is_none(),
|
||||
"non-block payload attestation at S+1 should not apply immediately"
|
||||
);
|
||||
|
||||
// Advance fork choice time to S+2, queue should now be processed.
|
||||
chain
|
||||
.canonical_head
|
||||
.fork_choice_write_lock()
|
||||
.update_time(s_plus_2)
|
||||
.expect("update_time should succeed");
|
||||
|
||||
let latest_after = chain
|
||||
.canonical_head
|
||||
.fork_choice_read_lock()
|
||||
.latest_message(0)
|
||||
.expect("latest message should exist after delay");
|
||||
assert_eq!(latest_after.slot, s_plus_2);
|
||||
assert!(latest_after.payload_present);
|
||||
}
|
||||
|
||||
/// Specification v0.12.1:
|
||||
///
|
||||
/// assert target.root == get_ancestor(store, attestation.data.beacon_block_root, target_slot)
|
||||
|
||||
Reference in New Issue
Block a user