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

pruntime: Optional use RocksDB/redb as trie state backend #1265

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d54546b
Use RocksDB as chain state backend
kvinwang May 9, 2023
157f9fa
pruntime: Use RocksDB as contract storage
kvinwang May 9, 2023
88807b7
Fix test issues
kvinwang May 10, 2023
ae4b35d
Test for rocksdb backend
kvinwang May 11, 2023
1d4ce69
Add fused TrieBackend
kvinwang May 11, 2023
ff96d4c
Use memdb backend for testing
kvinwang May 11, 2023
0691e98
Fix clippy
kvinwang May 11, 2023
0a29d1c
Add PRUNTIME_TRIE_CACHE_PATH in menifest
kvinwang May 11, 2023
3beb61b
Reduce DB creation
kvinwang May 11, 2023
af3b5f7
Update rocksdb
kvinwang May 16, 2023
e678b11
Rename a env var
kvinwang May 16, 2023
a0686af
Use immutable_env
kvinwang May 23, 2023
89c142e
Make rocksdb behind --use-kvdb
kvinwang May 31, 2023
bb95eb8
Use enum instead of bool for db type
kvinwang Jul 6, 2023
184ad18
Rename Rocks to RocksDB
kvinwang Jul 6, 2023
6a86cb0
impl KvStorage for RocksDB
kvinwang Jul 6, 2023
b7d5134
Refactor RocksHashDB
kvinwang Jul 6, 2023
5ae4171
Refactor test cache dir
kvinwang Jul 6, 2023
2101a5b
Refactor serde for RocksDB
kvinwang Jul 6, 2023
ede5dd6
impl KvStorage for Redb
kvinwang Jul 6, 2023
dc9a4d6
Rename RocksHashDB to HashRocksDB
kvinwang Jul 6, 2023
b89cba3
Add redb as trie backend
kvinwang Jul 7, 2023
f233673
e2e: use redb
kvinwang Jul 7, 2023
2b42183
Update rocksdb to 0.21
kvinwang Jul 7, 2023
32ecf4c
Fix unittest and clippy
kvinwang Jul 7, 2023
6c76d08
Merge deposit and contract call
kvinwang Jul 7, 2023
cc08d88
Optimize db access in query
kvinwang Jul 7, 2023
ad105ee
Improve expect message
kvinwang Jul 7, 2023
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
92 changes: 84 additions & 8 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ members = [
"crates/phala-types",
"crates/phala-git-revision",
"crates/prpc",
"crates/immutable_env",
"crates/prpc-build",
"crates/phactory",
"crates/phactory/api",
Expand Down
11 changes: 11 additions & 0 deletions crates/immutable_env/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "immutable_env"
version = "0.1.0"
edition = "2021"
description = "Immutable environment variables which are taken from the environment when the program starts and never change."
license = "MIT"
repository = "https://github.com/Phala-Network/phala-blockchain"
authors = ["Kevin Wang <wy721@qq.com>"]

[dependencies]
ctor = "0.2"
16 changes: 16 additions & 0 deletions crates/immutable_env/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//! Immutable environment variables which are taken from the environment when the program starts
//! and never change.

#[ctor::ctor]
static VALUES: std::collections::HashMap<String, String> = std::env::vars().collect();

pub fn get(key: &str) -> Option<String> {
VALUES.get(key).cloned()
}

#[test]
fn it_works() {
let init = std::env::var("PATH").ok();
std::env::set_var("PATH", "foo");
assert_eq!(get("PATH"), init);
}
16 changes: 9 additions & 7 deletions crates/phactory/src/contracts/pink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ impl OCalls for RuntimeHandle<'_> {
}

fn storage_get(&self, key: Vec<u8>) -> Option<Vec<u8>> {
self.cluster.storage.get(&key).map(|(_rc, val)| val.clone())
self.cluster.storage.get(&key).map(|(_rc, val)| val)
}

fn storage_commit(&mut self, _root: Hash, _changes: StorageChanges) {
Expand Down Expand Up @@ -410,6 +410,11 @@ impl OCalls for RuntimeHandleMut<'_> {
}

fn storage_commit(&mut self, root: Hash, changes: StorageChanges) {
// The underlying RocksDB would reject to commit to a snapshot.
if !context::get().mode.is_transaction() {
return;
}
info!("commit state root: {:?}", root);
self.cluster.storage.commit(root, changes);
}

Expand Down Expand Up @@ -641,15 +646,13 @@ impl Cluster {
context::using(&mut ctx, move || {
context::using_entry_contract(contract_id.clone(), || {
let mut runtime = self.runtime_mut(log_handler);
if deposit > 0 {
runtime.deposit(origin.clone(), deposit);
}
let args = TransactionArguments {
origin,
transfer,
gas_limit: WEIGHT_REF_TIME_PER_SECOND * 10,
gas_free: true,
storage_deposit_limit: None,
deposit,
};
let ink_result = runtime.call(contract_id, input_data, mode, args);
let effects = if mode.is_estimating() {
Expand Down Expand Up @@ -721,15 +724,13 @@ impl Cluster {
let log_handler = context.log_handler.clone();
context::using(&mut ctx, move || {
let mut runtime = self.runtime_mut(log_handler);
if deposit > 0 {
runtime.deposit(origin.clone(), deposit);
}
let args = TransactionArguments {
origin,
transfer,
gas_limit: WEIGHT_REF_TIME_PER_SECOND * 10,
gas_free: true,
storage_deposit_limit: None,
deposit,
};
let ink_result = runtime.instantiate(
code_hash,
Expand Down Expand Up @@ -778,6 +779,7 @@ impl Cluster {
gas_limit,
gas_free,
storage_deposit_limit,
deposit: 0,
};

let mut runtime = self.runtime_mut(context.log_handler.clone());
Expand Down
1 change: 1 addition & 0 deletions crates/phactory/src/contracts/support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ impl Contract {
gas_free: false,
storage_deposit_limit: None,
gas_limit,
deposit: 0,
};
let mut handle = env.contract_cluster.runtime_mut(env.log_handler.clone());
_ = handle.call(
Expand Down
5 changes: 4 additions & 1 deletion crates/phactory/src/prpc_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1324,10 +1324,13 @@ impl<Platform: pal::Platform + Serialize + DeserializeOwned> PhactoryApi for Rpc
// the singleton phactory. This way, we can avoid blocking the RPC server.
info!("Cloning Phactory to do RCU dispatch...");
let cloned = phactory.clone();
// Because the underlying KVDB of the cloned phactory is readonly, so we use the original
// phactory to dispatch the blocks.
let origin = std::mem::replace(&mut **phactory, cloned);
// We set rcu_dispatching = true to avoid a reentrant call to dispatch_blocks which
// would cause a state inconsistency.
phactory.rcu_dispatching = true;
cloned
origin
};
// Start to dispatch the blocks with the cloned phactory without locking the singleton phactory.
info!("Unlocked Phactory, dispatching blocks...");
Expand Down
6 changes: 6 additions & 0 deletions crates/phactory/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ mod storage_ext {
}

impl ChainStorage {
pub fn default_memdb() -> Self {
Self {
trie_storage: TrieStorage::default_memdb(),
}
}

fn get_raw(&self, key: impl AsRef<[u8]>) -> Option<Vec<u8>> {
self.trie_storage.get(key)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/phactory/src/system/gk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1609,7 +1609,7 @@ pub mod tests {

fn with_block(block_number: chain::BlockNumber, call: impl FnOnce(&BlockInfo)) {
// GK never use the storage ATM.
let storage = crate::ChainStorage::default();
let storage = crate::ChainStorage::default_memdb();
let mut recv_mq = phala_mq::MessageDispatcher::new();
let mut send_mq = phala_mq::MessageSendQueue::new();
let block = BlockInfo {
Expand Down
1 change: 1 addition & 0 deletions crates/phactory/src/system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,7 @@ impl<Platform: pal::Platform> System<Platform> {
gas_limit,
gas_free: false,
storage_deposit_limit,
deposit: 0,
};
let mut runtime = cluster.runtime_mut(log_handler.clone());
let _result = runtime.instantiate(
Expand Down
9 changes: 9 additions & 0 deletions crates/phala-trie-storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ serde = { version = "1.0", default-features = false, features = ["derive", "allo
hash-db = "0.16.0"
trie-db = "0.27.1"
im = { version = "15", features = ["serde"] }
rocksdb = "0.21"
librocksdb-sys = "0.11.0"
log = "0.4"
environmental = "1"
immutable_env = { version = "0.1.0", path = "../immutable_env" }
atomic = "0.5.3"
redb = "1"
ouroboros = "0.17"

[dev-dependencies]
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }
Expand All @@ -28,6 +35,8 @@ hex = "0.4"
serde_json = "1.0"
impl-serde = "0.4.0"
keccak-hasher = "0.16.0"
serde_cbor = "0.11.2"
tempfile = "3"

[features]
default = ["serde"]
Loading