]> git.bitcoin.ninja Git - rust-lightning/blob - src/util/transaction_utils.rs
4f5018f60bb757a97f02fd0f2d16ce6f0280a76c
[rust-lightning] / src / util / transaction_utils.rs
1 use bitcoin::blockdata::transaction::{TxIn, TxOut};
2
3 use std::cmp::Ordering;
4
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 {
8                         Ordering::Less
9                 } else if b.0.value < a.0.value {
10                         Ordering::Greater
11                 } else if a.0.script_pubkey[..] < b.0.script_pubkey[..] {
12                         Ordering::Less
13                 } else if b.0.script_pubkey[..] < a.0.script_pubkey[..] {
14                         Ordering::Greater
15                 } else {
16                         Ordering::Equal
17                 }
18         });
19 }
20
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() {
25             Ordering::Less
26         } else if b.0.prev_hash.into_le() < a.0.prev_hash.into_le() {
27             Ordering::Greater
28         } else if a.0.prev_index < b.0.prev_index {
29             Ordering::Less
30         } else if b.0.prev_index < a.0.prev_index {
31             Ordering::Greater
32         } else {
33             Ordering::Equal
34         }
35     });
36 }
37
38
39 #[cfg(test)]
40 mod tests {
41     use super::*;
42
43     use bitcoin::blockdata::opcodes;
44     use bitcoin::blockdata::script::Builder;
45     use bitcoin::blockdata::transaction::TxOut;
46
47     #[test]
48     fn sort_output_by_value() {
49         let txout1 = TxOut {
50             value:  100,
51             script_pubkey: Builder::new().push_int(0).into_script()
52         };
53         let txout1_ = txout1.clone();
54
55         let txout2 = TxOut {
56             value: 99,
57             script_pubkey: Builder::new().push_int(0).into_script()
58         };
59         let txout2_ = txout2.clone();
60
61         let mut outputs = vec![(txout1, "ignore"), (txout2, "ignore")];
62         sort_outputs(&mut outputs);
63
64         assert_eq!(
65             &outputs,
66             &vec![(txout2_, "ignore"), (txout1_, "ignore")]
67         );
68     }
69
70     #[test]
71     fn sort_output_by_script_pubkey() {
72         let txout1 = TxOut {
73             value:  100,
74             script_pubkey: Builder::new().push_int(3).into_script(),
75         };
76         let txout1_ = txout1.clone();
77
78         let txout2 = TxOut {
79             value: 100,
80             script_pubkey: Builder::new().push_int(1).push_int(2).into_script()
81         };
82         let txout2_ = txout2.clone();
83
84         let mut outputs = vec![(txout1, "ignore"), (txout2, "ignore")];
85         sort_outputs(&mut outputs);
86
87         assert_eq!(
88             &outputs,
89             &vec![(txout2_, "ignore"), (txout1_, "ignore")]
90         );
91     }
92 }