Skip to content

Commit e490add

Browse files
authored
Merge pull request #11 from seun-ja/8-kademlia-data-propagation
8 kademlia data propagation
2 parents c1a8c81 + b1d3c5b commit e490add

File tree

6 files changed

+132
-255
lines changed

6 files changed

+132
-255
lines changed

Cargo.lock

Lines changed: 4 additions & 105 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ libp2p = { version = "0.55.0", features = [
1515
"quic",
1616
"request-response",
1717
"cbor",
18-
"gossipsub",
1918
"kad",
19+
"mdns",
2020
] }
2121

2222
# Serde

src/block/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub struct TransactionData {
7676

7777
#[typetag::serde(tag = "type")]
7878
pub trait Transactable: Debug + Send + Sync + DynClone + Display {
79-
// TODO: Implement the _submit method to submit the transaction to the network.
79+
// TODO: #9 Implement the _submit method to submit the transaction to the network.
8080
fn _submit(&self) -> Result<(), TransactionError>;
8181
fn sign(&self, keypair: &Keypair) -> Result<(), TransactionError>;
8282
}

src/main.rs

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,71 @@ use std::{error::Error, net::Ipv4Addr, time::Duration};
22

33
use clap::Parser as _;
44
use libp2p::{
5-
Multiaddr,
6-
gossipsub::{self, AllowAllSubscriptionFilter, Config, IdentityTransform, MessageAuthenticity},
7-
kad::{self, store::MemoryStore},
5+
Multiaddr, PeerId,
6+
kad::{self, Mode, store::MemoryStore},
7+
mdns,
88
multiaddr::Protocol,
99
noise, tcp, yamux,
1010
};
1111
use peer_node::{
1212
cli::Args,
13-
network::{
14-
behaviour::PeerBehavior,
15-
event::{Topic, event_runner},
16-
},
13+
network::{behaviour::PeerBehavior, event::event_runner},
1714
};
1815

1916
#[tokio::main]
2017
async fn main() -> Result<(), Box<dyn Error>> {
2118
peer_node::tracing::init("info");
22-
let args = Args::parse();
19+
let args: Args = Args::parse();
2320

24-
let ip_addr = Ipv4Addr::new(0, 0, 0, 0);
21+
let ip_addr: Ipv4Addr = Ipv4Addr::new(0, 0, 0, 0);
2522

26-
let peer_multi_addr = Multiaddr::from(ip_addr).with(Protocol::Tcp(0));
23+
let peer_multi_addr: Multiaddr = Multiaddr::from(ip_addr).with(Protocol::Tcp(0));
2724

2825
tracing::info!("A {}", args.role);
2926

30-
let mut swarm = libp2p::SwarmBuilder::with_new_identity()
27+
let mut swarm: libp2p::Swarm<PeerBehavior> = libp2p::SwarmBuilder::with_new_identity()
3128
.with_tokio()
3229
.with_tcp(
3330
tcp::Config::default(),
3431
noise::Config::new,
3532
yamux::Config::default,
3633
)?
3734
.with_behaviour(|keypair| {
38-
let peer_id = keypair.public().to_peer_id();
39-
let store = MemoryStore::new(peer_id);
40-
let kademlia = kad::Behaviour::new(peer_id, store);
35+
let peer_id: libp2p::PeerId = keypair.public().to_peer_id();
36+
let store: MemoryStore = MemoryStore::new(peer_id);
37+
let kademlia: kad::Behaviour<MemoryStore> = kad::Behaviour::new(peer_id, store);
4138

42-
let gossipsub: gossipsub::Behaviour<IdentityTransform, AllowAllSubscriptionFilter> =
43-
gossipsub::Behaviour::new(
44-
MessageAuthenticity::Signed(keypair.clone()),
45-
Config::default(),
46-
)
47-
.expect("Gossipsub initiation fails");
39+
let mdns = mdns::tokio::Behaviour::new(mdns::Config::default(), peer_id)
40+
.expect("mDNS initiation fails");
4841

49-
PeerBehavior {
50-
kademlia,
51-
gossipsub,
52-
}
42+
PeerBehavior { kademlia, mdns }
5343
})?
5444
.with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(u64::MAX)))
5545
.build();
5646

57-
let topic = gossipsub::IdentTopic::new("peer-network");
58-
swarm.behaviour_mut().gossipsub.subscribe(&topic)?;
47+
// Roles to transition
48+
match args.role {
49+
peer_node::cli::Role::BootstapNode => {
50+
swarm.behaviour_mut().kademlia.set_mode(Some(Mode::Server))
51+
}
52+
peer_node::cli::Role::Sender => swarm.behaviour_mut().kademlia.set_mode(Some(Mode::Client)),
53+
}
5954

6055
swarm.listen_on(peer_multi_addr)?;
6156

62-
event_runner(
63-
swarm,
64-
args.role,
65-
args.peer_mutli_address,
66-
args.bootstrap_peer_id,
67-
Topic(topic.to_string()),
68-
)
69-
.await
57+
let _bootstrap_peer_id: Option<PeerId> = if let Some(bootstrap_peer_id) = args.bootstrap_peer_id
58+
{
59+
Some(bootstrap_peer_id.parse()?)
60+
} else {
61+
None
62+
};
63+
64+
let _bootstrap_peer_mutli_address: Option<Multiaddr> =
65+
if let Some(peer_mutli_address) = args.peer_mutli_address {
66+
Some(peer_mutli_address.parse()?)
67+
} else {
68+
None
69+
};
70+
71+
event_runner(swarm).await
7072
}

src/network/behaviour.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use libp2p::swarm::NetworkBehaviour;
2-
use libp2p::{gossipsub, kad};
2+
use libp2p::{kad, mdns};
33

44
#[derive(NetworkBehaviour)]
55
pub struct PeerBehavior {
66
pub kademlia: kad::Behaviour<kad::store::MemoryStore>,
7-
pub gossipsub: gossipsub::Behaviour,
7+
pub mdns: mdns::tokio::Behaviour,
88
}

0 commit comments

Comments
 (0)