Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(multi-trie): Added support for persistent storage tries #19

Merged
merged 11 commits into from
Mar 18, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ git # Deoxys Changelog

## Next release

- feat(multi-trie): Added support for persistent storage tries
- feat(pending): added support for pending blocks in RPC requests
- perf(l2 sync): parallel fetching of blocks, classes, state updates
- fix l1 thread to reflect correct state_root, block_number, block_hash
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ pallet-grandpa = { default-features = true, git = "https://github.com/massalabs/
pallet-timestamp = { default-features = true, git = "https://github.com/massalabs/polkadot-sdk", branch = "release-polkadot-v1.3.0-std" }

# Bonsai trie dependencies
bonsai-trie = { default-features = false, git = "https://github.com/trantorian1/bonsai-trie.git", branch = "oss" }
bonsai-trie = { default-features = false, git = "https://github.com/antiyro/bonsai-trie.git", branch = "oss" }

# Madara pallets
pallet-starknet = { path = "crates/pallets/starknet", default-features = false, features = [
Expand Down
14 changes: 12 additions & 2 deletions crates/client/db/src/bonsai_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::error::BonsaiDbError;
pub enum TrieColumn {
Class,
Contract,
ContractStorage,
}

#[derive(Debug)]
Expand All @@ -24,20 +25,24 @@ pub enum KeyType {

pub struct BonsaiConfigs<B: BlockT> {
pub contract: BonsaiStorage<BasicId, BonsaiDb<B>, Pedersen>,
pub contract_storage: BonsaiStorage<BasicId, BonsaiDb<B>, Pedersen>,
pub class: BonsaiStorage<BasicId, BonsaiDb<B>, Poseidon>,
}

impl<B: BlockT> BonsaiConfigs<B> {
pub fn new(contract: BonsaiDb<B>, class: BonsaiDb<B>) -> Self {
pub fn new(contract: BonsaiDb<B>, contract_storage: BonsaiDb<B>, class: BonsaiDb<B>) -> Self {
let config = BonsaiStorageConfig::default();

let contract =
BonsaiStorage::<_, _, Pedersen>::new(contract, config.clone()).expect("Failed to create bonsai storage");

let contract_storage = BonsaiStorage::<_, _, Pedersen>::new(contract_storage, config.clone())
.expect("Failed to create bonsai storage");

let class =
BonsaiStorage::<_, _, Poseidon>::new(class, config.clone()).expect("Failed to create bonsai storage");

Self { contract, class }
Self { contract, contract_storage, class }
}
}

Expand All @@ -54,6 +59,11 @@ impl TrieColumn {
KeyType::Flat => crate::columns::FLAT_BONSAI_CONTRACTS,
KeyType::TrieLog => crate::columns::LOG_BONSAI_CONTRACTS,
},
TrieColumn::ContractStorage => match key_type {
KeyType::Trie => crate::columns::TRIE_BONSAI_CONTRACTS,
KeyType::Flat => crate::columns::FLAT_BONSAI_CONTRACTS,
KeyType::TrieLog => crate::columns::LOG_BONSAI_CONTRACTS,
},
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion crates/client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ pub struct Backend<B: BlockT> {
sierra_classes: Arc<SierraClassesDb>,
l1_handler_paid_fee: Arc<L1HandlerTxFeeDb>,
bonsai_contract: Arc<Mutex<BonsaiStorage<BasicId, BonsaiDb<B>, Pedersen>>>,
bonsai_contract_storage: Arc<Mutex<BonsaiStorage<BasicId, BonsaiDb<B>, Pedersen>>>,
bonsai_class: Arc<Mutex<BonsaiStorage<BasicId, BonsaiDb<B>, Poseidon>>>,
}

Expand Down Expand Up @@ -172,8 +173,10 @@ impl<B: BlockT> Backend<B> {
let spdb: Arc<dyn Database<DbHash>> = db.1;

let contract = BonsaiDb { db: kvdb.clone(), _marker: PhantomData, current_column: TrieColumn::Contract };
let contract_storage =
BonsaiDb { db: kvdb.clone(), _marker: PhantomData, current_column: TrieColumn::ContractStorage };
let class = BonsaiDb { db: kvdb.clone(), _marker: PhantomData, current_column: TrieColumn::Class };
let config = BonsaiConfigs::new(contract, class);
let config = BonsaiConfigs::new(contract, contract_storage, class);

Ok(Self {
mapping: Arc::new(MappingDb::new(spdb.clone(), cache_more_things)),
Expand All @@ -183,6 +186,7 @@ impl<B: BlockT> Backend<B> {
sierra_classes: Arc::new(SierraClassesDb { db: spdb.clone() }),
l1_handler_paid_fee: Arc::new(L1HandlerTxFeeDb { db: spdb.clone() }),
bonsai_contract: Arc::new(Mutex::new(config.contract)),
bonsai_contract_storage: Arc::new(Mutex::new(config.contract_storage)),
bonsai_class: Arc::new(Mutex::new(config.class)),
})
}
Expand Down Expand Up @@ -216,6 +220,10 @@ impl<B: BlockT> Backend<B> {
&self.bonsai_contract
}

pub fn bonsai_contract_storage(&self) -> &Arc<Mutex<BonsaiStorage<BasicId, BonsaiDb<B>, Pedersen>>> {
&self.bonsai_contract_storage
}

pub fn bonsai_class(&self) -> &Arc<Mutex<BonsaiStorage<BasicId, BonsaiDb<B>, Poseidon>>> {
&self.bonsai_class
}
Expand Down
Loading
Loading