      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

    //! Bitcoin network
    //! https://www.anintegratedworld.com/unravelling-the-mysterious-block-chain-magic-number/

    use compact::Compact;
    use chain::Block;
    use primitives::hash::H256;
    use primitives::bigint::U256;
    use {ConsensusFork};

    const MAGIC_MAINNET: u32 = 0xD9B4BEF9;
    const MAGIC_TESTNET: u32 = 0x0709110B;
    const MAGIC_REGTEST: u32 = 0xDAB5BFFA;
    const MAGIC_UNITEST: u32 = 0x00000000;

    const BITCOIN_CASH_MAGIC_MAINNET: u32 = 0xE8F3E1E3;
    const BITCOIN_CASH_MAGIC_TESTNET: u32 = 0xF4F3E5F4;
    const BITCOIN_CASH_MAGIC_REGTEST: u32 = 0xFABFB5DA;

    lazy_static! {
    	static ref MAX_BITS_MAINNET: U256 = "00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff".parse()
    		.expect("hardcoded value should parse without errors");
    	static ref MAX_BITS_TESTNET: U256 = "00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff".parse()
    		.expect("hardcoded value should parse without errors");
    	static ref MAX_BITS_REGTEST: U256 = "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff".parse()
    		.expect("hardcoded value should parse without errors");
    }

    /// Network magic type.
    pub type Magic = u32;

    /// Bitcoin [network](https://bitcoin.org/en/glossary/mainnet)
    #[derive(Debug, PartialEq, Eq, Clone, Copy)]
    pub enum Network {
    	/// The original and main network for Bitcoin transactions, where satoshis have real economic value.
    	Mainnet,
    	/// The main bitcoin testnet.
    	Testnet,
    	/// Bitcoin regtest network.
    	Regtest,
    	/// Testnet for unittests, proof of work difficulty is almost 0
    	Unitest,
    	/// Any other network. By default behaves like bitcoin mainnet.
    	Other(u32),
    }

    impl Network {
    	pub fn magic(&self, fork: &ConsensusFork) -> Magic {
    		match (fork, *self) {
    			(&ConsensusFork::BitcoinCash(_), Network::Mainnet) => BITCOIN_CASH_MAGIC_MAINNET,
    			(&ConsensusFork::BitcoinCash(_), Network::Testnet) => BITCOIN_CASH_MAGIC_TESTNET,
    			(&ConsensusFork::BitcoinCash(_), Network::Regtest) => BITCOIN_CASH_MAGIC_REGTEST,
    			(_, Network::Mainnet) => MAGIC_MAINNET,
    			(_, Network::Testnet) => MAGIC_TESTNET,
    			(_, Network::Regtest) => MAGIC_REGTEST,
    			(_, Network::Unitest) => MAGIC_UNITEST,
    			(_, Network::Other(value)) => value,
    		}
    	}

    	pub fn max_bits(&self) -> U256 {
    		match *self {
    			Network::Mainnet | Network::Other(_) => MAX_BITS_MAINNET.clone(),
    			Network::Testnet => MAX_BITS_TESTNET.clone(),
    			Network::Regtest => MAX_BITS_REGTEST.clone(),
    			Network::Unitest => Compact::max_value().into(),
    		}
    	}

    	pub fn port(&self) -> u16 {
    		match *self {
    			Network::Mainnet | Network::Other(_)  => 8333,
    			Network::Testnet => 18333,
    			Network::Regtest | Network::Unitest => 18444,
    		}
    	}

    	pub fn rpc_port(&self) -> u16 {
    		match *self {
    			Network::Mainnet | Network::Other(_) => 8332,
    			Network::Testnet => 18332,
    			Network::Regtest | Network::Unitest => 18443,
    		}
    	}

    	pub fn genesis_block(&self) -> Block {
    		match *self {
    			Network::Mainnet | Network::Other(_) => " ".into(),
    			Network::Testnet => " ".into(),
    			Network::Regtest | Network::Unitest => " ".into(),
    		}
    	}

    	pub fn default_verification_edge(&self) -> H256 {
    		match *self {
    			Network::Mainnet => H256::from_reversed_str("0000000000000000030abc968e1bd635736e880b946085c93152969b9a81a6e2"),
    			Network::Testnet => H256::from_reversed_str("000000000871ee6842d3648317ccc8a435eb8cc3c2429aee94faff9ba26b05a0"),
    			_ => self.genesis_block().hash(),
    		}
    	}
    }

    #[cfg(test)]
    mod tests {
    	use compact::Compact;
    	use {ConsensusFork};
    	use super::{
    		Network, MAGIC_MAINNET, MAGIC_TESTNET, MAGIC_REGTEST, MAGIC_UNITEST,
    		MAX_BITS_MAINNET, MAX_BITS_TESTNET, MAX_BITS_REGTEST,
    	};

    	#[test]
    	fn test_network_magic_number() {
    		assert_eq!(MAGIC_MAINNET, Network::Mainnet.magic(&ConsensusFork::BitcoinCore));
    		assert_eq!(MAGIC_TESTNET, Network::Testnet.magic(&ConsensusFork::BitcoinCore));
    		assert_eq!(MAGIC_REGTEST, Network::Regtest.magic(&ConsensusFork::BitcoinCore));
    		assert_eq!(MAGIC_UNITEST, Network::Unitest.magic(&ConsensusFork::BitcoinCore));
    	}

    	#[test]
    	fn test_network_max_bits() {
    		assert_eq!(Network::Mainnet.max_bits(), *MAX_BITS_MAINNET);
    		assert_eq!(Network::Testnet.max_bits(), *MAX_BITS_TESTNET);
    		assert_eq!(Network::Regtest.max_bits(), *MAX_BITS_REGTEST);
    		assert_eq!(Network::Unitest.max_bits(), Compact::max_value().into());
    	}

    	#[test]
    	fn test_network_port() {
    		assert_eq!(Network::Mainnet.port(), 8333);
    		assert_eq!(Network::Testnet.port(), 18333);
    		assert_eq!(Network::Regtest.port(), 18444);
    		assert_eq!(Network::Unitest.port(), 18444);
    	}

    	#[test]
    	fn test_network_rpc_port() {
    		assert_eq!(Network::Mainnet.rpc_port(), 8332);
    		assert_eq!(Network::Testnet.rpc_port(), 18332);
    		assert_eq!(Network::Regtest.rpc_port(), 18443);
    		assert_eq!(Network::Unitest.rpc_port(), 18443);
    	}
    }

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g. `str,u8` or `String,struct:Vec,test`)