Skip to content

Commit

Permalink
Peer discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Jul 14, 2024
1 parent 7dd4014 commit 8a59e22
Show file tree
Hide file tree
Showing 12 changed files with 706 additions and 53 deletions.
77 changes: 75 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ unused_imports = "warn"
unused_import_braces = "deny"
unreachable_code = "deny"
unreachable_patterns = "deny"
dead_code = "deny"
dead_code = "warn"
deprecated = "deny"
deprecated_in_future = "deny"
trivial_casts = "deny"
Expand Down Expand Up @@ -137,3 +137,6 @@ libfuzzer-sys = "0.4"
wasm-bindgen = "0.2.92"
log = "0.4.22"
native-tls = "0.2.12"
dns-lookup = "2.0.4"
semver = "1.0.23"
env_logger = "0.11.3"
11 changes: 11 additions & 0 deletions crates/chia-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,14 @@ thiserror = { workspace = true }
sha2 = { workspace = true }
log = { workspace = true }
native-tls = { workspace = true }
dns-lookup = { workspace = true }
hex-literal = { workspace = true }
rand = { workspace = true }
semver = { workspace = true }
hex = { workspace = true }

[dev-dependencies]
chia-ssl = { path = "../chia-ssl" }
tokio = { workspace = true, features = ["full"] }
anyhow = { workspace = true }
env_logger = { workspace = true }
54 changes: 54 additions & 0 deletions crates/chia-client/examples/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use std::time::Duration;

use chia_client::{create_tls_connector, Client, ClientOptions, Event};
use chia_ssl::ChiaCertificate;
use tokio::time::sleep;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
env_logger::init();

log::info!("Generating certificate");
let cert = ChiaCertificate::generate()?;
let tls_connector = create_tls_connector(cert.cert_pem.as_bytes(), cert.key_pem.as_bytes())?;

log::info!("Creating client");
let (client, mut receiver) = Client::with_options(
tls_connector,
ClientOptions {
target_peers: 20,
..Default::default()
},
);

log::info!("Connecting to DNS introducers");
client.find_peers().await;

let client_clone = client.clone();

tokio::spawn(async move {
loop {
sleep(Duration::from_secs(10)).await;
let count = client_clone.peer_count().await;
log::info!("Currently connected to {} peers", count);
client.find_peers().await;
}
});

while let Some(event) = receiver.recv().await {
match event {
Event::Message(peer_id, message) => {
log::info!(
"Received message from peer {}: {:?}",
peer_id,
message.msg_type
);
}
Event::ConnectionClosed(peer_id) => {
log::info!("Peer {} disconnected", peer_id);
}
}
}

Ok(())
}
68 changes: 68 additions & 0 deletions crates/chia-client/examples/peer_discovery.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use std::time::Duration;

use chia_client::{create_tls_connector, Peer};
use chia_protocol::{Handshake, NodeType, ProtocolMessageTypes};
use chia_ssl::ChiaCertificate;
use chia_traits::Streamable;
use dns_lookup::lookup_host;
use tokio::time::timeout;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
env_logger::init();

let cert = ChiaCertificate::generate()?;
let tls = create_tls_connector(cert.cert_pem.as_bytes(), cert.key_pem.as_bytes())?;

for ip in lookup_host("dns-introducer.chia.net")? {
let Ok(response) =
timeout(Duration::from_secs(3), Peer::connect(ip, 8444, tls.clone())).await
else {
log::info!("{ip} exceeded connection timeout of 3 seconds");
continue;
};

let (peer, mut receiver) = response?;

peer.send(Handshake {
network_id: "mainnet".to_string(),
protocol_version: "0.0.37".to_string(),
software_version: "0.0.0".to_string(),
server_port: 0,
node_type: NodeType::Wallet,
capabilities: vec![
(1, "1".to_string()),
(2, "1".to_string()),
(3, "1".to_string()),
],
})
.await?;

let Ok(message) = timeout(Duration::from_secs(1), receiver.recv()).await else {
log::info!("{ip} exceeded timeout of 1 second");
continue;
};

let Some(message) = message else {
log::info!("{ip} did not send any messages");
continue;
};

if message.msg_type != ProtocolMessageTypes::Handshake {
log::info!("{ip} sent an unexpected message {:?}", message.msg_type);
continue;
}

let Ok(handshake) = Handshake::from_bytes(&message.data) else {
log::info!("{ip} sent an invalid handshake");
continue;
};

log::info!(
"{ip} handshake sent with protocol version {}",
handshake.protocol_version
);
}

Ok(())
}
Loading

0 comments on commit 8a59e22

Please sign in to comment.