Skip to content

Commit 6e9d66a

Browse files
authored
Merge of #6890
2 parents 6b40b98 + 53fb5ea commit 6e9d66a

File tree

2 files changed

+35
-37
lines changed

2 files changed

+35
-37
lines changed

beacon_node/network/src/subnet_service/mod.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ impl<T: BeaconChainTypes> SubnetService<T> {
216216
|| self.permanent_attestation_subscriptions.contains(subnet)
217217
}
218218

219+
/// Returns whether we are subscribed to a permanent subnet for testing purposes.
220+
#[cfg(test)]
221+
pub(crate) fn is_subscribed_permanent(&self, subnet: &Subnet) -> bool {
222+
self.permanent_attestation_subscriptions.contains(subnet)
223+
}
224+
219225
/// Processes a list of validator subscriptions.
220226
///
221227
/// This is fundamentally called form the HTTP API when a validator requests duties from us
@@ -629,9 +635,10 @@ impl<T: BeaconChainTypes> Stream for SubnetService<T> {
629635
// expire subscription.
630636
match self.scheduled_subscriptions.poll_next_unpin(cx) {
631637
Poll::Ready(Some(Ok(exact_subnet))) => {
632-
let ExactSubnet { subnet, .. } = exact_subnet;
633-
let current_slot = self.beacon_chain.slot_clock.now().unwrap_or_default();
634-
if let Err(e) = self.subscribe_to_subnet_immediately(subnet, current_slot + 1) {
638+
let ExactSubnet { subnet, slot } = exact_subnet;
639+
// Set the `end_slot` for the subscription to be `duty.slot + 1` so that we unsubscribe
640+
// only at the end of the duty slot.
641+
if let Err(e) = self.subscribe_to_subnet_immediately(subnet, slot + 1) {
635642
debug!(self.log, "Failed to subscribe to short lived subnet"; "subnet" => ?subnet, "err" => e);
636643
}
637644
self.waker

beacon_node/network/src/subnet_service/tests/mod.rs

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ use beacon_chain::{
77
};
88
use genesis::{generate_deterministic_keypairs, interop_genesis_state, DEFAULT_ETH1_BLOCK_HASH};
99
use lighthouse_network::NetworkConfig;
10-
use logging::test_logger;
11-
use slog::{o, Drain, Logger};
12-
use sloggers::{null::NullLoggerBuilder, Build};
1310
use slot_clock::{SlotClock, SystemTimeSlotClock};
1411
use std::sync::{Arc, LazyLock};
1512
use std::time::{Duration, SystemTime};
@@ -21,10 +18,6 @@ use types::{
2118
SyncCommitteeSubscription, SyncSubnetId, ValidatorSubscription,
2219
};
2320

24-
// Set to enable/disable logging
25-
// const TEST_LOG_LEVEL: Option<slog::Level> = Some(slog::Level::Debug);
26-
const TEST_LOG_LEVEL: Option<slog::Level> = None;
27-
2821
const SLOT_DURATION_MILLIS: u64 = 400;
2922

3023
type TestBeaconChainType = Witness<
@@ -46,7 +39,7 @@ impl TestBeaconChain {
4639

4740
let keypairs = generate_deterministic_keypairs(1);
4841

49-
let log = get_logger(TEST_LOG_LEVEL);
42+
let log = logging::test_logger();
5043
let store =
5144
HotColdDB::open_ephemeral(StoreConfig::default(), spec.clone(), log.clone()).unwrap();
5245

@@ -98,28 +91,10 @@ pub fn recent_genesis_time() -> u64 {
9891
.as_secs()
9992
}
10093

101-
fn get_logger(log_level: Option<slog::Level>) -> Logger {
102-
if let Some(level) = log_level {
103-
let drain = {
104-
let decorator = slog_term::TermDecorator::new().build();
105-
let decorator =
106-
logging::AlignedTermDecorator::new(decorator, logging::MAX_MESSAGE_WIDTH);
107-
let drain = slog_term::FullFormat::new(decorator).build().fuse();
108-
let drain = slog_async::Async::new(drain).chan_size(2048).build();
109-
drain.filter_level(level)
110-
};
111-
112-
Logger::root(drain.fuse(), o!())
113-
} else {
114-
let builder = NullLoggerBuilder;
115-
builder.build().expect("should build logger")
116-
}
117-
}
118-
11994
static CHAIN: LazyLock<TestBeaconChain> = LazyLock::new(TestBeaconChain::new_with_system_clock);
12095

12196
fn get_subnet_service() -> SubnetService<TestBeaconChainType> {
122-
let log = test_logger();
97+
let log = logging::test_logger();
12398
let config = NetworkConfig::default();
12499

125100
let beacon_chain = CHAIN.chain.clone();
@@ -501,8 +476,6 @@ mod test {
501476
let committee_count = 1;
502477

503478
// Makes 3 validator subscriptions to the same subnet but at different slots.
504-
// There should be just 1 unsubscription event for each of the later slots subscriptions
505-
// (subscription_slot2 and subscription_slot3).
506479
let subscription_slot1 = 0;
507480
let subscription_slot2 = MIN_PEER_DISCOVERY_SLOT_LOOK_AHEAD + 4;
508481
let subscription_slot3 = subscription_slot2 * 2;
@@ -585,7 +558,7 @@ mod test {
585558
let expected_unsubscription =
586559
SubnetServiceMessage::Unsubscribe(Subnet::Attestation(subnet_id1));
587560

588-
if !subnet_service.is_subscribed(&Subnet::Attestation(subnet_id1)) {
561+
if !subnet_service.is_subscribed_permanent(&Subnet::Attestation(subnet_id1)) {
589562
assert_eq!(expected_subscription, events[0]);
590563
assert_eq!(expected_unsubscription, events[2]);
591564
}
@@ -607,9 +580,18 @@ mod test {
607580

608581
assert_eq!(no_events, []);
609582

610-
let second_subscribe_event = get_events(&mut subnet_service, None, 2).await;
583+
let subscription_end_slot = current_slot + subscription_slot2 + 2; // +1 to get to the end of the duty slot, +1 for the slot to complete
584+
let wait_slots = subnet_service
585+
.beacon_chain
586+
.slot_clock
587+
.duration_to_slot(subscription_end_slot)
588+
.unwrap()
589+
.as_millis() as u64
590+
/ SLOT_DURATION_MILLIS;
591+
592+
let second_subscribe_event = get_events(&mut subnet_service, None, wait_slots as u32).await;
611593
// If the permanent and short lived subnets are different, we should get an unsubscription event.
612-
if !subnet_service.is_subscribed(&Subnet::Attestation(subnet_id1)) {
594+
if !subnet_service.is_subscribed_permanent(&Subnet::Attestation(subnet_id1)) {
613595
assert_eq!(
614596
[
615597
expected_subscription.clone(),
@@ -633,9 +615,18 @@ mod test {
633615

634616
assert_eq!(no_events, []);
635617

636-
let third_subscribe_event = get_events(&mut subnet_service, None, 2).await;
618+
let subscription_end_slot = current_slot + subscription_slot3 + 2; // +1 to get to the end of the duty slot, +1 for the slot to complete
619+
let wait_slots = subnet_service
620+
.beacon_chain
621+
.slot_clock
622+
.duration_to_slot(subscription_end_slot)
623+
.unwrap()
624+
.as_millis() as u64
625+
/ SLOT_DURATION_MILLIS;
626+
627+
let third_subscribe_event = get_events(&mut subnet_service, None, wait_slots as u32).await;
637628

638-
if !subnet_service.is_subscribed(&Subnet::Attestation(subnet_id1)) {
629+
if !subnet_service.is_subscribed_permanent(&Subnet::Attestation(subnet_id1)) {
639630
assert_eq!(
640631
[expected_subscription, expected_unsubscription],
641632
third_subscribe_event[..]

0 commit comments

Comments
 (0)