Skip to content
Open
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 118 additions & 2 deletions beacon_node/operation_pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,8 @@ mod release_tests {
use super::attestation::earliest_attestation_validators;
use super::*;
use beacon_chain::test_utils::{
BeaconChainHarness, EphemeralHarnessType, RelativeSyncCommittee, test_spec,
BeaconChainHarness, EphemeralHarnessType, MakeAttestationOptions, RelativeSyncCommittee,
test_spec,
};
use bls::Keypair;
use maplit::hashset;
Expand Down Expand Up @@ -2454,6 +2455,16 @@ mod release_tests {
}
}

// Insert one more distinct PayloadAttestationMessage at a different slot
let new_slot = Slot::new(0);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this sort of test that checks we don't include payload attestations from old slots, should be a small separate test from the one testing the cap.

let validator_index = 0;
let msg = make_payload_attestation_message(new_slot, validator_index, parent_root);
op_pool.insert_payload_attestation_message(msg).unwrap();

// The op pool has a total of (4+1) distinct PayloadAttestationMessage
// 4 from Slot 1 and 1 from Slot 0
assert_eq!(op_pool.payload_attestation_messages.read().len(), 5);

// When: we pack attestations for block production at slot 2.
let mut advanced_state = state.clone();
state_processing::state_advance::complete_state_advance(
Expand All @@ -2468,11 +2479,116 @@ mod release_tests {
.unwrap();

// Then: one attestation per combo, sorted by participation (most first).
assert_eq!(attestations.len(), 4);
// Only MaxPayloadAttestations (4) can be packed
// payload_attestation from Slot 0 is ignored and not packed
assert_eq!(
attestations.len(),
MinimalEthSpec::max_payload_attestations()
);
let bit_counts: Vec<_> = attestations
.iter()
.map(|a| a.aggregation_bits.num_set_bits())
.collect();
assert_eq!(bit_counts, vec![3, 2, 1, 1]);
}

/// Test when payload_present is true and the slot is not the head slot, `index` in AttestationData = 1 (for Gloas)
/// If payload_present is false, `index` = 0
/// https://github.com/ethereum/consensus-specs/blame/6b2201c3c25603f24ae967a92bbce5340d672c5c/specs/gloas/validator.md#L97-L111
#[tokio::test]
async fn attestation_payload_present_changes_index_field() {
let spec = test_spec::<MinimalEthSpec>();
if spec.gloas_fork_epoch.is_none() {
return;
}

let num_validators = 64;
let harness = get_harness::<MinimalEthSpec>(num_validators, Some(spec.clone()));
let all_validators: Vec<usize> = (0..num_validators).collect();

harness
.add_attested_blocks_at_slots(
harness.get_current_state(),
&[Slot::new(1)],
&all_validators,
)
.await;

let head = harness.chain.canonical_head.cached_head();
let state = &head.snapshot.beacon_state;
let state_root = head.head_state_root();
let head_block_root = head.head_block_root();

let attestations_at_head = harness.make_unaggregated_attestations(
&all_validators,
state,
state_root,
head_block_root.into(),
Slot::new(1),
);

// when block.slot == current slot, index should always be 0
// https://github.com/ethereum/consensus-specs/blame/6b2201c3c25603f24ae967a92bbce5340d672c5c/specs/gloas/validator.md#L104-L105
for committee_attestations in &attestations_at_head {
for (attestation, _subnetid) in committee_attestations {
assert_eq!(
attestation.data().index,
0,
"Attestation at head slot should always have index=0"
);
}
}

// Advance to slot 2 without producing a block (skipped slot).
harness.advance_slot();

let attestations_with_payload_present = harness.make_unaggregated_attestations(
&all_validators,
state,
state_root,
head_block_root.into(),
Slot::new(2),
);

// head_block is still Slot 1 (since Slot 2 is a skipped slot)
// block.slot (Slot 1) != current_slot (Slot 2)
// With payload_present and attesting at Slot 2, index = 1
for committee_attestations in &attestations_with_payload_present {
for (attestation, _subnetid) in committee_attestations {
assert_eq!(
attestation.data().index,
1,
"Attestation after head slot should have index=1 when payload_present=true"
);
}
}

// Create attestations at Slot 2 without payload_present
let fork = spec.fork_at_epoch(Slot::new(2).epoch(MinimalEthSpec::slots_per_epoch()));
let (attestations_no_payload_present, _attesters) = harness.make_attestations_with_opts(
&all_validators,
state,
state_root,
head_block_root.into(),
Slot::new(2),
MakeAttestationOptions {
limit: None,
fork,
payload_present_override: Some(false),
},
);

// Without payload_present, index = 0
for (committee_attestations, _signed_aggregate_and_proof) in
&attestations_no_payload_present
{
for (attestation, _subnetid) in committee_attestations {
assert_eq!(
attestation.data().index,
0,
"Attestation after head slot should have index=0 when payload_present=false"
);
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Next steps should be:

  1. Make an OperationPool
  2. Insert all of the attestations created (from head slot, from the skipped slot, with and without payload_present)
  3. Check that op_pool.get_attestations orders the more profitable attestations with the correct payload_present field first.

I think this implies there are a few variations of this test:

  1. The payload is considered available (in state.execution_payload_availability), so we should pack index == 1 attestations first (because they earn the head reward).
  2. The payload is considered unavailable (in state.execution_payload_availability), so we should pack index == 0 attestations first (because they earn the head reward).
  3. Same as case (1) but hitting the limit on the number of attestations can be included, so we check that index == 1 attestations do get included, and that we prefer to exclude index == 0 attestations.
  4. Same as case (2) but checking that index == 0 get included and index == 1 get excluded once the capacity is reached.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}
Loading