rust-bitcoin-unsafe-fast/src/network/constants.rs

87 lines
2.6 KiB
Rust
Raw Normal View History

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.
//!
use network::encodable::{ConsensusDecodable, ConsensusEncodable};
use network::serialize::{SimpleEncoder, SimpleDecoder};
user_enum! {
2015-03-26 15:31:19 +00:00
#[derive(PartialEq, Eq, Clone, Hash)]
#[doc="The cryptocurrency to act on"]
pub enum Network {
#[doc="Classic Bitcoin"]
Bitcoin <-> "bitcoin",
#[doc="Bitcoin's testnet"]
Testnet <-> "testnet"
}
}
pub const PROTOCOL_VERSION: u32 = 70001;
pub const SERVICES: u64 = 0;
pub const USER_AGENT: &'static str = "bitcoin-rust v0.1";
2014-07-18 13:56:17 +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 {
match network {
Network::Bitcoin => 0xD9B4BEF9,
Network::Testnet => 0x0709110B
// Note: any new entries here must be added to `deserialize` below
}
}
impl<S:SimpleEncoder<E>, E> ConsensusEncodable<S, E> for Network {
#[inline]
fn consensus_encode(&self, s: &mut S) -> Result<(), E> {
magic(*self).consensus_encode(s)
}
}
impl<D:SimpleDecoder<E>, E> ConsensusDecodable<D, E> for Network {
#[inline]
fn consensus_decode(d: &mut D) -> Result<Network, E> {
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).as_slice()))
}
}
}
#[cfg(test)]
mod tests {
use super::Network;
use 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!(deserialize(vec![0xf9, 0xbe, 0xb4, 0xd9]), Ok(Network::Bitcoin));
assert_eq!(deserialize(vec![0x0b, 0x11, 0x09, 0x07]), Ok(Network::Testnet));
let bad: Result<Network, _> = deserialize("fakenet".as_bytes().to_vec());
assert!(bad.is_err());
}
}