-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from xch-dev/clawbacks
Clawbacks
- Loading branch information
Showing
23 changed files
with
633 additions
and
47 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
crates/chia-sdk-driver/src/layers/augmented_condition_layer.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use chia_sdk_types::{ | ||
AugmentedConditionArgs, AugmentedConditionSolution, Condition, AUGMENTED_CONDITION_PUZZLE_HASH, | ||
}; | ||
use clvm_traits::{FromClvm, ToClvm}; | ||
use clvmr::{Allocator, NodePtr}; | ||
|
||
use crate::{DriverError, Layer, Puzzle, SpendContext}; | ||
|
||
/// The augmented condition [`Layer`] allows for adding a condition to a puzzle's output. | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub struct AugmentedConditionLayer<T, I> { | ||
pub condition: Condition<T>, | ||
pub inner_puzzle: I, | ||
} | ||
|
||
impl<T, I> Layer for AugmentedConditionLayer<T, I> | ||
where | ||
T: ToClvm<Allocator> + FromClvm<Allocator> + Clone, | ||
I: Layer, | ||
{ | ||
type Solution = AugmentedConditionSolution<NodePtr>; | ||
|
||
fn parse_puzzle(allocator: &Allocator, puzzle: Puzzle) -> Result<Option<Self>, DriverError> { | ||
let Some(puzzle) = puzzle.as_curried() else { | ||
return Ok(None); | ||
}; | ||
|
||
if puzzle.mod_hash != AUGMENTED_CONDITION_PUZZLE_HASH { | ||
return Ok(None); | ||
} | ||
|
||
let args = AugmentedConditionArgs::<T, Puzzle>::from_clvm(allocator, puzzle.args)?; | ||
let Some(inner_layer) = I::parse_puzzle(allocator, args.inner_puzzle)? else { | ||
return Ok(None); | ||
}; | ||
|
||
Ok(Some(Self { | ||
condition: args.condition, | ||
inner_puzzle: inner_layer, | ||
})) | ||
} | ||
|
||
fn parse_solution( | ||
allocator: &Allocator, | ||
solution: NodePtr, | ||
) -> Result<Self::Solution, DriverError> { | ||
Ok(AugmentedConditionSolution::<NodePtr>::from_clvm( | ||
allocator, solution, | ||
)?) | ||
} | ||
|
||
fn construct_puzzle(&self, ctx: &mut SpendContext) -> Result<NodePtr, DriverError> { | ||
let inner_puzzle = self.inner_puzzle.construct_puzzle(ctx)?; | ||
let curried = ctx.curry(AugmentedConditionArgs { | ||
condition: self.condition.clone(), | ||
inner_puzzle, | ||
})?; | ||
ctx.alloc(&curried) | ||
} | ||
|
||
fn construct_solution( | ||
&self, | ||
ctx: &mut SpendContext, | ||
solution: Self::Solution, | ||
) -> Result<NodePtr, DriverError> { | ||
ctx.alloc(&solution) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use chia_protocol::Bytes32; | ||
use chia_sdk_types::{P2CurriedArgs, P2CurriedSolution, P2_CURRIED_PUZZLE_HASH}; | ||
use clvm_traits::FromClvm; | ||
use clvmr::{Allocator, NodePtr}; | ||
|
||
use crate::{DriverError, Layer, Puzzle, SpendContext}; | ||
|
||
/// The p2 curried [`Layer`] allows for . | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
pub struct P2CurriedLayer { | ||
pub puzzle_hash: Bytes32, | ||
} | ||
|
||
impl Layer for P2CurriedLayer { | ||
type Solution = P2CurriedSolution<NodePtr, NodePtr>; | ||
|
||
fn parse_puzzle(allocator: &Allocator, puzzle: Puzzle) -> Result<Option<Self>, DriverError> { | ||
let Some(puzzle) = puzzle.as_curried() else { | ||
return Ok(None); | ||
}; | ||
|
||
if puzzle.mod_hash != P2_CURRIED_PUZZLE_HASH { | ||
return Ok(None); | ||
} | ||
|
||
let args = P2CurriedArgs::from_clvm(allocator, puzzle.args)?; | ||
|
||
Ok(Some(Self { | ||
puzzle_hash: args.puzzle_hash, | ||
})) | ||
} | ||
|
||
fn parse_solution( | ||
allocator: &Allocator, | ||
solution: NodePtr, | ||
) -> Result<Self::Solution, DriverError> { | ||
Ok(P2CurriedSolution::<NodePtr, NodePtr>::from_clvm( | ||
allocator, solution, | ||
)?) | ||
} | ||
|
||
fn construct_puzzle(&self, ctx: &mut SpendContext) -> Result<NodePtr, DriverError> { | ||
let curried = ctx.curry(P2CurriedArgs { | ||
puzzle_hash: self.puzzle_hash, | ||
})?; | ||
ctx.alloc(&curried) | ||
} | ||
|
||
fn construct_solution( | ||
&self, | ||
ctx: &mut SpendContext, | ||
solution: Self::Solution, | ||
) -> Result<NodePtr, DriverError> { | ||
ctx.alloc(&solution) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.