Skip to content

Commit

Permalink
cleaned refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
antiyro committed Mar 4, 2024
2 parents 1e90064 + 82bad26 commit b919713
Show file tree
Hide file tree
Showing 48 changed files with 1,878 additions and 2,970 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ starknet-e2e-test/contracts/build
# vscode settings
.vscode/settings.json

# script files
output_deoxys.json
output_pathfinder.json

tmp/
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ git # Deoxys Changelog

## Next release

- feat(rpc): add_invoke_tx, add_deploy_account_tx, add_declare_tx
- feat(rpc): tx_receipt, re-execute tx
- feat(script): added CI scripts for starting Deoxys and comparing JSON RPC
calls
- perf(verify_l2): parallelized l2 state root update
- perf(state_commitment): parallelized state commitment hash computations
- fix(L1): fix l1 thread with battle tested implementation + removed l1-l2
- fix: update and store ConfigFetch in l2 sync(), chainId rpc call
- fix: get_events paging with continuation_token
- fux(getStorageAt): #28
- fix(class): #125
- fix(getStorageAt): #28
- fix(genesis): #107
- fix(class): #32 #33 #34
- fix(class): #116
Expand Down
44 changes: 17 additions & 27 deletions Cargo.lock

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

180 changes: 89 additions & 91 deletions Cargo.toml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!-- markdownlint-disable -->
<div align="center">
<img src="https://github.com/KasarLabs/brand/blob/main/projects/deoxys/logo.png?raw=true" height="200" style="border-radius: 15px;">
<img src="https://github.com/KasarLabs/brand/blob/main/projects/deoxys/Full/GradientFullWhite.png?raw=true" height="200" style="border-radius: 15px;">
</div>
<div align="center">
<br />
Expand Down
4 changes: 2 additions & 2 deletions crates/client/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ repository = "https://github.com/keep-starknet-strange/madara"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
bonsai-trie = { workspace = true }
ethers = { workspace = true }
kvdb = "0.13.0"
kvdb-rocksdb = { version = "0.19.0", optional = true }
log = { workspace = true, default-features = true }
parity-db = { version = "0.4.12", optional = true }
Expand All @@ -32,8 +34,6 @@ starknet_api = { workspace = true, default-features = true, features = [
] }
thiserror = { workspace = true }
uuid = "1.4.1"
bonsai-trie = { workspace = true }
kvdb = "0.13.0"

[features]
default = ["kvdb-rocksdb", "parity-db"]
65 changes: 59 additions & 6 deletions crates/client/db/src/bonsai_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,58 @@ use sp_runtime::traits::Block as BlockT;

use crate::error::BonsaiDbError;

#[derive(Debug)]
pub enum TrieColumn {
Class,
Contract,
Storage,
}

#[derive(Debug)]
pub enum KeyType {
Trie,
Flat,
TrieLog,
}

impl TrieColumn {
pub fn to_index(&self, key_type: KeyType) -> u32 {
match self {
TrieColumn::Class => match key_type {
KeyType::Trie => crate::columns::TRIE_BONSAI_CLASSES,
KeyType::Flat => crate::columns::FLAT_BONSAI_CLASSES,
KeyType::TrieLog => crate::columns::LOG_BONSAI_CLASSES,
},
TrieColumn::Contract => match key_type {
KeyType::Trie => crate::columns::TRIE_BONSAI_CONTRACTS,
KeyType::Flat => crate::columns::FLAT_BONSAI_CONTRACTS,
KeyType::TrieLog => crate::columns::LOG_BONSAI_CONTRACTS,
},
TrieColumn::Storage => match key_type {
KeyType::Trie => crate::columns::TRIE_BONSAI_STORAGE,
KeyType::Flat => crate::columns::FLAT_BONSAI_STORAGE,
KeyType::TrieLog => crate::columns::LOG_BONSAI_STORAGE,
},
}
}
}

/// Represents a Bonsai database instance parameterized by a block type.
pub struct BonsaiDb<B: BlockT> {
/// Database interface for key-value operations.
pub(crate) db: Arc<dyn KeyValueDB>,
/// PhantomData to mark the block type used.
pub(crate) _marker: PhantomData<B>,
/// Set current column to give trie context
pub(crate) current_column: TrieColumn,
}

pub fn key_type(key: &DatabaseKey) -> KeyType {
match key {
DatabaseKey::Trie(_) => return KeyType::Trie,
DatabaseKey::Flat(_) => return KeyType::Flat,
DatabaseKey::TrieLog(_) => return KeyType::TrieLog,
}
}

impl<B: BlockT> BonsaiDatabase for &BonsaiDb<B> {
Expand All @@ -27,7 +73,8 @@ impl<B: BlockT> BonsaiDatabase for &BonsaiDb<B> {

/// Retrieves a value by its database key.
fn get(&self, key: &DatabaseKey) -> Result<Option<Vec<u8>>, Self::DatabaseError> {
let column = crate::columns::BONSAI;
let key_type = key_type(key);
let column = self.current_column.to_index(key_type);
let key_slice = key.as_slice();
self.db.get(column, key_slice).map_err(Into::into)
}
Expand All @@ -39,7 +86,9 @@ impl<B: BlockT> BonsaiDatabase for &BonsaiDb<B> {
value: &[u8],
batch: Option<&mut Self::Batch>,
) -> Result<Option<Vec<u8>>, Self::DatabaseError> {
let column = crate::columns::BONSAI;
// println!("Key and keytype: {:?} {:?}", self.current_column, key_type(key));
let key_type = key_type(key);
let column = self.current_column.to_index(key_type);
let key_slice = key.as_slice();
let previous_value = self.db.get(column, key_slice)?;

Expand All @@ -56,14 +105,16 @@ impl<B: BlockT> BonsaiDatabase for &BonsaiDb<B> {

/// Checks if a key exists in the database.
fn contains(&self, key: &DatabaseKey) -> Result<bool, Self::DatabaseError> {
let column = crate::columns::BONSAI;
let key_type = key_type(key);
let column = self.current_column.to_index(key_type);
let key_slice = key.as_slice();
self.db.has_key(column, key_slice).map_err(Into::into)
}

/// Retrieves all key-value pairs starting with a given prefix.
fn get_by_prefix(&self, prefix: &DatabaseKey) -> Result<Vec<(Vec<u8>, Vec<u8>)>, Self::DatabaseError> {
let column = crate::columns::BONSAI;
let key_type = key_type(prefix);
let column = self.current_column.to_index(key_type);
let prefix_slice = prefix.as_slice();
let mut result = Vec::new();

Expand All @@ -81,7 +132,8 @@ impl<B: BlockT> BonsaiDatabase for &BonsaiDb<B> {
key: &DatabaseKey,
batch: Option<&mut Self::Batch>,
) -> Result<Option<Vec<u8>>, Self::DatabaseError> {
let column = crate::columns::BONSAI;
let key_type = key_type(key);
let column = self.current_column.to_index(key_type);
let key_slice = key.as_slice();
let previous_value = self.db.get(column, key_slice)?;

Expand All @@ -98,7 +150,8 @@ impl<B: BlockT> BonsaiDatabase for &BonsaiDb<B> {

/// Removes all key-value pairs starting with a given prefix.
fn remove_by_prefix(&mut self, prefix: &DatabaseKey) -> Result<(), Self::DatabaseError> {
let column = crate::columns::BONSAI;
let key_type = key_type(prefix);
let column = self.current_column.to_index(key_type);
let prefix_slice = prefix.as_slice();
let mut transaction = self.create_batch();
transaction.delete_prefix(column, prefix_slice);
Expand Down
Loading

0 comments on commit b919713

Please sign in to comment.