Skip to content

Commit 957ecc4

Browse files
committed
fix(gossip): validate attestations against attestation slot state
1 parent 1689416 commit 957ecc4

2 files changed

Lines changed: 21 additions & 20 deletions

File tree

crates/networking/manager/src/gossipsub/handle.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,13 @@ async fn import_gossip_attestation(
134134
let (attestation, should_process_attestation) = {
135135
let store = beacon_chain.store.lock().await;
136136
let head_root = store.get_head()?;
137-
let state =
137+
let mut state =
138138
store.db.state_provider().get(head_root)?.ok_or_else(|| {
139139
anyhow::anyhow!("No beacon state found for head root: {head_root}")
140140
})?;
141+
if state.slot < single_attestation.data.slot {
142+
state.process_slots(single_attestation.data.slot)?;
143+
}
141144
let attestation = single_attestation_to_attestation(single_attestation, &state)?;
142145

143146
store

crates/networking/manager/src/gossipsub/validate/beacon_attestation.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,26 @@ pub async fn validate_beacon_attestation(
2525
let store = beacon_chain.store.lock().await;
2626

2727
let head_root = store.get_head()?;
28-
let state: BeaconState = store
28+
let mut state: BeaconState = store
2929
.db
3030
.state_provider()
3131
.get(head_root)?
3232
.ok_or_else(|| anyhow!("No beacon state found for head root: {head_root}"))?;
3333

34+
let current_slot = store.get_current_slot()?;
35+
36+
// [IGNORE] attestation.data.slot is equal to or earlier than the current_slot (with a
37+
// MAXIMUM_GOSSIP_CLOCK_DISPARITY allowance)
38+
if attestation.data.slot > current_slot {
39+
return Ok(ValidationResult::Ignore(
40+
"Attestation is from a future slot".to_string(),
41+
));
42+
}
43+
44+
if state.slot < attestation.data.slot {
45+
state.process_slots(attestation.data.slot)?;
46+
}
47+
3448
let committee_index = attestation.committee_index;
3549
let committees_per_slot = state.get_committee_count_per_slot(attestation.data.target.epoch);
3650

@@ -58,27 +72,11 @@ pub async fn validate_beacon_attestation(
5872
));
5973
}
6074

61-
let block = store
62-
.db
63-
.block_provider()
64-
.get(head_root)?
65-
.ok_or_else(|| anyhow!("Could not get block for head root: {head_root}"))?;
66-
67-
let current_slot = block.message.slot;
68-
69-
// [IGNORE] attestation.data.slot is equal to or earlier than the current_slot (with a
70-
// MAXIMUM_GOSSIP_CLOCK_DISPARITY allowance)
71-
if attestation.data.slot > current_slot {
72-
return Ok(ValidationResult::Ignore(
73-
"Attestation is from a future slot".to_string(),
74-
));
75-
}
76-
7775
// [IGNORE] the epoch of attestation.data.slot is either the current or previous epoch (with a
7876
// MAXIMUM_GOSSIP_CLOCK_DISPARITY allowance)
7977
let attestation_epoch = compute_epoch_at_slot(attestation.data.slot);
80-
let current_epoch = state.get_current_epoch();
81-
let previous_epoch = state.get_previous_epoch();
78+
let current_epoch = compute_epoch_at_slot(current_slot);
79+
let previous_epoch = current_epoch.saturating_sub(1);
8280

8381
if attestation_epoch != current_epoch && attestation_epoch != previous_epoch {
8482
return Ok(ValidationResult::Ignore(

0 commit comments

Comments
 (0)