1 use bitcoin::blockdata::transaction::{TxIn, TxOut};
3 use std::cmp::Ordering;
5 pub fn sort_outputs<T>(outputs: &mut Vec<(TxOut, T)>) {
6 outputs.sort_unstable_by(|a, b| {
7 if a.0.value < b.0.value {
9 } else if b.0.value < a.0.value {
11 } else if a.0.script_pubkey[..] < b.0.script_pubkey[..] {
13 } else if b.0.script_pubkey[..] < a.0.script_pubkey[..] {
21 // TODO savil. Add tests.
22 pub fn sort_inputs<T>(inputs: &mut Vec<(TxIn, T)>) {
23 inputs.sort_unstable_by(|a, b| {
24 if a.0.prev_hash.into_le() < b.0.prev_hash.into_le() {
26 } else if b.0.prev_hash.into_le() < a.0.prev_hash.into_le() {
28 } else if a.0.prev_index < b.0.prev_index {
30 } else if b.0.prev_index < a.0.prev_index {
43 use bitcoin::blockdata::opcodes;
44 use bitcoin::blockdata::script::Builder;
45 use bitcoin::blockdata::transaction::TxOut;
48 fn sort_output_by_value() {
51 script_pubkey: Builder::new().push_int(0).into_script()
53 let txout1_ = txout1.clone();
57 script_pubkey: Builder::new().push_int(0).into_script()
59 let txout2_ = txout2.clone();
61 let mut outputs = vec![(txout1, "ignore"), (txout2, "ignore")];
62 sort_outputs(&mut outputs);
66 &vec![(txout2_, "ignore"), (txout1_, "ignore")]
71 fn sort_output_by_script_pubkey() {
74 script_pubkey: Builder::new().push_int(3).into_script(),
76 let txout1_ = txout1.clone();
80 script_pubkey: Builder::new().push_int(1).push_int(2).into_script()
82 let txout2_ = txout2.clone();
84 let mut outputs = vec![(txout1, "ignore"), (txout2, "ignore")];
85 sort_outputs(&mut outputs);
89 &vec![(txout2_, "ignore"), (txout1_, "ignore")]