1
1
use core:: panic;
2
2
3
- use bitcoin:: { opcodes:: all:: { OP_ENDIF , OP_IF , OP_NOTIF } , script:: Instruction , ScriptBuf } ;
3
+ use bitcoin:: {
4
+ opcodes:: all:: { OP_ENDIF , OP_IF , OP_NOTIF } ,
5
+ script:: Instruction ,
6
+ ScriptBuf ,
7
+ } ;
4
8
5
9
use crate :: {
6
10
analyzer:: StackStatus ,
@@ -99,9 +103,7 @@ impl Chunker {
99
103
chunks. push ( chunk) ;
100
104
}
101
105
for chunk in chunks. iter_mut ( ) {
102
- // println!("chunk size: {}", chunk_size);
103
106
let status = self . stack_analyze ( & mut chunk. scripts ) ;
104
- // println!("stack_analyze: {:?}", status);
105
107
// ((-1 * access) as u32, (depth - access) as u32)
106
108
let stack_input_size = status. deepest_stack_accessed . abs ( ) as usize ;
107
109
let stack_output_size = ( status. stack_changed - status. deepest_stack_accessed ) as usize ;
@@ -132,14 +134,13 @@ impl Chunker {
132
134
return ( vec ! [ ] , 0 ) ;
133
135
}
134
136
135
- println ! ( "[INFO] Unable to close all ifs. Undoing the added scripts to a point where num_unclosed_ifs is 0." ) ;
136
137
let mut removed_scripts = vec ! [ ] ;
137
138
let mut removed_len = 0 ;
138
139
139
140
loop {
140
141
let builder = match undo_info. call_stack . pop ( ) {
141
142
Some ( builder) => builder,
142
- None => break , // the last block in the call stack
143
+ None => panic ! ( "num_unclosed_ifs != 0 but the undo_info call_stack is empty" ) , // the last block in the call stack
143
144
} ;
144
145
if builder. contains_flow_op ( ) {
145
146
if builder. is_script_buf ( ) && builder. len ( ) == 1 {
@@ -150,7 +151,7 @@ impl Chunker {
150
151
break ;
151
152
}
152
153
} else {
153
- for block in builder. blocks . iter ( ) . rev ( ) {
154
+ for block in builder. blocks {
154
155
match block {
155
156
Block :: Call ( id) => {
156
157
let sub_builder = builder. script_map . get ( & id) . unwrap ( ) ;
@@ -163,19 +164,28 @@ impl Chunker {
163
164
for instruction_res in script_buf. instructions ( ) {
164
165
let instruction = instruction_res. unwrap ( ) ;
165
166
match instruction {
166
- Instruction :: Op ( OP_IF ) | Instruction :: Op ( OP_ENDIF ) | Instruction :: Op ( OP_NOTIF ) => {
167
+ Instruction :: Op ( OP_IF )
168
+ | Instruction :: Op ( OP_ENDIF )
169
+ | Instruction :: Op ( OP_NOTIF ) => {
167
170
undo_info. call_stack . push ( Box :: new (
168
- StructuredScript :: new ( "" ) . push_script ( std:: mem:: take ( & mut tmp_script) ) ,
171
+ StructuredScript :: new ( "" )
172
+ . push_script ( std:: mem:: take ( & mut tmp_script) ) ,
169
173
) ) ;
170
- tmp_script. push_instruction_no_opt ( instruction) ;
174
+ tmp_script. push_instruction ( instruction) ;
171
175
undo_info. call_stack . push ( Box :: new (
172
- StructuredScript :: new ( "" ) . push_script ( std:: mem:: take ( & mut tmp_script) ) ,
176
+ StructuredScript :: new ( "" )
177
+ . push_script ( std:: mem:: take ( & mut tmp_script) ) ,
173
178
) ) ;
174
179
}
175
- _ => tmp_script. push_instruction_no_opt ( instruction) ,
176
-
180
+ _ => tmp_script. push_instruction ( instruction) ,
177
181
}
178
182
}
183
+ if !tmp_script. is_empty ( ) {
184
+ undo_info. call_stack . push ( Box :: new (
185
+ StructuredScript :: new ( "" )
186
+ . push_script ( tmp_script) ,
187
+ ) ) ;
188
+ }
179
189
}
180
190
}
181
191
}
@@ -210,9 +220,6 @@ impl Chunker {
210
220
None => break , // the last block in the call stack
211
221
} ;
212
222
213
- //println!("[INFO] current chunk_len: {} -- current num_unclosed_ifs: {}", chunk_len, num_unclosed_ifs);
214
- //println!("[INFO] Popping builder with size {} and num_unclosed_ifs {} from call_stack", builder.len(), builder.num_unclosed_ifs());
215
-
216
223
assert ! (
217
224
num_unclosed_ifs + builder. num_unclosed_ifs( ) >= 0 ,
218
225
"More OP_ENDIF's than OP_IF's in the script. num_unclosed_if: {:?}, builder: {:?}" ,
@@ -247,12 +254,16 @@ impl Chunker {
247
254
} else if chunk_len + block_len > self . target_chunk_size
248
255
&& ( chunk_len < self . target_chunk_size - self . tolerance
249
256
|| chunk_len == 0
250
- || depth <= max_depth)
257
+ || ( chunk_len < self . target_chunk_size && depth <= max_depth) )
251
258
{
252
259
// Case 3: Current builder too large and there is no acceptable solution yet
253
260
// Even if we have an acceptable solution we check if there is a better one in next depth calls
254
261
// Chunk inside a call of the current builder.
255
262
// Add all its calls to the call_stack.
263
+ if builder. is_script_buf ( ) {
264
+ self . call_stack . push ( Box :: new ( builder) ) ;
265
+ break ;
266
+ }
256
267
let mut contains_call = false ;
257
268
for block in builder. blocks . iter ( ) . rev ( ) {
258
269
match block {
0 commit comments