Skip to content

Commit

Permalink
include byte cost in return value from get_conditions_from_spendbundle()
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Aug 27, 2024
1 parent 9ec89f0 commit 274884c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
11 changes: 8 additions & 3 deletions crates/chia-consensus/src/spendbundle_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ pub fn get_conditions_from_spendbundle(
let mut state = ParseState::default();

for coin_spend in &spend_bundle.coin_spends {
let byte_cost = (coin_spend.puzzle_reveal.len() + coin_spend.solution.len()) as u64
* constants.cost_per_byte;

subtract_cost(a, &mut cost_left, byte_cost)?;

// process the spend
let puz = node_from_bytes(a, coin_spend.puzzle_reveal.as_slice())?;
let sol = node_from_bytes(a, coin_spend.solution.as_slice())?;
Expand Down Expand Up @@ -77,8 +82,8 @@ mod tests {
use std::fs::read;

#[rstest]
#[case("3000253", 8, 2, 13_344_870)]
#[case("1000101", 34, 15, 66_723_677)]
#[case("3000253", 8, 2, 46_860_870)]
#[case("1000101", 34, 15, 231_687_677)]
fn test_get_conditions_from_spendbundle(
#[case] filename: &str,
#[case] spends: usize,
Expand Down Expand Up @@ -112,7 +117,7 @@ mod tests {
#[case] filename: &str,
#[values(0, 1, 1_000_000, 5_000_000)] height: u32,
) {
let cost = 2_125_866;
let cost = 76_825_866;
let spend = CoinSpend::from_bytes(
&read(format!("../../ff-tests/{filename}.spend")).expect("read file"),
)
Expand Down
19 changes: 6 additions & 13 deletions crates/chia-consensus/src/spendbundle_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,19 +228,12 @@ ff01\
coin_spends,
aggregated_signature: G2Element::default(),
};
let result = validate_clvm_and_signature(
&spend_bundle,
TEST_CONSTANTS.max_block_cost_clvm / 2, // same as mempool_manager default
&TEST_CONSTANTS,
236,
);
assert!(matches!(result, Ok(..)));
let result = validate_clvm_and_signature(
&spend_bundle,
TEST_CONSTANTS.max_block_cost_clvm / 3, // lower than mempool_manager default
&TEST_CONSTANTS,
236,
);
let result = validate_clvm_and_signature(&spend_bundle, 5526552044, &TEST_CONSTANTS, 236);
let Ok((conds, _, _)) = result else {
panic!("failed");
};
assert_eq!(conds.cost, 5526552044);
let result = validate_clvm_and_signature(&spend_bundle, 5526552043, &TEST_CONSTANTS, 236);
assert!(matches!(result, Err(ErrorCode::CostExceeded)));
}

Expand Down

0 comments on commit 274884c

Please sign in to comment.