Skip to content

Commit 09849e8

Browse files
authored
Use sync_tolerance_epochs flag to control the proposer prep routines (#7044)
Replace the `2 + 2 == 5` hacks from `holesky-rescue` and use the existing `sync_tolerance_epochs` flag to control the proposer prep routines.
1 parent 066f287 commit 09849e8

File tree

5 files changed

+35
-29
lines changed

5 files changed

+35
-29
lines changed

beacon_node/beacon_chain/src/beacon_chain.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,6 @@ pub const FORK_CHOICE_DB_KEY: Hash256 = Hash256::ZERO;
144144
/// Defines how old a block can be before it's no longer a candidate for the early attester cache.
145145
const EARLY_ATTESTER_CACHE_HISTORIC_SLOTS: u64 = 4;
146146

147-
/// Defines a distance between the head block slot and the current slot.
148-
///
149-
/// If the head block is older than this value, don't bother preparing beacon proposers.
150-
const PREPARE_PROPOSER_HISTORIC_EPOCHS: u64 = 4;
151-
152147
/// If the head is more than `MAX_PER_SLOT_FORK_CHOICE_DISTANCE` slots behind the wall-clock slot, DO NOT
153148
/// run the per-slot tasks (primarily fork choice).
154149
///
@@ -4848,7 +4843,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
48484843
let proposer_index = if let Some(proposer) = cached_proposer {
48494844
proposer.index as u64
48504845
} else {
4851-
if head_epoch + 2 < proposal_epoch {
4846+
if head_epoch + self.config.sync_tolerance_epochs < proposal_epoch {
48524847
warn!(
48534848
self.log,
48544849
"Skipping proposer preparation";
@@ -6079,19 +6074,18 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
60796074
// Use a blocking task since blocking the core executor on the canonical head read lock can
60806075
// block the core tokio executor.
60816076
let chain = self.clone();
6077+
let tolerance_slots = self.config.sync_tolerance_epochs * T::EthSpec::slots_per_epoch();
60826078
let maybe_prep_data = self
60836079
.spawn_blocking_handle(
60846080
move || {
60856081
let cached_head = chain.canonical_head.cached_head();
60866082

60876083
// Don't bother with proposer prep if the head is more than
6088-
// `PREPARE_PROPOSER_HISTORIC_EPOCHS` prior to the current slot.
6084+
// `sync_tolerance_epochs` prior to the current slot.
60896085
//
60906086
// This prevents the routine from running during sync.
60916087
let head_slot = cached_head.head_slot();
6092-
if head_slot + T::EthSpec::slots_per_epoch() * PREPARE_PROPOSER_HISTORIC_EPOCHS
6093-
< current_slot
6094-
{
6088+
if head_slot + tolerance_slots < current_slot {
60956089
debug!(
60966090
chain.log,
60976091
"Head too old for proposer prep";

beacon_node/beacon_chain/src/chain_config.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ pub const DEFAULT_PREPARE_PAYLOAD_LOOKAHEAD_FACTOR: u32 = 3;
1616
/// Fraction of a slot lookahead for fork choice in the state advance timer (500ms on mainnet).
1717
pub const FORK_CHOICE_LOOKAHEAD_FACTOR: u32 = 24;
1818

19+
/// Default sync tolerance epochs.
20+
pub const DEFAULT_SYNC_TOLERANCE_EPOCHS: u64 = 2;
21+
1922
#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize)]
2023
pub struct ChainConfig {
2124
/// Maximum number of slots to skip when importing an attestation.
@@ -94,6 +97,9 @@ pub struct ChainConfig {
9497
/// The delay in milliseconds applied by the node between sending each blob or data column batch.
9598
/// This doesn't apply if the node is the block proposer.
9699
pub blob_publication_batch_interval: Duration,
100+
/// The max distance between the head block and the current slot at which Lighthouse will
101+
/// consider itself synced and still serve validator-related requests.
102+
pub sync_tolerance_epochs: u64,
97103
}
98104

99105
impl Default for ChainConfig {
@@ -129,6 +135,7 @@ impl Default for ChainConfig {
129135
enable_sampling: false,
130136
blob_publication_batches: 4,
131137
blob_publication_batch_interval: Duration::from_millis(300),
138+
sync_tolerance_epochs: DEFAULT_SYNC_TOLERANCE_EPOCHS,
132139
}
133140
}
134141
}

beacon_node/http_api/src/lib.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,6 @@ use warp_utils::{query::multi_key_query, reject::convert_rejection, uor::Unifyin
107107

108108
const API_PREFIX: &str = "eth";
109109

110-
/// If the node is within this many epochs from the head, we declare it to be synced regardless of
111-
/// the network sync state.
112-
///
113-
/// This helps prevent attacks where nodes can convince us that we're syncing some non-existent
114-
/// finalized head.
115-
const DEFAULT_SYNC_TOLERANCE_EPOCHS: u64 = 8;
116-
117110
/// A custom type which allows for both unsecured and TLS-enabled HTTP servers.
118111
type HttpServer = (SocketAddr, Pin<Box<dyn Future<Output = ()> + Send>>);
119112

@@ -157,7 +150,6 @@ pub struct Config {
157150
pub duplicate_block_status_code: StatusCode,
158151
pub enable_light_client_server: bool,
159152
pub target_peers: usize,
160-
pub sync_tolerance_epochs: Option<u64>,
161153
}
162154

163155
impl Default for Config {
@@ -174,7 +166,6 @@ impl Default for Config {
174166
duplicate_block_status_code: StatusCode::ACCEPTED,
175167
enable_light_client_server: true,
176168
target_peers: 100,
177-
sync_tolerance_epochs: None,
178169
}
179170
}
180171
}
@@ -475,10 +466,8 @@ pub fn serve<T: BeaconChainTypes>(
475466
)
476467
})?;
477468

478-
let sync_tolerance_epochs = config
479-
.sync_tolerance_epochs
480-
.unwrap_or(DEFAULT_SYNC_TOLERANCE_EPOCHS);
481-
let tolerance = sync_tolerance_epochs * T::EthSpec::slots_per_epoch();
469+
let tolerance =
470+
chain.config.sync_tolerance_epochs * T::EthSpec::slots_per_epoch();
482471

483472
if head_slot + tolerance >= current_slot {
484473
Ok(())

beacon_node/src/config.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use beacon_chain::graffiti_calculator::GraffitiOrigin;
88
use beacon_chain::TrustedSetup;
99
use clap::{parser::ValueSource, ArgMatches, Id};
1010
use clap_utils::flags::DISABLE_MALLOC_TUNING_FLAG;
11-
use clap_utils::{parse_flag, parse_optional, parse_required};
11+
use clap_utils::{parse_flag, parse_required};
1212
use client::{ClientConfig, ClientGenesis};
1313
use directory::{DEFAULT_BEACON_NODE_DIR, DEFAULT_NETWORK_DIR, DEFAULT_ROOT_DIR};
1414
use environment::RuntimeContext;
@@ -177,9 +177,6 @@ pub fn get_config<E: EthSpec>(
177177

178178
client_config.http_api.enable_light_client_server =
179179
!cli_args.get_flag("disable-light-client-server");
180-
181-
client_config.http_api.sync_tolerance_epochs =
182-
parse_optional(cli_args, "sync-tolerance-epochs")?;
183180
}
184181

185182
if cli_args.get_flag("light-client-server") {
@@ -194,6 +191,12 @@ pub fn get_config<E: EthSpec>(
194191
client_config.chain.enable_light_client_server = false;
195192
}
196193

194+
if let Some(sync_tolerance_epochs) =
195+
clap_utils::parse_optional(cli_args, "sync-tolerance-epochs")?
196+
{
197+
client_config.chain.sync_tolerance_epochs = sync_tolerance_epochs;
198+
}
199+
197200
if let Some(cache_size) = clap_utils::parse_optional(cli_args, "shuffling-cache-size")? {
198201
client_config.chain.shuffling_cache_size = cache_size;
199202
}

lighthouse/tests/beacon_node.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::exec::{CommandLineTestExec, CompletedTest};
22
use beacon_node::beacon_chain::chain_config::{
33
DisallowedReOrgOffsets, DEFAULT_RE_ORG_CUTOFF_DENOMINATOR, DEFAULT_RE_ORG_HEAD_THRESHOLD,
4-
DEFAULT_RE_ORG_MAX_EPOCHS_SINCE_FINALIZATION,
4+
DEFAULT_RE_ORG_MAX_EPOCHS_SINCE_FINALIZATION, DEFAULT_SYNC_TOLERANCE_EPOCHS,
55
};
66
use beacon_node::{
77
beacon_chain::graffiti_calculator::GraffitiOrigin,
@@ -2586,7 +2586,20 @@ fn sync_tolerance_epochs() {
25862586
.flag("sync-tolerance-epochs", Some("0"))
25872587
.run_with_zero_port()
25882588
.with_config(|config| {
2589-
assert_eq!(config.http_api.sync_tolerance_epochs, Some(0));
2589+
assert_eq!(config.chain.sync_tolerance_epochs, 0);
2590+
});
2591+
}
2592+
2593+
#[test]
2594+
fn sync_tolerance_epochs_default() {
2595+
CommandLineTest::new()
2596+
.flag("http", None)
2597+
.run_with_zero_port()
2598+
.with_config(|config| {
2599+
assert_eq!(
2600+
config.chain.sync_tolerance_epochs,
2601+
DEFAULT_SYNC_TOLERANCE_EPOCHS
2602+
);
25902603
});
25912604
}
25922605

0 commit comments

Comments
 (0)