Formatting and docstrings

This commit is contained in:
Jeremy Wall 2022-08-24 13:10:59 -04:00
parent 6e40f07f28
commit 45486b5cc7
8 changed files with 26 additions and 5 deletions

View File

@ -11,6 +11,9 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
//! Implements the HashWriter interface for the Blake2 hash function.
//! Requires the `blake2` feature to be set.
use crate::hash::*; use crate::hash::*;
use blake2::digest::Digest; use blake2::digest::Digest;
pub use blake2::{Blake2b512, Blake2s256}; pub use blake2::{Blake2b512, Blake2s256};

View File

@ -11,6 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
//! Implementation of the MerkleDag based off of the merkle-crdt whitepaper.
use std::{collections::BTreeSet, marker::PhantomData}; use std::{collections::BTreeSet, marker::PhantomData};
@ -68,8 +69,8 @@ where
/// and add it to the DAG with the given payload item and dependency id set. It is idempotent for any /// and add it to the DAG with the given payload item and dependency id set. It is idempotent for any
/// given set of inputs. /// given set of inputs.
/// ///
/// One result of not constructing/adding nodes in this way is that we ensure that we always satisfy /// One result of not constructing and then adding nodes in this way is that we ensure that we always
/// the implementation rule in the merkel-crdt's whitepaper. /// satisfy the implementation rule in the merkel-crdt's whitepaper.
pub fn add_node<'a, N: Into<Vec<u8>>>( pub fn add_node<'a, N: Into<Vec<u8>>>(
&'a mut self, &'a mut self,
item: N, item: N,

View File

@ -11,6 +11,8 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
//! HashWriter trait specification and default implementations.
use std::collections::hash_map::DefaultHasher; use std::collections::hash_map::DefaultHasher;
use std::hash::Hasher; use std::hash::Hasher;

View File

@ -11,6 +11,9 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
//! Module implementing a store interface using LevelDB for a MerkleDag.
//! Requires the `rusty-leveldb` interface.
use std::cell::RefCell; use std::cell::RefCell;
use std::path::Path; use std::path::Path;
@ -23,8 +26,9 @@ use crate::blake2::*;
use ciborium; use ciborium;
use rusty_leveldb; use rusty_leveldb;
// TODO(jwall): Add leveldb backing store for a Merkle-DAG /// A `Store` implementation using the rusty-leveldb port of leveldb.
/// The Default implementation of this `Default::default()` is an in-memory
/// implementation of the store.
pub struct LevelStore { pub struct LevelStore {
store: RefCell<rusty_leveldb::DB>, store: RefCell<rusty_leveldb::DB>,
} }
@ -37,6 +41,7 @@ impl LevelStore {
}) })
} }
} }
impl Store<Blake2b512> for LevelStore { impl Store<Blake2b512> for LevelStore {
fn contains(&self, id: &[u8]) -> Result<bool> { fn contains(&self, id: &[u8]) -> Result<bool> {
Ok(self.store.borrow_mut().get(id).is_some()) Ok(self.store.borrow_mut().get(id).is_some())

View File

@ -11,6 +11,8 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
//! A merkle dag along the lines of the merkle-crdt whitepaper.
#[cfg(feature = "blake2")] #[cfg(feature = "blake2")]
pub mod blake2; pub mod blake2;
pub mod dag; pub mod dag;

View File

@ -11,6 +11,8 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
//! `Node` types for satisfying the properties necessary for a MerkleDag.
use std::{collections::BTreeSet, marker::PhantomData}; use std::{collections::BTreeSet, marker::PhantomData};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};

View File

@ -11,7 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
//! Exports the most common set of types for this crate.
pub use crate::dag::*; pub use crate::dag::*;
pub use crate::hash::*; pub use crate::hash::*;
pub use crate::node::*; pub use crate::node::*;

View File

@ -11,6 +11,8 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
//! The MerkleDag backing store trait.
use std::collections::BTreeMap; use std::collections::BTreeMap;
use crate::{hash::HashWriter, node::Node}; use crate::{hash::HashWriter, node::Node};
@ -23,12 +25,16 @@ pub enum StoreError {
NoSuchDependents, NoSuchDependents,
} }
/// Trait representing the backing storage interface for a `DAG`.
pub trait Store<HW>: Default pub trait Store<HW>: Default
where where
HW: HashWriter, HW: HashWriter,
{ {
/// Checks if the `Store` contains a node with this id.
fn contains(&self, id: &[u8]) -> Result<bool>; fn contains(&self, id: &[u8]) -> Result<bool>;
/// Fetches a node from the `Store` by id if it exists.
fn get(&self, id: &[u8]) -> Result<Option<Node<HW>>>; fn get(&self, id: &[u8]) -> Result<Option<Node<HW>>>;
/// Stores a given node.
fn store(&mut self, node: Node<HW>) -> Result<()>; fn store(&mut self, node: Node<HW>) -> Result<()>;
} }