Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ pub(crate) fn verify_bid_state_conditions<E: EthSpec>(
spec: &ChainSpec,
) -> Result<(), PayloadBidError> {
let builder_index = bid.builder_index();
verify_builder_eligibility(builder_index, head_state, spec)?;
verify_builder_can_cover_bid(builder_index, bid, head_state, spec)
}

/// Verify that the bid's builder is a payload builder and is active in `head_state`.
///
/// Split out so gossip can run its bid-limit check before [`verify_builder_can_cover_bid`].
pub(crate) fn verify_builder_eligibility<E: EthSpec>(
builder_index: u64,
head_state: &BeaconState<E>,
spec: &ChainSpec,
) -> Result<(), PayloadBidError> {
let builder_version = head_state
.get_builder(builder_index)
.map_err(|_| PayloadBidError::InvalidBuilder { builder_index })?
Expand All @@ -172,6 +184,16 @@ pub(crate) fn verify_bid_state_conditions<E: EthSpec>(
return Err(PayloadBidError::InvalidBuilder { builder_index });
}

Ok(())
}

/// Verify that the bid's builder can cover the bid value in `head_state`.
pub(crate) fn verify_builder_can_cover_bid<E: EthSpec>(
builder_index: u64,
bid: ExecutionPayloadBidRef<E>,
head_state: &BeaconState<E>,
spec: &ChainSpec,
) -> Result<(), PayloadBidError> {
if !head_state.can_builder_cover_bid(builder_index, bid.value(), spec)? {
return Err(PayloadBidError::BuilderCantCoverBid {
builder_index,
Expand Down Expand Up @@ -321,20 +343,6 @@ impl<E: EthSpec> GossipVerifiedPayloadBid<E> {
let bid_value = signed_bid.message().value();
verify_bid_slot_range(ctx.slot_clock, bid_slot, ctx.spec)?;

if ctx
.gossip_verified_payload_bid_cache
.seen_builder_bid_for_parent(
&bid_slot,
bid_parent,
signed_bid.message().builder_index(),
)
{
return Err(PayloadBidError::BuilderAlreadySeen {
builder_index: signed_bid.message().builder_index(),
slot: bid_slot,
});
}

// TODO(gloas): Extract into `bid_value_over_threshold` on the bid cache and potentially
// make this more sophisticated than just a <= check.
if let Some(cached_bid) = ctx
Expand Down Expand Up @@ -466,7 +474,24 @@ impl<E: EthSpec> GossipVerifiedPayloadBid<E> {
return Err(PayloadBidError::InvalidPrevRandao { slot: bid_slot });
}

verify_bid_state_conditions(signed_bid.message(), head_state, ctx.spec)?;
let builder_index = signed_bid.message().builder_index();
verify_builder_eligibility(builder_index, head_state, ctx.spec)?;

// [IGNORE] The builder has not reached its bid limit for this slot and parent
let bid_limit = head_state.get_builder_bid_limit(builder_index)?;
if ctx.gossip_verified_payload_bid_cache.builder_bid_count(
&bid_slot,
bid_parent,
builder_index,
) >= bid_limit
{
return Err(PayloadBidError::BuilderBidLimitReached {
builder_index,
slot: bid_slot,
});
}

verify_builder_can_cover_bid(builder_index, signed_bid.message(), head_state, ctx.spec)?;

if parent_payload_exits_builder::<T>(
signed_bid.message(),
Expand Down
4 changes: 2 additions & 2 deletions beacon_node/beacon_chain/src/payload_bid_verification/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ pub enum PayloadBidError {
InvalidParentBlockRoot { bid: Hash256, expected: Hash256 },
/// The signature is invalid.
BadSignature,
/// A bid for this builder at this slot has already been seen.
BuilderAlreadySeen { builder_index: u64, slot: Slot },
/// The builder has reached its bid limit for this slot and parent.
BuilderBidLimitReached { builder_index: u64, slot: Slot },
/// Builder is not valid/active for the given epoch
InvalidBuilder { builder_index: u64 },
/// The bid was signed by a builder not in the requesting entry's `builder_pubkeys` (the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use educe::Educe;
use parking_lot::RwLock;
use std::{
collections::hash_map,
collections::{BTreeMap, HashMap, HashSet},
collections::{BTreeMap, HashMap},
sync::Arc,
};
use types::{
Expand Down Expand Up @@ -39,18 +39,18 @@ type HighestBidMap<E> = BTreeMap<Slot, HashMap<BidParent, GossipVerifiedPayloadB
pub struct GossipBidCacheInner<E: EthSpec> {
/// The current best bid for each `(slot, BidParent)`.
highest_bid: HighestBidMap<E>,
/// The `(BidParent, BuilderIndex)` pairs for which we have already accepted a gossip-verified
/// bid, per slot.
/// The number of gossip-verified bids accepted for each `(BidParent, BuilderIndex)` pair, per
/// slot.
///
/// Used to enforce one bid per builder per parent per slot: a builder may bid once for each
/// parent view compatible with the head.
seen_builder_bids: BTreeMap<Slot, HashSet<(BidParent, BuilderIndex)>>,
/// Used to enforce the builder bid limit: a builder may submit up to its limit of bids for
/// each slot and parent view compatible with the head.
builder_bid_counts: BTreeMap<Slot, HashMap<(BidParent, BuilderIndex), u64>>,
}

/// A cache of gossip-verified payload bids.
///
/// Tracks, per slot, the highest-value bid observed for each parent a bid may build on and the
/// set of `(parent, builder)` pairs that have already bid, so that duplicate and lower-value
/// number of bids each `(parent, builder)` pair has made, so that over-limit and lower-value
/// gossip bids can be rejected. Stale entries are removed via [`prune`](Self::prune) as the chain
/// advances.
#[derive(Educe)]
Expand Down Expand Up @@ -82,8 +82,8 @@ impl<E: EthSpec> GossipVerifiedPayloadBidCache<E> {

/// Record a gossip-verified `bid` in the cache.
///
/// This always marks the bid's `(parent, builder)` pair as seen for the bid's slot (see
/// [`seen_builder_bid_for_parent`](Self::seen_builder_bid_for_parent)). Additionally, if the
/// This always increments the bid count of the bid's `(parent, builder)` pair for the bid's
/// slot (see [`builder_bid_count`](Self::builder_bid_count)). Additionally, if the
/// bid has a strictly higher value than the currently cached bid for its `(slot, BidParent)`
/// (or no bid is cached yet), it replaces the cached bid.
///
Expand All @@ -93,11 +93,13 @@ impl<E: EthSpec> GossipVerifiedPayloadBidCache<E> {
let slot = bid.signed_bid.message().slot();
let key = BidParent::from_bid(bid.signed_bid.message());
let mut inner = self.inner.write();
inner
.seen_builder_bids
let bid_count = inner
.builder_bid_counts
.entry(slot)
.or_default()
.insert((key, bid.signed_bid.message().builder_index()));
.entry((key, bid.signed_bid.message().builder_index()))
.or_default();
*bid_count = bid_count.saturating_add(1u64);

match inner.highest_bid.entry(slot).or_default().entry(key) {
hash_map::Entry::Vacant(entry) => {
Expand All @@ -114,28 +116,29 @@ impl<E: EthSpec> GossipVerifiedPayloadBidCache<E> {
}
}

/// Returns `true` if a gossip-verified bid from `builder_index` has already been seen for
/// Returns the number of gossip-verified bids seen from `builder_index` for
/// `(slot, bid_parent)`.
pub fn seen_builder_bid_for_parent(
pub fn builder_bid_count(
&self,
slot: &Slot,
bid_parent: BidParent,
builder_index: BuilderIndex,
) -> bool {
) -> u64 {
self.inner
.read()
.seen_builder_bids
.builder_bid_counts
.get(slot)
.is_some_and(|seen_builders| seen_builders.contains(&(bid_parent, builder_index)))
.and_then(|bid_counts| bid_counts.get(&(bid_parent, builder_index)).copied())
.unwrap_or_default()
}

/// Removes all cached bids and seen-builder records for slots older than `current_slot`.
/// Removes all cached bids and bid counts for slots older than `current_slot`.
///
/// Entries for `current_slot` and later are retained.
pub fn prune(&self, current_slot: Slot) {
let mut inner = self.inner.write();
inner.highest_bid = inner.highest_bid.split_off(&current_slot);
inner.seen_builder_bids = inner.seen_builder_bids.split_off(&current_slot);
inner.builder_bid_counts = inner.builder_bid_counts.split_off(&current_slot);
}
}

Expand Down Expand Up @@ -179,7 +182,7 @@ mod tests {
}

#[test]
fn seen_builder_for_parent() {
fn builder_bid_count_for_parent() {
let cache = GossipVerifiedPayloadBidCache::<E>::default();
let slot = Slot::new(1);
let parent_a = BidParent {
Expand All @@ -200,11 +203,21 @@ mod tests {
);
cache.observe_bid(verified);

// Seen only for the exact (slot, parent tuple, builder) combination.
assert!(cache.seen_builder_bid_for_parent(&slot, parent_a, 0));
assert!(!cache.seen_builder_bid_for_parent(&slot, parent_b, 0));
assert!(!cache.seen_builder_bid_for_parent(&slot, parent_a, 1));
assert!(!cache.seen_builder_bid_for_parent(&Slot::new(2), parent_a, 0));
// Counted only for the exact (slot, parent tuple, builder) combination.
assert_eq!(cache.builder_bid_count(&slot, parent_a, 0), 1);
assert_eq!(cache.builder_bid_count(&slot, parent_b, 0), 0);
assert_eq!(cache.builder_bid_count(&slot, parent_a, 1), 0);
assert_eq!(cache.builder_bid_count(&Slot::new(2), parent_a, 0), 0);

// A repeat bid for the same combination increments its count.
cache.observe_bid(make_gossip_verified(
slot,
0,
parent_a.parent_block_hash,
parent_a.parent_block_root,
200,
));
assert_eq!(cache.builder_bid_count(&slot, parent_a, 0), 2);
}

#[test]
Expand Down Expand Up @@ -292,12 +305,18 @@ mod tests {
// Slots 1-7 pruned from both maps.
for slot in [1, 2, 3, 7] {
assert!(cache.get_highest_bid(Slot::new(slot), bid_parent).is_none());
assert!(!cache.seen_builder_bid_for_parent(&Slot::new(slot), bid_parent, slot));
assert_eq!(
cache.builder_bid_count(&Slot::new(slot), bid_parent, slot),
0
);
}
// Slots 8-10 retained in both maps.
for slot in [8, 9, 10] {
assert!(cache.get_highest_bid(Slot::new(slot), bid_parent).is_some());
assert!(cache.seen_builder_bid_for_parent(&Slot::new(slot), bid_parent, slot));
assert_eq!(
cache.builder_bid_count(&Slot::new(slot), bid_parent, slot),
1
);
}
}
}
Loading
Loading