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

Added boilerplate for Bonsai storage migration #70

Merged
merged 33 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
2d5f9fb
feat(storage): started migrating Substrate contract class storage to …
Apr 10, 2024
53a1cf0
tmp
Apr 18, 2024
9242142
refactor(storage): refactored storage
Apr 18, 2024
a10743b
refactor(storage): updated bonsai trie handler
Apr 18, 2024
e12bd76
feat(storage): view contract storage now vieable at any point in the …
Apr 19, 2024
ee924bc
feat(storage): added support for `get_storage_at`
Apr 19, 2024
8e98460
feat(storage): updated `starknet/lib.rs` to use new storage primitives
Apr 19, 2024
bec64d2
feat(storage): added `get_at` function to all storage handlers
Apr 20, 2024
c8ef2ed
tmp
Apr 20, 2024
affc563
tmp
Apr 21, 2024
2dc6ed5
Merge branch 'main' into feat/storage
Apr 21, 2024
d173e25
fix(storage): udpated project to use new storage
Apr 21, 2024
081dafc
tmp
Apr 22, 2024
f60116f
fix(storage): storage handlers now hold a mutex to read again
Apr 22, 2024
1804b01
feat(storage): storage handler functions no longer return a `Result`
Apr 22, 2024
f81e0bd
Merge branch 'main' into feat/storage
Apr 22, 2024
9255220
fix(cit): fixed `clippy` and linting issues
Apr 22, 2024
5e2d5d9
fix(ci): updated `CHANGELOG.md`
Apr 22, 2024
f7cfc48
Merge branch 'main' into feat/storage
Trantorian1 Apr 22, 2024
026685f
refactor(override): removed storage overrides
Apr 22, 2024
839e981
feat(storage): storage_handler now uses rocksdb
Apr 22, 2024
ef711d1
fix(storage): it works (compiles)
Apr 23, 2024
4a32564
feat(storage): moved `ContractClassHashes` to our own rocksdb impleme…
Apr 23, 2024
588692b
perf(storage): parallelized database insertion
Apr 23, 2024
2125f1e
refactor(runtime): removed pending tx storage from the runtime
Apr 23, 2024
17e21ce
refactor(runtime): removed dead code and unused storage from the runtime
Apr 23, 2024
d9b2eb9
fix(ci): fixed various linting and clippy issues
Apr 23, 2024
0f8ce1f
feat(bench): set up basic storage benchmarks
Apr 23, 2024
99a3066
fix(state_root): fixed some issues linked to the state root
Apr 23, 2024
6d80ae0
feat(storage): commits are now done in parallel
Apr 24, 2024
ea2ac51
Merge branch 'main' into feat/storage
Trantorian1 Apr 24, 2024
c994832
fix(get_at): get_at for contract data should now work
Apr 24, 2024
7ed1672
Merge branch 'main' into feat/storage
jbcaron Apr 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(storage): started migrating storage to the bonsai-lib
- fix: l1HandlerTx computed for commit
- refactor: optimise get_events RPC
- fix(root): fixed state commitments broken due to genesis loader
Expand Down
51 changes: 20 additions & 31 deletions Cargo.lock

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

10 changes: 4 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ members = [
"crates/client/genesis-data-provider",
"crates/client/mapping-sync",
"crates/client/rpc",
"crates/client/storage",
"crates/node",
"crates/pallets/starknet",
"crates/pallets/starknet/runtime_api/",
Expand All @@ -32,7 +31,6 @@ default-members = [
"crates/client/genesis-data-provider",
"crates/client/mapping-sync",
"crates/client/rpc",
"crates/client/storage",
"crates/node",
"crates/pallets/starknet",
"crates/pallets/starknet/runtime_api/",
Expand All @@ -56,14 +54,14 @@ panic = "abort"

[profile.release]
panic = "unwind"
debug = true

[profile.production]
codegen-units = 1 # Setting this to 1 allows for more optimizations at the cost of slower compile time
inherits = "release"
lto = true # Enables Link Time Optimization, enabling more aggressive optimizations across the entire codebase
lto = "fat" # Enables Link Time Optimization, enabling more aggressive optimizations across the entire codebase
opt-level = 3 # Optimize for speed regardless of binary size or compile time
rpath = false # Disables adding rpath to the binary
strip = "symbols" # Removes debug info and symbold from final binary

[workspace.package]
authors = ["Antiyro <@antiyro>"]
Expand Down Expand Up @@ -206,7 +204,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/keep-starknet-strange/bonsai-trie.git", branch = "oss", features = [
bonsai-trie = { default-features = false, git = "https://github.com/trantorian1/bonsai-trie.git", branch = "snapshot", features = [
"std",
] }

Expand Down Expand Up @@ -240,7 +238,6 @@ mc-db = { path = "crates/client/db" }
mc-genesis-data-provider = { path = "crates/client/genesis-data-provider" }
mc-mapping-sync = { path = "crates/client/mapping-sync" }
mc-rpc = { path = "crates/client/rpc" }
mc-storage = { path = "crates/client/storage" }
mc-sync = { path = "crates/client/sync" }

# Madara runtime
Expand Down Expand Up @@ -325,6 +322,7 @@ tokio = "1.34.0"
url = "2.4.1"
rayon = "1.10.0"
arc-swap = "1.7.1"
crossbeam-skiplist = "0.1"

[patch."https://github.com/w3f/ring-vrf"]
bandersnatch_vrfs = { git = "https://github.com/w3f/ring-vrf?rev=3ddc20", version = "0.0.4", rev = "3ddc20" }
6 changes: 6 additions & 0 deletions crates/client/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ sp-database = { workspace = true, default-features = true }
sp-runtime = { workspace = true, default-features = true }

# Madara crates
mp-block = { workspace = true }
mp-contract = { workspace = true }
mp-felt = { workspace = true }
mp-hashers = { workspace = true }
mp-types = { workspace = true }

Expand All @@ -47,10 +50,13 @@ starknet_api = { workspace = true, default-features = true }
anyhow.workspace = true
arc-swap = { workspace = true }
bitvec = { workspace = true }
crossbeam-skiplist = { workspace = true }
ethers = { workspace = true }
log = { workspace = true, default-features = true }
rayon = { workspace = true }
rocksdb = { version = "0.21", features = [
# "multi-threaded-cf",
] }
thiserror = { workspace = true }
tokio = { workspace = true }
uuid = "1.4.1"
4 changes: 2 additions & 2 deletions crates/client/db/src/bonsai_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ pub type RocksDBTransaction = WriteBatchWithTransaction<true>;
pub(crate) struct DatabaseKeyMapping {
pub(crate) flat: Column,
pub(crate) trie: Column,
pub(crate) trie_log: Column,
pub(crate) log: Column,
}

impl DatabaseKeyMapping {
pub(crate) fn map(&self, key: &DatabaseKey) -> Column {
match key {
DatabaseKey::Trie(_) => self.trie,
DatabaseKey::Flat(_) => self.flat,
DatabaseKey::TrieLog(_) => self.trie_log,
DatabaseKey::TrieLog(_) => self.log,
}
}
}
Expand Down
47 changes: 37 additions & 10 deletions crates/client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,21 @@ use sc_client_db::DatabaseSource;

mod error;
mod mapping_db;
use rocksdb::{BoundColumnFamily, ColumnFamilyDescriptor, MultiThreaded, OptimisticTransactionDB, Options};
use rocksdb::{
BoundColumnFamily, ColumnFamilyDescriptor, DBCompressionType, MultiThreaded, OptimisticTransactionDB, Options,
};
mod da_db;
use starknet_api::hash::StarkHash;
use starknet_types_core::hash::{Pedersen, Poseidon};
pub mod bonsai_db;
mod l1_handler_tx_fee;
mod meta_db;
pub mod storage;
pub mod storage_handler;
pub mod storage_updates;

pub use error::{BonsaiDbError, DbError};
pub use mapping_db::MappingCommitment;
use storage_handler::bonsai_identifier;

const DB_HASH_LEN: usize = 32;
/// Hash type that this backend uses for the database.
Expand Down Expand Up @@ -79,6 +83,7 @@ pub(crate) fn open_rocksdb(path: &Path, create: bool) -> Result<OptimisticTransa
opts.create_missing_column_families(true);
opts.set_bytes_per_sync(1024 * 1024);
opts.set_keep_log_file_num(1);
opts.set_compression_type(DBCompressionType::Zstd);
let cores = std::thread::available_parallelism().map(|e| e.get() as i32).unwrap_or(1);
opts.increase_parallelism(i32::max(cores / 2, 1));

Expand All @@ -98,6 +103,11 @@ pub enum Column {
TransactionMapping,
SyncedMapping,
Da,
BlockHashToNumber,
BlockNumberToHash,
ContractClassData,
ContractData,
ContractClassHashes,

/// This column is used to map starknet block hashes to a list of transaction hashes that are
/// contained in the block.
Expand Down Expand Up @@ -151,6 +161,11 @@ impl Column {
StarknetTransactionHashesCache,
StarknetBlockHashesCache,
L1HandlerPaidFee,
BlockHashToNumber,
BlockNumberToHash,
ContractClassData,
ContractData,
ContractClassHashes,
BonsaiContractsTrie,
BonsaiContractsFlat,
BonsaiContractsLog,
Expand Down Expand Up @@ -183,6 +198,11 @@ impl Column {
Column::BonsaiClassesTrie => "bonsai_classes_trie",
Column::BonsaiClassesFlat => "bonsai_classes_flat",
Column::BonsaiClassesLog => "bonsai_classes_log",
Column::BlockHashToNumber => "block_hash_to_number_trie",
Column::BlockNumberToHash => "block_to_hash_trie",
Column::ContractClassData => "contract_class_data",
Column::ContractData => "contract_data",
Column::ContractClassHashes => "contract_class_hashes",
}
}

Expand All @@ -202,7 +222,11 @@ pub(crate) trait DatabaseExt {

impl DatabaseExt for DB {
fn get_column(&self, col: Column) -> Arc<BoundColumnFamily<'_>> {
self.cf_handle(col.rocksdb_name()).expect("column not inititalized")
let name = col.rocksdb_name();
match self.cf_handle(name) {
Some(column) => column,
None => panic!("column {name} not initialized"),
}
}
}

Expand Down Expand Up @@ -301,41 +325,40 @@ impl DeoxysBackend {
DatabaseKeyMapping {
flat: Column::BonsaiContractsFlat,
trie: Column::BonsaiContractsTrie,
trie_log: Column::BonsaiContractsLog,
log: Column::BonsaiContractsLog,
},
),
bonsai_config.clone(),
)
.unwrap();
bonsai_contract.commit(BasicId::new(0)).unwrap();
bonsai_contract.init_tree(bonsai_identifier::CONTRACT).unwrap();

let mut bonsai_contract_storage = BonsaiStorage::new(
let bonsai_contract_storage = BonsaiStorage::new(
BonsaiDb::new(
db,
DatabaseKeyMapping {
flat: Column::BonsaiContractsStorageFlat,
trie: Column::BonsaiContractsStorageTrie,
trie_log: Column::BonsaiContractsStorageLog,
log: Column::BonsaiContractsStorageLog,
},
),
bonsai_config.clone(),
)
.unwrap();
bonsai_contract_storage.commit(BasicId::new(0)).unwrap();

let mut bonsai_classes = BonsaiStorage::new(
BonsaiDb::new(
db,
DatabaseKeyMapping {
flat: Column::BonsaiClassesFlat,
trie: Column::BonsaiClassesTrie,
trie_log: Column::BonsaiClassesLog,
log: Column::BonsaiClassesLog,
},
),
bonsai_config.clone(),
)
.unwrap();
bonsai_classes.commit(BasicId::new(0)).unwrap();
bonsai_classes.init_tree(bonsai_identifier::CLASS).unwrap();

Ok(Self {
mapping: Arc::new(MappingDb::new(Arc::clone(db), cache_more_things)),
Expand Down Expand Up @@ -375,6 +398,10 @@ impl DeoxysBackend {
BACKEND_SINGLETON.get().map(|backend| &backend.bonsai_class).expect("Backend not initialized")
}

pub(crate) fn expose_db() -> &'static Arc<DB> {
DB_SINGLETON.get().expect("Databsae not initialized")
}

/// Return l1 handler tx paid fee database manager
pub fn l1_handler_paid_fee() -> &'static Arc<L1HandlerTxFeeDb> {
BACKEND_SINGLETON.get().map(|backend| &backend.l1_handler_paid_fee).expect("Backend not initialized")
Expand Down
Loading