Skip to content

Commit b1d3c5b

Browse files
committed
kad full support + migrate from gossipsub
1 parent 44018a4 commit b1d3c5b

File tree

7 files changed

+99
-323
lines changed

7 files changed

+99
-323
lines changed

Cargo.lock

Lines changed: 4 additions & 106 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 & 2 deletions
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
@@ -29,7 +29,6 @@ hex = "0.4.3"
2929
sha256 = "1.5.0"
3030

3131
# Utils
32-
bytes = "1.10.1"
3332
chrono = "0.4.40"
3433
clap = { version = "4.5.28", features = ["derive"] }
3534
clap_derive = "4.5.32"

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/comms/message.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::fmt::Display;
22

3-
use bytes::Bytes;
43
use libp2p::identity::Keypair;
54
use serde::{Deserialize, Serialize};
65

@@ -39,12 +38,6 @@ impl From<String> for Message {
3938
}
4039
}
4140

42-
impl From<Message> for Bytes {
43-
fn from(val: Message) -> Self {
44-
Bytes::copy_from_slice(val.to_string().as_bytes())
45-
}
46-
}
47-
4841
impl Display for Message {
4942
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
5043
match self {

src/main.rs

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@ use std::{error::Error, net::Ipv4Addr, time::Duration};
33
use clap::Parser as _;
44
use libp2p::{
55
Multiaddr, PeerId,
6-
gossipsub::{self, AllowAllSubscriptionFilter, Config, IdentityTransform, MessageAuthenticity},
7-
kad::{self, store::MemoryStore},
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]
@@ -39,46 +36,37 @@ async fn main() -> Result<(), Box<dyn Error>> {
3936
let store: MemoryStore = MemoryStore::new(peer_id);
4037
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-
let bootstrap_peer_id: Option<PeerId> = if let Some(bootstrap_peer_id) = args.bootstrap_peer_id
57+
let _bootstrap_peer_id: Option<PeerId> = if let Some(bootstrap_peer_id) = args.bootstrap_peer_id
6358
{
6459
Some(bootstrap_peer_id.parse()?)
6560
} else {
6661
None
6762
};
6863

69-
let bootstrap_peer_mutli_address: Option<Multiaddr> =
64+
let _bootstrap_peer_mutli_address: Option<Multiaddr> =
7065
if let Some(peer_mutli_address) = args.peer_mutli_address {
7166
Some(peer_mutli_address.parse()?)
7267
} else {
7368
None
7469
};
7570

76-
event_runner(
77-
swarm,
78-
args.role,
79-
bootstrap_peer_mutli_address,
80-
bootstrap_peer_id,
81-
Topic(topic.to_string()),
82-
)
83-
.await
71+
event_runner(swarm).await
8472
}

src/network/behaviour.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +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,
8-
}
9-
10-
pub struct PeerDht {
11-
pub kademlia: kad::PeerRecord,
7+
pub mdns: mdns::tokio::Behaviour,
128
}

0 commit comments

Comments
 (0)