Skip to content
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
8 changes: 7 additions & 1 deletion crates/chia-consensus/fuzz/fuzz_targets/fast-forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ fn run_puzzle(

let dialect = ChiaDialect::new(ClvmFlags::empty());
let max_cost = 11_000_000_000;
let atoms_before = a.atom_count();
let pairs_before = a.pair_count();
let Reduction(clvm_cost, conditions) = run_program(a, &dialect, puzzle, solution, max_cost)?;
let atom_count = (a.atom_count() - atoms_before) as u64;
let pair_count = (a.pair_count() - pairs_before) as u64;

let mut ret = SpendBundleConditions {
removal_amount: amount as u128,
Expand All @@ -104,7 +108,9 @@ fn run_puzzle(
amount,
a.new_atom(&puzzle_hash)?,
coin_id,
0,
clvm_cost,
atom_count,
pair_count,
);

let mut visitor = MempoolVisitor::new_spend(&mut spend);
Expand Down
2 changes: 2 additions & 0 deletions crates/chia-consensus/fuzz/fuzz_targets/parse-conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ fuzz_target!(|data: &[u8]| {
flags: 0_u32,
execution_cost: 0_u64,
condition_cost: 0_u64,
atom_count: 0_u64,
pair_count: 0_u64,
fingerprint: [0; 32],
};
let mut visitor = MempoolVisitor::new_spend(&mut coin_spend);
Expand Down
2 changes: 2 additions & 0 deletions crates/chia-consensus/fuzz/fuzz_targets/process-spend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ fuzz_target!(|args: (ArbitraryClvmTree, [u8; 32], [u8; 32], u64)| {
*flags,
&mut cost_left,
0, // clvm_cost
0, // atom_count
0, // pair_count
&TEST_CONSTANTS,
);
}
Expand Down
26 changes: 25 additions & 1 deletion crates/chia-consensus/src/conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,12 @@ pub struct SpendConditions {
/// the cost of the conditions generated by this puzzle
pub condition_cost: u64,

/// the number of atoms allocated by running this puzzle with its solution
pub atom_count: u64,

/// the number of pairs allocated by running this puzzle with its solution
pub pair_count: u64,

/// If this spend is eligible for dedup, this fingerprint is set to
/// the sha256 hash of the conditions returned by the puzzle. Only known
/// conditions and their known arguments are considered.
Expand All @@ -840,6 +846,8 @@ impl SpendConditions {
puzzle_hash: NodePtr,
coin_id: Arc<Bytes32>,
clvm_cost: Cost,
atom_count: u64,
pair_count: u64,
) -> SpendConditions {
SpendConditions {
parent_id,
Expand All @@ -863,6 +871,8 @@ impl SpendConditions {
flags: 0,
execution_cost: clvm_cost,
condition_cost: 0,
atom_count,
pair_count,
fingerprint: [0; 32],
}
}
Expand Down Expand Up @@ -1005,6 +1015,8 @@ pub fn process_single_spend<'a, V: SpendVisitor>(
flags: ConsensusFlags,
max_cost: &mut Cost,
clvm_cost: Cost,
atom_count: u64,
pair_count: u64,
constants: &ConsensusConstants,
) -> Result<&'a mut SpendConditions, ValidationErr> {
let parent_id = sanitize_hash(a, parent_id, 32, ErrorCode::InvalidParentId)?;
Expand Down Expand Up @@ -1033,7 +1045,15 @@ pub fn process_single_spend<'a, V: SpendVisitor>(

ret.removal_amount += my_amount as u128;

let mut spend = SpendConditions::new(parent_id, my_amount, puzzle_hash, coin_id, clvm_cost);
let mut spend = SpendConditions::new(
parent_id,
my_amount,
puzzle_hash,
coin_id,
clvm_cost,
atom_count,
pair_count,
);

if flags.contains(ConsensusFlags::COST_CONDITIONS) {
if *max_cost < SPEND_COST {
Expand Down Expand Up @@ -1590,6 +1610,8 @@ pub fn parse_spends<V: SpendVisitor>(
flags,
&mut cost_left,
clvm_cost,
0, // atom_count
0, // pair_count
constants,
)?;
}
Expand Down Expand Up @@ -3796,6 +3818,8 @@ fn final_message(
a.new_atom(puzzle.as_slice()).expect("test should pass"),
Arc::new(Bytes32::try_from(coin.coin_id()).expect("test should pass")),
0, // clvm_cost
0, // atom_count
0, // pair_count
);

let spend = OwnedSpendConditions::from(&a, spend);
Expand Down
6 changes: 6 additions & 0 deletions crates/chia-consensus/src/fast_forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,12 @@ mod tests {

let dialect = ChiaDialect::new(ClvmFlags::empty());
let max_cost = 11_000_000_000;
let atoms_before = a.atom_count();
let pairs_before = a.pair_count();
let Reduction(clvm_cost, conditions) =
run_program(a, &dialect, puzzle, solution, max_cost)?;
let atom_count = (a.atom_count() - atoms_before) as u64;
let pair_count = (a.pair_count() - pairs_before) as u64;

let mut ret = SpendBundleConditions {
removal_amount: amount as u128,
Expand All @@ -221,6 +225,8 @@ mod tests {
a.new_atom(&puzzle_hash)?,
coin_id,
clvm_cost,
atom_count,
pair_count,
);

let mut visitor = MempoolVisitor::new_spend(&mut spend);
Expand Down
2 changes: 2 additions & 0 deletions crates/chia-consensus/src/make_aggsig_final_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ mod tests {
.expect("test should pass"),
Arc::new(Bytes32::try_from(coin.coin_id()).expect("test should pass")),
0,
0,
0,
);

let spend = OwnedSpendConditions::from(&a, spend);
Expand Down
5 changes: 5 additions & 0 deletions crates/chia-consensus/src/owned_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ pub struct OwnedSpendConditions {
/// per-spend execution and condition cost
pub execution_cost: u64,
pub condition_cost: u64,
/// the number of atoms and pairs allocated by running this puzzle
pub atom_count: u64,
pub pair_count: u64,
pub fingerprint: Bytes,
}

Expand Down Expand Up @@ -135,6 +138,8 @@ impl OwnedSpendConditions {
flags: spend.flags,
execution_cost: spend.execution_cost,
condition_cost: spend.condition_cost,
atom_count: spend.atom_count,
pair_count: spend.pair_count,
fingerprint,
}
}
Expand Down
6 changes: 6 additions & 0 deletions crates/chia-consensus/src/run_block_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,15 @@ where
let [parent_id, puzzle, amount, solution, _spend_level_extra] =
extract_n::<5>(&a, spend, ErrorCode::InvalidCondition)?;

let atoms_before = a.atom_count();
let pairs_before = a.pair_count();
let Reduction(clvm_cost, conditions) =
run_program(&mut a, &dialect, puzzle, solution, cost_left)?;

subtract_cost(&mut cost_left, clvm_cost)?;
ret.execution_cost += clvm_cost;
let atom_count = (a.atom_count() - atoms_before) as u64;
let pair_count = (a.pair_count() - pairs_before) as u64;

let buf = tree_hash_cached(&a, puzzle, &mut cache);
let puzzle_hash = a.new_atom(&buf)?;
Expand All @@ -319,6 +323,8 @@ where
flags,
&mut cost_left,
clvm_cost,
atom_count,
pair_count,
constants,
)?;
}
Expand Down
6 changes: 6 additions & 0 deletions crates/chia-consensus/src/spendbundle_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,14 @@ pub fn run_spendbundle(
let sol = node_from_bytes(a, coin_spend.solution.as_slice())?;
let parent = a.new_atom(coin_spend.coin.parent_coin_info.as_slice())?;
let amount = a.new_number(coin_spend.coin.amount.into())?;
let atoms_before = a.atom_count();
let pairs_before = a.pair_count();
let Reduction(clvm_cost, conditions) = run_program(a, &dialect, puz, sol, cost_left)?;

ret.execution_cost += clvm_cost;
subtract_cost(&mut cost_left, clvm_cost)?;
let atom_count = (a.atom_count() - atoms_before) as u64;
let pair_count = (a.pair_count() - pairs_before) as u64;

let buf = tree_hash(a, puz);
if coin_spend.coin.puzzle_hash != buf.into() {
Expand All @@ -126,6 +130,8 @@ pub fn run_spendbundle(
flags,
&mut cost_left,
clvm_cost,
atom_count,
pair_count,
constants,
)?;

Expand Down
6 changes: 5 additions & 1 deletion crates/chia-consensus/src/test_generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ pub(crate) fn print_conditions(a: &Allocator, c: &SpendBundleConditions, a2: &Al
for s in spends {
writeln!(
ret,
"- coin id: {} ph: {} exe-cost: {} cond-cost: {}",
"- coin id: {} ph: {} exe-cost: {} cond-cost: {} atoms: {} pairs: {}",
hex::encode(*s.coin_id),
hex::encode(a.atom(s.puzzle_hash)),
s.execution_cost,
s.condition_cost,
s.atom_count,
s.pair_count,
)
.unwrap();

Expand Down Expand Up @@ -447,6 +449,8 @@ fn run_generator(#[case] name: &str) {
for ms in &mut conditions.spends {
if ms.coin_id == s.coin_id {
ms.execution_cost = s.execution_cost;
ms.atom_count = s.atom_count;
ms.pair_count = s.pair_count;
break;
}
}
Expand Down
Loading
Loading