Skip to content

Commit 4c67b32

Browse files
committed
feat(relay): use binary encoding for exchanging messages in P2P pubsub topics
1 parent 4321cf8 commit 4c67b32

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mithril-relay/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ future_dmq = ["dep:mithril-dmq"]
1515

1616
[dependencies]
1717
anyhow = { workspace = true }
18+
bincode = { version = "2.0.1" }
1819
clap = { workspace = true }
1920
config = { workspace = true }
2021
libp2p = { version = "0.56.0", features = [

mithril-relay/src/p2p/peer.rs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use libp2p::{
1111
};
1212
use mithril_common::{
1313
StdResult,
14+
crypto_helper::{TryFromBytes, TryToBytes},
1415
logging::LoggerExtensions,
1516
messages::{RegisterSignatureMessageHttp, RegisterSignerMessage},
1617
};
@@ -74,6 +75,25 @@ pub enum BroadcastMessage {
7475
RegisterSignatureDmq(DmqMessage),
7576
}
7677

78+
impl TryToBytes for BroadcastMessage {
79+
fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
80+
let bytes =
81+
bincode::serde::encode_to_vec(self, bincode::config::standard()).map_err(|e| e.into());
82+
83+
bytes
84+
}
85+
}
86+
87+
impl TryFromBytes for BroadcastMessage {
88+
fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
89+
let (res, _) =
90+
bincode::serde::decode_from_slice::<Self, _>(bytes, bincode::config::standard())
91+
.map_err(|e| anyhow!(e))?;
92+
93+
Ok(res)
94+
}
95+
}
96+
7797
/// A peer in the P2P network
7898
pub struct Peer {
7999
topics: HashMap<TopicName, gossipsub::IdentTopic>,
@@ -185,7 +205,7 @@ impl Peer {
185205
match event {
186206
PeerEvent::Behaviour {
187207
event: PeerBehaviourEvent::Gossipsub(gossipsub::Event::Message { message, .. }),
188-
} => Ok(Some(serde_json::from_slice(&message.data)?)),
208+
} => Ok(Some(BroadcastMessage::try_from_bytes(&message.data)?)),
189209
_ => Ok(None),
190210
}
191211
}
@@ -247,6 +267,17 @@ impl Peer {
247267
)
248268
}
249269

270+
/// Publish a signer registration on the P2P pubsub
271+
pub fn publish_signer_registration(
272+
&mut self,
273+
message: &RegisterSignerMessage,
274+
) -> StdResult<gossipsub::MessageId> {
275+
self.publish_broadcast_message(
276+
&BroadcastMessage::RegisterSignerHttp(message.to_owned()),
277+
mithril_p2p_topic::SIGNERS_HTTP,
278+
)
279+
}
280+
250281
/// Publish a broadcast message on the P2P pubsub
251282
pub fn publish_broadcast_message(
252283
&mut self,
@@ -261,9 +292,10 @@ impl Peer {
261292
format!("Can not publish broadcast message on invalid topic: {topic_name}")
262293
})?
263294
.to_owned();
264-
let data = serde_json::to_vec(message).with_context(|| {
295+
let data = message.to_bytes_vec().with_context(|| {
265296
format!("Can not publish broadcast message with invalid format on topic {topic_name}")
266297
})?;
298+
println!("Publishing message on topic {topic_name}: {:?}", data.len());
267299

268300
let message_id = self
269301
.swarm
@@ -283,17 +315,6 @@ impl Peer {
283315
Ok(message_id.to_owned())
284316
}
285317

286-
/// Publish a signer registration on the P2P pubsub
287-
pub fn publish_signer_registration(
288-
&mut self,
289-
message: &RegisterSignerMessage,
290-
) -> StdResult<gossipsub::MessageId> {
291-
self.publish_broadcast_message(
292-
&BroadcastMessage::RegisterSignerHttp(message.to_owned()),
293-
mithril_p2p_topic::SIGNERS_HTTP,
294-
)
295-
}
296-
297318
/// Connect to a remote peer
298319
pub fn dial(&mut self, addr: Multiaddr) -> StdResult<()> {
299320
debug!(self.logger, "Dialing to"; "address" => ?addr, "local_peer_id" => ?self.local_peer_id());

0 commit comments

Comments
 (0)