Skip to content

Commit

Permalink
Create base project for starknet-p2p-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wojciechos committed Aug 8, 2024
0 parents commit f036158
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "starknet-p2p-tests"
version = "0.1.0"
edition = "2021"

[dependencies]
libp2p = { version = "0.53.2", features = [
"autonat",
"dcutr",
"dns",
"gossipsub",
"identify",
"kad",
"macros",
"noise",
"ping",
"relay",
"request-response",
"serde",
"tcp",
"tokio",
"yamux",
] }

tokio = { version = "1.28.0", features = ["full", "test-util"] }
anyhow = "1.0"
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod protocol;
pub mod tools;
pub mod tests;
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}
1 change: 1 addition & 0 deletions src/protocol/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod starknet;
73 changes: 73 additions & 0 deletions src/protocol/starknet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use libp2p::{
core::{
muxing::StreamMuxerBox,
transport::OrTransport,
upgrade,
}, dns, futures, identity, noise, relay, swarm, tcp, yamux, Multiaddr, PeerId, Swarm, Transport
};
use libp2p::swarm::SwarmEvent;
use futures::StreamExt;

use std::{error::Error, time::Duration};

pub async fn initialize_p2p(addr: &str) -> Result<(), Box<dyn Error>> {
// Generate a keypair for the local peer
let local_key = identity::Keypair::generate_ed25519();
let local_peer_id = local_key.public().to_peer_id();

// Create a multiaddr for the local peer
let local_multiaddr: Multiaddr = format!("/ip4/127.0.0.1/tcp/7777/p2p/{}", local_peer_id).parse()?;
println!("Local peer address: {}", local_multiaddr);

// Initialize the relay transport
let (relay_transport, relay_behaviour) = relay::client::new(local_peer_id.clone());

// Create the transport
let transport = create_transport(&local_key, relay_transport);

// Create the Swarm
let mut swarm = Swarm::new(
transport,
relay_behaviour,
local_peer_id,
swarm::Config::with_tokio_executor()
.with_idle_connection_timeout(Duration::from_secs(3600 * 365))
);

let multiaddr: Multiaddr = addr.parse()?;

swarm.dial(multiaddr.clone())?;
println!("Dialing peer at address: {}", multiaddr);

// Handle events
loop {
match swarm.next().await {
Some(SwarmEvent::ConnectionEstablished { peer_id, .. }) => {
println!("Connected to peer: {}", peer_id);
return Ok(());
}
Some(SwarmEvent::OutgoingConnectionError { error, .. }) => {
return Err(format!("Failed to connect to peer: {:?}", error).into());
}
_ => {}
}
}
}

pub fn create_transport(
keypair: &libp2p::identity::Keypair,
relay_transport: libp2p::relay::client::Transport,
) -> libp2p::core::transport::Boxed<(PeerId, StreamMuxerBox)> {
let transport = tcp::tokio::Transport::new(tcp::Config::new());
let transport = OrTransport::new(transport, relay_transport);
let transport = dns::tokio::Transport::system(transport).unwrap();

let noise_config =
noise::Config::new(keypair).expect("Signing libp2p-noise static DH keypair failed.");

transport
.upgrade(upgrade::Version::V1)
.authenticate(noise_config)
.multiplex(yamux::Config::default())
.boxed()
}
1 change: 1 addition & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod tests_p2p;
10 changes: 10 additions & 0 deletions src/tests/tests_p2p.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

#[test]
fn test_connect_to_peer() {
let node = crate::tools::synthetic_node::SyntheticNode::new();
let peer_address = "/ip4/35.237.66.77/tcp/7777/p2p/12D3KooWR8ikUDiinyE5wgdYiqsdLfJRsBDYKGii6L3oyoipVEaV";

let result = node.connect(peer_address);

assert!(result.is_ok(), "Failed to connect: {:?}", result.err().unwrap());
}
1 change: 1 addition & 0 deletions src/tools/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod synthetic_node;
18 changes: 18 additions & 0 deletions src/tools/synthetic_node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::protocol::starknet;
use tokio::runtime::Runtime;
use std::error::Error;

pub struct SyntheticNode;

impl SyntheticNode {
pub fn new() -> Self {
SyntheticNode
}

pub fn connect(&self, address: &str) -> Result<(), Box<dyn Error>> {
let runtime = Runtime::new().expect("Failed to create Tokio runtime");
runtime.block_on(async {
starknet::initialize_p2p(address).await
})
}
}

0 comments on commit f036158

Please sign in to comment.