mirror of
https://github.com/pezkuwichain/merkle-mountain-range.git
synced 2026-06-14 09:51:06 +00:00
Merge pull request #4 from nervosnetwork/remove-failure
refactor: remove failure and f64 for no_std
This commit is contained in:
@@ -12,7 +12,6 @@ default = ["std"]
|
|||||||
std = []
|
std = []
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
failure = "0.1.5"
|
|
||||||
cfg-if = "0.1"
|
cfg-if = "0.1"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|||||||
+19
-5
@@ -1,12 +1,26 @@
|
|||||||
pub use failure::Fail;
|
|
||||||
pub type Result<T> = core::result::Result<T, Error>;
|
pub type Result<T> = core::result::Result<T, Error>;
|
||||||
|
|
||||||
#[derive(Fail, Debug, PartialEq, Eq, Clone)]
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
#[fail(display = "Get root on an empty MMR")]
|
|
||||||
GetRootOnEmpty,
|
GetRootOnEmpty,
|
||||||
#[fail(display = "Inconsistent store")]
|
|
||||||
InconsistentStore,
|
InconsistentStore,
|
||||||
#[fail(display = "Store error {}", _0)]
|
|
||||||
StoreError(crate::string::String),
|
StoreError(crate::string::String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl core::fmt::Display for Error {
|
||||||
|
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
|
||||||
|
use Error::*;
|
||||||
|
match self {
|
||||||
|
GetRootOnEmpty => write!(f, "Get root on an empty MMR")?,
|
||||||
|
InconsistentStore => write!(f, "Inconsistent store")?,
|
||||||
|
StoreError(msg) => write!(f, "Store error {}", msg)?,
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg_if::cfg_if! {
|
||||||
|
if #[cfg(feature = "std")] {
|
||||||
|
impl ::std::error::Error for Error {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+10
-1
@@ -1,5 +1,14 @@
|
|||||||
use crate::vec::Vec;
|
use crate::vec::Vec;
|
||||||
|
|
||||||
|
fn log2(mut n: u64) -> u64 {
|
||||||
|
let mut k = 0;
|
||||||
|
while n > 1 {
|
||||||
|
k += 1;
|
||||||
|
n >>= 1;
|
||||||
|
}
|
||||||
|
k
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
||||||
@@ -10,7 +19,7 @@ pub fn leaf_index_to_pos(index: u64) -> u64 {
|
|||||||
let mut height = 0u32;
|
let mut height = 0u32;
|
||||||
while leaves > 1 {
|
while leaves > 1 {
|
||||||
// get heighest peak height
|
// get heighest peak height
|
||||||
height = (leaves as f64).log2() as u32;
|
height = log2(leaves) as u32;
|
||||||
// calculate leaves in peak
|
// calculate leaves in peak
|
||||||
let peak_leaves = 1 << height;
|
let peak_leaves = 1 << height;
|
||||||
// heighest positon
|
// heighest positon
|
||||||
|
|||||||
Reference in New Issue
Block a user