-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #138 from Chia-Network/run-puzzle
add new function run_puzzle()
- Loading branch information
Showing
12 changed files
with
322 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,64 @@ | ||
#![no_main] | ||
use libfuzzer_sys::fuzz_target; | ||
|
||
use std::sync::Arc; | ||
use std::collections::HashSet; | ||
use clvmr::allocator::Allocator; | ||
use chia::fuzzing_utils::{BitCursor, make_tree}; | ||
use chia::gen::conditions::parse_spends; | ||
use chia::gen::conditions::{parse_conditions, ParseState, Spend, SpendBundleConditions}; | ||
use clvm_utils::tree_hash::tree_hash; | ||
use chia_protocol::Bytes32; | ||
use chia_protocol::Coin; | ||
|
||
use chia::gen::flags::{COND_ARGS_NIL, STRICT_ARGS_COUNT, ENABLE_ASSERT_BEFORE, NO_UNKNOWN_CONDS}; | ||
|
||
fuzz_target!(|data: &[u8]| { | ||
let mut a = Allocator::new(); | ||
let input = make_tree(&mut a, &mut BitCursor::new(data), false); | ||
|
||
let mut ret = SpendBundleConditions { | ||
spends: Vec::new(), | ||
reserve_fee: 0, | ||
height_absolute: 0, | ||
seconds_absolute: 0, | ||
before_height_absolute: None, | ||
before_seconds_absolute: None, | ||
agg_sig_unsafe: Vec::new(), | ||
cost: 0, | ||
}; | ||
|
||
let amount = 1337_u64; | ||
let parent_id: Bytes32 = b"12345678901234567890123456789012".into(); | ||
let puzzle_hash = tree_hash(&a, input); | ||
let coin_id = Arc::<Bytes32>::new( | ||
Coin { | ||
parent_coin_info: parent_id.into(), | ||
puzzle_hash: puzzle_hash.into(), | ||
amount, | ||
} | ||
.coin_id() | ||
.into(), | ||
); | ||
|
||
let mut state = ParseState::new(); | ||
|
||
for flags in &[0, ENABLE_ASSERT_BEFORE | COND_ARGS_NIL, ENABLE_ASSERT_BEFORE | STRICT_ARGS_COUNT, NO_UNKNOWN_CONDS] { | ||
let _ret = parse_spends(&a, input, 33000000000, *flags); | ||
|
||
let 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"), | ||
coin_id: coin_id.clone(), | ||
height_relative: None, | ||
seconds_relative: 0, | ||
before_height_relative: None, | ||
before_seconds_relative: None, | ||
create_coin: HashSet::new(), | ||
agg_sig_me: Vec::new(), | ||
flags: 0_u32, | ||
}; | ||
let mut max_cost: u64 = 3300000000; | ||
let _ret = parse_conditions(&a, &mut ret, &mut state, coin_spend, input, *flags, &mut max_cost); | ||
} | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#![no_main] | ||
use libfuzzer_sys::fuzz_target; | ||
|
||
use clvmr::allocator::Allocator; | ||
use chia::fuzzing_utils::{BitCursor, make_tree}; | ||
use chia::gen::conditions::parse_spends; | ||
|
||
use chia::gen::flags::{COND_ARGS_NIL, STRICT_ARGS_COUNT, ENABLE_ASSERT_BEFORE, NO_UNKNOWN_CONDS}; | ||
|
||
fuzz_target!(|data: &[u8]| { | ||
let mut a = Allocator::new(); | ||
let input = make_tree(&mut a, &mut BitCursor::new(data), false); | ||
for flags in &[0, ENABLE_ASSERT_BEFORE | COND_ARGS_NIL, ENABLE_ASSERT_BEFORE | STRICT_ARGS_COUNT, NO_UNKNOWN_CONDS] { | ||
let _ret = parse_spends(&a, input, 33000000000, *flags); | ||
} | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use crate::gen::conditions::{ | ||
parse_conditions, ParseState, Spend, SpendBundleConditions, ELIGIBLE_FOR_DEDUP, | ||
}; | ||
use crate::gen::validation_error::ValidationErr; | ||
use chia_protocol::bytes::Bytes32; | ||
use chia_protocol::coin::Coin; | ||
use clvm_utils::tree_hash::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; | ||
use std::collections::HashSet; | ||
use std::sync::Arc; | ||
|
||
pub fn run_puzzle( | ||
a: &mut Allocator, | ||
puzzle: &[u8], | ||
solution: &[u8], | ||
parent_id: &[u8], | ||
amount: u64, | ||
max_cost: u64, | ||
flags: u32, | ||
) -> Result<SpendBundleConditions, ValidationErr> { | ||
let puzzle = node_from_bytes(a, puzzle)?; | ||
let solution = node_from_bytes(a, solution)?; | ||
|
||
let dialect = ChiaDialect::new(flags); | ||
let Reduction(clvm_cost, conditions) = run_program(a, &dialect, puzzle, solution, max_cost)?; | ||
|
||
let mut ret = SpendBundleConditions { | ||
spends: Vec::new(), | ||
reserve_fee: 0, | ||
height_absolute: 0, | ||
seconds_absolute: 0, | ||
before_height_absolute: None, | ||
before_seconds_absolute: None, | ||
agg_sig_unsafe: Vec::new(), | ||
cost: 0, | ||
}; | ||
let mut state = ParseState::new(); | ||
|
||
let puzzle_hash = tree_hash(a, puzzle); | ||
let coin_id = Arc::<Bytes32>::new( | ||
Coin { | ||
parent_coin_info: parent_id.into(), | ||
puzzle_hash: puzzle_hash.into(), | ||
amount, | ||
} | ||
.coin_id() | ||
.into(), | ||
); | ||
|
||
let spend = Spend { | ||
parent_id: a.new_atom(parent_id)?, | ||
coin_amount: amount, | ||
puzzle_hash: a.new_atom(&puzzle_hash)?, | ||
coin_id, | ||
height_relative: None, | ||
seconds_relative: 0, | ||
before_height_relative: None, | ||
before_seconds_relative: None, | ||
create_coin: HashSet::new(), | ||
agg_sig_me: Vec::new(), | ||
// assume it's eligible until we see an agg-sig condition | ||
flags: ELIGIBLE_FOR_DEDUP, | ||
}; | ||
|
||
let mut cost_left = max_cost - clvm_cost; | ||
|
||
parse_conditions( | ||
a, | ||
&mut ret, | ||
&mut state, | ||
spend, | ||
conditions, | ||
flags, | ||
&mut cost_left, | ||
)?; | ||
ret.cost = max_cost - cost_left; | ||
Ok(ret) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.