mirror of
https://github.com/pezkuwichain/merkle-mountain-range.git
synced 2026-04-22 03:17:57 +00:00
feat: support no_std
This commit is contained in:
@@ -7,8 +7,13 @@ license = "MIT"
|
|||||||
description = "A generalized merkle mountain range implementation"
|
description = "A generalized merkle mountain range implementation"
|
||||||
repository = "https://github.com/nervosnetwork/merkle-mountain-range"
|
repository = "https://github.com/nervosnetwork/merkle-mountain-range"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = ["std"]
|
||||||
|
std = []
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
failure = "0.1.5"
|
failure = "0.1.5"
|
||||||
|
cfg-if = "0.1"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
faster-hex = "0.3"
|
faster-hex = "0.3"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
default: ci
|
default: ci
|
||||||
|
|
||||||
ci: fmt clippy test bench-test
|
ci: fmt check-no-std clippy test bench-test
|
||||||
|
|
||||||
test:
|
test:
|
||||||
cargo test --all --all-features
|
cargo test --all --all-features
|
||||||
@@ -13,3 +13,6 @@ clippy:
|
|||||||
|
|
||||||
fmt:
|
fmt:
|
||||||
cargo fmt --all -- --check
|
cargo fmt --all -- --check
|
||||||
|
|
||||||
|
check-no-std:
|
||||||
|
cargo check --all --no-default-features
|
||||||
|
|||||||
+2
-2
@@ -1,5 +1,5 @@
|
|||||||
pub use failure::Fail;
|
pub use failure::Fail;
|
||||||
pub type Result<T> = ::std::result::Result<T, Error>;
|
pub type Result<T> = core::result::Result<T, Error>;
|
||||||
|
|
||||||
#[derive(Fail, Debug, PartialEq, Eq, Clone)]
|
#[derive(Fail, Debug, PartialEq, Eq, Clone)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
@@ -8,5 +8,5 @@ pub enum Error {
|
|||||||
#[fail(display = "Inconsistent store")]
|
#[fail(display = "Inconsistent store")]
|
||||||
InconsistentStore,
|
InconsistentStore,
|
||||||
#[fail(display = "Store error {}", _0)]
|
#[fail(display = "Store error {}", _0)]
|
||||||
StoreError(String),
|
StoreError(crate::string::String),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use crate::vec::Vec;
|
||||||
|
|
||||||
pub fn leaf_index_to_pos(index: u64) -> u64 {
|
pub fn leaf_index_to_pos(index: u64) -> u64 {
|
||||||
if index == 0 {
|
if index == 0 {
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
+17
@@ -1,3 +1,5 @@
|
|||||||
|
#![cfg_attr(not(feature = "std"), no_std)]
|
||||||
|
|
||||||
mod error;
|
mod error;
|
||||||
mod helper;
|
mod helper;
|
||||||
mod merge;
|
mod merge;
|
||||||
@@ -12,3 +14,18 @@ pub use helper::{leaf_index_to_mmr_size, leaf_index_to_pos};
|
|||||||
pub use merge::Merge;
|
pub use merge::Merge;
|
||||||
pub use mmr::{MerkleProof, MMR};
|
pub use mmr::{MerkleProof, MMR};
|
||||||
pub use mmr_store::MMRStore;
|
pub use mmr_store::MMRStore;
|
||||||
|
|
||||||
|
cfg_if::cfg_if! {
|
||||||
|
if #[cfg(feature = "std")] {
|
||||||
|
use std::borrow;
|
||||||
|
use std::collections;
|
||||||
|
use std::vec;
|
||||||
|
use std::string;
|
||||||
|
} else {
|
||||||
|
extern crate alloc;
|
||||||
|
use alloc::borrow;
|
||||||
|
use alloc::collections;
|
||||||
|
use alloc::vec;
|
||||||
|
use alloc::string;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+4
-3
@@ -4,12 +4,13 @@
|
|||||||
//! https://github.com/mimblewimble/grin/blob/master/doc/mmr.md#structure
|
//! https://github.com/mimblewimble/grin/blob/master/doc/mmr.md#structure
|
||||||
//! https://github.com/mimblewimble/grin/blob/0ff6763ee64e5a14e70ddd4642b99789a1648a32/core/src/core/pmmr.rs#L606
|
//! https://github.com/mimblewimble/grin/blob/0ff6763ee64e5a14e70ddd4642b99789a1648a32/core/src/core/pmmr.rs#L606
|
||||||
|
|
||||||
|
use crate::borrow::Cow;
|
||||||
use crate::helper::{get_peaks, parent_offset, pos_height_in_tree, sibling_offset};
|
use crate::helper::{get_peaks, parent_offset, pos_height_in_tree, sibling_offset};
|
||||||
use crate::mmr_store::{MMRBatch, MMRStore};
|
use crate::mmr_store::{MMRBatch, MMRStore};
|
||||||
|
use crate::vec::Vec;
|
||||||
use crate::{Error, Merge, Result};
|
use crate::{Error, Merge, Result};
|
||||||
use std::borrow::Cow;
|
use core::fmt::Debug;
|
||||||
use std::fmt::Debug;
|
use core::marker::PhantomData;
|
||||||
use std::marker::PhantomData;
|
|
||||||
|
|
||||||
pub struct MMR<T, M, S: MMRStore<T>> {
|
pub struct MMR<T, M, S: MMRStore<T>> {
|
||||||
mmr_size: u64,
|
mmr_size: u64,
|
||||||
|
|||||||
+2
-2
@@ -1,4 +1,4 @@
|
|||||||
use crate::Result;
|
use crate::{vec::Vec, Result};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct MMRBatch<Elem, Store: MMRStore<Elem>> {
|
pub struct MMRBatch<Elem, Store: MMRStore<Elem>> {
|
||||||
@@ -45,7 +45,7 @@ impl<Elem: Clone, Store: MMRStore<Elem>> MMRBatch<Elem, Store> {
|
|||||||
|
|
||||||
impl<Elem, Store: MMRStore<Elem>> IntoIterator for MMRBatch<Elem, Store> {
|
impl<Elem, Store: MMRStore<Elem>> IntoIterator for MMRBatch<Elem, Store> {
|
||||||
type Item = (u64, Vec<Elem>);
|
type Item = (u64, Vec<Elem>);
|
||||||
type IntoIter = ::std::vec::IntoIter<Self::Item>;
|
type IntoIter = crate::vec::IntoIter<Self::Item>;
|
||||||
|
|
||||||
fn into_iter(self) -> Self::IntoIter {
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
self.memory_batch.into_iter()
|
self.memory_batch.into_iter()
|
||||||
|
|||||||
+7
-7
@@ -1,11 +1,11 @@
|
|||||||
use crate::{MMRStore, Merge, MerkleProof, Result, MMR};
|
use crate::collections::BTreeMap;
|
||||||
use std::cell::RefCell;
|
use crate::{vec::Vec, MMRStore, Merge, MerkleProof, Result, MMR};
|
||||||
use std::collections::HashMap;
|
use core::cell::RefCell;
|
||||||
use std::fmt::Debug;
|
use core::fmt::Debug;
|
||||||
use std::marker::PhantomData;
|
use core::marker::PhantomData;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct MemStore<T>(RefCell<HashMap<u64, T>>);
|
pub struct MemStore<T>(RefCell<BTreeMap<u64, T>>);
|
||||||
|
|
||||||
impl<T> Default for MemStore<T> {
|
impl<T> Default for MemStore<T> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
@@ -15,7 +15,7 @@ impl<T> Default for MemStore<T> {
|
|||||||
|
|
||||||
impl<T> MemStore<T> {
|
impl<T> MemStore<T> {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
MemStore(RefCell::new(HashMap::new()))
|
MemStore(RefCell::new(Default::default()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user