Skip to content

Commit

Permalink
Merge pull request #47 from Rigidity/condition-constructors
Browse files Browse the repository at this point in the history
Condition constructors
  • Loading branch information
Rigidity authored Jun 9, 2024
2 parents fee3291 + 4f25273 commit a844bb4
Show file tree
Hide file tree
Showing 13 changed files with 302 additions and 83 deletions.
14 changes: 4 additions & 10 deletions crates/chia-sdk-driver/src/puzzles/cat/issue_cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,8 @@ impl IssueCat {
})?;
let asset_id = ctx.tree_hash(tail).into();

self.raw_condition(ctx.alloc(&RunTail {
program: tail,
solution: NodePtr::NIL,
})?)
.finish_raw(ctx, asset_id, amount)
self.raw_condition(ctx.alloc(&RunTail::new(tail, ()))?)
.finish_raw(ctx, asset_id, amount)
}

pub fn multi_issuance(
Expand All @@ -75,11 +72,8 @@ impl IssueCat {
})?;
let asset_id = ctx.tree_hash(tail).into();

self.raw_condition(ctx.alloc(&RunTail {
program: tail,
solution: NodePtr::NIL,
})?)
.finish_raw(ctx, asset_id, amount)
self.raw_condition(ctx.alloc(&RunTail::new(tail, ()))?)
.finish_raw(ctx, asset_id, amount)
}

pub fn finish_raw(
Expand Down
6 changes: 1 addition & 5 deletions crates/chia-sdk-driver/src/spend_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ pub trait P2Spend: Sized {
amount: u64,
hint: Bytes32,
) -> Result<Self, SpendError> {
Ok(self.raw_condition(ctx.alloc(&CreateCoin::with_custom_hint(
puzzle_hash,
amount,
hint,
))?))
Ok(self.raw_condition(ctx.alloc(&CreateCoin::with_hint(puzzle_hash, amount, hint))?))
}

fn create_coin_announcement(
Expand Down
27 changes: 17 additions & 10 deletions crates/chia-sdk-parser/src/puzzles/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,24 @@ impl NftPuzzle {
ownership_solution.inner_solution,
)?;

let create_coin = conditions.iter().find_map(|condition| match condition {
Condition::CreateCoin(create_coin) if create_coin.amount % 2 == 1 => Some(create_coin),
_ => None,
});

let new_owner = conditions.iter().find_map(|condition| match condition {
Condition::NewNftOwner(new_owner) => Some(new_owner),
_ => None,
});
let mut create_coin = None;
let mut new_nft_owner = None;

for condition in conditions {
match condition {
Condition::CreateCoin(condition) => {
create_coin = Some(condition);
}
Condition::Other(condition) => {
if let Ok(condition) = NewNftOwner::from_clvm(allocator, condition) {
new_nft_owner = Some(condition);
}
}
_ => {}
}
}

Ok((create_coin.cloned(), new_owner.cloned()))
Ok((create_coin, new_nft_owner))
}

pub fn child_coin_info(
Expand Down
10 changes: 3 additions & 7 deletions crates/chia-sdk-signer/src/required_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ impl RequiredSignature {
let appended_info = match condition.kind {
AggSigKind::Parent => {
hasher.update([43]);
let parent = coin.parent_coin_info;
parent.to_vec()
coin.parent_coin_info.to_vec()
}
AggSigKind::Puzzle => {
hasher.update([44]);
let puzzle = coin.puzzle_hash;
puzzle.to_vec()
coin.puzzle_hash.to_vec()
}
AggSigKind::Amount => {
hasher.update([45]);
Expand All @@ -56,9 +54,7 @@ impl RequiredSignature {
}
AggSigKind::ParentPuzzle => {
hasher.update([48]);
let parent = coin.parent_coin_info;
let puzzle = coin.puzzle_hash;
[parent.to_vec(), puzzle.to_vec()].concat()
[coin.parent_coin_info.to_vec(), coin.puzzle_hash.to_vec()].concat()
}
AggSigKind::Unsafe => {
return Self {
Expand Down
31 changes: 8 additions & 23 deletions crates/chia-sdk-test/src/simulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl Drop for Simulator {
mod tests {
use chia_bls::{PublicKey, Signature};
use chia_protocol::{
CoinSpend, CoinStateFilters, CoinStateUpdate, RejectCoinState, RejectPuzzleState,
Bytes, CoinSpend, CoinStateFilters, CoinStateUpdate, RejectCoinState, RejectPuzzleState,
RequestCoinState, RequestPuzzleState, RespondCoinState, RespondPuzzleState, SpendBundle,
};
use chia_sdk_types::conditions::{AggSigMe, CreateCoin, Remark};
Expand Down Expand Up @@ -245,10 +245,7 @@ mod tests {
vec![CoinSpend::new(
coin,
puzzle_reveal,
to_program([AggSigMe {
public_key,
message: Vec::new().into(),
}])?,
to_program([AggSigMe::new(public_key, Bytes::default())])?,
)],
Signature::default(),
);
Expand All @@ -272,10 +269,7 @@ mod tests {
vec![CoinSpend::new(
coin,
puzzle_reveal,
to_program([AggSigMe {
public_key: PublicKey::default(),
message: Vec::new().into(),
}])?,
to_program([AggSigMe::new(PublicKey::default(), Bytes::default())])?,
)],
Signature::default(),
);
Expand All @@ -302,10 +296,7 @@ mod tests {
vec![CoinSpend::new(
coin,
puzzle_reveal,
to_program([AggSigMe {
public_key: pk,
message: b"Hello, world!".to_vec().into(),
}])?,
to_program([AggSigMe::new(pk, b"Hello, world!".to_vec().into())])?,
)],
&[sk],
sim.config.genesis_challenge,
Expand Down Expand Up @@ -336,14 +327,8 @@ mod tests {
coin,
puzzle_reveal,
to_program([
AggSigMe {
public_key: pk1,
message: b"Hello, world!".to_vec().into(),
},
AggSigMe {
public_key: pk2,
message: b"Goodbye, world!".to_vec().into(),
},
AggSigMe::new(pk1, b"Hello, world!".to_vec().into()),
AggSigMe::new(pk2, b"Goodbye, world!".to_vec().into()),
])?,
)],
&[sk1, sk2],
Expand Down Expand Up @@ -485,7 +470,7 @@ mod tests {
let peer = sim.connect().await?;

let (puzzle_hash, puzzle_reveal) = to_puzzle(1)?;
let solution = to_program([Remark {}])?;
let solution = to_program([Remark::new(())])?;

let coin = sim.mint_coin(puzzle_hash, 0).await;

Expand Down Expand Up @@ -734,7 +719,7 @@ mod tests {
vec![CoinSpend::new(
coin,
puzzle_reveal,
to_program([CreateCoin::with_custom_hint(puzzle_hash, 0, hint)])?,
to_program([CreateCoin::with_hint(puzzle_hash, 0, hint)])?,
)],
Signature::default(),
);
Expand Down
23 changes: 5 additions & 18 deletions crates/chia-sdk-types/src/conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ pub use output::*;
pub use puzzles::*;
pub use time::*;

use clvm_traits::{apply_constants, FromClvm, ToClvm};
use clvm_traits::{FromClvm, ToClvm};
use clvmr::NodePtr;

#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
#[clvm(transparent)]
pub enum Condition<T> {
Remark(Remark),
pub enum Condition<T = NodePtr> {
Remark(Remark<T>),
AggSig(AggSig),
CreateCoin(CreateCoin),
ReserveFee(ReserveFee),
Expand All @@ -45,19 +46,5 @@ pub enum Condition<T> {
AssertBeforeHeightRelative(AssertBeforeHeightRelative),
AssertBeforeHeightAbsolute(AssertBeforeHeightAbsolute),
Softfork(Softfork<T>),
RunTail(RunTail<T, T>),
MeltSingleton(MeltSingleton),
NewNftOwner(NewNftOwner),
}

#[derive(ToClvm, FromClvm)]
#[apply_constants]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[clvm(list)]
pub struct Softfork<T> {
#[clvm(constant = 90)]
pub opcode: u8,
pub cost: u64,
#[clvm(rest)]
pub rest: T,
Other(T),
}
82 changes: 82 additions & 0 deletions crates/chia-sdk-types/src/conditions/agg_sig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ pub struct AggSig {
pub message: Bytes,
}

impl AggSig {
pub fn new(kind: AggSigKind, public_key: PublicKey, message: Bytes) -> Self {
Self {
kind,
public_key,
message,
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, ToClvm, FromClvm, Hash)]
#[repr(u8)]
#[clvm(atom)]
Expand All @@ -35,6 +45,15 @@ pub struct AggSigParent {
pub message: Bytes,
}

impl AggSigParent {
pub fn new(public_key: PublicKey, message: Bytes) -> Self {
Self {
public_key,
message,
}
}
}

#[derive(ToClvm, FromClvm)]
#[apply_constants]
#[derive(Debug, Clone, PartialEq, Eq)]
Expand All @@ -46,6 +65,15 @@ pub struct AggSigPuzzle {
pub message: Bytes,
}

impl AggSigPuzzle {
pub fn new(public_key: PublicKey, message: Bytes) -> Self {
Self {
public_key,
message,
}
}
}

#[derive(ToClvm, FromClvm)]
#[apply_constants]
#[derive(Debug, Clone, PartialEq, Eq)]
Expand All @@ -57,6 +85,15 @@ pub struct AggSigAmount {
pub message: Bytes,
}

impl AggSigAmount {
pub fn new(public_key: PublicKey, message: Bytes) -> Self {
Self {
public_key,
message,
}
}
}

#[derive(ToClvm, FromClvm)]
#[apply_constants]
#[derive(Debug, Clone, PartialEq, Eq)]
Expand All @@ -68,6 +105,15 @@ pub struct AggSigPuzzleAmount {
pub message: Bytes,
}

impl AggSigPuzzleAmount {
pub fn new(public_key: PublicKey, message: Bytes) -> Self {
Self {
public_key,
message,
}
}
}

#[derive(ToClvm, FromClvm)]
#[apply_constants]
#[derive(Debug, Clone, PartialEq, Eq)]
Expand All @@ -79,6 +125,15 @@ pub struct AggSigParentAmount {
pub message: Bytes,
}

impl AggSigParentAmount {
pub fn new(public_key: PublicKey, message: Bytes) -> Self {
Self {
public_key,
message,
}
}
}

#[derive(ToClvm, FromClvm)]
#[apply_constants]
#[derive(Debug, Clone, PartialEq, Eq)]
Expand All @@ -90,6 +145,15 @@ pub struct AggSigParentPuzzle {
pub message: Bytes,
}

impl AggSigParentPuzzle {
pub fn new(public_key: PublicKey, message: Bytes) -> Self {
Self {
public_key,
message,
}
}
}

#[derive(ToClvm, FromClvm)]
#[apply_constants]
#[derive(Debug, Clone, PartialEq, Eq)]
Expand All @@ -101,6 +165,15 @@ pub struct AggSigUnsafe {
pub message: Bytes,
}

impl AggSigUnsafe {
pub fn new(public_key: PublicKey, message: Bytes) -> Self {
Self {
public_key,
message,
}
}
}

#[derive(ToClvm, FromClvm)]
#[apply_constants]
#[derive(Debug, Clone, PartialEq, Eq)]
Expand All @@ -111,3 +184,12 @@ pub struct AggSigMe {
pub public_key: PublicKey,
pub message: Bytes,
}

impl AggSigMe {
pub fn new(public_key: PublicKey, message: Bytes) -> Self {
Self {
public_key,
message,
}
}
}
Loading

0 comments on commit a844bb4

Please sign in to comment.