1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
use std::{
    collections::{BTreeMap, HashMap, HashSet},
    fmt::Display,
    ops::Range,
};

use anyhow::bail;
use dialoguer::{theme::ColorfulTheme, BasicHistory, Confirm, FuzzySelect, History, Input, Select};
use essential_constraint_asm::Op;
use essential_constraint_vm::{
    error::OpError, mut_keys_set, transient_data, Access, BytecodeMapped, OpAccess,
    ProgramControlFlow, Repeat, SolutionAccess, Stack, StateSlotSlice, StateSlots, TransientData,
};
use essential_types::{
    predicate::Predicate,
    solution::{Solution, SolutionDataIndex},
    ContentAddress, Key, Value, Word,
};

pub use source::Source;

mod parse_types;
mod source;
mod state;

const PROMPT: &str = "<essential-dbg>";
const PRIMITIVES: &[&str] = &["int", "bool", "b256"];
const COMPOUND: &[&str] = &["array", "tuple"];
const SHOW: &[&str] = &["transient", "pre state", "post state", "decision vars"];

pub struct ConstraintDebugger {
    stack: Stack,
    memory: essential_constraint_vm::Memory,
    repeat: Repeat,
    pc: usize,
    code: BytecodeMapped<Op>,
    solution: Solution,
    pre_state: Vec<Vec<Word>>,
    post_state: Vec<Vec<Word>>,
    index: SolutionDataIndex,
}

pub struct Session<'a> {
    solution: &'a Solution,
    index: SolutionDataIndex,
    mutable_keys: HashSet<&'a [Word]>,
    transient_data: TransientData,
    pre: &'a StateSlotSlice,
    post: &'a StateSlotSlice,
    code: &'a mut BytecodeMapped<Op>,
    stack: &'a mut Stack,
    memory: &'a mut essential_constraint_vm::Memory,
    repeat: &'a mut Repeat,
    pc: &'a mut usize,
    last_op: Option<essential_constraint_asm::Constraint>,
    pos: usize,
}

pub enum Outcome {
    ProgramEnd,
    Step,
    Panic(OpError),
}

pub async fn run_with_source(
    solution: Solution,
    index: SolutionDataIndex,
    predicate: Predicate,
    constraint: usize,
    state: HashMap<ContentAddress, BTreeMap<Key, Value>>,
    source: Source,
) -> anyhow::Result<()> {
    run_inner(solution, index, predicate, constraint, state, Some(source)).await
}

pub async fn run(
    solution: Solution,
    index: SolutionDataIndex,
    predicate: Predicate,
    constraint: usize,
    state: HashMap<ContentAddress, BTreeMap<Key, Value>>,
) -> anyhow::Result<()> {
    run_inner(solution, index, predicate, constraint, state, None).await
}

async fn run_inner(
    solution: Solution,
    index: SolutionDataIndex,
    predicate: Predicate,
    constraint: usize,
    state: HashMap<ContentAddress, BTreeMap<Key, Value>>,
    source: Option<Source>,
) -> anyhow::Result<()> {
    let mut debugger =
        ConstraintDebugger::new(solution, index, predicate, constraint, state).await?;
    let mut session = debugger.start_session();

    let mut out = String::new();

    let mut history = BasicHistory::new().max_entries(20).no_duplicates(true);

    loop {
        let command: String = Input::with_theme(&ColorfulTheme::default())
            .with_prompt(&format!("{}\n{}", out, PROMPT))
            .history_with(&mut history)
            .interact_text()?;

        match command.as_str() {
            "n" | "next" => session.next(&mut out)?,
            "b" | "back" => session.back(&mut out)?,
            "e" | "end" => session.play_till_error(&mut out)?,
            "q" | "quit" | "exit" => break,
            "h" | "help" => {
                out = help_msg();
            }
            "h t" | "help type" | "h type" | "help t" => {
                out = types_msg();
            }
            "h c" | "help code" | "h code" | "help c" => {
                out = help_code();
            }
            "s" | "show" => {
                let prompt = format!("{}::show", PROMPT);
                let selection = FuzzySelect::with_theme(&ColorfulTheme::default())
                    .with_prompt(&format!("What would you like to show?\n{}", prompt))
                    .default(0)
                    .items(SHOW)
                    .interact()?;
                match SHOW[selection] {
                    "transient" => {
                        let prompt = format!("{}::transient", prompt);
                        let indices = (0..session.solution.data.len()).collect::<Vec<_>>();
                        let selection = Select::with_theme(&ColorfulTheme::default())
                            .with_prompt(&format!("Which solution data?\n{}", prompt))
                            .default(0)
                            .items(&indices)
                            .interact()?;

                        let prompt = format!("{}::{}", prompt, selection);
                        let t = session
                            .transient_data
                            .get(&(selection as u16))
                            .expect("Can't be out of bounds");
                        let keys: Vec<String> = t
                            .keys()
                            .map(|k| {
                                k.iter()
                                    .map(|i| i.to_string())
                                    .collect::<Vec<String>>()
                                    .join(" ")
                            })
                            .collect();
                        let selection = FuzzySelect::with_theme(&ColorfulTheme::default())
                            .with_prompt(&format!("Which key would you like to show?\n{}", prompt))
                            .default(0)
                            .items(&keys)
                            .interact()?;
                        let key = keys[selection]
                            .split(' ')
                            .map(|i| i.parse().unwrap())
                            .collect::<Vec<_>>();
                        let v = t.get(&key).unwrap();
                        out = format!("Transient data: {:?} => {:?}", key, v);
                    }
                    "pre state" => {
                        let prompt = format!("{}::pre", prompt);
                        let indices = (0..session.pre.len()).collect::<Vec<_>>();
                        let selection = Select::with_theme(&ColorfulTheme::default())
                            .with_prompt(&format!("Which slot would you like to show?\n{}", prompt))
                            .default(0)
                            .items(&indices)
                            .interact()?;
                        let v = &session.pre[selection];
                        out = format!("Pre state slot {}: {:?}", selection, v);
                    }
                    "post state" => {
                        let prompt = format!("{}::post", prompt);
                        let indices = (0..session.post.len()).collect::<Vec<_>>();
                        let selection = Select::with_theme(&ColorfulTheme::default())
                            .with_prompt(&format!("Which slot would you like to show?\n{}", prompt))
                            .default(0)
                            .items(&indices)
                            .interact()?;
                        let v = &session.post[selection];
                        out = format!("Post state slot {}: {:?}", selection, v);
                    }
                    "decision vars" => {
                        let prompt = format!("{}::decision_vars", prompt);
                        let indices = (0..session.solution.data[index as usize]
                            .decision_variables
                            .len())
                            .collect::<Vec<_>>();
                        let selection = Select::with_theme(&ColorfulTheme::default())
                            .with_prompt(&format!(
                                "Which solution data slot would you like to show?\n{}",
                                prompt
                            ))
                            .default(0)
                            .items(&indices)
                            .interact()?;
                        let v =
                            &session.solution.data[index as usize].decision_variables[selection];
                        out = format!("Decision variable {}: {:?}", selection, v);
                    }
                    _ => unreachable!(),
                }
            }
            _ => {
                let mut c = command.split(' ');

                let Some(next_command) = c.next() else {
                    out = format!("Unknown command: {}", command);
                    continue;
                };
                match next_command {
                    "p" | "play" => {
                        let i = c
                            .next()
                            .and_then(|i| i.parse::<usize>().ok())
                            .unwrap_or_default();
                        session.play(i, &mut out)?;
                    }
                    "l" | "list" => match c.next() {
                        Some(i) => {
                            let start = i.parse::<isize>().unwrap_or(0);
                            let end = c.next().and_then(|i| i.parse::<isize>().ok()).unwrap_or(10);
                            session.list_range(start..end, &mut out);
                        }
                        None => session.list(&mut out),
                    },
                    "t" | "type" => {
                        let rest = c.filter(|s| !s.is_empty()).collect::<Vec<_>>().join(" ");
                        if rest.is_empty() {
                            let prompt = format!("{}::type", PROMPT);
                            let pos: String = Input::with_theme(&ColorfulTheme::default())
                                .with_prompt(&format!("Enter position\n{}", prompt))
                                .default("0".to_string())
                                .history_with(&mut history)
                                .interact_text()?;
                            let pos: usize = pos.trim().parse().unwrap_or_default();

                            let prompt = format!("{}::{}", prompt, pos);
                            let mut options = PRIMITIVES.to_vec();
                            options.extend_from_slice(COMPOUND);

                            let selection = FuzzySelect::with_theme(&ColorfulTheme::default())
                                .with_prompt(&format!("Select type\n{}", prompt))
                                .default(0)
                                .items(&options[..])
                                .interact()?;
                            if PRIMITIVES.contains(&options[selection]) {
                                let input = format!("{} {}", pos, &options[selection]);
                                out = session.parse_type(&input);
                            } else {
                                let prompt = format!("{}::{}", prompt, options[selection]);
                                let input =
                                    match options[selection] {
                                        "array" => {
                                            let selection =
                                                FuzzySelect::with_theme(&ColorfulTheme::default())
                                                    .with_prompt(&format!(
                                                        "Select array type\n{}",
                                                        prompt
                                                    ))
                                                    .default(0)
                                                    .items(PRIMITIVES)
                                                    .interact()?;

                                            let prompt =
                                                format!("{}::{}", prompt, PRIMITIVES[selection]);
                                            let len: String =
                                                Input::with_theme(&ColorfulTheme::default())
                                                    .with_prompt(&format!(
                                                        "Enter array length\n{}",
                                                        prompt
                                                    ))
                                                    .default("1".to_string())
                                                    .history_with(&mut history)
                                                    .interact_text()?;
                                            let len: usize = len.trim().parse().unwrap_or_default();
                                            format!("{} {}[{}]", pos, PRIMITIVES[selection], len)
                                        }
                                        "tuple" => {
                                            let mut fields = String::new();
                                            let mut add_field = true;
                                            while add_field {
                                                let p = format!("{}::{{ {} }}", prompt, fields);
                                                let selection = FuzzySelect::with_theme(
                                                    &ColorfulTheme::default(),
                                                )
                                                .with_prompt(&format!("Select field type\n{}", p))
                                                .default(0)
                                                .items(PRIMITIVES)
                                                .interact()?;

                                                fields.push_str(PRIMITIVES[selection]);

                                                let p = format!("{}::{{ {} }}", prompt, fields);

                                                add_field =
                                                    Confirm::with_theme(&ColorfulTheme::default())
                                                        .with_prompt(&format!(
                                                            "Do you want to add another field?\n{}",
                                                            p
                                                        ))
                                                        .default(true)
                                                        .interact()?;
                                                if add_field {
                                                    fields.push_str(", ");
                                                }
                                            }
                                            format!("{} {{ {} }}", pos, fields)
                                        }
                                        _ => unreachable!(),
                                    };

                                let force_hex = Confirm::with_theme(&ColorfulTheme::default())
                                    .with_prompt(&format!(
                                        "Do you want to force HEX formatting?\n{}",
                                        prompt
                                    ))
                                    .default(false)
                                    .interact()?;
                                let input = if force_hex {
                                    format!("{} HEX", input)
                                } else {
                                    input
                                };
                                history.write(&format!("t {}", input));
                                out = session.parse_type(&input);
                            }
                        } else {
                            out = session.parse_type(&rest);
                        }
                    }
                    "c" | "code" => {
                        out = source::show_code(&source, c.next().into());
                    }
                    _ => {
                        out = format!("Unknown command: {}", command);
                    }
                }
            }
        }
    }

    Ok(())
}

fn end(out: &mut String) {
    out.push_str("\nProgram ended");
}

fn help_msg() -> String {
    r#"Commands:
    n | next: Step forward
    b | back: Step back
    p | play [i]: Play to ith op
    e | end: Play till end or error is hit
    l | list [start] [end]: List ops from start to end
    s | show: Show transient data, pre state, or post state
    c | code: Show source code. See `help code` for more info.
    t | type <i> [type]: Parse the ith word in the stack as the given type. See `help type` for more info.
    q | quit | exit: Quit
    h | help: Show this message
    "#
    .to_string()
}

fn types_msg() -> String {
    r#"Primitives: int, bool, b256
    Arrays: primitive[] (e.g. int[])
    Tuple: { primitive, primitive, ... } (e.g. {int, bool, b256})
    Note that nesting types is not currently supported.
    To parse a section of the stack as a type, use `t <i> [type]` 
    e.g. `t 1 int[2]` to parse the second and third word as ints.
    `b256` is always printed as hex. 
    You can force hex formatting by adding `HEX` to the end of the command.
    e.g. `t 1 int HEX`
    "#
    .to_string()
}

fn help_code() -> String {
    r#"Shows source code if present.
    c | code: equivalent to `code constraint`.
    `code` can be followed by:
    Commands:
    a | all: Show all source code
    p | predicate: Show predicate source code
    c | constraint: Show predicate source code with only the constraint
        that is being debugged. Constraint line number is required.
    co | constraint_only: Show only the constraint line.
        Constraint line number is required.
    "#
    .to_string()
}

impl ConstraintDebugger {
    pub async fn new(
        solution: Solution,
        index: SolutionDataIndex,
        predicate: Predicate,
        constraint: usize,
        state: HashMap<ContentAddress, BTreeMap<Key, Value>>,
    ) -> anyhow::Result<Self> {
        let slots = state::read_state(&solution, index, &predicate, state.clone()).await?;

        let Some(code) = predicate.constraints.get(constraint).cloned() else {
            bail!("No constraint found");
        };

        let code = BytecodeMapped::try_from_bytes(code)?;
        let s = Self {
            stack: Default::default(),
            memory: Default::default(),
            repeat: Default::default(),
            pc: 0,
            code,
            solution,
            pre_state: slots.pre,
            post_state: slots.post,
            index,
        };
        Ok(s)
    }

    pub fn start_session(&mut self) -> Session<'_> {
        let mutable_keys = mut_keys_set(&self.solution, self.index);
        let transient_data = transient_data(&self.solution);
        Session {
            code: &mut self.code,
            stack: &mut self.stack,
            memory: &mut self.memory,
            repeat: &mut self.repeat,
            pc: &mut self.pc,
            last_op: None,
            solution: &self.solution,
            index: self.index,
            mutable_keys,
            transient_data,
            pre: &self.pre_state,
            post: &self.post_state,
            pos: 0,
        }
    }
}

fn handle_outcome(outcome: Outcome, out: &mut String) {
    match outcome {
        Outcome::ProgramEnd => end(out),
        Outcome::Panic(e) => {
            *out = format!("Program panic: {:?}\n{}", e, out);
        }
        Outcome::Step => (),
    }
}

impl Session<'_> {
    pub fn reset_session(&mut self) {
        *self.stack = Default::default();
        *self.memory = Default::default();
        *self.repeat = Default::default();
        *self.pc = 0;
        self.pos = 0;
    }

    pub fn next(&mut self, out: &mut String) -> anyhow::Result<()> {
        let outcome = self.step_forward()?;

        *out = format!("{}", self);

        handle_outcome(outcome, out);
        Ok(())
    }

    pub fn back(&mut self, out: &mut String) -> anyhow::Result<()> {
        let pos = self.pos.saturating_sub(1);
        self.reset_session();
        let outcome = self.play_to(pos)?;
        *out = format!("{}", self);

        handle_outcome(outcome, out);
        Ok(())
    }

    pub fn play(&mut self, i: usize, out: &mut String) -> anyhow::Result<()> {
        self.reset_session();
        let outcome = self.play_to(i)?;
        *out = format!("{}", self);

        handle_outcome(outcome, out);
        Ok(())
    }

    pub fn play_till_error(&mut self, out: &mut String) -> anyhow::Result<()> {
        loop {
            match self.step_forward()? {
                Outcome::Step => (),
                Outcome::ProgramEnd => match &self.stack[..] {
                    [1] => {
                        *out = format!("Program ended successfully.\n{}", self);
                        break;
                    }
                    [0] => {
                        *out = format!("Program ended with false!\n{}", self);
                        break;
                    }
                    _ => {
                        *out = format!(
                            "Program ended with unexpected stack: {:?}\n{}",
                            self.stack, self
                        );
                        break;
                    }
                },
                Outcome::Panic(e) => {
                    *out = format!("Program panic: {:?}\n{}", e, self);
                    break;
                }
            }
        }
        Ok(())
    }

    pub fn step_forward(&mut self) -> anyhow::Result<Outcome> {
        let Self {
            code,
            stack,
            memory,
            repeat,
            pc,
            last_op,
            solution,
            index,
            mutable_keys,
            transient_data,
            pre,
            post,
            pos,
        } = self;

        let access = Access {
            solution: SolutionAccess::new(solution, *index, mutable_keys, transient_data),
            state_slots: StateSlots { pre, post },
        };

        let op = (&**code).op_access(**pc);

        let op = match op {
            Some(Ok(op)) => op,
            Some(Err(err)) => {
                // Handle error
                bail!("Error: {:?}", err);
            }
            None => {
                // end of program
                return Ok(Outcome::ProgramEnd);
            }
        };

        last_op.replace(op);

        let result = match essential_constraint_vm::step_op(access, op, stack, memory, **pc, repeat)
        {
            Ok(r) => r,
            Err(e) => {
                *pos += 1;
                return Ok(Outcome::Panic(e));
            }
        };
        *pos += 1;

        match result {
            Some(ProgramControlFlow::Pc(new_pc)) => {
                **pc = new_pc;
                Ok(Outcome::Step)
            }
            Some(ProgramControlFlow::Halt) => Ok(Outcome::ProgramEnd),
            None => {
                **pc += 1;
                Ok(Outcome::Step)
            }
        }
    }

    pub fn play_to(&mut self, i: usize) -> anyhow::Result<Outcome> {
        let mut out = None;
        let i = if i == 0 { 1 } else { i };
        for _ in 0..i {
            match self.step_forward()? {
                Outcome::ProgramEnd => return Ok(Outcome::ProgramEnd),
                Outcome::Panic(e) => return Ok(Outcome::Panic(e)),
                Outcome::Step => {
                    out = Some(Outcome::Step);
                }
            }
        }
        let Some(out) = out else {
            bail!("Program didn't run");
        };
        Ok(out)
    }

    pub fn list_range(&self, range: Range<isize>, out: &mut String) {
        use std::fmt::Write;
        let start = (self.pos as isize).saturating_add(range.start).max(0) as usize;
        let end = (self.pos as isize).saturating_add(range.end).max(0) as usize;
        let len = end.saturating_sub(start);
        let this_op = (start..end)
            .contains(&self.pos)
            .then_some(self.pos.saturating_sub(start));
        if let Some(ops) = &self.code.ops_from(start) {
            *out = ops
                .ops()
                .take(len)
                .enumerate()
                .fold(String::new(), |mut out, (i, op)| {
                    match &this_op {
                        Some(this_op) if *this_op == i => {
                            let _ = writeln!(
                                out,
                                "{}:Op: {:?}",
                                start + i,
                                dialoguer::console::style(op).cyan()
                            );
                        }
                        _ => {
                            let _ = writeln!(out, "{}:Op: {:?}", start + i, op);
                        }
                    }
                    out
                });
        }
    }

    pub fn list(&self, out: &mut String) {
        use std::fmt::Write;
        if let Some(ops) = &self.code.ops_from(0) {
            *out = ops
                .ops()
                .enumerate()
                .fold(String::new(), |mut out, (i, op)| {
                    if self.pos == i {
                        let _ =
                            writeln!(out, "{}:Op: {:?}", i, dialoguer::console::style(op).cyan());
                    } else {
                        let _ = writeln!(out, "{}:Op: {:?}", i, op);
                    }
                    out
                });
        }
    }

    pub fn parse_type(&self, ty: &str) -> String {
        parse_types::parse_type(&self.stack[..], ty)
    }
}

impl Display for Session<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let Some(op) = &self.last_op {
            writeln!(f, "Op: {:?}", op)?;
        }
        writeln!(f, "  ├── {:?}\n  └── {:?}", self.stack, self.memory)
    }
}