Skip to content

Commit

Permalink
Merge pull request #289 from Chia-Network/eligible-for-ff
Browse files Browse the repository at this point in the history
introduce ELIGIBLE_FOR_FF flag on Spends via policy class
  • Loading branch information
arvidn authored Oct 26, 2023
2 parents 978746c + 7360551 commit 7aaa598
Show file tree
Hide file tree
Showing 18 changed files with 443 additions and 114 deletions.
10 changes: 7 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;
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,8 +183,12 @@ 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(&a, generator_output, ti.cost - clvm_cost, MEMPOOL_MODE)
{
let conds = match parse_spends::<MempoolVisitor>(
&a,
generator_output,
ti.cost - clvm_cost,
MEMPOOL_MODE,
) {
Err(ValidationErr(_, _)) => {
panic!("failed to parse conditions in block {height}");
}
Expand Down
19 changes: 11 additions & 8 deletions chia-tools/src/bin/test-block-generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use chia_traits::Streamable;

use sqlite::State;

use chia::gen::conditions::NewCoin;
use chia::gen::conditions::Spend;
use chia::gen::conditions::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 @@ -159,9 +157,9 @@ fn main() {
};

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

while let Ok(State::Row) = statement.next() {
Expand Down Expand Up @@ -257,9 +255,14 @@ fn main() {
}

if args.validate {
let mut baseline =
run_block_generator(&mut a, prg.as_ref(), &block_refs, ti.cost, 0)
.expect("failed to run block generator");
let mut baseline = run_block_generator::<_, EmptyVisitor>(
&mut a,
prg.as_ref(),
&block_refs,
ti.cost,
0,
)
.expect("failed to run block generator");
assert_eq!(baseline.cost, ti.cost);

baseline.spends.sort_by_key(|s| *s.coin_id);
Expand Down
5 changes: 3 additions & 2 deletions fuzz/fuzz_targets/fast-forward.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![no_main]
use chia::fast_forward::fast_forward_singleton;
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 @@ -56,7 +57,7 @@ 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(),
Expand All @@ -67,7 +68,7 @@ fuzz_target!(|data: &[u8]| {
);

// run new spend
let conditions2 = run_puzzle(
let conditions2 = run_puzzle::<MempoolVisitor>(
&mut a,
spend.puzzle_reveal.as_slice(),
new_solution.as_slice(),
Expand Down
9 changes: 7 additions & 2 deletions fuzz/fuzz_targets/parse-conditions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#![no_main]
use libfuzzer_sys::fuzz_target;

use chia::gen::conditions::{parse_conditions, ParseState, Spend, SpendBundleConditions};
use chia::gen::conditions::{
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 @@ -42,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 @@ -63,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 @@ -72,6 +76,7 @@ fuzz_target!(|data: &[u8]| {
input,
*flags,
&mut max_cost,
&mut visitor,
);
}
});
4 changes: 2 additions & 2 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;
use chia::gen::conditions::{parse_spends, MempoolVisitor};
use clvmr::allocator::Allocator;
use fuzzing_utils::{make_tree, BitCursor};

Expand All @@ -11,6 +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);
let _ret = parse_spends::<MempoolVisitor>(&a, input, 33000000000, *flags);
}
});
6 changes: 4 additions & 2 deletions fuzz/fuzz_targets/process-spend.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![no_main]
use chia::gen::conditions::{process_single_spend, ParseState, SpendBundleConditions};
use chia::gen::conditions::{
process_single_spend, MempoolVisitor, ParseState, SpendBundleConditions,
};
use chia::gen::flags::{COND_ARGS_NIL, NO_UNKNOWN_CONDS, STRICT_ARGS_COUNT};
use clvmr::allocator::Allocator;
use fuzzing_utils::{make_tree, BitCursor};
Expand All @@ -18,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 Down
12 changes: 10 additions & 2 deletions fuzz/fuzz_targets/run-generator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![no_main]
use chia::allocator::make_allocator;
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 @@ -8,11 +9,18 @@ use libfuzzer_sys::fuzz_target;

fuzz_target!(|data: &[u8]| {
let mut a1 = make_allocator(LIMIT_HEAP | LIMIT_OBJECTS);
let r1 = run_block_generator::<&[u8]>(&mut a1, data, &[], 110000000, ALLOW_BACKREFS);
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]>(&mut a2, data, &[], 110000000, ALLOW_BACKREFS);
let r2 = run_block_generator2::<&[u8], MempoolVisitor>(
&mut a2,
data,
&[],
110000000,
ALLOW_BACKREFS,
);
drop(a2);

match (r1, r2) {
Expand Down
3 changes: 2 additions & 1 deletion fuzz/fuzz_targets/run-puzzle.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![no_main]
use chia::gen::conditions::MempoolVisitor;
use chia::gen::flags::ALLOW_BACKREFS;
use chia::gen::run_puzzle::run_puzzle;
use chia_protocol::CoinSpend;
Expand All @@ -13,7 +14,7 @@ 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(),
Expand Down
5 changes: 3 additions & 2 deletions src/fast_forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ pub fn fast_forward_singleton(
#[cfg(test)]
mod tests {
use super::*;
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 @@ -241,7 +242,7 @@ 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(),
Expand All @@ -253,7 +254,7 @@ mod tests {
.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(),
Expand Down
Loading

0 comments on commit 7aaa598

Please sign in to comment.