Skip to content

Commit

Permalink
Merge branch 'main' into cargo-config-speed-up-build
Browse files Browse the repository at this point in the history
  • Loading branch information
hanabi1224 authored Oct 20, 2022
2 parents 23051c0 + ba2ba80 commit 2c6d309
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions blockchain/blocks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ thiserror.workspace = true
async-std.workspace = true
forest_test_utils.workspace = true
hex.workspace = true
quickcheck_macros.workspace = true
serde_json.workspace = true
44 changes: 44 additions & 0 deletions blockchain/blocks/src/tipset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,50 @@ impl quickcheck::Arbitrary for Tipset {
}
}

#[cfg(test)]
mod property_tests {
use super::tipset_json::{TipsetJson, TipsetJsonRef};
use super::tipset_keys_json::TipsetKeysJson;
use super::{Tipset, TipsetKeys};
use cid::Cid;
use quickcheck_macros::quickcheck;
use serde_json;

#[derive(Clone)]
struct ArbitraryCid(Cid);

impl quickcheck::Arbitrary for ArbitraryCid {
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
ArbitraryCid(Cid::new_v1(
u64::arbitrary(g),
cid::multihash::Multihash::wrap(u64::arbitrary(g), &[u8::arbitrary(g)]).unwrap(),
))
}
}

impl quickcheck::Arbitrary for TipsetKeys {
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
let arbitrary_cids: Vec<ArbitraryCid> = Vec::arbitrary(g);
let cids: Vec<Cid> = arbitrary_cids.iter().map(|cid| cid.0).collect();
Self { cids }
}
}

#[quickcheck]
fn tipset_keys_roundtrip(tipset_keys: TipsetKeys) {
let serialized = serde_json::to_string(&TipsetKeysJson(tipset_keys.clone())).unwrap();
let parsed: TipsetKeysJson = serde_json::from_str(&serialized).unwrap();
assert_eq!(tipset_keys, parsed.0);
}

#[quickcheck]
fn tipset_roundtrip(tipset: Tipset) {
let serialized = serde_json::to_string(&TipsetJsonRef(&tipset)).unwrap();
let parsed: TipsetJson = serde_json::from_str(&serialized).unwrap();
assert_eq!(&tipset, parsed.0.as_ref());
}
}

impl From<FullTipset> for Tipset {
fn from(full_tipset: FullTipset) -> Self {
let key = full_tipset.key;
Expand Down

0 comments on commit 2c6d309

Please sign in to comment.