Skip to content

Commit

Permalink
introduce ELIGIBLE_FOR_FF flag on Spends when parsing and validating …
Browse files Browse the repository at this point in the history
…conditions. This indicates whether some of the requirements are met to be able to fast-forward a singleton spend
  • Loading branch information
arvidn committed Oct 24, 2023
1 parent dab6383 commit 135f232
Show file tree
Hide file tree
Showing 18 changed files with 464 additions and 83 deletions.
11 changes: 8 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, MempoolPolicy};
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,13 @@ 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(
&a,
generator_output,
ti.cost - clvm_cost,
MEMPOOL_MODE,
&mut MempoolPolicy::default(),
) {
Err(ValidationErr(_, _)) => {
panic!("failed to parse conditions in block {height}");
}
Expand Down
31 changes: 21 additions & 10 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::{NewCoin, NonePolicy, 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::<_, NonePolicy>
} else {
run_block_generator
run_block_generator::<_, NonePolicy>
};

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

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

if args.rust_generator || args.test_backrefs {
assert!(conditions.cost <= ti.cost);
Expand All @@ -257,9 +262,15 @@ 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(
&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);

baseline.spends.sort_by_key(|s| *s.coin_id);
Expand Down
3 changes: 3 additions & 0 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::MempoolPolicy;
use chia::gen::run_puzzle::run_puzzle;
use chia::gen::validation_error::ValidationErr;
use chia_protocol::Bytes32;
Expand Down Expand Up @@ -64,6 +65,7 @@ fuzz_target!(|data: &[u8]| {
spend.coin.amount,
11000000000,
0,
&mut MempoolPolicy::default(),
);

// run new spend
Expand All @@ -75,6 +77,7 @@ fuzz_target!(|data: &[u8]| {
new_coin.amount,
11000000000,
0,
&mut MempoolPolicy::default(),
);

match (conditions1, conditions2) {
Expand Down
5 changes: 4 additions & 1 deletion fuzz/fuzz_targets/parse-conditions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#![no_main]
use libfuzzer_sys::fuzz_target;

use chia::gen::conditions::{parse_conditions, ParseState, Spend, SpendBundleConditions};
use chia::gen::conditions::{
parse_conditions, MempoolPolicy, ParseState, Spend, SpendBundleConditions,
};
use chia_protocol::Bytes32;
use chia_protocol::Coin;
use clvm_utils::tree_hash;
Expand Down Expand Up @@ -72,6 +74,7 @@ fuzz_target!(|data: &[u8]| {
input,
*flags,
&mut max_cost,
&mut MempoolPolicy::default(),
);
}
});
10 changes: 8 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, MempoolPolicy};
use clvmr::allocator::Allocator;
use fuzzing_utils::{make_tree, BitCursor};

Expand All @@ -11,6 +11,12 @@ 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(
&a,
input,
33000000000,
*flags,
&mut MempoolPolicy::default(),
);
}
});
5 changes: 4 additions & 1 deletion 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, MempoolPolicy, 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 Down Expand Up @@ -28,6 +30,7 @@ fuzz_target!(|data: &[u8]| {
conds,
*flags,
&mut cost_left,
&mut MempoolPolicy::default(),
);
}
});
19 changes: 17 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::MempoolPolicy;
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,25 @@ 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], MempoolPolicy>(
&mut a1,
data,
&[],
110000000,
ALLOW_BACKREFS,
&mut MempoolPolicy::default(),
);
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], MempoolPolicy>(
&mut a2,
data,
&[],
110000000,
ALLOW_BACKREFS,
&mut MempoolPolicy::default(),
);
drop(a2);

match (r1, r2) {
Expand Down
2 changes: 2 additions & 0 deletions 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::MempoolPolicy;
use chia::gen::flags::ALLOW_BACKREFS;
use chia::gen::run_puzzle::run_puzzle;
use chia_protocol::CoinSpend;
Expand All @@ -21,5 +22,6 @@ fuzz_target!(|data: &[u8]| {
spend.coin.amount,
11000000000,
ALLOW_BACKREFS,
&mut MempoolPolicy::default(),
);
});
3 changes: 3 additions & 0 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::MempoolPolicy;
use crate::gen::run_puzzle::run_puzzle;
use chia_protocol::CoinSpend;
use chia_traits::streamable::Streamable;
Expand Down Expand Up @@ -249,6 +250,7 @@ mod tests {
spend.coin.amount,
11000000000,
0,
&mut MempoolPolicy::default(),
)
.expect("run_puzzle");

Expand All @@ -261,6 +263,7 @@ mod tests {
new_coin.amount,
11000000000,
0,
&mut MempoolPolicy::default(),
)
.expect("run_puzzle");

Expand Down
Loading

0 comments on commit 135f232

Please sign in to comment.