Skip to content

Commit

Permalink
Merge pull request #739 from Chia-Network/remove-run-puzzle
Browse files Browse the repository at this point in the history
remove unused `run_puzzle()` function
  • Loading branch information
arvidn authored Oct 1, 2024
2 parents f23eaeb + 5935eab commit 3981915
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 308 deletions.
7 changes: 0 additions & 7 deletions crates/chia-consensus/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,6 @@ test = false
doc = false
bench = false

[[bin]]
name = "run-puzzle"
path = "fuzz_targets/run-puzzle.rs"
test = false
doc = false
bench = false

[[bin]]
name = "fast-forward"
path = "fuzz_targets/fast-forward.rs"
Expand Down
78 changes: 67 additions & 11 deletions crates/chia-consensus/fuzz/fuzz_targets/fast-forward.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
#![no_main]
use chia_consensus::consensus_constants::TEST_CONSTANTS;
use chia_consensus::fast_forward::fast_forward_singleton;
use chia_consensus::gen::conditions::{MempoolVisitor, ELIGIBLE_FOR_FF};
use chia_consensus::gen::run_puzzle::run_puzzle;
use chia_consensus::gen::conditions::{
parse_conditions, MempoolVisitor, ParseState, SpendBundleConditions, SpendConditions,
ELIGIBLE_FOR_FF,
};
use chia_consensus::gen::spend_visitor::SpendVisitor;
use chia_consensus::gen::validation_error::{ErrorCode, ValidationErr};
use chia_protocol::Bytes32;
use chia_protocol::Coin;
use chia_protocol::CoinSpend;
use chia_traits::streamable::Streamable;
use clvm_traits::ToClvm;
use clvm_utils::tree_hash;
use clvmr::serde::node_to_bytes;
use clvmr::serde::{node_from_bytes, node_to_bytes};
use clvmr::{Allocator, NodePtr};
use hex_literal::hex;
use libfuzzer_sys::fuzz_target;
use std::io::Cursor;

use clvmr::chia_dialect::ChiaDialect;
use clvmr::reduction::Reduction;
use clvmr::run_program::run_program;
use std::sync::Arc;

fuzz_target!(|data: &[u8]| {
let Ok(spend) = CoinSpend::parse::<false>(&mut Cursor::new(data)) else {
return;
Expand Down Expand Up @@ -66,6 +74,60 @@ fuzz_target!(|data: &[u8]| {
}
});

fn run_puzzle(
a: &mut Allocator,
puzzle: &[u8],
solution: &[u8],
parent_id: &[u8],
amount: u64,
) -> core::result::Result<SpendBundleConditions, ValidationErr> {
let puzzle = node_from_bytes(a, puzzle)?;
let solution = node_from_bytes(a, solution)?;

let dialect = ChiaDialect::new(0);
let max_cost = 11_000_000_000;
let Reduction(clvm_cost, conditions) = run_program(a, &dialect, puzzle, solution, max_cost)?;

let mut ret = SpendBundleConditions {
removal_amount: amount as u128,
..Default::default()
};
let mut state = ParseState::default();

let puzzle_hash = tree_hash(a, puzzle);
let coin_id = Arc::<Bytes32>::new(
Coin {
parent_coin_info: parent_id.try_into().unwrap(),
puzzle_hash: puzzle_hash.into(),
amount,
}
.coin_id(),
);

let mut spend = SpendConditions::new(
a.new_atom(parent_id)?,
amount,
a.new_atom(&puzzle_hash)?,
coin_id,
);

let mut visitor = MempoolVisitor::new_spend(&mut spend);

let mut cost_left = max_cost - clvm_cost;
parse_conditions(
a,
&mut ret,
&mut state,
spend,
conditions,
0,
&mut cost_left,
&TEST_CONSTANTS,
&mut visitor,
)?;
ret.cost = max_cost - cost_left;
Ok(ret)
}
fn test_ff(
a: &mut Allocator,
spend: &CoinSpend,
Expand All @@ -83,27 +145,21 @@ fn test_ff(
let new_solution = node_to_bytes(a, new_solution).expect("serialize new solution");

// run original spend
let conditions1 = run_puzzle::<MempoolVisitor>(
let conditions1 = run_puzzle(
a,
spend.puzzle_reveal.as_slice(),
spend.solution.as_slice(),
&spend.coin.parent_coin_info,
spend.coin.amount,
11_000_000_000,
0,
&TEST_CONSTANTS,
);

// run new spend
let conditions2 = run_puzzle::<MempoolVisitor>(
let conditions2 = run_puzzle(
a,
spend.puzzle_reveal.as_slice(),
new_solution.as_slice(),
&new_coin.parent_coin_info,
new_coin.amount,
11_000_000_000,
0,
&TEST_CONSTANTS,
);

// These are the kinds of failures that can happen because of the
Expand Down
28 changes: 0 additions & 28 deletions crates/chia-consensus/fuzz/fuzz_targets/run-puzzle.rs

This file was deleted.

83 changes: 73 additions & 10 deletions crates/chia-consensus/src/fast_forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,85 @@ pub fn fast_forward_singleton(
#[cfg(test)]
mod tests {
use super::*;
use crate::consensus_constants::ConsensusConstants;
use crate::consensus_constants::TEST_CONSTANTS;
use crate::gen::conditions::MempoolVisitor;
use crate::gen::run_puzzle::run_puzzle;
use crate::gen::conditions::{
parse_conditions, ParseState, SpendBundleConditions, SpendConditions,
};
use crate::gen::spend_visitor::SpendVisitor;
use crate::gen::validation_error::ValidationErr;
use chia_protocol::Bytes32;
use chia_protocol::Coin;
use chia_protocol::CoinSpend;
use chia_traits::streamable::Streamable;
use clvm_traits::ToClvm;
use clvmr::serde::{node_from_bytes, node_to_bytes};
use clvm_utils::tree_hash;
use clvmr::allocator::Allocator;
use clvmr::chia_dialect::ChiaDialect;
use clvmr::reduction::Reduction;
use clvmr::run_program::run_program;
use clvmr::serde::{node_from_bytes, node_from_bytes_backrefs, node_to_bytes};
use hex_literal::hex;
use rstest::rstest;
use std::fs;
use std::sync::Arc;

pub fn run_puzzle(
a: &mut Allocator,
puzzle: &[u8],
solution: &[u8],
parent_id: &[u8],
amount: u64,
) -> core::result::Result<SpendBundleConditions, ValidationErr> {
let puzzle = node_from_bytes(a, puzzle)?;
let solution = node_from_bytes(a, solution)?;

let dialect = ChiaDialect::new(0);
let max_cost = 11_000_000_000;
let Reduction(clvm_cost, conditions) =
run_program(a, &dialect, puzzle, solution, max_cost)?;

let mut ret = SpendBundleConditions {
removal_amount: amount as u128,
..Default::default()
};
let mut state = ParseState::default();

let puzzle_hash = tree_hash(a, puzzle);
let coin_id = Arc::<Bytes32>::new(
Coin {
parent_coin_info: parent_id.try_into().unwrap(),
puzzle_hash: puzzle_hash.into(),
amount,
}
.coin_id(),
);

let mut spend = SpendConditions::new(
a.new_atom(parent_id)?,
amount,
a.new_atom(&puzzle_hash)?,
coin_id,
);

let mut visitor = MempoolVisitor::new_spend(&mut spend);

let mut cost_left = max_cost - clvm_cost;
parse_conditions(
a,
&mut ret,
&mut state,
spend,
conditions,
0,
&mut cost_left,
&TEST_CONSTANTS,
&mut visitor,
)?;
ret.cost = max_cost - cost_left;
Ok(ret)
}

// this test loads CoinSpends from file (Coin, puzzle, solution)-triples
// and "fast-forwards" the spend onto a few different parent-parent coins
Expand Down Expand Up @@ -226,28 +295,22 @@ mod tests {
let new_solution = node_to_bytes(&a, new_solution).expect("serialize new solution");

// run original spend
let conditions1 = run_puzzle::<MempoolVisitor>(
let conditions1 = run_puzzle(
&mut a,
spend.puzzle_reveal.as_slice(),
spend.solution.as_slice(),
&spend.coin.parent_coin_info,
spend.coin.amount,
11_000_000_000,
0,
&TEST_CONSTANTS,
)
.expect("run_puzzle");

// run new spend
let conditions2 = run_puzzle::<MempoolVisitor>(
let conditions2 = run_puzzle(
&mut a,
spend.puzzle_reveal.as_slice(),
new_solution.as_slice(),
&new_coin.parent_coin_info,
new_coin.amount,
11_000_000_000,
0,
&TEST_CONSTANTS,
)
.expect("run_puzzle");

Expand Down
1 change: 0 additions & 1 deletion crates/chia-consensus/src/gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ pub mod messages;
pub mod opcodes;
pub mod owned_conditions;
pub mod run_block_generator;
pub mod run_puzzle;
pub mod sanitize_int;
pub mod solution_generator;
pub mod spend_visitor;
Expand Down
80 changes: 0 additions & 80 deletions crates/chia-consensus/src/gen/run_puzzle.rs

This file was deleted.

Loading

0 comments on commit 3981915

Please sign in to comment.