Skip to content

Commit 5c2982e

Browse files
authored
Merge pull request #22 from cowlicks/replication
Changes for Replication
2 parents 48140bd + 85952b9 commit 5c2982e

File tree

6 files changed

+71
-19
lines changed

6 files changed

+71
-19
lines changed

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,15 @@ tracing = "0.1"
3636
pretty-hash = "0.4"
3737
futures-timer = "3"
3838
futures-lite = "1"
39-
hypercore = { version = "0.14.0", default-features = false }
4039
sha2 = "0.10"
4140
curve25519-dalek = "4"
4241
crypto_secretstream = "0.2"
4342

43+
[dependencies.hypercore]
44+
version = "0.14.0"
45+
default-features = false
46+
47+
4448
[dev-dependencies]
4549
async-std = { version = "1.12.0", features = ["attributes", "unstable"] }
4650
async-compat = "0.2.1"

src/channels.rs

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ use std::pin::Pin;
1313
use std::sync::atomic::{AtomicBool, Ordering};
1414
use std::sync::Arc;
1515
use std::task::Poll;
16+
use tracing::debug;
1617

1718
/// A protocol channel.
1819
///
1920
/// This is the handle that can be sent to other threads.
21+
#[derive(Clone)]
2022
pub struct Channel {
2123
inbound_rx: Option<Receiver<Message>>,
2224
direct_inbound_tx: Sender<Message>,
@@ -44,6 +46,25 @@ impl fmt::Debug for Channel {
4446
}
4547

4648
impl Channel {
49+
fn new(
50+
inbound_rx: Option<Receiver<Message>>,
51+
direct_inbound_tx: Sender<Message>,
52+
outbound_tx: Sender<Vec<ChannelMessage>>,
53+
discovery_key: DiscoveryKey,
54+
key: Key,
55+
local_id: usize,
56+
closed: Arc<AtomicBool>,
57+
) -> Self {
58+
Self {
59+
inbound_rx,
60+
direct_inbound_tx,
61+
outbound_tx,
62+
key,
63+
discovery_key,
64+
local_id,
65+
closed,
66+
}
67+
}
4768
/// Get the discovery key of this channel.
4869
pub fn discovery_key(&self) -> &[u8; 32] {
4970
&self.discovery_key
@@ -72,6 +93,7 @@ impl Channel {
7293
"Channel is closed",
7394
));
7495
}
96+
debug!("TX:\n{message:?}\n");
7597
let message = ChannelMessage::new(self.local_id as u64, message);
7698
self.outbound_tx
7799
.send(vec![message])
@@ -97,9 +119,13 @@ impl Channel {
97119
"Channel is closed",
98120
));
99121
}
122+
100123
let messages = messages
101124
.iter()
102-
.map(|message| ChannelMessage::new(self.local_id as u64, message.clone()))
125+
.map(|message| {
126+
debug!("TX:\n{message:?}\n");
127+
ChannelMessage::new(self.local_id as u64, message.clone())
128+
})
103129
.collect();
104130
self.outbound_tx
105131
.send(messages)
@@ -120,7 +146,7 @@ impl Channel {
120146
/// you will only want to send a LocalSignal message with this sender to make
121147
/// it clear what event came from the remote peer and what was local
122148
/// signaling.
123-
pub fn local_sender(&mut self) -> Sender<Message> {
149+
pub fn local_sender(&self) -> Sender<Message> {
124150
self.direct_inbound_tx.clone()
125151
}
126152

@@ -257,15 +283,16 @@ impl ChannelHandle {
257283
.expect("May not open channel that is not locally attached");
258284

259285
let (inbound_tx, inbound_rx) = async_channel::unbounded();
260-
let channel = Channel {
261-
inbound_rx: Some(inbound_rx),
262-
direct_inbound_tx: inbound_tx.clone(),
286+
let channel = Channel::new(
287+
Some(inbound_rx),
288+
inbound_tx.clone(),
263289
outbound_tx,
264-
discovery_key: self.discovery_key,
265-
key: local_state.key,
266-
local_id: local_state.local_id,
267-
closed: self.closed.clone(),
268-
};
290+
self.discovery_key,
291+
local_state.key,
292+
local_state.local_id,
293+
self.closed.clone(),
294+
);
295+
269296
self.inbound_tx = Some(inbound_tx);
270297
channel
271298
}
@@ -387,7 +414,7 @@ impl ChannelMap {
387414
}
388415
}
389416

390-
pub(crate) fn has_channel(&mut self, discovery_key: &[u8]) -> bool {
417+
pub(crate) fn has_channel(&self, discovery_key: &[u8]) -> bool {
391418
let hdkey = hex::encode(discovery_key);
392419
self.channels.contains_key(&hdkey)
393420
}

src/message.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ impl Message {
406406
}
407407

408408
/// Pre-encodes a message to state, returns length
409-
pub(crate) fn preencode(&mut self, state: &mut HypercoreState) -> Result<usize, EncodingError> {
409+
pub(crate) fn preencode(&self, state: &mut HypercoreState) -> Result<usize, EncodingError> {
410410
match self {
411411
Self::Open(ref message) => state.0.preencode(message)?,
412412
Self::Close(ref message) => state.0.preencode(message)?,
@@ -427,7 +427,7 @@ impl Message {
427427

428428
/// Encodes a message to a given buffer, using preencoded state, results size
429429
pub(crate) fn encode(
430-
&mut self,
430+
&self,
431431
state: &mut HypercoreState,
432432
buf: &mut [u8],
433433
) -> Result<usize, EncodingError> {

src/protocol.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,6 @@ impl fmt::Debug for State {
134134
}
135135

136136
/// A Protocol stream.
137-
///
138-
#[derive(Debug)]
139137
pub struct Protocol<IO> {
140138
write_state: WriteState,
141139
read_state: ReadState,
@@ -152,6 +150,26 @@ pub struct Protocol<IO> {
152150
queued_events: VecDeque<Event>,
153151
}
154152

153+
impl<IO> std::fmt::Debug for Protocol<IO> {
154+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
155+
f.debug_struct("Protocol")
156+
.field("write_state", &self.write_state)
157+
.field("read_state", &self.read_state)
158+
//.field("io", &self.io)
159+
.field("state", &self.state)
160+
.field("options", &self.options)
161+
.field("handshake", &self.handshake)
162+
.field("channels", &self.channels)
163+
.field("command_rx", &self.command_rx)
164+
.field("command_tx", &self.command_tx)
165+
.field("outbound_rx", &self.outbound_rx)
166+
.field("outbound_tx", &self.outbound_tx)
167+
.field("keepalive", &self.keepalive)
168+
.field("queued_events", &self.queued_events)
169+
.finish()
170+
}
171+
}
172+
155173
impl<IO> Protocol<IO>
156174
where
157175
IO: AsyncWrite + AsyncRead + Send + Unpin + 'static,
@@ -510,6 +528,7 @@ where
510528
}
511529
}
512530

531+
/// Open a Channel with the given key. Adding it to our channel map
513532
fn command_open(&mut self, key: Key) -> Result<()> {
514533
// Create a new channel.
515534
let channel_handle = self.channels.attach_local(key);

src/schema.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,14 +461,15 @@ impl CompactEncoding<Bitfield> for State {
461461
}
462462

463463
/// Range message. Type 8.
464+
/// Notifies Peer's that the Sender has a range of contiguous blocks.
464465
#[derive(Debug, Clone, PartialEq)]
465466
pub struct Range {
466467
/// If true, notifies that data has been cleared from this range.
467468
/// If false, notifies existing data range.
468469
pub drop: bool,
469-
/// Start index
470+
/// Range starts at this index
470471
pub start: u64,
471-
/// Length
472+
/// Length of the range
472473
pub length: u64,
473474
}
474475

tests/js_interop.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ async fn js_interop_rcrs_simple_server_writer() -> Result<()> {
110110
}
111111

112112
#[test(async_test)]
113-
#[cfg_attr(not(feature = "js_interop_tests"), ignore)]
113+
//#[cfg_attr(not(feature = "js_interop_tests"), ignore)]
114+
#[ignore] // FIXME this tests hangs sporadically
114115
async fn js_interop_rcrs_simple_client_writer() -> Result<()> {
115116
js_interop_rcrs_simple(false, 8108).await?;
116117
Ok(())

0 commit comments

Comments
 (0)