Skip to content

Commit a9b1143

Browse files
committed
Add test flag to override SYNC_TOLERANCE_EPOCHS for testing range sync.
1 parent 3fab6a2 commit a9b1143

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>>);
@@ -157,6 +157,7 @@ pub struct Config {
157157
pub duplicate_block_status_code: StatusCode,
158158
pub enable_light_client_server: bool,
159159
pub target_peers: usize,
160+
pub sync_tolerance_epochs: Option<u64>,
160161
}
161162

162163
impl Default for Config {
@@ -173,6 +174,7 @@ impl Default for Config {
173174
duplicate_block_status_code: StatusCode::ACCEPTED,
174175
enable_light_client_server: true,
175176
target_peers: 100,
177+
sync_tolerance_epochs: None,
176178
}
177179
}
178180
}
@@ -473,7 +475,10 @@ pub fn serve<T: BeaconChainTypes>(
473475
)
474476
})?;
475477

476-
let tolerance = SYNC_TOLERANCE_EPOCHS * T::EthSpec::slots_per_epoch();
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();
477482

478483
if head_slot + tolerance >= current_slot {
479484
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;
@@ -177,6 +177,9 @@ 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")?;
180183
}
181184

182185
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
@@ -2545,6 +2545,17 @@ fn light_client_http_server_disabled() {
25452545
});
25462546
}
25472547

2548+
#[test]
2549+
fn sync_tolerance_epochs() {
2550+
CommandLineTest::new()
2551+
.flag("http", None)
2552+
.flag("sync-tolerance-epochs", Some("0"))
2553+
.run_with_zero_port()
2554+
.with_config(|config| {
2555+
assert_eq!(config.http_api.sync_tolerance_epochs, Some(0));
2556+
});
2557+
}
2558+
25482559
#[test]
25492560
fn gui_flag() {
25502561
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)