mod commit_dag;
use alloc::vec::Vec;
use crate::{
collections::{Map, Set},
depth::{DepthMetric, MAX_STRATA_DEPTH},
digest::Digest,
fragment::{Fragment, FragmentSpec, FragmentSummary},
id::SedimentreeId,
loose_commit::LooseCommit,
};
#[derive(Clone, Debug, PartialEq, Eq, Default, minicbor::Encode, minicbor::Decode)]
#[cfg_attr(not(feature = "std"), derive(Hash))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SedimentreeSummary {
#[n(0)]
#[cbor(with = "crate::cbor::set")]
fragment_summaries: Set<FragmentSummary>,
#[n(1)]
#[cbor(with = "crate::cbor::set")]
commits: Set<LooseCommit>,
}
impl SedimentreeSummary {
#[must_use]
pub const fn new(
fragment_summaries: Set<FragmentSummary>,
commits: Set<LooseCommit>,
) -> SedimentreeSummary {
SedimentreeSummary {
fragment_summaries,
commits,
}
}
#[must_use]
pub const fn fragment_summaries(&self) -> &Set<FragmentSummary> {
&self.fragment_summaries
}
#[must_use]
pub const fn loose_commits(&self) -> &Set<LooseCommit> {
&self.commits
}
#[must_use]
pub fn as_remote_diff(&self) -> RemoteDiff<'_> {
RemoteDiff {
remote_fragment_summaries: self.fragment_summaries.iter().collect(),
remote_commits: self.commits.iter().collect(),
local_fragments: Vec::new(),
local_commits: Vec::new(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Diff<'a> {
pub left_missing_fragments: Vec<&'a Fragment>,
pub left_missing_commits: Vec<&'a LooseCommit>,
pub right_missing_fragments: Vec<&'a Fragment>,
pub right_missing_commits: Vec<&'a LooseCommit>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RemoteDiff<'a> {
pub remote_fragment_summaries: Vec<&'a FragmentSummary>,
pub remote_commits: Vec<&'a LooseCommit>,
pub local_fragments: Vec<&'a Fragment>,
pub local_commits: Vec<&'a LooseCommit>,
}
#[derive(Default, Clone, PartialEq, Eq)]
#[cfg_attr(not(feature = "std"), derive(PartialOrd, Ord, Hash))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct Sedimentree {
fragments: Set<Fragment>,
commits: Set<LooseCommit>,
}
impl Sedimentree {
#[must_use]
pub fn new(fragments: Vec<Fragment>, commits: Vec<LooseCommit>) -> Self {
Self {
fragments: fragments.into_iter().collect(),
commits: commits.into_iter().collect(),
}
}
pub fn merge(&mut self, other: Sedimentree) {
self.fragments.extend(other.fragments);
self.commits.extend(other.commits);
}
#[must_use]
pub fn minimal_hash<M: DepthMetric>(&self, depth_metric: &M) -> MinimalTreeHash {
let minimal = self.minimize(depth_metric);
let mut hashes = minimal
.fragments()
.flat_map(|s| {
core::iter::once(s.head())
.chain(s.boundary().iter().copied())
.chain(s.checkpoints().iter().copied())
})
.chain(minimal.commits.iter().map(LooseCommit::digest))
.collect::<Vec<_>>();
hashes.sort();
let mut h = blake3::Hasher::new();
for hash in hashes {
h.update(hash.as_bytes());
}
MinimalTreeHash(*h.finalize().as_bytes())
}
pub fn add_commit(&mut self, commit: LooseCommit) -> bool {
self.commits.insert(commit)
}
pub fn add_fragment(&mut self, fragment: Fragment) -> bool {
self.fragments.insert(fragment)
}
#[must_use]
pub fn diff<'a>(&'a self, other: &'a Sedimentree) -> Diff<'a> {
Diff {
left_missing_fragments: other.fragments.difference(&self.fragments).collect(),
left_missing_commits: other.commits.difference(&self.commits).collect(),
right_missing_fragments: self.fragments.difference(&other.fragments).collect(),
right_missing_commits: self.commits.difference(&other.commits).collect(),
}
}
#[must_use]
pub fn diff_remote<'a>(&'a self, remote: &'a SedimentreeSummary) -> RemoteDiff<'a> {
let fragment_by_summary: Map<&FragmentSummary, &Fragment> =
self.fragments.iter().map(|f| (f.summary(), f)).collect();
let our_fragments_meta: Set<&FragmentSummary> =
fragment_by_summary.keys().copied().collect();
let their_fragments: Set<&FragmentSummary> = remote.fragment_summaries.iter().collect();
let local_fragments: Vec<&Fragment> = our_fragments_meta
.difference(&their_fragments)
.filter_map(|summary| fragment_by_summary.get(summary).copied())
.collect();
let remote_fragments = their_fragments.difference(&our_fragments_meta);
let our_commits = self.commits.iter().collect::<Set<&LooseCommit>>();
let their_commits = remote.commits.iter().collect();
let local_commits = our_commits.difference(&their_commits);
let remote_commits = their_commits.difference(&our_commits);
RemoteDiff {
remote_fragment_summaries: remote_fragments.into_iter().copied().collect(),
remote_commits: remote_commits.into_iter().copied().collect(),
local_fragments,
local_commits: local_commits.into_iter().copied().collect(),
}
}
pub fn fragments(&self) -> impl Iterator<Item = &Fragment> {
self.fragments.iter()
}
pub fn loose_commits(&self) -> impl Iterator<Item = &LooseCommit> {
self.commits.iter()
}
#[must_use]
pub fn has_loose_commit(&self, digest: Digest<LooseCommit>) -> bool {
self.loose_commits().any(|c| c.digest() == digest)
}
#[must_use]
pub fn has_fragment_starting_with<M: DepthMetric>(
&self,
digest: Digest<LooseCommit>,
depth_metric: &M,
) -> bool {
self.heads(depth_metric).contains(&digest)
}
#[must_use]
pub fn minimize<M: DepthMetric>(&self, depth_metric: &M) -> Sedimentree {
let mut fragments = self.fragments.iter().collect::<Vec<_>>();
fragments.sort_by_key(|a| a.depth(depth_metric));
let mut minimized_fragments = Vec::<Fragment>::new();
for fragment in fragments {
if !minimized_fragments
.iter()
.any(|existing| existing.supports(fragment.summary(), depth_metric))
{
minimized_fragments.push(fragment.clone());
}
}
let dag = commit_dag::CommitDag::from_commits(self.commits.iter());
let simplified_dag = dag.simplify(&minimized_fragments, depth_metric);
let commits = self
.commits
.iter()
.filter(|&c| simplified_dag.contains_commit(&c.digest()))
.cloned()
.collect();
Sedimentree::new(minimized_fragments, commits)
}
#[must_use]
pub fn summarize(&self) -> SedimentreeSummary {
SedimentreeSummary {
fragment_summaries: self
.fragments
.iter()
.map(|fragment| fragment.summary().clone())
.collect(),
commits: self.commits.clone(),
}
}
#[must_use]
pub fn heads<M: DepthMetric>(&self, depth_metric: &M) -> Vec<Digest<LooseCommit>> {
let minimized = self.minimize(depth_metric);
let dag = commit_dag::CommitDag::from_commits(minimized.commits.iter());
let mut heads = Vec::<Digest<LooseCommit>>::new();
for fragment in &minimized.fragments {
if !minimized
.fragments
.iter()
.any(|s| s.boundary().contains(&fragment.head()))
&& fragment
.boundary()
.iter()
.all(|end| !dag.contains_commit(end))
{
heads.extend(fragment.boundary());
}
}
heads.extend(dag.heads());
heads
}
pub fn into_items(self) -> impl Iterator<Item = CommitOrFragment> {
self.fragments
.into_iter()
.map(CommitOrFragment::Fragment)
.chain(self.commits.into_iter().map(CommitOrFragment::Commit))
}
#[must_use]
pub fn missing_fragments<M: DepthMetric>(
&self,
id: SedimentreeId,
depth_metric: &M,
) -> Vec<FragmentSpec> {
use alloc::vec;
let dag = commit_dag::CommitDag::from_commits(self.commits.iter());
let mut runs_by_level =
Map::<crate::depth::Depth, (Digest<LooseCommit>, Vec<Digest<LooseCommit>>)>::new();
let mut all_bundles = Vec::new();
for commit_hash in dag.canonical_sequence(self.fragments.iter(), depth_metric) {
let level = depth_metric.to_depth(commit_hash);
for (run_level, (_start, checkpoints)) in &mut runs_by_level {
if run_level < &level {
checkpoints.push(commit_hash);
}
}
if level >= MAX_STRATA_DEPTH
&& let Some((head, checkpoints)) = runs_by_level.remove(&level)
{
if self.fragments.iter().any(|s| s.supports_block(commit_hash)) {
runs_by_level.insert(level, (commit_hash, Vec::new()));
} else {
all_bundles.push(FragmentSpec::new(
id,
head,
checkpoints.clone(),
vec![commit_hash],
));
}
}
}
all_bundles
}
#[must_use]
pub fn as_local_diff(&self) -> RemoteDiff<'_> {
RemoteDiff {
remote_fragment_summaries: Vec::new(),
remote_commits: Vec::new(),
local_fragments: self.fragments.iter().collect(),
local_commits: self.commits.iter().collect(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CommitOrFragment {
Commit(LooseCommit),
Fragment(Fragment),
}
impl core::fmt::Debug for Sedimentree {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Sedimentree")
.field("fragments", &self.fragments.len())
.field("commits", &self.commits.len())
.finish()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct MinimalTreeHash([u8; 32]);
impl MinimalTreeHash {
#[must_use]
pub const fn as_bytes(&self) -> &[u8; 32] {
&self.0
}
}
impl From<[u8; 32]> for MinimalTreeHash {
fn from(value: [u8; 32]) -> Self {
Self(value)
}
}
pub fn has_commit_boundary<
I: IntoIterator<Item = D>,
D: Into<Digest<LooseCommit>>,
M: DepthMetric,
>(
commits: I,
depth_metric: &M,
) -> bool {
commits
.into_iter()
.any(|digest| depth_metric.to_depth(digest.into()) <= MAX_STRATA_DEPTH)
}
#[cfg(test)]
mod tests {
use alloc::vec;
use crate::blob::BlobMeta;
use super::*;
fn make_commit(seed: u8) -> LooseCommit {
let mut bytes = [0u8; 32];
bytes[0] = seed;
let digest = Digest::from_bytes(bytes);
let blob_meta = BlobMeta::new(&[seed]);
LooseCommit::new(digest, vec![], blob_meta)
}
fn make_fragment(seed: u8) -> Fragment {
let mut head_bytes = [0u8; 32];
head_bytes[0] = seed;
let mut boundary_bytes = [0u8; 32];
boundary_bytes[0] = seed;
boundary_bytes[1] = 1;
let blob_meta = BlobMeta::new(&[seed]);
Fragment::new(
Digest::from_bytes(head_bytes),
vec![Digest::from_bytes(boundary_bytes)],
vec![],
blob_meta,
)
}
#[test]
fn diff_identical_non_empty_trees() {
let commits = vec![make_commit(1), make_commit(2), make_commit(3)];
let fragments = vec![make_fragment(1), make_fragment(2)];
let a = Sedimentree::new(fragments.clone(), commits.clone());
let b = Sedimentree::new(fragments, commits);
let diff = a.diff(&b);
assert!(
diff.left_missing_commits.is_empty(),
"identical trees have no left missing commits"
);
assert!(
diff.right_missing_commits.is_empty(),
"identical trees have no right missing commits"
);
assert!(
diff.left_missing_fragments.is_empty(),
"identical trees have no left missing fragments"
);
assert!(
diff.right_missing_fragments.is_empty(),
"identical trees have no right missing fragments"
);
let b_summary = b.summarize();
let remote_diff = a.diff_remote(&b_summary);
assert!(
remote_diff.local_commits.is_empty(),
"identical trees have no local-only commits"
);
assert!(
remote_diff.remote_commits.is_empty(),
"identical trees have no remote-only commits"
);
assert!(
remote_diff.local_fragments.is_empty(),
"identical trees have no local-only fragments"
);
assert!(
remote_diff.remote_fragment_summaries.is_empty(),
"identical trees have no remote-only fragments"
);
}
#[test]
fn diff_superset_commits() {
let shared = vec![make_commit(1), make_commit(2)];
let extra = vec![make_commit(3), make_commit(4)];
let a = Sedimentree::new(vec![], shared.clone());
let b = Sedimentree::new(vec![], [shared, extra.clone()].concat());
let diff = a.diff(&b);
assert_eq!(diff.left_missing_commits.len(), 2);
assert!(diff.right_missing_commits.is_empty());
assert!(diff.left_missing_fragments.is_empty());
assert!(diff.right_missing_fragments.is_empty());
}
#[test]
fn diff_superset_fragments() {
let shared = vec![make_fragment(1), make_fragment(2)];
let extra = vec![make_fragment(3)];
let a = Sedimentree::new(shared.clone(), vec![]);
let b = Sedimentree::new([shared, extra].concat(), vec![]);
let diff = a.diff(&b);
assert_eq!(diff.left_missing_fragments.len(), 1);
assert!(diff.right_missing_fragments.is_empty());
assert!(diff.left_missing_commits.is_empty());
assert!(diff.right_missing_commits.is_empty());
}
#[test]
fn diff_diverged_with_overlap() {
let shared_commits = vec![make_commit(1), make_commit(2)];
let a_only_commits = vec![make_commit(10), make_commit(11)];
let b_only_commits = vec![make_commit(20), make_commit(21), make_commit(22)];
let shared_fragments = vec![make_fragment(1)];
let a_only_fragments = vec![make_fragment(10)];
let b_only_fragments = vec![make_fragment(20), make_fragment(21)];
let a = Sedimentree::new(
[shared_fragments.clone(), a_only_fragments.clone()].concat(),
[shared_commits.clone(), a_only_commits.clone()].concat(),
);
let b = Sedimentree::new(
[shared_fragments, b_only_fragments].concat(),
[shared_commits, b_only_commits].concat(),
);
let diff = a.diff(&b);
assert_eq!(diff.left_missing_commits.len(), 3); assert_eq!(diff.left_missing_fragments.len(), 2);
assert_eq!(diff.right_missing_commits.len(), 2); assert_eq!(diff.right_missing_fragments.len(), 1); }
#[test]
fn diff_remote_superset() {
let shared = vec![make_commit(1), make_commit(2)];
let remote_extra = vec![make_commit(3)];
let local = Sedimentree::new(vec![], shared.clone());
let remote = Sedimentree::new(vec![], [shared, remote_extra].concat());
let remote_summary = remote.summarize();
let diff = local.diff_remote(&remote_summary);
assert!(diff.local_commits.is_empty());
assert!(diff.local_fragments.is_empty());
assert_eq!(diff.remote_commits.len(), 1);
assert!(diff.remote_fragment_summaries.is_empty());
}
#[test]
fn diff_remote_diverged_with_overlap() {
let shared = vec![make_commit(1)];
let local_only = vec![make_commit(10)];
let remote_only = vec![make_commit(20), make_commit(21)];
let local = Sedimentree::new(vec![], [shared.clone(), local_only].concat());
let remote = Sedimentree::new(vec![], [shared, remote_only].concat());
let remote_summary = remote.summarize();
let diff = local.diff_remote(&remote_summary);
assert_eq!(diff.local_commits.len(), 1);
assert_eq!(diff.remote_commits.len(), 2);
}
mod proptests {
use alloc::vec;
use core::sync::atomic::{AtomicU64, Ordering};
use rand::{Rng, SeedableRng, rngs::SmallRng};
use crate::{blob::BlobMeta, commit::CountLeadingZeroBytes};
use super::super::*;
static SEED_COUNTER: AtomicU64 = AtomicU64::new(0);
fn hash_with_leading_zeros(zeros_count: u32) -> Digest<LooseCommit> {
let seed = SEED_COUNTER.fetch_add(1, Ordering::Relaxed);
let mut rng = SmallRng::seed_from_u64(seed);
let mut byte_arr: [u8; 32] = rng.r#gen::<[u8; 32]>();
for slot in byte_arr.iter_mut().take(zeros_count as usize) {
*slot = 0;
}
if (zeros_count as usize) < 32 {
let idx = zeros_count as usize;
#[allow(clippy::indexing_slicing)]
if byte_arr[idx] == 0 {
byte_arr[idx] = 1; }
}
Digest::from_bytes(byte_arr)
}
#[test]
fn fragment_supports_higher_levels() {
#[derive(Debug)]
struct Scenario {
deeper: Fragment,
shallower: FragmentSummary,
}
impl<'a> arbitrary::Arbitrary<'a> for Scenario {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
#[allow(clippy::enum_variant_names)]
#[derive(arbitrary::Arbitrary)]
enum ShallowerDepthType {
StartsAtStartBoundaryAtCheckpoint,
StartsAtCheckpointBoundaryAtCheckpoint,
StartsAtCheckpointBoundaryAtBoundary,
}
let start_hash = hash_with_leading_zeros(10);
let deeper_boundary_hash = hash_with_leading_zeros(10);
let shallower_start_hash: Digest<LooseCommit>;
let shallower_boundary_hash: Digest<LooseCommit>;
let mut checkpoints = Vec::<Digest<LooseCommit>>::arbitrary(u)?;
let lower_level_type = ShallowerDepthType::arbitrary(u)?;
match lower_level_type {
ShallowerDepthType::StartsAtStartBoundaryAtCheckpoint => {
shallower_start_hash = start_hash;
shallower_boundary_hash = hash_with_leading_zeros(9);
checkpoints.push(shallower_boundary_hash);
}
ShallowerDepthType::StartsAtCheckpointBoundaryAtCheckpoint => {
shallower_start_hash = hash_with_leading_zeros(9);
shallower_boundary_hash = hash_with_leading_zeros(9);
checkpoints.push(shallower_start_hash);
checkpoints.push(shallower_boundary_hash);
}
ShallowerDepthType::StartsAtCheckpointBoundaryAtBoundary => {
shallower_start_hash = hash_with_leading_zeros(9);
checkpoints.push(shallower_start_hash);
shallower_boundary_hash = deeper_boundary_hash;
}
}
let deeper = Fragment::new(
start_hash,
vec![deeper_boundary_hash],
checkpoints,
BlobMeta::arbitrary(u)?,
);
let shallower = FragmentSummary::new(
shallower_start_hash,
vec![shallower_boundary_hash],
BlobMeta::arbitrary(u)?,
);
Ok(Self { deeper, shallower })
}
}
bolero::check!().with_arbitrary::<Scenario>().for_each(
|Scenario { deeper, shallower }| {
assert!(deeper.supports(shallower, &CountLeadingZeroBytes));
},
);
}
#[test]
fn minimized_loose_commit_dag_doesnt_change() {
#[derive(Debug)]
struct Scenario {
commits: Vec<LooseCommit>,
}
impl<'a> arbitrary::Arbitrary<'a> for Scenario {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let mut frontier: Vec<Digest<LooseCommit>> = Vec::new();
let num_commits: u32 = u.int_in_range(1..=20)?;
let mut result = Vec::with_capacity(num_commits as usize);
for _ in 0..num_commits {
let contents = Vec::<u8>::arbitrary(u)?;
let blob_meta = BlobMeta::new(&contents);
let hash = Digest::<LooseCommit>::arbitrary(u)?;
let mut parents = Vec::new();
let mut num_parents = u.int_in_range(0..=frontier.len())?;
let mut parent_choices = frontier.iter().collect::<Vec<_>>();
while num_parents > 0 {
let parent = u.choose(&parent_choices)?;
parents.push(**parent);
#[allow(clippy::unwrap_used)]
parent_choices
.remove(parent_choices.iter().position(|p| p == parent).unwrap());
num_parents -= 1;
}
frontier.retain(|p| !parents.contains(p));
frontier.push(hash);
result.push(LooseCommit::new(hash, parents, blob_meta));
}
Ok(Scenario { commits: result })
}
}
bolero::check!()
.with_arbitrary::<Scenario>()
.for_each(|Scenario { commits }| {
let tree = Sedimentree::new(vec![], commits.clone());
let minimized = tree.minimize(&CountLeadingZeroBytes);
assert_eq!(tree, minimized);
});
}
#[test]
fn diff_self_is_empty() {
bolero::check!()
.with_arbitrary::<Sedimentree>()
.for_each(|tree| {
let diff = tree.diff(tree);
assert!(
diff.left_missing_fragments.is_empty(),
"self-diff should have no left missing fragments"
);
assert!(
diff.left_missing_commits.is_empty(),
"self-diff should have no left missing commits"
);
assert!(
diff.right_missing_fragments.is_empty(),
"self-diff should have no right missing fragments"
);
assert!(
diff.right_missing_commits.is_empty(),
"self-diff should have no right missing commits"
);
});
}
#[test]
fn diff_is_symmetric() {
bolero::check!()
.with_arbitrary::<(Sedimentree, Sedimentree)>()
.for_each(|(a, b)| {
let ab = a.diff(b);
let ba = b.diff(a);
assert_eq!(
ab.left_missing_fragments.len(),
ba.right_missing_fragments.len(),
"left_missing in a.diff(b) should equal right_missing in b.diff(a)"
);
assert_eq!(
ab.right_missing_fragments.len(),
ba.left_missing_fragments.len(),
"right_missing in a.diff(b) should equal left_missing in b.diff(a)"
);
assert_eq!(
ab.left_missing_commits.len(),
ba.right_missing_commits.len(),
"left_missing commits in a.diff(b) should equal right_missing in b.diff(a)"
);
assert_eq!(
ab.right_missing_commits.len(),
ba.left_missing_commits.len(),
"right_missing commits in a.diff(b) should equal left_missing in b.diff(a)"
);
});
}
#[test]
fn diff_remote_matches_diff_for_local_items() {
bolero::check!()
.with_arbitrary::<(Sedimentree, Sedimentree)>()
.for_each(|(local, remote)| {
let remote_summary = remote.summarize();
let remote_diff = local.diff_remote(&remote_summary);
let local_diff = local.diff(remote);
assert_eq!(
remote_diff.local_fragments.len(),
local_diff.right_missing_fragments.len(),
"diff_remote local_fragments should match diff right_missing_fragments"
);
assert_eq!(
remote_diff.local_commits.len(),
local_diff.right_missing_commits.len(),
"diff_remote local_commits should match diff right_missing_commits"
);
assert_eq!(
remote_diff.remote_fragment_summaries.len(),
local_diff.left_missing_fragments.len(),
"diff_remote remote_fragment_summaries count should match diff left_missing_fragments"
);
assert_eq!(
remote_diff.remote_commits.len(),
local_diff.left_missing_commits.len(),
"diff_remote remote_commits should match diff left_missing_commits"
);
});
}
#[test]
fn diff_merge_produces_equal_trees() {
bolero::check!()
.with_arbitrary::<(Sedimentree, Sedimentree)>()
.for_each(|(a, b)| {
let diff = a.diff(b);
let mut a_updated = a.clone();
let mut b_updated = b.clone();
for fragment in diff.left_missing_fragments {
a_updated.add_fragment(fragment.clone());
}
for commit in diff.left_missing_commits {
a_updated.add_commit(commit.clone());
}
for fragment in diff.right_missing_fragments {
b_updated.add_fragment(fragment.clone());
}
for commit in diff.right_missing_commits {
b_updated.add_commit(commit.clone());
}
assert_eq!(
a_updated, b_updated,
"after applying diff, trees should be equal"
);
});
}
}
}