Skip to content

Commit 5d77a6e

Browse files
committed
Extend tracker trait with jet call tracing
1 parent f631264 commit 5d77a6e

2 files changed

Lines changed: 33 additions & 13 deletions

File tree

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ default = ["elements"]
1515
elements = ["dep:elements", "bitcoin"]
1616
test-utils = ["simplicity-sys/test-utils"]
1717
serde = ["dep:serde", "bitcoin/serde", "elements/serde"]
18-
trace = []
1918

2019
[lib]
2120
name = "simplicity"

src/bit_machine/mod.rs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::types::Final;
2020
use crate::{analysis, Ihr};
2121
use crate::{Cmr, FailEntropy, Value};
2222
use frame::Frame;
23+
use simplicity_sys::ffi::UWORD;
2324

2425
pub use self::limits::LimitError;
2526

@@ -242,7 +243,12 @@ impl BitMachine {
242243
Ok(tracker)
243244
}
244245

245-
fn exec_with_tracker<J: Jet, T: CaseTracker>(
246+
/// Execute the given `program` on the Bit Machine, using the given environment and tracker.
247+
///
248+
/// ## Precondition
249+
///
250+
/// The Bit Machine is constructed via [`Self::for_program()`] to ensure enough space.
251+
pub fn exec_with_tracker<J: Jet, T: ExecTracker<J>>(
246252
&mut self,
247253
program: &RedeemNode<J>,
248254
env: &J::Environment,
@@ -371,7 +377,7 @@ impl BitMachine {
371377
}
372378
}
373379
node::Inner::Witness(value) => self.write_value(value),
374-
node::Inner::Jet(jet) => self.exec_jet(*jet, env)?,
380+
node::Inner::Jet(jet) => self.exec_jet(*jet, env, tracker)?,
375381
node::Inner::Word(value) => self.write_value(value.as_value()),
376382
node::Inner::Fail(entropy) => {
377383
return Err(ExecutionError::ReachedFailNode(*entropy))
@@ -408,7 +414,12 @@ impl BitMachine {
408414
}
409415
}
410416

411-
fn exec_jet<J: Jet>(&mut self, jet: J, env: &J::Environment) -> Result<(), JetFailed> {
417+
fn exec_jet<J: Jet, T: ExecTracker<J>>(
418+
&mut self,
419+
jet: J,
420+
env: &J::Environment,
421+
tracker: &mut T,
422+
) -> Result<(), JetFailed> {
412423
use crate::ffi::c_jets::frame_ffi::{c_readBit, c_writeBit, CFrameItem};
413424
use crate::ffi::c_jets::uword_width;
414425
use crate::ffi::ffi::UWORD;
@@ -501,9 +512,7 @@ impl BitMachine {
501512
let c_env = J::c_jet_env(env);
502513
let success = jet_fn(&mut output_write_frame, input_read_frame, c_env);
503514

504-
if cfg!(feature = "trace") {
505-
println!("{:?} {:?} -> {:?}", jet, input_buffer, output_buffer);
506-
}
515+
tracker.track_jet_call(&jet, &input_buffer, &output_buffer, success);
507516

508517
if !success {
509518
Err(JetFailed)
@@ -514,20 +523,28 @@ impl BitMachine {
514523
}
515524
}
516525

517-
/// A type that keeps track of which case branches were executed
518-
/// during the execution of the Bit Machine.
526+
/// A type that keeps track of Bit Machine execution.
519527
///
520-
/// The trait is implemented for [`SetTracker`], which does the actual tracking,
528+
/// The trait is implemented for [`SetTracker`], that tracks which case branches were executed,
521529
/// and it is implemented for [`NoTracker`], which is a dummy tracker that is
522530
/// optimized out by the compiler.
523531
///
524532
/// The trait enables us to turn tracking on or off depending on a generic parameter.
525-
trait CaseTracker {
533+
pub trait ExecTracker<J: Jet> {
526534
/// Track the execution of the left branch of the case node with the given `ihr`.
527535
fn track_left(&mut self, ihr: Ihr);
528536

529537
/// Track the execution of the right branch of the case node with the given `ihr`.
530538
fn track_right(&mut self, ihr: Ihr);
539+
540+
/// Track the execution of a `jet` call with the given `input_buffer`, `output_buffer`, and call result `success`.
541+
fn track_jet_call(
542+
&mut self,
543+
jet: &J,
544+
input_buffer: &[UWORD],
545+
output_buffer: &[UWORD],
546+
success: bool,
547+
);
531548
}
532549

533550
/// Tracker of executed left and right branches for each case node.
@@ -552,20 +569,24 @@ impl SetTracker {
552569
#[derive(Copy, Clone, Debug)]
553570
struct NoTracker;
554571

555-
impl CaseTracker for SetTracker {
572+
impl<J: Jet> ExecTracker<J> for SetTracker {
556573
fn track_left(&mut self, ihr: Ihr) {
557574
self.left.insert(ihr);
558575
}
559576

560577
fn track_right(&mut self, ihr: Ihr) {
561578
self.right.insert(ihr);
562579
}
580+
581+
fn track_jet_call(&mut self, _: &J, _: &[UWORD], _: &[UWORD], _: bool) {}
563582
}
564583

565-
impl CaseTracker for NoTracker {
584+
impl<J: Jet> ExecTracker<J> for NoTracker {
566585
fn track_left(&mut self, _: Ihr) {}
567586

568587
fn track_right(&mut self, _: Ihr) {}
588+
589+
fn track_jet_call(&mut self, _: &J, _: &[UWORD], _: &[UWORD], _: bool) {}
569590
}
570591

571592
/// Errors related to simplicity Execution

0 commit comments

Comments
 (0)