Skip to content

Commit

Permalink
Merge pull request #178 from xch-dev/compiler-bindings
Browse files Browse the repository at this point in the history
Add IR and compiler CLVM bindings
  • Loading branch information
Rigidity authored Feb 25, 2025
2 parents 045d199 + 1ddb880 commit fc186c3
Show file tree
Hide file tree
Showing 8 changed files with 376 additions and 40 deletions.
368 changes: 328 additions & 40 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ sha2 = "0.10.8"
pyo3 = "0.23.3"
js-sys = "0.3.77"
parking_lot = "0.12.3"
clvm_tools_rs = "0.1.46"

[profile.release]
lto = true
Expand Down
6 changes: 6 additions & 0 deletions bindings/clvm.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
"coin_spends": {
"return": "Vec<CoinSpend>"
},
"parse": {
"args": {
"program": "String"
},
"return": "Program"
},
"deserialize": {
"args": {
"value": "SerializedProgram"
Expand Down
6 changes: 6 additions & 0 deletions bindings/program.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
"Program": {
"type": "class",
"methods": {
"compile": {
"return": "Output"
},
"unparse": {
"return": "String"
},
"serialize": {
"return": "SerializedProgram"
},
Expand Down
1 change: 1 addition & 0 deletions crates/chia-sdk-bindings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ rand = { workspace = true }
rand_chacha = { workspace = true }
num-bigint = { workspace = true }
paste = { workspace = true }
clvm_tools_rs = { workspace = true }
7 changes: 7 additions & 0 deletions crates/chia-sdk-bindings/src/clvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use bindy::{Error, Result};
use chia_protocol::{Bytes, Bytes32, Program as SerializedProgram};
use chia_puzzle_types::nft;
use chia_sdk_driver::{HashedPtr, Launcher, SpendContext, StandardLayer};
use clvm_tools_rs::classic::clvm_tools::binutils::assemble;
use clvm_traits::{clvm_quote, ToClvm};
use clvm_utils::TreeHash;
use clvmr::{
Expand Down Expand Up @@ -207,6 +208,12 @@ impl Clvm {
})
}

pub fn parse(&self, program: String) -> Result<Program> {
let mut ctx = self.0.write().unwrap();
let ptr = assemble(&mut ctx.allocator, &program)?;
Ok(Program(self.0.clone(), ptr))
}

pub fn deserialize(&self, value: SerializedProgram) -> Result<Program> {
let mut ctx = self.0.write().unwrap();
let ptr = node_from_bytes(&mut ctx.allocator, &value)?;
Expand Down
24 changes: 24 additions & 0 deletions crates/chia-sdk-bindings/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ use bindy::{Error, Result};
use chia_protocol::{Bytes, Program as SerializedProgram};
use chia_puzzle_types::nft;
use chia_sdk_driver::SpendContext;
use clvm_tools_rs::classic::clvm_tools::stages::run;
use clvm_tools_rs::classic::clvm_tools::stages::stage_0::TRunProgram;
use clvm_tools_rs::classic::clvm_tools::{
binutils::disassemble, stages::stage_2::operators::run_program_for_search_paths,
};
use clvm_traits::{ClvmDecoder, ClvmEncoder, FromClvm};
use clvm_utils::{tree_hash, TreeHash};
use clvmr::{
Expand All @@ -19,6 +24,25 @@ use crate::{CurriedProgram, NftMetadata, Output, Pair, Puzzle};
pub struct Program(pub(crate) Arc<RwLock<SpendContext>>, pub(crate) NodePtr);

impl Program {
pub fn compile(&self) -> Result<Output> {
let mut ctx = self.0.write().unwrap();

let invoke = run(&mut ctx.allocator);
let input = ctx.allocator.new_pair(self.1, NodePtr::NIL)?;
let run_program = run_program_for_search_paths("program.clsp", &[], false);
let output = run_program.run_program(&mut ctx.allocator, invoke, input, None)?;

Ok(Output {
value: Program(self.0.clone(), output.1),
cost: output.0,
})
}

pub fn unparse(&self) -> Result<String> {
let ctx = self.0.read().unwrap();
Ok(disassemble(&ctx.allocator, self.1, None))
}

pub fn serialize(&self) -> Result<SerializedProgram> {
let ctx = self.0.read().unwrap();
Ok(node_to_bytes(&ctx.allocator, self.1)?.into())
Expand Down
3 changes: 3 additions & 0 deletions napi/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,8 @@ export declare class Mnemonic {
toSeed(password: string): Uint8Array
}
export declare class Program {
compile(): Output
unparse(): string
serialize(): Uint8Array
serializeWithBackrefs(): Uint8Array
run(solution: Program, maxCost: bigint, mempoolMode: boolean): Output
Expand Down Expand Up @@ -588,6 +590,7 @@ export declare class Clvm {
addCoinSpend(coinSpend: CoinSpend): void
spendCoin(coin: Coin, spend: Spend): void
coinSpends(): Array<CoinSpend>
parse(program: string): Program
deserialize(value: Uint8Array): Program
deserializeWithBackrefs(value: Uint8Array): Program
pair(first: Program, rest: Program): Program
Expand Down

0 comments on commit fc186c3

Please sign in to comment.