Skip to content

Commit

Permalink
Merge pull request #22 from Rigidity/improve-arch
Browse files Browse the repository at this point in the history
Improve arch
  • Loading branch information
Rigidity authored May 17, 2024
2 parents 497c022 + ad2eca1 commit 5b4b2bf
Show file tree
Hide file tree
Showing 19 changed files with 371 additions and 393 deletions.
6 changes: 1 addition & 5 deletions src/parser/puzzles/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,7 @@ mod tests {
asset_id: issuance_info.asset_id,
p2_puzzle_hash: puzzle_hash,
coin: Coin::new(issuance_info.eve_coin.coin_id(), cat_puzzle_hash.into(), 1),
lineage_proof: LineageProof {
parent_parent_coin_id: issuance_info.eve_coin.parent_coin_info,
parent_inner_puzzle_hash: issuance_info.eve_inner_puzzle_hash,
parent_amount: 1,
},
lineage_proof: issuance_info.lineage_proof,
};

StandardSpend::new()
Expand Down
40 changes: 25 additions & 15 deletions src/parser/puzzles/did.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use chia_protocol::Bytes32;
use chia_puzzles::{
did::{DidArgs, DidSolution, DID_INNER_PUZZLE_HASH},
singleton::{SingletonArgs, SingletonStruct, SINGLETON_TOP_LAYER_PUZZLE_HASH},
LineageProof, Proof,
};
use clvm_traits::FromClvm;
use clvm_utils::tree_hash;
use clvm_utils::{tree_hash, CurriedProgram, ToTreeHash, TreeHash};
use clvmr::{reduction::Reduction, run_program, Allocator, ChiaDialect, NodePtr};

use crate::{
did_inner_puzzle_hash, singleton_puzzle_hash, CreateCoinWithMemos, DidInfo, ParseContext,
ParseError, ParseSingleton,
};
use crate::{CreateCoinWithMemos, DidInfo, ParseContext, ParseError, ParseSingleton};

pub fn parse_did(
allocator: &mut Allocator,
Expand Down Expand Up @@ -62,16 +60,28 @@ pub fn parse_did(
return Err(ParseError::MissingCreateCoin);
};

let did_inner_puzzle_hash = did_inner_puzzle_hash(
p2_puzzle_hash,
args.recovery_did_list_hash,
args.num_verifications_required,
args.singleton_struct.launcher_id,
tree_hash(allocator, args.metadata).into(),
);

let singleton_puzzle_hash =
singleton_puzzle_hash(args.singleton_struct.launcher_id, did_inner_puzzle_hash);
let did_inner_puzzle_hash = CurriedProgram {
program: DID_INNER_PUZZLE_HASH,
args: DidArgs {
inner_puzzle: TreeHash::from(p2_puzzle_hash),
recovery_did_list_hash: args.recovery_did_list_hash,
num_verifications_required: args.num_verifications_required,
metadata: tree_hash(allocator, args.metadata),
singleton_struct: SingletonStruct::new(args.singleton_struct.launcher_id),
},
}
.tree_hash()
.into();

let singleton_puzzle_hash: Bytes32 = CurriedProgram {
program: SINGLETON_TOP_LAYER_PUZZLE_HASH,
args: SingletonArgs {
singleton_struct: args.singleton_struct,
inner_puzzle: TreeHash::from(did_inner_puzzle_hash),
},
}
.tree_hash()
.into();

if singleton_puzzle_hash != ctx.coin().puzzle_hash {
return Err(ParseError::UnknownDidOutput);
Expand Down
2 changes: 1 addition & 1 deletion src/spends/puzzles/cat/cat_spend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl CatSpend {
}

pub fn finish(self, ctx: &mut SpendContext) -> Result<(), SpendError> {
let cat_puzzle_ptr = ctx.cat_puzzle();
let cat_puzzle_ptr = ctx.cat_puzzle()?;
let len = self.cat_spends.len();

let mut total_delta = 0;
Expand Down
31 changes: 18 additions & 13 deletions src/spends/puzzles/cat/issue_cat.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use chia_bls::PublicKey;
use chia_protocol::{Bytes32, Coin, CoinSpend};
use chia_puzzles::cat::{
CatArgs, CatSolution, CoinProof, EverythingWithSignatureTailArgs, CAT_PUZZLE_HASH,
use chia_puzzles::{
cat::{CatArgs, CatSolution, CoinProof, EverythingWithSignatureTailArgs, CAT_PUZZLE_HASH},
LineageProof,
};
use clvm_traits::clvm_quote;
use clvm_utils::CurriedProgram;
Expand All @@ -16,8 +17,8 @@ pub struct IssueCat {

pub struct CatIssuanceInfo {
pub asset_id: Bytes32,
pub lineage_proof: LineageProof,
pub eve_coin: Coin,
pub eve_inner_puzzle_hash: Bytes32,
}

impl IssueCat {
Expand All @@ -44,7 +45,7 @@ impl IssueCat {
public_key: PublicKey,
amount: u64,
) -> Result<(ChainedSpend, CatIssuanceInfo), SpendError> {
let tail_puzzle_ptr = ctx.everything_with_signature_tail_puzzle();
let tail_puzzle_ptr = ctx.everything_with_signature_tail_puzzle()?;

let tail = ctx.alloc(CurriedProgram {
program: tail_puzzle_ptr,
Expand All @@ -56,16 +57,16 @@ impl IssueCat {
program: tail,
solution: NodePtr::NIL,
})?)
.finish(ctx, asset_id, amount)
.finish_raw(ctx, asset_id, amount)
}

pub fn finish(
pub fn finish_raw(
self,
ctx: &mut SpendContext,
asset_id: Bytes32,
amount: u64,
) -> Result<(ChainedSpend, CatIssuanceInfo), SpendError> {
let cat_puzzle_ptr = ctx.cat_puzzle();
let cat_puzzle_ptr = ctx.cat_puzzle()?;

let inner_puzzle = ctx.alloc(clvm_quote!(self.conditions))?;
let inner_puzzle_hash = ctx.tree_hash(inner_puzzle).into();
Expand All @@ -80,13 +81,13 @@ impl IssueCat {
})?;

let puzzle_hash = ctx.tree_hash(puzzle).into();
let coin = Coin::new(self.parent_coin_id, puzzle_hash, amount);
let eve_coin = Coin::new(self.parent_coin_id, puzzle_hash, amount);

let solution = ctx.serialize(CatSolution {
inner_puzzle_solution: (),
lineage_proof: None,
prev_coin_id: coin.coin_id(),
this_coin_info: coin,
prev_coin_id: eve_coin.coin_id(),
this_coin_info: eve_coin,
next_coin_proof: CoinProof {
parent_coin_info: self.parent_coin_id,
inner_puzzle_hash,
Expand All @@ -97,7 +98,7 @@ impl IssueCat {
})?;

let puzzle_reveal = ctx.serialize(puzzle)?;
ctx.spend(CoinSpend::new(coin, puzzle_reveal, solution));
ctx.spend(CoinSpend::new(eve_coin, puzzle_reveal, solution));

let chained_spend = ChainedSpend {
parent_conditions: vec![ctx.alloc(CreateCoinWithMemos {
Expand All @@ -109,8 +110,12 @@ impl IssueCat {

let issuance_info = CatIssuanceInfo {
asset_id,
eve_coin: coin,
eve_inner_puzzle_hash: inner_puzzle_hash,
lineage_proof: LineageProof {
parent_parent_coin_id: eve_coin.parent_coin_info,
parent_inner_puzzle_hash: inner_puzzle_hash,
parent_amount: eve_coin.amount,
},
eve_coin,
};

Ok((chained_spend, issuance_info))
Expand Down
124 changes: 18 additions & 106 deletions src/spends/puzzles/did/create_did.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
use chia_bls::PublicKey;
use chia_protocol::Bytes32;
use chia_puzzles::{
did::DID_INNER_PUZZLE_HASH,
singleton::{SINGLETON_LAUNCHER_PUZZLE_HASH, SINGLETON_TOP_LAYER_PUZZLE_HASH},
did::{DidArgs, DID_INNER_PUZZLE_HASH},
singleton::SingletonStruct,
standard::{StandardArgs, STANDARD_PUZZLE_HASH},
EveProof, Proof,
};
use clvm_traits::ToClvm;
use clvm_utils::{curry_tree_hash, tree_hash_atom, tree_hash_pair, CurriedProgram, ToTreeHash};
use clvm_utils::{tree_hash_atom, CurriedProgram, ToTreeHash, TreeHash};
use clvmr::NodePtr;

use crate::{
u64_to_bytes, ChainedSpend, DidInfo, SpendContext, SpendError, SpendableLauncher,
StandardDidSpend,
};
use crate::{ChainedSpend, DidInfo, SpendContext, SpendError, SpendableLauncher, StandardDidSpend};

pub trait CreateDid {
fn create_eve_did<M>(
Expand Down Expand Up @@ -86,15 +83,20 @@ impl CreateDid for SpendableLauncher {
M: ToClvm<NodePtr>,
{
let metadata_ptr = ctx.alloc(&metadata)?;
let metadata_hash = ctx.tree_hash(metadata_ptr).into();

let did_inner_puzzle_hash = did_inner_puzzle_hash(
p2_puzzle_hash,
recovery_did_list_hash,
num_verifications_required,
self.coin().coin_id(),
metadata_hash,
);
let metadata_hash = ctx.tree_hash(metadata_ptr);

let did_inner_puzzle_hash = CurriedProgram {
program: DID_INNER_PUZZLE_HASH,
args: DidArgs {
inner_puzzle: TreeHash::from(p2_puzzle_hash),
recovery_did_list_hash,
num_verifications_required,
metadata: metadata_hash,
singleton_struct: SingletonStruct::new(self.coin().coin_id()),
},
}
.tree_hash()
.into();

let launcher_coin = self.coin();
let (chained_spend, eve_coin) = self.spend(ctx, did_inner_puzzle_hash, ())?;
Expand All @@ -118,93 +120,3 @@ impl CreateDid for SpendableLauncher {
Ok((chained_spend, did_info))
}
}

pub fn did_inner_puzzle_hash(
inner_puzzle_hash: Bytes32,
recovery_did_list_hash: Bytes32,
num_verifications_required: u64,
launcher_id: Bytes32,
metadata_hash: Bytes32,
) -> Bytes32 {
let recovery_hash = tree_hash_atom(&recovery_did_list_hash);
let num_verifications_hash = tree_hash_atom(&u64_to_bytes(num_verifications_required));

let singleton_hash = tree_hash_atom(&SINGLETON_TOP_LAYER_PUZZLE_HASH);
let launcher_id_hash = tree_hash_atom(&launcher_id);
let launcher_puzzle_hash = tree_hash_atom(&SINGLETON_LAUNCHER_PUZZLE_HASH);

let pair = tree_hash_pair(launcher_id_hash, launcher_puzzle_hash);
let singleton_struct_hash = tree_hash_pair(singleton_hash, pair);

curry_tree_hash(
DID_INNER_PUZZLE_HASH,
&[
inner_puzzle_hash.into(),
recovery_hash,
num_verifications_hash,
singleton_struct_hash,
metadata_hash.into(),
],
)
.into()
}

#[cfg(test)]
mod tests {
use super::*;

use chia_puzzles::{
did::DidArgs,
singleton::{
SingletonStruct, SINGLETON_LAUNCHER_PUZZLE_HASH, SINGLETON_TOP_LAYER_PUZZLE_HASH,
},
};
use clvm_utils::CurriedProgram;
use clvmr::Allocator;

#[test]
fn test_puzzle_hash() {
let mut allocator = Allocator::new();
let mut ctx = SpendContext::new(&mut allocator);

let inner_puzzle = ctx.alloc([1, 2, 3]).unwrap();
let inner_puzzle_hash = ctx.tree_hash(inner_puzzle).into();

let metadata = ctx.alloc([4, 5, 6]).unwrap();
let metadata_hash = ctx.tree_hash(metadata).into();

let launcher_id = Bytes32::new([34; 32]);
let recovery_did_list_hash = Bytes32::new([42; 32]);
let num_verifications_required = 2;

let did_inner_puzzle = ctx.did_inner_puzzle();

let puzzle = ctx
.alloc(CurriedProgram {
program: did_inner_puzzle,
args: DidArgs {
inner_puzzle,
recovery_did_list_hash,
num_verifications_required,
singleton_struct: SingletonStruct {
mod_hash: SINGLETON_TOP_LAYER_PUZZLE_HASH.into(),
launcher_id,
launcher_puzzle_hash: SINGLETON_LAUNCHER_PUZZLE_HASH.into(),
},
metadata,
},
})
.unwrap();
let allocated_puzzle_hash = ctx.tree_hash(puzzle);

let puzzle_hash = did_inner_puzzle_hash(
inner_puzzle_hash,
recovery_did_list_hash,
num_verifications_required,
launcher_id,
metadata_hash,
);

assert_eq!(hex::encode(allocated_puzzle_hash), hex::encode(puzzle_hash));
}
}
2 changes: 1 addition & 1 deletion src/spends/puzzles/did/did_spend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub fn raw_did_spend<M>(
where
M: ToClvm<NodePtr>,
{
let did_inner_puzzle = ctx.did_inner_puzzle();
let did_inner_puzzle = ctx.did_inner_puzzle()?;

let puzzle = ctx.alloc(CurriedProgram {
program: did_inner_puzzle,
Expand Down
Loading

0 comments on commit 5b4b2bf

Please sign in to comment.