Skip to content

Commit eadaaf8

Browse files
committed
Compress patter match in script_size
Compress match arms using `|`, this makes the code more terse and easier to quickly see if terminal variants return the expected length (for the trivial ones at least).
1 parent 01bd9a1 commit eadaaf8

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

src/miniscript/mod.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -121,33 +121,24 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Miniscript<Pk, Ctx> {
121121
/// to instead call the corresponding function on a `Descriptor`, which
122122
/// will handle the segwit/non-segwit technicalities for you.
123123
pub fn script_size(&self) -> usize {
124+
use Terminal::*;
125+
124126
let mut len = 0;
125127
for ms in self.pre_order_iter() {
126128
len += match ms.node {
129+
AndV(..) => 0,
130+
True | False | Swap(..) | Check(..) | ZeroNotEqual(..) | AndB(..) | OrB(..) => 1,
131+
Alt(..) | OrC(..) => 2,
132+
DupIf(..) | AndOr(..) | OrD(..) | OrI(..) => 3,
133+
NonZero(..) => 4,
134+
PkH(..) | RawPkH(..) => 24,
135+
Ripemd160(..) | Hash160(..) => 21 + 6,
136+
Sha256(..) | Hash256(..) => 33 + 6,
137+
127138
Terminal::PkK(ref pk) => Ctx::pk_len(pk),
128-
Terminal::PkH(..) | Terminal::RawPkH(..) => 24,
129139
Terminal::After(n) => script_num_size(n.to_consensus_u32() as usize) + 1,
130140
Terminal::Older(n) => script_num_size(n.to_consensus_u32() as usize) + 1,
131-
Terminal::Sha256(..) => 33 + 6,
132-
Terminal::Hash256(..) => 33 + 6,
133-
Terminal::Ripemd160(..) => 21 + 6,
134-
Terminal::Hash160(..) => 21 + 6,
135-
Terminal::True => 1,
136-
Terminal::False => 1,
137-
Terminal::Alt(..) => 2,
138-
Terminal::Swap(..) => 1,
139-
Terminal::Check(..) => 1,
140-
Terminal::DupIf(..) => 3,
141141
Terminal::Verify(ref sub) => usize::from(!sub.ext.has_free_verify),
142-
Terminal::NonZero(..) => 4,
143-
Terminal::ZeroNotEqual(..) => 1,
144-
Terminal::AndV(..) => 0,
145-
Terminal::AndB(..) => 1,
146-
Terminal::AndOr(..) => 3,
147-
Terminal::OrB(..) => 1,
148-
Terminal::OrD(..) => 3,
149-
Terminal::OrC(..) => 2,
150-
Terminal::OrI(..) => 3,
151142
Terminal::Thresh(k, ref subs) => {
152143
assert!(!subs.is_empty(), "threshold must be nonempty");
153144
script_num_size(k) // k

0 commit comments

Comments
 (0)