Skip to content

Commit

Permalink
Merge pull request #679 from Chia-Network/get_conditions_from_spendbu…
Browse files Browse the repository at this point in the history
…ndle

include byte cost in return value from `get_conditions_from_spendbundle()`
  • Loading branch information
arvidn authored Aug 27, 2024
2 parents 9ec89f0 + 29ea8d7 commit e032f9d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
27 changes: 21 additions & 6 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 Expand Up @@ -297,9 +302,19 @@ mod tests {
Ok(mut conditions) => {
// the cost of running the spend bundle should never be higher
// than the whole block but it's likely less.
println!("block_cost: {block_cost}");
println!("bundle_cost: {}", conditions.cost);
assert!(conditions.cost <= block_cost);
// but only if the byte cost is not taken into account. The
// block will likely be smaller because the compression makes it
// smaller.
let block_byte_cost = generator_buffer.len() as u64 * TEST_CONSTANTS.cost_per_byte;
let bundle_byte_cost = bundle
.coin_spends
.iter()
.map(|s| s.puzzle_reveal.len() + s.solution.len())
.sum::<usize>() as u64
* TEST_CONSTANTS.cost_per_byte;
println!("block_cost: {block_cost} bytes: {block_byte_cost}");
println!("bundle_cost: {} bytes: {bundle_byte_cost}", conditions.cost);
assert!(conditions.cost - bundle_byte_cost <= block_cost - block_byte_cost);
assert!(conditions.cost > 0);
// update the cost we print here, just to be compatible with
// the test cases we have. We've already ensured the cost is
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 e032f9d

Please sign in to comment.