chia_sdk_driver/actions/
run_tail.rs

1use chia_sdk_types::Conditions;
2
3use crate::{Delta, Deltas, DriverError, Id, Spend, SpendAction, SpendContext, SpendKind, Spends};
4
5#[derive(Debug, Clone, Copy)]
6pub struct RunTailAction {
7    pub id: Id,
8    pub tail_spend: Spend,
9    pub supply_delta: Delta,
10}
11
12impl RunTailAction {
13    pub fn new(id: Id, tail_spend: Spend, supply_delta: Delta) -> Self {
14        Self {
15            id,
16            tail_spend,
17            supply_delta,
18        }
19    }
20}
21
22impl SpendAction for RunTailAction {
23    fn calculate_delta(&self, deltas: &mut Deltas, _index: usize) {
24        *deltas.update(Id::Xch) += -self.supply_delta;
25        *deltas.update(self.id) += self.supply_delta;
26        deltas.set_needed(self.id);
27    }
28
29    fn spend(
30        &self,
31        ctx: &mut SpendContext,
32        spends: &mut Spends,
33        _index: usize,
34    ) -> Result<(), DriverError> {
35        let cat = spends
36            .cats
37            .get_mut(&self.id)
38            .ok_or(DriverError::InvalidAssetId)?;
39
40        let source_index = cat.run_tail_source(ctx)?;
41        let source = &mut cat.items[source_index];
42
43        match &mut source.kind {
44            SpendKind::Conditions(spend) => {
45                spend.add_conditions(
46                    Conditions::new()
47                        .run_cat_tail(self.tail_spend.puzzle, self.tail_spend.solution),
48                );
49            }
50            SpendKind::Settlement(_) => {
51                return Err(DriverError::CannotEmitConditions);
52            }
53        }
54
55        Ok(())
56    }
57}
58
59#[cfg(test)]
60mod tests {
61    use anyhow::Result;
62    use chia_protocol::Bytes32;
63    use chia_puzzle_types::cat::EverythingWithSignatureTailArgs;
64    use chia_sdk_test::Simulator;
65    use clvmr::NodePtr;
66    use indexmap::indexmap;
67    use rstest::rstest;
68
69    use crate::{Action, Relation};
70
71    use super::*;
72
73    #[rstest]
74    #[case::normal(None)]
75    #[case::revocable(Some(Bytes32::default()))]
76    fn test_action_melt_cat(#[case] hidden_puzzle_hash: Option<Bytes32>) -> Result<()> {
77        let mut sim = Simulator::new();
78        let mut ctx = SpendContext::new();
79
80        let alice = sim.bls(1);
81
82        let tail = ctx.curry(EverythingWithSignatureTailArgs::new(alice.pk))?;
83        let tail_spend = Spend::new(tail, NodePtr::NIL);
84
85        let mut spends = Spends::new(alice.puzzle_hash);
86        spends.add(alice.coin);
87
88        let deltas = spends.apply(
89            &mut ctx,
90            &[
91                Action::issue_cat(tail_spend, hidden_puzzle_hash, 1),
92                Action::run_tail(Id::New(0), tail_spend, Delta::new(0, 1)),
93            ],
94        )?;
95
96        let outputs = spends.finish_with_keys(
97            &mut ctx,
98            &deltas,
99            Relation::None,
100            &indexmap! { alice.puzzle_hash => alice.pk },
101        )?;
102
103        sim.spend_coins(ctx.take(), &[alice.sk])?;
104
105        // TODO: Filter outputs better
106        let coin = outputs
107            .xch
108            .iter()
109            .find(|c| c.puzzle_hash == alice.puzzle_hash)
110            .expect("missing coin");
111        assert_ne!(sim.coin_state(coin.coin_id()), None);
112        assert_eq!(coin.amount, 1);
113
114        Ok(())
115    }
116
117    #[rstest]
118    #[case::normal(None)]
119    #[case::revocable(Some(Bytes32::default()))]
120    fn test_action_melt_cat_separate_spends(
121        #[case] hidden_puzzle_hash: Option<Bytes32>,
122    ) -> Result<()> {
123        let mut sim = Simulator::new();
124        let mut ctx = SpendContext::new();
125
126        let alice = sim.bls(1);
127
128        let tail = ctx.curry(EverythingWithSignatureTailArgs::new(alice.pk))?;
129        let tail_spend = Spend::new(tail, NodePtr::NIL);
130
131        let mut spends = Spends::new(alice.puzzle_hash);
132        spends.add(alice.coin);
133
134        let deltas = spends.apply(
135            &mut ctx,
136            &[Action::issue_cat(tail_spend, hidden_puzzle_hash, 1)],
137        )?;
138
139        let outputs = spends.finish_with_keys(
140            &mut ctx,
141            &deltas,
142            Relation::None,
143            &indexmap! { alice.puzzle_hash => alice.pk },
144        )?;
145
146        sim.spend_coins(ctx.take(), &[alice.sk.clone()])?;
147
148        let cat = outputs.cats[&Id::New(0)][0];
149
150        let mut spends = Spends::new(alice.puzzle_hash);
151        spends.add(sim.new_coin(alice.puzzle_hash, 0));
152        spends.add(cat);
153
154        let deltas = spends.apply(
155            &mut ctx,
156            &[Action::run_tail(
157                Id::Existing(cat.info.asset_id),
158                tail_spend,
159                Delta::new(0, 1),
160            )],
161        )?;
162
163        let outputs = spends.finish_with_keys(
164            &mut ctx,
165            &deltas,
166            Relation::None,
167            &indexmap! { alice.puzzle_hash => alice.pk },
168        )?;
169
170        sim.spend_coins(ctx.take(), &[alice.sk])?;
171
172        let coin = outputs.xch[0];
173        assert_ne!(sim.coin_state(coin.coin_id()), None);
174        assert_eq!(coin.puzzle_hash, alice.puzzle_hash);
175        assert_eq!(coin.amount, 1);
176
177        Ok(())
178    }
179}