Relicense as dual Apache-2.0 + MIT
[rust-lightning] / lightning / src / util / transaction_utils.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
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
8 // licenses.
9
10 use bitcoin::blockdata::transaction::TxOut;
11
12 use std::cmp::Ordering;
13
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)
19                         })
20                 })
21         });
22 }
23
24 #[cfg(test)]
25 mod tests {
26         use super::*;
27
28         use bitcoin::blockdata::script::{Script, Builder};
29         use bitcoin::blockdata::transaction::TxOut;
30
31         use hex::decode;
32
33         #[test]
34         fn sort_output_by_value() {
35                 let txout1 = TxOut {
36                         value:  100,
37                         script_pubkey: Builder::new().push_int(0).into_script()
38                 };
39                 let txout1_ = txout1.clone();
40
41                 let txout2 = TxOut {
42                         value: 99,
43                         script_pubkey: Builder::new().push_int(0).into_script()
44                 };
45                 let txout2_ = txout2.clone();
46
47                 let mut outputs = vec![(txout1, "ignore"), (txout2, "ignore")];
48                 sort_outputs(&mut outputs, |_, _| { unreachable!(); });
49
50                 assert_eq!(
51                         &outputs,
52                         &vec![(txout2_, "ignore"), (txout1_, "ignore")]
53                         );
54         }
55
56         #[test]
57         fn sort_output_by_script_pubkey() {
58                 let txout1 = TxOut {
59                         value:  100,
60                         script_pubkey: Builder::new().push_int(3).into_script(),
61                 };
62                 let txout1_ = txout1.clone();
63
64                 let txout2 = TxOut {
65                         value: 100,
66                         script_pubkey: Builder::new().push_int(1).push_int(2).into_script()
67                 };
68                 let txout2_ = txout2.clone();
69
70                 let mut outputs = vec![(txout1, "ignore"), (txout2, "ignore")];
71                 sort_outputs(&mut outputs, |_, _| { unreachable!(); });
72
73                 assert_eq!(
74                         &outputs,
75                         &vec![(txout2_, "ignore"), (txout1_, "ignore")]
76                         );
77         }
78
79         #[test]
80         fn sort_output_by_bip_test() {
81                 let txout1 = TxOut {
82                         value: 100000000,
83                         script_pubkey: script_from_hex("41046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac")
84                 };
85                 let txout1_ = txout1.clone();
86
87                 // doesn't deserialize cleanly:
88                 let txout2 = TxOut {
89                         value: 2400000000,
90                         script_pubkey: script_from_hex("41044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad769f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875a390f67c1f6c94cfc617c0ea45afac")
91                 };
92                 let txout2_ = txout2.clone();
93
94                 let mut outputs = vec![(txout1, "ignore"), (txout2, "ignore")];
95                 sort_outputs(&mut outputs, |_, _| { unreachable!(); });
96
97                 assert_eq!(&outputs, &vec![(txout1_, "ignore"), (txout2_, "ignore")]);
98         }
99
100         #[test]
101         fn sort_output_tie_breaker_test() {
102                 let txout1 = TxOut {
103                         value:  100,
104                         script_pubkey: Builder::new().push_int(1).push_int(2).into_script()
105                 };
106                 let txout1_ = txout1.clone();
107
108                 let txout2 = txout1.clone();
109                 let txout2_ = txout1.clone();
110
111                 let mut outputs = vec![(txout1, 420), (txout2, 69)];
112                 sort_outputs(&mut outputs, |a, b| { a.cmp(b) });
113
114                 assert_eq!(
115                         &outputs,
116                         &vec![(txout2_, 69), (txout1_, 420)]
117                 );
118         }
119
120         fn script_from_hex(hex_str: &str) -> Script {
121                 Script::from(decode(hex_str).unwrap())
122         }
123
124         macro_rules! bip_txout_tests {
125                 ($($name:ident: $value:expr,)*) => {
126                         $(
127                                 #[test]
128                                 fn $name() {
129                                         let expected_raw: Vec<(u64, &str)> = $value;
130                                         let expected: Vec<(TxOut, &str)> = expected_raw.iter()
131                                                 .map(|txout_raw| TxOut {
132                                                         value: txout_raw.0,
133                                                         script_pubkey: script_from_hex(txout_raw.1)
134                                                 }).map(|txout| (txout, "ignore"))
135                                         .collect();
136
137                                         let mut outputs = expected.clone();
138                                         outputs.reverse(); // prep it
139
140                                         // actually do the work!
141                                         sort_outputs(&mut outputs, |_, _| { unreachable!(); });
142
143                                         assert_eq!(outputs, expected);
144                                 }
145                         )*
146                 }
147         }
148
149         const TXOUT1: [(u64, &str); 2] = [
150                 (400057456, "76a9144a5fba237213a062f6f57978f796390bdcf8d01588ac"),
151                 (40000000000, "76a9145be32612930b8323add2212a4ec03c1562084f8488ac"),
152         ];
153         const TXOUT2: [(u64, &str); 2] = [
154                 (100000000, "41046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac"),
155                 (2400000000, "41044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad769f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875a390f67c1f6c94cfc617c0ea45afac"),
156         ];
157         bip_txout_tests! {
158                 bip69_txout_test_1: TXOUT1.to_vec(),
159                 bip69_txout_test_2: TXOUT2.to_vec(),
160         }
161 }