Skip to content

Commit

Permalink
refactor: merge trie db to main db
Browse files Browse the repository at this point in the history
  • Loading branch information
KaoImin committed Aug 18, 2023
1 parent 0bd3332 commit 4f353be
Show file tree
Hide file tree
Showing 36 changed files with 464 additions and 215 deletions.
16 changes: 12 additions & 4 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 @@ -32,6 +32,7 @@ members = [
"common/pubsub",
"common/version",
"core/api",
"core/db",
"core/cli",
"core/consensus",
"core/executor",
Expand Down
4 changes: 2 additions & 2 deletions core/api/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl<M, S, DB, Net> DefaultAPIAdapter<M, S, DB, Net>
where
M: MemPool + 'static,
S: ReadOnlyStorage + 'static,
DB: trie::DB + 'static,
DB: trie::DB + Send + Sync + 'static,
Net: Network + 'static,
{
pub fn new(mempool: Arc<M>, storage: Arc<S>, trie_db: Arc<DB>, net: Arc<Net>) -> Self {
Expand Down Expand Up @@ -63,7 +63,7 @@ impl<M, S, DB, Net> APIAdapter for DefaultAPIAdapter<M, S, DB, Net>
where
M: MemPool + 'static,
S: ReadOnlyStorage + 'static,
DB: trie::DB + 'static,
DB: trie::DB + Send + Sync + 'static,
Net: Network + 'static,
{
async fn insert_signed_txs(
Expand Down
6 changes: 3 additions & 3 deletions core/consensus/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ where
M: MemPool + 'static,
N: Network + Rpc + PeerTrust + Gossip + 'static,
S: Storage + 'static,
DB: trie::DB + 'static,
DB: trie::DB + Send + Sync + 'static,
{
#[trace_span(kind = "consensus.adapter")]
async fn get_txs_from_mempool(
Expand Down Expand Up @@ -128,7 +128,7 @@ where
M: MemPool + 'static,
N: Network + Rpc + PeerTrust + Gossip + 'static,
S: Storage + 'static,
DB: trie::DB + 'static,
DB: trie::DB + Send + Sync + 'static,
{
#[trace_span(kind = "consensus.adapter")]
fn update_status(
Expand Down Expand Up @@ -229,7 +229,7 @@ where
M: MemPool + 'static,
N: Network + Rpc + PeerTrust + Gossip + 'static,
S: Storage + 'static,
DB: trie::DB + 'static,
DB: trie::DB + Send + Sync + 'static,
{
/// Save a block to the database.
#[trace_span(kind = "consensus.adapter", logs = "{txs_len: block.tx_hashes.len()}")]
Expand Down
12 changes: 12 additions & 0 deletions core/db/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "core-db"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rocksdb = { package = "ckb-rocksdb", version = "0.20"}

protocol = { path = "../../protocol", package = "axon-protocol" }

14 changes: 14 additions & 0 deletions core/db/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
2 changes: 1 addition & 1 deletion core/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ common-config-parser = { path = "../../common/config-parser" }
common-crypto = { path = "../../common/crypto" }
common-merkle = { path = "../../common/merkle" }
core-interoperation = { path = "../interoperation" }
core-storage = { path = "../storage" }
ethers = "2.0"
evm = { version = "0.37", features = ["tracing"] }
futures = "0.3"
Expand All @@ -39,7 +40,6 @@ ckb-jsonrpc-types = "0.108"
ckb-types = "0.108"
common-crypto = { path = "../../common/crypto" }
core-rpc-client = { path = "../rpc-client" }
core-storage = { path = "../storage" }
criterion = "0.5"
ethabi = "18.0"
ethabi-contract = { git = "https://github.com/rust-ethereum/ethabi.git", rev = "7edf185" }
Expand Down
16 changes: 9 additions & 7 deletions core/executor/benches/bench_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use common_crypto::{
ToPublicKey, UncompressedPublicKey,
};
use core_executor::{AxonExecutor, AxonExecutorApplyAdapter, MPTTrie, RocksTrieDB};
use core_storage::{adapter::rocks::RocksAdapter, ImplStorage};
use core_storage::{adapter::RocksAdapter, ImplStorage};
use protocol::codec::{hex_decode, ProtocolCodec};
use protocol::traits::Executor;
use protocol::traits::{Executor, StateStorageCategory};
use protocol::types::{
public_to_address, Account, Address, Eip1559Transaction, ExecutorContext, Hash, Public,
SignedTransaction, TransactionAction, UnsignedTransaction, UnverifiedTransaction, NIL_DATA,
Expand All @@ -23,7 +23,6 @@ lazy_static::lazy_static! {
static ref DISTRIBUTE_ADDRESS: Address = Address::from_hex("0x35e70c3f5a794a77efc2ec5ba964bffcc7fd2c0a").unwrap();
}

const STATE_PATH: &str = "../../free-space/rocks/state";
const DATA_PATH: &str = "../../free-space/rocks/data";

struct BenchAdapter {
Expand All @@ -33,12 +32,15 @@ struct BenchAdapter {

impl BenchAdapter {
fn new() -> Self {
let db = RocksAdapter::new(DATA_PATH, Default::default()).unwrap();

BenchAdapter {
trie_db: Arc::new(RocksTrieDB::new(STATE_PATH, Default::default(), 1000).unwrap()),
storage: Arc::new(ImplStorage::new(
Arc::new(RocksAdapter::new(DATA_PATH, Default::default()).unwrap()),
100,
trie_db: Arc::new(RocksTrieDB::new(
db.inner_db(),
StateStorageCategory::EvmState,
1000,
)),
storage: Arc::new(ImplStorage::new(Arc::new(db), 100)),
}
}

Expand Down
10 changes: 3 additions & 7 deletions core/executor/benches/bench_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ use protocol::{
types::{Account, Address, ExecutorContext},
};

use crate::mock::{
init_account, mock_executor_context, mock_transactions, new_rocks_trie_db, new_storage,
};
use crate::mock::{init_account, mock_executor_context, mock_transactions, new_storage};
use crate::revm_adapter::{revm_exec, RevmAdapter};

trait BackendInit<S: Storage + 'static, DB: trie::DB + 'static> {
Expand Down Expand Up @@ -73,8 +71,7 @@ fn criterion_10000_txs(c: &mut Criterion) {
let txs = mock_transactions(10000);
// MacOS M1 Pro, 16GB: time: 20.098ms
c.bench_function("revm 10000 tx", |b| {
let storage = new_storage();
let db = new_rocks_trie_db();
let (db, storage) = new_storage();
let exec_ctx = mock_executor_context();
let (account, addr) = init_account();
let revm_adapter = RevmAdapter::init(storage, db, exec_ctx, account, addr);
Expand All @@ -86,8 +83,7 @@ fn criterion_10000_txs(c: &mut Criterion) {
});
// MacOS M1 Pro, 16GB: time:54.987ms
c.bench_function("evm 10000 tx", |b| {
let storage = new_storage();
let db = new_rocks_trie_db();
let (db, storage) = new_storage();
let exec_ctx = mock_executor_context();
let (account, addr) = init_account();
let mut axon_adapter = AxonExecutorApplyAdapter::init(storage, db, exec_ctx, account, addr);
Expand Down
16 changes: 7 additions & 9 deletions core/executor/benches/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use common_crypto::{
ToPublicKey, UncompressedPublicKey,
};
use core_executor::RocksTrieDB;
use core_storage::adapter::rocks::RocksAdapter;
use core_storage::adapter::RocksAdapter;
use core_storage::ImplStorage;
use protocol::{
codec::hex_decode,
traits::StateStorageCategory,
types::{
public_to_address, Account, Address, Bytes, Eip1559Transaction, ExecutorContext, Public,
SignedTransaction, TransactionAction, UnsignedTransaction, UnverifiedTransaction, H160,
Expand All @@ -22,17 +23,14 @@ lazy_static::lazy_static! {
static ref DISTRIBUTE_ADDRESS: Address = Address::from_hex("0x35e70c3f5a794a77efc2ec5ba964bffcc7fd2c0a").unwrap();
}

const STATE_PATH: &str = "../../free-space/rocks/state";
const DATA_PATH: &str = "../../free-space/rocks/data";

pub fn new_rocks_trie_db() -> RocksTrieDB {
RocksTrieDB::new(STATE_PATH, Default::default(), 1000).unwrap()
}
pub fn new_storage() -> (RocksTrieDB, ImplStorage<RocksAdapter>) {
let db = RocksAdapter::new(DATA_PATH, Default::default()).unwrap();

pub fn new_storage() -> ImplStorage<RocksAdapter> {
ImplStorage::new(
Arc::new(RocksAdapter::new(DATA_PATH, Default::default()).unwrap()),
100,
(
RocksTrieDB::new(db.inner_db(), StateStorageCategory::EvmState, 1000),
ImplStorage::new(Arc::new(db), 100),
)
}

Expand Down
Loading

0 comments on commit 4f353be

Please sign in to comment.