Skip to content

Commit ca4e497

Browse files
committed
bit_machine: move tracker stuff to its own module
Re-export everything so there is no API change. This just tidies up the code a little bit.
1 parent eaa02d0 commit ca4e497

2 files changed

Lines changed: 104 additions & 89 deletions

File tree

src/bit_machine/mod.rs

Lines changed: 3 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@
88
99
mod frame;
1010
mod limits;
11+
mod tracker;
1112

12-
use std::collections::HashSet;
1313
use std::error;
1414
use std::fmt;
1515
use std::sync::Arc;
1616

17+
use crate::analysis;
1718
use crate::jet::{Jet, JetFailed};
1819
use crate::node::{self, RedeemNode};
1920
use crate::types::Final;
20-
use crate::{analysis, Ihr};
2121
use crate::{Cmr, FailEntropy, Value};
2222
use frame::Frame;
23-
use simplicity_sys::ffi::UWORD;
2423

2524
pub use self::limits::LimitError;
25+
pub use self::tracker::{ExecTracker, NoTracker, SetTracker};
2626

2727
/// An iterator over the contents of a read or write frame which yields bits.
2828
pub type FrameIter<'a> = crate::BitIter<core::iter::Copied<core::slice::Iter<'a, u8>>>;
@@ -539,92 +539,6 @@ impl BitMachine {
539539
}
540540
}
541541

542-
/// A type that keeps track of Bit Machine execution.
543-
///
544-
/// The trait is implemented for [`SetTracker`], that tracks which case branches were executed,
545-
/// and it is implemented for [`NoTracker`], which is a dummy tracker that is
546-
/// optimized out by the compiler.
547-
///
548-
/// The trait enables us to turn tracking on or off depending on a generic parameter.
549-
pub trait ExecTracker<J: Jet> {
550-
/// Track the execution of the left branch of the case node with the given `ihr`.
551-
fn track_left(&mut self, ihr: Ihr);
552-
553-
/// Track the execution of the right branch of the case node with the given `ihr`.
554-
fn track_right(&mut self, ihr: Ihr);
555-
556-
/// Track the execution of a `jet` call with the given `input_buffer`, `output_buffer`, and call result `success`.
557-
fn track_jet_call(
558-
&mut self,
559-
jet: &J,
560-
input_buffer: &[UWORD],
561-
output_buffer: &[UWORD],
562-
success: bool,
563-
);
564-
565-
/// Track the potential execution of a `dbg!` call with the given `cmr` and `value`.
566-
fn track_dbg_call(&mut self, cmr: &Cmr, value: Value);
567-
568-
/// Check if tracking debug calls is enabled.
569-
fn is_track_debug_enabled(&self) -> bool;
570-
}
571-
572-
/// Tracker of executed left and right branches for each case node.
573-
#[derive(Clone, Debug, Default)]
574-
pub struct SetTracker {
575-
left: HashSet<Ihr>,
576-
right: HashSet<Ihr>,
577-
}
578-
579-
impl SetTracker {
580-
/// Access the set of IHRs of case nodes whose left branch was executed.
581-
pub fn left(&self) -> &HashSet<Ihr> {
582-
&self.left
583-
}
584-
585-
/// Access the set of IHRs of case nodes whose right branch was executed.
586-
pub fn right(&self) -> &HashSet<Ihr> {
587-
&self.right
588-
}
589-
}
590-
591-
/// Tracker that does not do anything (noop).
592-
#[derive(Copy, Clone, Debug)]
593-
pub struct NoTracker;
594-
595-
impl<J: Jet> ExecTracker<J> for SetTracker {
596-
fn track_left(&mut self, ihr: Ihr) {
597-
self.left.insert(ihr);
598-
}
599-
600-
fn track_right(&mut self, ihr: Ihr) {
601-
self.right.insert(ihr);
602-
}
603-
604-
fn track_jet_call(&mut self, _: &J, _: &[UWORD], _: &[UWORD], _: bool) {}
605-
606-
fn track_dbg_call(&mut self, _: &Cmr, _: Value) {}
607-
608-
fn is_track_debug_enabled(&self) -> bool {
609-
false
610-
}
611-
}
612-
613-
impl<J: Jet> ExecTracker<J> for NoTracker {
614-
fn track_left(&mut self, _: Ihr) {}
615-
616-
fn track_right(&mut self, _: Ihr) {}
617-
618-
fn track_jet_call(&mut self, _: &J, _: &[UWORD], _: &[UWORD], _: bool) {}
619-
620-
fn track_dbg_call(&mut self, _: &Cmr, _: Value) {}
621-
622-
fn is_track_debug_enabled(&self) -> bool {
623-
// Set flag to test frame decoding in unit tests
624-
cfg!(test)
625-
}
626-
}
627-
628542
/// Errors related to simplicity Execution
629543
#[derive(Debug)]
630544
pub enum ExecutionError {

src/bit_machine/tracker.rs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// SPDX-License-Identifier: CC0-1.0
2+
3+
//! Bit Machine Tracker
4+
//!
5+
//! This module provides traits for adding "trackers" to the bit machine execution
6+
//! and pruning algorithms, which can provide debugging output or control which
7+
//! branches are pruned. It also provides a couple example/utility trackers.
8+
//!
9+
//! It is a private module but all types and traits are re-exported above.
10+
11+
use simplicity_sys::ffi::UWORD;
12+
use std::collections::HashSet;
13+
14+
use crate::jet::Jet;
15+
use crate::{Cmr, Ihr, Value};
16+
17+
/// A type that keeps track of Bit Machine execution.
18+
///
19+
/// The trait is implemented for [`SetTracker`], that tracks which case branches were executed,
20+
/// and it is implemented for [`NoTracker`], which is a dummy tracker that is
21+
/// optimized out by the compiler.
22+
///
23+
/// The trait enables us to turn tracking on or off depending on a generic parameter.
24+
pub trait ExecTracker<J: Jet> {
25+
/// Track the execution of the left branch of the case node with the given `ihr`.
26+
fn track_left(&mut self, ihr: Ihr);
27+
28+
/// Track the execution of the right branch of the case node with the given `ihr`.
29+
fn track_right(&mut self, ihr: Ihr);
30+
31+
/// Track the execution of a `jet` call with the given `input_buffer`, `output_buffer`, and call result `success`.
32+
fn track_jet_call(
33+
&mut self,
34+
jet: &J,
35+
input_buffer: &[UWORD],
36+
output_buffer: &[UWORD],
37+
success: bool,
38+
);
39+
40+
/// Track the potential execution of a `dbg!` call with the given `cmr` and `value`.
41+
fn track_dbg_call(&mut self, cmr: &Cmr, value: Value);
42+
43+
/// Check if tracking debug calls is enabled.
44+
fn is_track_debug_enabled(&self) -> bool;
45+
}
46+
47+
/// Tracker of executed left and right branches for each case node.
48+
#[derive(Clone, Debug, Default)]
49+
pub struct SetTracker {
50+
left: HashSet<Ihr>,
51+
right: HashSet<Ihr>,
52+
}
53+
54+
impl SetTracker {
55+
/// Access the set of IHRs of case nodes whose left branch was executed.
56+
pub fn left(&self) -> &HashSet<Ihr> {
57+
&self.left
58+
}
59+
60+
/// Access the set of IHRs of case nodes whose right branch was executed.
61+
pub fn right(&self) -> &HashSet<Ihr> {
62+
&self.right
63+
}
64+
}
65+
66+
/// Tracker that does not do anything (noop).
67+
#[derive(Copy, Clone, Debug)]
68+
pub struct NoTracker;
69+
70+
impl<J: Jet> ExecTracker<J> for SetTracker {
71+
fn track_left(&mut self, ihr: Ihr) {
72+
self.left.insert(ihr);
73+
}
74+
75+
fn track_right(&mut self, ihr: Ihr) {
76+
self.right.insert(ihr);
77+
}
78+
79+
fn track_jet_call(&mut self, _: &J, _: &[UWORD], _: &[UWORD], _: bool) {}
80+
81+
fn track_dbg_call(&mut self, _: &Cmr, _: Value) {}
82+
83+
fn is_track_debug_enabled(&self) -> bool {
84+
false
85+
}
86+
}
87+
88+
impl<J: Jet> ExecTracker<J> for NoTracker {
89+
fn track_left(&mut self, _: Ihr) {}
90+
91+
fn track_right(&mut self, _: Ihr) {}
92+
93+
fn track_jet_call(&mut self, _: &J, _: &[UWORD], _: &[UWORD], _: bool) {}
94+
95+
fn track_dbg_call(&mut self, _: &Cmr, _: Value) {}
96+
97+
fn is_track_debug_enabled(&self) -> bool {
98+
// Set flag to test frame decoding in unit tests
99+
cfg!(test)
100+
}
101+
}

0 commit comments

Comments
 (0)