Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include byte cost in return value from get_conditions_from_spendbundle() #679

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading