From 63ee179defe4825efb4e1ada782516278e0bd540 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Wed, 15 May 2019 15:22:34 +1000 Subject: [PATCH] Add CachedTreeHash testing to ssz_static --- tests/ef_tests/src/cases/ssz_static.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/ef_tests/src/cases/ssz_static.rs b/tests/ef_tests/src/cases/ssz_static.rs index 3957bd81a..79739f729 100644 --- a/tests/ef_tests/src/cases/ssz_static.rs +++ b/tests/ef_tests/src/cases/ssz_static.rs @@ -7,6 +7,7 @@ use ssz::Decode; use std::fmt::Debug; use tree_hash::TreeHash; use types::{ + test_utils::{SeedableRng, TestRandom, XorShiftRng}, Attestation, AttestationData, AttestationDataAndCustodyBit, AttesterSlashing, BeaconBlock, BeaconBlockBody, BeaconBlockHeader, BeaconState, Crosslink, Deposit, DepositData, Eth1Data, EthSpec, Fork, Hash256, HistoricalBatch, IndexedAttestation, PendingAttestation, @@ -90,7 +91,13 @@ impl EfTest for Cases { fn ssz_static_test(tc: &SszStatic) -> Result<(), Error> where - T: Decode + Debug + PartialEq + serde::de::DeserializeOwned + TreeHash + CachedTreeHash, + T: Decode + + Debug + + PartialEq + + serde::de::DeserializeOwned + + TreeHash + + CachedTreeHash + + TestRandom, { // Verify we can decode SSZ in the same way we can decode YAML. let ssz = hex::decode(&tc.serialized[2..]) @@ -112,5 +119,13 @@ where let cached_tree_hash_root = Hash256::from_slice(cache.tree_hash_root().unwrap()); compare_result::(&Ok(cached_tree_hash_root), &Some(expected_root))?; + // Verify the root after an update from a random CachedTreeHash to the decoded struct. + let mut rng = XorShiftRng::from_seed([42; 16]); + let random_instance = T::random_for_test(&mut rng); + let mut cache = TreeHashCache::new(&random_instance).unwrap(); + cache.update(&decoded).unwrap(); + let updated_root = Hash256::from_slice(cache.tree_hash_root().unwrap()); + compare_result::(&Ok(updated_root), &Some(expected_root))?; + Ok(()) }