Skip to content

Commit

Permalink
Merge branch 'main' into tx_receipt
Browse files Browse the repository at this point in the history
  • Loading branch information
jbcaron committed Feb 27, 2024
2 parents da8ed06 + 3118703 commit 5023778
Show file tree
Hide file tree
Showing 33 changed files with 1,146 additions and 2,644 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ git # Madara Changelog

## Next release

- 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
43 changes: 16 additions & 27 deletions Cargo.lock

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

178 changes: 88 additions & 90 deletions Cargo.toml

Large diffs are not rendered by default.

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
58 changes: 49 additions & 9 deletions crates/client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use std::marker::PhantomData;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use bonsai_db::BonsaiDb;
use bonsai_db::{BonsaiDb, TrieColumn};
use da_db::DaDb;
use l1_handler_tx_fee::L1HandlerTxFeeDb;
use mapping_db::MappingDb;
Expand All @@ -56,7 +56,7 @@ pub(crate) mod columns {
// ===== /!\ ===================================================================================
// MUST BE INCREMENTED WHEN A NEW COLUMN IN ADDED
// ===== /!\ ===================================================================================
pub const NUM_COLUMNS: u32 = 10;
pub const NUM_COLUMNS: u32 = 18;

pub const META: u32 = 0;
pub const BLOCK_MAPPING: u32 = 1;
Expand All @@ -79,8 +79,23 @@ pub(crate) mod columns {
/// This column stores the fee paid on l1 for L1Handler transactions
pub const L1_HANDLER_PAID_FEE: u32 = 8;

/// This column contains the bonsai trie keys
pub const BONSAI: u32 = 9;
/// The bonsai columns are triplicated since we need to set a column for
///
/// const TRIE_LOG_CF: &str = "trie_log";
/// const TRIE_CF: &str = "trie";
/// const FLAT_CF: &str = "flat";
/// as defined in https://github.com/keep-starknet-strange/bonsai-trie/blob/oss/src/databases/rocks_db.rs
///
/// For each tries CONTRACTS, CLASSES and STORAGE
pub const TRIE_BONSAI_CONTRACTS: u32 = 9;
pub const FLAT_BONSAI_CONTRACTS: u32 = 10;
pub const LOG_BONSAI_CONTRACTS: u32 = 11;
pub const TRIE_BONSAI_CLASSES: u32 = 12;
pub const FLAT_BONSAI_CLASSES: u32 = 13;
pub const LOG_BONSAI_CLASSES: u32 = 14;
pub const TRIE_BONSAI_STORAGE: u32 = 15;
pub const FLAT_BONSAI_STORAGE: u32 = 16;
pub const LOG_BONSAI_STORAGE: u32 = 17;
}

pub mod static_keys {
Expand All @@ -89,6 +104,14 @@ pub mod static_keys {
pub const LAST_SYNCED_L1_EVENT_BLOCK: &[u8] = b"LAST_SYNCED_L1_EVENT_BLOCK";
}

/// The Bonsai databases backend
#[derive(Clone)]
pub struct BonsaiDbs<B: BlockT> {
pub contract: Arc<BonsaiDb<B>>,
pub class: Arc<BonsaiDb<B>>,
pub storage: Arc<BonsaiDb<B>>,
}

/// The Madara client database backend
///
/// Contains five distinct databases: `meta`, `mapping`, `messaging`, `da` and `bonsai``.
Expand All @@ -104,7 +127,7 @@ pub struct Backend<B: BlockT> {
messaging: Arc<MessagingDb>,
sierra_classes: Arc<SierraClassesDb>,
l1_handler_paid_fee: Arc<L1HandlerTxFeeDb>,
bonsai: Arc<BonsaiDb<B>>,
bonsai: BonsaiDbs<B>,
}

/// Returns the Starknet database directory.
Expand Down Expand Up @@ -143,14 +166,24 @@ impl<B: BlockT> Backend<B> {
let kvdb: Arc<dyn KeyValueDB> = db.0;
let spdb: Arc<dyn Database<DbHash>> = db.1;

let bonsai_dbs = BonsaiDbs {
contract: Arc::new(BonsaiDb {
db: kvdb.clone(),
_marker: PhantomData,
current_column: TrieColumn::Contract,
}),
class: Arc::new(BonsaiDb { db: kvdb.clone(), _marker: PhantomData, current_column: TrieColumn::Class }),
storage: Arc::new(BonsaiDb { db: kvdb, _marker: PhantomData, current_column: TrieColumn::Storage }),
};

Ok(Self {
mapping: Arc::new(MappingDb::new(spdb.clone(), cache_more_things)),
meta: Arc::new(MetaDb { db: spdb.clone(), _marker: PhantomData }),
da: Arc::new(DaDb { db: spdb.clone() }),
messaging: Arc::new(MessagingDb { db: spdb.clone() }),
sierra_classes: Arc::new(SierraClassesDb { db: spdb.clone() }),
l1_handler_paid_fee: Arc::new(L1HandlerTxFeeDb { db: spdb.clone() }),
bonsai: Arc::new(BonsaiDb { db: kvdb, _marker: PhantomData }),
bonsai: bonsai_dbs,
})
}

Expand Down Expand Up @@ -179,9 +212,16 @@ impl<B: BlockT> Backend<B> {
&self.sierra_classes
}

/// Return the bonsai database manager
pub fn bonsai(&self) -> &Arc<BonsaiDb<B>> {
&self.bonsai
pub fn bonsai_contract(&self) -> &Arc<BonsaiDb<B>> {
&self.bonsai.contract
}

pub fn bonsai_class(&self) -> &Arc<BonsaiDb<B>> {
&self.bonsai.class
}

pub fn bonsai_storage(&self) -> &Arc<BonsaiDb<B>> {
&self.bonsai.storage
}

/// Return l1 handler tx paid fee database manager
Expand Down
2 changes: 1 addition & 1 deletion crates/client/deoxys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ serde = { workspace = true, default-features = true }
tokio = { workspace = true, features = ["macros", "parking_lot", "test-util"] }
url = { workspace = true }
validator = { workspace = true, features = ["derive"] }
crossbeam-skiplist ={ workspace = true }

madara-runtime = { workspace = true }
parity-scale-codec = { workspace = true, features = ["derive"] }
Expand All @@ -63,7 +64,6 @@ mc-commitment-state-diff = { workspace = true }
mc-rpc-core = { workspace = true }
mc-storage = { workspace = true }
mp-block = { workspace = true }
mp-commitments = { workspace = true }
mp-contract = { workspace = true }
mp-fee = { workspace = true }
mp-felt = { workspace = true }
Expand Down
Loading

0 comments on commit 5023778

Please sign in to comment.