Skip to content

Commit bf55ce4

Browse files
Merge pull request #3 from Chia-Network/20240505-build-rs
introduce build.rs and ci integration
2 parents 6169d95 + ef448aa commit bf55ce4

17 files changed

+277
-227
lines changed

.github/workflows/build-test.yml

+2-6
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,10 @@ jobs:
110110
export RUSTFLAGS="-Cinstrument-coverage"
111111
export LLVM_PROFILE_FILE=$(pwd)/target/clvm_tools_rs-%p-%m.profraw
112112
export CARGO_TARGET_DIR=$(pwd)/target
113-
cargo test --release --workspace
114113
python -m venv venv
115114
source venv/bin/activate
116-
git clone https://github.com/Chia-Network/clvm_tools.git --branch=main --single-branch
117-
pip install ./clvm_tools
118-
pip install maturin pytest
119-
maturin develop --release
120-
(cd resources/tests/cmdline/tests && pytest)
115+
pip install chia-blockchain
116+
cargo test --release --workspace --features=sim-tests
121117
grcov . --binary-path target -s . --branch --ignore-not-existing --ignore='*/.cargo/*' --ignore='*/tests/*' -o rust_cov.info
122118
python -c 'with open("rust_cov.info") as f: lines = [l for l in f if not (l.startswith("DA:") and int(l.split(",")[1].strip()) >= 2**63)]; open("lcov.info", "w").writelines(lines)'
123119
- name: Upload to Coveralls

Cargo.toml

+9-4
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ edition = "2021"
77
sim-tests = []
88

99
[dependencies]
10-
clvm_tools_rs = "0.1.42"
11-
clvmr = { version = "0.3.0", features = ["pre-eval"] }
10+
clvm_tools_rs = { git = "https://github.com/Chia-Network/clvm_tools_rs.git", rev = "ec75759377791c9b785123d2d3e2457d08ac6621" }
11+
clvmr = { version = "=0.3.2", features = ["pre-eval"] }
1212
serde_json = "1.0"
1313
rand = "0.8.5"
1414
rand_chacha = "0.3.1"
1515
clvm-traits = "0.7.0"
1616
subtle = "2.5.0"
1717
num-bigint = "0.4.4"
18-
num-traits = "0.2.18"
18+
num-traits = "0.2.15"
1919
chia-bls = "0.7.0"
2020
clvm-utils = "0.7.0"
2121
lazy_static = "1.4.0"
@@ -32,9 +32,14 @@ tracing-subscriber = "0.3"
3232

3333
[dev-dependencies]
3434
pyo3 = { version = "0.20.0", features = ["auto-initialize"] }
35-
3635
bls12_381 = { version = "=0.8.0", features = ["experimental"] }
3736

37+
[build-dependencies]
38+
serde = "1.0.203"
39+
toml = "0.8.14"
40+
clvm_tools_rs = { git = "https://github.com/Chia-Network/clvm_tools_rs.git", rev = "ec75759377791c9b785123d2d3e2457d08ac6621" }
41+
clvmr = { version = "=0.3.2", features = ["pre-eval"] }
42+
3843
[lib]
3944
name = "chia_gaming"
4045
crate-type = ["cdylib", "rlib"]

build.rs

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use std::collections::HashMap;
2+
use std::fs;
3+
4+
use clvmr::Allocator;
5+
use toml::{Table, Value};
6+
7+
use clvm_tools_rs::classic::clvm_tools::clvmc::CompileError;
8+
use clvm_tools_rs::classic::clvm_tools::comp_input::RunAndCompileInputData;
9+
use clvm_tools_rs::classic::platform::argparse::ArgumentValue;
10+
use clvm_tools_rs::compiler::comptypes::CompileErr;
11+
use clvm_tools_rs::compiler::srcloc::Srcloc;
12+
13+
fn do_compile(title: &str, filename: &str) -> Result<(), CompileError> {
14+
let mut allocator = Allocator::new();
15+
let mut arguments: HashMap<String, ArgumentValue> = HashMap::new();
16+
arguments.insert(
17+
"include".to_string(),
18+
ArgumentValue::ArgArray(vec![ArgumentValue::ArgString(None, ".".to_string())]),
19+
);
20+
21+
let file_content = fs::read_to_string(filename).map_err(|e| {
22+
CompileErr(
23+
Srcloc::start(filename),
24+
format!("failed to read {filename}: {e:?}"),
25+
)
26+
})?;
27+
28+
arguments.insert(
29+
"path_or_code".to_string(),
30+
ArgumentValue::ArgString(Some(filename.to_string()), file_content),
31+
);
32+
33+
let parsed = RunAndCompileInputData::new(&mut allocator, &arguments).map_err(|e| {
34+
CompileError::Modern(
35+
Srcloc::start("*error*"),
36+
format!("error building chialisp {title}: {e}"),
37+
)
38+
})?;
39+
let mut symbol_table = HashMap::new();
40+
41+
parsed.compile_modern(&mut allocator, &mut symbol_table)?;
42+
43+
Ok(())
44+
}
45+
46+
fn compile_chialisp() -> Result<(), CompileError> {
47+
let srcloc = Srcloc::start("chialisp.toml");
48+
let chialisp_toml_text = fs::read_to_string("chialisp.toml").map_err(|e| {
49+
CompileError::Modern(
50+
srcloc.clone(),
51+
format!("Error reading chialisp.toml: {e:?}"),
52+
)
53+
})?;
54+
55+
let chialisp_toml = chialisp_toml_text
56+
.parse::<Table>()
57+
.map_err(|e| CompileError::Modern(srcloc, format!("Error parsing chialisp.toml: {e:?}")))?;
58+
59+
if let Some(Value::Table(t)) = chialisp_toml.get("compile") {
60+
for (k, v) in t.iter() {
61+
if let Value::String(s) = v {
62+
do_compile(k, s)?;
63+
}
64+
}
65+
}
66+
67+
Ok(())
68+
}
69+
70+
// Compile chialisp programs in this tree.
71+
fn main() {
72+
if let Err(e) = compile_chialisp() {
73+
panic!("error compiling chialisp: {e:?}");
74+
}
75+
}

chialisp.toml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[compile]
2+
unroll-puzzle = "resources/unroll_puzzle.clsp"
3+
unroll-meta-puzzle = "resources/unroll_meta_puzzle.clsp"

src/channel_handler/game.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use clvm_traits::{ClvmEncoder, ToClvm};
2-
use clvmr::allocator::NodePtr;
3-
use clvmr::run_program;
2+
use clvmr::{run_program, NodePtr};
43

54
use clvm_tools_rs::classic::clvm::sexp::proper_list;
65

@@ -66,11 +65,11 @@ impl Game {
6665
.expect("should be an atom");
6766
let required_size_factor = Amount::new(
6867
atom_from_clvm(allocator, template_list[3])
69-
.and_then(|a| u64_from_atom(a))
68+
.and_then(u64_from_atom)
7069
.expect("should be an atom"),
7170
);
7271
let initial_max_move_size = atom_from_clvm(allocator, template_list[4])
73-
.and_then(|a| usize_from_atom(a))
72+
.and_then(usize_from_atom)
7473
.expect("should be an atom");
7574
let initial_validation_program = ValidationProgram::new(allocator, template_list[5]);
7675
let initial_validation_program_hash =
@@ -83,7 +82,7 @@ impl Game {
8382
};
8483
let initial_state = template_list[7];
8584
let initial_mover_share_proportion = atom_from_clvm(allocator, template_list[8])
86-
.and_then(|a| usize_from_atom(a))
85+
.and_then(usize_from_atom)
8786
.expect("should be an atom");
8887
Ok(Game {
8988
id: game_id,

src/channel_handler/game_handler.rs

+18-21
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ fn run_code(
155155
pub enum TheirTurnResult {
156156
FinalMove(NodePtr),
157157
MakeMove(NodePtr, GameHandler, Vec<u8>),
158-
Slash(Evidence, Aggsig),
158+
Slash(Evidence, Box<Aggsig>),
159159
}
160160

161161
impl GameHandler {
@@ -206,10 +206,7 @@ impl GameHandler {
206206
inputs.amount.clone(),
207207
(
208208
inputs.last_mover_share.clone(),
209-
(
210-
inputs.last_max_move_size.clone(),
211-
(inputs.entropy.clone(), ()),
212-
),
209+
(inputs.last_max_move_size, (inputs.entropy.clone(), ())),
213210
),
214211
),
215212
)
@@ -249,27 +246,27 @@ impl GameHandler {
249246
}
250247

251248
let max_move_size =
252-
if let Some(mm) = atom_from_clvm(allocator, pl[4]).and_then(|a| usize_from_atom(a)) {
249+
if let Some(mm) = atom_from_clvm(allocator, pl[4]).and_then(usize_from_atom) {
253250
mm
254251
} else {
255252
return Err(Error::StrErr("bad max move size".to_string()));
256253
};
257-
let mover_share =
258-
if let Some(ms) = atom_from_clvm(allocator, pl[5]).and_then(|a| u64_from_atom(a)) {
259-
Amount::new(ms)
260-
} else {
261-
return Err(Error::StrErr(format!(
262-
"bad share {}",
263-
disassemble(allocator.allocator(), pl[5], None)
264-
)));
265-
};
254+
let mover_share = if let Some(ms) = atom_from_clvm(allocator, pl[5]).and_then(u64_from_atom)
255+
{
256+
Amount::new(ms)
257+
} else {
258+
return Err(Error::StrErr(format!(
259+
"bad share {}",
260+
disassemble(allocator.allocator(), pl[5], None)
261+
)));
262+
};
266263
let message_parser = if pl[7] == allocator.allocator().null() {
267264
None
268265
} else {
269266
Some(MessageHandler::from_nodeptr(pl[7]))
270267
};
271268
let validation_program_hash =
272-
if let Some(h) = atom_from_clvm(allocator, pl[2]).map(|a| Hash::from_slice(a)) {
269+
if let Some(h) = atom_from_clvm(allocator, pl[2]).map(Hash::from_slice) {
273270
h
274271
} else {
275272
return Err(Error::StrErr("bad hash".to_string()));
@@ -312,7 +309,7 @@ impl GameHandler {
312309
let driver_args = (
313310
inputs.amount.clone(),
314311
(
315-
Node(inputs.last_state.clone()),
312+
Node(inputs.last_state),
316313
(
317314
Node(
318315
allocator
@@ -322,7 +319,7 @@ impl GameHandler {
322319
(
323320
inputs.new_move.validation_info_hash.clone(),
324321
(
325-
inputs.new_move.basic.max_move_size.clone(),
322+
inputs.new_move.basic.max_move_size,
326323
(inputs.new_move.basic.mover_share.clone(), ()),
327324
),
328325
),
@@ -362,10 +359,10 @@ impl GameHandler {
362359

363360
if move_type == 0 {
364361
if pl.len() < 2 {
365-
return Err(Error::StrErr(format!(
362+
Err(Error::StrErr(format!(
366363
"bad length for move result {}",
367364
disassemble(allocator.allocator(), run_result, None)
368-
)));
365+
)))
369366
} else if pl.len() < 3 {
370367
Ok(TheirTurnResult::FinalMove(pl[1]))
371368
} else {
@@ -390,7 +387,7 @@ impl GameHandler {
390387
let sig_bytes = allocator.allocator().atom(pl[2]).to_vec();
391388
Ok(TheirTurnResult::Slash(
392389
Evidence::from_nodeptr(pl[1]),
393-
Aggsig::from_slice(&sig_bytes)?,
390+
Box::new(Aggsig::from_slice(&sig_bytes)?),
394391
))
395392
} else {
396393
Err(Error::StrErr("unknown move result type".to_string()))

0 commit comments

Comments
 (0)