-
Notifications
You must be signed in to change notification settings - Fork 38
Replace H256 with the one in rust-numext #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
joii2020
wants to merge
16
commits into
nervosnetwork:master
Choose a base branch
from
joii2020:dev_rep_H256
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ff3ef11
Replace H256 with the one in rust-numext
joii2020 d3e48c3
Delete class H256 completely, use numext H256 instead.
joii2020 70c2fd2
Repair warning, delete two unused codes
joii2020 fcb8ac4
The last time the format was changed, the CI problem has been resolved
joii2020 dff1e56
Remove H256 from lib.rs and change it to SmtH256 to avoid confusion; …
joii2020 fc0a843
Revert "Remove H256 from lib.rs and change it to SmtH256 to avoid con…
joii2020 fbf439b
The struce H256 is marked as pub(crate) and fix CI
joii2020 79a677f
merge H256Ord to h256.rs to avoid code duplication
joii2020 02985a2
Replace several iter with into_iter,
joii2020 071c201
revert new_ckb_smt in smt.rs file _new_ckb_smt
joii2020 dd466f5
H256Ord use Deref to return H256
joii2020 e015964
Use a function to implement H256 sorting instead of using a class
joii2020 89b7e85
remove std in features
joii2020 da76840
Remove two unreasonable codes:
joii2020 103801e
Remove the "extern crate std" in test.rs
joii2020 018c5f3
Organize the code, remove some unnecessary code
joii2020 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,5 +66,4 @@ impl core::fmt::Display for Error { | |
} | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
impl std::error::Error for Error {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,48 @@ | ||
use core::cmp::Ordering; | ||
use numext_fixed_hash; | ||
|
||
/// Represent 256 bits | ||
#[derive(Eq, PartialEq, Debug, Default, Hash, Clone, Copy)] | ||
pub struct H256([u8; 32]); | ||
pub(crate) type H256 = numext_fixed_hash::H256; | ||
TheWaWaR marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const ZERO: H256 = H256([0u8; 32]); | ||
const BYTE_SIZE: u8 = 8; | ||
|
||
impl H256 { | ||
pub const fn zero() -> Self { | ||
ZERO | ||
} | ||
|
||
pub fn is_zero(&self) -> bool { | ||
self == &ZERO | ||
} | ||
|
||
#[inline] | ||
pub fn get_bit(&self, i: u8) -> bool { | ||
let byte_pos = i / BYTE_SIZE; | ||
let bit_pos = i % BYTE_SIZE; | ||
let bit = self.0[byte_pos as usize] >> bit_pos & 1; | ||
bit != 0 | ||
} | ||
|
||
#[inline] | ||
pub fn set_bit(&mut self, i: u8) { | ||
let byte_pos = i / BYTE_SIZE; | ||
let bit_pos = i % BYTE_SIZE; | ||
self.0[byte_pos as usize] |= 1 << bit_pos as u8; | ||
} | ||
|
||
#[inline] | ||
pub fn clear_bit(&mut self, i: u8) { | ||
let byte_pos = i / BYTE_SIZE; | ||
let bit_pos = i % BYTE_SIZE; | ||
self.0[byte_pos as usize] &= !((1 << bit_pos) as u8); | ||
} | ||
|
||
#[inline] | ||
pub fn is_right(&self, height: u8) -> bool { | ||
self.get_bit(height) | ||
} | ||
|
||
pub fn as_slice(&self) -> &[u8] { | ||
&self.0[..] | ||
} | ||
|
||
/// Treat H256 as a path in a tree | ||
/// fork height is the number of common bits(from heigher to lower: 255..=0) of two H256 | ||
pub fn fork_height(&self, key: &H256) -> u8 { | ||
for h in (0..=core::u8::MAX).rev() { | ||
if self.get_bit(h) != key.get_bit(h) { | ||
return h; | ||
} | ||
pub fn fork_height(data: &H256, key: &H256) -> u8 { | ||
for h in (0..=core::u8::MAX).rev() { | ||
if data.bit(h.into()).unwrap_or(false) != key.bit(h.into()).unwrap_or(false) { | ||
return h; | ||
} | ||
0 | ||
} | ||
0 | ||
} | ||
|
||
/// Treat H256 as a path in a tree | ||
/// return parent_path of self | ||
pub fn parent_path(&self, height: u8) -> Self { | ||
if height == core::u8::MAX { | ||
H256::zero() | ||
} else { | ||
self.copy_bits(height + 1) | ||
} | ||
pub fn parent_path(data: &H256, height: u8) -> H256 { | ||
if height == core::u8::MAX { | ||
H256::empty() | ||
} else { | ||
copy_bits(data, height + 1) | ||
} | ||
} | ||
|
||
/// Copy bits and return a new H256 | ||
pub fn copy_bits(&self, start: u8) -> Self { | ||
let mut target = H256::zero(); | ||
|
||
let start_byte = (start / BYTE_SIZE) as usize; | ||
// copy bytes | ||
target.0[start_byte..].copy_from_slice(&self.0[start_byte..]); | ||
/// Copy bits and return a new H256 | ||
pub fn copy_bits(data: &H256, start: u8) -> H256 { | ||
// It can also be implemented with And, but the performance is not as good as this | ||
let mut target = H256::empty(); | ||
|
||
// reset remain bytes | ||
let remain = start % BYTE_SIZE; | ||
if remain > 0 { | ||
target.0[start_byte] &= 0b11111111 << remain | ||
} | ||
let start_byte = (start / BYTE_SIZE) as usize; | ||
// copy bytes | ||
target.0[start_byte..].copy_from_slice(&data.0[start_byte..]); | ||
|
||
target | ||
// reset remain bytes | ||
let remain = start % BYTE_SIZE; | ||
if remain > 0 { | ||
target.0[start_byte] &= 0b11111111 << remain | ||
} | ||
} | ||
|
||
impl PartialOrd for H256 { | ||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
Some(self.cmp(other)) | ||
} | ||
} | ||
|
||
impl Ord for H256 { | ||
fn cmp(&self, other: &Self) -> Ordering { | ||
// Compare bits from heigher to lower (255..0) | ||
self.0.iter().rev().cmp(other.0.iter().rev()) | ||
} | ||
target | ||
} | ||
|
||
impl From<[u8; 32]> for H256 { | ||
fn from(v: [u8; 32]) -> H256 { | ||
H256(v) | ||
} | ||
pub(crate) fn smt_sort_unstable(v: &mut Vec<H256>) { | ||
(*v).sort_unstable_by(|k1, k2| k1.0.iter().rev().cmp(k2.0.iter().rev())); | ||
} | ||
|
||
impl Into<[u8; 32]> for H256 { | ||
fn into(self: H256) -> [u8; 32] { | ||
self.0 | ||
} | ||
pub(crate) fn smt_sort_unstable_kv(v: &mut Vec<(H256, H256)>) { | ||
v.sort_unstable_by(|(k1, _v1), (k2, _v2)| k1.0.iter().rev().cmp(k2.0.iter().rev())); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
numext-fixed-hash
breaks the no-std feature. We should introduce it as optional, if user choosefeature=std
then enable thenumext_fixed_hash::H256
, otherwise we use the H256([u8; 32]).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we had agreed earlier, that
no_std
feature here does not make any sense, it's best we remove it and we should only leverage C based verifier in smart contract? Has anything changed since then?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we decided to remove
no_std
support, we should actually remove thestd
feature fromfeatures
section.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With #16 coming I do suggest we remove
no_std
support. @joii2020 can you make the change per @jjyr's suggestion?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK