Checkpoint commit (nope, not yet to small commit sizes :))

This is mostly fixing compile errors in `cargo test`. We are down
to 3 in `cargo build` and 14 in `cargo test`, at least for this
round.
This commit is contained in:
Andrew Poelstra 2015-04-08 17:23:45 -05:00
parent 08a20f8764
commit e5a3e84c41
20 changed files with 235 additions and 238 deletions

View File

@ -14,6 +14,9 @@ git = "https://github.com/apoelstra/bitcoin-secp256k1-rs.git"
[dependencies.eventual]
git = "https://github.com/carllerche/eventual"
[dependencies.hamcrest]
git = "https://github.com/carllerche/eventual"
[dependencies]
byteorder = "*"
num_cpus = "*"
@ -23,3 +26,6 @@ rustc-serialize = "*"
serde = "*"
time = "*"
[dev-dependencies]
hamcrest = { git = "https://github.com/carllerche/hamcrest-rust.git" }

View File

@ -137,7 +137,6 @@ impl_consensus_encoding!(LoneBlockHeader, header, tx_count);
#[cfg(test)]
mod tests {
use std::io;
use serialize::hex::FromHex;
use blockdata::block::Block;
@ -151,22 +150,22 @@ mod tests {
let prevhash = "4ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000".from_hex().unwrap();
let merkle = "bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914c".from_hex().unwrap();
let decode: io::Result<Block> = deserialize(some_block.clone());
let bad_decode: io::Result<Block> = deserialize(cutoff_block);
let decode: Result<Block, _> = deserialize(&some_block);
let bad_decode: Result<Block, _> = deserialize(&cutoff_block);
assert!(decode.is_ok());
assert!(bad_decode.is_err());
let real_decode = decode.unwrap();
assert_eq!(real_decode.header.version, 1);
assert_eq!(serialize(&real_decode.header.prev_blockhash), Ok(prevhash));
assert_eq!(serialize(&real_decode.header.prev_blockhash).ok(), Some(prevhash));
// [test] TODO: actually compute the merkle root
assert_eq!(serialize(&real_decode.header.merkle_root), Ok(merkle));
assert_eq!(serialize(&real_decode.header.merkle_root).ok(), Some(merkle));
assert_eq!(real_decode.header.time, 1231965655);
assert_eq!(real_decode.header.bits, 486604799);
assert_eq!(real_decode.header.nonce, 2067413810);
// [test] TODO: check the transaction data
assert_eq!(serialize(&real_decode), Ok(some_block));
assert_eq!(serialize(&real_decode).ok(), Some(some_block));
}
}

View File

@ -618,8 +618,6 @@ impl Blockchain {
#[cfg(test)]
mod tests {
use std::io;
use blockdata::blockchain::Blockchain;
use blockdata::constants::genesis_block;
use network::constants::Network::Bitcoin;
@ -632,7 +630,7 @@ mod tests {
genesis_block(Bitcoin).header.bitcoin_hash());
let serial = serialize(&empty_chain);
let deserial: io::Result<Blockchain> = deserialize(serial.unwrap());
let deserial: Result<Blockchain, _> = deserialize(&serial.unwrap());
assert!(deserial.is_ok());
let read_chain = deserial.unwrap();

View File

@ -136,13 +136,13 @@ mod test {
assert_eq!(gen.input.len(), 1);
assert_eq!(gen.input[0].prev_hash, Default::default());
assert_eq!(gen.input[0].prev_index, 0xFFFFFFFF);
assert_eq!(serialize(&gen.input[0].script_sig),
Ok("4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73".from_hex().unwrap()));
assert_eq!(serialize(&gen.input[0].script_sig).ok(),
Some("4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73".from_hex().unwrap()));
assert_eq!(gen.input[0].sequence, MAX_SEQUENCE);
assert_eq!(gen.output.len(), 1);
assert_eq!(serialize(&gen.output[0].script_pubkey),
Ok("434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac".from_hex().unwrap()));
assert_eq!(serialize(&gen.output[0].script_pubkey).ok(),
Some("434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac".from_hex().unwrap()));
assert_eq!(gen.output[0].value, 50 * COIN_VALUE);
assert_eq!(gen.lock_time, 0);
@ -152,7 +152,7 @@ mod test {
#[test]
fn bitcoin_genesis_full_block() {
let gen = genesis_block(network::Bitcoin);
let gen = genesis_block(Network::Bitcoin);
assert_eq!(gen.header.version, 1);
assert_eq!(gen.header.prev_blockhash, Default::default());
@ -167,7 +167,7 @@ mod test {
#[test]
fn testnet_genesis_full_block() {
let gen = genesis_block(network::Testnet);
let gen = genesis_block(Network::Testnet);
assert_eq!(gen.header.version, 1);
assert_eq!(gen.header.prev_blockhash, Default::default());
assert_eq!(gen.header.merkle_root.be_hex_string(),

View File

@ -2552,10 +2552,9 @@ impl<D: SimpleDecoder> ConsensusDecodable<D> for Script {
#[cfg(test)]
mod test {
use std::io;
use serialize::hex::FromHex;
use super::{Error, Script, build_scriptint, read_scriptint, read_scriptbool};
use super::{Error, Script, ScriptBuilder, build_scriptint, read_scriptint, read_scriptbool};
use super::MaybeOwned::Owned;
use network::serialize::{deserialize, serialize};
@ -2565,11 +2564,11 @@ mod test {
fn test_tx(tx_hex: &'static str, output_hex: Vec<&'static str>) {
let tx_hex = tx_hex.from_hex().unwrap();
let tx: Transaction = deserialize(tx_hex.clone()).ok().expect("transaction");
let tx: Transaction = deserialize(&tx_hex).ok().expect("transaction");
let script_pk: Vec<Script> = output_hex.iter()
.map(|hex| format!("{:02x}{}", hex.len() / 2, hex))
.map(|hex| (&hex[..]).from_hex().unwrap())
.map(|hex| deserialize(hex.clone())
.map(|hex| deserialize(&hex)
.ok()
.expect("scriptpk"))
.collect();
@ -2579,44 +2578,44 @@ mod test {
assert_eq!(tx.input[n].script_sig.evaluate(&mut stack, Some((&tx, n)), None), Ok(()));
assert_eq!(script.evaluate(&mut stack, Some((&tx, n)), None), Ok(()));
assert!(stack.len() >= 1);
assert_eq!(read_scriptbool(&stack.pop().unwrap()), true);
assert_eq!(read_scriptbool(&stack.pop().unwrap()[..]), true);
}
}
#[test]
fn script() {
let mut comp = vec![];
let mut script = Script::new();
assert_eq!(script, Script(comp.into_boxed_slice()));
let mut script = ScriptBuilder::new();
assert_eq!(&script[..], &comp[..]);
// small ints
script.push_int(1); comp.push(82u8); assert_eq!(script, Script(comp.clone()));
script.push_int(0); comp.push(0u8); assert_eq!(script, Script(comp.clone()));
script.push_int(4); comp.push(85u8); assert_eq!(script, Script(comp.clone()));
script.push_int(-1); comp.push(80u8); assert_eq!(script, Script(comp.clone()));
script.push_int(1); comp.push(82u8); assert_eq!(&script[..], &comp[..]);
script.push_int(0); comp.push(0u8); assert_eq!(&script[..], &comp[..]);
script.push_int(4); comp.push(85u8); assert_eq!(&script[..], &comp[..]);
script.push_int(-1); comp.push(80u8); assert_eq!(&script[..], &comp[..]);
// forced scriptint
script.push_scriptint(4); comp.push_all([1u8, 4]); assert_eq!(script, Script(comp.clone()));
script.push_scriptint(4); comp.push_all(&[1u8, 4]); assert_eq!(&script[..], &comp[..]);
// big ints
script.push_int(17); comp.push_all([1u8, 17]); assert_eq!(script, Script(comp.clone()));
script.push_int(10000); comp.push_all([2u8, 16, 39]); assert_eq!(script, Script(comp.clone()));
script.push_int(17); comp.push_all(&[1u8, 17]); assert_eq!(&script[..], &comp[..]);
script.push_int(10000); comp.push_all(&[2u8, 16, 39]); assert_eq!(&script[..], &comp[..]);
// notice the sign bit set here, hence the extra zero/128 at the end
script.push_int(10000000); comp.push_all([4u8, 128, 150, 152, 0]); assert_eq!(script, Script(comp.clone()));
script.push_int(-10000000); comp.push_all([4u8, 128, 150, 152, 128]); assert_eq!(script, Script(comp.clone()));
script.push_int(10000000); comp.push_all(&[4u8, 128, 150, 152, 0]); assert_eq!(&script[..], &comp[..]);
script.push_int(-10000000); comp.push_all(&[4u8, 128, 150, 152, 128]); assert_eq!(&script[..], &comp[..]);
// data
script.push_slice("NRA4VR".as_bytes()); comp.push_all([6u8, 78, 82, 65, 52, 86, 82]); assert_eq!(script, Script(comp.clone()));
script.push_slice("NRA4VR".as_bytes()); comp.push_all(&[6u8, 78, 82, 65, 52, 86, 82]); assert_eq!(&script[..], &comp[..]);
// opcodes
script.push_opcode(opcodes::all::OP_CHECKSIG); comp.push(0xACu8); assert_eq!(script, Script(comp.clone()));
script.push_opcode(opcodes::all::OP_CHECKSIG); comp.push(0xACu8); assert_eq!(script, Script(comp.clone()));
script.push_opcode(opcodes::All::OP_CHECKSIG); comp.push(0xACu8); assert_eq!(&script[..], &comp[..]);
script.push_opcode(opcodes::All::OP_CHECKSIG); comp.push(0xACu8); assert_eq!(&script[..], &comp[..]);
}
#[test]
fn script_serialize() {
let hex_script = "6c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52".from_hex().unwrap();
let script: io::Result<Script> = deserialize(hex_script.clone());
let script: Result<Script, _> = deserialize(&hex_script);
assert!(script.is_ok());
assert_eq!(serialize(&script.unwrap()), Ok(hex_script));
assert_eq!(serialize(&script.unwrap()).ok(), Some(hex_script));
}
#[test]
@ -2637,17 +2636,17 @@ mod test {
#[test]
fn script_eval_simple() {
let mut script = Script::new();
assert!(script.evaluate(&mut vec![], None, None).is_ok());
let mut script = ScriptBuilder::new();
assert!(script.clone().into_script().evaluate(&mut vec![], None, None).is_ok());
script.push_opcode(opcodes::all::OP_RETURN);
assert!(script.evaluate(&mut vec![], None, None).is_err());
script.push_opcode(opcodes::All::OP_RETURN);
assert!(script.clone().into_script().evaluate(&mut vec![], None, None).is_err());
}
#[test]
fn script_eval_checksig_without_tx() {
let hex_pk = "1976a914e729dea4a3a81108e16376d1cc329c91db58999488ac".from_hex().unwrap();
let script_pk: Script = deserialize(hex_pk.clone()).ok().expect("scriptpk");
let script_pk: Script = deserialize(&hex_pk).ok().expect("scriptpk");
// Should be able to check that the sig is there and pk correct
// before needing a transaction
assert_eq!(script_pk.evaluate(&mut vec![], None, None), Err(Error::PopEmptyStack));
@ -2666,14 +2665,14 @@ mod test {
let output_hex = "1976a914299567077f41bc20059dc21a1eb1ef5a6a43b9c088ac".from_hex().unwrap();
let tx: Transaction = deserialize(tx_hex.clone()).ok().expect("transaction");
let script_pk: Script = deserialize(output_hex.clone()).ok().expect("scriptpk");
let tx: Transaction = deserialize(&tx_hex).ok().expect("transaction");
let script_pk: Script = deserialize(&output_hex).ok().expect("scriptpk");
let mut stack = vec![];
assert_eq!(tx.input[0].script_sig.evaluate(&mut stack, None, None), Ok(()));
assert_eq!(script_pk.evaluate(&mut stack, Some((&tx, 0)), None), Ok(()));
assert_eq!(stack.len(), 1);
assert_eq!(read_scriptbool(&stack.pop().unwrap()), true);
assert_eq!(read_scriptbool(&stack.pop().unwrap()[..]), true);
}

View File

@ -308,22 +308,20 @@ impl_consensus_encoding!(Transaction, version, input, output, lock_time);
mod tests {
use super::{Transaction, TxIn};
use std::io;
use network::serialize::BitcoinHash;
use network::serialize::deserialize;
use util::misc::hex_bytes;
#[test]
fn test_txin() {
let txin: io::Result<TxIn> = deserialize(hex_bytes("a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff").unwrap());
let txin: Result<TxIn, _> = deserialize(&hex_bytes("a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff").unwrap());
assert!(txin.is_ok());
}
#[test]
fn test_transaction() {
let hex_tx = hex_bytes("0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000").unwrap();
let tx: io::Result<Transaction> = deserialize(hex_tx);
let tx: Result<Transaction, _> = deserialize(&hex_tx);
assert!(tx.is_ok());
let realtx = tx.unwrap();
// All these tests aren't really needed because if they fail, the hash check at the end

View File

@ -417,7 +417,6 @@ impl UtxoSet {
#[cfg(test)]
mod tests {
use std::prelude::*;
use std::io;
use serialize::hex::FromHex;
use super::{UtxoSet, ValidationLevel};
@ -430,7 +429,7 @@ mod tests {
fn utxoset_serialize_test() {
let mut empty_set = UtxoSet::new(Bitcoin, 100);
let new_block: Block = deserialize("010000004ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914cd74d6e49ffff001d323b3a7b0201000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0804ffff001d026e04ffffffff0100f2052a0100000043410446ef0102d1ec5240f0d061a4246c1bdef63fc3dbab7733052fbbf0ecd8f41fc26bf049ebb4f9527f374280259e7cfa99c48b0e3f39c51347a19a5819651503a5ac00000000010000000321f75f3139a013f50f315b23b0c9a2b6eac31e2bec98e5891c924664889942260000000049483045022100cb2c6b346a978ab8c61b18b5e9397755cbd17d6eb2fe0083ef32e067fa6c785a02206ce44e613f31d9a6b0517e46f3db1576e9812cc98d159bfdaf759a5014081b5c01ffffffff79cda0945903627c3da1f85fc95d0b8ee3e76ae0cfdc9a65d09744b1f8fc85430000000049483045022047957cdd957cfd0becd642f6b84d82f49b6cb4c51a91f49246908af7c3cfdf4a022100e96b46621f1bffcf5ea5982f88cef651e9354f5791602369bf5a82a6cd61a62501fffffffffe09f5fe3ffbf5ee97a54eb5e5069e9da6b4856ee86fc52938c2f979b0f38e82000000004847304402204165be9a4cbab8049e1af9723b96199bfd3e85f44c6b4c0177e3962686b26073022028f638da23fc003760861ad481ead4099312c60030d4cb57820ce4d33812a5ce01ffffffff01009d966b01000000434104ea1feff861b51fe3f5f8a3b12d0f4712db80e919548a80839fc47c6a21e66d957e9c5d8cd108c7a2d2324bad71f9904ac0ae7336507d785b17a2c115e427a32fac00000000".from_hex().unwrap()).unwrap();
let new_block: Block = deserialize(&"010000004ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914cd74d6e49ffff001d323b3a7b0201000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0804ffff001d026e04ffffffff0100f2052a0100000043410446ef0102d1ec5240f0d061a4246c1bdef63fc3dbab7733052fbbf0ecd8f41fc26bf049ebb4f9527f374280259e7cfa99c48b0e3f39c51347a19a5819651503a5ac00000000010000000321f75f3139a013f50f315b23b0c9a2b6eac31e2bec98e5891c924664889942260000000049483045022100cb2c6b346a978ab8c61b18b5e9397755cbd17d6eb2fe0083ef32e067fa6c785a02206ce44e613f31d9a6b0517e46f3db1576e9812cc98d159bfdaf759a5014081b5c01ffffffff79cda0945903627c3da1f85fc95d0b8ee3e76ae0cfdc9a65d09744b1f8fc85430000000049483045022047957cdd957cfd0becd642f6b84d82f49b6cb4c51a91f49246908af7c3cfdf4a022100e96b46621f1bffcf5ea5982f88cef651e9354f5791602369bf5a82a6cd61a62501fffffffffe09f5fe3ffbf5ee97a54eb5e5069e9da6b4856ee86fc52938c2f979b0f38e82000000004847304402204165be9a4cbab8049e1af9723b96199bfd3e85f44c6b4c0177e3962686b26073022028f638da23fc003760861ad481ead4099312c60030d4cb57820ce4d33812a5ce01ffffffff01009d966b01000000434104ea1feff861b51fe3f5f8a3b12d0f4712db80e919548a80839fc47c6a21e66d957e9c5d8cd108c7a2d2324bad71f9904ac0ae7336507d785b17a2c115e427a32fac00000000".from_hex().unwrap()).unwrap();
// Make sure we can't add the block directly, since we are missing the inputs
assert!(empty_set.update(&new_block, 1, ValidationLevel::Inputs).is_err());
@ -466,7 +465,7 @@ mod tests {
// Serialize/deserialize the resulting UTXO set
let serial = serialize(&empty_set).unwrap();
let deserial: io::Result<UtxoSet> = deserialize(serial.clone());
let deserial: Result<UtxoSet, _> = deserialize(&serial);
assert!(deserial.is_ok());
// Check that all outputs are there
@ -486,7 +485,7 @@ mod tests {
}
}
let deserial_again: io::Result<UtxoSet> = deserialize(serial);
let deserial_again: Result<UtxoSet, _> = deserialize(&serial);
let mut read_again = deserial_again.unwrap();
assert!(read_again.rewind(&new_block));
assert_eq!(read_again.n_utxos(), 0);

View File

@ -44,21 +44,22 @@
#![deny(unused_mut)]
#![warn(missing_doc)]
#[cfg(test)] extern crate hamcrest;
extern crate alloc;
extern crate byteorder;
extern crate collections;
extern crate crypto;
extern crate eventual;
extern crate num_cpus;
extern crate rand;
extern crate rustc_serialize as serialize;
extern crate secp256k1;
extern crate serde;
#[plugin(serde_macros)]
extern crate test;
extern crate time;
extern crate secp256k1;
extern crate crypto;
#[macro_use]
mod internal_macros;
#[macro_use]

View File

@ -97,34 +97,32 @@ impl Eq for Address {}
mod test {
use super::Address;
use std::io;
use network::serialize::{deserialize, serialize};
#[test]
fn serialize_address_test() {
assert_eq!(serialize(&Address {
services: 1,
address: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0x0a, 0, 0, 1],
address: [0, 0, 0, 0, 0, 0xffff, 0x0a00, 0x0001],
port: 8333
}),
Ok(vec![1u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0xff, 0xff, 0x0a, 0, 0, 1, 0x20, 0x8d]));
}).ok(),
Some(vec![1u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0xff, 0xff, 0x0a, 0, 0, 1, 0x20, 0x8d]));
}
#[test]
fn deserialize_address_test() {
let mut addr: io::Result<Address> = deserialize(vec![1u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0xff, 0xff, 0x0a, 0,
0, 1, 0x20, 0x8d]);
let mut addr: Result<Address, _> = deserialize(&[1u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0xff, 0xff, 0x0a, 0,
0, 1, 0x20, 0x8d]);
assert!(addr.is_ok());
let full = addr.unwrap();
assert!(full.services == 1);
assert!(full.address == [0u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0x0a, 0, 0, 1]);
assert!(full.address == [0, 0, 0, 0, 0, 0xffff, 0x0a00, 0x0001]);
assert!(full.port == 8333);
addr = deserialize(vec![1u8, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0x0a, 0, 0, 1]);
addr = deserialize(&[1u8, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0x0a, 0, 0, 1]);
assert!(addr.is_err());
}
}

View File

@ -68,18 +68,20 @@ impl<D: SimpleDecoder> ConsensusDecodable<D> for Network {
#[cfg(test)]
mod tests {
use super::Network;
use network::serialize::{deserialize, serialize};
use hamcrest::is;
#[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));
assert_eq!(deserialize(&[0xf9, 0xbe, 0xb4, 0xd9]).ok(), Some(Network::Bitcoin));
assert_eq!(deserialize(&[0x0b, 0x11, 0x09, 0x07]).ok(), Some(Network::Testnet));
let bad: Result<Network, _> = deserialize("fakenet".as_bytes().to_vec());
let bad: Result<Network, _> = deserialize("fakenet".as_bytes());
assert!(bad.is_err());
}
}

View File

@ -397,167 +397,165 @@ impl<D, K, V, H> ConsensusDecodable<D> for HashMap<K, V, H>
mod tests {
use super::{CheckedData, VarInt};
use std::io;
use network::serialize::{deserialize, serialize};
#[test]
fn serialize_int_test() {
// bool
assert_eq!(serialize(&false), Ok(vec![0u8]));
assert_eq!(serialize(&true), Ok(vec![1u8]));
assert_eq!(serialize(&false).ok(), Some(vec![0u8]));
assert_eq!(serialize(&true).ok(), Some(vec![1u8]));
// u8
assert_eq!(serialize(&1u8), Ok(vec![1u8]));
assert_eq!(serialize(&0u8), Ok(vec![0u8]));
assert_eq!(serialize(&255u8), Ok(vec![255u8]));
assert_eq!(serialize(&1u8).ok(), Some(vec![1u8]));
assert_eq!(serialize(&0u8).ok(), Some(vec![0u8]));
assert_eq!(serialize(&255u8).ok(), Some(vec![255u8]));
// u16
assert_eq!(serialize(&1u16), Ok(vec![1u8, 0]));
assert_eq!(serialize(&256u16), Ok(vec![0u8, 1]));
assert_eq!(serialize(&5000u16), Ok(vec![136u8, 19]));
assert_eq!(serialize(&1u16).ok(), Some(vec![1u8, 0]));
assert_eq!(serialize(&256u16).ok(), Some(vec![0u8, 1]));
assert_eq!(serialize(&5000u16).ok(), Some(vec![136u8, 19]));
// u32
assert_eq!(serialize(&1u32), Ok(vec![1u8, 0, 0, 0]));
assert_eq!(serialize(&256u32), Ok(vec![0u8, 1, 0, 0]));
assert_eq!(serialize(&5000u32), Ok(vec![136u8, 19, 0, 0]));
assert_eq!(serialize(&500000u32), Ok(vec![32u8, 161, 7, 0]));
assert_eq!(serialize(&168430090u32), Ok(vec![10u8, 10, 10, 10]));
assert_eq!(serialize(&1u32).ok(), Some(vec![1u8, 0, 0, 0]));
assert_eq!(serialize(&256u32).ok(), Some(vec![0u8, 1, 0, 0]));
assert_eq!(serialize(&5000u32).ok(), Some(vec![136u8, 19, 0, 0]));
assert_eq!(serialize(&500000u32).ok(), Some(vec![32u8, 161, 7, 0]));
assert_eq!(serialize(&168430090u32).ok(), Some(vec![10u8, 10, 10, 10]));
// TODO: test negative numbers
assert_eq!(serialize(&1i32), Ok(vec![1u8, 0, 0, 0]));
assert_eq!(serialize(&256i32), Ok(vec![0u8, 1, 0, 0]));
assert_eq!(serialize(&5000i32), Ok(vec![136u8, 19, 0, 0]));
assert_eq!(serialize(&500000i32), Ok(vec![32u8, 161, 7, 0]));
assert_eq!(serialize(&168430090i32), Ok(vec![10u8, 10, 10, 10]));
assert_eq!(serialize(&1i32).ok(), Some(vec![1u8, 0, 0, 0]));
assert_eq!(serialize(&256i32).ok(), Some(vec![0u8, 1, 0, 0]));
assert_eq!(serialize(&5000i32).ok(), Some(vec![136u8, 19, 0, 0]));
assert_eq!(serialize(&500000i32).ok(), Some(vec![32u8, 161, 7, 0]));
assert_eq!(serialize(&168430090i32).ok(), Some(vec![10u8, 10, 10, 10]));
// u64
assert_eq!(serialize(&1u64), Ok(vec![1u8, 0, 0, 0, 0, 0, 0, 0]));
assert_eq!(serialize(&256u64), Ok(vec![0u8, 1, 0, 0, 0, 0, 0, 0]));
assert_eq!(serialize(&5000u64), Ok(vec![136u8, 19, 0, 0, 0, 0, 0, 0]));
assert_eq!(serialize(&500000u64), Ok(vec![32u8, 161, 7, 0, 0, 0, 0, 0]));
assert_eq!(serialize(&723401728380766730u64), Ok(vec![10u8, 10, 10, 10, 10, 10, 10, 10]));
assert_eq!(serialize(&1u64).ok(), Some(vec![1u8, 0, 0, 0, 0, 0, 0, 0]));
assert_eq!(serialize(&256u64).ok(), Some(vec![0u8, 1, 0, 0, 0, 0, 0, 0]));
assert_eq!(serialize(&5000u64).ok(), Some(vec![136u8, 19, 0, 0, 0, 0, 0, 0]));
assert_eq!(serialize(&500000u64).ok(), Some(vec![32u8, 161, 7, 0, 0, 0, 0, 0]));
assert_eq!(serialize(&723401728380766730u64).ok(), Some(vec![10u8, 10, 10, 10, 10, 10, 10, 10]));
// TODO: test negative numbers
assert_eq!(serialize(&1i64), Ok(vec![1u8, 0, 0, 0, 0, 0, 0, 0]));
assert_eq!(serialize(&256i64), Ok(vec![0u8, 1, 0, 0, 0, 0, 0, 0]));
assert_eq!(serialize(&5000i64), Ok(vec![136u8, 19, 0, 0, 0, 0, 0, 0]));
assert_eq!(serialize(&500000i64), Ok(vec![32u8, 161, 7, 0, 0, 0, 0, 0]));
assert_eq!(serialize(&723401728380766730i64), Ok(vec![10u8, 10, 10, 10, 10, 10, 10, 10]));
assert_eq!(serialize(&1i64).ok(), Some(vec![1u8, 0, 0, 0, 0, 0, 0, 0]));
assert_eq!(serialize(&256i64).ok(), Some(vec![0u8, 1, 0, 0, 0, 0, 0, 0]));
assert_eq!(serialize(&5000i64).ok(), Some(vec![136u8, 19, 0, 0, 0, 0, 0, 0]));
assert_eq!(serialize(&500000i64).ok(), Some(vec![32u8, 161, 7, 0, 0, 0, 0, 0]));
assert_eq!(serialize(&723401728380766730i64).ok(), Some(vec![10u8, 10, 10, 10, 10, 10, 10, 10]));
}
#[test]
fn serialize_varint_test() {
assert_eq!(serialize(&VarInt(10)), Ok(vec![10u8]));
assert_eq!(serialize(&VarInt(0xFC)), Ok(vec![0xFCu8]));
assert_eq!(serialize(&VarInt(0xFD)), Ok(vec![0xFDu8, 0xFD, 0]));
assert_eq!(serialize(&VarInt(0xFFF)), Ok(vec![0xFDu8, 0xFF, 0xF]));
assert_eq!(serialize(&VarInt(0xF0F0F0F)), Ok(vec![0xFEu8, 0xF, 0xF, 0xF, 0xF]));
assert_eq!(serialize(&VarInt(0xF0F0F0F0F0E0)), Ok(vec![0xFFu8, 0xE0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0, 0]));
assert_eq!(serialize(&VarInt(10)).ok(), Some(vec![10u8]));
assert_eq!(serialize(&VarInt(0xFC)).ok(), Some(vec![0xFCu8]));
assert_eq!(serialize(&VarInt(0xFD)).ok(), Some(vec![0xFDu8, 0xFD, 0]));
assert_eq!(serialize(&VarInt(0xFFF)).ok(), Some(vec![0xFDu8, 0xFF, 0xF]));
assert_eq!(serialize(&VarInt(0xF0F0F0F)).ok(), Some(vec![0xFEu8, 0xF, 0xF, 0xF, 0xF]));
assert_eq!(serialize(&VarInt(0xF0F0F0F0F0E0)).ok(), Some(vec![0xFFu8, 0xE0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0, 0]));
}
#[test]
fn serialize_checkeddata_test() {
let cd = CheckedData(vec![1u8, 2, 3, 4, 5]);
assert_eq!(serialize(&cd), Ok(vec![5, 0, 0, 0, 162, 107, 175, 90, 1, 2, 3, 4, 5]));
assert_eq!(serialize(&cd).ok(), Some(vec![5, 0, 0, 0, 162, 107, 175, 90, 1, 2, 3, 4, 5]));
}
#[test]
fn serialize_vector_test() {
assert_eq!(serialize(&vec![1u8, 2, 3]), Ok(vec![3u8, 1, 2, 3]));
assert_eq!(serialize(&[1u8, 2, 3].as_slice()), Ok(vec![3u8, 1, 2, 3]));
assert_eq!(serialize(&vec![1u8, 2, 3]).ok(), Some(vec![3u8, 1, 2, 3]));
assert_eq!(serialize(&[1u8, 2, 3].as_slice()).ok(), Some(vec![3u8, 1, 2, 3]));
// TODO: test vectors of more interesting objects
}
#[test]
fn serialize_strbuf_test() {
assert_eq!(serialize(&"Andrew".to_string()), Ok(vec![6u8, 0x41, 0x6e, 0x64, 0x72, 0x65, 0x77]));
assert_eq!(serialize(&"Andrew".to_string()).ok(), Some(vec![6u8, 0x41, 0x6e, 0x64, 0x72, 0x65, 0x77]));
}
#[test]
fn serialize_box_test() {
assert_eq!(serialize(&Box::new(1u8)), Ok(vec![1u8]));
assert_eq!(serialize(&Box::new(1u16)), Ok(vec![1u8, 0]));
assert_eq!(serialize(&Box::new(1u64)), Ok(vec![1u8, 0, 0, 0, 0, 0, 0, 0]));
assert_eq!(serialize(&Box::new(1u8)).ok(), Some(vec![1u8]));
assert_eq!(serialize(&Box::new(1u16)).ok(), Some(vec![1u8, 0]));
assert_eq!(serialize(&Box::new(1u64)).ok(), Some(vec![1u8, 0, 0, 0, 0, 0, 0, 0]));
}
#[test]
fn serialize_option_test() {
let none_ser = serialize(&None::<u8>);
let some_ser = serialize(&Some(0xFFu8));
assert_eq!(none_ser, Ok(vec![0]));
assert_eq!(some_ser, Ok(vec![1, 0xFF]));
assert_eq!(none_ser.ok(), Some(vec![0]));
assert_eq!(some_ser.ok(), Some(vec![1, 0xFF]));
}
#[test]
fn deserialize_int_test() {
// bool
assert_eq!(deserialize(vec![58u8, 0]), Ok(true));
assert_eq!(deserialize(vec![58u8]), Ok(true));
assert_eq!(deserialize(vec![1u8]), Ok(true));
assert_eq!(deserialize(vec![0u8]), Ok(false));
assert_eq!(deserialize(vec![0u8, 1]), Ok(false));
assert_eq!(deserialize(&[58u8, 0]).ok(), Some(true));
assert_eq!(deserialize(&[58u8]).ok(), Some(true));
assert_eq!(deserialize(&[1u8]).ok(), Some(true));
assert_eq!(deserialize(&[0u8]).ok(), Some(false));
assert_eq!(deserialize(&[0u8, 1]).ok(), Some(false));
// u8
assert_eq!(deserialize(vec![58u8]), Ok(58u8));
assert_eq!(deserialize(&[58u8]).ok(), Some(58u8));
// u16
assert_eq!(deserialize(vec![0x01u8, 0x02]), Ok(0x0201u16));
assert_eq!(deserialize(vec![0xABu8, 0xCD]), Ok(0xCDABu16));
assert_eq!(deserialize(vec![0xA0u8, 0x0D]), Ok(0xDA0u16));
let failure16: io::Result<u16> = deserialize(vec![1u8]);
assert_eq!(deserialize(&[0x01u8, 0x02]).ok(), Some(0x0201u16));
assert_eq!(deserialize(&[0xABu8, 0xCD]).ok(), Some(0xCDABu16));
assert_eq!(deserialize(&[0xA0u8, 0x0D]).ok(), Some(0xDA0u16));
let failure16: Result<u16, _> = deserialize(&[1u8]);
assert!(failure16.is_err());
// u32
assert_eq!(deserialize(vec![0xABu8, 0xCD, 0, 0]), Ok(0xCDABu32));
assert_eq!(deserialize(vec![0xA0u8, 0x0D, 0xAB, 0xCD]), Ok(0xCDAB0DA0u32));
let failure32: io::Result<u32> = deserialize(vec![1u8, 2, 3]);
assert_eq!(deserialize(&[0xABu8, 0xCD, 0, 0]).ok(), Some(0xCDABu32));
assert_eq!(deserialize(&[0xA0u8, 0x0D, 0xAB, 0xCD]).ok(), Some(0xCDAB0DA0u32));
let failure32: Result<u32, _> = deserialize(&[1u8, 2, 3]);
assert!(failure32.is_err());
// TODO: test negative numbers
assert_eq!(deserialize(vec![0xABu8, 0xCD, 0, 0]), Ok(0xCDABi32));
assert_eq!(deserialize(vec![0xA0u8, 0x0D, 0xAB, 0x2D]), Ok(0x2DAB0DA0i32));
let failurei32: io::Result<i32> = deserialize(vec![1u8, 2, 3]);
assert_eq!(deserialize(&[0xABu8, 0xCD, 0, 0]).ok(), Some(0xCDABi32));
assert_eq!(deserialize(&[0xA0u8, 0x0D, 0xAB, 0x2D]).ok(), Some(0x2DAB0DA0i32));
let failurei32: Result<i32, _> = deserialize(&[1u8, 2, 3]);
assert!(failurei32.is_err());
// u64
assert_eq!(deserialize(vec![0xABu8, 0xCD, 0, 0, 0, 0, 0, 0]), Ok(0xCDABu64));
assert_eq!(deserialize(vec![0xA0u8, 0x0D, 0xAB, 0xCD, 0x99, 0, 0, 0x99]), Ok(0x99000099CDAB0DA0u64));
let failure64: io::Result<u64> = deserialize(vec![1u8, 2, 3, 4, 5, 6, 7]);
assert_eq!(deserialize(&[0xABu8, 0xCD, 0, 0, 0, 0, 0, 0]).ok(), Some(0xCDABu64));
assert_eq!(deserialize(&[0xA0u8, 0x0D, 0xAB, 0xCD, 0x99, 0, 0, 0x99]).ok(), Some(0x99000099CDAB0DA0u64));
let failure64: Result<u64, _> = deserialize(&[1u8, 2, 3, 4, 5, 6, 7]);
assert!(failure64.is_err());
// TODO: test negative numbers
assert_eq!(deserialize(vec![0xABu8, 0xCD, 0, 0, 0, 0, 0, 0]), Ok(0xCDABi64));
assert_eq!(deserialize(vec![0xA0u8, 0x0D, 0xAB, 0xCD, 0x99, 0, 0, 0x99]), Ok(-0x66ffff663254f260i64));
let failurei64: io::Result<i64> = deserialize(vec![1u8, 2, 3, 4, 5, 6, 7]);
assert_eq!(deserialize(&[0xABu8, 0xCD, 0, 0, 0, 0, 0, 0]).ok(), Some(0xCDABi64));
assert_eq!(deserialize(&[0xA0u8, 0x0D, 0xAB, 0xCD, 0x99, 0, 0, 0x99]).ok(), Some(-0x66ffff663254f260i64));
let failurei64: Result<i64, _> = deserialize(&[1u8, 2, 3, 4, 5, 6, 7]);
assert!(failurei64.is_err());
}
#[test]
fn deserialize_vec_test() {
assert_eq!(deserialize(vec![3u8, 2, 3, 4]), Ok(vec![2u8, 3, 4]));
assert_eq!(deserialize(vec![4u8, 2, 3, 4, 5, 6]), Ok(vec![2u8, 3, 4, 5]));
assert_eq!(deserialize(&[3u8, 2, 3, 4]).ok(), Some(vec![2u8, 3, 4]));
assert_eq!(deserialize(&[4u8, 2, 3, 4, 5, 6]).ok(), Some(vec![2u8, 3, 4, 5]));
}
#[test]
fn deserialize_strbuf_test() {
assert_eq!(deserialize(vec![6u8, 0x41, 0x6e, 0x64, 0x72, 0x65, 0x77]), Ok(String::from_str("Andrew")));
assert_eq!(deserialize(&[6u8, 0x41, 0x6e, 0x64, 0x72, 0x65, 0x77]).ok(), Some(String::from_str("Andrew")));
}
#[test]
fn deserialize_checkeddata_test() {
let cd: io::Result<CheckedData> = deserialize(vec![5u8, 0, 0, 0, 162, 107, 175, 90, 1, 2, 3, 4, 5]);
assert_eq!(cd, Ok(CheckedData(vec![1u8, 2, 3, 4, 5])));
let cd: Result<CheckedData, _> = deserialize(&[5u8, 0, 0, 0, 162, 107, 175, 90, 1, 2, 3, 4, 5]);
assert_eq!(cd.ok(), Some(CheckedData(vec![1u8, 2, 3, 4, 5])));
}
#[test]
fn deserialize_option_test() {
let none: io::Result<Option<u8>> = deserialize(vec![0u8]);
let good: io::Result<Option<u8>> = deserialize(vec![1u8, 0xFF]);
let bad: io::Result<Option<u8>> = deserialize(vec![2u8]);
let none: Result<Option<u8>, _> = deserialize(&[0u8]);
let good: Result<Option<u8>, _> = deserialize(&[1u8, 0xFF]);
let bad: Result<Option<u8>, _> = deserialize(&[2u8]);
assert!(bad.is_err());
assert_eq!(none, Ok(None));
assert_eq!(good, Ok(Some(0xFF)));
assert_eq!(none.ok(), Some(None));
assert_eq!(good.ok(), Some(Some(0xFF)));
}
#[test]
fn deserialize_box_test() {
let zero: io::Result<Box<u8>> = deserialize(vec![0u8]);
let one: io::Result<Box<u8>> = deserialize(vec![1u8]);
assert_eq!(zero, Ok(Box::new(0)));
assert_eq!(one, Ok(Box::new(1)));
let zero: Result<Box<u8>, _> = deserialize(&[0u8]);
let one: Result<Box<u8>, _> = deserialize(&[1u8]);
assert_eq!(zero.ok(), Some(Box::new(0)));
assert_eq!(one.ok(), Some(Box::new(1)));
}
}

View File

@ -192,41 +192,39 @@ impl<D: SimpleDecoder<Error=util::Error>> ConsensusDecodable<D> for RawNetworkMe
mod test {
use super::{RawNetworkMessage, NetworkMessage, CommandString};
use std::io::Result;
use network::serialize::{deserialize, serialize};
#[test]
fn serialize_commandstring_test() {
let cs = CommandString(String::from_str("Andrew"));
assert_eq!(serialize(&cs), Ok(vec![0x41u8, 0x6e, 0x64, 0x72, 0x65, 0x77, 0, 0, 0, 0, 0, 0]));
assert_eq!(serialize(&cs).ok(), Some(vec![0x41u8, 0x6e, 0x64, 0x72, 0x65, 0x77, 0, 0, 0, 0, 0, 0]));
}
#[test]
fn deserialize_commandstring_test() {
let cs: io::Result<CommandString> = deserialize(vec![0x41u8, 0x6e, 0x64, 0x72, 0x65, 0x77, 0, 0, 0, 0, 0, 0]);
let cs: Result<CommandString, _> = deserialize(&[0x41u8, 0x6e, 0x64, 0x72, 0x65, 0x77, 0, 0, 0, 0, 0, 0]);
assert!(cs.is_ok());
assert_eq!(cs.unwrap(), CommandString(String::from_str("Andrew")));
let short_cs: io::Result<CommandString> = deserialize(vec![0x41u8, 0x6e, 0x64, 0x72, 0x65, 0x77, 0, 0, 0, 0, 0]);
let short_cs: Result<CommandString, _> = deserialize(&[0x41u8, 0x6e, 0x64, 0x72, 0x65, 0x77, 0, 0, 0, 0, 0]);
assert!(short_cs.is_err());
}
#[test]
fn serialize_verack_test() {
assert_eq!(serialize(&RawNetworkMessage { magic: 0xd9b4bef9, payload: NetworkMessage::Verack }),
Ok(vec![0xf9, 0xbe, 0xb4, 0xd9, 0x76, 0x65, 0x72, 0x61,
0x63, 0x6B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x5d, 0xf6, 0xe0, 0xe2]));
assert_eq!(serialize(&RawNetworkMessage { magic: 0xd9b4bef9, payload: NetworkMessage::Verack }).ok(),
Some(vec![0xf9, 0xbe, 0xb4, 0xd9, 0x76, 0x65, 0x72, 0x61,
0x63, 0x6B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x5d, 0xf6, 0xe0, 0xe2]));
}
#[test]
fn serialize_ping_test() {
assert_eq!(serialize(&RawNetworkMessage { magic: 0xd9b4bef9, payload: NetworkMessage::Ping(100) }),
Ok(vec![0xf9, 0xbe, 0xb4, 0xd9, 0x70, 0x69, 0x6e, 0x67,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x08, 0x00, 0x00, 0x00, 0x24, 0x67, 0xf1, 0x1d,
0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]));
assert_eq!(serialize(&RawNetworkMessage { magic: 0xd9b4bef9, payload: NetworkMessage::Ping(100) }).ok(),
Some(vec![0xf9, 0xbe, 0xb4, 0xd9, 0x70, 0x69, 0x6e, 0x67,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x08, 0x00, 0x00, 0x00, 0x24, 0x67, 0xf1, 0x1d,
0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]));
}
}

View File

@ -130,7 +130,6 @@ impl<D: SimpleDecoder> ConsensusDecodable<D> for Inventory {
mod tests {
use super::{GetHeadersMessage, GetBlocksMessage};
use std::io;
use serialize::hex::FromHex;
use network::serialize::{deserialize, serialize};
@ -141,15 +140,15 @@ mod tests {
let from_sat = "72110100014a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b0000000000000000000000000000000000000000000000000000000000000000".from_hex().unwrap();
let genhash = "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b".from_hex().unwrap();
let decode: io::Result<GetBlocksMessage> = deserialize(from_sat.clone());
let decode: Result<GetBlocksMessage, _> = deserialize(&from_sat);
assert!(decode.is_ok());
let real_decode = decode.unwrap();
assert_eq!(real_decode.version, 70002);
assert_eq!(real_decode.locator_hashes.len(), 1);
assert_eq!(serialize(&real_decode.locator_hashes[0]), Ok(genhash));
assert_eq!(serialize(&real_decode.locator_hashes[0]).ok(), Some(genhash));
assert_eq!(real_decode.stop_hash, Default::default());
assert_eq!(serialize(&real_decode), Ok(from_sat));
assert_eq!(serialize(&real_decode).ok(), Some(from_sat));
}
#[test]
@ -157,15 +156,15 @@ mod tests {
let from_sat = "72110100014a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b0000000000000000000000000000000000000000000000000000000000000000".from_hex().unwrap();
let genhash = "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b".from_hex().unwrap();
let decode: io::Result<GetHeadersMessage> = deserialize(from_sat.clone());
let decode: Result<GetHeadersMessage, _> = deserialize(&from_sat);
assert!(decode.is_ok());
let real_decode = decode.unwrap();
assert_eq!(real_decode.version, 70002);
assert_eq!(real_decode.locator_hashes.len(), 1);
assert_eq!(serialize(&real_decode.locator_hashes[0]), Ok(genhash));
assert_eq!(serialize(&real_decode.locator_hashes[0]).ok(), Some(genhash));
assert_eq!(real_decode.stop_hash, Default::default());
assert_eq!(serialize(&real_decode), Ok(from_sat));
assert_eq!(serialize(&real_decode).ok(), Some(from_sat));
}
}

View File

@ -18,8 +18,6 @@
//! capabilities
//!
use std::io;
use network::constants;
use network::address::Address;
use network::socket::Socket;
@ -81,7 +79,6 @@ impl_consensus_encoding!(VersionMessage, version, services, timestamp,
mod tests {
use super::VersionMessage;
use std::io;
use serialize::hex::FromHex;
use network::serialize::{deserialize, serialize};
@ -91,7 +88,7 @@ mod tests {
// This message is from my satoshi node, morning of May 27 2014
let from_sat = "721101000100000000000000e6e0845300000000010000000000000000000000000000000000ffff0000000000000100000000000000fd87d87eeb4364f22cf54dca59412db7208d47d920cffce83ee8102f5361746f7368693a302e392e39392f2c9f040001".from_hex().unwrap();
let decode: io::Result<VersionMessage> = deserialize(from_sat.clone());
let decode: Result<VersionMessage, _> = deserialize(&from_sat);
assert!(decode.is_ok());
let real_decode = decode.unwrap();
assert_eq!(real_decode.version, 70002);
@ -103,7 +100,7 @@ mod tests {
assert_eq!(real_decode.start_height, 302892);
assert_eq!(real_decode.relay, true);
assert_eq!(serialize(&real_decode), Ok(from_sat));
assert_eq!(serialize(&real_decode).ok(), Some(from_sat));
}
}

View File

@ -16,7 +16,7 @@
use byteorder::{ByteOrder, LittleEndian, WriteBytesExt};
use std::str;
use std::{iter, str};
use util::hash::Sha256dHash;
@ -60,7 +60,7 @@ static BASE58_DIGITS: [Option<u8>; 128] = [
/// Trait for objects which can be read as base58
pub trait FromBase58: Sized {
/// Constructs an object flrom the byte-encoding (base 256)
/// Constructs an object from the byte-encoding (base 256)
/// representation of its base58 format
fn from_base58_layout(data: Vec<u8>) -> Result<Self, Error>;
@ -168,6 +168,11 @@ impl<'a> ToBase58 for &'a [u8] {
fn to_base58(&self) -> String { base58_encode_slice(*self) }
}
impl<'a> ToBase58 for Vec<u8> {
fn base58_layout(&self) -> Vec<u8> { self.clone() }
fn to_base58(&self) -> String { base58_encode_slice(&self[..]) }
}
impl FromBase58 for Vec<u8> {
fn from_base58_layout(data: Vec<u8>) -> Result<Vec<u8>, Error> {
Ok(data)
@ -184,43 +189,43 @@ mod tests {
#[test]
fn test_base58_encode() {
// Basics
assert_eq!([0].as_slice().to_base58().as_slice(), "1");
assert_eq!([1].as_slice().to_base58().as_slice(), "2");
assert_eq!([58].as_slice().to_base58().as_slice(), "21");
assert_eq!([13, 36].as_slice().to_base58().as_slice(), "211");
assert_eq!(&(&[0][..]).to_base58(), "1");
assert_eq!(&(&[1][..]).to_base58(), "2");
assert_eq!(&(&[58][..]).to_base58(), "21");
assert_eq!(&(&[13, 36][..]).to_base58(), "211");
// Leading zeroes
assert_eq!([0, 13, 36].as_slice().to_base58().as_slice(), "1211");
assert_eq!([0, 0, 0, 0, 13, 36].as_slice().to_base58().as_slice(), "1111211");
assert_eq!(&(&[0, 13, 36][..]).to_base58(), "1211");
assert_eq!(&(&[0, 0, 0, 0, 13, 36][..]).to_base58(), "1111211");
// Addresses
assert_eq!("00f8917303bfa8ef24f292e8fa1419b20460ba064d".from_hex().unwrap().to_base58check().as_slice(),
assert_eq!(&"00f8917303bfa8ef24f292e8fa1419b20460ba064d".from_hex().unwrap().to_base58check(),
"1PfJpZsjreyVrqeoAfabrRwwjQyoSQMmHH");
}
#[test]
fn test_base58_decode() {
// Basics
assert_eq!(FromBase58::from_base58("1"), Ok(vec![0u8]));
assert_eq!(FromBase58::from_base58("2"), Ok(vec![1u8]));
assert_eq!(FromBase58::from_base58("21"), Ok(vec![58u8]));
assert_eq!(FromBase58::from_base58("211"), Ok(vec![13u8, 36]));
assert_eq!(FromBase58::from_base58("1").ok(), Some(vec![0u8]));
assert_eq!(FromBase58::from_base58("2").ok(), Some(vec![1u8]));
assert_eq!(FromBase58::from_base58("21").ok(), Some(vec![58u8]));
assert_eq!(FromBase58::from_base58("211").ok(), Some(vec![13u8, 36]));
// Leading zeroes
assert_eq!(FromBase58::from_base58("1211"), Ok(vec![0u8, 13, 36]));
assert_eq!(FromBase58::from_base58("111211"), Ok(vec![0u8, 0, 0, 13, 36]));
assert_eq!(FromBase58::from_base58("1211").ok(), Some(vec![0u8, 13, 36]));
assert_eq!(FromBase58::from_base58("111211").ok(), Some(vec![0u8, 0, 0, 13, 36]));
// Addresses
assert_eq!(FromBase58::from_base58check("1PfJpZsjreyVrqeoAfabrRwwjQyoSQMmHH"),
Ok("00f8917303bfa8ef24f292e8fa1419b20460ba064d".from_hex().unwrap()))
assert_eq!(FromBase58::from_base58check("1PfJpZsjreyVrqeoAfabrRwwjQyoSQMmHH").ok(),
Some("00f8917303bfa8ef24f292e8fa1419b20460ba064d".from_hex().unwrap()))
}
#[test]
fn test_base58_roundtrip() {
let s = "xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs";
let v: Vec<u8> = FromBase58::from_base58check(s).unwrap();
assert_eq!(v.to_base58check().as_slice(), s);
assert_eq!(FromBase58::from_base58check(v.to_base58check().as_slice()), Ok(v));
assert_eq!(&v.to_base58check(), s);
assert_eq!(FromBase58::from_base58check(&v.to_base58check()).ok(), Some(v));
}
}

View File

@ -266,6 +266,7 @@ impl <T: BitcoinHash> MerkleRoot for Vec<T> {
mod tests {
use std::prelude::*;
use std::io::Cursor;
use std::num::FromPrimitive;
use std::str::from_utf8;
use serialize::Encodable;
use serialize::json;
@ -288,7 +289,7 @@ mod tests {
fn test_consenus_encode_roundtrip() {
let hash = Sha256dHash::from_data(&[]);
let serial = serialize(&hash).unwrap();
let deserial = deserialize(serial).unwrap();
let deserial = deserialize(&serial).unwrap();
assert_eq!(hash, deserial);
}
@ -300,8 +301,8 @@ mod tests {
let mut encoder = json::Encoder::new(&mut writer);
assert!(hash.encode(&mut encoder).is_ok());
}
let res = writer.unwrap();
assert_eq!(&res.as_slice(),
let res = writer.into_inner();
assert_eq!(&res[..],
"\"56944c5d3f98413ef45cf54545538103cc9f298e0575820ad3591376e2e0f65d\"".as_bytes());
assert_eq!(json::decode(from_utf8(res.as_slice()).unwrap()), Ok(hash));
}

View File

@ -107,38 +107,38 @@ mod tests {
fn test_script_find_and_remove() {
let mut v = vec![101u8, 102, 103, 104, 102, 103, 104, 102, 103, 104, 105, 106, 107, 108, 109];
assert_eq!(script_find_and_remove(&mut v, []), 0);
assert_eq!(script_find_and_remove(&mut v, [105, 105, 105]), 0);
assert_eq!(script_find_and_remove(&mut v, &[]), 0);
assert_eq!(script_find_and_remove(&mut v, &[105, 105, 105]), 0);
assert_eq!(v, vec![101, 102, 103, 104, 102, 103, 104, 102, 103, 104, 105, 106, 107, 108, 109]);
assert_eq!(script_find_and_remove(&mut v, [105, 106, 107]), 1);
assert_eq!(script_find_and_remove(&mut v, &[105, 106, 107]), 1);
assert_eq!(v, vec![101, 102, 103, 104, 102, 103, 104, 102, 103, 104, 108, 109]);
assert_eq!(script_find_and_remove(&mut v, [104, 108, 109]), 1);
assert_eq!(script_find_and_remove(&mut v, &[104, 108, 109]), 1);
assert_eq!(v, vec![101, 102, 103, 104, 102, 103, 104, 102, 103]);
assert_eq!(script_find_and_remove(&mut v, [101]), 1);
assert_eq!(script_find_and_remove(&mut v, &[101]), 1);
assert_eq!(v, vec![102, 103, 104, 102, 103, 104, 102, 103]);
assert_eq!(script_find_and_remove(&mut v, [102]), 3);
assert_eq!(script_find_and_remove(&mut v, &[102]), 3);
assert_eq!(v, vec![103, 104, 103, 104, 103]);
assert_eq!(script_find_and_remove(&mut v, [103, 104]), 2);
assert_eq!(script_find_and_remove(&mut v, &[103, 104]), 2);
assert_eq!(v, vec![103]);
assert_eq!(script_find_and_remove(&mut v, [105, 105, 5]), 0);
assert_eq!(script_find_and_remove(&mut v, [105]), 0);
assert_eq!(script_find_and_remove(&mut v, [103]), 1);
assert_eq!(script_find_and_remove(&mut v, &[105, 105, 5]), 0);
assert_eq!(script_find_and_remove(&mut v, &[105]), 0);
assert_eq!(script_find_and_remove(&mut v, &[103]), 1);
assert_eq!(v, vec![]);
assert_eq!(script_find_and_remove(&mut v, [105, 105, 5]), 0);
assert_eq!(script_find_and_remove(&mut v, [105]), 0);
assert_eq!(script_find_and_remove(&mut v, &[105, 105, 5]), 0);
assert_eq!(script_find_and_remove(&mut v, &[105]), 0);
}
#[test]
fn test_script_codesep_remove() {
let mut s = vec![33u8, 3, 132, 121, 160, 250, 153, 140, 211, 82, 89, 162, 239, 10, 122, 92, 104, 102, 44, 20, 116, 248, 140, 203, 109, 8, 167, 103, 123, 190, 199, 242, 32, 65, 173, 171, 33, 3, 132, 121, 160, 250, 153, 140, 211, 82, 89, 162, 239, 10, 122, 92, 104, 102, 44, 20, 116, 248, 140, 203, 109, 8, 167, 103, 123, 190, 199, 242, 32, 65, 173, 171, 81];
assert_eq!(script_find_and_remove(&mut s, [171]), 2);
assert_eq!(script_find_and_remove(&mut s, &[171]), 2);
assert_eq!(s, vec![33, 3, 132, 121, 160, 250, 153, 140, 211, 82, 89, 162, 239, 10, 122, 92, 104, 102, 44, 20, 116, 248, 140, 203, 109, 8, 167, 103, 123, 190, 199, 242, 32, 65, 173, 33, 3, 132, 121, 160, 250, 153, 140, 211, 82, 89, 162, 239, 10, 122, 92, 104, 102, 44, 20, 116, 248, 140, 203, 109, 8, 167, 103, 123, 190, 199, 242, 32, 65, 173, 81]);
}

View File

@ -548,8 +548,8 @@ impl<'a, K, V> Iterator for MutItems<'a, K, V> {
#[cfg(test)]
mod tests {
use std::prelude::*;
use std::io;
use std::num::Zero;
use std::num::FromPrimitive;
use network::serialize::{deserialize, serialize};
use util::hash::Sha256dHash;
@ -647,18 +647,18 @@ mod tests {
fn patricia_iter_test() {
let n_elems = 5000;
let mut tree = PatriciaTree::new();
let mut data = Vec::from_elem(n_elems, None);
let mut data = vec![None; n_elems];
// Start by inserting a bunch of stuff
for i in 0..n_elems {
let hash = Sha256dHash::from_data(&[(i / 0x100) as u8, (i % 0x100) as u8]).into_le().low_128();
tree.insert(&hash, 128, i);
*data.get_mut(i) = Some(());
data[i] = Some(());
}
// Iterate over and try to get everything
for n in tree.iter() {
assert!(data[*n].is_some());
*data.get_mut(*n) = None;
data[*n] = None;
}
// Check that we got everything
@ -669,12 +669,12 @@ mod tests {
fn patricia_mut_iter_test() {
let n_elems = 5000;
let mut tree = PatriciaTree::new();
let mut data = Vec::from_elem(n_elems, None);
let mut data = vec![None; n_elems];
// Start by inserting a bunch of stuff
for i in 0..n_elems {
let hash = Sha256dHash::from_data(&[(i / 0x100) as u8, (i % 0x100) as u8]).into_le().low_128();
tree.insert(&hash, 128, i);
*data.get_mut(i) = Some(());
data[i] = Some(());
}
// Iterate over and flip all the values
@ -685,7 +685,7 @@ mod tests {
// Iterate over and try to get everything
for n in tree.mut_iter() {
assert!(data[*n].is_some());
*data.get_mut(*n) = None;
data[*n] = None;
}
// Check that we got everything
@ -706,7 +706,7 @@ mod tests {
// Serialize it
let serialized = serialize(&tree).unwrap();
// Deserialize it
let deserialized: io::Result<PatriciaTree<Uint128, u32>> = deserialize(serialized);
let deserialized: Result<PatriciaTree<Uint128, u32>, _> = deserialize(&serialized);
assert!(deserialized.is_ok());
let new_tree = deserialized.unwrap();

View File

@ -388,7 +388,6 @@ impl Uint256 {
#[cfg(test)]
mod tests {
use std::io;
use std::num::from_u64;
use network::serialize::{deserialize, serialize};
@ -497,11 +496,11 @@ mod tests {
let start2 = Uint256([0x8C8C3EE70C644118u64, 0x0209E7378231E632, 0xABCD, 0xFFFF]);
let serial1 = serialize(&start1).unwrap();
let serial2 = serialize(&start2).unwrap();
let end1: io::Result<Uint256> = deserialize(serial1);
let end2: io::Result<Uint256> = deserialize(serial2);
let end1: Result<Uint256, _> = deserialize(&serial1);
let end2: Result<Uint256, _> = deserialize(&serial2);
assert_eq!(end1, Ok(start1));
assert_eq!(end2, Ok(start2));
assert_eq!(end1.ok(), Some(start1));
assert_eq!(end2.ok(), Some(start2));
}
}

View File

@ -442,32 +442,32 @@ mod tests {
fn test_vector_1() {
let seed = "000102030405060708090a0b0c0d0e0f".from_hex().unwrap();
// m
test_path(Bitcoin, &seed, [],
test_path(Bitcoin, &seed, &[],
"xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi",
"xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8");
// m/0h
test_path(Bitcoin, &seed, [Hardened(0)],
test_path(Bitcoin, &seed, &[Hardened(0)],
"xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7",
"xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw");
// m/0h/1
test_path(Bitcoin, &seed, [Hardened(0), Normal(1)],
test_path(Bitcoin, &seed, &[Hardened(0), Normal(1)],
"xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs",
"xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ");
// m/0h/1/2h
test_path(Bitcoin, &seed, [Hardened(0), Normal(1), Hardened(2)],
test_path(Bitcoin, &seed, &[Hardened(0), Normal(1), Hardened(2)],
"xprv9z4pot5VBttmtdRTWfWQmoH1taj2axGVzFqSb8C9xaxKymcFzXBDptWmT7FwuEzG3ryjH4ktypQSAewRiNMjANTtpgP4mLTj34bhnZX7UiM",
"xpub6D4BDPcP2GT577Vvch3R8wDkScZWzQzMMUm3PWbmWvVJrZwQY4VUNgqFJPMM3No2dFDFGTsxxpG5uJh7n7epu4trkrX7x7DogT5Uv6fcLW5");
// m/0h/1/2h/2
test_path(Bitcoin, &seed, [Hardened(0), Normal(1), Hardened(2), Normal(2)],
test_path(Bitcoin, &seed, &[Hardened(0), Normal(1), Hardened(2), Normal(2)],
"xprvA2JDeKCSNNZky6uBCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334",
"xpub6FHa3pjLCk84BayeJxFW2SP4XRrFd1JYnxeLeU8EqN3vDfZmbqBqaGJAyiLjTAwm6ZLRQUMv1ZACTj37sR62cfN7fe5JnJ7dh8zL4fiyLHV");
// m/0h/1/2h/2/1000000000
test_path(Bitcoin, &seed, [Hardened(0), Normal(1), Hardened(2), Normal(2), Normal(1000000000)],
test_path(Bitcoin, &seed, &[Hardened(0), Normal(1), Hardened(2), Normal(2), Normal(1000000000)],
"xprvA41z7zogVVwxVSgdKUHDy1SKmdb533PjDz7J6N6mV6uS3ze1ai8FHa8kmHScGpWmj4WggLyQjgPie1rFSruoUihUZREPSL39UNdE3BBDu76",
"xpub6H1LXWLaKsWFhvm6RVpEL9P4KfRZSW7abD2ttkWP3SSQvnyA8FSVqNTEcYFgJS2UaFcxupHiYkro49S8yGasTvXEYBVPamhGW6cFJodrTHy");
}
@ -477,32 +477,32 @@ mod tests {
let seed = "fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542".from_hex().unwrap();
// m
test_path(Bitcoin, &seed, [],
test_path(Bitcoin, &seed, &[],
"xprv9s21ZrQH143K31xYSDQpPDxsXRTUcvj2iNHm5NUtrGiGG5e2DtALGdso3pGz6ssrdK4PFmM8NSpSBHNqPqm55Qn3LqFtT2emdEXVYsCzC2U",
"xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB");
// m/0
test_path(Bitcoin, &seed, [Normal(0)],
test_path(Bitcoin, &seed, &[Normal(0)],
"xprv9vHkqa6EV4sPZHYqZznhT2NPtPCjKuDKGY38FBWLvgaDx45zo9WQRUT3dKYnjwih2yJD9mkrocEZXo1ex8G81dwSM1fwqWpWkeS3v86pgKt",
"xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH");
// m/0/2147483647h
test_path(Bitcoin, &seed, [Normal(0), Hardened(2147483647)],
test_path(Bitcoin, &seed, &[Normal(0), Hardened(2147483647)],
"xprv9wSp6B7kry3Vj9m1zSnLvN3xH8RdsPP1Mh7fAaR7aRLcQMKTR2vidYEeEg2mUCTAwCd6vnxVrcjfy2kRgVsFawNzmjuHc2YmYRmagcEPdU9",
"xpub6ASAVgeehLbnwdqV6UKMHVzgqAG8Gr6riv3Fxxpj8ksbH9ebxaEyBLZ85ySDhKiLDBrQSARLq1uNRts8RuJiHjaDMBU4Zn9h8LZNnBC5y4a");
// m/0/2147483647h/1
test_path(Bitcoin, &seed, [Normal(0), Hardened(2147483647), Normal(1)],
test_path(Bitcoin, &seed, &[Normal(0), Hardened(2147483647), Normal(1)],
"xprv9zFnWC6h2cLgpmSA46vutJzBcfJ8yaJGg8cX1e5StJh45BBciYTRXSd25UEPVuesF9yog62tGAQtHjXajPPdbRCHuWS6T8XA2ECKADdw4Ef",
"xpub6DF8uhdarytz3FWdA8TvFSvvAh8dP3283MY7p2V4SeE2wyWmG5mg5EwVvmdMVCQcoNJxGoWaU9DCWh89LojfZ537wTfunKau47EL2dhHKon");
// m/0/2147483647h/1/2147483646h
test_path(Bitcoin, &seed, [Normal(0), Hardened(2147483647), Normal(1), Hardened(2147483646)],
test_path(Bitcoin, &seed, &[Normal(0), Hardened(2147483647), Normal(1), Hardened(2147483646)],
"xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc",
"xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL");
// m/0/2147483647h/1/2147483646h/2
test_path(Bitcoin, &seed, [Normal(0), Hardened(2147483647), Normal(1), Hardened(2147483646), Normal(2)],
test_path(Bitcoin, &seed, &[Normal(0), Hardened(2147483647), Normal(1), Hardened(2147483646), Normal(2)],
"xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j",
"xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt");
}