Added regtest

Completed regtest integration to the code

Added tests for regtest

Get rid of panics
This commit is contained in:
Igor Aleksanov 2018-05-17 16:08:02 +03:00
parent 6f3c7e19dd
commit 34e228c699
5 changed files with 29 additions and 9 deletions

View File

@ -121,6 +121,20 @@ pub fn genesis_block(network: Network) -> Block {
txdata: txdata txdata: txdata
} }
} }
Network::Regtest => {
let txdata = vec![bitcoin_genesis_tx()];
Block {
header: BlockHeader {
version: 1,
prev_blockhash: Default::default(),
merkle_root: txdata.merkle_root(),
time: 1296688602,
bits: 0x207fffff,
nonce: 2
},
txdata: txdata
}
}
} }
} }

View File

@ -28,7 +28,9 @@ user_enum! {
#[doc="Classic Bitcoin"] #[doc="Classic Bitcoin"]
Bitcoin <-> "bitcoin", Bitcoin <-> "bitcoin",
#[doc="Bitcoin's testnet"] #[doc="Bitcoin's testnet"]
Testnet <-> "testnet" Testnet <-> "testnet",
#[doc="Bitcoin's regtest"]
Regtest <-> "regtest"
} }
} }
@ -44,7 +46,8 @@ pub const USER_AGENT: &'static str = "bitcoin-rust v0.1";
pub fn magic(network: Network) -> u32 { pub fn magic(network: Network) -> u32 {
match network { match network {
Network::Bitcoin => 0xD9B4BEF9, Network::Bitcoin => 0xD9B4BEF9,
Network::Testnet => 0x0709110B Network::Testnet => 0x0709110B,
Network::Regtest => 0xDAB5BFFA,
// Note: any new entries here must be added to `consensus_decode` below // Note: any new entries here must be added to `consensus_decode` below
} }
} }
@ -63,6 +66,7 @@ impl<D: SimpleDecoder> ConsensusDecodable<D> for Network {
match magic { match magic {
0xD9B4BEF9 => Ok(Network::Bitcoin), 0xD9B4BEF9 => Ok(Network::Bitcoin),
0x0709110B => Ok(Network::Testnet), 0x0709110B => Ok(Network::Testnet),
0xDAB5BFFA => Ok(Network::Regtest),
x => Err(d.error(format!("Unknown network (magic {:x})", x))) x => Err(d.error(format!("Unknown network (magic {:x})", x)))
} }
} }
@ -77,9 +81,11 @@ mod tests {
fn serialize_test() { fn serialize_test() {
assert_eq!(serialize(&Network::Bitcoin).unwrap(), vec![0xf9, 0xbe, 0xb4, 0xd9]); 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::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(&[0xf9, 0xbe, 0xb4, 0xd9]).ok(), Some(Network::Bitcoin));
assert_eq!(deserialize(&[0x0b, 0x11, 0x09, 0x07]).ok(), Some(Network::Testnet)); 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()); let bad: Result<Network, _> = deserialize("fakenet".as_bytes());
assert!(bad.is_err()); assert!(bad.is_err());

View File

@ -162,7 +162,7 @@ impl Address {
fn bech_network (network: Network) -> bitcoin_bech32::constants::Network { fn bech_network (network: Network) -> bitcoin_bech32::constants::Network {
match network { match network {
Network::Bitcoin => bitcoin_bech32::constants::Network::Bitcoin, Network::Bitcoin => bitcoin_bech32::constants::Network::Bitcoin,
Network::Testnet => bitcoin_bech32::constants::Network::Testnet Network::Testnet | Network::Regtest => bitcoin_bech32::constants::Network::Testnet,
} }
} }
@ -206,7 +206,7 @@ impl ToString for Address {
let mut prefixed = [0; 21]; let mut prefixed = [0; 21];
prefixed[0] = match self.network { prefixed[0] = match self.network {
Network::Bitcoin => 0, Network::Bitcoin => 0,
Network::Testnet => 111, Network::Testnet | Network::Regtest => 111,
}; };
prefixed[1..].copy_from_slice(&hash[..]); prefixed[1..].copy_from_slice(&hash[..]);
base58::check_encode_slice(&prefixed[..]) base58::check_encode_slice(&prefixed[..])
@ -215,7 +215,7 @@ impl ToString for Address {
let mut prefixed = [0; 21]; let mut prefixed = [0; 21];
prefixed[0] = match self.network { prefixed[0] = match self.network {
Network::Bitcoin => 0, Network::Bitcoin => 0,
Network::Testnet => 111, Network::Testnet | Network::Regtest => 111,
}; };
prefixed[1..].copy_from_slice(&hash[..]); prefixed[1..].copy_from_slice(&hash[..]);
base58::check_encode_slice(&prefixed[..]) base58::check_encode_slice(&prefixed[..])
@ -224,7 +224,7 @@ impl ToString for Address {
let mut prefixed = [0; 21]; let mut prefixed = [0; 21];
prefixed[0] = match self.network { prefixed[0] = match self.network {
Network::Bitcoin => 5, Network::Bitcoin => 5,
Network::Testnet => 196, Network::Testnet | Network::Regtest => 196,
}; };
prefixed[1..].copy_from_slice(&hash[..]); prefixed[1..].copy_from_slice(&hash[..]);
base58::check_encode_slice(&prefixed[..]) base58::check_encode_slice(&prefixed[..])

View File

@ -344,7 +344,7 @@ impl ToString for ExtendedPrivKey {
let mut ret = [0; 78]; let mut ret = [0; 78];
ret[0..4].copy_from_slice(&match self.network { ret[0..4].copy_from_slice(&match self.network {
Network::Bitcoin => [0x04, 0x88, 0xAD, 0xE4], Network::Bitcoin => [0x04, 0x88, 0xAD, 0xE4],
Network::Testnet => [0x04, 0x35, 0x83, 0x94], Network::Testnet | Network::Regtest => [0x04, 0x35, 0x83, 0x94],
}[..]); }[..]);
ret[4] = self.depth as u8; ret[4] = self.depth as u8;
ret[5..9].copy_from_slice(&self.parent_fingerprint[..]); ret[5..9].copy_from_slice(&self.parent_fingerprint[..]);
@ -402,7 +402,7 @@ impl ToString for ExtendedPubKey {
let mut ret = [0; 78]; let mut ret = [0; 78];
ret[0..4].copy_from_slice(&match self.network { ret[0..4].copy_from_slice(&match self.network {
Network::Bitcoin => [0x04u8, 0x88, 0xB2, 0x1E], Network::Bitcoin => [0x04u8, 0x88, 0xB2, 0x1E],
Network::Testnet => [0x04u8, 0x35, 0x87, 0xCF], Network::Testnet | Network::Regtest => [0x04u8, 0x35, 0x87, 0xCF],
}[..]); }[..]);
ret[4] = self.depth as u8; ret[4] = self.depth as u8;
ret[5..9].copy_from_slice(&self.parent_fingerprint[..]); ret[5..9].copy_from_slice(&self.parent_fingerprint[..]);

View File

@ -96,7 +96,7 @@ impl ToString for Privkey {
let mut ret = [0; 34]; let mut ret = [0; 34];
ret[0] = match self.network { ret[0] = match self.network {
Network::Bitcoin => 128, Network::Bitcoin => 128,
Network::Testnet => 239 Network::Testnet | Network::Regtest => 239,
}; };
ret[1..33].copy_from_slice(&self.key[..]); ret[1..33].copy_from_slice(&self.key[..]);
if self.compressed { if self.compressed {