Skip to content

Commit 10f2022

Browse files
committed
refactor(tracker): require debug symbols at construction time
revert some changes to tracker tests.
1 parent ff1e076 commit 10f2022

1 file changed

Lines changed: 30 additions & 42 deletions

File tree

src/tracker.rs

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -71,35 +71,36 @@ fn default_warning_sink(message: &str) {
7171
///
7272
/// This tracker extends [`SetTracker`] with SimplicityHL-specific functionality:
7373
///
74-
/// - Optionally traces jet invocations with decoded arguments and return values.
7574
/// - Decodes and forwards `dbg!()` calls to a configurable sink, using
76-
/// debug symbols supplied via [`with_debug_symbols`](DefaultTracker::with_debug_symbols).
75+
/// the provided [`DebugSymbols`] to resolve CMRs to debug information.
76+
/// - Optionally traces jet invocations with decoded arguments and return values.
7777
///
7878
/// # Example
7979
///
8080
/// ```rust,ignore
81-
/// let tracker = DefaultTracker::new()
82-
/// .with_debug_symbols(&debug_symbols)
81+
/// let tracker = DefaultTracker::new(&debug_symbols)
8382
/// .with_log_level(TrackerLogLevel::Debug);
8483
///
8584
/// let pruned = program.prune_with_tracker(&env, &mut tracker)?;
8685
/// ```
87-
#[derive(Default)]
8886
pub struct DefaultTracker<'a> {
89-
debug_symbols: Option<&'a DebugSymbols>,
87+
debug_symbols: &'a DebugSymbols,
9088
debug_sink: Option<DebugSink<'a>>,
9189
jet_trace_sink: Option<JetTraceSink<'a>>,
9290
warning_sink: Option<WarningSink<'a>>,
9391
inner: SetTracker,
9492
}
9593

9694
impl<'a> DefaultTracker<'a> {
97-
/// Supplies the debug symbol table needed to decode `dbg!()` calls.
98-
///
99-
/// Required when using any debug sink; has no effect otherwise.
100-
pub fn with_debug_symbols(mut self, debug_symbols: &'a DebugSymbols) -> Self {
101-
self.debug_symbols = Some(debug_symbols);
102-
self
95+
/// Creates a new tracker bound to the given debug symbol table.
96+
pub fn new(debug_symbols: &'a DebugSymbols) -> Self {
97+
Self {
98+
debug_symbols,
99+
debug_sink: None,
100+
jet_trace_sink: None,
101+
warning_sink: None,
102+
inner: SetTracker::default(),
103+
}
103104
}
104105

105106
/// Enables forwarding of `debug!()` calls to the provided sink.
@@ -184,6 +185,11 @@ impl<'a> DefaultTracker<'a> {
184185

185186
let mut input_frame = input.clone();
186187

188+
// The reason we need to advance by a bit is that the AssertL combinator is actually a Case combinator,
189+
// which takes a bit of input to decide which branch to take. But this bit is "meaningless" and
190+
// is always 0 because it's an assertion.
191+
let _ = input_frame.next();
192+
187193
let args = match parse_jet_arguments(jet, &mut input_frame) {
188194
Ok(args) => args,
189195
Err(e) => {
@@ -213,6 +219,13 @@ impl<'a> DefaultTracker<'a> {
213219
let target_ty = &node.arrow().target;
214220
let jet_target_ty = resolve_jet_type(&target_type(jet));
215221

222+
// Skip the leading bit when the frame has extra padding.
223+
// This occurs because some jets (like eq_64 etc.) are wrapped in AssertL (a Case combinator),
224+
// see compile::with_debug_symbol
225+
if target_ty.as_sum().is_some() {
226+
let _ = output_frame.next();
227+
}
228+
216229
let output_value = SimValue::from_padded_bits(&mut output_frame, target_ty)
217230
.expect("output from bit machine is always well-formed");
218231

@@ -235,12 +248,7 @@ impl<'a> DefaultTracker<'a> {
235248
return;
236249
}
237250

238-
let Some(debug_symbols) = self.debug_symbols else {
239-
self.warn("Debug sink set but no debug symbols provided");
240-
return;
241-
};
242-
243-
let Some(tracked_call) = debug_symbols.get(cmr) else {
251+
let Some(tracked_call) = self.debug_symbols.get(cmr) else {
244252
self.warn(&format!("Unknown debug symbol: CMR {cmr}"));
245253
return;
246254
};
@@ -251,9 +259,7 @@ impl<'a> DefaultTracker<'a> {
251259

252260
let mut input_frame = input.clone();
253261

254-
// AssertL is a Case combinator, which reads one selector bit before passing
255-
// the remainder to its child. The tracker sees the full frame at the AssertL
256-
// node, so skip that bit to reach the actual value.
262+
// Skip the Case combinator's branch selection bit (see handle_jet).
257263
let _ = input_frame.next();
258264

259265
// The debug call has signature `dbg!(T) -> T`, so the target type
@@ -398,8 +404,7 @@ mod tests {
398404
let debug_clone = debug_store.clone();
399405
let jet_clone = jet_store.clone();
400406

401-
let tracker = DefaultTracker::default()
402-
.with_debug_symbols(debug_symbols)
407+
let tracker = DefaultTracker::new(debug_symbols)
403408
.with_debug_sink(move |label, value| {
404409
debug_clone
405410
.borrow_mut()
@@ -418,23 +423,6 @@ mod tests {
418423
(tracker, debug_store, jet_store)
419424
}
420425

421-
fn create_jet_tracker() -> (DefaultTracker<'static>, JetStore) {
422-
let jet_store: JetStore = Rc::default();
423-
let jet_clone = jet_store.clone();
424-
425-
let tracker = DefaultTracker::default().with_jet_trace_sink(move |jet, args, result| {
426-
jet_clone.borrow_mut().insert(
427-
jet.to_string(),
428-
(
429-
args.map(|a| a.iter().map(|v| v.to_string()).collect()),
430-
result.map(|r| r.to_string()),
431-
),
432-
);
433-
});
434-
435-
(tracker, jet_store)
436-
}
437-
438426
fn create_test_env() -> ElementsEnv<Arc<elements::Transaction>> {
439427
let mut tx = PartiallySignedTransaction::new_v2();
440428
let outpoint = OutPoint::new(Txid::from_slice(&[2; 32]).unwrap(), 33);
@@ -530,7 +518,7 @@ mod tests {
530518
let program = program.instantiate(Arguments::default(), true).unwrap();
531519
let satisfied = program.satisfy(WitnessValues::default()).unwrap();
532520

533-
let (mut tracker, jet_store) = create_jet_tracker();
521+
let (mut tracker, _, jet_store) = create_test_tracker(&satisfied.debug_symbols);
534522

535523
let _ = satisfied.redeem().prune_with_tracker(&env, &mut tracker);
536524

@@ -584,7 +572,7 @@ mod tests {
584572
let program = program.instantiate(Arguments::default(), true).unwrap();
585573
let satisfied = program.satisfy(WitnessValues::default()).unwrap();
586574

587-
let (mut tracker, jet_store) = create_jet_tracker();
575+
let (mut tracker, _, jet_store) = create_test_tracker(&satisfied.debug_symbols);
588576

589577
let _ = satisfied.redeem().prune_with_tracker(&env, &mut tracker);
590578

0 commit comments

Comments
 (0)