clvm_tools_rs 0.4.0

tools for working with chialisp language; compiler, repl, python and wasm bindings
Documentation
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
use std::borrow::Borrow;
use std::collections::{BTreeMap, HashMap};
use std::rc::Rc;

use clvm_rs::allocator::Allocator;
use num_bigint::ToBigInt;

use crate::classic::clvm::__type_compatibility__::{Bytes, BytesFromType};
use crate::classic::clvm_tools::stages::stage_0::TRunProgram;

use crate::compiler::cldb::{CldbNoOverride, CldbRun, CldbRunEnv};
use crate::compiler::clvm;
use crate::compiler::clvm::{sha256tree, truthy, RunStep};
use crate::compiler::runtypes::RunFailure;
use crate::compiler::sexp::{decode_string, parse_sexp, SExp};
use crate::compiler::srcloc::Srcloc;

#[derive(Clone, Debug)]
pub struct RunStepRelevantInfo {
    name: String,
    hash: Vec<u8>,
    prog: Rc<SExp>,
    formal_parameters: Rc<SExp>,
    runtime_argument_values: Rc<SExp>,
    left_env: bool,
}

#[derive(Clone, Debug)]
pub enum RunPurpose {
    ComputeArgument,
    Main,
}

pub struct HierarchyFrame {
    pub purpose: RunPurpose,

    pub prog: Rc<SExp>,
    pub env: Rc<SExp>,

    pub function_hash: Vec<u8>,
    pub function_name: String,
    pub function_arguments: Rc<SExp>,
    pub function_left_env: bool,

    pub source: Srcloc,
    pub named_args: HashMap<String, Rc<SExp>>,

    pub run: CldbRun,
}

pub struct HierarchialRunner {
    allocator: Allocator,
    runner: Rc<dyn TRunProgram>,
    prim_map: Rc<HashMap<Vec<u8>, Rc<SExp>>>,
    symbol_table: Rc<HashMap<String, String>>,
    error: bool,
    input_file: Option<String>,
    program_lines: Rc<Vec<String>>,
    prog: Rc<SExp>,
    flags: u32,

    pub running: Vec<HierarchyFrame>,
}

#[derive(Clone, Debug)]
pub enum HierarchialStepResult {
    ShapeChange,
    Info(Option<BTreeMap<String, String>>),
    Done(Option<BTreeMap<String, String>>),
}

pub enum RunClass {
    Primitive(Rc<SExp>),
    Application(RunStepRelevantInfo),
}

pub fn hex_of_hash(hash: &[u8]) -> String {
    Bytes::new(Some(BytesFromType::Raw(hash.to_vec()))).hex()
}

pub fn is_op(v: u8, op: Rc<SExp>) -> bool {
    match op.borrow() {
        SExp::Integer(_, i) => *i == v.to_bigint().unwrap(),
        SExp::Atom(_, n) => *n == vec![v],
        SExp::QuotedString(_, _, n) => *n == vec![v],
        _ => false,
    }
}

pub fn is_apply_op(op: Rc<SExp>) -> bool {
    is_op(2, op)
}

pub fn get_fun_hash(op: Rc<SExp>, sexp: Rc<SExp>) -> Option<(Vec<u8>, Rc<SExp>, Rc<SExp>)> {
    if let SExp::Cons(_, prog, args) = sexp.borrow() {
        if is_apply_op(op) {
            if let SExp::Cons(_, env, _) = args.borrow() {
                return Some((clvm::sha256tree(prog.clone()), prog.clone(), env.clone()));
            }
        }
    }

    None
}

pub fn relevant_run_step_info(
    symbol_table: &HashMap<String, String>,
    step: &RunStep,
) -> Option<RunStepRelevantInfo> {
    if let RunStep::Op(op, _, args, Some(remaining_args), _) = step {
        if remaining_args.is_empty() {
            return get_fun_hash(op.clone(), args.clone())
                .and_then(|(hash, prog, env)| make_relevant_info(symbol_table, &hash, prog, env));
        }
    }

    None
}

fn sexp_from_symbol_table(
    symbol_table: &HashMap<String, String>,
    item_name: &str,
) -> Option<Rc<SExp>> {
    let loc = Srcloc::compiler_internal_srcloc();
    symbol_table.get(item_name).and_then(|data| {
        parse_sexp(loc.clone(), data.as_bytes().iter().copied())
            .ok()
            .and_then(|p| {
                if p.is_empty() {
                    None
                } else {
                    Some(Rc::new(p[0].atomize()))
                }
            })
    })
}

fn uses_left_env(symbol_table: &HashMap<String, String>, hash: &[u8]) -> bool {
    let hex_hash = hex_of_hash(hash);
    let item_name = format!("{hex_hash}_left_env");
    sexp_from_symbol_table(symbol_table, &item_name)
        .map(truthy)
        .unwrap_or_else(|| false)
}

fn make_relevant_info(
    symbol_table: &HashMap<String, String>,
    hash: &[u8],
    prog: Rc<SExp>,
    env: Rc<SExp>,
) -> Option<RunStepRelevantInfo> {
    let hex_hash = hex_of_hash(hash);
    let args_name = format!("{hex_hash}_arguments");
    let fun_args = sexp_from_symbol_table(symbol_table, &args_name).unwrap_or_else(|| {
        let name: Vec<u8> = args_name.as_bytes().to_vec();
        Rc::new(SExp::Atom(prog.loc(), name))
    });
    symbol_table
        .get(&hex_hash)
        .map(|fun_name| RunStepRelevantInfo {
            hash: hash.to_vec(),
            name: fun_name.clone(),
            formal_parameters: fun_args,
            prog: prog.clone(),
            runtime_argument_values: env.clone(),
            left_env: uses_left_env(symbol_table, hash),
        })
}

fn get_args_from_env(
    arg_map: &mut HashMap<String, Rc<SExp>>,
    lines: Rc<Vec<String>>,
    args: Rc<SExp>,
    env: Rc<SExp>,
    left_env: bool,
) {
    match (args.atomize(), env.borrow()) {
        (SExp::Cons(_, a, b), SExp::Cons(_, x, y)) => {
            if left_env {
                get_args_from_env(arg_map, lines, args.clone(), y.clone(), false);
                return;
            }

            get_args_from_env(arg_map, lines.clone(), a, x.clone(), false);
            get_args_from_env(arg_map, lines, b, y.clone(), false);
        }
        (SExp::Atom(_, n), _) => {
            arg_map.insert(decode_string(&n), env);
        }
        _ => {}
    }
}

impl HierarchialRunner {
    pub fn new(
        runner: Rc<dyn TRunProgram>,
        prim_map: Rc<HashMap<Vec<u8>, Rc<SExp>>>,
        input_file: Option<String>,
        program_lines: Rc<Vec<String>>,
        symbol_table: Rc<HashMap<String, String>>,
        prog: Rc<SExp>,
        env: Rc<SExp>,
    ) -> Self {
        let step = clvm::start_step(prog.clone(), env.clone());
        let run = CldbRun::new(
            runner.clone(),
            prim_map.clone(),
            Box::new(CldbRunEnv::new(
                input_file.clone(),
                program_lines.clone(),
                Box::new(CldbNoOverride::new()),
            )),
            step,
        );

        let fun_args = sexp_from_symbol_table(symbol_table.borrow(), "__chia__main_arguments")
            .unwrap_or_else(|| Rc::new(SExp::Nil(prog.loc())));

        let mut program_args = HashMap::new();
        get_args_from_env(
            &mut program_args,
            program_lines.clone(),
            fun_args.clone(),
            env.clone(),
            false,
        );

        HierarchialRunner {
            allocator: Allocator::new(),
            runner,
            prim_map,
            symbol_table,
            input_file: input_file.clone(),
            program_lines,
            error: false,
            prog: prog.clone(),
            flags: 0,

            running: vec![HierarchyFrame {
                purpose: RunPurpose::Main,

                prog: prog.clone(),
                env,

                function_hash: sha256tree(prog.clone()),
                function_name: input_file.unwrap_or_else(|| {
                    format!(
                        "clvm_program_{}",
                        hex_of_hash(&clvm::sha256tree(prog.clone()))
                    )
                }),
                function_arguments: fun_args,
                function_left_env: false,

                named_args: program_args,
                source: prog.loc(),

                run,
            }],
        }
    }

    pub fn set_flags(&mut self, flags: u32) {
        self.flags = flags;
        for r in self.running.iter_mut() {
            r.run.set_flags(flags);
        }
    }

    pub fn is_ended(&self) -> bool {
        self.running.is_empty()
            || self.error
            || self.running.len() == 1 && self.running[0].run.is_ended()
    }

    fn push_synthetic_stack_frame(&mut self, current_env: Rc<SExp>, info: &RunStepRelevantInfo) {
        let arg_step = clvm::start_step(info.prog.clone(), info.runtime_argument_values.clone());

        let mut arg_run = CldbRun::new(
            self.runner.clone(),
            self.prim_map.clone(),
            Box::new(CldbRunEnv::new(
                self.input_file.clone(),
                self.program_lines.clone(),
                Box::new(CldbNoOverride::new()),
            )),
            arg_step,
        );

        arg_run.set_flags(self.flags);

        let mut named_args = HashMap::new();
        get_args_from_env(
            &mut named_args,
            self.program_lines.clone(),
            info.formal_parameters.clone(),
            info.runtime_argument_values.clone(),
            info.left_env,
        );

        let arg_frame = HierarchyFrame {
            purpose: RunPurpose::ComputeArgument,

            prog: info.prog.clone(),
            env: info.runtime_argument_values.clone(),

            function_hash: info.hash.clone(),
            function_name: info.name.clone(),
            function_arguments: info.runtime_argument_values.clone(),
            function_left_env: info.left_env,

            named_args: named_args.clone(),

            run: arg_run,
            source: info.prog.loc(),
        };

        // Make an empty frame to repopulate (maybe option here?).
        let step = clvm::start_step(info.prog.clone(), current_env.clone());
        let mut run = CldbRun::new(
            self.runner.clone(),
            self.prim_map.clone(),
            Box::new(CldbRunEnv::new(
                self.input_file.clone(),
                self.program_lines.clone(),
                Box::new(CldbNoOverride::new()),
            )),
            step,
        );

        run.set_flags(self.flags);

        self.running.push(HierarchyFrame {
            purpose: RunPurpose::Main,

            prog: info.prog.clone(),
            env: current_env,

            function_hash: info.hash.clone(),
            function_name: info.name.clone(),
            function_arguments: info.formal_parameters.clone(),
            function_left_env: info.left_env,
            source: info.prog.loc(),

            named_args,

            run,
        });

        self.running.push(arg_frame);
    }

    pub fn step(&mut self) -> Result<HierarchialStepResult, RunFailure> {
        if self.running.is_empty() {
            return Err(RunFailure::RunErr(
                self.prog.loc(),
                "no running code".to_string(),
            ));
        }

        let mut idx = self.running.len() - 1;
        let current_env = self.running[idx].env.clone();
        let current_step = self.running[idx].run.current_step();
        if let Some(outcome) = self.running[idx].run.final_result() {
            if self.running.pop().is_none() {
                return Err(RunFailure::RunErr(
                    self.prog.loc(),
                    "running had no frame to pop".to_string(),
                ));
            }

            if self.running.is_empty() {
                return Ok(HierarchialStepResult::Done(None));
            }

            idx -= 1;

            self.running[idx].env = outcome;

            let step = clvm::step_return_value(
                &self.running[idx].run.current_step(),
                self.running[idx].env.clone(),
            );

            self.running[idx].run = CldbRun::new(
                self.runner.clone(),
                self.prim_map.clone(),
                Box::new(CldbRunEnv::new(
                    self.input_file.clone(),
                    self.program_lines.clone(),
                    Box::new(CldbNoOverride::new()),
                )),
                step,
            );

            self.running[idx].run.set_flags(self.flags);

            Ok(HierarchialStepResult::ShapeChange)
        } else if let Some(info) = relevant_run_step_info(&self.symbol_table, &current_step) {
            // Create a frame based on the last argument.
            self.push_synthetic_stack_frame(current_env, &info);

            Ok(HierarchialStepResult::ShapeChange)
        } else {
            // Not final result, we'll step the top of the stack.
            let info = self.running[idx].run.step(&mut self.allocator);
            if let Some(i) = &info {
                self.error |= i.get("Failure").is_some();
            }
            Ok(HierarchialStepResult::Info(info))
        }
    }
}