Skip to content
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

Path and Identifier validation and improvements #22

Merged
merged 27 commits into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f5678a0
First pass at using validate_identifier in store.rs
seanchen1991 Oct 8, 2021
dac68ff
Call `validate_identifier` in `Identifier::is_valid`
seanchen1991 Oct 12, 2021
1df1bc0
Remove unnecessary `TraceError` modifier
seanchen1991 Oct 12, 2021
cbdb015
Merge branch 'seanchen1991-restructure'
seanchen1991 Oct 13, 2021
ee4d113
Add some simple property tests
seanchen1991 Oct 13, 2021
b1673de
Add some more proptests
seanchen1991 Oct 13, 2021
b0b4c50
Remove debug print statement
seanchen1991 Oct 13, 2021
4efd7b8
Merge branch 'main' of github.com:informalsystems/basecoin-rs
seanchen1991 Oct 14, 2021
cd228d2
Ensure that generated chars are ASCII
seanchen1991 Oct 14, 2021
8e4dec3
Merge branch 'main' of github.com:informalsystems/basecoin-rs
seanchen1991 Nov 9, 2021
9a9c434
Merge branch 'main' of github.com:informalsystems/basecoin-rs
seanchen1991 Nov 22, 2021
4a8a29b
First pass at using validate_identifier in store.rs
seanchen1991 Oct 8, 2021
02c3029
Call `validate_identifier` in `Identifier::is_valid`
seanchen1991 Oct 12, 2021
374e404
Remove unnecessary `TraceError` modifier
seanchen1991 Oct 12, 2021
6b8ff6b
Add some simple property tests
seanchen1991 Oct 13, 2021
734d1ea
Fix conflicts while rebasing
seanchen1991 Nov 22, 2021
aacfaae
Remove debug print statement
seanchen1991 Oct 13, 2021
f24bdf0
Ensure that generated chars are ASCII
seanchen1991 Oct 14, 2021
6e59b5b
Fix conflicts while pulling
seanchen1991 Nov 22, 2021
db9b47a
Allow unused imports to appease clippy
seanchen1991 Nov 22, 2021
8c8ad90
Implement Path as a collection of Identifiers
hu55a1n1 Dec 2, 2021
b66625a
Polish Display impl for Path
hu55a1n1 Dec 2, 2021
93ac739
Fix AsBytes trait
hu55a1n1 Dec 2, 2021
82021fb
Use ibc Paths
hu55a1n1 Dec 2, 2021
40fec64
Clippy happy
hu55a1n1 Dec 2, 2021
a38bc23
Merge branch 'phase4' into path-identifier-cleanup
hu55a1n1 Dec 2, 2021
002de7a
Merge branch 'main' into path-identifier-cleanup
hu55a1n1 Dec 9, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix AsBytes trait
  • Loading branch information
hu55a1n1 committed Dec 2, 2021
commit 93ac739d786efc2522818bc1762d1e4b34ae4a66
42 changes: 28 additions & 14 deletions src/app/store/avl/as_bytes.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,63 @@
//! # AsBytes trait definition
//!
//! This module hosts the `AsBytes` traits, which is used by the AVL Tree to convert value to raw
//! This module hosts the `AsBytes` trait, which is used by the AVL Tree to convert value to raw
//! bytes. This is helpful for making the AVL Tree generic over a wide range of data types for its
//! keys (the values still need to implement `Borrow<[u8]>), as long as they can be interpreted as
//! a slice of bytes.
//!
//! To add support for a new type in the AVL Tree, simply implement the `AsByte` trait for that type.

pub enum ByteSlice<'a> {
Slice(&'a [u8]),
Vector(Vec<u8>),
}

impl AsRef<[u8]> for ByteSlice<'_> {
fn as_ref(&self) -> &[u8] {
match self {
ByteSlice::Slice(s) => *s,
ByteSlice::Vector(v) => v.as_slice(),
}
}
}

/// A trait for objects that can be interpreted as a slice of bytes.
pub trait AsBytes {
fn as_bytes(&self) -> &[u8];
fn as_bytes(&self) -> ByteSlice;
}

impl AsBytes for Vec<u8> {
fn as_bytes(&self) -> &[u8] {
self.as_slice()
fn as_bytes(&self) -> ByteSlice {
ByteSlice::Slice(self)
}
}

impl AsBytes for [u8] {
fn as_bytes(&self) -> &[u8] {
self
fn as_bytes(&self) -> ByteSlice {
ByteSlice::Slice(self)
}
}

impl AsBytes for str {
fn as_bytes(&self) -> &[u8] {
self.as_bytes()
fn as_bytes(&self) -> ByteSlice {
ByteSlice::Slice(self.as_bytes())
}
}

impl AsBytes for &str {
fn as_bytes(&self) -> &[u8] {
(*self).as_bytes()
fn as_bytes(&self) -> ByteSlice {
ByteSlice::Slice((*self).as_bytes())
}
}

impl AsBytes for String {
fn as_bytes(&self) -> &[u8] {
self.as_bytes()
fn as_bytes(&self) -> ByteSlice {
ByteSlice::Slice(self.as_bytes())
}
}

impl AsBytes for [u8; 1] {
fn as_bytes(&self) -> &[u8] {
self
fn as_bytes(&self) -> ByteSlice {
ByteSlice::Slice(self)
}
}
2 changes: 1 addition & 1 deletion src/app/store/avl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use tendermint::hash::Algorithm;

pub use as_bytes::AsBytes;
pub use as_bytes::{AsBytes, ByteSlice};
pub use node::AvlNode;
pub use proof::get_proof_spec;
pub use tree::AvlTree;
Expand Down
2 changes: 1 addition & 1 deletion src/app/store/avl/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ where
fn new(key: K, value: V) -> Self {
let mut sha = Sha256::new();
sha.update(proof::LEAF_PREFIX);
sha.update(key.as_bytes());
sha.update(key.as_bytes().as_ref());
sha.update(value.borrow());
let hash = sha.finalize();
let merkle_hash = Hash::from_bytes(HASH_ALGO, &Sha256::digest(&hash)).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/app/store/avl/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ where
prefix: proof::LEAF_PREFIX.to_vec(),
});
let proof = ExistenceProof {
key: node.key.as_bytes().to_owned(),
key: node.key.as_bytes().as_ref().to_owned(),
value: node.value.borrow().to_owned(),
leaf,
path: vec![],
Expand Down
14 changes: 9 additions & 5 deletions src/app/store/memory.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::app::store::avl::{AsBytes, AvlTree};
use crate::app::store::avl::{AsBytes, AvlTree, ByteSlice};
use crate::app::store::{Height, Path, ProvableStore, Store};

use ics23::CommitmentProof;
Expand Down Expand Up @@ -81,7 +81,12 @@ impl Store for InMemoryStore {
self.pending
.get_keys()
.into_iter()
.filter_map(|key| key.as_bytes().starts_with(key_prefix).then(|| key.clone()))
.filter_map(|key| {
key.as_bytes()
.as_ref()
.starts_with(key_prefix.as_ref())
.then(|| key.clone())
})
.collect()
}
}
Expand All @@ -101,9 +106,8 @@ impl ProvableStore for InMemoryStore {
}

impl AsBytes for Path {
fn as_bytes(&self) -> &[u8] {
// self.as_str().as_bytes()
todo!()
fn as_bytes(&self) -> ByteSlice {
ByteSlice::Vector(self.to_string().into_bytes())
}
}

Expand Down