Skip to content

Commit 7dbd826

Browse files
committed
Refactor to get rid of closures
1 parent 414538c commit 7dbd826

File tree

3 files changed

+4
-109
lines changed

3 files changed

+4
-109
lines changed

src/lib.rs

Lines changed: 3 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -148,47 +148,6 @@ pub fn define_pushable(_: TokenStream) -> TokenStream {
148148

149149
pub struct Builder(pub BitcoinBuilder);
150150

151-
pub fn parse_instruction(
152-
instruction: Option<Result<Instruction<'_>, ::bitcoin::script::Error>>,
153-
) -> Option<Opcode> {
154-
match instruction {
155-
Some(Ok(bitcoin::script::Instruction::PushBytes(push_bytes))) => {
156-
// Handle OP_0 (Stored in rust-bitcoin as PushBytes(PushBytes([])))
157-
if push_bytes.len() == 0 {
158-
Some(::bitcoin::opcodes::all::OP_PUSHBYTES_0)
159-
} else {
160-
None
161-
}
162-
}
163-
Some(Ok(bitcoin::script::Instruction::Op(op))) => Some(op),
164-
_ => None,
165-
}
166-
}
167-
168-
pub fn check_optimality(
169-
opcode: Option<Opcode>,
170-
next_opcode: Option<Opcode>,
171-
) -> (bool, Option<Opcode>) {
172-
if opcode == None || next_opcode == None {
173-
(true, None)
174-
} else {
175-
let opcode = opcode.unwrap();
176-
let next_opcode = next_opcode.unwrap();
177-
match (opcode, next_opcode) {
178-
(OP_PUSHNUM_1, OP_ADD) => (false, Some(OP_1ADD)),
179-
(OP_PUSHNUM_1, OP_SUB) => (false, Some(OP_1SUB)),
180-
(OP_DROP, OP_DROP) => (false, Some(OP_2DROP)),
181-
(OP_PUSHBYTES_0, OP_ROLL) => (false, None),
182-
(OP_PUSHNUM_1, OP_ROLL) => (false, Some(OP_SWAP)),
183-
(OP_PUSHNUM_2, OP_ROLL) => (false, Some(OP_ROT)),
184-
(OP_PUSHBYTES_0, OP_PICK) => (false, Some(OP_DUP)),
185-
(OP_PUSHBYTES_1, OP_PICK) => (false, Some(OP_OVER)),
186-
(OP_IF, OP_ELSE) => (false, Some(OP_NOTIF)),
187-
(_, _) => (true, None),
188-
}
189-
}
190-
}
191-
192151
impl Builder {
193152
pub fn new() -> Self {
194153
let builder = BitcoinBuilder::new();
@@ -203,38 +162,8 @@ pub fn define_pushable(_: TokenStream) -> TokenStream {
203162
self.0.as_script()
204163
}
205164

206-
pub fn last_opcode(&self) -> Option<Opcode> {
207-
match self.0.as_script().instructions().last() {
208-
Some(Ok(instr)) => match instr {
209-
bitcoin::script::Instruction::PushBytes(push_bytes) => {
210-
// Handle OP_0 (Stored in rust-bitcoin as PushBytes(PushBytes([])))
211-
if push_bytes.len() == 0 {
212-
Some(::bitcoin::opcodes::all::OP_PUSHBYTES_0)
213-
} else {
214-
None
215-
}
216-
}
217-
bitcoin::script::Instruction::Op(op) => Some(op),
218-
},
219-
_ => None,
220-
}
221-
}
222-
223165
pub fn push_opcode(mut self, opcode: Opcode) -> Builder {
224-
let (optimal, replacement_opcode) =
225-
check_optimality(self.last_opcode(), Some(opcode));
226-
if optimal {
227-
self.0 = self.0.push_opcode(opcode);
228-
} else {
229-
// NOTE: This is only valid because here we know that the last element of
230-
// the script is either 0 or an Opcode and not some arbitrary data push.
231-
let mut script_bytes = self.0.into_bytes();
232-
script_bytes.truncate(script_bytes.len() - 1);
233-
self.0 = BitcoinBuilder::from(script_bytes);
234-
if let Some(replacement_opcode) = replacement_opcode {
235-
self.0 = self.0.push_opcode(replacement_opcode);
236-
}
237-
}
166+
self.0 = self.0.push_opcode(opcode);
238167
self
239168
}
240169

@@ -305,24 +234,9 @@ pub fn define_pushable(_: TokenStream) -> TokenStream {
305234
}
306235
impl NotU8Pushable for ::bitcoin::ScriptBuf {
307236
fn bitcoin_script_push(self, builder: Builder) -> Builder {
308-
let (optimal, replacement_opcode) =
309-
check_optimality(builder.last_opcode(), self.first_opcode());
310237
let mut script_vec = Vec::with_capacity(builder.0.as_bytes().len() + self.as_bytes().len());
311-
if optimal {
312-
script_vec.extend_from_slice(builder.as_bytes());
313-
script_vec.extend_from_slice(self.as_bytes());
314-
} else {
315-
let first_script_bytes = builder.0.as_bytes();
316-
script_vec
317-
.extend_from_slice(&first_script_bytes[..first_script_bytes.len() - 1]);
318-
match replacement_opcode {
319-
Some(opcode) => script_vec.push(opcode.to_u8()),
320-
None => (),
321-
}
322-
let second_script_bytes = self.as_bytes();
323-
script_vec
324-
.extend_from_slice(&second_script_bytes[1..second_script_bytes.len()]);
325-
}
238+
script_vec.extend_from_slice(builder.as_bytes());
239+
script_vec.extend_from_slice(self.as_bytes());
326240
Builder::from(script_vec)
327241
}
328242
}

src/parse.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ where
144144
{
145145
let mut escape = quote! {
146146
let mut script_var = vec![];
147-
let mut last_opcode = None;
148147
};
149148
escape.extend(std::iter::once(token.clone()));
150149

@@ -157,24 +156,7 @@ where
157156
let next_script = script !{
158157
#inner_block
159158
};
160-
// Store last instruction of next_script as Opcode in last_opcode.
161-
let new_last_opcode = pushable::parse_instruction(next_script.instructions().last());
162-
// Check optimality for the next_script first opcode and the previous
163-
// scripts last opcode.
164-
let (optimal, replacement_opcode) =
165-
pushable::check_optimality(last_opcode, next_script.first_opcode());
166-
if optimal {
167-
script_var.extend_from_slice(next_script.as_bytes());
168-
} else {
169-
script_var = script_var[..script_var.len() - 1].to_vec();
170-
match replacement_opcode {
171-
Some(opcode) => script_var.push(opcode.to_u8()),
172-
None => (),
173-
}
174-
let new_script_bytes = next_script.as_bytes();
175-
script_var.extend_from_slice(&new_script_bytes[1..new_script_bytes.len()]);
176-
}
177-
last_opcode = new_last_opcode;
159+
script_var.extend_from_slice(next_script.as_bytes());
178160
}
179161
bitcoin::script::ScriptBuf::from(script_var)
180162
});

tests/test.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ fn test_performance_loop() {
144144
OP_ADD
145145
OP_ADD
146146
OP_ADD
147-
script_from_func
148147
};
149148

150149
let script = script! {

0 commit comments

Comments
 (0)