Skip to content

Commit

Permalink
Send back name and version to identify requests (#417)
Browse files Browse the repository at this point in the history
* Rename ClientConfig::system_* options to client_*

* Send back name and version to identify requests

* CHANGELOG PR link
  • Loading branch information
tomaka authored Apr 13, 2023
1 parent 7324fd8 commit f1c3bc9
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 22 deletions.
1 change: 1 addition & 0 deletions full-node/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ pub async fn run(cli_options: cli::CliOptionsRun) {
.into_iter(),
)
.collect(),
identify_agent_version: concat!(env!("CARGO_PKG_NAME"), " ", env!("CARGO_PKG_VERSION")).to_owned(),
noise_key,
tasks_executor: &mut |task| threads_pool.spawn_ok(task),
jaeger_service: jaeger_service.clone(),
Expand Down
11 changes: 10 additions & 1 deletion full-node/src/run/network_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ pub struct Config<'a> {
/// List of block chains to be connected to.
pub chains: Vec<ChainConfig>,

/// Value sent back for the agent version when receiving an identification request.
pub identify_agent_version: String,

/// Key used for the encryption layer.
/// This is a Noise static key, according to the Noise specification.
/// Signed using the actual libp2p key.
Expand Down Expand Up @@ -144,6 +147,9 @@ struct Inner {
/// Fields behind a mutex.
guarded: Mutex<Guarded>,

/// Value provided through [`Config::identify_agent_version`].
identify_agent_version: String,

/// Event to notify when the background task needs to be waken up.
///
/// Waking up this event guarantees a full loop of the background task. In other words,
Expand Down Expand Up @@ -274,6 +280,7 @@ impl NetworkService {
*network.noise_key().libp2p_public_ed25519_key(),
)
.into_peer_id(),
identify_agent_version: config.identify_agent_version,
wake_up_main_background_task: event_listener::Event::new(),
databases,
guarded: Mutex::new(Guarded {
Expand Down Expand Up @@ -896,7 +903,9 @@ async fn update_round(inner: &Arc<Inner>, event_senders: &mut [mpsc::Sender<Even
request_id,
} => {
log::debug!("identify-request; peer_id={}", peer_id);
guarded.network.respond_identify(request_id, "smoldot");
guarded
.network
.respond_identify(request_id, &inner.identify_agent_version);
}
service::Event::BlocksRequestIn {
peer_id,
Expand Down
4 changes: 2 additions & 2 deletions light-base/examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ fn main() {
tasks_spawner: Box::new(move |_name, task| {
async_std::task::spawn(task);
}),
system_name: env!("CARGO_PKG_NAME").into(),
system_version: env!("CARGO_PKG_VERSION").into(),
client_name: env!("CARGO_PKG_NAME").into(),
client_version: env!("CARGO_PKG_VERSION").into(),
});

// Ask the client to connect to a chain.
Expand Down
37 changes: 21 additions & 16 deletions light-base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ pub struct ClientConfig {
/// The first parameter is the name of the task, which can be useful for debugging purposes.
pub tasks_spawner: Box<dyn Fn(String, future::BoxFuture<'static, ()>) + Send + Sync>,

/// Value returned when a JSON-RPC client requests the name of the client. Reasonable value
/// is `env!("CARGO_PKG_NAME")`.
pub system_name: String,
/// Value returned when a JSON-RPC client requests the name of the client, or when a peer
/// performs an identification request. Reasonable value is `env!("CARGO_PKG_NAME")`.
pub client_name: String,

/// Value returned when a JSON-RPC client requests the version of the client. Reasonable value
/// is `env!("CARGO_PKG_VERSION")`.
pub system_version: String,
/// Value returned when a JSON-RPC client requests the version of the client, or when a peer
/// performs an identification request. Reasonable value is `env!("CARGO_PKG_VERSION")`.
pub client_version: String,
}

/// See [`Client::add_chain`].
Expand Down Expand Up @@ -178,13 +178,11 @@ pub struct Client<TPlat: platform::Platform, TChain = ()> {
// TODO: use SipHasher
chains_by_key: HashMap<ChainKey, RunningChain<TPlat>, fnv::FnvBuildHasher>,

/// Value to return when the `system_name` RPC is called. Should be set to the name of the
/// final executable.
system_name: String,
/// See [`ClientConfig::client_name`].
client_name: String,

/// Value to return when the `system_version` RPC is called. Should be set to the version of
/// the final executable.
system_version: String,
/// See [`ClientConfig::client_version`].
client_version: String,
}

struct PublicApiChain<TChain> {
Expand Down Expand Up @@ -322,8 +320,8 @@ impl<TPlat: platform::Platform, TChain> Client<TPlat, TChain> {
spawn_new_task: config.tasks_spawner.into(),
public_api_chains: slab::Slab::with_capacity(expected_chains),
chains_by_key: HashMap::with_capacity_and_hasher(expected_chains, Default::default()),
system_name: config.system_name,
system_version: config.system_version,
client_name: config.client_name,
client_version: config.client_version,
}
}

Expand Down Expand Up @@ -634,6 +632,10 @@ impl<TPlat: platform::Platform, TChain> Client<TPlat, TChain> {
// peer-to-peer network.
let network_noise_key = connection::NoiseKey::new(&rand::random());

// Version of the client when requested through the networking.
let network_identify_agent_version =
format!("{} {}", self.client_name, self.client_version);

// Spawn a background task that initializes the services of the new chain and
// yields a `ChainServices`.
let running_chain_init_future: future::RemoteHandle<ChainServices<TPlat>> = {
Expand Down Expand Up @@ -675,6 +677,7 @@ impl<TPlat: platform::Platform, TChain> Client<TPlat, TChain> {
.scale_encoding_vec(chain_spec.block_number_bytes().into()),
chain_spec,
relay_chain.as_ref().map(|(r, _)| r),
network_identify_agent_version,
network_noise_key,
)
.await;
Expand Down Expand Up @@ -825,8 +828,8 @@ impl<TPlat: platform::Platform, TChain> Client<TPlat, TChain> {
});

let spawn_new_task = self.spawn_new_task.clone();
let system_name = self.system_name.clone();
let system_version = self.system_version.clone();
let system_name = self.client_name.clone();
let system_version = self.client_version.clone();

let init_future = async move {
// Wait for the chain to finish initializing before starting the JSON-RPC service.
Expand Down Expand Up @@ -1001,6 +1004,7 @@ async fn start_services<TPlat: platform::Platform>(
genesis_block_scale_encoded_header: Vec<u8>,
chain_spec: chain_spec::ChainSpec,
relay_chain: Option<&ChainServices<TPlat>>,
network_identify_agent_version: String,
network_noise_key: connection::NoiseKey,
) -> ChainServices<TPlat> {
// Since `network_noise_key` is moved out below, use it to build the network identity ahead
Expand All @@ -1016,6 +1020,7 @@ async fn start_services<TPlat: platform::Platform>(
move |name, fut| spawn_new_task(name, fut)
}),
num_events_receivers: 1, // Configures the length of `network_event_receivers`
identify_agent_version: network_identify_agent_version,
noise_key: network_noise_key,
chains: vec![network_service::ConfigChain {
log_name: log_name.clone(),
Expand Down
11 changes: 10 additions & 1 deletion light-base/src/network_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ pub struct Config {
/// Closure that spawns background tasks.
pub tasks_executor: Box<dyn FnMut(String, future::BoxFuture<'static, ()>) + Send>,

/// Value sent back for the agent version when receiving an identification request.
pub identify_agent_version: String,

/// Key to use for the encryption layer of all the connections. Gives the node its identity.
pub noise_key: connection::NoiseKey,

Expand Down Expand Up @@ -126,6 +129,9 @@ struct Shared<TPlat: Platform> {
/// Fields protected by a mutex.
guarded: Mutex<SharedGuarded<TPlat>>,

/// Value provided through [`Config::identify_agent_version`].
identify_agent_version: String,

/// Names of the various chains the network service connects to. Used only for logging
/// purposes.
log_chain_names: Vec<String>,
Expand Down Expand Up @@ -272,6 +278,7 @@ impl<TPlat: Platform> NetworkService<TPlat> {
Default::default(),
),
}),
identify_agent_version: config.identify_agent_version,
log_chain_names,
wake_up_main_background_task: event_listener::Event::new(),
});
Expand Down Expand Up @@ -1222,7 +1229,9 @@ async fn update_round<TPlat: Platform>(
"Connection({}) => IdentifyRequest",
peer_id,
);
guarded.network.respond_identify(request_id, "smoldot");
guarded
.network
.respond_identify(request_id, &shared.identify_agent_version);
}
service::Event::BlocksRequestIn { .. } => unreachable!(),
service::Event::RequestInCancel { .. } => {
Expand Down
4 changes: 4 additions & 0 deletions wasm-node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Changed

- When receiving an identify request through the libp2p protocol, smoldot now sends back `smoldot-light-wasm vX.X.X` (with proper version numbers) as its agent name and version, instead of previously just `smoldot`. ([#417](https://github.com/smol-dot/smoldot/pull/417))

## 1.0.2 - 2023-04-12

### Changed
Expand Down
4 changes: 2 additions & 2 deletions wasm-node/rust/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ pub(crate) fn init<TPlat: smoldot_light::platform::Platform, TChain>(
tasks_spawner: Box::new(move |name, task| {
new_task_tx.unbounded_send((name, task)).unwrap()
}),
system_name: env!("CARGO_PKG_NAME").into(),
system_version: env!("CARGO_PKG_VERSION").into(),
client_name: env!("CARGO_PKG_NAME").into(),
client_version: env!("CARGO_PKG_VERSION").into(),
});

Client {
Expand Down

0 comments on commit f1c3bc9

Please sign in to comment.