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