Skip to content

Commit 0452040

Browse files
committed
Order arms as in enum definition
Currently there are a bunch of places where when matching on the `Policy` enum variants are matched in different order to the enum. During recent work this was maintained to make diffs easier to review however since we just modified a lot of this file lets clean it up while we are here. Move the match arms around so that they are ordered in the same order as the variants are defined in the `Policy` enum. Refactor only, no logic changes.
1 parent d2278d5 commit 0452040

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

src/policy/concrete.rs

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -579,13 +579,13 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
579579
Hash160(ref h) => t.hash160(h).map(Hash160)?,
580580
Older(ref n) => Older(*n),
581581
After(ref n) => After(*n),
582-
Threshold(ref k, ref subs) => Threshold(*k, (0..subs.len()).map(child_n).collect()),
583582
And(ref subs) => And((0..subs.len()).map(child_n).collect()),
584583
Or(ref subs) => Or(subs
585584
.iter()
586585
.enumerate()
587586
.map(|(i, (prob, _))| (*prob, child_n(i)))
588587
.collect()),
588+
Threshold(ref k, ref subs) => Threshold(*k, (0..subs.len()).map(child_n).collect()),
589589
};
590590
translated.push(Arc::new(new_policy));
591591
}
@@ -605,15 +605,15 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
605605

606606
let new_policy = match data.node.as_ref() {
607607
Policy::Key(ref k) if k.clone() == *key => Some(Policy::Unsatisfiable),
608-
Threshold(k, ref subs) => {
609-
Some(Threshold(*k, (0..subs.len()).map(child_n).collect()))
610-
}
611608
And(ref subs) => Some(And((0..subs.len()).map(child_n).collect())),
612609
Or(ref subs) => Some(Or(subs
613610
.iter()
614611
.enumerate()
615612
.map(|(i, (prob, _))| (*prob, child_n(i)))
616613
.collect())),
614+
Threshold(k, ref subs) => {
615+
Some(Threshold(*k, (0..subs.len()).map(child_n).collect()))
616+
}
617617
_ => None,
618618
};
619619
match new_policy {
@@ -724,10 +724,6 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
724724
cltv_with_time: false,
725725
contains_combination: false,
726726
},
727-
Threshold(ref k, subs) => {
728-
let iter = (0..subs.len()).map(info_for_child_n);
729-
TimelockInfo::combine_threshold(*k, iter)
730-
}
731727
And(ref subs) => {
732728
let iter = (0..subs.len()).map(info_for_child_n);
733729
TimelockInfo::combine_threshold(subs.len(), iter)
@@ -736,6 +732,10 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
736732
let iter = (0..subs.len()).map(info_for_child_n);
737733
TimelockInfo::combine_threshold(1, iter)
738734
}
735+
Threshold(ref k, subs) => {
736+
let iter = (0..subs.len()).map(info_for_child_n);
737+
TimelockInfo::combine_threshold(*k, iter)
738+
}
739739
_ => TimelockInfo::default(),
740740
};
741741
infos.push(info);
@@ -756,6 +756,20 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
756756

757757
for policy in self.pre_order_iter() {
758758
match *policy {
759+
After(n) => {
760+
if n == absolute::LockTime::ZERO.into() {
761+
return Err(PolicyError::ZeroTime);
762+
} else if n.to_u32() > 2u32.pow(31) {
763+
return Err(PolicyError::TimeTooFar);
764+
}
765+
}
766+
Older(n) => {
767+
if n == Sequence::ZERO {
768+
return Err(PolicyError::ZeroTime);
769+
} else if n.to_consensus_u32() > 2u32.pow(31) {
770+
return Err(PolicyError::TimeTooFar);
771+
}
772+
}
759773
And(ref subs) => {
760774
if subs.len() != 2 {
761775
return Err(PolicyError::NonBinaryArgAnd);
@@ -771,20 +785,6 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
771785
return Err(PolicyError::IncorrectThresh);
772786
}
773787
}
774-
After(n) => {
775-
if n == absolute::LockTime::ZERO.into() {
776-
return Err(PolicyError::ZeroTime);
777-
} else if n.to_u32() > 2u32.pow(31) {
778-
return Err(PolicyError::TimeTooFar);
779-
}
780-
}
781-
Older(n) => {
782-
if n == Sequence::ZERO {
783-
return Err(PolicyError::ZeroTime);
784-
} else if n.to_consensus_u32() > 2u32.pow(31) {
785-
return Err(PolicyError::TimeTooFar);
786-
}
787-
}
788788
_ => {}
789789
}
790790
}
@@ -810,18 +810,6 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
810810
Sha256(_) | Hash256(_) | Ripemd160(_) | Hash160(_) | After(_) | Older(_) => {
811811
(false, true)
812812
}
813-
Threshold(k, ref subs) => {
814-
let (safe_count, non_mall_count) = (0..subs.len()).map(acc_for_child_n).fold(
815-
(0, 0),
816-
|(safe_count, non_mall_count), (safe, non_mall)| {
817-
(safe_count + safe as usize, non_mall_count + non_mall as usize)
818-
},
819-
);
820-
(
821-
safe_count >= (subs.len() - k + 1),
822-
non_mall_count == subs.len() && safe_count >= (subs.len() - k),
823-
)
824-
}
825813
And(ref subs) => {
826814
let (atleast_one_safe, all_non_mall) = (0..subs.len())
827815
.map(acc_for_child_n)
@@ -836,6 +824,18 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
836824
});
837825
(all_safe, atleast_one_safe && all_non_mall)
838826
}
827+
Threshold(k, ref subs) => {
828+
let (safe_count, non_mall_count) = (0..subs.len()).map(acc_for_child_n).fold(
829+
(0, 0),
830+
|(safe_count, non_mall_count), (safe, non_mall)| {
831+
(safe_count + safe as usize, non_mall_count + non_mall as usize)
832+
},
833+
);
834+
(
835+
safe_count >= (subs.len() - k + 1),
836+
non_mall_count == subs.len() && safe_count >= (subs.len() - k),
837+
)
838+
}
839839
};
840840
acc.push(new);
841841
}

0 commit comments

Comments
 (0)