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/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
//! # Network constants
|
|
|
|
//!
|
|
|
|
//! This module provides various constants relating to the Bitcoin network
|
|
|
|
//! protocol, such as protocol versioning and magic header bytes.
|
|
|
|
//!
|
|
|
|
|
2014-08-01 16:01:39 +00:00
|
|
|
use network::encodable::{ConsensusDecodable, ConsensusEncodable};
|
|
|
|
use network::serialize::{SimpleEncoder, SimpleDecoder};
|
2014-07-18 21:38:35 +00:00
|
|
|
|
2015-01-18 18:16:01 +00:00
|
|
|
user_enum! {
|
2015-04-10 23:15:57 +00:00
|
|
|
#[derive(Copy, PartialEq, Eq, Clone, Hash)]
|
2015-04-07 01:51:11 +00:00
|
|
|
#[doc="The cryptocurrency to act on"]
|
|
|
|
pub enum Network {
|
|
|
|
#[doc="Classic Bitcoin"]
|
|
|
|
Bitcoin <-> "bitcoin",
|
|
|
|
#[doc="Bitcoin's testnet"]
|
|
|
|
Testnet <-> "testnet"
|
|
|
|
}
|
2015-01-18 18:16:01 +00:00
|
|
|
}
|
2014-08-03 13:03:00 +00:00
|
|
|
|
2015-09-20 20:27:12 +00:00
|
|
|
/// Version of the protocol as appearing in network message headers
|
2015-01-18 18:16:01 +00:00
|
|
|
pub const PROTOCOL_VERSION: u32 = 70001;
|
2015-09-20 20:27:12 +00:00
|
|
|
/// Bitfield of services provided by this node
|
2015-01-18 18:16:01 +00:00
|
|
|
pub const SERVICES: u64 = 0;
|
2015-09-20 20:27:12 +00:00
|
|
|
/// User agent as it appears in the version message
|
2015-01-18 18:16:01 +00:00
|
|
|
pub const USER_AGENT: &'static str = "bitcoin-rust v0.1";
|
2014-07-18 13:56:17 +00:00
|
|
|
|
2014-07-18 21:38:35 +00:00
|
|
|
/// Return the network magic bytes, which should be encoded little-endian
|
|
|
|
/// at the start of every message
|
|
|
|
pub fn magic(network: Network) -> u32 {
|
2015-04-07 01:51:11 +00:00
|
|
|
match network {
|
|
|
|
Network::Bitcoin => 0xD9B4BEF9,
|
|
|
|
Network::Testnet => 0x0709110B
|
|
|
|
// Note: any new entries here must be added to `consensus_decode` below
|
|
|
|
}
|
2014-07-18 21:38:35 +00:00
|
|
|
}
|
|
|
|
|
2015-04-06 00:10:37 +00:00
|
|
|
impl<S: SimpleEncoder> ConsensusEncodable<S> for Network {
|
2015-04-07 01:51:11 +00:00
|
|
|
#[inline]
|
|
|
|
fn consensus_encode(&self, s: &mut S) -> Result<(), S::Error> {
|
|
|
|
magic(*self).consensus_encode(s)
|
|
|
|
}
|
2014-08-01 16:01:39 +00:00
|
|
|
}
|
2014-07-18 21:38:35 +00:00
|
|
|
|
2015-04-06 00:10:37 +00:00
|
|
|
impl<D: SimpleDecoder> ConsensusDecodable<D> for Network {
|
2015-04-07 01:51:11 +00:00
|
|
|
#[inline]
|
|
|
|
fn consensus_decode(d: &mut D) -> Result<Network, D::Error> {
|
|
|
|
let magic: u32 = try!(ConsensusDecodable::consensus_decode(d));
|
|
|
|
match magic {
|
|
|
|
0xD9B4BEF9 => Ok(Network::Bitcoin),
|
|
|
|
0x0709110B => Ok(Network::Testnet),
|
|
|
|
x => Err(d.error(format!("Unknown network (magic {:x})", x)))
|
|
|
|
}
|
2014-07-18 21:38:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-03 13:03:00 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2015-04-05 17:58:49 +00:00
|
|
|
use super::Network;
|
2014-08-03 13:03:00 +00:00
|
|
|
use network::serialize::{deserialize, serialize};
|
|
|
|
|
2014-08-06 02:08:06 +00:00
|
|
|
#[test]
|
2014-08-03 13:03:00 +00:00
|
|
|
fn serialize_test() {
|
2015-04-05 17:58:49 +00:00
|
|
|
assert_eq!(serialize(&Network::Bitcoin).unwrap(), vec![0xf9, 0xbe, 0xb4, 0xd9]);
|
2015-04-05 19:43:44 +00:00
|
|
|
assert_eq!(serialize(&Network::Testnet).unwrap(), vec![0x0b, 0x11, 0x09, 0x07]);
|
2014-08-03 13:03:00 +00:00
|
|
|
|
2015-04-08 22:23:45 +00:00
|
|
|
assert_eq!(deserialize(&[0xf9, 0xbe, 0xb4, 0xd9]).ok(), Some(Network::Bitcoin));
|
|
|
|
assert_eq!(deserialize(&[0x0b, 0x11, 0x09, 0x07]).ok(), Some(Network::Testnet));
|
2014-08-03 13:03:00 +00:00
|
|
|
|
2015-04-08 22:23:45 +00:00
|
|
|
let bad: Result<Network, _> = deserialize("fakenet".as_bytes());
|
2014-08-03 13:03:00 +00:00
|
|
|
assert!(bad.is_err());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|