Skip to content

Commit 7b6bdf9

Browse files
authored
Merge pull request rust-bitcoin#122 from nyonson/less-feature-flags
Remove the futures flag
2 parents 1be2134 + ab6e129 commit 7b6bdf9

6 files changed

Lines changed: 30 additions & 46 deletions

File tree

protocol/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,11 @@ rust-version = "1.63.0"
1111

1212
[features]
1313
default = ["std"]
14-
# High-level wrappers using futures traits.
15-
futures = ["std", "dep:futures"]
1614
# High-level wrappers using tokio traits - may affect MSRV requirements.
1715
tokio = ["std", "dep:tokio"]
1816
std = ["bitcoin/std", "bitcoin_hashes/std", "chacha20-poly1305/std", "rand/std", "rand/std_rng"]
1917

2018
[dependencies]
21-
futures = { version = "0.3.30", default-features = false, optional = true, features = ["std"] }
2219
# The tokio feature may increase the MSRV beyond 1.63.0
2320
# depending on which version of tokio is selected by the caller.
2421
tokio = { version = "1", default-features = false, optional = true, features = ["io-util"] }

protocol/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ A BIP324 library to establish and communicate over an encrypted channel.
44

55
The library is designed with a bare `no_std` and "Sans I/O" interface to keep it as agnostic as possible to application runtimes, but higher level interfaces are exposed for ease of use.
66

7-
The `futures` feature includes the high-level `AsyncProcotol` type which helps create and manage an encrypted channel.
7+
The `tokio` feature includes the high-level `AsyncProcotol` type which helps create and manage an encrypted channel.
88

99
The lower-level `CipherSession` and `Handshake` types can be directly used by applications which require more control. The handshake performs the one-and-a-half round trip dance between the peers in order to generate secret materials and verify a channel. A successful handshake results in a cipher session which performs the encrypt and decrypt operations for the lifetime of the channel.
1010

1111
## Feature Flags
1212

1313
* `std` -- Standard library dependencies for I/O, memory allocation, and random number generators.
14-
* `futures` -- High level wrappers for asynchronous read and write runtimes using agnostic futures-rs traits.
15-
* `tokio` -- Same wrappers as `futures`, but using the popular tokio runtime's specific traits instead of futures-rs.
14+
* `tokio` -- High level I/O wrappers for asynchronous tokio runtime.
1615

1716
## Minimum Supported Rust Version (MSRV)
1817

protocol/src/io.rs

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,31 @@
44
//! connections over Read/Write transports.
55
66
use core::fmt;
7-
8-
#[cfg(feature = "std")]
7+
#[cfg(feature = "tokio")]
98
use std::vec;
10-
#[cfg(feature = "std")]
119
use std::vec::Vec;
1210

13-
use bitcoin::Network;
11+
use crate::{Error, PacketType};
1412

15-
// Default to the futures-rs traits, but can overwrite with more specific
16-
// tokio implementations for easier caller integration.
17-
#[cfg(all(feature = "futures", not(feature = "tokio")))]
18-
use futures::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
1913
#[cfg(feature = "tokio")]
20-
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
14+
use bitcoin::Network;
2115

16+
#[cfg(feature = "tokio")]
2217
use crate::{
2318
handshake::{self, GarbageResult, VersionResult},
24-
Error, Handshake, InboundCipher, OutboundCipher, PacketType, Role, NUM_ELLIGATOR_SWIFT_BYTES,
19+
Handshake, InboundCipher, OutboundCipher, Role, NUM_ELLIGATOR_SWIFT_BYTES,
2520
NUM_GARBAGE_TERMINTOR_BYTES,
2621
};
2722

23+
#[cfg(feature = "tokio")]
24+
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
25+
2826
/// A decrypted BIP324 payload with its packet type.
29-
#[cfg(feature = "std")]
3027
pub struct Payload {
3128
contents: Vec<u8>,
3229
packet_type: PacketType,
3330
}
3431

35-
#[cfg(feature = "std")]
3632
impl Payload {
3733
/// Create a new payload.
3834
pub fn new(contents: Vec<u8>, packet_type: PacketType) -> Self {
@@ -54,7 +50,6 @@ impl Payload {
5450
}
5551

5652
/// High level error type for the protocol interface.
57-
#[cfg(feature = "std")]
5853
#[derive(Debug)]
5954
pub enum ProtocolError {
6055
/// Wrap all IO errors with suggestion for next step on failure.
@@ -64,7 +59,6 @@ pub enum ProtocolError {
6459
}
6560

6661
/// Suggest to caller next step on protocol failure.
67-
#[cfg(feature = "std")]
6862
#[derive(Debug)]
6963
pub enum ProtocolFailureSuggestion {
7064
/// Caller could attempt to retry the connection with protocol V1 if desired.
@@ -73,7 +67,6 @@ pub enum ProtocolFailureSuggestion {
7367
Abort,
7468
}
7569

76-
#[cfg(feature = "std")]
7770
impl From<std::io::Error> for ProtocolError {
7871
fn from(error: std::io::Error) -> Self {
7972
// Detect IO errors which possibly mean the remote doesn't understand
@@ -94,19 +87,18 @@ impl From<std::io::Error> for ProtocolError {
9487
}
9588
}
9689

97-
#[cfg(feature = "std")]
9890
impl From<Error> for ProtocolError {
9991
fn from(error: Error) -> Self {
10092
ProtocolError::Internal(error)
10193
}
10294
}
10395

104-
#[cfg(feature = "std")]
10596
impl ProtocolError {
10697
/// Create an EOF error that suggests retrying with V1 protocol.
10798
///
10899
/// This is used when the remote peer closes the connection during handshake,
109100
/// which often indicates they don't support the V2 protocol.
101+
#[cfg(feature = "tokio")]
110102
fn eof() -> Self {
111103
ProtocolError::Io(
112104
std::io::Error::new(
@@ -118,7 +110,6 @@ impl ProtocolError {
118110
}
119111
}
120112

121-
#[cfg(feature = "std")]
122113
impl std::error::Error for ProtocolError {
123114
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
124115
match self {
@@ -128,7 +119,6 @@ impl std::error::Error for ProtocolError {
128119
}
129120
}
130121

131-
#[cfg(feature = "std")]
132122
impl fmt::Display for ProtocolError {
133123
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
134124
match self {
@@ -149,13 +139,13 @@ impl fmt::Display for ProtocolError {
149139
}
150140

151141
/// A protocol session with handshake and send/receive packet management.
152-
#[cfg(any(feature = "futures", feature = "tokio"))]
142+
#[cfg(feature = "tokio")]
153143
pub struct AsyncProtocol {
154144
reader: AsyncProtocolReader,
155145
writer: AsyncProtocolWriter,
156146
}
157147

158-
#[cfg(any(feature = "futures", feature = "tokio"))]
148+
#[cfg(feature = "tokio")]
159149
impl AsyncProtocol {
160150
/// New protocol session which completes the initial handshake and returns a handler.
161151
///
@@ -297,7 +287,9 @@ impl AsyncProtocol {
297287
}
298288

299289
/// State machine of an asynchronous packet read.
300-
#[cfg(any(feature = "futures", feature = "tokio"))]
290+
///
291+
/// This maintains state between await points to ensure cancellation safety.
292+
#[cfg(feature = "tokio")]
301293
#[derive(Debug)]
302294
enum DecryptState {
303295
ReadingLength {
@@ -310,7 +302,7 @@ enum DecryptState {
310302
},
311303
}
312304

313-
#[cfg(any(feature = "futures", feature = "tokio"))]
305+
#[cfg(feature = "tokio")]
314306
impl DecryptState {
315307
/// Transition state to reading the length bytes.
316308
fn init_reading_length() -> Self {
@@ -330,13 +322,13 @@ impl DecryptState {
330322
}
331323

332324
/// Manages an async buffer to automatically decrypt contents of received packets.
333-
#[cfg(any(feature = "futures", feature = "tokio"))]
325+
#[cfg(feature = "tokio")]
334326
pub struct AsyncProtocolReader {
335327
inbound_cipher: InboundCipher,
336328
state: DecryptState,
337329
}
338330

339-
#[cfg(any(feature = "futures", feature = "tokio"))]
331+
#[cfg(feature = "tokio")]
340332
impl AsyncProtocolReader {
341333
/// Decrypt contents of received packet from buffer.
342334
///
@@ -397,12 +389,12 @@ impl AsyncProtocolReader {
397389
}
398390

399391
/// Manages an async buffer to automatically encrypt and send contents in packets.
400-
#[cfg(any(feature = "futures", feature = "tokio"))]
392+
#[cfg(feature = "tokio")]
401393
pub struct AsyncProtocolWriter {
402394
outbound_cipher: OutboundCipher,
403395
}
404396

405-
#[cfg(any(feature = "futures", feature = "tokio"))]
397+
#[cfg(feature = "tokio")]
406398
impl AsyncProtocolWriter {
407399
/// Encrypt contents and write packet buffer.
408400
///

protocol/src/lib.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extern crate std;
2323

2424
mod fschacha20poly1305;
2525
mod handshake;
26-
#[cfg(any(feature = "futures", feature = "tokio"))]
26+
#[cfg(feature = "std")]
2727
pub mod io;
2828
#[cfg(feature = "std")]
2929
pub mod serde;
@@ -43,14 +43,7 @@ pub use handshake::{
4343
GarbageResult, Handshake, Initialized, ReceivedGarbage, ReceivedKey, SentKey, SentVersion,
4444
VersionResult,
4545
};
46-
// Re-exports from io module (async I/O types for backwards compatibility)
47-
#[cfg(any(feature = "futures", feature = "tokio"))]
48-
pub use io::{
49-
AsyncProtocol, AsyncProtocolReader, AsyncProtocolWriter, Payload, ProtocolError,
50-
ProtocolFailureSuggestion,
51-
};
5246

53-
// Internal imports
5447
use fschacha20poly1305::{FSChaCha20, FSChaCha20Poly1305};
5548

5649
/// Value for header byte with the decoy flag flipped to true.

proxy/src/bin/proxy.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
use std::str::FromStr;
77

88
use bip324::{
9+
io::{AsyncProtocol, ProtocolFailureSuggestion},
910
serde::{deserialize, serialize},
10-
AsyncProtocol, PacketType, ProtocolFailureSuggestion, Role,
11+
PacketType, Role,
1112
};
1213
use bip324_proxy::{V1ProtocolReader, V1ProtocolWriter};
1314
use bitcoin::Network;
@@ -87,7 +88,9 @@ async fn v2_proxy(
8788
.await
8889
{
8990
Ok(p) => p,
90-
Err(bip324::ProtocolError::Io(_, ProtocolFailureSuggestion::RetryV1)) if v1_fallback => {
91+
Err(bip324::io::ProtocolError::Io(_, ProtocolFailureSuggestion::RetryV1))
92+
if v1_fallback =>
93+
{
9194
info!("V2 protocol failed, falling back to V1...");
9295
return v1_proxy(client, network).await;
9396
}

proxy/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub enum Error {
3434
WrongCommand,
3535
Serde,
3636
Io(std::io::Error),
37-
Protocol(bip324::ProtocolError),
37+
Protocol(bip324::io::ProtocolError),
3838
}
3939

4040
impl fmt::Display for Error {
@@ -61,8 +61,8 @@ impl std::error::Error for Error {
6161
}
6262
}
6363

64-
impl From<bip324::ProtocolError> for Error {
65-
fn from(e: bip324::ProtocolError) -> Self {
64+
impl From<bip324::io::ProtocolError> for Error {
65+
fn from(e: bip324::io::ProtocolError) -> Self {
6666
Error::Protocol(e)
6767
}
6868
}

0 commit comments

Comments
 (0)