-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor protocol into internal, external modules.
This commit just moves things around and patches import paths.
- Loading branch information
1 parent
8a9a5ba
commit 6db852f
Showing
16 changed files
with
137 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,6 @@ where | |
let client = hs.call((stream, addr)).await?; | ||
Ok(Change::Insert(addr, client)) | ||
} | ||
.boxed() | ||
.boxed() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,8 @@ | ||
//! Zcash network protocol handling. | ||
pub mod codec; | ||
pub mod message; | ||
pub mod types; | ||
|
||
pub mod inv; | ||
|
||
// XXX at some later point the above should move to an `external` submodule, so | ||
// that we have | ||
// - protocol::external::{all_bitcoin_zcash_types}; | ||
// - protocol::internal::{all_internal_req_rsp_types}; | ||
|
||
/// The external Bitcoin-based protocol. | ||
pub mod external; | ||
/// The internal request/response protocol. | ||
pub mod internal; | ||
/// Newtype wrappers giving semantic meaning to primitive datatypes. | ||
pub mod types; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/// A Tokio codec that transforms an `AsyncRead` into a `Stream` of `Message`s. | ||
mod codec; | ||
/// Inventory items. | ||
mod inv; | ||
/// An enum of all supported Bitcoin message types. | ||
mod message; | ||
/// Newtype wrappers for primitive types. | ||
pub mod types; | ||
|
||
pub use codec::Codec; | ||
pub use inv::InventoryHash; | ||
pub use message::Message; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use hex; | ||
use std::fmt; | ||
|
||
/// A magic number identifying the network. | ||
#[derive(Copy, Clone, Eq, PartialEq)] | ||
pub struct Magic(pub [u8; 4]); | ||
|
||
impl fmt::Debug for Magic { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
f.debug_tuple("Magic").field(&hex::encode(&self.0)).finish() | ||
} | ||
} | ||
|
||
/// A protocol version number. | ||
#[derive(Copy, Clone, Debug, Eq, PartialEq)] | ||
pub struct Version(pub u32); | ||
|
||
bitflags! { | ||
/// A bitflag describing services advertised by a node in the network. | ||
/// | ||
/// Note that bits 24-31 are reserved for temporary experiments; other | ||
/// service bits should be allocated via the ZIP process. | ||
#[derive(Default)] | ||
pub struct PeerServices: u64 { | ||
/// NODE_NETWORK means that the node is a full node capable of serving | ||
/// blocks, as opposed to a light client that makes network requests but | ||
/// does not provide network services. | ||
const NODE_NETWORK = (1 << 0); | ||
/// NODE_BLOOM means that the node supports bloom-filtered connections. | ||
const NODE_BLOOM = (1 << 2); | ||
} | ||
} | ||
|
||
/// A nonce used in the networking layer to identify messages. | ||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] | ||
pub struct Nonce(pub u64); | ||
|
||
impl Default for Nonce { | ||
fn default() -> Self { | ||
use rand::{thread_rng, Rng}; | ||
Self(thread_rng().gen()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
use crate::constants::magics; | ||
|
||
#[test] | ||
fn magic_debug() { | ||
assert_eq!(format!("{:?}", magics::MAINNET), "Magic(\"24e92764\")"); | ||
assert_eq!(format!("{:?}", magics::TESTNET), "Magic(\"fa1af9bf\")"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,5 @@ | ||
//! Message types for the internal request/response protocol. | ||
//! | ||
//! These are currently defined just as enums with all possible requests and | ||
//! responses, so that we have unified types to pass around. No serialization | ||
//! is performed as these are only internal types. | ||
mod request; | ||
mod response; | ||
|
||
use std::error::Error; | ||
|
||
use zebra_chain::transaction::Transaction; | ||
|
||
use crate::meta_addr::MetaAddr; | ||
|
||
use super::types::Nonce; | ||
|
||
/// A network request, represented in internal format. | ||
#[derive(Clone, Debug, Eq, PartialEq)] | ||
pub enum Request { | ||
/// Requests additional peers from the server. | ||
GetPeers, | ||
/// Advertises peers to the remote server. | ||
PushPeers(Vec<MetaAddr>), | ||
/// Heartbeats triggered on peer connection start. | ||
// This is included as a bit of a hack, it should only be used | ||
// internally for connection management. You should not expect to | ||
// be firing or handling `Ping` requests or `Pong` responses. | ||
Ping(Nonce), | ||
/// Requests the transactions the remote server has verified but | ||
/// not yet confirmed. | ||
GetMempool, | ||
} | ||
|
||
/// A response to a network request, represented in internal format. | ||
#[derive(Clone, Debug, Eq, PartialEq)] | ||
pub enum Response { | ||
/// Generic success. | ||
Ok, | ||
/// Generic error. | ||
Error, | ||
/// A list of peers, used to respond to `GetPeers`. | ||
Peers(Vec<MetaAddr>), | ||
/// A list of transactions, such as in response to `GetMempool`. | ||
Transactions(Vec<Transaction>), | ||
} | ||
|
||
impl<E> From<E> for Response | ||
where | ||
E: Error, | ||
{ | ||
fn from(_e: E) -> Self { | ||
Self::Error | ||
} | ||
} | ||
pub use request::Request; | ||
pub use response::Response; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use crate::meta_addr::MetaAddr; | ||
|
||
use super::super::types::Nonce; | ||
|
||
/// A network request, represented in internal format. | ||
#[derive(Clone, Debug, Eq, PartialEq)] | ||
pub enum Request { | ||
/// Requests additional peers from the server. | ||
GetPeers, | ||
/// Advertises peers to the remote server. | ||
PushPeers(Vec<MetaAddr>), | ||
/// Heartbeats triggered on peer connection start. | ||
// This is included as a bit of a hack, it should only be used | ||
// internally for connection management. You should not expect to | ||
// be firing or handling `Ping` requests or `Pong` responses. | ||
Ping(Nonce), | ||
/// Requests the transactions the remote server has verified but | ||
/// not yet confirmed. | ||
GetMempool, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use std::error::Error; | ||
|
||
// XXX clean module layout of zebra_chain | ||
use zebra_chain::transaction::Transaction; | ||
|
||
use crate::meta_addr::MetaAddr; | ||
|
||
/// A response to a network request, represented in internal format. | ||
#[derive(Clone, Debug, Eq, PartialEq)] | ||
pub enum Response { | ||
/// Generic success. | ||
Ok, | ||
/// Generic error. | ||
Error, | ||
/// A list of peers, used to respond to `GetPeers`. | ||
Peers(Vec<MetaAddr>), | ||
/// A list of transactions, such as in response to `GetMempool`. | ||
Transactions(Vec<Transaction>), | ||
} | ||
|
||
impl<E> From<E> for Response | ||
where | ||
E: Error, | ||
{ | ||
fn from(_e: E) -> Self { | ||
Self::Error | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,4 @@ | ||
//! Newtype wrappers assigning semantic meaning to primitive types. | ||
use hex; | ||
use std::fmt; | ||
|
||
/// A magic number identifying the network. | ||
#[derive(Copy, Clone, Eq, PartialEq)] | ||
pub struct Magic(pub [u8; 4]); | ||
|
||
impl fmt::Debug for Magic { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
f.debug_tuple("Magic").field(&hex::encode(&self.0)).finish() | ||
} | ||
} | ||
|
||
/// A protocol version number. | ||
#[derive(Copy, Clone, Debug, Eq, PartialEq)] | ||
pub struct Version(pub u32); | ||
|
||
bitflags! { | ||
/// A bitflag describing services advertised by a node in the network. | ||
/// | ||
/// Note that bits 24-31 are reserved for temporary experiments; other | ||
/// service bits should be allocated via the ZIP process. | ||
#[derive(Default)] | ||
pub struct PeerServices: u64 { | ||
/// NODE_NETWORK means that the node is a full node capable of serving | ||
/// blocks, as opposed to a light client that makes network requests but | ||
/// does not provide network services. | ||
const NODE_NETWORK = (1 << 0); | ||
/// NODE_BLOOM means that the node supports bloom-filtered connections. | ||
const NODE_BLOOM = (1 << 2); | ||
} | ||
} | ||
|
||
/// A nonce used in the networking layer to identify messages. | ||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] | ||
pub struct Nonce(pub u64); | ||
|
||
impl Default for Nonce { | ||
fn default() -> Self { | ||
use rand::{thread_rng, Rng}; | ||
Self(thread_rng().gen()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
use crate::constants::magics; | ||
|
||
#[test] | ||
fn magic_debug() { | ||
assert_eq!(format!("{:?}", magics::MAINNET), "Magic(\"24e92764\")"); | ||
assert_eq!(format!("{:?}", magics::TESTNET), "Magic(\"fa1af9bf\")"); | ||
} | ||
} | ||
// Because of the `ping` hack, `Nonce` is included in `Request`s. | ||
pub use super::external::types::Nonce; | ||
// The services flag is used in `MetaAddr`s. | ||
pub use super::external::types::PeerServices; |