Skip to content

Commit 42bad3a

Browse files
committed
PR cleanup - remove redundent logic & structs
1 parent bca54f3 commit 42bad3a

File tree

11 files changed

+668
-484
lines changed

11 files changed

+668
-484
lines changed

Cargo.lock

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

debugger/session/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pub mod presentation;
22
pub mod session;
3+
pub mod util;

debugger/session/src/presentation.rs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use silverscript_lang::debug_info::SourceSpan;
22

33
use crate::session::DebugValue;
4+
use crate::util::{decode_i64, encode_hex};
45

56
#[derive(Debug, Clone)]
67
pub struct SourceContextLine {
@@ -110,28 +111,3 @@ fn array_element_size(element_type: &str) -> Option<usize> {
110111
other => other.strip_prefix("bytes").and_then(|v| v.parse::<usize>().ok()),
111112
}
112113
}
113-
114-
fn decode_i64(bytes: &[u8]) -> Result<i64, String> {
115-
if bytes.is_empty() {
116-
return Ok(0);
117-
}
118-
if bytes.len() > 8 {
119-
return Err("numeric value is longer than 8 bytes".to_string());
120-
}
121-
let msb = bytes[bytes.len() - 1];
122-
let sign = 1 - 2 * ((msb >> 7) as i64);
123-
let first_byte = (msb & 0x7f) as i64;
124-
let mut value = first_byte;
125-
for byte in bytes[..bytes.len() - 1].iter().rev() {
126-
value = (value << 8) + (*byte as i64);
127-
}
128-
Ok(value * sign)
129-
}
130-
131-
fn encode_hex(bytes: &[u8]) -> String {
132-
let mut out = vec![0u8; bytes.len() * 2];
133-
if faster_hex::hex_encode(bytes, &mut out).is_err() {
134-
return String::new();
135-
}
136-
String::from_utf8(out).unwrap_or_default()
137-
}

debugger/session/src/session.rs

Lines changed: 1 addition & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use kaspa_txscript::caches::Cache;
66
use kaspa_txscript::covenants::CovenantsContext;
77
use kaspa_txscript::script_builder::ScriptBuilder;
88
use kaspa_txscript::{DynOpcodeImplementation, EngineCtx, EngineFlags, TxScriptEngine, parse_script};
9-
use serde::{Deserialize, Serialize};
109

1110
use silverscript_lang::ast::{Expr, ExprKind};
1211
use silverscript_lang::compiler::compile_debug_expr;
@@ -16,6 +15,7 @@ use silverscript_lang::debug_info::{
1615

1716
pub use crate::presentation::{SourceContext, SourceContextLine};
1817
use crate::presentation::{build_source_context, format_value as format_debug_value};
18+
use crate::util::{decode_i64, encode_hex};
1919

2020
pub type DebugTx<'a> = PopulatedTransaction<'a>;
2121
pub type DebugReused = SigHashReusedValuesUnsync;
@@ -76,20 +76,6 @@ pub struct SessionState {
7676
pub stack: Vec<String>,
7777
}
7878

79-
#[derive(Debug, Clone, Serialize, Deserialize)]
80-
pub struct StackSnapshot {
81-
pub dstack: Vec<String>,
82-
pub astack: Vec<String>,
83-
}
84-
85-
#[derive(Debug, Clone, Serialize, Deserialize)]
86-
pub struct OpcodeMeta {
87-
pub index: usize,
88-
pub byte_offset: usize,
89-
pub display: String,
90-
pub mapping: Option<DebugMapping>,
91-
}
92-
9379
pub struct DebugSession<'a, 'i> {
9480
engine: DebugEngine<'a>,
9581
shadow_tx_context: Option<ShadowTxContext<'a>>,
@@ -124,17 +110,6 @@ struct VariableContext<'a> {
124110
impl<'a, 'i> DebugSession<'a, 'i> {
125111
// --- Session construction + stepping ---
126112

127-
/// Creates a debug session for lockscript-only execution.
128-
/// Use this when debugging pure contract logic without sigscript setup.
129-
pub fn lockscript_only(
130-
script: &[u8],
131-
source: &str,
132-
debug_info: Option<DebugInfo<'i>>,
133-
engine: DebugEngine<'a>,
134-
) -> Result<Self, kaspa_txscript_errors::TxScriptError> {
135-
Self::from_scripts(script, source, debug_info, engine)
136-
}
137-
138113
/// Creates a debug session simulating a full transaction spend.
139114
/// Executes sigscript first to seed the stack, then debugs lockscript execution.
140115
pub fn full(
@@ -239,11 +214,6 @@ impl<'a, 'i> DebugSession<'a, 'i> {
239214
self.step_with_depth_predicate(|candidate, current| candidate < current)
240215
}
241216

242-
/// Backward-compatible statement stepping alias.
243-
pub fn step_statement(&mut self) -> Result<Option<SessionState>, kaspa_txscript_errors::TxScriptError> {
244-
self.step_over()
245-
}
246-
247217
/// Shared stepping loop for `step_into`, `step_over`, and `step_out`.
248218
/// Picks the next steppable mapping whose call depth satisfies `predicate`,
249219
/// executes opcodes until that mapping becomes active, and skips candidates
@@ -354,35 +324,6 @@ impl<'a, 'i> DebugSession<'a, 'i> {
354324
self.engine.is_executing()
355325
}
356326

357-
/// Returns the current data and alt stack contents.
358-
pub fn stacks_snapshot(&self) -> StackSnapshot {
359-
let stacks = self.engine.stacks();
360-
StackSnapshot {
361-
dstack: stacks.dstack.iter().map(|item| encode_hex(item)).collect(),
362-
astack: stacks.astack.iter().map(|item| encode_hex(item)).collect(),
363-
}
364-
}
365-
366-
/// Returns metadata for all opcodes (executed/pending status, byte offset).
367-
pub fn opcode_metas(&self) -> Vec<OpcodeMeta> {
368-
(0..self.op_displays.len())
369-
.map(|index| {
370-
let byte_offset = self.opcode_offsets.get(index).copied().unwrap_or(self.script_len);
371-
OpcodeMeta {
372-
index,
373-
byte_offset,
374-
display: self.op_displays.get(index).cloned().unwrap_or_default(),
375-
mapping: self.mapping_for_offset(byte_offset).cloned(),
376-
}
377-
})
378-
.collect()
379-
}
380-
381-
/// Returns the total number of opcodes in the script.
382-
pub fn opcode_count(&self) -> usize {
383-
self.op_displays.len()
384-
}
385-
386327
pub fn debug_info(&self) -> &DebugInfo<'i> {
387328
&self.debug_info
388329
}
@@ -807,26 +748,6 @@ fn decode_value_by_type(type_name: &str, bytes: Vec<u8>) -> Result<DebugValue, S
807748
}
808749
}
809750

810-
/// Decodes a txscript script number (little-endian sign-magnitude, max 8 bytes).
811-
/// Mirrors txscript's internal numeric decode logic; kept local because txscript
812-
/// exposes this helper only as crate-private internals today.
813-
fn decode_i64(bytes: &[u8]) -> Result<i64, String> {
814-
if bytes.is_empty() {
815-
return Ok(0);
816-
}
817-
if bytes.len() > 8 {
818-
return Err("numeric value is longer than 8 bytes".to_string());
819-
}
820-
let msb = bytes[bytes.len() - 1];
821-
let sign = 1 - 2 * ((msb >> 7) as i64);
822-
let first_byte = (msb & 0x7f) as i64;
823-
let mut value = first_byte;
824-
for byte in bytes[..bytes.len() - 1].iter().rev() {
825-
value = (value << 8) + (*byte as i64);
826-
}
827-
Ok(value * sign)
828-
}
829-
830751
/// Executes sigscript to seed the stack before debugging lockscript.
831752
fn seed_engine_with_sigscript(engine: &mut DebugEngine<'_>, sigscript: &[u8]) -> Result<(), kaspa_txscript_errors::TxScriptError> {
832753
for opcode in parse_script::<DebugTx<'_>, DebugReused>(sigscript) {
@@ -853,7 +774,6 @@ fn mapping_kind_order(kind: &MappingKind) -> u8 {
853774
MappingKind::Virtual {} => 1,
854775
MappingKind::Statement {} => 2,
855776
MappingKind::InlineCallExit { .. } => 3,
856-
MappingKind::Synthetic { .. } => 4,
857777
}
858778
}
859779

@@ -865,14 +785,6 @@ fn mapping_matches_offset(mapping: &DebugMapping, offset: usize) -> bool {
865785
}
866786
}
867787

868-
fn encode_hex(bytes: &[u8]) -> String {
869-
let mut out = vec![0u8; bytes.len() * 2];
870-
if faster_hex::hex_encode(bytes, &mut out).is_err() {
871-
return String::new();
872-
}
873-
String::from_utf8(out).unwrap_or_default()
874-
}
875-
876788
#[cfg(test)]
877789
mod tests {
878790
use super::*;

debugger/session/src/util.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/// Decodes a txscript script number (little-endian sign-magnitude, max 8 bytes).
2+
/// Mirrors txscript's internal numeric decode logic; kept local because txscript
3+
/// exposes this helper only as crate-private internals today.
4+
pub fn decode_i64(bytes: &[u8]) -> Result<i64, String> {
5+
if bytes.is_empty() {
6+
return Ok(0);
7+
}
8+
if bytes.len() > 8 {
9+
return Err("numeric value is longer than 8 bytes".to_string());
10+
}
11+
let msb = bytes[bytes.len() - 1];
12+
let sign = 1 - 2 * ((msb >> 7) as i64);
13+
let first_byte = (msb & 0x7f) as i64;
14+
let mut value = first_byte;
15+
for byte in bytes[..bytes.len() - 1].iter().rev() {
16+
value = (value << 8) + (*byte as i64);
17+
}
18+
Ok(value * sign)
19+
}
20+
21+
pub fn encode_hex(bytes: &[u8]) -> String {
22+
let mut out = vec![0u8; bytes.len() * 2];
23+
if faster_hex::hex_encode(bytes, &mut out).is_err() {
24+
return String::new();
25+
}
26+
String::from_utf8(out).unwrap_or_default()
27+
}

debugger/session/tests/debug_session_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ fn debug_session_steps_forward() -> Result<(), Box<dyn Error>> {
114114
session.run_to_first_executed_statement()?;
115115
let before = session.state().pc;
116116
let before_span = session.current_span();
117-
session.step_statement()?;
117+
session.step_over()?;
118118
let after = session.state().pc;
119119
let after_span = session.current_span();
120120
assert!(after > before || after_span != before_span, "expected statement step to make source progress");

0 commit comments

Comments
 (0)