Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SpendBundle members #315

Merged
merged 4 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions chia-protocol/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ cargo-fuzz = true
libfuzzer-sys = "0.4"
clvmr = "0.3.3"
chia-traits = { path = "../../chia-traits" }
clvm-traits = { path = "../../clvm-traits" }
arbitrary = { version = "=1.3.0" }
sha2 = "0.10.8"
hex = "0.4.3"
Expand Down Expand Up @@ -60,3 +61,10 @@ path = "fuzz_targets/streamable.rs"
test = false
doc = false
bench = false

[[bin]]
name = "spend-bundle"
path = "fuzz_targets/spend-bundle.rs"
test = false
doc = false
bench = false
61 changes: 61 additions & 0 deletions chia-protocol/fuzz/fuzz_targets/spend-bundle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#![no_main]

use chia_protocol::Coin;
use chia_protocol::{Bytes32, SpendBundle};
use chia_traits::Streamable;
use clvm_traits::FromClvm;
use clvmr::op_utils::{first, rest};
use clvmr::ENABLE_FIXED_DIV;
use clvmr::{Allocator, NodePtr};
use libfuzzer_sys::fuzz_target;
use std::collections::HashSet;
use std::iter::FromIterator;

fuzz_target!(|data: &[u8]| {
let Ok(bundle) = SpendBundle::from_bytes(data) else {
return;
};
let Ok(additions) = bundle.additions() else {
return;
};

let additions = HashSet::from_iter(additions.iter().cloned());

let mut expected = HashSet::new();

let mut a = Allocator::new();
let mut total_cost = 0;
for cs in &bundle.coin_spends {
let (cost, mut conds) = cs
.puzzle_reveal
.run(&mut a, ENABLE_FIXED_DIV, 11000000000, &cs.solution)
.expect("run");
total_cost += cost;

let parent_coin_info: Bytes32 = cs.coin.coin_id().into();

while let Some((c, tail)) = a.next(conds) {
conds = tail;
let op = first(&a, c).expect("first");
let c = rest(&a, c).expect("rest");
let buf = a.atom(op);
if buf.len() != 1 {
continue;
}
if buf[0] == 51 {
let (puzzle_hash, (amount, _)) =
<(Bytes32, (u64, NodePtr))>::from_clvm(&a, c).expect("parse spend");
expected.insert(Coin {
parent_coin_info,
puzzle_hash,
amount,
});
total_cost += 1800000;
}
}
}

assert!(total_cost <= 11000000000);

assert_eq!(additions, expected);
});
49 changes: 42 additions & 7 deletions chia-protocol/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ use chia_traits::chia_error::{Error, Result};
use chia_traits::Streamable;
use clvm_traits::{FromClvmError, ToClvmError};
use clvmr::allocator::NodePtr;
use clvmr::cost::Cost;
use clvmr::reduction::EvalErr;
use clvmr::run_program;
use clvmr::serde::{
node_from_bytes, node_to_bytes, serialized_length_from_bytes,
node_from_bytes, node_from_bytes_backrefs, node_to_bytes, serialized_length_from_bytes,
serialized_length_from_bytes_trusted,
};
use clvmr::{Allocator, FromNodePtr, ToNodePtr};
use clvmr::{Allocator, ChiaDialect, FromNodePtr, ToNodePtr};
use sha2::{Digest, Sha256};
use std::io::Cursor;

Expand Down Expand Up @@ -51,6 +54,31 @@ impl Program {
pub fn as_slice(&self) -> &[u8] {
self.0.as_slice()
}

#[cfg(test)]
pub fn new(buf: Bytes) -> Program {
Program(buf)
}

pub fn run<A: ToNodePtr>(
&self,
a: &mut Allocator,
flags: u32,
max_cost: Cost,
arg: &A,
) -> std::result::Result<(Cost, NodePtr), EvalErr> {
let arg = arg.to_node_ptr(a).map_err(|_| {
EvalErr(
a.null(),
"failed to convert argument to CLVM objects".to_string(),
)
})?;
let program =
node_from_bytes_backrefs(a, self.0.as_ref()).expect("invalid SerializedProgram");
let dialect = ChiaDialect::new(flags);
let reduction = run_program(a, &dialect, program, arg, max_cost)?;
Ok((reduction.0, reduction.1))
}
}

impl Default for Program {
Expand All @@ -74,9 +102,6 @@ use pyo3::prelude::*;
#[cfg(feature = "py-bindings")]
use pyo3::types::{PyList, PyTuple};

#[cfg(feature = "py-bindings")]
use clvmr::serde::node_from_bytes_backrefs;

#[cfg(feature = "py-bindings")]
use clvmr::allocator::SExp;

Expand Down Expand Up @@ -296,8 +321,6 @@ impl Program {
args: &PyAny,
) -> PyResult<(u64, &'a PyAny)> {
use clvmr::reduction::Response;
use clvmr::run_program;
use clvmr::ChiaDialect;
use std::rc::Rc;

let mut a = Allocator::new_limited(500000000, 62500000, 62500000);
Expand Down Expand Up @@ -465,4 +488,16 @@ mod tests {
let round_trip = program.to_node_ptr(a).unwrap();
assert_eq!(expected, hex::encode(node_to_bytes(a, round_trip).unwrap()));
}

#[test]
fn program_run() {
let a = &mut Allocator::new();

// (+ 2 5)
let prg = Program::from_bytes(&hex::decode("ff10ff02ff0580").expect("hex::decode"))
.expect("from_bytes");
let (cost, result) = prg.run(a, 0, 1000, &[1300, 37]).expect("run");
assert_eq!(cost, 869);
assert_eq!(a.number(result), 1337.into());
}
}
Loading
Loading