#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(
clippy::dbg_macro,
clippy::expect_used,
clippy::missing_const_for_fn,
clippy::panic,
clippy::todo,
clippy::unwrap_used,
future_incompatible,
let_underscore,
missing_copy_implementations,
missing_debug_implementations,
missing_docs,
nonstandard_style,
rust_2021_compatibility
)]
#![deny(
clippy::all,
clippy::cargo,
clippy::pedantic,
rust_2018_idioms,
unreachable_pub,
unused_extern_crates
)]
#![forbid(unsafe_code)]
use std::{
collections::{BTreeMap, BTreeSet, HashSet},
fmt::Formatter,
str::FromStr,
};
use blob::{BlobMeta, Digest};
use depth::{Depth, DepthMetric, MAX_STRATA_DEPTH};
pub mod blob;
pub mod commit;
mod commit_dag;
pub mod depth;
pub mod future;
pub mod storage;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SedimentreeId([u8; 32]);
impl SedimentreeId {
#[must_use]
pub const fn new(id: [u8; 32]) -> Self {
Self(id)
}
#[must_use]
pub const fn from_bytes(bytes: [u8; 32]) -> Self {
Self(bytes)
}
}
#[derive(Debug, Clone, Copy)]
pub struct BadSedimentreeId;
impl FromStr for SedimentreeId {
type Err = BadSedimentreeId;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if !s.len().is_multiple_of(2) {
return Err(BadSedimentreeId);
}
let bytes = (0..s.len())
.step_by(2)
.map(|i| u8::from_str_radix(&s[i..i + 2], 16).map_err(|_| BadSedimentreeId))
.collect::<Result<Vec<u8>, BadSedimentreeId>>()?;
if bytes.len() == 32 {
let mut arr = [0; 32];
arr.copy_from_slice(&bytes);
Ok(SedimentreeId(arr))
} else {
Err(BadSedimentreeId)
}
}
}
impl std::fmt::Debug for SedimentreeId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for byte in self.0 {
write!(f, "{byte:02x}")?;
}
Ok(())
}
}
impl std::fmt::Display for SedimentreeId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
for byte in self.0 {
write!(f, "{byte:02x}")?;
}
Ok(())
}
}
#[derive(Clone, Debug, PartialEq, Eq, Default, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SedimentreeSummary {
fragment_summaries: BTreeSet<FragmentSummary>,
commits: BTreeSet<LooseCommit>,
}
impl SedimentreeSummary {
#[must_use]
pub const fn new(
fragment_summaries: BTreeSet<FragmentSummary>,
commits: BTreeSet<LooseCommit>,
) -> SedimentreeSummary {
SedimentreeSummary {
fragment_summaries,
commits,
}
}
#[must_use]
pub const fn fragment_summaries(&self) -> &BTreeSet<FragmentSummary> {
&self.fragment_summaries
}
#[must_use]
pub const fn loose_commits(&self) -> &BTreeSet<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, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Fragment {
summary: FragmentSummary,
checkpoints: Vec<Digest>,
digest: Digest,
}
impl Fragment {
#[must_use]
pub fn new(
head: Digest,
boundary: Vec<Digest>,
checkpoints: Vec<Digest>,
blob_meta: BlobMeta,
) -> Self {
let digest = {
let mut hasher = blake3::Hasher::new();
hasher.update(head.as_bytes());
for end in &boundary {
hasher.update(end.as_bytes());
}
hasher.update(blob_meta.digest().as_bytes());
for checkpoint in &checkpoints {
hasher.update(checkpoint.as_bytes());
}
Digest::from(*hasher.finalize().as_bytes())
};
Self {
summary: FragmentSummary {
head,
boundary,
blob_meta,
},
checkpoints,
digest,
}
}
#[must_use]
pub fn supports<M: DepthMetric>(&self, other: &FragmentSummary, hash_metric: &M) -> bool {
if &self.summary == other {
return true;
}
if self.depth(hash_metric) < other.depth(hash_metric) {
return false;
}
if self.summary.head == other.head
&& self
.checkpoints
.iter()
.collect::<HashSet<_>>()
.is_superset(&other.boundary.iter().collect::<HashSet<_>>())
{
return true;
}
if self.checkpoints.contains(&other.head)
&& other
.boundary
.iter()
.all(|end| self.checkpoints.contains(end))
{
return true;
}
if self.checkpoints.contains(&other.head)
&& self
.summary
.boundary
.iter()
.collect::<HashSet<_>>()
.is_superset(&other.boundary.iter().collect::<HashSet<_>>())
{
return true;
}
false
}
#[must_use]
pub fn supports_block(&self, fragment_end: Digest) -> bool {
self.checkpoints.contains(&fragment_end) || self.summary.boundary.contains(&fragment_end)
}
#[must_use]
pub const fn summary(&self) -> &FragmentSummary {
&self.summary
}
#[must_use]
pub fn depth<M: DepthMetric>(&self, hash_metric: &M) -> Depth {
self.summary.depth(hash_metric)
}
#[must_use]
pub const fn head(&self) -> Digest {
self.summary.head
}
#[must_use]
pub const fn boundary(&self) -> &[Digest] {
self.summary.boundary.as_slice()
}
#[must_use]
pub const fn checkpoints(&self) -> &Vec<Digest> {
&self.checkpoints
}
#[must_use]
pub const fn digest(&self) -> Digest {
self.digest
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FragmentSummary {
head: Digest,
boundary: Vec<Digest>,
blob_meta: BlobMeta,
}
impl FragmentSummary {
#[must_use]
pub const fn new(head: Digest, boundary: Vec<Digest>, blob_meta: BlobMeta) -> Self {
Self {
head,
boundary,
blob_meta,
}
}
#[must_use]
pub const fn head(&self) -> Digest {
self.head
}
#[must_use]
pub const fn boundary(&self) -> &[Digest] {
self.boundary.as_slice()
}
#[must_use]
pub const fn blob_meta(&self) -> BlobMeta {
self.blob_meta
}
#[must_use]
pub fn depth<M: DepthMetric>(&self, hash_metric: &M) -> Depth {
hash_metric.to_depth(self.head)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct LooseCommit {
digest: Digest,
parents: Vec<Digest>,
blob: BlobMeta,
}
impl LooseCommit {
#[must_use]
pub const fn new(digest: Digest, parents: Vec<Digest>, blob: BlobMeta) -> Self {
Self {
digest,
parents,
blob,
}
}
#[must_use]
pub const fn digest(&self) -> Digest {
self.digest
}
#[must_use]
pub const fn parents(&self) -> &Vec<Digest> {
&self.parents
}
#[must_use]
pub const fn blob(&self) -> &BlobMeta {
&self.blob
}
}
#[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, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct Sedimentree {
fragments: BTreeSet<Fragment>,
commits: BTreeSet<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(),
}
}
#[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| {
std::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_fragment(&mut self, fragment: Fragment) -> bool {
self.fragments.insert(fragment)
}
pub fn add_commit(&mut self, commit: LooseCommit) -> bool {
self.commits.insert(commit)
}
#[must_use]
pub fn diff<'a>(&'a self, other: &'a Sedimentree) -> Diff<'a> {
let our_fragments = self.fragments.iter().collect::<HashSet<_>>();
let their_fragments = other.fragments.iter().collect();
let left_missing_fragments = our_fragments.difference(&their_fragments);
let right_missing_fragments = their_fragments.difference(&our_fragments);
let our_commits = self.commits.iter().collect::<HashSet<_>>();
let their_commits = other.commits.iter().collect();
let left_missing_commits = our_commits.difference(&their_commits);
let right_missing_commits = their_commits.difference(&our_commits);
Diff {
left_missing_fragments: left_missing_fragments.into_iter().copied().collect(),
left_missing_commits: left_missing_commits.into_iter().copied().collect(),
right_missing_fragments: right_missing_fragments.into_iter().copied().collect(),
right_missing_commits: right_missing_commits.into_iter().copied().collect(),
}
}
#[must_use]
pub fn diff_remote<'a, M: DepthMetric>(
&'a self,
remote: &'a SedimentreeSummary,
hash_metric: &M,
) -> RemoteDiff<'a> {
let our_fragments_meta = self
.fragments
.iter()
.map(|s| &s.summary)
.collect::<HashSet<&FragmentSummary>>();
let their_fragments = remote.fragment_summaries.iter().collect::<HashSet<_>>();
let mut local_fragments = Vec::new();
for m in our_fragments_meta.difference(&their_fragments) {
for s in &self.fragments {
if s.head() == m.head
&& *s.boundary() == m.boundary
&& s.depth(hash_metric) == m.depth(hash_metric)
{
local_fragments.push(s);
break;
}
}
}
let remote_fragments = their_fragments.difference(&our_fragments_meta);
let our_commits = self.commits.iter().collect::<HashSet<&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) -> bool {
self.loose_commits().any(|c| c.digest() == digest)
}
#[must_use]
pub fn has_fragment_starting_with<M: DepthMetric>(
&self,
digest: Digest,
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> {
let minimized = self.minimize(depth_metric);
let dag = commit_dag::CommitDag::from_commits(minimized.commits.iter());
let mut heads = Vec::<Digest>::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> {
let dag = commit_dag::CommitDag::from_commits(self.commits.iter());
let mut runs_by_level = BTreeMap::<Depth, (Digest, Vec<Digest>)>::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 >= crate::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 {
id,
head,
boundary: vec![commit_hash],
checkpoints: checkpoints.clone(),
});
}
}
}
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)]
pub struct FragmentSpec {
id: SedimentreeId,
head: Digest,
checkpoints: Vec<Digest>,
boundary: Vec<Digest>,
}
impl FragmentSpec {
#[must_use]
pub const fn id(&self) -> SedimentreeId {
self.id
}
#[must_use]
pub const fn head(&self) -> Digest {
self.head
}
#[must_use]
pub const fn boundary(&self) -> &[Digest] {
self.boundary.as_slice()
}
#[must_use]
pub const fn checkpoints(&self) -> &Vec<Digest> {
&self.checkpoints
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CommitOrFragment {
Commit(LooseCommit),
Fragment(Fragment),
}
impl std::fmt::Debug for Sedimentree {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::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>, 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 rand::Rng;
use crate::{blob::BlobMeta, commit::CountLeadingZeroBytes};
use super::*;
fn hash_with_leading_zeros(zeros_count: u32) -> Digest {
let mut byte_arr: [u8; 32] = rand::rng().random::<[u8; 32]>();
for slot in byte_arr.iter_mut().take(zeros_count as usize) {
*slot = 0;
}
Digest::from(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;
let shallower_boundary_hash: Digest;
let mut checkpoints = Vec::<Digest>::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<super::LooseCommit>,
}
impl<'a> arbitrary::Arbitrary<'a> for Scenario {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let mut frontier: Vec<Digest> = 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 = BlobMeta::new(&contents);
let hash = crate::Digest::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(super::LooseCommit {
digest: hash,
parents,
blob,
});
}
Ok(Scenario { commits: result })
}
}
bolero::check!()
.with_arbitrary::<Scenario>()
.for_each(|Scenario { commits }| {
let tree = super::Sedimentree::new(vec![], commits.clone());
let minimized = tree.minimize(&CountLeadingZeroBytes);
assert_eq!(tree, minimized);
});
}
}