Skip to content

Commit

Permalink
rename Policy -> Visitor. construct one per spend
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Oct 25, 2023
1 parent 135f232 commit 2f474da
Show file tree
Hide file tree
Showing 16 changed files with 81 additions and 113 deletions.
5 changes: 2 additions & 3 deletions chia-tools/src/bin/analyze-chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::time::SystemTime;

use sqlite::State;

use chia::gen::conditions::{parse_spends, MempoolPolicy};
use chia::gen::conditions::{parse_spends, MempoolVisitor};
use chia::gen::flags::MEMPOOL_MODE;
use chia::gen::validation_error::ValidationErr;
use chia::generator_rom::{COST_PER_BYTE, GENERATOR_ROM};
Expand Down Expand Up @@ -183,12 +183,11 @@ fn main() {
let start_conditions = SystemTime::now();
// we pass in what's left of max_cost here, to fail early in case the
// cost of a condition brings us over the cost limit
let conds = match parse_spends(
let conds = match parse_spends::<MempoolVisitor>(
&a,
generator_output,
ti.cost - clvm_cost,
MEMPOOL_MODE,
&mut MempoolPolicy::default(),
) {
Err(ValidationErr(_, _)) => {
panic!("failed to parse conditions in block {height}");
Expand Down
20 changes: 6 additions & 14 deletions chia-tools/src/bin/test-block-generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use chia_traits::Streamable;

use sqlite::State;

use chia::gen::conditions::{NewCoin, NonePolicy, Spend, SpendBundleConditions};
use chia::gen::conditions::{EmptyVisitor, NewCoin, Spend, SpendBundleConditions};
use chia::gen::flags::{ALLOW_BACKREFS, MEMPOOL_MODE};
use chia::gen::run_block_generator::{run_block_generator, run_block_generator2};
use clvmr::allocator::NodePtr;
Expand Down Expand Up @@ -157,9 +157,9 @@ fn main() {
};

let block_runner = if args.rust_generator {
run_block_generator2::<_, NonePolicy>
run_block_generator2::<_, EmptyVisitor>
} else {
run_block_generator::<_, NonePolicy>
run_block_generator::<_, EmptyVisitor>
};

while let Ok(State::Row) = statement.next() {
Expand Down Expand Up @@ -239,15 +239,8 @@ fn main() {
prg.as_ref()
};

let mut conditions = block_runner(
&mut a,
generator,
&block_refs,
ti.cost,
flags,
&mut NonePolicy::default(),
)
.expect("failed to run block generator");
let mut conditions = block_runner(&mut a, generator, &block_refs, ti.cost, flags)
.expect("failed to run block generator");

if args.rust_generator || args.test_backrefs {
assert!(conditions.cost <= ti.cost);
Expand All @@ -262,13 +255,12 @@ fn main() {
}

if args.validate {
let mut baseline = run_block_generator(
let mut baseline = run_block_generator::<_, EmptyVisitor>(
&mut a,
prg.as_ref(),
&block_refs,
ti.cost,
0,
&mut NonePolicy::default(),
)
.expect("failed to run block generator");
assert_eq!(baseline.cost, ti.cost);
Expand Down
8 changes: 3 additions & 5 deletions fuzz/fuzz_targets/fast-forward.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![no_main]
use chia::fast_forward::fast_forward_singleton;
use chia::gen::conditions::MempoolPolicy;
use chia::gen::conditions::MempoolVisitor;
use chia::gen::run_puzzle::run_puzzle;
use chia::gen::validation_error::ValidationErr;
use chia_protocol::Bytes32;
Expand Down Expand Up @@ -57,27 +57,25 @@ fuzz_target!(|data: &[u8]| {
let new_solution = node_to_bytes(&a, new_solution).expect("serialize new solution");

// run original spend
let conditions1 = run_puzzle(
let conditions1 = run_puzzle::<MempoolVisitor>(
&mut a,
spend.puzzle_reveal.as_slice(),
spend.solution.as_slice(),
&spend.coin.parent_coin_info,
spend.coin.amount,
11000000000,
0,
&mut MempoolPolicy::default(),
);

// run new spend
let conditions2 = run_puzzle(
let conditions2 = run_puzzle::<MempoolVisitor>(
&mut a,
spend.puzzle_reveal.as_slice(),
new_solution.as_slice(),
&new_coin.parent_coin_info,
new_coin.amount,
11000000000,
0,
&mut MempoolPolicy::default(),
);

match (conditions1, conditions2) {
Expand Down
8 changes: 5 additions & 3 deletions fuzz/fuzz_targets/parse-conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
use libfuzzer_sys::fuzz_target;

use chia::gen::conditions::{
parse_conditions, MempoolPolicy, ParseState, Spend, SpendBundleConditions,
parse_conditions, MempoolVisitor, ParseState, Spend, SpendBundleConditions,
};
use chia::gen::spend_visitor::SpendVisitor;
use chia_protocol::Bytes32;
use chia_protocol::Coin;
use clvm_utils::tree_hash;
Expand Down Expand Up @@ -44,7 +45,7 @@ fuzz_target!(|data: &[u8]| {
NO_UNKNOWN_CONDS,
ENABLE_SOFTFORK_CONDITION,
] {
let coin_spend = Spend {
let mut coin_spend = Spend {
parent_id: a.new_atom(&parent_id).expect("atom failed"),
coin_amount: amount,
puzzle_hash: a.new_atom(&puzzle_hash).expect("atom failed"),
Expand All @@ -65,6 +66,7 @@ fuzz_target!(|data: &[u8]| {
agg_sig_parent_puzzle: Vec::new(),
flags: 0_u32,
};
let mut visitor = MempoolVisitor::new_spend(&mut coin_spend);
let mut max_cost: u64 = 3300000000;
let _ret = parse_conditions(
&a,
Expand All @@ -74,7 +76,7 @@ fuzz_target!(|data: &[u8]| {
input,
*flags,
&mut max_cost,
&mut MempoolPolicy::default(),
&mut visitor,
);
}
});
10 changes: 2 additions & 8 deletions fuzz/fuzz_targets/parse-spends.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![no_main]
use libfuzzer_sys::fuzz_target;

use chia::gen::conditions::{parse_spends, MempoolPolicy};
use chia::gen::conditions::{parse_spends, MempoolVisitor};
use clvmr::allocator::Allocator;
use fuzzing_utils::{make_tree, BitCursor};

Expand All @@ -11,12 +11,6 @@ fuzz_target!(|data: &[u8]| {
let mut a = Allocator::new();
let input = make_tree(&mut a, &mut BitCursor::new(data), false);
for flags in &[0, COND_ARGS_NIL, STRICT_ARGS_COUNT, NO_UNKNOWN_CONDS] {
let _ret = parse_spends(
&a,
input,
33000000000,
*flags,
&mut MempoolPolicy::default(),
);
let _ret = parse_spends::<MempoolVisitor>(&a, input, 33000000000, *flags);
}
});
5 changes: 2 additions & 3 deletions fuzz/fuzz_targets/process-spend.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![no_main]
use chia::gen::conditions::{
process_single_spend, MempoolPolicy, ParseState, SpendBundleConditions,
process_single_spend, MempoolVisitor, ParseState, SpendBundleConditions,
};
use chia::gen::flags::{COND_ARGS_NIL, NO_UNKNOWN_CONDS, STRICT_ARGS_COUNT};
use clvmr::allocator::Allocator;
Expand All @@ -20,7 +20,7 @@ fuzz_target!(|data: &[u8]| {

for flags in &[0, COND_ARGS_NIL, STRICT_ARGS_COUNT, NO_UNKNOWN_CONDS] {
let mut cost_left = 11000000;
let _ = process_single_spend(
let _ = process_single_spend::<MempoolVisitor>(
&a,
&mut ret,
&mut state,
Expand All @@ -30,7 +30,6 @@ fuzz_target!(|data: &[u8]| {
conds,
*flags,
&mut cost_left,
&mut MempoolPolicy::default(),
);
}
});
15 changes: 4 additions & 11 deletions fuzz/fuzz_targets/run-generator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![no_main]
use chia::allocator::make_allocator;
use chia::gen::conditions::MempoolPolicy;
use chia::gen::conditions::MempoolVisitor;
use chia::gen::flags::{ALLOW_BACKREFS, LIMIT_OBJECTS};
use chia::gen::run_block_generator::{run_block_generator, run_block_generator2};
use chia::gen::validation_error::{ErrorCode, ValidationErr};
Expand All @@ -9,24 +9,17 @@ use libfuzzer_sys::fuzz_target;

fuzz_target!(|data: &[u8]| {
let mut a1 = make_allocator(LIMIT_HEAP | LIMIT_OBJECTS);
let r1 = run_block_generator::<&[u8], MempoolPolicy>(
&mut a1,
data,
&[],
110000000,
ALLOW_BACKREFS,
&mut MempoolPolicy::default(),
);
let r1 =
run_block_generator::<&[u8], MempoolVisitor>(&mut a1, data, &[], 110000000, ALLOW_BACKREFS);
drop(a1);

let mut a2 = make_allocator(LIMIT_HEAP | LIMIT_OBJECTS);
let r2 = run_block_generator2::<&[u8], MempoolPolicy>(
let r2 = run_block_generator2::<&[u8], MempoolVisitor>(
&mut a2,
data,
&[],
110000000,
ALLOW_BACKREFS,
&mut MempoolPolicy::default(),
);
drop(a2);

Expand Down
5 changes: 2 additions & 3 deletions fuzz/fuzz_targets/run-puzzle.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![no_main]
use chia::gen::conditions::MempoolPolicy;
use chia::gen::conditions::MempoolVisitor;
use chia::gen::flags::ALLOW_BACKREFS;
use chia::gen::run_puzzle::run_puzzle;
use chia_protocol::CoinSpend;
Expand All @@ -14,14 +14,13 @@ fuzz_target!(|data: &[u8]| {
let Ok(spend) = CoinSpend::parse(&mut Cursor::new(data)) else {
return;
};
let _ = run_puzzle(
let _ = run_puzzle::<MempoolVisitor>(
&mut a,
spend.puzzle_reveal.as_slice(),
spend.solution.as_slice(),
(&spend.coin.parent_coin_info).into(),
spend.coin.amount,
11000000000,
ALLOW_BACKREFS,
&mut MempoolPolicy::default(),
);
});
8 changes: 3 additions & 5 deletions src/fast_forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ pub fn fast_forward_singleton(
#[cfg(test)]
mod tests {
use super::*;
use crate::gen::conditions::MempoolPolicy;
use crate::gen::conditions::MempoolVisitor;
use crate::gen::run_puzzle::run_puzzle;
use chia_protocol::CoinSpend;
use chia_traits::streamable::Streamable;
Expand Down Expand Up @@ -242,28 +242,26 @@ mod tests {
let new_solution = node_to_bytes(&a, new_solution).expect("serialize new solution");

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

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

Expand Down
Loading

0 comments on commit 2f474da

Please sign in to comment.