merkle-dag/src/dag.rs

191 lines
6.5 KiB
Rust
Raw Normal View History

2022-08-05 14:29:07 -04:00
// Copyright 2022 Jeremy Wall (Jeremy@marzhilsltudios.com)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2022-08-05 14:29:07 -04:00
use std::{collections::BTreeSet, marker::PhantomData};
2022-08-05 14:29:07 -04:00
2022-08-05 14:29:07 -04:00
use crate::{
2022-08-06 20:16:08 -04:00
hash::HashWriter,
2022-08-05 14:29:07 -04:00
node::Node,
2022-08-06 20:16:08 -04:00
store::{Result, Store, StoreError},
2022-08-05 14:29:07 -04:00
};
2022-08-05 14:29:07 -04:00
/// Node comparison values. In a given Merkle DAG a Node can come `After`, `Before`, be `Equivalent`, or `Uncomparable`.
/// If the two nodes have the same id they are eqivalent. If two nodes are not part of the same sub graph within the DAG
/// then they are Uncomparable. If one node is an ancestor of another DAG then that node comes before the other. If the
/// reverse is true then that node comes after the other.
#[derive(PartialEq, Debug)]
pub enum NodeCompare {
After,
Before,
Equivalent,
Uncomparable,
}
/// A Merkle-DAG implementation. This is a modification on the standard Merkle Tree data structure
/// but instead of a tree it is a DAG and as a result can have multiple roots. A merkle-dag specifies
/// a partial ordering on all the nodes and utilizes the api to ensure that this ordering is
/// preserved during construction.
///
/// The merkle dag consists of a set of pointers to the current known roots as well as the total set
/// of nodes in the dag. Node payload items must be of a single type and implement the `ByteEncoder`
/// trait.
///
/// A merkle DAG instance is tied to a specific implementation of the HashWriter interface to ensure
/// that all hash identifiers are of the same hash algorithm.
#[derive(Clone, Debug)]
2022-08-24 10:00:08 -04:00
pub struct Merkle<S, HW>
2022-08-05 14:29:07 -04:00
where
2022-08-24 10:00:08 -04:00
HW: HashWriter,
S: Store<HW>,
2022-08-05 14:29:07 -04:00
{
2022-08-24 10:00:08 -04:00
roots: BTreeSet<Vec<u8>>,
2022-08-05 14:29:07 -04:00
nodes: S,
2022-08-24 10:00:08 -04:00
_phantom_node: PhantomData<Node<HW>>,
2022-08-05 14:29:07 -04:00
}
2022-08-24 10:00:08 -04:00
impl<S, HW> Merkle<S, HW>
2022-08-05 14:29:07 -04:00
where
2022-08-24 10:00:08 -04:00
HW: HashWriter,
S: Store<HW>,
2022-08-05 14:29:07 -04:00
{
/// Construct a new empty DAG. The empty DAG is also the default for a DAG.
pub fn new() -> Self {
Self::default()
}
/// Add a new payload with a required set of dependency_ids. This method will construct a new node
/// and add it to the DAG with the given payload item and dependency id set. It is idempotent for any
/// given set of inputs.
///
/// One result of not constructing/adding nodes in this way is that we ensure that we always satisfy
/// the implementation rule in the merkel-crdt's whitepaper.
2022-08-06 20:16:08 -04:00
pub fn add_node<'a, N: Into<Vec<u8>>>(
2022-08-05 14:29:07 -04:00
&'a mut self,
item: N,
2022-08-24 10:00:08 -04:00
dependency_ids: BTreeSet<Vec<u8>>,
) -> Result<Vec<u8>> {
let node = Node::<HW>::new(item.into(), dependency_ids.clone());
let id = node.id().to_vec();
if self.nodes.contains(id.as_slice())? {
2022-08-05 14:29:07 -04:00
// We've already added this node so there is nothing left to do.
2022-08-24 10:00:08 -04:00
return Ok(self
.nodes
.get(id.as_slice())
.unwrap()
.unwrap()
.id()
.to_vec());
2022-08-05 14:29:07 -04:00
}
2022-08-05 14:29:07 -04:00
let mut root_removals = Vec::new();
2022-08-05 14:29:07 -04:00
for dep_id in dependency_ids.iter() {
2022-08-06 20:16:08 -04:00
if !self.nodes.contains(dep_id)? {
2022-08-05 14:29:07 -04:00
return Err(StoreError::NoSuchDependents);
2022-08-05 14:29:07 -04:00
}
// If any of our dependencies is in the roots pointer list then
2022-08-05 14:29:07 -04:00
// we need to remove it below.
2022-08-05 14:29:07 -04:00
if self.roots.contains(dep_id) {
2022-08-05 14:29:07 -04:00
root_removals.push(dep_id);
2022-08-05 14:29:07 -04:00
}
}
2022-08-05 14:29:07 -04:00
self.nodes.store(node)?;
for removal in root_removals {
self.roots.remove(removal);
}
2022-08-24 10:00:08 -04:00
self.roots.insert(id.to_vec());
Ok(id.to_vec())
2022-08-05 14:29:07 -04:00
}
/// Check if we already have a copy of a node.
2022-08-24 10:00:08 -04:00
pub fn check_for_node(&self, id: &[u8]) -> Result<bool> {
2022-08-05 14:29:07 -04:00
return self.nodes.contains(id);
2022-08-05 14:29:07 -04:00
}
/// Get a node from the DAG by it's hash identifier if it exists.
2022-08-24 10:00:08 -04:00
pub fn get_node_by_id(&self, id: &[u8]) -> Result<Option<Node<HW>>> {
2022-08-05 14:29:07 -04:00
self.nodes.get(id)
}
/// Get the set of root node ids.
2022-08-24 10:00:08 -04:00
pub fn get_roots(&self) -> &BTreeSet<Vec<u8>> {
2022-08-05 14:29:07 -04:00
&self.roots
}
/// Get the map of all nodes in the DAG.
2022-08-05 14:29:07 -04:00
pub fn get_nodes(&self) -> &S {
2022-08-05 14:29:07 -04:00
&self.nodes
}
/// Compare two nodes by id in the graph. If the left id is an ancestor of the right node
/// then `returns `NodeCompare::Before`. If the right id is an ancestor of the left node
/// then returns `NodeCompare::After`. If both id's are equal then the returns
/// `NodeCompare::Equivalent`. If neither id are parts of the same subgraph then returns
/// `NodeCompare::Uncomparable`.
2022-08-24 10:00:08 -04:00
pub fn compare(&self, left: &[u8], right: &[u8]) -> Result<NodeCompare> {
2022-08-06 20:16:08 -04:00
Ok(if left == right {
2022-08-05 14:29:07 -04:00
NodeCompare::Equivalent
} else {
// Is left node an ancestor of right node?
2022-08-06 20:16:08 -04:00
if self.search_graph(right, left)? {
2022-08-05 14:29:07 -04:00
NodeCompare::Before
// is right node an ancestor of left node?
2022-08-06 20:16:08 -04:00
} else if self.search_graph(left, right)? {
2022-08-05 14:29:07 -04:00
NodeCompare::After
} else {
NodeCompare::Uncomparable
}
2022-08-06 20:16:08 -04:00
})
2022-08-05 14:29:07 -04:00
}
2022-08-24 10:00:08 -04:00
fn search_graph(&self, root_id: &[u8], search_id: &[u8]) -> Result<bool> {
2022-08-05 14:29:07 -04:00
if root_id == search_id {
2022-08-06 20:16:08 -04:00
return Ok(true);
2022-08-05 14:29:07 -04:00
}
2022-08-06 20:16:08 -04:00
let root_node = match self.get_node_by_id(root_id)? {
2022-08-05 14:29:07 -04:00
Some(n) => n,
None => {
2022-08-06 20:16:08 -04:00
return Ok(false);
2022-08-05 14:29:07 -04:00
}
};
let mut stack = vec![root_node];
while !stack.is_empty() {
let node = stack.pop().unwrap();
let deps = node.dependency_ids();
for dep in deps {
if search_id == dep {
2022-08-06 20:16:08 -04:00
return Ok(true);
2022-08-05 14:29:07 -04:00
}
2022-08-06 20:16:08 -04:00
stack.push(match self.get_node_by_id(dep)? {
2022-08-05 14:29:07 -04:00
Some(n) => n,
None => panic!("Invalid DAG STATE encountered"),
})
}
}
2022-08-06 20:16:08 -04:00
return Ok(false);
2022-08-05 14:29:07 -04:00
}
}
2022-08-24 10:00:08 -04:00
impl<S, HW> Default for Merkle<S, HW>
2022-08-05 14:29:07 -04:00
where
2022-08-24 10:00:08 -04:00
HW: HashWriter,
S: Store<HW>,
2022-08-05 14:29:07 -04:00
{
fn default() -> Self {
Self {
roots: BTreeSet::new(),
2022-08-05 14:29:07 -04:00
nodes: S::default(),
_phantom_node: Default::default(),
2022-08-05 14:29:07 -04:00
}
}
}