}, None));
}
- transaction_utils::sort_outputs(&mut txouts);
+ transaction_utils::sort_outputs(&mut txouts, |a, b| {
+ if let &Some(ref a_htlc) = a {
+ if let &Some(ref b_htlc) = b {
+ a_htlc.0.cltv_expiry.cmp(&b_htlc.0.cltv_expiry)
+ // Note that due to hash collisions, we have to have a fallback comparison
+ // here for fuzztarget mode (otherwise at least chanmon_fail_consistency
+ // may fail)!
+ .then(a_htlc.0.payment_hash.0.cmp(&b_htlc.0.payment_hash.0))
+ // For non-HTLC outputs, if they're copying our SPK we don't really care if we
+ // close the channel due to mismatches - they're doing something dumb:
+ } else { cmp::Ordering::Equal }
+ } else { cmp::Ordering::Equal }
+ });
let mut outputs: Vec<TxOut> = Vec::with_capacity(txouts.len());
let mut htlcs_included: Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)> = Vec::with_capacity(txouts.len() + included_dust_htlcs.len());
}, ()));
}
- transaction_utils::sort_outputs(&mut txouts);
+ transaction_utils::sort_outputs(&mut txouts, |_, _| { cmp::Ordering::Equal }); // Ordering doesnt matter if they used our pubkey...
let mut outputs: Vec<TxOut> = Vec::new();
for out in txouts.drain(..) {
use std::cmp::Ordering;
-pub fn sort_outputs<T>(outputs: &mut Vec<(TxOut, T)>) {
+pub fn sort_outputs<T, C : Fn(&T, &T) -> Ordering>(outputs: &mut Vec<(TxOut, T)>, tie_breaker: C) {
outputs.sort_unstable_by(|a, b| {
- a.0.value.cmp(&b.0.value).then(
- a.0.script_pubkey[..].cmp(&b.0.script_pubkey[..])
- )
+ a.0.value.cmp(&b.0.value).then_with(|| {
+ a.0.script_pubkey[..].cmp(&b.0.script_pubkey[..]).then_with(|| {
+ tie_breaker(&a.1, &b.1)
+ })
+ })
});
}
let txout2_ = txout2.clone();
let mut outputs = vec![(txout1, "ignore"), (txout2, "ignore")];
- sort_outputs(&mut outputs);
+ sort_outputs(&mut outputs, |_, _| { unreachable!(); });
assert_eq!(
&outputs,
let txout2_ = txout2.clone();
let mut outputs = vec![(txout1, "ignore"), (txout2, "ignore")];
- sort_outputs(&mut outputs);
+ sort_outputs(&mut outputs, |_, _| { unreachable!(); });
assert_eq!(
&outputs,
let txout2_ = txout2.clone();
let mut outputs = vec![(txout1, "ignore"), (txout2, "ignore")];
- sort_outputs(&mut outputs);
+ sort_outputs(&mut outputs, |_, _| { unreachable!(); });
assert_eq!(&outputs, &vec![(txout1_, "ignore"), (txout2_, "ignore")]);
}
outputs.reverse(); // prep it
// actually do the work!
- sort_outputs(&mut outputs);
+ sort_outputs(&mut outputs, |_, _| { unreachable!(); });
assert_eq!(outputs, expected);
}