From b5dd28bac3d10239eefb13f1504c059fe26abf42 Mon Sep 17 00:00:00 2001 From: LucasLvy Date: Thu, 25 Jan 2024 11:23:56 +0100 Subject: [PATCH] impl suggestions --- .github/workflows/check_lint.yml | 12 +++++------ .github/workflows/test.yml | 1 + .gitignore | 3 ++- ensure_no_std/Cargo.lock | 8 +++++++- src/bonsai_database.rs | 6 ++++++ src/databases/hashmap_db.rs | 35 ++++++++------------------------ src/databases/mod.rs | 2 +- src/lib.rs | 2 +- src/tests/madara_comparison.rs | 1 + src/tests/proof.rs | 7 ++----- src/tests/simple.rs | 1 + src/tests/transactional_state.rs | 1 + src/tests/trie_log.rs | 1 + 13 files changed, 39 insertions(+), 41 deletions(-) diff --git a/.github/workflows/check_lint.yml b/.github/workflows/check_lint.yml index 17df54c..f95f00b 100644 --- a/.github/workflows/check_lint.yml +++ b/.github/workflows/check_lint.yml @@ -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: @@ -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 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2265daa..251c4dd 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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: diff --git a/.gitignore b/.gitignore index b1dec57..baa0341 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ target/ rocksdb/ .vscode/ -documentation/node_modules/ \ No newline at end of file +documentation/node_modules/ +Cargo.lock diff --git a/ensure_no_std/Cargo.lock b/ensure_no_std/Cargo.lock index c273b59..05af383 100644 --- a/ensure_no_std/Cargo.lock +++ b/ensure_no_std/Cargo.lock @@ -14,6 +14,12 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "allocator-api2" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5" + [[package]] name = "arrayvec" version = "0.7.4" @@ -57,7 +63,6 @@ dependencies = [ "log", "parity-scale-codec", "serde", - "serde_derive", "starknet-types-core", ] @@ -156,6 +161,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" dependencies = [ "ahash", + "allocator-api2", ] [[package]] diff --git a/src/bonsai_database.rs b/src/bonsai_database.rs index 6f5b3c4..0d49152 100644 --- a/src/bonsai_database.rs +++ b/src/bonsai_database.rs @@ -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; + #[cfg(not(feature = "std"))] type DatabaseError: Into; /// Create a new empty batch of changes to be used in `insert`, `remove` and applied in database using `write_batch`. @@ -78,6 +81,9 @@ pub trait BonsaiDatabase { } pub trait BonsaiPersistentDatabase { + #[cfg(feature = "std")] + type DatabaseError: std::error::Error + Into; + #[cfg(not(feature = "std"))] type DatabaseError: Into; type Transaction: BonsaiDatabase; /// Save a snapshot of the current database state diff --git a/src/databases/hashmap_db.rs b/src/databases/hashmap_db.rs index bf13e4c..74a2d13 100644 --- a/src/databases/hashmap_db.rs +++ b/src/databases/hashmap_db.rs @@ -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, "") @@ -34,25 +31,11 @@ impl From for BonsaiStorageError { } #[derive(Clone, Default)] -pub struct HashMapDbConfig {} - -#[derive(Clone)] pub struct HashMapDb { - config: HashMapDbConfig, db: HashMap, Vec>, snapshots: BTreeMap>, } -impl HashMapDb { - pub fn new(config: HashMapDbConfig) -> Self { - Self { - config, - db: HashMap::new(), - snapshots: BTreeMap::new(), - } - } -} - impl BonsaiDatabase for HashMapDb { type Batch = (); type DatabaseError = HashMapDbError; @@ -122,7 +105,7 @@ impl BonsaiDatabase for HashMapDb { #[cfg(test)] fn dump_database(&self) { - println!("{:?}", self.db); + log::debug!("{:?}", self.db); } } diff --git a/src/databases/mod.rs b/src/databases/mod.rs index 2f3eb0f..e73625d 100644 --- a/src/databases/mod.rs +++ b/src/databases/mod.rs @@ -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; diff --git a/src/lib.rs b/src/lib.rs index 10dfb19..8335803 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, diff --git a/src/tests/madara_comparison.rs b/src/tests/madara_comparison.rs index 07e81c5..6a525f5 100644 --- a/src/tests/madara_comparison.rs +++ b/src/tests/madara_comparison.rs @@ -1,3 +1,4 @@ +#![cfg(feature = "std")] use bitvec::{bits, order::Msb0, vec::BitVec}; use starknet_types_core::{felt::Felt, hash::Pedersen}; diff --git a/src/tests/proof.rs b/src/tests/proof.rs index 479f019..a315eda 100644 --- a/src/tests/proof.rs +++ b/src/tests/proof.rs @@ -1,8 +1,4 @@ -#[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; @@ -10,6 +6,7 @@ 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}, diff --git a/src/tests/simple.rs b/src/tests/simple.rs index ef84608..a67902f 100644 --- a/src/tests/simple.rs +++ b/src/tests/simple.rs @@ -1,3 +1,4 @@ +#![cfg(feature = "std")] use crate::{ databases::{create_rocks_db, RocksDB, RocksDBConfig}, id::BasicIdBuilder, diff --git a/src/tests/transactional_state.rs b/src/tests/transactional_state.rs index 41e2c00..84c1fd7 100644 --- a/src/tests/transactional_state.rs +++ b/src/tests/transactional_state.rs @@ -1,3 +1,4 @@ +#![cfg(feature = "std")] use crate::{ databases::{create_rocks_db, RocksDB, RocksDBConfig}, id::BasicIdBuilder, diff --git a/src/tests/trie_log.rs b/src/tests/trie_log.rs index f061e25..3145425 100644 --- a/src/tests/trie_log.rs +++ b/src/tests/trie_log.rs @@ -1,3 +1,4 @@ +#![cfg(feature = "std")] use crate::{ databases::{create_rocks_db, RocksDB, RocksDBConfig}, id::BasicIdBuilder,