@@ -29,6 +29,10 @@ pub(crate) const MIN_PEER_DISCOVERY_SLOT_LOOK_AHEAD: u64 = 2;
29
29
/// Currently a whole slot ahead.
30
30
const ADVANCE_SUBSCRIBE_SLOT_FRACTION : u32 = 1 ;
31
31
32
+ /// The number of slots after an aggregator duty where we remove the entry from
33
+ /// `aggregate_validators_on_subnet` delay map.
34
+ const UNSUBSCRIBE_AFTER_AGGREGATOR_DUTY : u32 = 2 ;
35
+
32
36
#[ derive( Debug , Clone , Copy , Hash , PartialEq , Eq ) ]
33
37
pub ( crate ) enum SubscriptionKind {
34
38
/// Long lived subscriptions.
@@ -462,23 +466,27 @@ impl<T: BeaconChainTypes> AttestationService<T> {
462
466
) -> Result < ( ) , & ' static str > {
463
467
let slot_duration = self . beacon_chain . slot_clock . slot_duration ( ) ;
464
468
469
+ // The short time we schedule the subscription before it's actually required. This
470
+ // ensures we are subscribed on time, and allows consecutive subscriptions to the same
471
+ // subnet to overlap, reducing subnet churn.
472
+ let advance_subscription_duration = slot_duration / ADVANCE_SUBSCRIBE_SLOT_FRACTION ;
473
+ // The time to the required slot.
474
+ let time_to_subscription_slot = self
475
+ . beacon_chain
476
+ . slot_clock
477
+ . duration_to_slot ( slot)
478
+ . unwrap_or_default ( ) ; // If this is a past slot we will just get a 0 duration.
479
+
465
480
// Calculate how long before we need to subscribe to the subnet.
466
- let time_to_subscription_start = {
467
- // The short time we schedule the subscription before it's actually required. This
468
- // ensures we are subscribed on time, and allows consecutive subscriptions to the same
469
- // subnet to overlap, reducing subnet churn.
470
- let advance_subscription_duration = slot_duration / ADVANCE_SUBSCRIBE_SLOT_FRACTION ;
471
- // The time to the required slot.
472
- let time_to_subscription_slot = self
473
- . beacon_chain
474
- . slot_clock
475
- . duration_to_slot ( slot)
476
- . unwrap_or_default ( ) ; // If this is a past slot we will just get a 0 duration.
477
- time_to_subscription_slot. saturating_sub ( advance_subscription_duration)
478
- } ;
481
+ let time_to_subscription_start =
482
+ time_to_subscription_slot. saturating_sub ( advance_subscription_duration) ;
479
483
484
+ // The time after a duty slot where we no longer need it in the `aggregate_validators_on_subnet`
485
+ // delay map.
486
+ let time_to_unsubscribe =
487
+ time_to_subscription_slot + UNSUBSCRIBE_AFTER_AGGREGATOR_DUTY * slot_duration;
480
488
if let Some ( tracked_vals) = self . aggregate_validators_on_subnet . as_mut ( ) {
481
- tracked_vals. insert ( ExactSubnet { subnet_id, slot } ) ;
489
+ tracked_vals. insert_at ( ExactSubnet { subnet_id, slot } , time_to_unsubscribe ) ;
482
490
}
483
491
484
492
// If the subscription should be done in the future, schedule it. Otherwise subscribe
0 commit comments