Skip to content

Commit

Permalink
impl suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLucqs committed Jan 25, 2024
1 parent 73a3a80 commit 41b53a9
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 40 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/check_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ on:
- oss


name: Check and Lint and wasm
name: Check and Lint and no-std

jobs:
build-wasm:
name: Build wasm
build-no-std:
name: Build no-std
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install rust
working-directory: ./ensure_no_std
run: rustup show
- name: Build wasm
- name: Build no-std
run: cargo build

fmt:
Expand Down Expand Up @@ -49,12 +49,12 @@ jobs:
- name: Clippy default features
run: cargo clippy

clippy-wasm:
clippy-no-std:
name: Clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install rust
run: rustup show
- name: Clippy wasm
- name: Clippy no-std
run: cargo clippy --no-default-features
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
tree;
zip -0 ccov.zip `find . \( -name "$PROJECT_NAME_UNDERSCORE*.gc*" \) -print`;
grcov ccov.zip -s . -t lcov --llvm --ignore-not-existing --ignore "/*" --ignore "tests/*" -o lcov.info;
cargo test $CARGO_OPTIONS --no-default-features
- name: Upload test results
uses: EnricoMi/publish-unit-test-result-action@v1
with:
Expand Down
8 changes: 7 additions & 1 deletion ensure_no_std/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/bonsai_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ impl KeyType<'_> {
/// Trait to be implemented on any type that can be used as a database.
pub trait BonsaiDatabase {
type Batch: Default;
#[cfg(feature = "std")]
type DatabaseError: std::error::Error + Into<BonsaiStorageError>;
#[cfg(not(feature = "std"))]
type DatabaseError: Into<BonsaiStorageError>;

/// Create a new empty batch of changes to be used in `insert`, `remove` and applied in database using `write_batch`.
Expand Down Expand Up @@ -78,6 +81,9 @@ pub trait BonsaiDatabase {
}

pub trait BonsaiPersistentDatabase<ID: Id> {
#[cfg(feature = "std")]
type DatabaseError: std::error::Error + Into<BonsaiStorageError>;
#[cfg(not(feature = "std"))]
type DatabaseError: Into<BonsaiStorageError>;
type Transaction: BonsaiDatabase;
/// Save a snapshot of the current database state
Expand Down
35 changes: 9 additions & 26 deletions src/databases/hashmap_db.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
use crate::{
bonsai_database::BonsaiPersistentDatabase, error::BonsaiStorageError, id::Id, BonsaiDatabase,
};
#[cfg(not(feature = "std"))]
use alloc::{
fmt,
fmt::Display,
vec::Vec,
{collections::BTreeMap, string::ToString},
};
use core::{fmt, fmt::Display};
#[cfg(not(feature = "std"))]
use hashbrown::HashMap;
#[cfg(feature = "std")]
use std::{
collections::{BTreeMap, HashMap},
fmt,
fmt::Display,
};

use crate::{
bonsai_database::BonsaiPersistentDatabase, error::BonsaiStorageError, id::Id, BonsaiDatabase,
};
use std::collections::{BTreeMap, HashMap};

#[derive(Debug)]
pub struct HashMapDbError {}

#[cfg(feature = "std")]
impl std::error::Error for HashMapDbError {}

impl Display for HashMapDbError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "")
Expand All @@ -34,25 +31,11 @@ impl From<HashMapDbError> for BonsaiStorageError {
}

#[derive(Clone, Default)]
pub struct HashMapDbConfig {}

#[derive(Clone)]
pub struct HashMapDb<ID: Id> {
config: HashMapDbConfig,
db: HashMap<Vec<u8>, Vec<u8>>,
snapshots: BTreeMap<ID, HashMapDb<ID>>,
}

impl<ID: Id> HashMapDb<ID> {
pub fn new(config: HashMapDbConfig) -> Self {
Self {
config,
db: HashMap::new(),
snapshots: BTreeMap::new(),
}
}
}

impl<ID: Id> BonsaiDatabase for HashMapDb<ID> {
type Batch = ();
type DatabaseError = HashMapDbError;
Expand Down Expand Up @@ -122,7 +105,7 @@ impl<ID: Id> BonsaiDatabase for HashMapDb<ID> {

#[cfg(test)]
fn dump_database(&self) {
println!("{:?}", self.db);
log::debug!("{:?}", self.db);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/databases/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(dead_code)]
mod hashmap_db;
pub use hashmap_db::{HashMapDb, HashMapDbConfig};
pub use hashmap_db::HashMapDb;

#[cfg(feature = "rocksdb")]
mod rocks_db;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! This implementation can be used with any database that implements the `BonsaiDatabase` trait.
//!
//! Example usage with a RocksDB database:
//! ```
//! ```ignore
//! # use bonsai_trie::{
//! # databases::{RocksDB, create_rocks_db, RocksDBConfig},
//! # BonsaiStorageError,
Expand Down
1 change: 1 addition & 0 deletions src/tests/madara_comparison.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "std")]
use bitvec::{bits, order::Msb0, vec::BitVec};
use starknet_types_core::{felt::Felt, hash::Pedersen};

Expand Down
7 changes: 2 additions & 5 deletions src/tests/proof.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
#[cfg(feature = "std")]
use std::collections::HashMap;
#[cfg(not(feature = "std"))]
use {alloc::string::String, hashbrown::HashMap};

#![cfg(feature = "std")]
use bitvec::vec::BitVec;
use pathfinder_common::{hash::PedersenHash, trie::TrieNode};
use pathfinder_crypto::Felt as PathfinderFelt;
use pathfinder_merkle_tree::tree::{MerkleTree, TestStorage};
use pathfinder_storage::{Node, StoredNode};
use rand::Rng;
use starknet_types_core::{felt::Felt, hash::Pedersen};
use std::collections::HashMap;

use crate::{
databases::{create_rocks_db, RocksDB, RocksDBConfig},
Expand Down
1 change: 1 addition & 0 deletions src/tests/simple.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "std")]
use crate::{
databases::{create_rocks_db, RocksDB, RocksDBConfig},
id::BasicIdBuilder,
Expand Down
1 change: 1 addition & 0 deletions src/tests/transactional_state.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "std")]
use crate::{
databases::{create_rocks_db, RocksDB, RocksDBConfig},
id::BasicIdBuilder,
Expand Down
1 change: 1 addition & 0 deletions src/tests/trie_log.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "std")]
use crate::{
databases::{create_rocks_db, RocksDB, RocksDBConfig},
id::BasicIdBuilder,
Expand Down

0 comments on commit 41b53a9

Please sign in to comment.