Skip to content

Commit

Permalink
refactor api
Browse files Browse the repository at this point in the history
  • Loading branch information
KaoImin committed Aug 21, 2023
1 parent b33de8d commit c6e57ce
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 60 deletions.
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 @@ -32,9 +32,9 @@ members = [
"common/pubsub",
"common/version",
"core/api",
"core/db",
"core/cli",
"core/consensus",
"core/db",
"core/executor",
"core/mempool",
"core/network",
Expand Down
2 changes: 2 additions & 0 deletions core/cli/src/args/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ impl RunArgs {
) -> Result<()> {
let Self { config, spec } = self;
let genesis = spec.genesis.build_rich_block();

utils::check_version(
&config.data_path_for_version(),
&kernel_version,
utils::latest_compatible_version(),
)?;
utils::register_log(&config);

Axon::new(application_version.to_string(), config, spec, genesis)
.run(key_provider)
.map_err(Error::Running)
Expand Down
3 changes: 1 addition & 2 deletions core/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
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]
parking_lot = "0.12"
rocksdb = { package = "ckb-rocksdb", version = "0.20"}
rocksdb = { package = "ckb-rocksdb", version = "0.20" }

common-apm = { path = "../../common/apm" }
common-apm-derive = { path = "../../common/apm-derive" }
Expand Down
3 changes: 3 additions & 0 deletions core/db/src/rocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ impl RocksAdapter {
map_category(StorageCategory::Wal),
map_category(StorageCategory::HashHeight),
map_category(StorageCategory::Code),
map_category(StorageCategory::EvmState),
map_category(StorageCategory::MetadataState),
map_category(StorageCategory::CkbLightClientState),
];

let (mut opts, cf_descriptors) = if let Some(ref file) = config.options_file {
Expand Down
8 changes: 2 additions & 6 deletions core/executor/benches/bench_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use common_crypto::{
ToPublicKey, UncompressedPublicKey,
};
use protocol::codec::{hex_decode, ProtocolCodec};
use protocol::traits::{Executor, StateStorageCategory};
use protocol::traits::Executor;
use protocol::types::{
public_to_address, Account, Address, Eip1559Transaction, ExecutorContext, Hash, Public,
SignedTransaction, TransactionAction, UnsignedTransaction, UnverifiedTransaction, NIL_DATA,
Expand Down Expand Up @@ -37,11 +37,7 @@ impl BenchAdapter {
let db = RocksAdapter::new(DATA_PATH, Default::default()).unwrap();

BenchAdapter {
trie_db: Arc::new(RocksTrieDB::new(
db.inner_db(),
StateStorageCategory::EvmState,
1000,
)),
trie_db: Arc::new(RocksTrieDB::new_evm(db.inner_db(), 1000)),
storage: Arc::new(ImplStorage::new(Arc::new(db), 100)),
}
}
Expand Down
3 changes: 1 addition & 2 deletions core/executor/benches/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use common_crypto::{
};
use protocol::{
codec::hex_decode,
traits::StateStorageCategory,
types::{
public_to_address, Account, Address, Bytes, Eip1559Transaction, ExecutorContext, Public,
SignedTransaction, TransactionAction, UnsignedTransaction, UnverifiedTransaction, H160,
Expand All @@ -30,7 +29,7 @@ pub fn new_storage() -> (RocksTrieDB, ImplStorage<RocksAdapter>) {
let db = RocksAdapter::new(DATA_PATH, Default::default()).unwrap();

(
RocksTrieDB::new(db.inner_db(), StateStorageCategory::EvmState, 1000),
RocksTrieDB::new_evm(db.inner_db(), 1000),
ImplStorage::new(Arc::new(db), 100),
)
}
Expand Down
14 changes: 13 additions & 1 deletion core/executor/src/adapter/trie/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,19 @@ impl trie::DB for RocksTrieDB {
}

impl RocksTrieDB {
pub fn new(db: Arc<DB>, category: StateStorageCategory, cache_size: usize) -> Self {
pub fn new_evm(db: Arc<DB>, cache_size: usize) -> Self {
Self::new(db, StateStorageCategory::EvmState, cache_size)
}

pub fn new_metadata(db: Arc<DB>, cache_size: usize) -> Self {
Self::new(db, StateStorageCategory::MetadataState, cache_size)
}

pub fn new_ckb_light_client(db: Arc<DB>, cache_size: usize) -> Self {
Self::new(db, StateStorageCategory::CkbLightClientState, cache_size)
}

fn new(db: Arc<DB>, category: StateStorageCategory, cache_size: usize) -> Self {
let cache = RwLock::new(HashMap::with_capacity(cache_size));
RocksTrieDB {
db,
Expand Down
3 changes: 1 addition & 2 deletions core/executor/src/adapter/trie/wrapped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ mod tests {

use core_db::RocksAdapter;
use protocol::rand::random;
use protocol::traits::StateStorageCategory;

use crate::adapter::RocksTrieDB;

Expand All @@ -92,7 +91,7 @@ mod tests {
let dir = tempfile::tempdir().unwrap();
let inner_db =
Arc::new(RocksAdapter::new(dir.path(), Default::default()).unwrap()).inner_db();
let db = RocksTrieDB::new(inner_db, StateStorageCategory::EvmState, 100);
let db = RocksTrieDB::new_evm(inner_db, 100);
let mut mpt = MPTTrie::new(Arc::new(db));

let key_1 = rand_bytes(5);
Expand Down
8 changes: 2 additions & 6 deletions core/executor/src/debugger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use evm::tracing::{Event, EventListener};
use common_config_parser::parse_file;
use common_crypto::{PrivateKey, Secp256k1RecoverablePrivateKey, Signature};
use protocol::codec::{hex_decode, ProtocolCodec};
use protocol::traits::{Backend, Executor, StateStorageCategory};
use protocol::traits::{Backend, Executor};
use protocol::types::{
Account, Eip1559Transaction, ExecResp, ExecutorContext, Hash, Hasher, RichBlock,
SignedTransaction, TxResp, UnsignedTransaction, UnverifiedTransaction, H160, H256,
Expand Down Expand Up @@ -46,11 +46,7 @@ impl EvmDebugger {
db_state_path.push_str("/state");
let _ = std::fs::create_dir_all(&db_state_path);
let inner_db = rocks_adapter.inner_db();
let trie = Arc::new(RocksTrieDB::new(
inner_db,
StateStorageCategory::EvmState,
1000,
));
let trie = Arc::new(RocksTrieDB::new_evm(inner_db, 1000));

let mut mpt = MPTTrie::new(Arc::clone(&trie));

Expand Down
9 changes: 3 additions & 6 deletions core/executor/src/system_contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ use evm::backend::ApplyBackend;
use parking_lot::RwLock;
use rocksdb::DB;

use protocol::ckb_blake2b_256;
use protocol::traits::{ExecutorAdapter, StateStorageCategory};
use protocol::types::{Bytes, Hasher, SignedTransaction, TxResp, H160, H256};
use protocol::{ckb_blake2b_256, traits::ExecutorAdapter};

use crate::adapter::RocksTrieDB;
use crate::system_contract::{
Expand Down Expand Up @@ -114,18 +113,16 @@ pub fn init<Adapter: ExecutorAdapter + ApplyBackend>(
// Init metadata db.
{
let mut _db = METADATA_DB.write();
_db.replace(Arc::new(RocksTrieDB::new(
_db.replace(Arc::new(RocksTrieDB::new_metadata(
db.clone(),
StateStorageCategory::MetadataState,
METADATA_DB_CACHE_SIZE,
)));
}

{
let mut _db = HEADER_CELL_DB.write();
_db.replace(Arc::new(RocksTrieDB::new(
_db.replace(Arc::new(RocksTrieDB::new_ckb_light_client(
db,
StateStorageCategory::CkbLightClientState,
HEADER_CELL_DB_CACHE_SIZE,
)));
}
Expand Down
5 changes: 2 additions & 3 deletions core/interoperation/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ use std::io::BufReader;
use std::sync::Arc;

use protocol::codec::ProtocolCodec;
use protocol::traits::{Context, Executor, StateStorageCategory, Storage};
use protocol::traits::{Context, Executor, Storage};
use protocol::types::{
Account, Address, Bytes, Eip1559Transaction, ExecResp, Proposal, Public, RichBlock,
SignatureComponents, SignedTransaction, TransactionAction, UnsignedTransaction,
UnverifiedTransaction, H256, NIL_DATA, RLP_NULL, U256,
};


use core_db::RocksAdapter;
use core_executor::adapter::{MPTTrie, RocksTrieDB};
use core_executor::{
Expand Down Expand Up @@ -43,7 +42,7 @@ impl TestHandle {
.unwrap();
let inner_db = storage_adapter.inner_db();

let trie_db = RocksTrieDB::new(inner_db.clone(), StateStorageCategory::EvmState, 100);
let trie_db = RocksTrieDB::new_evm(inner_db.clone(), 100);

let mut handle = TestHandle {
storage: Arc::new(ImplStorage::new(Arc::new(storage_adapter), 10)),
Expand Down
39 changes: 16 additions & 23 deletions core/run/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@ use {

use common_apm::metrics::mempool::{MEMPOOL_CO_QUEUE_LEN, MEMPOOL_LEN_GAUGE};
use common_apm::{server::run_prometheus_server, tracing::global_tracer_register};
use common_config_parser::types::{
spec::{ChainSpec, InitialAccount},
Config, ConfigJaeger, ConfigPrometheus, ConfigRocksDB,
};
use common_config_parser::types::spec::{ChainSpec, InitialAccount};
use common_config_parser::types::{Config, ConfigJaeger, ConfigPrometheus};
use common_crypto::{
BlsPrivateKey, BlsPublicKey, PublicKey, Secp256k1, Secp256k1PrivateKey, Secp256k1PublicKey,
ToPublicKey, UncompressedPublicKey,
};

use protocol::lazy::CHAIN_ID;
use protocol::codec::{hex_decode, ProtocolCodec};
#[cfg(unix)]
use protocol::tokio::signal::unix as os_impl;
use protocol::tokio::{
Expand All @@ -39,12 +37,8 @@ use protocol::types::{
SignedTransaction, Validator, ValidatorExtend, H256, NIL_DATA, RLP_NULL,
};
use protocol::{
async_trait, trie::DB as TrieDB, Display, From, ProtocolError, ProtocolErrorKind,
ProtocolResult,
};
use protocol::{
codec::{hex_decode, ProtocolCodec},
traits::StateStorageCategory,
async_trait, lazy::CHAIN_ID, trie::DB as TrieDB, Display, From, ProtocolError,
ProtocolErrorKind, ProtocolResult,
};

use core_api::{jsonrpc::run_jsonrpc_server, DefaultAPIAdapter};
Expand Down Expand Up @@ -129,10 +123,10 @@ impl Axon {
log::info!("The Genesis block has been initialized.");
self.apply_genesis_after_checks(&genesis).await?;
} else {
self.create_genesis(inner_db, &storage).await?;
self.create_genesis(inner_db.clone(), &storage).await?;
}
drop(storage);
self.start(key_provider).await

self.start(key_provider, storage, inner_db).await
})?;
rt.shutdown_timeout(std::time::Duration::from_secs(1));

Expand Down Expand Up @@ -257,7 +251,12 @@ impl Axon {
Ok(())
}

pub async fn start<K: KeyProvider>(self, key_provider: Option<K>) -> ProtocolResult<()> {
pub async fn start<K: KeyProvider>(
self,
key_provider: Option<K>,
storage: Arc<ImplStorage<RocksAdapter>>,
inner_db: Arc<RocksDB>,
) -> ProtocolResult<()> {
// Start jaeger
Self::run_jaeger(self.config.jaeger.clone());

Expand All @@ -275,7 +274,6 @@ impl Axon {
observe_listen_port_occupancy(&[self.config.network.listening_address.clone()]).await?;

// Init Block db and get the current block
let (storage, inner_db) = self.init_storage(true).await?;
let current_block = storage.get_latest_block(Context::new()).await?;
let current_state_root = current_block.header.state_root;

Expand Down Expand Up @@ -449,9 +447,8 @@ impl Axon {
}

fn init_evm_trie_db(&self, inner_db: Arc<RocksDB>) -> Arc<RocksTrieDB> {
Arc::new(RocksTrieDB::new(
Arc::new(RocksTrieDB::new_evm(
inner_db,
StateStorageCategory::EvmState,
self.config.executor.triedb_cache_size,
))
}
Expand Down Expand Up @@ -1020,11 +1017,7 @@ where
S: Storage + 'static,
{
let executor = AxonExecutor::default();
let trie_db = Arc::new(RocksTrieDB::new(
inner_db.clone(),
StateStorageCategory::EvmState,
cache_size,
));
let trie_db = Arc::new(RocksTrieDB::new_evm(inner_db.clone(), cache_size));
let mut backend = AxonExecutorApplyAdapter::from_root(
state_root,
trie_db,
Expand Down
6 changes: 1 addition & 5 deletions core/run/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@ use common_config_parser::types::{
spec::{ChainSpec, ChainSpecValueParser},
Config, ConfigValueParser,
};
use core_executor::{MPTTrie, RocksTrieDB};
use core_storage::{adapter::rocks::RocksAdapter, ImplStorage};
use protocol::{
codec::hex_decode,
tokio,
traits::StateStorageCategory,
types::{RichBlock, H256},
};

Expand Down Expand Up @@ -143,9 +140,8 @@ async fn check_genesis_data<'a>(case: &TestCase<'a>) {
let inner_db = rocks_adapter.inner_db();

let storage = Arc::new(ImplStorage::new(rocks_adapter, config.rocksdb.cache_size));
let trie_db = Arc::new(RocksTrieDB::new(
let trie_db = Arc::new(RocksTrieDB::new_evm(
inner_db.clone(),
StateStorageCategory::EvmState,
config.executor.triedb_cache_size,
));

Expand Down
2 changes: 1 addition & 1 deletion protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ rlp = "0.5"
rlp-derive = "0.1"
serde = { version = "1.0", features = ["derive"] }
thiserror = "1.0"
tokio = { version = "1.31", features = ["full"] }
tokio = { version = "1.32", features = ["full"] }
trie = { package = "cita_trie", git = "https://github.com/KaoImin/cita-trie.git", rev = "eea569c", version = "4.1" }

common-crypto = { path = "../common/crypto" }
Expand Down

0 comments on commit c6e57ce

Please sign in to comment.