2014-07-18 13:56:17 +00:00
|
|
|
// Rust Bitcoin Library
|
|
|
|
// Written in 2014 by
|
|
|
|
// Andrew Poelstra <apoelstra@wpsoftware.net>
|
|
|
|
//
|
|
|
|
// To the extent possible under law, the author(s) have dedicated all
|
|
|
|
// copyright and related and neighboring rights to this software to
|
|
|
|
// the public domain worldwide. This software is distributed without
|
|
|
|
// any warranty.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the CC0 Public Domain Dedication
|
|
|
|
// along with this software.
|
|
|
|
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
|
|
|
|
//
|
|
|
|
|
2018-08-10 17:09:22 +00:00
|
|
|
//! Network constants
|
2014-07-18 13:56:17 +00:00
|
|
|
//!
|
|
|
|
//! This module provides various constants relating to the Bitcoin network
|
|
|
|
//! protocol, such as protocol versioning and magic header bytes.
|
|
|
|
//!
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
//! The [`Network`][1] type implements the [`Decodable`][2] and
|
|
|
|
//! [`Encodable`][3] traits and encodes the magic bytes of the given
|
2018-08-10 17:09:22 +00:00
|
|
|
//! network
|
|
|
|
//!
|
|
|
|
//! [1]: enum.Network.html
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
//! [2]: ../../consensus/encode/trait.Decodable.html
|
|
|
|
//! [3]: ../../consensus/encode/trait.Encodable.html
|
2018-08-10 17:09:22 +00:00
|
|
|
//!
|
|
|
|
//! # Example: encoding a network's magic bytes
|
|
|
|
//!
|
|
|
|
//! ```rust
|
|
|
|
//! use bitcoin::network::constants::Network;
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
//! use bitcoin::consensus::encode::serialize;
|
2018-08-10 17:09:22 +00:00
|
|
|
//!
|
|
|
|
//! let network = Network::Bitcoin;
|
2019-05-23 20:28:10 +00:00
|
|
|
//! let bytes = serialize(&network.magic());
|
2018-08-10 17:09:22 +00:00
|
|
|
//!
|
|
|
|
//! assert_eq!(&bytes[..], &[0xF9, 0xBE, 0xB4, 0xD9]);
|
|
|
|
//! ```
|
2014-07-18 13:56:17 +00:00
|
|
|
|
2019-12-05 20:13:45 +00:00
|
|
|
use std::{fmt, io, ops};
|
2019-11-22 00:42:15 +00:00
|
|
|
|
|
|
|
use consensus::encode::{self, Encodable, Decodable};
|
|
|
|
|
2018-08-10 17:09:22 +00:00
|
|
|
/// Version of the protocol as appearing in network message headers
|
|
|
|
pub const PROTOCOL_VERSION: u32 = 70001;
|
|
|
|
|
2015-01-18 18:16:01 +00:00
|
|
|
user_enum! {
|
2018-08-10 17:09:22 +00:00
|
|
|
/// The cryptocurrency to act on
|
2018-08-20 09:10:21 +00:00
|
|
|
#[derive(Copy, PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
|
2015-04-07 01:51:11 +00:00
|
|
|
pub enum Network {
|
2018-08-10 17:09:22 +00:00
|
|
|
/// Classic Bitcoin
|
2015-04-07 01:51:11 +00:00
|
|
|
Bitcoin <-> "bitcoin",
|
2018-08-10 17:09:22 +00:00
|
|
|
/// Bitcoin's testnet
|
2018-05-17 13:08:02 +00:00
|
|
|
Testnet <-> "testnet",
|
2018-08-10 17:09:22 +00:00
|
|
|
/// Bitcoin's regtest
|
2018-05-17 13:08:02 +00:00
|
|
|
Regtest <-> "regtest"
|
2015-04-07 01:51:11 +00:00
|
|
|
}
|
2015-01-18 18:16:01 +00:00
|
|
|
}
|
2014-08-03 13:03:00 +00:00
|
|
|
|
2018-08-10 17:09:22 +00:00
|
|
|
impl Network {
|
|
|
|
/// Creates a `Network` from the magic bytes.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use bitcoin::network::constants::Network;
|
|
|
|
///
|
|
|
|
/// assert_eq!(Some(Network::Bitcoin), Network::from_magic(0xD9B4BEF9));
|
|
|
|
/// assert_eq!(None, Network::from_magic(0xFFFFFFFF));
|
|
|
|
/// ```
|
|
|
|
pub fn from_magic(magic: u32) -> Option<Network> {
|
|
|
|
// Note: any new entries here must be added to `magic` below
|
|
|
|
match magic {
|
|
|
|
0xD9B4BEF9 => Some(Network::Bitcoin),
|
|
|
|
0x0709110B => Some(Network::Testnet),
|
|
|
|
0xDAB5BFFA => Some(Network::Regtest),
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
|
2018-08-10 17:09:22 +00:00
|
|
|
/// Return the network magic bytes, which should be encoded little-endian
|
|
|
|
/// at the start of every message
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use bitcoin::network::constants::Network;
|
|
|
|
///
|
|
|
|
/// let network = Network::Bitcoin;
|
|
|
|
/// assert_eq!(network.magic(), 0xD9B4BEF9);
|
|
|
|
/// ```
|
|
|
|
pub fn magic(&self) -> u32 {
|
|
|
|
// Note: any new entries here must be added to `from_magic` above
|
|
|
|
match *self {
|
|
|
|
Network::Bitcoin => 0xD9B4BEF9,
|
|
|
|
Network::Testnet => 0x0709110B,
|
|
|
|
Network::Regtest => 0xDAB5BFFA,
|
|
|
|
}
|
2015-04-07 01:51:11 +00:00
|
|
|
}
|
2014-07-18 21:38:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 00:42:15 +00:00
|
|
|
/// Flags to indicate which network services a node supports.
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub struct ServiceFlags(u64);
|
|
|
|
|
|
|
|
impl ServiceFlags {
|
|
|
|
/// NONE means no services supported.
|
|
|
|
pub const NONE: ServiceFlags = ServiceFlags(0);
|
|
|
|
|
|
|
|
/// NETWORK means that the node is capable of serving the complete block chain. It is currently
|
|
|
|
/// set by all Bitcoin Core non pruned nodes, and is unset by SPV clients or other light
|
|
|
|
/// clients.
|
|
|
|
pub const NETWORK: ServiceFlags = ServiceFlags(1 << 0);
|
|
|
|
|
|
|
|
/// GETUTXO means the node is capable of responding to the getutxo protocol request. Bitcoin
|
|
|
|
/// Core does not support this but a patch set called Bitcoin XT does.
|
|
|
|
/// See BIP 64 for details on how this is implemented.
|
|
|
|
pub const GETUTXO: ServiceFlags = ServiceFlags(1 << 1);
|
|
|
|
|
|
|
|
/// BLOOM means the node is capable and willing to handle bloom-filtered connections. Bitcoin
|
|
|
|
/// Core nodes used to support this by default, without advertising this bit, but no longer do
|
|
|
|
/// as of protocol version 70011 (= NO_BLOOM_VERSION)
|
|
|
|
pub const BLOOM: ServiceFlags = ServiceFlags(1 << 2);
|
|
|
|
|
|
|
|
/// WITNESS indicates that a node can be asked for blocks and transactions including witness
|
|
|
|
/// data.
|
|
|
|
pub const WITNESS: ServiceFlags = ServiceFlags(1 << 3);
|
2019-12-21 01:39:30 +00:00
|
|
|
|
|
|
|
/// COMPACT_FILTERS means the node will service basic block filter requests.
|
|
|
|
/// See BIP157 and BIP158 for details on how this is implemented.
|
|
|
|
pub const COMPACT_FILTERS: ServiceFlags = ServiceFlags(1 << 6);
|
2019-11-22 00:42:15 +00:00
|
|
|
|
|
|
|
/// NETWORK_LIMITED means the same as NODE_NETWORK with the limitation of only serving the last
|
|
|
|
/// 288 (2 day) blocks.
|
|
|
|
/// See BIP159 for details on how this is implemented.
|
|
|
|
pub const NETWORK_LIMITED: ServiceFlags = ServiceFlags(1 << 10);
|
|
|
|
|
2019-12-05 20:13:45 +00:00
|
|
|
// NOTE: When adding new flags, remember to update the Display impl accordingly.
|
|
|
|
|
2019-11-22 00:42:15 +00:00
|
|
|
/// Add [ServiceFlags] together.
|
|
|
|
///
|
|
|
|
/// Returns itself.
|
|
|
|
pub fn add(&mut self, other: ServiceFlags) -> ServiceFlags {
|
|
|
|
self.0 |= other.0;
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove [ServiceFlags] from this.
|
|
|
|
///
|
|
|
|
/// Returns itself.
|
|
|
|
pub fn remove(&mut self, other: ServiceFlags) -> ServiceFlags {
|
|
|
|
self.0 ^= other.0;
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check whether [ServiceFlags] are included in this one.
|
|
|
|
pub fn has(&self, flags: ServiceFlags) -> bool {
|
|
|
|
(self.0 | flags.0) == self.0
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the integer representation of this [ServiceFlags].
|
|
|
|
pub fn as_u64(&self) -> u64 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 20:13:45 +00:00
|
|
|
impl fmt::LowerHex for ServiceFlags {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt::LowerHex::fmt(&self.0, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::UpperHex for ServiceFlags {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt::UpperHex::fmt(&self.0, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for ServiceFlags {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
if *self == ServiceFlags::NONE {
|
|
|
|
return write!(f, "ServiceFlags(NONE)");
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut flags = self.clone();
|
|
|
|
let mut first = true;
|
|
|
|
macro_rules! write_flag {
|
|
|
|
($f:ident) => {
|
|
|
|
if flags.has(ServiceFlags::$f) {
|
|
|
|
if !first {
|
|
|
|
write!(f, "|")?;
|
|
|
|
}
|
|
|
|
first = false;
|
|
|
|
write!(f, stringify!($f))?;
|
|
|
|
flags.remove(ServiceFlags::$f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
write!(f, "ServiceFlags(")?;
|
|
|
|
write_flag!(NETWORK);
|
|
|
|
write_flag!(GETUTXO);
|
|
|
|
write_flag!(BLOOM);
|
|
|
|
write_flag!(WITNESS);
|
2019-12-21 02:06:03 +00:00
|
|
|
write_flag!(COMPACT_FILTERS);
|
2019-12-05 20:13:45 +00:00
|
|
|
write_flag!(NETWORK_LIMITED);
|
|
|
|
// If there are unknown flags left, we append them in hex.
|
|
|
|
if flags != ServiceFlags::NONE {
|
|
|
|
if !first {
|
|
|
|
write!(f, "|")?;
|
|
|
|
}
|
|
|
|
write!(f, "0x{:x}", flags)?;
|
|
|
|
}
|
|
|
|
write!(f, ")")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 00:42:15 +00:00
|
|
|
impl From<u64> for ServiceFlags {
|
|
|
|
fn from(f: u64) -> Self {
|
|
|
|
ServiceFlags(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<u64> for ServiceFlags {
|
|
|
|
fn into(self) -> u64 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::BitOr for ServiceFlags {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn bitor(mut self, rhs: Self) -> Self {
|
|
|
|
self.add(rhs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::BitOrAssign for ServiceFlags {
|
|
|
|
fn bitor_assign(&mut self, rhs: Self) {
|
|
|
|
self.add(rhs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::BitXor for ServiceFlags {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn bitxor(mut self, rhs: Self) -> Self {
|
|
|
|
self.remove(rhs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::BitXorAssign for ServiceFlags {
|
|
|
|
fn bitxor_assign(&mut self, rhs: Self) {
|
|
|
|
self.remove(rhs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Encodable for ServiceFlags {
|
|
|
|
#[inline]
|
|
|
|
fn consensus_encode<S: io::Write>(
|
|
|
|
&self,
|
|
|
|
mut s: S,
|
|
|
|
) -> Result<usize, encode::Error> {
|
|
|
|
self.0.consensus_encode(&mut s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Decodable for ServiceFlags {
|
|
|
|
#[inline]
|
|
|
|
fn consensus_decode<D: io::Read>(mut d: D) -> Result<Self, encode::Error> {
|
|
|
|
Ok(ServiceFlags(Decodable::consensus_decode(&mut d)?))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-03 13:03:00 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-11-22 00:42:15 +00:00
|
|
|
use super::{Network, ServiceFlags};
|
2019-05-23 20:28:10 +00:00
|
|
|
use consensus::encode::{deserialize, serialize};
|
2014-08-03 13:03:00 +00:00
|
|
|
|
2019-05-23 20:28:10 +00:00
|
|
|
#[test]
|
|
|
|
fn serialize_test() {
|
|
|
|
assert_eq!(
|
|
|
|
serialize(&Network::Bitcoin.magic()),
|
|
|
|
&[0xf9, 0xbe, 0xb4, 0xd9]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
serialize(&Network::Testnet.magic()),
|
|
|
|
&[0x0b, 0x11, 0x09, 0x07]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
serialize(&Network::Regtest.magic()),
|
|
|
|
&[0xfa, 0xbf, 0xb5, 0xda]
|
|
|
|
);
|
2014-08-03 13:03:00 +00:00
|
|
|
|
2019-05-23 20:28:10 +00:00
|
|
|
assert_eq!(
|
|
|
|
deserialize(&[0xf9, 0xbe, 0xb4, 0xd9]).ok(),
|
|
|
|
Some(Network::Bitcoin.magic())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
deserialize(&[0x0b, 0x11, 0x09, 0x07]).ok(),
|
|
|
|
Some(Network::Testnet.magic())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
deserialize(&[0xfa, 0xbf, 0xb5, 0xda]).ok(),
|
|
|
|
Some(Network::Regtest.magic())
|
|
|
|
);
|
|
|
|
}
|
2018-05-16 09:22:07 +00:00
|
|
|
|
2019-11-22 00:42:15 +00:00
|
|
|
#[test]
|
|
|
|
fn string_test() {
|
|
|
|
assert_eq!(Network::Bitcoin.to_string(), "bitcoin");
|
|
|
|
assert_eq!(Network::Testnet.to_string(), "testnet");
|
|
|
|
assert_eq!(Network::Regtest.to_string(), "regtest");
|
|
|
|
|
|
|
|
assert_eq!("bitcoin".parse::<Network>().unwrap(), Network::Bitcoin);
|
|
|
|
assert_eq!("testnet".parse::<Network>().unwrap(), Network::Testnet);
|
|
|
|
assert_eq!("regtest".parse::<Network>().unwrap(), Network::Regtest);
|
|
|
|
assert!("fakenet".parse::<Network>().is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn service_flags_test() {
|
|
|
|
let all = [
|
|
|
|
ServiceFlags::NETWORK,
|
|
|
|
ServiceFlags::GETUTXO,
|
|
|
|
ServiceFlags::BLOOM,
|
|
|
|
ServiceFlags::WITNESS,
|
2019-12-21 01:39:30 +00:00
|
|
|
ServiceFlags::COMPACT_FILTERS,
|
2019-11-22 00:42:15 +00:00
|
|
|
ServiceFlags::NETWORK_LIMITED,
|
|
|
|
];
|
|
|
|
|
|
|
|
let mut flags = ServiceFlags::NONE;
|
|
|
|
for f in all.iter() {
|
|
|
|
assert!(!flags.has(*f));
|
|
|
|
}
|
|
|
|
|
|
|
|
flags |= ServiceFlags::WITNESS;
|
|
|
|
assert_eq!(flags, ServiceFlags::WITNESS);
|
2018-05-16 09:22:07 +00:00
|
|
|
|
2019-11-22 00:42:15 +00:00
|
|
|
let mut flags2 = flags | ServiceFlags::GETUTXO;
|
|
|
|
for f in all.iter() {
|
|
|
|
assert_eq!(flags2.has(*f), *f == ServiceFlags::WITNESS || *f == ServiceFlags::GETUTXO);
|
|
|
|
}
|
|
|
|
|
|
|
|
flags2 ^= ServiceFlags::WITNESS;
|
|
|
|
assert_eq!(flags2, ServiceFlags::GETUTXO);
|
2019-12-21 01:39:30 +00:00
|
|
|
|
|
|
|
flags2 |= ServiceFlags::COMPACT_FILTERS;
|
|
|
|
flags2 ^= ServiceFlags::GETUTXO;
|
|
|
|
assert_eq!(flags2, ServiceFlags::COMPACT_FILTERS);
|
2019-12-05 20:13:45 +00:00
|
|
|
|
|
|
|
// Test formatting.
|
|
|
|
assert_eq!("ServiceFlags(NONE)", ServiceFlags::NONE.to_string());
|
|
|
|
assert_eq!("ServiceFlags(WITNESS)", ServiceFlags::WITNESS.to_string());
|
|
|
|
let flag = ServiceFlags::WITNESS | ServiceFlags::BLOOM | ServiceFlags::NETWORK;
|
|
|
|
assert_eq!("ServiceFlags(NETWORK|BLOOM|WITNESS)", flag.to_string());
|
|
|
|
let flag = ServiceFlags::WITNESS | 0xf0.into();
|
|
|
|
assert_eq!("ServiceFlags(WITNESS|0xf0)", flag.to_string());
|
2019-11-22 00:42:15 +00:00
|
|
|
}
|
2014-08-03 13:03:00 +00:00
|
|
|
}
|
|
|
|
|