Skip to content

Commit

Permalink
Merge pull request #409 from Chia-Network/tree-hash-tests
Browse files Browse the repository at this point in the history
extend tree-hash test coverage
  • Loading branch information
arvidn committed Feb 23, 2024
2 parents d78eab7 + affd7ed commit 5d77229
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 34 deletions.
1 change: 0 additions & 1 deletion chia-bls/fuzz/fuzz_targets/blspy-fidelity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#![no_main]
use libfuzzer_sys::fuzz_target;
use pyo3::prelude::*;
use std::convert::TryFrom;

use chia_bls::derivable_key::DerivableKey;
use chia_bls::secret_key::SecretKey;
Expand Down
1 change: 0 additions & 1 deletion chia-bls/src/mnemonic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use anyhow::Error;
use bip39::{Language, Mnemonic, Seed};
use std::array::TryFromSliceError;
use std::result::Result;

pub fn entropy_to_mnemonic(entropy: &[u8; 32]) -> String {
Mnemonic::from_entropy(entropy, Language::English)
Expand Down
1 change: 0 additions & 1 deletion chia-bls/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use blst::*;
use chia_traits::{read_bytes, Streamable};
use sha2::{Digest, Sha256};
use std::borrow::Borrow;
use std::convert::AsRef;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::io::Cursor;
Expand Down
1 change: 0 additions & 1 deletion chia-protocol/fuzz/fuzz_targets/spend-bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ 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 {
Expand Down
1 change: 0 additions & 1 deletion chia-protocol/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use chia_traits::{chia_error, read_bytes, Streamable};
use clvm_traits::{ClvmDecoder, ClvmEncoder, FromClvm, FromClvmError, ToClvm, ToClvmError};
use sha2::{Digest, Sha256};
use std::array::TryFromSliceError;
use std::convert::{AsRef, TryInto};
use std::fmt;
use std::io::Cursor;
use std::ops::Deref;
Expand Down
1 change: 0 additions & 1 deletion chia-protocol/src/coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use clvm_traits::{
ToClvm, ToClvmError,
};
use sha2::{Digest, Sha256};
use std::convert::TryInto;

#[cfg(feature = "py-bindings")]
use pyo3::prelude::*;
Expand Down
1 change: 0 additions & 1 deletion chia-protocol/src/spend_bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use clvmr::cost::Cost;
use clvmr::op_utils::{first, rest};
use clvmr::reduction::EvalErr;
use clvmr::Allocator;
use std::result::Result;

#[cfg(feature = "py-bindings")]
use pyo3::prelude::*;
Expand Down
5 changes: 2 additions & 3 deletions chia-traits/src/int.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use pyo3::prelude::*;
use pyo3::types::{PyAny, PyBool, PyList, PyString, PyTuple};
use pyo3::PyResult;
use pyo3::types::{PyAny, PyBool, PyList, PyModule, PyString, PyTuple};
use pyo3::{IntoPy, PyResult, Python};

/// A custom to-python conversion trait that turns primitive integer types into
/// the chia-blockchain fixed-width integer types (uint8, int8, etc.)
Expand Down
1 change: 0 additions & 1 deletion chia-traits/src/streamable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::chia_error::{Error, Result};
use sha2::{Digest, Sha256};
use std::convert::TryInto;
use std::io::Cursor;
use std::mem::size_of;

Expand Down
106 changes: 91 additions & 15 deletions clvm-utils/src/tree_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,21 +208,67 @@ use rstest::rstest;

#[cfg(test)]
#[rstest]
#[case("block-1ee588dc")]
#[case("block-6fe59b24")]
#[case("block-b45268ac")]
#[case("block-c2a8df0d")]
#[case("block-e5002df2")]
#[case("block-4671894")]
#[case("block-225758")]
#[case("block-834752")]
#[case("block-834752-compressed")]
#[case("block-834760")]
#[case("block-834761")]
#[case("block-834765")]
#[case("block-834766")]
#[case("block-834768")]
fn test_tree_hash_cached(#[case] name: &str, #[values(true, false)] compressed: bool) {
#[case(
"block-1ee588dc",
"1cba0b22b84b597d265d77fbabb57fada01d963f75dc3956a6166a2385997ef2"
)]
#[case(
"block-6fe59b24",
"540c5afac7c26728ed6b7891d8ce2f5b26009c4b0090d7035403c2425dc54e1d"
)]
#[case(
"block-b45268ac",
"7cc321f5554126c9f430afbc7dd9c804f5d34a248e3192f275f5d585ecf8e873"
)]
#[case(
"block-c2a8df0d",
"2e25efa524e420111006fee77f50fb8fbd725920a5312d5480af239d81ab5e7e"
)]
#[case(
"block-e5002df2",
"c179ece232dceef984ba000f7e5b67ee3092582668bf6178969df10845eb8b18"
)]
#[case(
"block-4671894",
"3750f0e1bde9fcb407135f974aa276a4580e1e76a47e6d8d9bb2911d0fe91db1"
)]
#[case(
"block-225758",
"880df94c3c9e0f7c26c42ae99723e683a4cd37e73f74c6322d1dfabaa1d64d93"
)]
#[case(
"block-834752",
"be755b8ef03d917b8bd37ae152792a7daa7de81bbb0eaa21c530571c2105c130"
)]
#[case(
"block-834752-compressed",
"be755b8ef03d917b8bd37ae152792a7daa7de81bbb0eaa21c530571c2105c130"
)]
#[case(
"block-834760",
"77558768f74c5f863b36232a1390843a63a397fc22da1321fea3a05eab67be2c"
)]
#[case(
"block-834761",
"4bac8b299c6545a37a825883c863b79ce850e7f6c8f1d2abeec2865f5450f1c5"
)]
#[case(
"block-834765",
"b915ec5f9f8ea723e0a99b035df206673369b802766dd76b6c8f4c15ab7bca2c"
)]
#[case(
"block-834766",
"409559c3395fb18a6c3390ccccd55e82162b1e68b867490a90ccbddf78147c9d"
)]
#[case(
"block-834768",
"905441945a9a56558337c8b7a536a6b9606ad63e11a265a938f301747ccfb7af"
)]
fn test_tree_hash_cached(
#[case] name: &str,
#[case] expect: &str,
#[values(true, false)] compressed: bool,
) {
use clvmr::serde::{
node_from_bytes_backrefs, node_from_bytes_backrefs_record, node_to_bytes_backrefs,
};
Expand Down Expand Up @@ -253,5 +299,35 @@ fn test_tree_hash_cached(#[case] name: &str, #[values(true, false)] compressed:
// println!(" {key:?}: {}", hex::encode(value));
// }
assert_eq!(hash1, hash2);
assert_eq!(hash1, hex::decode(expect).unwrap().as_slice());
assert!(!compressed || !backrefs.is_empty());
}

#[cfg(test)]
fn test_sha256_atom(buf: &[u8]) {
let hash = tree_hash_atom(buf);

let mut hasher = Sha256::new();
hasher.update([1_u8]);
if !buf.is_empty() {
hasher.update(buf);
}

assert_eq!(hash, hasher.finalize().as_slice());
}

#[test]
fn test_tree_hash_atom() {
test_sha256_atom(&[]);
for val in 0..255 {
test_sha256_atom(&[val]);
}

for val in 0..255 {
test_sha256_atom(&[0, val]);
}

for val in 0..255 {
test_sha256_atom(&[0xff, val]);
}
}
1 change: 0 additions & 1 deletion src/gen/get_puzzle_and_solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use ::chia_protocol::bytes::Bytes32;
use clvm_utils::tree_hash;
use clvmr::allocator::{Allocator, Atom, NodePtr};
use clvmr::op_utils::u64_from_bytes;
use std::convert::AsRef;

// returns parent-coin ID, amount, puzzle-reveal and solution
pub fn parse_coin_spend(
Expand Down
1 change: 0 additions & 1 deletion src/gen/solution_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ where
#[cfg(test)]
mod tests {
use super::*;
use chia_protocol::Coin;
use clvmr::{run_program, ChiaDialect};
use hex_literal::hex;

Expand Down
5 changes: 1 addition & 4 deletions wheel/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,10 @@ use clvmr::{ENABLE_BLS_OPS_OUTSIDE_GUARD, ENABLE_FIXED_DIV, LIMIT_HEAP, NO_UNKNO
use pyo3::buffer::PyBuffer;
use pyo3::exceptions::PyRuntimeError;
use pyo3::prelude::*;
use pyo3::types::PyAny;
use pyo3::types::PyBytes;
use pyo3::types::PyList;
use pyo3::types::PyModule;
use pyo3::types::PyTuple;
use pyo3::{wrap_pyfunction, PyResult, Python};
use std::convert::TryInto;
use pyo3::wrap_pyfunction;
use std::iter::zip;

use crate::run_program::{run_chia_program, serialized_length};
Expand Down
2 changes: 0 additions & 2 deletions wheel/src/compression.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use chia::compression::compressor::create_autoextracting_clvm_program;
use pyo3::prelude::*;
use pyo3::types::PyBytes;
use pyo3::types::PyModule;
use pyo3::{wrap_pyfunction, PyResult, Python};

#[pyfunction]
fn create_compressed_generator<'p>(py: Python<'p>, input_program: &[u8]) -> PyResult<&'p PyBytes> {
Expand Down

0 comments on commit 5d77229

Please sign in to comment.