1 // This file is Copyright its original authors, visible in version control
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
10 use bitcoin::blockdata::transaction::TxOut;
12 use std::cmp::Ordering;
14 pub fn sort_outputs<T, C : Fn(&T, &T) -> Ordering>(outputs: &mut Vec<(TxOut, T)>, tie_breaker: C) {
15 outputs.sort_unstable_by(|a, b| {
16 a.0.value.cmp(&b.0.value).then_with(|| {
17 a.0.script_pubkey[..].cmp(&b.0.script_pubkey[..]).then_with(|| {
18 tie_breaker(&a.1, &b.1)
28 use bitcoin::blockdata::script::{Script, Builder};
29 use bitcoin::blockdata::transaction::TxOut;
34 fn sort_output_by_value() {
37 script_pubkey: Builder::new().push_int(0).into_script()
39 let txout1_ = txout1.clone();
43 script_pubkey: Builder::new().push_int(0).into_script()
45 let txout2_ = txout2.clone();
47 let mut outputs = vec![(txout1, "ignore"), (txout2, "ignore")];
48 sort_outputs(&mut outputs, |_, _| { unreachable!(); });
52 &vec![(txout2_, "ignore"), (txout1_, "ignore")]
57 fn sort_output_by_script_pubkey() {
60 script_pubkey: Builder::new().push_int(3).into_script(),
62 let txout1_ = txout1.clone();
66 script_pubkey: Builder::new().push_int(1).push_int(2).into_script()
68 let txout2_ = txout2.clone();
70 let mut outputs = vec![(txout1, "ignore"), (txout2, "ignore")];
71 sort_outputs(&mut outputs, |_, _| { unreachable!(); });
75 &vec![(txout2_, "ignore"), (txout1_, "ignore")]
80 fn sort_output_by_bip_test() {
83 script_pubkey: script_from_hex("41046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac")
85 let txout1_ = txout1.clone();
87 // doesn't deserialize cleanly:
90 script_pubkey: script_from_hex("41044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad769f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875a390f67c1f6c94cfc617c0ea45afac")
92 let txout2_ = txout2.clone();
94 let mut outputs = vec![(txout1, "ignore"), (txout2, "ignore")];
95 sort_outputs(&mut outputs, |_, _| { unreachable!(); });
97 assert_eq!(&outputs, &vec![(txout1_, "ignore"), (txout2_, "ignore")]);
101 fn sort_output_tie_breaker_test() {
104 script_pubkey: Builder::new().push_int(1).push_int(2).into_script()
106 let txout1_ = txout1.clone();
108 let txout2 = txout1.clone();
109 let txout2_ = txout1.clone();
111 let mut outputs = vec![(txout1, 420), (txout2, 69)];
112 sort_outputs(&mut outputs, |a, b| { a.cmp(b) });
116 &vec![(txout2_, 69), (txout1_, 420)]
120 fn script_from_hex(hex_str: &str) -> Script {
121 Script::from(decode(hex_str).unwrap())
124 macro_rules! bip_txout_tests {
125 ($($name:ident: $value:expr,)*) => {
129 let expected_raw: Vec<(u64, &str)> = $value;
130 let expected: Vec<(TxOut, &str)> = expected_raw.iter()
131 .map(|txout_raw| TxOut {
133 script_pubkey: script_from_hex(txout_raw.1)
134 }).map(|txout| (txout, "ignore"))
137 let mut outputs = expected.clone();
138 outputs.reverse(); // prep it
140 // actually do the work!
141 sort_outputs(&mut outputs, |_, _| { unreachable!(); });
143 assert_eq!(outputs, expected);
149 const TXOUT1: [(u64, &str); 2] = [
150 (400057456, "76a9144a5fba237213a062f6f57978f796390bdcf8d01588ac"),
151 (40000000000, "76a9145be32612930b8323add2212a4ec03c1562084f8488ac"),
153 const TXOUT2: [(u64, &str); 2] = [
154 (100000000, "41046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac"),
155 (2400000000, "41044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad769f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875a390f67c1f6c94cfc617c0ea45afac"),
158 bip69_txout_test_1: TXOUT1.to_vec(),
159 bip69_txout_test_2: TXOUT2.to_vec(),