use chia_protocol::{Coin, CoinSpend};
use clvm_traits::{FromClvm, ToClvm};
use clvmr::{Allocator, NodePtr};
use crate::{DriverError, Puzzle, Spend, SpendContext};
pub trait Layer {
type Solution;
fn parse_puzzle(allocator: &Allocator, puzzle: Puzzle) -> Result<Option<Self>, DriverError>
where
Self: Sized;
fn parse_solution(
allocator: &Allocator,
solution: NodePtr,
) -> Result<Self::Solution, DriverError>;
fn construct_puzzle(&self, ctx: &mut SpendContext) -> Result<NodePtr, DriverError>;
fn construct_solution(
&self,
ctx: &mut SpendContext,
solution: Self::Solution,
) -> Result<NodePtr, DriverError>;
fn construct_spend(
&self,
ctx: &mut SpendContext,
solution: Self::Solution,
) -> Result<Spend, DriverError> {
let solution = self.construct_solution(ctx, solution)?;
let puzzle = self.construct_puzzle(ctx)?;
Ok(Spend::new(puzzle, solution))
}
fn construct_coin_spend(
&self,
ctx: &mut SpendContext,
coin: Coin,
solution: Self::Solution,
) -> Result<CoinSpend, DriverError> {
let solution = self.construct_solution(ctx, solution)?;
let puzzle = self.construct_puzzle(ctx)?;
Ok(CoinSpend::new(
coin,
ctx.serialize(&puzzle)?,
ctx.serialize(&solution)?,
))
}
}
impl<T> Layer for T
where
T: ToClvm<Allocator> + FromClvm<Allocator>,
{
type Solution = NodePtr;
fn parse_puzzle(allocator: &Allocator, puzzle: Puzzle) -> Result<Option<Self>, DriverError> {
Ok(Some(T::from_clvm(allocator, puzzle.ptr())?))
}
fn parse_solution(
_allocator: &Allocator,
solution: NodePtr,
) -> Result<Self::Solution, DriverError> {
Ok(solution)
}
fn construct_puzzle(&self, ctx: &mut SpendContext) -> Result<NodePtr, DriverError> {
ctx.alloc(self)
}
fn construct_solution(
&self,
_ctx: &mut SpendContext,
solution: Self::Solution,
) -> Result<NodePtr, DriverError> {
Ok(solution)
}
}