Skip to content

Commit 2269563

Browse files
committed
Store script_map in StructuredScript again
1 parent ef9e8a1 commit 2269563

File tree

3 files changed

+35
-31
lines changed

3 files changed

+35
-31
lines changed

src/analyzer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::builder::{thread_get_script, Block, StructuredScript};
1+
use crate::builder::{Block, StructuredScript};
22
use bitcoin::blockdata::opcodes::Opcode;
33
use bitcoin::blockdata::script::{read_scriptint, Instruction};
44
use bitcoin::opcodes::all::*;
@@ -144,7 +144,7 @@ impl StackAnalyzer {
144144
for block in builder.blocks.iter() {
145145
match block {
146146
Block::Call(id) => {
147-
let called_script = thread_get_script(id);
147+
let called_script = builder.get_structured_script(id);
148148
match called_script.stack_hint() {
149149
Some(stack_hint) => {
150150
self.debug_position += called_script.len();

src/builder.rs

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,9 @@ use std::cmp::min;
88
use std::collections::HashMap;
99
use std::convert::TryFrom;
1010
use std::hash::{DefaultHasher, Hash, Hasher};
11-
use std::sync::RwLock;
1211

1312
use crate::analyzer::{StackAnalyzer, StackStatus};
1413

15-
// One global script map per thread.
16-
thread_local! {
17-
static SCRIPT_MAP: RwLock<HashMap<u64, Box<StructuredScript>>> =
18-
RwLock::new(HashMap::new());
19-
}
20-
21-
pub(crate) fn thread_add_script(id: u64, script: StructuredScript) {
22-
SCRIPT_MAP.with(|script_map| {
23-
let mut map = script_map.write().unwrap();
24-
map.entry(id).or_insert_with(|| Box::new(script));
25-
});
26-
}
27-
28-
pub(crate) fn thread_get_script(id: &u64) -> Box<StructuredScript> {
29-
SCRIPT_MAP.with(|script_map| {
30-
let map = script_map.read().unwrap();
31-
map.get(id)
32-
.expect("script id not found in SCRIPT_MAP")
33-
.clone()
34-
})
35-
}
36-
3714
#[derive(Clone, Debug, Hash)]
3815
pub enum Block {
3916
Call(u64),
@@ -57,11 +34,11 @@ pub struct StructuredScript {
5734
extra_endif_positions: Vec<usize>,
5835
max_if_interval: (usize, usize),
5936
pub blocks: Vec<Block>,
37+
script_map: HashMap<u64, StructuredScript>,
6038
}
6139

6240
impl Hash for StructuredScript {
6341
fn hash<H: Hasher>(&self, state: &mut H) {
64-
self.size.hash(state);
6542
self.blocks.hash(state);
6643
}
6744
}
@@ -84,13 +61,26 @@ impl StructuredScript {
8461
extra_endif_positions: vec![],
8562
max_if_interval: (0, 0),
8663
blocks,
64+
script_map: HashMap::new(),
8765
}
8866
}
8967

9068
pub fn len(&self) -> usize {
9169
self.size
9270
}
9371

72+
pub fn add_structured_script(&mut self, id: u64, script: StructuredScript) {
73+
self.script_map.entry(id).or_insert(script);
74+
}
75+
76+
pub fn get_structured_script(&self, id: &u64) -> &StructuredScript {
77+
self.script_map.get(id)
78+
.expect(&format!(
79+
"script id: {} not found in script_map.",
80+
id
81+
))
82+
}
83+
9484
pub fn contains_flow_op(&self) -> bool {
9585
!(self.unclosed_if_positions.is_empty()
9686
&& self.extra_endif_positions().is_empty()
@@ -129,7 +119,11 @@ impl StructuredScript {
129119
assert!(current_pos <= position, "Target position not found");
130120
match block {
131121
Block::Call(id) => {
132-
let called_script = thread_get_script(id);
122+
//let called_script = self.get_structured_script(id);
123+
let called_script = self
124+
.script_map
125+
.get(id)
126+
.expect("Missing entry for a called script");
133127
if position >= current_pos && position < current_pos + called_script.len() {
134128
return called_script.debug_info(position - current_pos);
135129
}
@@ -244,6 +238,12 @@ impl StructuredScript {
244238
}
245239

246240
pub fn push_env_script(mut self, mut data: StructuredScript) -> StructuredScript {
241+
if data.len() == 0 {
242+
return self;
243+
}
244+
if self.len() == 0 {
245+
return data;
246+
}
247247
data.debug_identifier = format!("{} {}", self.debug_identifier, data.debug_identifier);
248248
// Try closing ifs
249249
let num_closable_ifs = min(
@@ -273,8 +273,8 @@ impl StructuredScript {
273273
self.num_unclosed_ifs += data.num_unclosed_ifs;
274274
let id = calculate_hash(&data);
275275
self.blocks.push(Block::Call(id));
276-
// Register script in the global script map
277-
thread_add_script(id, data);
276+
// Register script in the script map
277+
self.add_structured_script(id, data);
278278
self
279279
}
280280

@@ -284,7 +284,10 @@ impl StructuredScript {
284284
for block in self.blocks.as_slice() {
285285
match block {
286286
Block::Call(id) => {
287-
let called_script = thread_get_script(id);
287+
let called_script = self
288+
.script_map
289+
.get(id)
290+
.expect("Missing entry for a called script");
288291
// Check if the script with the hash id is in cache
289292
match cache.get(id) {
290293
Some(called_start) => {

tests/test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use bitcoin::{
2-
consensus::{self, encode, Encodable},
2+
consensus::{encode, Encodable},
33
opcodes::all::OP_ADD,
44
Witness,
55
};
@@ -397,6 +397,7 @@ fn test_is_script_buf() {
397397
#[test]
398398
fn test_is_script_buf_false() {
399399
let script = script! {
400+
OP_ADD
400401
{ script! {OP_ADD} }
401402
};
402403
assert!(!script.is_script_buf());

0 commit comments

Comments
 (0)