Skip to content

Commit

Permalink
Merge pull request #139 from xch-dev/mod
Browse files Browse the repository at this point in the history
Add Mod trait for currying
  • Loading branch information
Rigidity authored Dec 4, 2024
2 parents a14a7cc + 8b4bd21 commit 509953a
Show file tree
Hide file tree
Showing 36 changed files with 711 additions and 679 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ must_use_candidate = "allow"
workspace = true

[features]
chip-0035 = ["chia-sdk-driver/chip-0035"]
chip-0035 = ["chia-sdk-driver/chip-0035", "chia-sdk-types/chip-0035"]
offers = ["chia-sdk-driver/offers"]
native-tls = ["chia-sdk-client/native-tls"]
rustls = ["chia-sdk-client/rustls"]
Expand Down
12 changes: 9 additions & 3 deletions crates/chia-sdk-driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ all-features = true
workspace = true

[features]
chip-0035 = []
offers = ["dep:bech32", "dep:chia-traits", "dep:flate2", "dep:indexmap", "dep:once_cell"]
chip-0035 = ["chia-sdk-types/chip-0035"]
offers = [
"dep:bech32",
"dep:chia-traits",
"dep:flate2",
"dep:indexmap",
"dep:once_cell",
]

[dependencies]
chia-bls = { workspace = true }
Expand All @@ -31,7 +37,7 @@ clvmr = { workspace = true }
thiserror = { workspace = true }
chia-sdk-types = { workspace = true }
hex-literal = { workspace = true }
num-bigint = { workspace = true}
num-bigint = { workspace = true }
hex = { workspace = true }
bigdecimal = { workspace = true }
bech32 = { workspace = true, optional = true }
Expand Down
13 changes: 0 additions & 13 deletions crates/chia-sdk-driver/src/layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,3 @@ mod datalayer;

#[cfg(feature = "chip-0035")]
pub use datalayer::*;

#[cfg(test)]
mod tests {
#[macro_export]
macro_rules! assert_puzzle_hash {
($puzzle:ident => $puzzle_hash:ident) => {
let mut a = clvmr::Allocator::new();
let ptr = clvmr::serde::node_from_bytes(&mut a, &$puzzle)?;
let hash = clvm_utils::tree_hash(&mut a, ptr);
assert_eq!($puzzle_hash, hash);
};
}
}
9 changes: 3 additions & 6 deletions crates/chia-sdk-driver/src/layers/cat_layer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use chia_protocol::Bytes32;
use chia_puzzles::cat::{CatArgs, CatSolution, CAT_PUZZLE_HASH};
use clvm_traits::FromClvm;
use clvm_utils::{CurriedProgram, ToTreeHash, TreeHash};
use clvm_utils::{ToTreeHash, TreeHash};
use clvmr::{Allocator, NodePtr};

use crate::{DriverError, Layer, Puzzle, SpendContext};
Expand Down Expand Up @@ -76,11 +76,8 @@ where
}

fn construct_puzzle(&self, ctx: &mut SpendContext) -> Result<NodePtr, DriverError> {
let curried = CurriedProgram {
program: ctx.cat_puzzle()?,
args: CatArgs::new(self.asset_id, self.inner_puzzle.construct_puzzle(ctx)?),
};
ctx.alloc(&curried)
let inner_puzzle = self.inner_puzzle.construct_puzzle(ctx)?;
ctx.curry(CatArgs::new(self.asset_id, inner_puzzle))
}

fn construct_solution(
Expand Down
29 changes: 3 additions & 26 deletions crates/chia-sdk-driver/src/layers/datalayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,20 @@ mod delegation_layer;
mod oracle_layer;
mod writer_layer;

use clvm_utils::TreeHash;
use hex_literal::hex;

pub use delegation_layer::*;
pub use oracle_layer::*;
pub use writer_layer::*;

pub const DL_METADATA_UPDATER_PUZZLE: [u8; 1] = hex!(
"
0b
"
);

pub const DL_METADATA_UPDATER_PUZZLE_HASH: TreeHash = TreeHash::new(hex!(
"
57bfd1cb0adda3d94315053fda723f2028320faa8338225d99f629e3d46d43a9
"
));

#[cfg(test)]
mod tests {
use chia_sdk_types::DL_METADATA_UPDATER_PUZZLE;
use clvm_traits::{clvm_list, ToClvm};
use clvm_utils::tree_hash;
use clvmr::serde::node_from_bytes;
use hex_literal::hex;
use rstest::rstest;

use crate::{assert_puzzle_hash, SpendContext};

use super::*;

#[test]
fn test_puzzle_hashes() -> anyhow::Result<()> {
assert_puzzle_hash!(DELEGATION_LAYER_PUZZLE => DELEGATION_LAYER_PUZZLE_HASH);
assert_puzzle_hash!(WRITER_FILTER_PUZZLE => WRITER_FILTER_PUZZLE_HASH);
assert_puzzle_hash!(DL_METADATA_UPDATER_PUZZLE => DL_METADATA_UPDATER_PUZZLE_HASH);
Ok(())
}
use crate::SpendContext;

// tests that DL metadata updater indeed returns the third argument
#[rstest]
Expand Down
108 changes: 7 additions & 101 deletions crates/chia-sdk-driver/src/layers/datalayer/delegation_layer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use chia_protocol::Bytes32;
use clvm_traits::{FromClvm, ToClvm};
use clvm_utils::{CurriedProgram, ToTreeHash, TreeHash};
use chia_sdk_types::{DelegationLayerArgs, DelegationLayerSolution, DELEGATION_LAYER_PUZZLE_HASH};
use clvm_traits::FromClvm;
use clvmr::{Allocator, NodePtr};
use hex_literal::hex;

use crate::{DriverError, Layer, Puzzle, SpendContext};

Expand Down Expand Up @@ -60,14 +59,11 @@ impl Layer for DelegationLayer {
}

fn construct_puzzle(&self, ctx: &mut SpendContext) -> Result<NodePtr, DriverError> {
let curried = CurriedProgram {
program: ctx.delegation_layer_puzzle()?,
args: DelegationLayerArgs::new(
self.launcher_id,
self.owner_puzzle_hash,
self.merkle_root,
),
};
let curried = ctx.curry(DelegationLayerArgs::new(
self.launcher_id,
self.owner_puzzle_hash,
self.merkle_root,
))?;
ctx.alloc(&curried)
}

Expand All @@ -79,93 +75,3 @@ impl Layer for DelegationLayer {
ctx.alloc(&solution)
}
}

pub const DELEGATION_LAYER_PUZZLE: [u8; 1027] = hex!(
"
ff02ffff01ff02ff12ffff04ff02ffff04ff05ffff04ff0bffff04ff17ffff04ff2fffff04ff5fff
ff04ffff02ff81bfff82017f80ffff04ffff02ff16ffff04ff02ffff04ff81bfff80808080ff8080
8080808080808080ffff04ffff01ffffff3381f3ff02ffffa04bf5122f344554c53bde2ebb8cd2b7
e3d1600ad631c385a5d7cce23c7785459aa09dcf97a184f32623d11a73124ceb99a5709b083721e8
78a16d78f596718ba7b2ffa102a12871fee210fb8619291eaea194581cbd2531e4b23759d225f680
6923f63222a102a8d5dd63fba471ebcb1f3e8f7c1e1879b7152a6e7298a91ce119a63400ade7c5ff
ffff02ffff03ffff09ff82017fff1780ffff0181bfffff01ff02ffff03ffff09ff2fffff02ff1eff
ff04ff02ffff04ffff0bffff0101ff82017f80ffff04ff5fff808080808080ffff01ff02ff1affff
04ff02ffff04ff05ffff04ff0bffff04ff17ffff04ff81bfffff04ffff04ff2fffff04ff0bff8080
80ff8080808080808080ffff01ff088080ff018080ff0180ff02ffff03ff2fffff01ff02ffff03ff
ff09ff818fff1880ffff01ff02ff1affff04ff02ffff04ff05ffff04ff0bffff04ff17ffff04ff6f
ffff04ff81cfff8080808080808080ffff01ff04ffff02ffff03ffff02ffff03ffff09ff818fffff
0181e880ffff01ff22ffff09ff820acfff8080ffff09ff8214cfffff01a057bfd1cb0adda3d94315
053fda723f2028320faa8338225d99f629e3d46d43a98080ffff01ff010180ff0180ffff014fffff
01ff088080ff0180ffff02ff1affff04ff02ffff04ff05ffff04ff0bffff04ff17ffff04ff6fffff
04ff5fff80808080808080808080ff0180ffff01ff04ffff04ff10ffff04ffff0bff5cffff0bff14
ffff0bff14ff6cff0580ffff0bff14ffff0bff7cffff0bff14ffff0bff14ff6cffff0bffff0101ff
058080ffff0bff14ffff0bff7cffff0bff14ffff0bff14ff6cffff0bffff0101ff0b8080ffff0bff
14ffff0bff7cffff0bff14ffff0bff14ff6cffff0bffff0101ff178080ffff0bff14ffff0bff7cff
ff0bff14ffff0bff14ff6cffff0bffff0101ff819f8080ffff0bff14ff6cff4c808080ff4c808080
ff4c808080ff4c808080ff4c808080ffff04ffff0101ffff04ff81dfff8080808080ff808080ff01
80ffff02ffff03ffff07ff0580ffff01ff0bffff0102ffff02ff16ffff04ff02ffff04ff09ff8080
8080ffff02ff16ffff04ff02ffff04ff0dff8080808080ffff01ff0bffff0101ff058080ff0180ff
02ffff03ff1bffff01ff02ff1effff04ff02ffff04ffff02ffff03ffff18ffff0101ff1380ffff01
ff0bffff0102ff2bff0580ffff01ff0bffff0102ff05ff2b8080ff0180ffff04ffff04ffff17ff13
ffff0181ff80ff3b80ff8080808080ffff010580ff0180ff018080
"
);

pub const DELEGATION_LAYER_PUZZLE_HASH: TreeHash = TreeHash::new(hex!(
"
c3b249466cb15c51e5abb5c54ef5077c1624ae2e6a0f8f7a3fa197a943a5d62e
"
));

#[derive(ToClvm, FromClvm, Debug, Clone, Copy, PartialEq, Eq)]
#[clvm(curry)]
pub struct DelegationLayerArgs {
pub mod_hash: Bytes32,
pub launcher_id: Bytes32,
pub owner_puzzle_hash: Bytes32,
pub merkle_root: Bytes32,
}

impl DelegationLayerArgs {
pub fn new(launcher_id: Bytes32, owner_puzzle_hash: Bytes32, merkle_root: Bytes32) -> Self {
Self {
mod_hash: DELEGATION_LAYER_PUZZLE_HASH.into(),
launcher_id,
owner_puzzle_hash,
merkle_root,
}
}
}

impl DelegationLayerArgs {
pub fn curry_tree_hash(
launcher_id: Bytes32,
owner_puzzle_hash: Bytes32,
merkle_root: Bytes32,
) -> TreeHash {
CurriedProgram {
program: DELEGATION_LAYER_PUZZLE_HASH,
args: DelegationLayerArgs {
mod_hash: DELEGATION_LAYER_PUZZLE_HASH.into(),
launcher_id,
owner_puzzle_hash,
merkle_root,
},
}
.tree_hash()
}
}

#[derive(ToClvm, FromClvm, Debug, Clone, PartialEq, Eq)]
#[clvm(list)]
pub struct DelegationLayerSolution<P, S> {
pub merkle_proof: Option<(u32, Vec<Bytes32>)>,
pub puzzle_reveal: P,
pub puzzle_solution: S,
}

impl SpendContext {
pub fn delegation_layer_puzzle(&mut self) -> Result<NodePtr, DriverError> {
self.puzzle(DELEGATION_LAYER_PUZZLE_HASH, &DELEGATION_LAYER_PUZZLE)
}
}
63 changes: 6 additions & 57 deletions crates/chia-sdk-driver/src/layers/datalayer/writer_layer.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use chia_puzzles::standard::StandardSolution;
use chia_sdk_types::Conditions;
use clvm_traits::{clvm_quote, FromClvm, ToClvm};
use clvm_utils::{CurriedProgram, ToTreeHash, TreeHash};
use chia_sdk_types::{Conditions, WriterLayerArgs, WriterLayerSolution, WRITER_LAYER_PUZZLE_HASH};
use clvm_traits::{clvm_quote, FromClvm};
use clvm_utils::{ToTreeHash, TreeHash};
use clvmr::{Allocator, NodePtr};
use hex_literal::hex;

use crate::{DriverError, Layer, Puzzle, Spend, SpendContext, StandardLayer};

Expand Down Expand Up @@ -32,7 +31,7 @@ where
return Ok(None);
};

if puzzle.mod_hash != WRITER_FILTER_PUZZLE_HASH {
if puzzle.mod_hash != WRITER_LAYER_PUZZLE_HASH {
return Ok(None);
}

Expand All @@ -58,10 +57,8 @@ where
}

fn construct_puzzle(&self, ctx: &mut SpendContext) -> Result<NodePtr, DriverError> {
let curried = CurriedProgram {
program: ctx.delegated_writer_filter()?,
args: WriterLayerArgs::new(self.inner_puzzle.construct_puzzle(ctx)?),
};
let inner_puzzle = self.inner_puzzle.construct_puzzle(ctx)?;
let curried = ctx.curry(WriterLayerArgs::new(inner_puzzle))?;
ctx.alloc(&curried)
}

Expand Down Expand Up @@ -107,51 +104,3 @@ impl WriterLayer<StandardLayer> {
Ok(Spend { puzzle, solution })
}
}

pub const WRITER_FILTER_PUZZLE: [u8; 110] = hex!(
"
ff02ffff01ff02ff02ffff04ff02ffff04ffff02ff05ff0b80ff80808080ffff04ffff01ff02ffff
03ff05ffff01ff02ffff03ffff09ff11ffff0181f380ffff01ff0880ffff01ff04ff09ffff02ff02
ffff04ff02ffff04ff0dff808080808080ff0180ff8080ff0180ff018080
"
);

pub const WRITER_FILTER_PUZZLE_HASH: TreeHash = TreeHash::new(hex!(
"
407f70ea751c25052708219ae148b45db2f61af2287da53d600b2486f12b3ca6
"
));

#[derive(ToClvm, FromClvm, Debug, Clone, Copy, PartialEq, Eq)]
#[clvm(curry)]
pub struct WriterLayerArgs<I> {
pub inner_puzzle: I,
}

impl<I> WriterLayerArgs<I> {
pub fn new(inner_puzzle: I) -> Self {
Self { inner_puzzle }
}
}

impl WriterLayerArgs<TreeHash> {
pub fn curry_tree_hash(inner_puzzle: TreeHash) -> TreeHash {
CurriedProgram {
program: WRITER_FILTER_PUZZLE_HASH,
args: WriterLayerArgs { inner_puzzle },
}
.tree_hash()
}
}

#[derive(ToClvm, FromClvm, Debug, Clone, PartialEq, Eq)]
#[clvm(list)]
pub struct WriterLayerSolution<I> {
pub inner_solution: I,
}

impl SpendContext {
pub fn delegated_writer_filter(&mut self) -> Result<NodePtr, DriverError> {
self.puzzle(WRITER_FILTER_PUZZLE_HASH, &WRITER_FILTER_PUZZLE)
}
}
21 changes: 9 additions & 12 deletions crates/chia-sdk-driver/src/layers/did_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use chia_puzzles::{
singleton::{SingletonStruct, SINGLETON_LAUNCHER_PUZZLE_HASH, SINGLETON_TOP_LAYER_PUZZLE_HASH},
};
use clvm_traits::{FromClvm, ToClvm};
use clvm_utils::{CurriedProgram, ToTreeHash, TreeHash};
use clvm_utils::{ToTreeHash, TreeHash};
use clvmr::{Allocator, NodePtr};

use crate::{DriverError, Layer, Puzzle, SpendContext};
Expand Down Expand Up @@ -106,17 +106,14 @@ where
}

fn construct_puzzle(&self, ctx: &mut SpendContext) -> Result<NodePtr, DriverError> {
let curried = CurriedProgram {
program: ctx.did_inner_puzzle()?,
args: DidArgs::new(
self.inner_puzzle.construct_puzzle(ctx)?,
self.recovery_list_hash,
self.num_verifications_required,
SingletonStruct::new(self.launcher_id),
&self.metadata,
),
};
ctx.alloc(&curried)
let inner_puzzle = self.inner_puzzle.construct_puzzle(ctx)?;
ctx.curry(DidArgs::new(
inner_puzzle,
self.recovery_list_hash,
self.num_verifications_required,
SingletonStruct::new(self.launcher_id),
&self.metadata,
))
}

fn construct_solution(
Expand Down
Loading

0 comments on commit 509953a

Please sign in to comment.