Skip to content

Commit

Permalink
Add test for CAT spend
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Feb 22, 2024
1 parent e8476ff commit 2802b6a
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 9 deletions.
89 changes: 89 additions & 0 deletions src/spends/cat/raw_spend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,92 @@ pub fn spend_cat_coins(
})
.collect()
}

#[cfg(test)]
mod tests {
use chia_bls::{derive_keys::master_to_wallet_unhardened, SecretKey};
use chia_protocol::Bytes32;
use chia_wallet::{
cat::{cat_puzzle_hash, CAT_PUZZLE},
standard::{standard_puzzle_hash, DEFAULT_HIDDEN_PUZZLE_HASH, STANDARD_PUZZLE},
DeriveSynthetic,
};
use clvmr::serde::{node_from_bytes, node_to_bytes};
use hex_literal::hex;

use crate::testing::SEED;

use super::*;

#[test]
fn test_cat_spend() {
let synthetic_key =
master_to_wallet_unhardened(&SecretKey::from_seed(SEED.as_ref()).public_key(), 0)
.derive_synthetic(&DEFAULT_HIDDEN_PUZZLE_HASH);

let a = &mut Allocator::new();
let standard_puzzle_ptr = node_from_bytes(a, &STANDARD_PUZZLE).unwrap();
let cat_puzzle_ptr = node_from_bytes(a, &CAT_PUZZLE).unwrap();

let asset_id = [42; 32];

let p2_puzzle_hash = standard_puzzle_hash(&synthetic_key);
let cat_puzzle_hash = cat_puzzle_hash(asset_id, p2_puzzle_hash);

let parent_coin = Coin::new(Bytes32::new([0; 32]), Bytes32::new(cat_puzzle_hash), 69);
let coin = Coin::new(
Bytes32::from(parent_coin.coin_id()),
Bytes32::new(cat_puzzle_hash),
42,
);

let conditions = vec![CatCondition::Normal(Condition::CreateCoin(
CreateCoin::Normal {
puzzle_hash: coin.puzzle_hash,
amount: coin.amount,
},
))];

let coin_spend = spend_cat_coins(
a,
standard_puzzle_ptr,
cat_puzzle_ptr,
asset_id,
&[CatSpend {
coin,
synthetic_key,
conditions,
extra_delta: 0,
lineage_proof: LineageProof {
parent_coin_info: parent_coin.parent_coin_info,
inner_puzzle_hash: p2_puzzle_hash.into(),
amount: parent_coin.amount,
},
p2_puzzle_hash,
}],
)
.unwrap()
.remove(0);

let output_ptr = coin_spend
.puzzle_reveal
.run(a, 0, u64::MAX, &coin_spend.solution)
.unwrap()
.1;
let actual = node_to_bytes(a, output_ptr).unwrap();

let expected = hex!(
"
ffff46ffa06438c882c2db9f5c2a8b4cbda9258c40a6583b2d7c6becc1678607
4d558c834980ffff3cffa1cb9c4d253a0e1a091d620a55616e104f3329f58ee8
6e708d0527b1cc58a73b649e80ffff3dffa0c3bb7f0a7e1bd2cae332bbd0d1a7
e275c1e6c643b2659e22c24f513886d3874e80ffff32ffb08584adae5630842a
1766bc444d2b872dd3080f4e5daaecf6f762a4be7dc148f37868149d4217f3dc
c9183fe61e48d8bfffa0e5924c23faf33c9a1bf18c70d40cb09e4b194f521b9f
6fceb2685c0612ac34a980ffff33ffa0f9f2d59294f2aae8f9833db876d1bf43
95d46af18c17312041c6f4a4d73fa041ff2a8080
"
);
assert_eq!(hex::encode(actual), hex::encode(expected));
}
}
13 changes: 4 additions & 9 deletions src/spends/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,15 @@ mod tests {

use super::*;

// Calculates a synthetic key at the given derivation index, using the test seed.
fn synthetic_key(index: u32) -> PublicKey {
let sk = SecretKey::from_seed(SEED.as_ref());
let pk = sk.public_key();
let child_key = master_to_wallet_unhardened(&pk, index);
child_key.derive_synthetic(&DEFAULT_HIDDEN_PUZZLE_HASH)
}

#[test]
fn test_standard_spend() {
let synthetic_key =
master_to_wallet_unhardened(&SecretKey::from_seed(SEED.as_ref()).public_key(), 0)
.derive_synthetic(&DEFAULT_HIDDEN_PUZZLE_HASH);

let a = &mut Allocator::new();
let standard_puzzle_ptr = node_from_bytes(a, &STANDARD_PUZZLE).unwrap();
let coin = Coin::new(Bytes32::from([0; 32]), Bytes32::from([1; 32]), 42);
let synthetic_key = synthetic_key(0);

let conditions = vec![Condition::CreateCoin(CreateCoin::Normal {
puzzle_hash: coin.puzzle_hash,
Expand Down

0 comments on commit 2802b6a

Please sign in to comment.