-
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 #122 from Chia-Network/generator-rom
run_block_generator()
- Loading branch information
Showing
12 changed files
with
196 additions
and
63 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
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,57 @@ | ||
use crate::gen::conditions::{parse_spends, SpendBundleConditions}; | ||
use crate::gen::validation_error::ValidationErr; | ||
use crate::generator_rom::{COST_PER_BYTE, GENERATOR_ROM}; | ||
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; | ||
|
||
// Runs the generator ROM and passes in the program (transactions generator). | ||
// The program is expected to return a list of spends. Each item being: | ||
|
||
// (parent-coin-id puzzle-reveal amount solution) | ||
|
||
// The puzzle-reveals are then executed with the corresponding solution being | ||
// passed as the argument. The output from those puzzles are lists of | ||
// conditions. The conditions are parsed and returned in the | ||
// SpendBundleConditions. Some conditions are validated, and if invalid may | ||
// cause the function to return an error. | ||
|
||
// the only reason we need to pass in the allocator is because the returned | ||
// SpendBundleConditions contains NodePtr fields. If that's changed, we could | ||
// create the allocator inside this functions as well. | ||
pub fn run_block_generator<GenBuf: AsRef<[u8]>>( | ||
a: &mut Allocator, | ||
program: &[u8], | ||
block_refs: &[GenBuf], | ||
max_cost: u64, | ||
flags: u32, | ||
) -> Result<SpendBundleConditions, ValidationErr> { | ||
let byte_cost = program.len() as u64 * COST_PER_BYTE; | ||
|
||
let generator_rom = node_from_bytes(a, &GENERATOR_ROM)?; | ||
let program = node_from_bytes(a, program)?; | ||
|
||
// iterate in reverse order since we're building a linked list from | ||
// the tail | ||
let mut args = a.null(); | ||
for g in block_refs.iter().rev() { | ||
let ref_gen = a.new_atom(g.as_ref())?; | ||
args = a.new_pair(ref_gen, args)?; | ||
} | ||
|
||
args = a.new_pair(args, a.null())?; | ||
let args = a.new_pair(args, a.null())?; | ||
let args = a.new_pair(program, args)?; | ||
|
||
let dialect = ChiaDialect::new(flags); | ||
let Reduction(clvm_cost, generator_output) = | ||
run_program(a, &dialect, generator_rom, args, max_cost - byte_cost)?; | ||
|
||
// 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 mut result = parse_spends(a, generator_output, max_cost - clvm_cost - byte_cost, flags)?; | ||
result.cost += clvm_cost + byte_cost; | ||
Ok(result) | ||
} |
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,55 @@ | ||
// the generator ROM from: | ||
// https://github.com/Chia-Network/chia-blockchain/blob/main/chia/wallet/puzzles/rom_bootstrap_generator.clvm.hex | ||
pub const GENERATOR_ROM: [u8; 737] = [ | ||
0xff, 0x02, 0xff, 0xff, 0x01, 0xff, 0x02, 0xff, 0x0c, 0xff, 0xff, 0x04, 0xff, 0x02, 0xff, 0xff, | ||
0x04, 0xff, 0xff, 0x02, 0xff, 0x05, 0xff, 0xff, 0x04, 0xff, 0x08, 0xff, 0xff, 0x04, 0xff, 0x13, | ||
0xff, 0x80, 0x80, 0x80, 0x80, 0xff, 0x80, 0x80, 0x80, 0x80, 0xff, 0xff, 0x04, 0xff, 0xff, 0x01, | ||
0xff, 0xff, 0xff, 0x02, 0xff, 0xff, 0x01, 0xff, 0x05, 0xff, 0xff, 0x02, 0xff, 0x3e, 0xff, 0xff, | ||
0x04, 0xff, 0x02, 0xff, 0xff, 0x04, 0xff, 0x05, 0xff, 0x80, 0x80, 0x80, 0x80, 0x80, 0xff, 0xff, | ||
0x04, 0xff, 0xff, 0x01, 0xff, 0xff, 0xff, 0x81, 0xff, 0x7f, 0xff, 0x81, 0xdf, 0x81, 0xbf, 0xff, | ||
0xff, 0xff, 0x02, 0xff, 0xff, 0x03, 0xff, 0xff, 0x09, 0xff, 0x0b, 0xff, 0xff, 0x01, 0x81, 0x80, | ||
0x80, 0xff, 0xff, 0x01, 0xff, 0x04, 0xff, 0x80, 0xff, 0xff, 0x04, 0xff, 0x05, 0xff, 0x80, 0x80, | ||
0x80, 0xff, 0xff, 0x01, 0xff, 0x02, 0xff, 0xff, 0x03, 0xff, 0xff, 0x0a, 0xff, 0x0b, 0xff, 0x18, | ||
0x80, 0xff, 0xff, 0x01, 0xff, 0x02, 0xff, 0x1a, 0xff, 0xff, 0x04, 0xff, 0x02, 0xff, 0xff, 0x04, | ||
0xff, 0xff, 0x02, 0xff, 0xff, 0x03, 0xff, 0xff, 0x0a, 0xff, 0x0b, 0xff, 0x1c, 0x80, 0xff, 0xff, | ||
0x01, 0xff, 0x02, 0xff, 0xff, 0x03, 0xff, 0xff, 0x0a, 0xff, 0x0b, 0xff, 0x14, 0x80, 0xff, 0xff, | ||
0x01, 0xff, 0x08, 0x80, 0xff, 0xff, 0x01, 0xff, 0x04, 0xff, 0xff, 0x0e, 0xff, 0xff, 0x18, 0xff, | ||
0xff, 0x01, 0x1f, 0xff, 0x0b, 0x80, 0xff, 0xff, 0x0c, 0xff, 0x05, 0xff, 0x80, 0xff, 0xff, 0x01, | ||
0x01, 0x80, 0x80, 0xff, 0xff, 0x04, 0xff, 0xff, 0x0c, 0xff, 0x05, 0xff, 0xff, 0x01, 0x01, 0x80, | ||
0xff, 0x80, 0x80, 0x80, 0x80, 0xff, 0x01, 0x80, 0xff, 0xff, 0x01, 0xff, 0x04, 0xff, 0xff, 0x18, | ||
0xff, 0xff, 0x01, 0x3f, 0xff, 0x0b, 0x80, 0xff, 0xff, 0x04, 0xff, 0x05, 0xff, 0x80, 0x80, 0x80, | ||
0x80, 0xff, 0x01, 0x80, 0xff, 0x80, 0x80, 0x80, 0x80, 0xff, 0xff, 0x01, 0xff, 0x04, 0xff, 0x0b, | ||
0xff, 0xff, 0x04, 0xff, 0x05, 0xff, 0x80, 0x80, 0x80, 0x80, 0xff, 0x01, 0x80, 0x80, 0xff, 0x01, | ||
0x80, 0xff, 0x04, 0xff, 0xff, 0x0c, 0xff, 0x15, 0xff, 0x80, 0xff, 0x09, 0x80, 0xff, 0xff, 0x04, | ||
0xff, 0xff, 0x0c, 0xff, 0x15, 0xff, 0x09, 0x80, 0xff, 0x80, 0x80, 0x80, 0xff, 0xff, 0x04, 0xff, | ||
0xff, 0x04, 0xff, 0x05, 0xff, 0x13, 0x80, 0xff, 0xff, 0x04, 0xff, 0x2b, 0xff, 0x80, 0x80, 0x80, | ||
0xff, 0xff, 0x02, 0xff, 0x16, 0xff, 0xff, 0x04, 0xff, 0x02, 0xff, 0xff, 0x04, 0xff, 0x09, 0xff, | ||
0xff, 0x04, 0xff, 0xff, 0x02, 0xff, 0x3e, 0xff, 0xff, 0x04, 0xff, 0x02, 0xff, 0xff, 0x04, 0xff, | ||
0x15, 0xff, 0x80, 0x80, 0x80, 0x80, 0xff, 0x80, 0x80, 0x80, 0x80, 0x80, 0xff, 0x02, 0xff, 0xff, | ||
0x03, 0xff, 0xff, 0x09, 0xff, 0xff, 0x0c, 0xff, 0x05, 0xff, 0x80, 0xff, 0xff, 0x01, 0x01, 0x80, | ||
0xff, 0x10, 0x80, 0xff, 0xff, 0x01, 0xff, 0x02, 0xff, 0x2e, 0xff, 0xff, 0x04, 0xff, 0x02, 0xff, | ||
0xff, 0x04, 0xff, 0xff, 0x02, 0xff, 0x3e, 0xff, 0xff, 0x04, 0xff, 0x02, 0xff, 0xff, 0x04, 0xff, | ||
0xff, 0x0c, 0xff, 0x05, 0xff, 0xff, 0x01, 0x01, 0x80, 0xff, 0x80, 0x80, 0x80, 0x80, 0xff, 0x80, | ||
0x80, 0x80, 0x80, 0xff, 0xff, 0x01, 0xff, 0x02, 0xff, 0x12, 0xff, 0xff, 0x04, 0xff, 0x02, 0xff, | ||
0xff, 0x04, 0xff, 0xff, 0x0c, 0xff, 0x05, 0xff, 0xff, 0x01, 0x01, 0x80, 0xff, 0xff, 0x04, 0xff, | ||
0xff, 0x0c, 0xff, 0x05, 0xff, 0x80, 0xff, 0xff, 0x01, 0x01, 0x80, 0xff, 0x80, 0x80, 0x80, 0x80, | ||
0x80, 0x80, 0xff, 0x01, 0x80, 0xff, 0x01, 0x80, 0x80, 0xff, 0x04, 0xff, 0xff, 0x02, 0xff, 0x16, | ||
0xff, 0xff, 0x04, 0xff, 0x02, 0xff, 0xff, 0x04, 0xff, 0x09, 0xff, 0x80, 0x80, 0x80, 0x80, 0xff, | ||
0x0d, 0x80, 0xff, 0xff, 0x04, 0xff, 0x09, 0xff, 0xff, 0x04, 0xff, 0xff, 0x02, 0xff, 0x1e, 0xff, | ||
0xff, 0x04, 0xff, 0x02, 0xff, 0xff, 0x04, 0xff, 0x15, 0xff, 0x80, 0x80, 0x80, 0x80, 0xff, 0xff, | ||
0x04, 0xff, 0x2d, 0xff, 0xff, 0x04, 0xff, 0xff, 0x02, 0xff, 0x15, 0xff, 0x5d, 0x80, 0xff, 0x7d, | ||
0x80, 0x80, 0x80, 0x80, 0xff, 0xff, 0x02, 0xff, 0xff, 0x03, 0xff, 0x05, 0xff, 0xff, 0x01, 0xff, | ||
0x04, 0xff, 0xff, 0x02, 0xff, 0x0a, 0xff, 0xff, 0x04, 0xff, 0x02, 0xff, 0xff, 0x04, 0xff, 0x09, | ||
0xff, 0x80, 0x80, 0x80, 0x80, 0xff, 0xff, 0x02, 0xff, 0x16, 0xff, 0xff, 0x04, 0xff, 0x02, 0xff, | ||
0xff, 0x04, 0xff, 0x0d, 0xff, 0x80, 0x80, 0x80, 0x80, 0x80, 0xff, 0x80, 0x80, 0xff, 0x01, 0x80, | ||
0xff, 0x02, 0xff, 0xff, 0x03, 0xff, 0xff, 0x07, 0xff, 0x05, 0x80, 0xff, 0xff, 0x01, 0xff, 0x0b, | ||
0xff, 0xff, 0x01, 0x02, 0xff, 0xff, 0x02, 0xff, 0x1e, 0xff, 0xff, 0x04, 0xff, 0x02, 0xff, 0xff, | ||
0x04, 0xff, 0x09, 0xff, 0x80, 0x80, 0x80, 0x80, 0xff, 0xff, 0x02, 0xff, 0x1e, 0xff, 0xff, 0x04, | ||
0xff, 0x02, 0xff, 0xff, 0x04, 0xff, 0x0d, 0xff, 0x80, 0x80, 0x80, 0x80, 0x80, 0xff, 0xff, 0x01, | ||
0xff, 0x0b, 0xff, 0xff, 0x01, 0x01, 0xff, 0x05, 0x80, 0x80, 0xff, 0x01, 0x80, 0xff, 0x01, 0x80, | ||
0x80, | ||
]; | ||
|
||
// constant from the main chia blockchain: | ||
// https://github.com/Chia-Network/chia-blockchain/blob/main/chia/consensus/default_constants.py | ||
pub const COST_PER_BYTE: u64 = 12000; |
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,5 +1,6 @@ | ||
pub mod compression; | ||
pub mod gen; | ||
pub mod generator_rom; | ||
pub mod merkle_set; | ||
|
||
#[cfg(fuzzing)] | ||
|
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
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
Oops, something went wrong.