Skip to content

Commit 54b4150

Browse files
authored
Add test flag to override SYNC_TOLERANCE_EPOCHS for range sync testing (#7030)
Related to #6880, an issue that's usually observed on local devnets with small number of nodes. When testing range sync, I usually shutdown a node for some period of time and restart it again. However, if it's within `SYNC_TOLERANCE_EPOCHS` (8), Lighthouse would consider the node as synced, and if it may attempt to produce a block if requested by a validator - on a local devnet, nodes frequently produce blocks - when this happens, the node ends up producing a block that would revert finality and would get disconnected from peers immediately. ### Usage Run Lighthouse BN with this flag to override: ``` --sync-tolerance--epoch 0 ```
1 parent 454c7d0 commit 54b4150

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

beacon_node/http_api/src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ const API_PREFIX: &str = "eth";
112112
///
113113
/// This helps prevent attacks where nodes can convince us that we're syncing some non-existent
114114
/// finalized head.
115-
const SYNC_TOLERANCE_EPOCHS: u64 = 8;
115+
const DEFAULT_SYNC_TOLERANCE_EPOCHS: u64 = 8;
116116

117117
/// A custom type which allows for both unsecured and TLS-enabled HTTP servers.
118118
type HttpServer = (SocketAddr, Pin<Box<dyn Future<Output = ()> + Send>>);
@@ -156,6 +156,7 @@ pub struct Config {
156156
#[serde(with = "eth2::types::serde_status_code")]
157157
pub duplicate_block_status_code: StatusCode,
158158
pub target_peers: usize,
159+
pub sync_tolerance_epochs: Option<u64>,
159160
}
160161

161162
impl Default for Config {
@@ -171,6 +172,7 @@ impl Default for Config {
171172
enable_beacon_processor: true,
172173
duplicate_block_status_code: StatusCode::ACCEPTED,
173174
target_peers: 100,
175+
sync_tolerance_epochs: None,
174176
}
175177
}
176178
}
@@ -459,7 +461,10 @@ pub fn serve<T: BeaconChainTypes>(
459461
)
460462
})?;
461463

462-
let tolerance = SYNC_TOLERANCE_EPOCHS * T::EthSpec::slots_per_epoch();
464+
let sync_tolerance_epochs = config
465+
.sync_tolerance_epochs
466+
.unwrap_or(DEFAULT_SYNC_TOLERANCE_EPOCHS);
467+
let tolerance = sync_tolerance_epochs * T::EthSpec::slots_per_epoch();
463468

464469
if head_slot + tolerance >= current_slot {
465470
Ok(())

beacon_node/src/cli.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,6 +1509,19 @@ pub fn cli_app() -> Command {
15091509
.help_heading(FLAG_HEADER)
15101510
.display_order(0)
15111511
)
1512+
.arg(
1513+
Arg::new("sync-tolerance-epochs")
1514+
.long("sync-tolerance-epochs")
1515+
.help("Overrides the default SYNC_TOLERANCE_EPOCHS. This flag is not intended \
1516+
for production and MUST only be used in TESTING only. This is primarily used \
1517+
for testing range sync, to prevent the node from producing a block before the \
1518+
node is synced with the network which may result in the node getting \
1519+
disconnected from peers immediately.")
1520+
.hide(true)
1521+
.requires("enable_http")
1522+
.action(ArgAction::Set)
1523+
.display_order(0)
1524+
)
15121525
.arg(
15131526
Arg::new("gui")
15141527
.long("gui")

beacon_node/src/config.rs

Lines changed: 4 additions & 1 deletion
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_required};
11+
use clap_utils::{parse_flag, parse_optional, parse_required};
1212
use client::{ClientConfig, ClientGenesis};
1313
use directory::{DEFAULT_BEACON_NODE_DIR, DEFAULT_NETWORK_DIR, DEFAULT_ROOT_DIR};
1414
use environment::RuntimeContext;
@@ -174,6 +174,9 @@ pub fn get_config<E: EthSpec>(
174174

175175
client_config.http_api.duplicate_block_status_code =
176176
parse_required(cli_args, "http-duplicate-block-status")?;
177+
178+
client_config.http_api.sync_tolerance_epochs =
179+
parse_optional(cli_args, "sync-tolerance-epochs")?;
177180
}
178181

179182
if cli_args.get_flag("light-client-server") {

lighthouse/tests/beacon_node.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2543,6 +2543,17 @@ fn light_client_http_server_disabled() {
25432543
});
25442544
}
25452545

2546+
#[test]
2547+
fn sync_tolerance_epochs() {
2548+
CommandLineTest::new()
2549+
.flag("http", None)
2550+
.flag("sync-tolerance-epochs", Some("0"))
2551+
.run_with_zero_port()
2552+
.with_config(|config| {
2553+
assert_eq!(config.http_api.sync_tolerance_epochs, Some(0));
2554+
});
2555+
}
2556+
25462557
#[test]
25472558
fn gui_flag() {
25482559
CommandLineTest::new()

scripts/local_testnet/network_params_das.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ participants:
44
cl_extra_params:
55
- --subscribe-all-data-column-subnets
66
- --subscribe-all-subnets
7+
# Note: useful for testing range sync (only produce block if node is in sync to prevent forking)
8+
- --sync-tolerance-epochs=0
79
- --target-peers=3
810
count: 2
911
- cl_type: lighthouse
1012
cl_image: lighthouse:local
1113
cl_extra_params:
14+
# Note: useful for testing range sync (only produce block if node is in sync to prevent forking)
15+
- --sync-tolerance-epochs=0
1216
- --target-peers=3
1317
count: 2
1418
network_params:

0 commit comments

Comments
 (0)