From b181b4d61967a184381bc4f819aeac282491f0a0 Mon Sep 17 00:00:00 2001 From: Rigidity Date: Sat, 16 Mar 2024 12:47:44 -0400 Subject: [PATCH] Change coin_id to return Bytes32 --- .../chia-consensus/fuzz/fuzz_targets/fast-forward.rs | 2 +- .../fuzz/fuzz_targets/parse-conditions.rs | 3 +-- crates/chia-consensus/src/fast_forward.rs | 10 +++++----- crates/chia-consensus/src/gen/run_puzzle.rs | 3 +-- crates/chia-protocol/fuzz/fuzz_targets/spend-bundle.rs | 2 +- crates/chia-protocol/src/coin.rs | 7 ++++--- crates/chia-protocol/src/spend_bundle.rs | 4 ++-- crates/chia-tools/src/bin/fast-forward-spend.rs | 2 +- wheel/src/api.rs | 2 +- 9 files changed, 17 insertions(+), 18 deletions(-) diff --git a/crates/chia-consensus/fuzz/fuzz_targets/fast-forward.rs b/crates/chia-consensus/fuzz/fuzz_targets/fast-forward.rs index 11768ae12..b588ba887 100644 --- a/crates/chia-consensus/fuzz/fuzz_targets/fast-forward.rs +++ b/crates/chia-consensus/fuzz/fuzz_targets/fast-forward.rs @@ -44,7 +44,7 @@ fuzz_target!(|data: &[u8]| { }; let new_coin = Coin { - parent_coin_info: new_parent_coin.coin_id().into(), + parent_coin_info: new_parent_coin.coin_id(), puzzle_hash, amount: if new_amount == 0 { spend.coin.amount diff --git a/crates/chia-consensus/fuzz/fuzz_targets/parse-conditions.rs b/crates/chia-consensus/fuzz/fuzz_targets/parse-conditions.rs index 678e4e3bf..2e778efc0 100644 --- a/crates/chia-consensus/fuzz/fuzz_targets/parse-conditions.rs +++ b/crates/chia-consensus/fuzz/fuzz_targets/parse-conditions.rs @@ -34,8 +34,7 @@ fuzz_target!(|data: &[u8]| { puzzle_hash: puzzle_hash.into(), amount, } - .coin_id() - .into(), + .coin_id(), ); let parent_id = a.new_atom(&parent_id).expect("atom failed"); let puzzle_hash = a.new_atom(&puzzle_hash).expect("atom failed"); diff --git a/crates/chia-consensus/src/fast_forward.rs b/crates/chia-consensus/src/fast_forward.rs index 92a60bdb6..eaa049f18 100644 --- a/crates/chia-consensus/src/fast_forward.rs +++ b/crates/chia-consensus/src/fast_forward.rs @@ -141,7 +141,7 @@ pub fn fast_forward_singleton( amount: new_solution.lineage_proof.parent_amount, }; - if parent_coin.coin_id() != *coin.parent_coin_info { + if parent_coin.coin_id() != coin.parent_coin_info { return Err(Error::ParentCoinMismatch); } @@ -166,7 +166,7 @@ pub fn fast_forward_singleton( let expected_new_parent = new_parent.coin_id(); - if *new_coin.parent_coin_info != expected_new_parent { + if new_coin.parent_coin_info != expected_new_parent { return Err(Error::CoinMismatch); } @@ -224,7 +224,7 @@ mod tests { }; let new_coin = Coin { - parent_coin_info: new_parent_coin.coin_id().into(), + parent_coin_info: new_parent_coin.coin_id(), puzzle_hash, amount: if new_amount == 0 { spend.coin.amount @@ -292,7 +292,7 @@ mod tests { }; let mut new_coin = Coin { - parent_coin_info: new_parent_coin.coin_id().into(), + parent_coin_info: new_parent_coin.coin_id(), puzzle_hash, amount: spend.coin.amount, }; @@ -463,7 +463,7 @@ mod tests { new_coin.puzzle_hash = parent_puzzle_hash; - coin.parent_coin_info = new_parent.coin_id().into(); + coin.parent_coin_info = new_parent.coin_id(); coin.puzzle_hash = parent_puzzle_hash; }, Error::InnerPuzzleHashMismatch, diff --git a/crates/chia-consensus/src/gen/run_puzzle.rs b/crates/chia-consensus/src/gen/run_puzzle.rs index 30316dcaf..70b5e1105 100644 --- a/crates/chia-consensus/src/gen/run_puzzle.rs +++ b/crates/chia-consensus/src/gen/run_puzzle.rs @@ -45,8 +45,7 @@ pub fn run_puzzle( puzzle_hash: puzzle_hash.into(), amount, } - .coin_id() - .into(), + .coin_id(), ); let mut spend = Spend::new( diff --git a/crates/chia-protocol/fuzz/fuzz_targets/spend-bundle.rs b/crates/chia-protocol/fuzz/fuzz_targets/spend-bundle.rs index 302b293f6..4f5d368a7 100644 --- a/crates/chia-protocol/fuzz/fuzz_targets/spend-bundle.rs +++ b/crates/chia-protocol/fuzz/fuzz_targets/spend-bundle.rs @@ -31,7 +31,7 @@ fuzz_target!(|data: &[u8]| { .expect("run"); total_cost += cost; - let parent_coin_info: Bytes32 = cs.coin.coin_id().into(); + let parent_coin_info = cs.coin.coin_id(); while let Some((c, tail)) = a.next(conds) { conds = tail; diff --git a/crates/chia-protocol/src/coin.rs b/crates/chia-protocol/src/coin.rs index 02ed96e3f..d3a4c6ef2 100644 --- a/crates/chia-protocol/src/coin.rs +++ b/crates/chia-protocol/src/coin.rs @@ -17,7 +17,7 @@ pub struct Coin { } impl Coin { - pub fn coin_id(&self) -> [u8; 32] { + pub fn coin_id(&self) -> Bytes32 { let mut hasher = Sha256::new(); hasher.update(self.parent_coin_info); hasher.update(self.puzzle_hash); @@ -41,7 +41,8 @@ impl Coin { hasher.update(&amount_bytes[start..]); } - hasher.finalize().as_slice().try_into().unwrap() + let coin_id: [u8; 32] = hasher.finalize().as_slice().try_into().unwrap(); + Bytes32::new(coin_id) } } @@ -110,7 +111,7 @@ mod tests { sha256.update(parent_coin); sha256.update(puzzle_hash); sha256.update(bytes); - assert_eq!(c.coin_id(), &sha256.finalize() as &[u8]); + assert_eq!(c.coin_id().to_bytes(), &sha256.finalize() as &[u8]); } #[test] diff --git a/crates/chia-protocol/src/spend_bundle.rs b/crates/chia-protocol/src/spend_bundle.rs index 9c63f226a..61bf8c8e2 100644 --- a/crates/chia-protocol/src/spend_bundle.rs +++ b/crates/chia-protocol/src/spend_bundle.rs @@ -58,7 +58,7 @@ impl SpendBundle { return Err(EvalErr(a.nil(), "cost exceeded".to_string())); } cost_left -= cost; - let parent_coin_info: Bytes32 = cs.coin.coin_id().into(); + let parent_coin_info: Bytes32 = cs.coin.coin_id(); while let Some((c, tail)) = a.next(conds) { conds = tail; @@ -216,7 +216,7 @@ ff01\ let additions = bundle.additions().expect("additions"); let new_coin = Coin::new( - test_coin.coin_id().into(), + test_coin.coin_id(), hex::decode("2222222222222222222222222222222222222222222222222222222222222222") .unwrap() .try_into() diff --git a/crates/chia-tools/src/bin/fast-forward-spend.rs b/crates/chia-tools/src/bin/fast-forward-spend.rs index 17d4f26fa..422452888 100644 --- a/crates/chia-tools/src/bin/fast-forward-spend.rs +++ b/crates/chia-tools/src/bin/fast-forward-spend.rs @@ -49,7 +49,7 @@ fn main() { }; let new_coin = Coin { - parent_coin_info: new_parent_coin.coin_id().into(), + parent_coin_info: new_parent_coin.coin_id(), puzzle_hash, amount: spend.coin.amount, }; diff --git a/wheel/src/api.rs b/wheel/src/api.rs index 31734dc08..b9a13683d 100644 --- a/wheel/src/api.rs +++ b/wheel/src/api.rs @@ -283,7 +283,7 @@ fn supports_fast_forward(spend: &CoinSpend) -> bool { amount: spend.coin.amount, }; let new_coin = Coin { - parent_coin_info: new_parent.coin_id().into(), + parent_coin_info: new_parent.coin_id(), puzzle_hash: spend.coin.puzzle_hash, amount: spend.coin.amount, };