Add serde impls for all Transaction types

Bump version number; introduce rust-jsonrpc dependency
This commit is contained in:
Andrew Poelstra 2016-02-10 03:37:24 +00:00
parent 56b7e7d3f4
commit b88d04f0bc
4 changed files with 21 additions and 3 deletions

View File

@ -1,7 +1,7 @@
[package]
name = "bitcoin"
version = "0.5.2"
version = "0.5.3"
authors = ["Andrew Poelstra <apoelstra@wpsoftware.net>"]
license = "CC0-1.0"
homepage = "https://github.com/apoelstra/rust-bitcoin/"
@ -17,6 +17,7 @@ path = "src/lib.rs"
[dependencies]
byteorder = "0.3"
jsonrpc = "0.7" # for serde macros
num = "0.1"
num_cpus = "0.2"
rand = "0.3"

View File

@ -40,6 +40,7 @@ pub struct TxOutRef {
/// The index of the referenced output in its transaction's vout
pub index: usize
}
serde_struct_impl!(TxOutRef, txid, index);
impl fmt::Display for TxOutRef {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -63,6 +64,7 @@ pub struct TxIn {
/// the miner behaviour cannot be enforced.
pub sequence: u32,
}
serde_struct_impl!(TxIn, prev_hash, prev_index, script_sig, sequence);
/// A transaction output, which defines new coins to be created from old ones.
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
@ -72,6 +74,7 @@ pub struct TxOut {
/// The script which must satisfy for the output to be spent
pub script_pubkey: Script
}
serde_struct_impl!(TxOut, value, script_pubkey);
// This is used as a "null txout" in consensus signing code
impl Default for TxOut {
@ -93,6 +96,7 @@ pub struct Transaction {
/// List of outputs
pub output: Vec<TxOut>
}
serde_struct_impl!(Transaction, version, lock_time, input, output);
impl Transaction {
/// Computes a "normalized TXID" which does not include any signatures.
@ -170,6 +174,8 @@ impl_consensus_encoding!(Transaction, version, input, output, lock_time);
#[cfg(test)]
mod tests {
use strason;
use super::{Transaction, TxIn};
use blockdata::script::Script;
@ -219,5 +225,17 @@ mod tests {
tx.output[0].script_pubkey = Script::new();
assert!(old_ntxid != tx.ntxid());
}
#[test]
fn test_txn_encode_decode() {
let hex_tx = hex_bytes("0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000").unwrap();
let tx: Transaction = deserialize(&hex_tx).unwrap();
let encoded = strason::from_serialize(&tx).unwrap();
assert_eq!(encoded.to_bytes(),
"\"56944c5d3f98413ef45cf54545538103cc9f298e0575820ad3591376e2e0f65d\"".as_bytes());
let decoded = encoded.into_deserialize().unwrap();
assert_eq!(tx, decoded);
}
}

View File

@ -39,6 +39,7 @@
extern crate byteorder;
extern crate crypto;
#[macro_use] extern crate jsonrpc;
extern crate num;
extern crate num_cpus;
extern crate rand;

View File

@ -378,8 +378,6 @@ mod tests {
fn test_hash_encode_decode() {
let hash = Sha256dHash::from_data(&[]);
let encoded = strason::from_serialize(&hash).unwrap();
assert_eq!(encoded.to_bytes(),
"\"56944c5d3f98413ef45cf54545538103cc9f298e0575820ad3591376e2e0f65d\"".as_bytes());
let decoded = encoded.into_deserialize().unwrap();
assert_eq!(hash, decoded);
}