Skip to content

Commit

Permalink
Re-export all types inside the network module
Browse files Browse the repository at this point in the history
Also make the inner modules private so that all network-related
types come in a single namespace in the API. This gives more ease-of-use
and gives us more flexibility to organize the types as we like.
  • Loading branch information
stevenroose committed Nov 26, 2020
1 parent ee192eb commit 3b8efcc
Show file tree
Hide file tree
Showing 21 changed files with 67 additions and 63 deletions.
31 changes: 16 additions & 15 deletions examples/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use std::{env, process};
use std::io::Write;

use bitcoin::consensus::encode;
use bitcoin::network::{address, constants, message, message_network};
use bitcoin::network::stream_reader::StreamReader;
use bitcoin::network::{
Address, Network, NetworkMessage, RawNetworkMessage, ServiceFlags, StreamReader, VersionMessage,
};
use bitcoin::secp256k1;
use bitcoin::secp256k1::rand::Rng;

Expand All @@ -29,8 +30,8 @@ fn main() {

let version_message = build_version_message(address);

let first_message = message::RawNetworkMessage {
magic: constants::Network::Bitcoin.magic(),
let first_message = RawNetworkMessage {
magic: Network::Bitcoin.magic(),
payload: version_message,
};

Expand All @@ -44,20 +45,20 @@ fn main() {
let mut stream_reader = StreamReader::new(read_stream, None);
loop {
// Loop an retrieve new messages
let reply: message::RawNetworkMessage = stream_reader.read_next().unwrap();
let reply: RawNetworkMessage = stream_reader.read_next().unwrap();
match reply.payload {
message::NetworkMessage::Version(_) => {
NetworkMessage::Version(_) => {
println!("Received version message: {:?}", reply.payload);

let second_message = message::RawNetworkMessage {
magic: constants::Network::Bitcoin.magic(),
payload: message::NetworkMessage::Verack,
let second_message = RawNetworkMessage {
magic: Network::Bitcoin.magic(),
payload: NetworkMessage::Verack,
};

let _ = stream.write_all(encode::serialize(&second_message).as_slice());
println!("Sent verack message");
}
message::NetworkMessage::Verack => {
NetworkMessage::Verack => {
println!("Received verack message: {:?}", reply.payload);
break;
}
Expand All @@ -73,12 +74,12 @@ fn main() {
}
}

fn build_version_message(address: SocketAddr) -> message::NetworkMessage {
fn build_version_message(address: SocketAddr) -> NetworkMessage {
// Building version message, see https://en.bitcoin.it/wiki/Protocol_documentation#version
let my_address = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0);

// "bitfield of features to be enabled for this connection"
let services = constants::ServiceFlags::NONE;
let services = ServiceFlags::NONE;

// "standard UNIX timestamp in seconds"
let timestamp = SystemTime::now()
Expand All @@ -87,10 +88,10 @@ fn build_version_message(address: SocketAddr) -> message::NetworkMessage {
.as_secs();

// "The network address of the node receiving this message"
let addr_recv = address::Address::new(&address, constants::ServiceFlags::NONE);
let addr_recv = Address::new(&address, ServiceFlags::NONE);

// "The network address of the node emitting this message"
let addr_from = address::Address::new(&my_address, constants::ServiceFlags::NONE);
let addr_from = Address::new(&my_address, ServiceFlags::NONE);

// "Node random nonce, randomly generated every time a version packet is sent. This nonce is used to detect connections to self."
let nonce: u64 = secp256k1::rand::thread_rng().gen();
Expand All @@ -102,7 +103,7 @@ fn build_version_message(address: SocketAddr) -> message::NetworkMessage {
let start_height: i32 = 0;

// Construct the message
message::NetworkMessage::Version(message_network::VersionMessage::new(
NetworkMessage::Version(VersionMessage::new(
services,
timestamp as i64,
addr_recv,
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/deserialize_script.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate bitcoin;

use bitcoin::util::address::Address;
use bitcoin::network::constants::Network;
use bitcoin::network::Network;
use bitcoin::blockdata::script;
use bitcoin::consensus::encode;

Expand Down
2 changes: 1 addition & 1 deletion src/blockdata/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use hashes::{Hash, HashEngine};
use hash_types::{Wtxid, BlockHash, TxMerkleNode, WitnessMerkleNode, WitnessCommitment};
use util::uint::Uint256;
use consensus::encode::Encodable;
use network::constants::Network;
use network::Network;
use blockdata::transaction::Transaction;
use blockdata::constants::{max_target, WITNESS_SCALE_FACTOR};
use blockdata::script;
Expand Down
4 changes: 2 additions & 2 deletions src/blockdata/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use blockdata::opcodes;
use blockdata::script;
use blockdata::transaction::{OutPoint, Transaction, TxOut, TxIn};
use blockdata::block::{Block, BlockHeader};
use network::constants::Network;
use network::Network;
use util::uint::Uint256;

/// The maximum allowable sequence number
Expand Down Expand Up @@ -162,7 +162,7 @@ mod test {
use std::default::Default;
use hashes::hex::FromHex;

use network::constants::Network;
use network::Network;
use consensus::encode::serialize;
use blockdata::constants::{genesis_block, bitcoin_genesis_tx};
use blockdata::constants::{MAX_SEQUENCE, COIN_VALUE};
Expand Down
4 changes: 2 additions & 2 deletions src/blockdata/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl OutPoint {
///
/// ```rust
/// use bitcoin::blockdata::constants::genesis_block;
/// use bitcoin::network::constants::Network;
/// use bitcoin::network::Network;
///
/// let block = genesis_block(Network::Bitcoin);
/// let tx = &block.txdata[0];
Expand Down Expand Up @@ -749,7 +749,7 @@ mod tests {

#[test]
fn test_is_coinbase () {
use network::constants::Network;
use network::Network;
use blockdata::constants;

let genesis = constants::genesis_block(Network::Bitcoin);
Expand Down
6 changes: 2 additions & 4 deletions src/consensus/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ use util::endian;
use util::psbt;

use blockdata::transaction::{TxOut, Transaction, TxIn};
use network::message_blockdata::Inventory;
use network::address::{Address, AddrV2Message};
use network::{Address, AddrV2Message, Inventory};

/// Encoding error
#[derive(Debug)]
Expand Down Expand Up @@ -742,8 +741,7 @@ mod tests {
use consensus::{Encodable, deserialize_partial, Decodable};
use util::endian::{u64_to_array_le, u32_to_array_le, u16_to_array_le};
use secp256k1::rand::{thread_rng, Rng};
use network::message_blockdata::Inventory;
use network::Address;
use network::{Address, Inventory};

#[test]
fn serialize_int_test() {
Expand Down
2 changes: 1 addition & 1 deletion src/consensus/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! This module provides predefined set of parameters for different chains.
//!

use network::constants::Network;
use network::Network;
use util::uint::Uint256;

/// Lowest possible difficulty for Mainnet. See comment on Params::pow_limit for more info.
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub use blockdata::transaction::TxOut;
pub use blockdata::transaction::OutPoint;
pub use blockdata::transaction::SigHashType;
pub use consensus::encode::VarInt;
pub use network::constants::Network;
pub use network::Network;
pub use util::Error;
pub use util::address::Address;
pub use util::address::AddressType;
Expand Down
4 changes: 2 additions & 2 deletions src/network/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::io;
use std::fmt;
use std::net::{SocketAddr, Ipv6Addr, SocketAddrV4, SocketAddrV6, Ipv4Addr};

use network::constants::ServiceFlags;
use network::ServiceFlags;
use consensus::encode::{self, Decodable, Encodable, VarInt, ReadExt, WriteExt};

/// A message which can be sent on the Bitcoin network
Expand Down Expand Up @@ -268,7 +268,7 @@ impl Decodable for AddrV2Message {
mod test {
use std::str::FromStr;
use super::{AddrV2Message, AddrV2, Address};
use network::constants::ServiceFlags;
use network::ServiceFlags;
use std::net::{SocketAddr, IpAddr, Ipv4Addr, Ipv6Addr};
use hashes::hex::FromHex;

Expand Down
6 changes: 3 additions & 3 deletions src/network/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
//! # Example: encoding a network's magic bytes
//!
//! ```rust
//! use bitcoin::network::constants::Network;
//! use bitcoin::Network;
//! use bitcoin::consensus::encode::serialize;
//!
//! let network = Network::Bitcoin;
Expand Down Expand Up @@ -79,7 +79,7 @@ impl Network {
/// # Examples
///
/// ```rust
/// use bitcoin::network::constants::Network;
/// use bitcoin::network::Network;
///
/// assert_eq!(Some(Network::Bitcoin), Network::from_magic(0xD9B4BEF9));
/// assert_eq!(None, Network::from_magic(0xFFFFFFFF));
Expand All @@ -101,7 +101,7 @@ impl Network {
/// # Examples
///
/// ```rust
/// use bitcoin::network::constants::Network;
/// use bitcoin::network::Network;
///
/// let network = Network::Bitcoin;
/// assert_eq!(network.magic(), 0xD9B4BEF9);
Expand Down
2 changes: 1 addition & 1 deletion src/network/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ mod test {
use std::io;
use std::net::Ipv4Addr;
use super::{RawNetworkMessage, NetworkMessage, CommandString};
use network::constants::ServiceFlags;
use network::ServiceFlags;
use consensus::encode::{Encodable, deserialize, deserialize_partial, serialize};
use hashes::hex::FromHex;
use hashes::sha256d::Hash;
Expand Down
6 changes: 3 additions & 3 deletions src/network/message_blockdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::io;

use hashes::sha256d;

use network::constants;
use network::PROTOCOL_VERSION;
use consensus::encode::{self, Decodable, Encodable};
use hash_types::{BlockHash, Txid, Wtxid};

Expand Down Expand Up @@ -114,7 +114,7 @@ impl GetBlocksMessage {
/// Construct a new `getblocks` message
pub fn new(locator_hashes: Vec<BlockHash>, stop_hash: BlockHash) -> GetBlocksMessage {
GetBlocksMessage {
version: constants::PROTOCOL_VERSION,
version: PROTOCOL_VERSION,
locator_hashes: locator_hashes,
stop_hash: stop_hash
}
Expand All @@ -127,7 +127,7 @@ impl GetHeadersMessage {
/// Construct a new `getheaders` message
pub fn new(locator_hashes: Vec<BlockHash>, stop_hash: BlockHash) -> GetHeadersMessage {
GetHeadersMessage {
version: constants::PROTOCOL_VERSION,
version: PROTOCOL_VERSION,
locator_hashes: locator_hashes,
stop_hash: stop_hash
}
Expand Down
8 changes: 3 additions & 5 deletions src/network/message_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
use std::io;
use std::borrow::Cow;

use network::address::Address;
use network::constants::{self, ServiceFlags};
use network::{Address, CommandString, PROTOCOL_VERSION, ServiceFlags};
use consensus::{Encodable, Decodable, ReadExt};
use consensus::encode;
use network::message::CommandString;
use hashes::sha256d;

/// Some simple messages
Expand Down Expand Up @@ -67,7 +65,7 @@ impl VersionMessage {
start_height: i32,
) -> VersionMessage {
VersionMessage {
version: constants::PROTOCOL_VERSION,
version: PROTOCOL_VERSION,
services: services,
timestamp: timestamp,
receiver: receiver,
Expand Down Expand Up @@ -148,7 +146,7 @@ mod tests {
use super::VersionMessage;

use hashes::hex::FromHex;
use network::constants::ServiceFlags;
use network::ServiceFlags;

use consensus::encode::{deserialize, serialize};

Expand Down
26 changes: 17 additions & 9 deletions src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,24 @@ use std::fmt;
use std::io;
use std::error;

pub mod constants;
mod address;
mod constants;
mod message;
mod message_blockdata;
mod message_network;
mod message_filter;
mod stream_reader;

pub mod address;
pub use self::address::Address;
pub mod message;
pub mod message_blockdata;
pub mod message_network;
pub mod message_filter;
pub mod stream_reader;
// Re-export all network types.
pub use self::address::{Address, AddrV2, AddrV2Message};
pub use self::constants::{Network, PROTOCOL_VERSION, ServiceFlags};
pub use self::message::{CommandString, NetworkMessage, RawNetworkMessage};
pub use self::message_blockdata::{GetBlocksMessage, GetHeadersMessage, Inventory};
pub use self::message_network::{Reject, RejectReason, VersionMessage};
pub use self::message_filter::{
CFCheckpt, CFHeaders, CFilter, GetCFCheckpt, GetCFHeaders, GetCFilters,
};
pub use self::stream_reader::StreamReader;

/// Network error
#[derive(Debug)]
Expand Down Expand Up @@ -61,7 +70,6 @@ impl From<io::Error> for Error {
}

impl error::Error for Error {

fn cause(&self) -> Option<&dyn error::Error> {
match *self {
Error::Io(ref e) => Some(e),
Expand Down
2 changes: 1 addition & 1 deletion src/network/stream_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ mod test {
use std::io::{self, BufReader, Write};
use std::net::{TcpListener, TcpStream, Shutdown};
use std::thread::JoinHandle;
use network::constants::ServiceFlags;
use network::ServiceFlags;

use super::StreamReader;
use network::message::{NetworkMessage, RawNetworkMessage};
Expand Down
6 changes: 3 additions & 3 deletions src/util/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//!
//! ```rust
//!
//! use bitcoin::network::constants::Network;
//! use bitcoin::network::Network;
//! use bitcoin::util::address::Address;
//! use bitcoin::util::key;
//! use bitcoin::secp256k1::Secp256k1;
Expand All @@ -44,7 +44,7 @@ use bech32;
use hashes::Hash;
use hash_types::{PubkeyHash, WPubkeyHash, ScriptHash, WScriptHash};
use blockdata::script;
use network::constants::Network;
use network::Network;
use util::base58;
use util::key;

Expand Down Expand Up @@ -499,7 +499,7 @@ mod tests {
use hashes::hex::{FromHex, ToHex};

use blockdata::script::Script;
use network::constants::Network::{Bitcoin, Testnet};
use network::Network::{Bitcoin, Testnet};
use util::key::PublicKey;

use super::*;
Expand Down
2 changes: 1 addition & 1 deletion src/util/bip143.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ mod tests {
use blockdata::script::Script;
use blockdata::transaction::Transaction;
use consensus::encode::deserialize;
use network::constants::Network;
use network::Network;
use util::address::Address;
use util::key::PublicKey;
use hashes::hex::FromHex;
Expand Down
4 changes: 2 additions & 2 deletions src/util/bip32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use hash_types::XpubIdentifier;
use hashes::{sha512, Hash, HashEngine, Hmac, HmacEngine};
use secp256k1::{self, Secp256k1};

use network::constants::Network;
use network::Network;
use util::{base58, endian};
use util::key::{self, PublicKey, PrivateKey};

Expand Down Expand Up @@ -747,7 +747,7 @@ mod tests {
use secp256k1::{self, Secp256k1};
use hashes::hex::FromHex;

use network::constants::Network::{self, Bitcoin};
use network::Network::{self, Bitcoin};

#[test]
fn test_parse_derivation_path() {
Expand Down
Loading

0 comments on commit 3b8efcc

Please sign in to comment.