stacks_common/deps_common/bitcoin/network/constants.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
// 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/>.
//
//! Network constants
//!
//! This module provides various constants relating to the Bitcoin network
//! protocol, such as protocol versioning and magic header bytes.
//!
//! The [`Network`][1] type implements the [`ConsensusDecodable`][2] and
//! [`ConsensusEncodable`][3] and encodes the magic bytes of the given
//! network
//!
//! [1]: enum.Network.html
//! [2]: ../encodable/trait.ConsensusDecodable.html
//! [3]: ../encodable/trait.ConsensusEncodable.html
//!
//! # Example: encoding a network's magic bytes
//!
//! ```rust
//! use stacks_common::deps_common::bitcoin::network::constants::Network;
//! use stacks_common::deps_common::bitcoin::network::serialize::serialize;
//!
//! let network = Network::Bitcoin;
//! let bytes = serialize(&network).unwrap();
//!
//! assert_eq!(&bytes[..], &[0xF9, 0xBE, 0xB4, 0xD9]);
//! ```
use crate::deps_common::bitcoin::network::encodable::{ConsensusDecodable, ConsensusEncodable};
use crate::deps_common::bitcoin::network::serialize::{self, SimpleDecoder, SimpleEncoder};
/// Version of the protocol as appearing in network message headers
pub const PROTOCOL_VERSION: u32 = 70001;
/// Bitfield of services provided by this node
pub const SERVICES: u64 = 0;
/// User agent as it appears in the version message
pub const USER_AGENT: &str = "bitcoin-rust v0.1";
user_enum! {
/// The cryptocurrency to act on
#[derive(Copy, PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
pub enum Network {
/// Classic Bitcoin
Bitcoin <-> "bitcoin",
/// Bitcoin's testnet
Testnet <-> "testnet",
/// Bitcoin's regtest
Regtest <-> "regtest"
}
}
impl Network {
/// Creates a `Network` from the magic bytes.
///
/// # Examples
///
/// ```rust
/// use stacks_common::deps_common::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,
}
}
/// Return the network magic bytes, which should be encoded little-endian
/// at the start of every message
///
/// # Examples
///
/// ```rust
/// use stacks_common::deps_common::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,
}
}
}
impl<S: SimpleEncoder> ConsensusEncodable<S> for Network {
/// Encodes the magic bytes of `Network`.
#[inline]
fn consensus_encode(&self, s: &mut S) -> Result<(), serialize::Error> {
self.magic().consensus_encode(s)
}
}
impl<D: SimpleDecoder> ConsensusDecodable<D> for Network {
/// Decodes the magic bytes of `Network`.
#[inline]
fn consensus_decode(d: &mut D) -> Result<Network, serialize::Error> {
u32::consensus_decode(d)
.and_then(|m| Network::from_magic(m).ok_or(serialize::Error::UnknownNetworkMagic(m)))
}
}
#[cfg(test)]
mod tests {
use super::Network;
use crate::deps_common::bitcoin::network::serialize::{deserialize, serialize};
#[test]
fn serialize_test() {
assert_eq!(
serialize(&Network::Bitcoin).unwrap(),
vec![0xf9, 0xbe, 0xb4, 0xd9]
);
assert_eq!(
serialize(&Network::Testnet).unwrap(),
vec![0x0b, 0x11, 0x09, 0x07]
);
assert_eq!(
serialize(&Network::Regtest).unwrap(),
vec![0xfa, 0xbf, 0xb5, 0xda]
);
assert_eq!(
deserialize(&[0xf9, 0xbe, 0xb4, 0xd9]).ok(),
Some(Network::Bitcoin)
);
assert_eq!(
deserialize(&[0x0b, 0x11, 0x09, 0x07]).ok(),
Some(Network::Testnet)
);
assert_eq!(
deserialize(&[0xfa, 0xbf, 0xb5, 0xda]).ok(),
Some(Network::Regtest)
);
let bad: Result<Network, _> = deserialize("fakenet".as_bytes());
assert!(bad.is_err());
}
#[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());
}
}