Merge pull request #270 from stevenroose/signed-amount

Add Amount and SignedAmount
This commit is contained in:
Andrew Poelstra 2019-06-07 09:16:02 +00:00 committed by GitHub
commit a6c1eacd70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 1430 additions and 7 deletions

View File

@ -18,7 +18,7 @@ path = "src/lib.rs"
fuzztarget = ["secp256k1/fuzztarget", "bitcoin_hashes/fuzztarget"] fuzztarget = ["secp256k1/fuzztarget", "bitcoin_hashes/fuzztarget"]
serde-decimal = ["use-serde", "strason"] serde-decimal = ["use-serde", "strason"]
unstable = [] unstable = []
use-serde = ["serde", "serde_test", "bitcoin_hashes/serde"] use-serde = ["serde", "bitcoin_hashes/serde"]
[dependencies] [dependencies]
bitcoin-bech32 = "0.9.0" bitcoin-bech32 = "0.9.0"
@ -27,11 +27,9 @@ rand = "0.3"
bitcoin_hashes = "0.3" bitcoin_hashes = "0.3"
bitcoinconsensus = { version = "0.16", optional = true } bitcoinconsensus = { version = "0.16", optional = true }
[dev-dependencies]
tempfile = "3"
[dependencies.serde] [dependencies.serde]
version = "1" version = "1"
features = ["derive"]
optional = true optional = true
[dependencies.serde_test] [dependencies.serde_test]
@ -49,3 +47,9 @@ version = "=0.3.2"
[dependencies.secp256k1] [dependencies.secp256k1]
version = "0.12" version = "0.12"
features = [ "rand" ] features = [ "rand" ]
[dev-dependencies]
serde_derive = "1"
serde_json = "1"
serde_test = "1"
tempfile = "3"

View File

@ -36,6 +36,10 @@ path = "fuzz_targets/deserialize_transaction.rs"
name = "deserialize_address" name = "deserialize_address"
path = "fuzz_targets/deserialize_address.rs" path = "fuzz_targets/deserialize_address.rs"
[[bin]]
name = "deserialize_amount"
path = "fuzz_targets/deserialize_amount.rs"
[[bin]] [[bin]]
name = "deserialize_decimal" name = "deserialize_decimal"
path = "fuzz_targets/deserialize_decimal.rs" path = "fuzz_targets/deserialize_decimal.rs"

View File

@ -0,0 +1,74 @@
extern crate bitcoin;
use std::str::FromStr;
fn do_test(data: &[u8]) {
let data_str = String::from_utf8_lossy(data);
// signed
let samt = match bitcoin::util::amount::SignedAmount::from_str(&data_str) {
Ok(amt) => amt,
Err(_) => return,
};
let samt_roundtrip = match bitcoin::util::amount::SignedAmount::from_str(&samt.to_string()) {
Ok(amt) => amt,
Err(_) => return,
};
assert_eq!(samt, samt_roundtrip);
// unsigned
let amt = match bitcoin::util::amount::Amount::from_str(&data_str) {
Ok(amt) => amt,
Err(_) => return,
};
let amt_roundtrip = match bitcoin::util::amount::Amount::from_str(&amt.to_string()) {
Ok(amt) => amt,
Err(_) => return,
};
assert_eq!(amt, amt_roundtrip);
}
#[cfg(feature = "afl")]
#[macro_use] extern crate afl;
#[cfg(feature = "afl")]
fn main() {
fuzz!(|data| {
do_test(&data);
});
}
#[cfg(feature = "honggfuzz")]
#[macro_use] extern crate honggfuzz;
#[cfg(feature = "honggfuzz")]
fn main() {
loop {
fuzz!(|data| {
do_test(data);
});
}
}
#[cfg(test)]
mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0;
for (idx, c) in hex.as_bytes().iter().enumerate() {
b <<= 4;
match *c {
b'A'...b'F' => b |= c - b'A' + 10,
b'a'...b'f' => b |= c - b'a' + 10,
b'0'...b'9' => b |= c - b'0',
_ => panic!("Bad hex"),
}
if (idx & 1) == 1 {
out.push(b);
b = 0;
}
}
}
#[test]
fn duplicate_crash() {
let mut a = Vec::new();
extend_vec_from_hex("00000000", &mut a);
super::do_test(&a);
}
}

View File

@ -49,8 +49,10 @@ extern crate hex;
extern crate rand; extern crate rand;
extern crate secp256k1; extern crate secp256k1;
#[cfg(feature = "serde")] extern crate serde; #[cfg(feature = "serde")] extern crate serde;
#[cfg(feature = "serde_test")] extern crate serde_test;
#[cfg(feature = "strason")] extern crate strason; #[cfg(feature = "strason")] extern crate strason;
#[cfg(all(test, feature = "serde"))] #[macro_use] extern crate serde_derive; // for 1.22.0 compat
#[cfg(all(test, feature = "serde"))] extern crate serde_json;
#[cfg(all(test, feature = "serde"))] extern crate serde_test;
#[cfg(all(test, feature = "unstable"))] extern crate test; #[cfg(all(test, feature = "unstable"))] extern crate test;
#[cfg(feature="bitcoinconsensus")] extern crate bitcoinconsensus; #[cfg(feature="bitcoinconsensus")] extern crate bitcoinconsensus;
@ -77,6 +79,8 @@ pub use consensus::encode::VarInt;
pub use network::constants::Network; pub use network::constants::Network;
pub use util::Error; pub use util::Error;
pub use util::address::Address; pub use util::address::Address;
pub use util::amount::Amount;
pub use util::amount::SignedAmount;
pub use util::hash::BitcoinHash; pub use util::hash::BitcoinHash;
pub use util::key::PrivateKey; pub use util::key::PrivateKey;
pub use util::key::PublicKey; pub use util::key::PublicKey;

1336
src/util/amount.rs Normal file

File diff suppressed because it is too large Load Diff

View File

@ -204,7 +204,7 @@ mod tests {
assert_eq!(script_find_and_remove(&mut v, &[105, 105, 5]), 0); 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]), 0);
assert_eq!(script_find_and_remove(&mut v, &[103]), 1); assert_eq!(script_find_and_remove(&mut v, &[103]), 1);
assert_eq!(v, vec![]); assert_eq!(v, Vec::<u8>::new());
assert_eq!(script_find_and_remove(&mut v, &[105, 105, 5]), 0); 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]), 0);

View File

@ -18,6 +18,7 @@
pub mod key; pub mod key;
pub mod address; pub mod address;
pub mod amount;
pub mod base58; pub mod base58;
pub mod bip32; pub mod bip32;
pub mod bip143; pub mod bip143;

View File

@ -116,7 +116,7 @@ impl<D: Decoder> Decodable<D> for PartiallySignedTransaction {
return Err(Error::InvalidMagic.into()); return Err(Error::InvalidMagic.into());
} }
if 0xff_u8 != Decodable::consensus_decode(d)? { if 0xff_u8 != u8::consensus_decode(d)? {
return Err(Error::InvalidSeparator.into()); return Err(Error::InvalidSeparator.into());
} }