Skip to content

Commit

Permalink
Merge pull request #134 from KasarLabs/feat/root
Browse files Browse the repository at this point in the history
✨ feat(root): Introducing state root and other commitments using Bonsai Tries
  • Loading branch information
antiyro authored Feb 24, 2024
2 parents 3b43b3b + 7106452 commit 3f00b99
Show file tree
Hide file tree
Showing 30 changed files with 866 additions and 2,489 deletions.
32 changes: 5 additions & 27 deletions Cargo.lock

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

5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ members = [
"crates/pallets/starknet/runtime_api/",
"crates/primitives/block",
"crates/primitives/chain-id",
"crates/primitives/commitments",
"crates/primitives/contract",
"crates/primitives/convert",
"crates/primitives/digest-log",
Expand Down Expand Up @@ -46,7 +45,6 @@ default-members = [
"crates/pallets/starknet/runtime_api/",
"crates/primitives/block",
"crates/primitives/chain-id",
"crates/primitives/commitments",
"crates/primitives/digest-log",
"crates/primitives/fee",
"crates/primitives/felt",
Expand Down Expand Up @@ -225,7 +223,6 @@ pallet-starknet-runtime-api = { path = "crates/pallets/starknet/runtime_api", de
# Madara primtitives
mp-block = { path = "crates/primitives/block", default-features = false }
mp-chain-id = { path = "crates/primitives/chain-id", default-features = false }
mp-commitments = { path = "crates/primitives/commitments", default-features = false }
mp-contract = { path = "crates/primitives/contract", default-features = false }
mp-convert = { path = "crates/primitives/convert", default-features = false }
mp-digest-log = { path = "crates/primitives/digest-log", default-features = false }
Expand Down Expand Up @@ -271,7 +268,7 @@ starknet-crypto = { git = "https://github.com/jbcaron/starknet-rs.git", branch =
starknet-ff = { git = "https://github.com/jbcaron/starknet-rs.git", branch = "classes", default-features = false }
starknet-providers = { git = "https://github.com/jbcaron/starknet-rs.git", branch = "classes", default-features = false }
starknet-signers = { git = "https://github.com/jbcaron/starknet-rs.git", branch = "classes", default-features = false }
starknet-types-core = { version = "0.0.7", default-features = false }
starknet-types-core = { git = "https://github.com/starknet-io/types-rs", branch = "main", default-features = false }

blockifier = { git = "https://github.com/massalabs/blockifier", branch = "no_std-support-7578442-std", default-features = false, features = [
"parity-scale-codec",
Expand Down
67 changes: 61 additions & 6 deletions crates/client/db/src/bonsai_db.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::default;
use std::marker::PhantomData;
use std::sync::atomic::AtomicU32;
use std::sync::Arc;

use bonsai_trie::id::Id;
Expand All @@ -8,12 +10,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(bytes) => return KeyType::Trie,
DatabaseKey::Flat(bytes) => return KeyType::Flat,
DatabaseKey::TrieLog(bytes) => return KeyType::TrieLog,
}
}

impl<B: BlockT> BonsaiDatabase for &BonsaiDb<B> {
Expand All @@ -27,7 +75,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 +88,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 +107,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 +134,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 +152,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
59 changes: 50 additions & 9 deletions crates/client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ mod meta_db;

use std::marker::PhantomData;
use std::path::{Path, PathBuf};
use std::sync::atomic::AtomicU32;
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 +57,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 +80,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 +105,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 +128,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 +167,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 +213,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
1 change: 0 additions & 1 deletion crates/client/deoxys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,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 3f00b99

Please sign in to comment.