Skip to content

Commit

Permalink
Added LightClientBootstrap V1 (sigp#3711)
Browse files Browse the repository at this point in the history
Partially addresses sigp#3651

Adds server-side support for light_client_bootstrap_v1 topic

This PR, creates each time a bootstrap without using cache, I do not know how necessary a cache is in this case as this topic is not supposed to be called frequently and IMHO we can just prevent abuse by using the limiter, but let me know what you think or if there is any caveat to this, or if it is necessary only for the sake of good practice.

Co-authored-by: Pawan Dhananjay <pawandhananjay@gmail.com>
  • Loading branch information
2 people authored and Woodpile37 committed Jan 6, 2024
1 parent 069a612 commit d066db7
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 12 deletions.
3 changes: 0 additions & 3 deletions beacon_node/beacon_chain/src/chain_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ pub struct ChainConfig {
pub count_unrealized_full: CountUnrealizedFull,
/// Optionally set timeout for calls to checkpoint sync endpoint.
pub checkpoint_sync_url_timeout: u64,
/// Whether to enable the light client server protocol.
pub enable_light_client_server: bool,
}

impl Default for ChainConfig {
Expand All @@ -70,7 +68,6 @@ impl Default for ChainConfig {
paranoid_block_proposal: false,
count_unrealized_full: CountUnrealizedFull::default(),
checkpoint_sync_url_timeout: 60,
enable_light_client_server: false,
}
}
}
4 changes: 4 additions & 0 deletions beacon_node/lighthouse_network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ pub struct Config {

/// Whether metrics are enabled.
pub metrics_enabled: bool,

/// Whether light client protocols should be enabled.
pub enable_light_client_server: bool,
}

impl Default for Config {
Expand Down Expand Up @@ -207,6 +210,7 @@ impl Default for Config {
shutdown_after_sync: false,
topics: Vec::new(),
metrics_enabled: false,
enable_light_client_server: false,
}
}
}
Expand Down
14 changes: 11 additions & 3 deletions beacon_node/lighthouse_network/src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ pub(crate) use protocol::{InboundRequest, RPCProtocol};
use crate::rpc::methods::MAX_REQUEST_BLOBS_SIDECARS;
pub use handler::SubstreamId;
pub use methods::{
BlocksByRangeRequest, BlocksByRootRequest, GoodbyeReason, MaxRequestBlocks,
RPCResponseErrorCode, ResponseTermination, StatusMessage, MAX_REQUEST_BLOCKS,
BlocksByRangeRequest, BlocksByRootRequest, GoodbyeReason, LightClientBootstrapRequest,
MaxRequestBlocks, RPCResponseErrorCode, ResponseTermination, StatusMessage, MAX_REQUEST_BLOCKS,
};
pub(crate) use outbound::OutboundRequest;
pub use protocol::{max_rpc_size, Protocol, RPCError};
Expand Down Expand Up @@ -109,18 +109,24 @@ pub struct RPC<Id: ReqId, TSpec: EthSpec> {
/// Queue of events to be processed.
events: Vec<NetworkBehaviourAction<RPCMessage<Id, TSpec>, RPCHandler<Id, TSpec>>>,
fork_context: Arc<ForkContext>,
enable_light_client_server: bool,
/// Slog logger for RPC behaviour.
log: slog::Logger,
}

impl<Id: ReqId, TSpec: EthSpec> RPC<Id, TSpec> {
pub fn new(fork_context: Arc<ForkContext>, log: slog::Logger) -> Self {
pub fn new(
fork_context: Arc<ForkContext>,
enable_light_client_server: bool,
log: slog::Logger,
) -> Self {
let log = log.new(o!("service" => "libp2p_rpc"));
let limiter = RPCRateLimiterBuilder::new()
.n_every(Protocol::MetaData, 2, Duration::from_secs(5))
.n_every(Protocol::Ping, 2, Duration::from_secs(10))
.n_every(Protocol::Status, 5, Duration::from_secs(15))
.one_every(Protocol::Goodbye, Duration::from_secs(10))
.one_every(Protocol::LightClientBootstrap, Duration::from_secs(10))
.n_every(
Protocol::BlocksByRange,
methods::MAX_REQUEST_BLOCKS,
Expand All @@ -139,6 +145,7 @@ impl<Id: ReqId, TSpec: EthSpec> RPC<Id, TSpec> {
limiter,
events: Vec::new(),
fork_context,
enable_light_client_server,
log,
}
}
Expand Down Expand Up @@ -195,6 +202,7 @@ where
RPCProtocol {
fork_context: self.fork_context.clone(),
max_rpc_size: max_rpc_size(&self.fork_context),
enable_light_client_server: self.enable_light_client_server,
phantom: PhantomData,
},
(),
Expand Down
21 changes: 20 additions & 1 deletion beacon_node/lighthouse_network/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,11 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
(gossipsub, update_gossipsub_scores)
};

let eth2_rpc = RPC::new(ctx.fork_context.clone(), log.clone());
let eth2_rpc = RPC::new(
ctx.fork_context.clone(),
config.enable_light_client_server,
log.clone(),
);

let discovery = {
// Build and start the discovery sub-behaviour
Expand Down Expand Up @@ -983,6 +987,9 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
Request::Status(_) => {
metrics::inc_counter_vec(&metrics::TOTAL_RPC_REQUESTS, &["status"])
}
Request::LightClientBootstrap(_) => {
metrics::inc_counter_vec(&metrics::TOTAL_RPC_REQUESTS, &["light_client_bootstrap"])
}
Request::BlocksByRange { .. } => {
metrics::inc_counter_vec(&metrics::TOTAL_RPC_REQUESTS, &["blocks_by_range"])
}
Expand Down Expand Up @@ -1255,6 +1262,14 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
);
Some(event)
}
InboundRequest::LightClientBootstrap(req) => {
let event = self.build_request(
peer_request_id,
peer_id,
Request::LightClientBootstrap(req),
);
Some(event)
}
}
}
Ok(RPCReceived::Response(id, resp)) => {
Expand Down Expand Up @@ -1285,6 +1300,10 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
RPCResponse::BlocksByRoot(resp) => {
self.build_response(id, peer_id, Response::BlocksByRoot(Some(resp)))
}
// Should never be reached
RPCResponse::LightClientBootstrap(bootstrap) => {
self.build_response(id, peer_id, Response::LightClientBootstrap(bootstrap))
}
}
}
Ok(RPCReceived::EndOfStream(id, termination)) => {
Expand Down
12 changes: 12 additions & 0 deletions beacon_node/network/src/router/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,18 @@ impl<T: BeaconChainTypes> Processor<T> {
))
}

/// Handle a `LightClientBootstrap` request from the peer.
pub fn on_lightclient_bootstrap(
&mut self,
peer_id: PeerId,
request_id: PeerRequestId,
request: LightClientBootstrapRequest,
) {
self.send_beacon_processor_work(BeaconWorkEvent::lightclient_bootstrap_request(
peer_id, request_id, request,
))
}

/// Handle a `BlocksByRange` request from the peer.
pub fn on_blocks_by_range_request(
&mut self,
Expand Down
6 changes: 3 additions & 3 deletions beacon_node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,9 +713,6 @@ pub fn get_config<E: EthSpec>(
client_config.chain.builder_fallback_disable_checks =
cli_args.is_present("builder-fallback-disable-checks");

// Light client server config.
client_config.chain.enable_light_client_server = cli_args.is_present("light-client-server");

Ok(client_config)
}

Expand Down Expand Up @@ -927,6 +924,9 @@ pub fn set_network_config(
config.discv5_config.table_filter = |_| true;
}

// Light client server config.
config.enable_light_client_server = cli_args.is_present("light-client-server");

Ok(())
}

Expand Down
4 changes: 2 additions & 2 deletions lighthouse/tests/beacon_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1587,13 +1587,13 @@ fn sync_eth1_chain_disable_deposit_contract_sync_flag() {
fn light_client_server_default() {
CommandLineTest::new()
.run_with_zero_port()
.with_config(|config| assert_eq!(config.chain.enable_light_client_server, false));
.with_config(|config| assert_eq!(config.network.enable_light_client_server, false));
}

#[test]
fn light_client_server_enabled() {
CommandLineTest::new()
.flag("light-client-server", None)
.run_with_zero_port()
.with_config(|config| assert_eq!(config.chain.enable_light_client_server, true));
.with_config(|config| assert_eq!(config.network.enable_light_client_server, true));
}

0 comments on commit d066db7

Please sign in to comment.