Skip to content

Commit

Permalink
Merge up
Browse files Browse the repository at this point in the history
  • Loading branch information
prozacchiwawa committed Jul 18, 2024
2 parents 39a6289 + 6ed6ee8 commit 0f9451e
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ jobs:
# Check that recompiling deployed puzzles match with their deployed hashes
cp support/install_deps.sh support/verify_compiler_version.sh chia-blockchain
(cd chia-blockchain && python -m venv venv && . venv/bin/activate && pip install --upgrade pip && \
(cd chia-blockchain && python -m venv .venv && . .venv/bin/activate && pip install --upgrade pip && \
python -m pip install maturin==1.1.0 && \
cd .. && pip install --no-index --find-links target/wheels/ clvm_tools_rs && \
cd chia-blockchain && \
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/dependency-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ jobs:
- name: "Dependency Review"
uses: actions/dependency-review-action@v4
with:
allow-dependencies-licenses: pkg:pypi/pylint, pkg:pypi/pyinstaller
deny-licenses: AGPL-1.0-only, AGPL-1.0-or-later, AGPL-1.0-or-later, AGPL-3.0-or-later, GPL-1.0-only, GPL-1.0-or-later, GPL-2.0-only, GPL-2.0-or-later, GPL-3.0-only, GPL-3.0-or-later
1 change: 1 addition & 0 deletions resources/tests/bin-quote.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'test
12 changes: 11 additions & 1 deletion src/classic/clvm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct KwAtomPair {
version: usize,
}

const KW_PAIRS: [KwAtomPair; 46] = [
const KW_PAIRS: [KwAtomPair; 48] = [
KwAtomPair {
v: &[0x01],
n: "q",
Expand Down Expand Up @@ -241,6 +241,16 @@ const KW_PAIRS: [KwAtomPair; 46] = [
n: "bls_verify",
version: 1,
},
KwAtomPair {
v: &[0x3c],
n: "modpow",
version: 1,
},
KwAtomPair {
v: &[0x3d],
n: "%",
version: 1,
},
KwAtomPair {
v: &[0x13, 0xd6, 0x1f, 0x00],
n: "secp256k1_verify",
Expand Down
54 changes: 29 additions & 25 deletions src/classic/clvm_tools/cmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,23 +381,26 @@ fn yamlette_string(to_print: &[BTreeMap<String, YamlElement>]) -> String {
}
}

pub fn cldb_hierarchy(
runner: Rc<dyn TRunProgram>,
prim_map: Rc<HashMap<Vec<u8>, Rc<sexp::SExp>>>,
input_file_name: Option<String>,
lines: Rc<Vec<String>>,
symbol_table: Rc<HashMap<String, String>>,
prog: Rc<sexp::SExp>,
args: Rc<sexp::SExp>,
) -> Vec<BTreeMap<String, YamlElement>> {
pub struct CldbHierarchyArgs {
pub runner: Rc<dyn TRunProgram>,
pub prim_map: Rc<HashMap<Vec<u8>, Rc<sexp::SExp>>>,
pub input_file_name: Option<String>,
pub lines: Rc<Vec<String>>,
pub symbol_table: Rc<HashMap<String, String>>,
pub prog: Rc<sexp::SExp>,
pub args: Rc<sexp::SExp>,
pub flags: u32,
}

pub fn cldb_hierarchy(args: CldbHierarchyArgs) -> Vec<BTreeMap<String, YamlElement>> {
let mut runner = HierarchialRunner::new(
runner,
prim_map,
input_file_name,
lines,
symbol_table,
prog,
args,
args.runner,
args.prim_map,
args.input_file_name,
args.lines,
args.symbol_table,
args.prog,
args.args,
);

let mut output_stack = vec![Vec::new()];
Expand Down Expand Up @@ -432,7 +435,7 @@ pub fn cldb_hierarchy(
);
let mut arg_values = BTreeMap::new();
for (k, v) in runner.running[run_idx].named_args.iter() {
arg_values.insert(k.clone(), YamlElement::String(format!("{v}")));
arg_values.insert(k.clone(), YamlElement::String(format!("{}", v.clone())));
}
function_entry.insert(
"Function-Args".to_string(),
Expand Down Expand Up @@ -688,15 +691,16 @@ pub fn cldb(args: &[String]) {
);

if parsed_args.contains_key("tree") {
let result = cldb_hierarchy(
let result = cldb_hierarchy(CldbHierarchyArgs {
runner,
Rc::new(prim_map),
parsed.program.path.clone(),
program_lines,
Rc::new(use_symbol_table),
program,
env,
);
prim_map: Rc::new(prim_map),
input_file_name: parsed.program.path.clone(),
lines: program_lines,
symbol_table: Rc::new(use_symbol_table),
prog: program,
args: env,
flags: 0,
});

// Print the tree
let string_result = yamlette_string(&result);
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/preprocessor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ impl Preprocessor {
desc: IncludeDesc,
) -> Result<(), CompileErr> {
let name_string = decode_string(&desc.name);
if KNOWN_DIALECTS.contains_key(&name_string) {
// Terminate early checking anything with a processed include type.
if KNOWN_DIALECTS.contains_key(&name_string) || desc.kind.is_some() {
return Ok(());
}

Expand Down
8 changes: 8 additions & 0 deletions src/compiler/prims.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,14 @@ pub fn prims() -> Vec<(Vec<u8>, SExp)> {
"bls_verify".as_bytes().to_vec(),
SExp::Integer(primloc.clone(), 59_u32.to_bigint().unwrap()),
),
(
"modpow".as_bytes().to_vec(),
SExp::Integer(primloc.clone(), 60_u32.to_bigint().unwrap()),
),
(
"%".as_bytes().to_vec(),
SExp::Integer(primloc.clone(), 61_u32.to_bigint().unwrap()),
),
(
"secp256k1_verify".as_bytes().to_vec(),
SExp::Integer(primloc.clone(), 0x13d61f00.to_bigint().unwrap()),
Expand Down
14 changes: 12 additions & 2 deletions src/compiler/sexp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,18 @@ fn normalize_int(v: Vec<u8>, base: u32) -> Number {
// left to retain their sign and hex constants are considered unsigned so _not_
// padded.
fn from_hex(l: Srcloc, v: &[u8]) -> SExp {
let mut result = vec![0; (v.len() - 2) / 2];
hex2bin(&v[2..], &mut result).expect("should convert from hex");
let mut result = vec![0; (v.len() - 1) / 2];
let mut hex_const;
// This assigns a new reference so the vec is copied only when we need
// to pad it.
let v_ref = if v.len() % 2 == 1 {
hex_const = v.to_vec();
hex_const.insert(2, b'0');
&hex_const[2..]
} else {
&v[2..]
};
hex2bin(v_ref, &mut result).ok();
SExp::QuotedString(l, b'x', result)
}

Expand Down
57 changes: 57 additions & 0 deletions src/tests/classic/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2429,6 +2429,50 @@ fn test_assign_cse_tricky_2() {
assert_eq!(program, wanted_repr);
}

#[test]
fn test_classic_modpow() {
let result = do_basic_brun(&vec![
"brun".to_string(),
"(modpow (q . 2) (q . 6) (q . 5))".to_string(),
]);
// 64 % 5 == 4
assert_eq!(result.trim(), "4");
}

#[test]
fn test_classic_mod_op() {
let result = do_basic_brun(&vec![
"brun".to_string(),
"(% (q . 13) (q . 10))".to_string(),
]);
// 13 % 10 == 3
assert_eq!(result.trim(), "3");
}

#[test]
fn test_modern_modpow() {
let program = do_basic_run(&vec![
"run".to_string(),
"(mod (X Y Z) (include *standard-cl-23*) (modpow X Y Z))".to_string(),
]);
assert_eq!(program.trim(), "(60 2 5 11)");
let result = do_basic_brun(&vec!["brun".to_string(), program, "(2 7 10)".to_string()]);
// 128 % 10 == 8
assert_eq!(result.trim(), "8");
}

#[test]
fn test_modern_mod_op() {
let program = do_basic_run(&vec![
"run".to_string(),
"(mod (X Y) (include *standard-cl-23*) (% X Y))".to_string(),
]);
assert_eq!(program.trim(), "(61 2 5)");
let result = do_basic_brun(&vec!["brun".to_string(), program, "(137 6)".to_string()]);
// 137 % 6 == 5
assert_eq!(result.trim(), "5");
}

#[test]
fn test_include_zero_bin() {
let program = do_basic_run(&vec![
Expand All @@ -2451,3 +2495,16 @@ fn test_include_zero_bin_pre_fix() {
]);
assert_eq!(program, "(2 (1 14 (1 . 1) 2) (4 (1 . 1) 1))");
}

#[test]
fn test_include_bin_should_not_be_parsed() {
let program = do_basic_run(&vec![
"run".to_string(),
"-i".to_string(),
"resources/tests".to_string(),
"(mod (X) (include *standard-cl-23.1*) (embed-file test bin bin-quote.bin) test)"
.to_string(),
]);
let result = do_basic_brun(&vec!["brun".to_string(), program]);
assert_eq!(result.trim(), "\"'test\"");
}
43 changes: 26 additions & 17 deletions src/tests/compiler/cldb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::rc::Rc;

use clvmr::allocator::Allocator;

use crate::classic::clvm_tools::cmds::{cldb_hierarchy, YamlElement};
use crate::classic::clvm_tools::cmds::{cldb_hierarchy, CldbHierarchyArgs, YamlElement};
use crate::classic::clvm_tools::stages::stage_0::DefaultProgramRunner;
use crate::compiler::cldb::{hex_to_modern_sexp, CldbNoOverride, CldbRun, CldbRunEnv};
use crate::compiler::clvm::{start_step, RunStep};
Expand Down Expand Up @@ -49,6 +49,7 @@ fn run_clvm_in_cldb<V>(
symbols: HashMap<String, String>,
args: Rc<SExp>,
viewer: &mut V,
flags: u32,
) -> Option<String>
where
V: StepOfCldbViewer,
Expand All @@ -67,6 +68,7 @@ where
Box::new(CldbNoOverride::new_symbols(symbols)),
);
let mut cldbrun = CldbRun::new(runner, Rc::new(prim_map), Box::new(cldbenv), step);

let mut output: BTreeMap<String, String> = BTreeMap::new();

loop {
Expand Down Expand Up @@ -116,6 +118,7 @@ fn test_run_clvm_in_cldb() {
symbols,
args,
&mut DoesntWatchCldb {},
0,
),
Some("120".to_string())
);
Expand Down Expand Up @@ -169,6 +172,7 @@ fn compile_and_run_program_with_tree(
input_program_text: &str,
args_text: &str,
search_paths: &[String],
flags: u32,
) -> Vec<BTreeMap<String, YamlElement>> {
let mut allocator = Allocator::new();
let runner = Rc::new(DefaultProgramRunner::new());
Expand Down Expand Up @@ -197,15 +201,16 @@ fn compile_and_run_program_with_tree(
let program_lines: Rc<Vec<String>> =
Rc::new(input_program_text.lines().map(|x| x.to_string()).collect());

cldb_hierarchy(
cldb_hierarchy(CldbHierarchyArgs {
runner,
Rc::new(prim_map),
Some(input_file.to_owned()),
program_lines,
Rc::new(use_symbol_table),
Rc::new(program),
prim_map: Rc::new(prim_map),
input_file_name: Some(input_file.to_owned()),
lines: program_lines,
symbol_table: Rc::new(use_symbol_table),
prog: Rc::new(program),
args,
)
flags,
})
}

fn run_program_as_tree_from_hex(
Expand Down Expand Up @@ -240,15 +245,16 @@ fn run_program_as_tree_from_hex(
}
let program_lines = Rc::new(vec![]);
let runner = Rc::new(DefaultProgramRunner::new());
cldb_hierarchy(
cldb_hierarchy(CldbHierarchyArgs {
runner,
Rc::new(prim_map),
Some(file_name.to_owned()),
program_lines,
Rc::new(symbol_table),
program,
prim_map: Rc::new(prim_map),
input_file_name: Some(file_name.to_owned()),
lines: program_lines,
symbol_table: Rc::new(symbol_table),
prog: program,
args,
)
flags: 0,
})
}

fn compare_run_output(
Expand All @@ -275,7 +281,8 @@ fn test_cldb_hierarchy_mode() {
.expect("test resources should exist: test_rec_1.cl");
let input_file = "./test_rec_1.cl";

let result = compile_and_run_program_with_tree(&input_file, &input_program, "(3 2)", &vec![]);
let result =
compile_and_run_program_with_tree(&input_file, &input_program, "(3 2)", &vec![], 0);

compare_run_output(result, run_entries);
}
Expand Down Expand Up @@ -362,7 +369,8 @@ fn test_cldb_explicit_throw() {
program,
HashMap::new(),
args,
&mut watcher
&mut watcher,
0
),
None
);
Expand All @@ -387,6 +395,7 @@ fn test_clvm_operator_with_weird_tail() {
HashMap::new(),
args,
&mut DoesntWatchCldb {},
0,
),
Some("8".to_string())
);
Expand Down
10 changes: 10 additions & 0 deletions src/tests/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2448,3 +2448,13 @@ fn test_almost_empty_lambda_gives_error() {
assert!(res.is_err());
assert!(format!("{res:?}").contains("Must provide at least arguments and body to lambda"));
}

#[test]
fn test_odd_hex_works() {
let res = run_string(
&"(mod () (include *standard-cl-23*) (+ 1 0xf))".to_string(),
&"()".to_string(),
)
.expect("should work");
assert_eq!(res.to_string(), "16");
}

0 comments on commit 0f9451e

Please sign in to comment.