Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
Improve diagnostics message in P2P
Browse files Browse the repository at this point in the history
- In response message, some of the wrapping elements were missing.
- Added public node key to response, so that each node / response can
  be identified.
  • Loading branch information
tuommaki committed Feb 27, 2024
1 parent 3a9fd4a commit fc81e2d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
7 changes: 5 additions & 2 deletions crates/node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ impl workflow::TransactionStore for storage::Database {
}

async fn run(config: Arc<Config>) -> Result<()> {
let node_key = read_node_key(&config.node_key_file)?;

let database = Arc::new(Database::new(&config.db_url).await?);

let http_peer_list: Arc<tokio::sync::RwLock<HashMap<SocketAddr, Option<u16>>>> =
Expand Down Expand Up @@ -213,11 +215,13 @@ async fn run(config: Arc<Config>) -> Result<()> {
});
}

let public_node_key = PublicKey::from_secret_key(&node_key);
let p2p = Arc::new(
networking::P2P::new(
"gevulot-p2p-network",
config.p2p_listen_addr,
&config.p2p_psk_passphrase,
public_node_key,
Some(config.http_download_port),
config.p2p_advertised_listen_addr,
http_peer_list,
Expand Down Expand Up @@ -246,8 +250,6 @@ async fn run(config: Arc<Config>) -> Result<()> {
resource_manager.clone(),
);

let node_key = read_node_key(&config.node_key_file)?;

let workflow_engine = Arc::new(WorkflowEngine::new(database.clone()));
let download_url_prefix = format!(
"http://{}:{}",
Expand Down Expand Up @@ -337,6 +339,7 @@ async fn p2p_beacon(config: P2PBeaconConfig) -> Result<()> {
"gevulot-network",
config.p2p_listen_addr,
&config.p2p_psk_passphrase,
PublicKey::from_secret_key(&SecretKey::default()), // P2P beacons don't need the key atm.
None,
config.p2p_advertised_listen_addr,
http_peer_list,
Expand Down
30 changes: 19 additions & 11 deletions crates/node/src/networking/p2p/pea2pea.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::txvalidation::P2pSender;
use crate::txvalidation::TxEventSender;
use futures_util::Stream;
use libsecp256k1::PublicKey;
use std::{
collections::{BTreeSet, HashMap},
io,
Expand Down Expand Up @@ -42,6 +43,7 @@ pub struct P2P {
http_port: Option<u16>,
nat_listen_addr: Option<SocketAddr>,
psk: Vec<u8>,
public_node_key: PublicKey,

// Send Tx to the process loop.
tx_sender: TxEventSender<P2pSender>,
Expand All @@ -59,6 +61,7 @@ impl P2P {
name: &str,
listen_addr: SocketAddr,
psk_passphrase: &str,
public_node_key: PublicKey,
http_port: Option<u16>,
nat_listen_addr: Option<SocketAddr>,
peer_http_port_list: Arc<tokio::sync::RwLock<HashMap<SocketAddr, Option<u16>>>>,
Expand All @@ -83,6 +86,7 @@ impl P2P {
node,
noise_states: Default::default(),
psk: psk.to_vec(),
public_node_key,
peer_list: Default::default(),
peer_addr_mapping: Default::default(),
peer_http_port_list,
Expand Down Expand Up @@ -163,16 +167,19 @@ impl P2P {
source: SocketAddr,
req: protocol::DiagnosticsRequestKind,
) -> io::Result<()> {
let resp = protocol::DiagnosticsResponseV0::Version {
major: env!("CARGO_PKG_VERSION_MAJOR").parse::<u16>().unwrap(),
minor: env!("CARGO_PKG_VERSION_MINOR").parse::<u16>().unwrap(),
patch: env!("CARGO_PKG_VERSION_PATCH").parse::<u16>().unwrap(),
build: format!(
"{}: {}",
env!("VERGEN_BUILD_TIMESTAMP"),
env!("VERGEN_GIT_DESCRIBE")
),
};
let resp = protocol::Message::V0(protocol::MessageV0::DiagnosticsResponse(
self.public_node_key,
protocol::DiagnosticsResponseV0::Version {
major: env!("CARGO_PKG_VERSION_MAJOR").parse::<u16>().unwrap(),
minor: env!("CARGO_PKG_VERSION_MINOR").parse::<u16>().unwrap(),
patch: env!("CARGO_PKG_VERSION_PATCH").parse::<u16>().unwrap(),
build: format!(
"{}: {}",
env!("VERGEN_BUILD_TIMESTAMP"),
env!("VERGEN_GIT_DESCRIBE")
),
},
));

let bs =
Bytes::from(bincode::serialize(&resp).expect("diagnostics response serialization"));
Expand Down Expand Up @@ -392,7 +399,7 @@ impl Reading for P2P {
self.process_diagnostics_request(source, kind).await?;
}
// Nodes are expected to ignore the diagnostics response.
protocol::MessageV0::DiagnosticsResponse(_) => (),
protocol::MessageV0::DiagnosticsResponse(_, _) => (),
},
Err(err) => tracing::error!("failed to decode incoming transaction: {}", err),
}
Expand Down Expand Up @@ -464,6 +471,7 @@ mod tests {
name,
"127.0.0.1:0".parse().unwrap(),
"secret passphrase",
PublicKey::from_secret_key(&SecretKey::default()),
None,
None,
http_peer_list1,
Expand Down
3 changes: 2 additions & 1 deletion crates/node/src/networking/p2p/protocol.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{collections::BTreeSet, net::SocketAddr};

use gevulot_node::types;
use libsecp256k1::PublicKey;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Serialize)]
Expand All @@ -25,7 +26,7 @@ pub(crate) enum Message {
pub(crate) enum MessageV0 {
Transaction(types::Transaction<types::transaction::Validated>),
DiagnosticsRequest(DiagnosticsRequestKind),
DiagnosticsResponse(DiagnosticsResponseV0),
DiagnosticsResponse(PublicKey, DiagnosticsResponseV0),
}

#[derive(Clone, Debug, Deserialize, Serialize)]
Expand Down

0 comments on commit fc81e2d

Please sign in to comment.