Skip to content

Commit 3251bcf

Browse files
committed
Remove RecvInfo and SendInfo and keep only phase instead
1 parent 26a501c commit 3251bcf

File tree

7 files changed

+33
-67
lines changed

7 files changed

+33
-67
lines changed

docs/src/examples/channel.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
The `Channel` trait in our MPC engine provides a flexible and extensible abstraction for message-passing between parties. It allows communication to be implemented in various ways, enabling users to choose between platform-specific implementations. Polytune is deliberately communication-agnostic, while remaining quite flexible, offering the following features:
44

55
- **Customizable Transport**: Implement the `Channel` trait using any transport mechanism — HTTP, WebSockets, in-memory queues, or custom networking protocols.
6-
- **Serialization-Aware**: The trait ensures that messages can be efficiently serialized and chunked when necessary, making it ideal for large payloads.
6+
- **Serialization-Aware**: The trait ensures that messages can be efficiently serialized.
77

88
We provide example implementations for:
99

@@ -37,13 +37,13 @@ trait Channel {
3737
&self,
3838
p: usize,
3939
msg: Vec<u8>,
40-
info: SendInfo,
40+
phase: &str,
4141
) -> Result<(), Self::SendError>;
4242

4343
async fn recv_bytes_from(
4444
&self,
4545
p: usize,
46-
info: RecvInfo,
46+
phase: &str,
4747
) -> Result<Vec<u8>, Self::RecvError>;
4848
}
4949
```
@@ -53,7 +53,7 @@ trait Channel {
5353
1. **Channel Parameters**:
5454

5555
- `p`: Index of the target party for send/receive
56-
- `info`: Various information useful for logging
56+
- `phase`: Phase of the protocol where the message is sent
5757
- `msg`: Message sent to the target party (only in `send_bytes_to`)
5858

5959
2. **Connection Management**:

examples/api-integration/src/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use axum::{
88
};
99
use clap::Parser;
1010
use polytune::{
11-
channel::{Channel, RecvInfo, SendInfo},
11+
channel::Channel,
1212
garble_lang::{compile_with_constants, literal::Literal},
1313
mpc,
1414
};
@@ -373,13 +373,12 @@ impl Channel for HttpChannel {
373373
&self,
374374
p: usize,
375375
msg: Vec<u8>,
376-
info: SendInfo,
376+
phase: &str,
377377
) -> Result<(), Self::SendError> {
378378
let simulated_delay_in_ms = 300;
379379
let client = reqwest::Client::new();
380380
let url = format!("{}msg/{}", self.urls[p], self.party);
381381
let mb = msg.len() as f64 / 1024.0 / 1024.0;
382-
let phase = info.phase();
383382
info!("Sending msg {phase} to party {p} ({mb:.2}MB)...");
384383
loop {
385384
sleep(Duration::from_millis(simulated_delay_in_ms)).await;
@@ -402,7 +401,7 @@ impl Channel for HttpChannel {
402401
}
403402
}
404403

405-
async fn recv_bytes_from(&self, p: usize, _info: RecvInfo) -> Result<Vec<u8>, Self::RecvError> {
404+
async fn recv_bytes_from(&self, p: usize, _phase: &str) -> Result<Vec<u8>, Self::RecvError> {
406405
let mut r = self.recv[p].lock().await;
407406
timeout(Duration::from_secs(30 * 60), r.recv())
408407
.await

examples/http-multi-server-channels/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use axum::{
77
};
88
use clap::Parser;
99
use polytune::{
10-
channel::{Channel, RecvInfo, SendInfo},
10+
channel::Channel,
1111
garble_lang::compile,
1212
mpc,
1313
};
@@ -112,7 +112,7 @@ impl Channel for HttpChannel {
112112
&self,
113113
p: usize,
114114
msg: Vec<u8>,
115-
_info: SendInfo,
115+
_phase: &str,
116116
) -> Result<(), Self::SendError> {
117117
let client = reqwest::Client::new();
118118
let url = format!("{}msg/{}", self.urls[p], self.party);
@@ -133,7 +133,7 @@ impl Channel for HttpChannel {
133133
}
134134
}
135135

136-
async fn recv_bytes_from(&self, p: usize, _info: RecvInfo) -> Result<Vec<u8>, Self::RecvError> {
136+
async fn recv_bytes_from(&self, p: usize, _phase: &str) -> Result<Vec<u8>, Self::RecvError> {
137137
let mut r = self.recv[p].lock().await;
138138
Ok(timeout(Duration::from_secs(1), r.recv())
139139
.await

examples/http-single-server-channels/src/http_channel.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::time::Duration;
22

3-
use polytune::channel::{Channel, RecvInfo, SendInfo};
3+
use polytune::channel::Channel;
44
use reqwest::StatusCode;
55
use tokio::time::sleep;
66

@@ -62,7 +62,7 @@ impl Channel for PollingHttpChannel {
6262
&self,
6363
p: usize,
6464
msg: Vec<u8>,
65-
_info: SendInfo,
65+
_phase: &str,
6666
) -> Result<(), HttpChannelError> {
6767
let url = format!("{}/send/{}/{}/{}", self.url, self.session, self.party, p);
6868
let resp: reqwest::Response = self.client.post(url).body(msg).send().await?;
@@ -76,7 +76,7 @@ impl Channel for PollingHttpChannel {
7676
async fn recv_bytes_from(
7777
&self,
7878
p: usize,
79-
_info: RecvInfo,
79+
_phase: &str,
8080
) -> Result<Vec<u8>, HttpChannelError> {
8181
let url = format!("{}/recv/{}/{}/{}", self.url, self.session, p, self.party);
8282
let mut attempts = 0;

examples/sql-integration/src/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use axum::{
88
};
99
use clap::Parser;
1010
use polytune::{
11-
channel::{Channel, RecvInfo, SendInfo},
11+
channel::Channel,
1212
garble_lang::{
1313
ast::{Type, Variant},
1414
compile_with_constants,
@@ -689,13 +689,12 @@ impl Channel for HttpChannel {
689689
&self,
690690
p: usize,
691691
msg: Vec<u8>,
692-
info: SendInfo,
692+
phase: &str,
693693
) -> Result<(), Self::SendError> {
694694
let simulated_delay_in_ms = 300;
695695
let client = reqwest::Client::new();
696696
let url = format!("{}msg/{}", self.urls[p], self.party);
697697
let mb = msg.len() as f64 / 1024.0 / 1024.0;
698-
let phase = info.phase();
699698
info!("Sending msg {phase} to party {p} ({mb:.2}MB)...");
700699
loop {
701700
sleep(Duration::from_millis(simulated_delay_in_ms)).await;
@@ -718,7 +717,7 @@ impl Channel for HttpChannel {
718717
}
719718
}
720719

721-
async fn recv_bytes_from(&self, p: usize, _info: RecvInfo) -> Result<Vec<u8>, Self::RecvError> {
720+
async fn recv_bytes_from(&self, p: usize, _phase: &str) -> Result<Vec<u8>, Self::RecvError> {
722721
let mut r = self.recv[p].lock().await;
723722
timeout(Duration::from_secs(30 * 60), r.recv())
724723
.await

examples/wasm-http-channels/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::str::FromStr;
22

33
use gloo_timers::future::TimeoutFuture;
44
use polytune::{
5-
channel::{Channel, RecvInfo, SendInfo},
5+
channel::Channel,
66
garble_lang::{compile, literal::Literal, token::SignedNumType},
77
mpc,
88
};
@@ -64,7 +64,7 @@ impl Channel for HttpChannel {
6464
&self,
6565
p: usize,
6666
msg: Vec<u8>,
67-
_info: SendInfo,
67+
_phase: &str,
6868
) -> Result<(), Self::SendError> {
6969
let client = reqwest::Client::new();
7070
let url = format!("{}send/{}/{}", self.url, self.party, p);
@@ -83,7 +83,7 @@ impl Channel for HttpChannel {
8383
return Err(format!("Could not reach {url}"));
8484
}
8585

86-
async fn recv_bytes_from(&self, p: usize, _info: RecvInfo) -> Result<Vec<u8>, Self::RecvError> {
86+
async fn recv_bytes_from(&self, p: usize, _phase: &str) -> Result<Vec<u8>, Self::RecvError> {
8787
let client = reqwest::Client::new();
8888
let url = format!("{}recv/{}/{}", self.url, self.party, p);
8989
for _ in 0..50 {

src/channel.rs

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -57,32 +57,6 @@ pub enum ErrorKind {
5757
InvalidLength,
5858
}
5959

60-
/// Information about a sent message that can be useful for logging.
61-
#[derive(Debug, Clone)]
62-
pub struct SendInfo {
63-
phase: String,
64-
}
65-
66-
impl SendInfo {
67-
/// The name of the protocol phase that sent the message.
68-
pub fn phase(&self) -> &str {
69-
&self.phase
70-
}
71-
}
72-
73-
/// Information about a received message that can be useful for logging.
74-
#[derive(Debug, Clone)]
75-
pub struct RecvInfo {
76-
phase: String,
77-
}
78-
79-
impl RecvInfo {
80-
/// The name of the protocol phase that sent the message.
81-
pub fn phase(&self) -> &str {
82-
&self.phase
83-
}
84-
}
85-
8660
/// A communication channel used to send/receive messages to/from another party.
8761
///
8862
/// This trait defines the core interface for message transport in the protocol.
@@ -102,16 +76,16 @@ pub trait Channel {
10276
async fn send_bytes_to(
10377
&self,
10478
party: usize,
105-
chunk: Vec<u8>,
106-
info: SendInfo,
79+
data: Vec<u8>,
80+
phase: &str,
10781
) -> Result<(), Self::SendError>;
10882

10983
/// Awaits a response from the party with the given index (must be between `0..participants`).
11084
#[allow(async_fn_in_trait)]
11185
async fn recv_bytes_from(
11286
&self,
11387
party: usize,
114-
info: RecvInfo,
88+
phase: &str,
11589
) -> Result<Vec<u8>, Self::RecvError>;
11690
}
11791

@@ -126,11 +100,8 @@ pub(crate) async fn send_to<S: Serialize + std::fmt::Debug>(
126100
phase: format!("sending {phase}"),
127101
reason: ErrorKind::SerdeError(format!("{e:?}")),
128102
})?;
129-
let info = SendInfo {
130-
phase: phase.to_string(),
131-
};
132103
channel
133-
.send_bytes_to(party, data, info)
104+
.send_bytes_to(party, data, phase)
134105
.await
135106
.map_err(|e| Error {
136107
phase: phase.to_string(),
@@ -145,11 +116,8 @@ pub(crate) async fn recv_from<T: DeserializeOwned + std::fmt::Debug>(
145116
party: usize,
146117
phase: &str,
147118
) -> Result<Vec<T>, Error> {
148-
let info = RecvInfo {
149-
phase: phase.to_string(),
150-
};
151119
let data = channel
152-
.recv_bytes_from(party, info)
120+
.recv_bytes_from(party, phase)
153121
.await
154122
.map_err(|e| Error {
155123
phase: phase.to_string(),
@@ -370,7 +338,7 @@ impl Channel for SimpleChannel {
370338
&self,
371339
p: usize,
372340
msg: Vec<u8>,
373-
info: SendInfo,
341+
phase: &str,
374342
) -> Result<(), tokio::sync::mpsc::error::SendError<Vec<u8>>> {
375343
self.bytes_sent
376344
.fetch_add(msg.len() as u64, Ordering::Relaxed);
@@ -383,19 +351,19 @@ impl Channel for SimpleChannel {
383351
.await
384352
}
385353

386-
#[tracing::instrument(level = Level::TRACE, skip(self), fields(info = ?_info))]
387-
async fn recv_bytes_from(&self, p: usize, _info: RecvInfo) -> Result<Vec<u8>, AsyncRecvError> {
354+
#[tracing::instrument(level = Level::TRACE, skip(self), fields(phase = ?_phase))]
355+
async fn recv_bytes_from(&self, p: usize, _phase: &str) -> Result<Vec<u8>, AsyncRecvError> {
388356
let mut r = self.r[p]
389357
.as_ref()
390358
.unwrap_or_else(|| panic!("No receiver for party {p}"))
391359
.lock()
392360
.await;
393-
let chunk = r.recv();
394-
match tokio::time::timeout(std::time::Duration::from_secs(10 * 60), chunk).await {
395-
Ok(Some(chunk)) => {
396-
let mb = chunk.len() as f64 / 1024.0 / 1024.0;
397-
trace!(size = mb, "Received chunk");
398-
Ok(chunk)
361+
let data = r.recv();
362+
match tokio::time::timeout(std::time::Duration::from_secs(10 * 60), data).await {
363+
Ok(Some(data)) => {
364+
let mb = data.len() as f64 / 1024.0 / 1024.0;
365+
trace!(size = mb, "Received data");
366+
Ok(data)
399367
}
400368
Ok(None) => Err(AsyncRecvError::Closed),
401369
Err(_) => Err(AsyncRecvError::TimeoutElapsed),

0 commit comments

Comments
 (0)