From 88f08f5b007d171e1d28ac891d425701d7fd0d69 Mon Sep 17 00:00:00 2001 From: Rigidity Date: Tue, 25 Feb 2025 19:13:47 -0500 Subject: [PATCH] Handle RESERVE_FEE differently with identical spend aggregation --- crates/chia-consensus/src/gen/conditions.rs | 29 +++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/crates/chia-consensus/src/gen/conditions.rs b/crates/chia-consensus/src/gen/conditions.rs index 39213398a..14e7cc10b 100644 --- a/crates/chia-consensus/src/gen/conditions.rs +++ b/crates/chia-consensus/src/gen/conditions.rs @@ -1336,6 +1336,8 @@ pub fn parse_spends( validate_signature(&state, aggregate_signature, flags, bls_cache)?; ret.validated_signature = (flags & DONT_VALIDATE_SIGNATURE) == 0; + check_deduplication(&mut ret); + ret.cost = max_cost - cost_left; Ok(ret) } @@ -1540,6 +1542,33 @@ pub fn validate_signature( Ok(()) } +pub fn check_deduplication(ret: &mut SpendBundleConditions) { + let mut non_dedup_removal_amount = 0; + let mut non_dedup_addition_amount = 0; + + for spend in &ret.spends { + if (spend.flags & ELIGIBLE_FOR_DEDUP) == 0 { + continue; + } + + non_dedup_removal_amount += spend.coin_amount; + non_dedup_addition_amount += spend + .create_coin + .iter() + .fold(0, |acc, coin| acc + coin.amount); + } + + let non_dedup_fees = non_dedup_addition_amount - non_dedup_removal_amount; + + if non_dedup_fees >= ret.reserve_fee { + return; + } + + ret.spends.iter_mut().for_each(|spend| { + spend.flags &= !ELIGIBLE_FOR_DEDUP; + }); +} + #[cfg(test)] use crate::consensus_constants::TEST_CONSTANTS; #[cfg(test)]