Skip to content

Commit

Permalink
use db-backend instead of in-mem-backend
Browse files Browse the repository at this point in the history
  • Loading branch information
icodezjb authored and gguoss committed Aug 31, 2018
1 parent 4df9c05 commit dd6df51
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ substrate-primitives = { git = "https://github.com/chainx-org/substrate" }
substrate-client = { git = "https://github.com/chainx-org/substrate" }
substrate-client-db = { git = "https://github.com/chainx-org/substrate" }
substrate-state-db = { git = "https://github.com/chainx-org/substrate" }
substrate-state-machine = { git = "https://github.com/chainx-org/substrate" }
substrate-codec = { git = "https://github.com/chainx-org/substrate", default_features = false }
substrate-bft = { git = "https://github.com/chainx-org/substrate", default_features = false }
substrate-rpc-servers = { git = "https://github.com/chainx-org/substrate" }
Expand Down
1 change: 1 addition & 0 deletions pool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ substrate-primitives = { git = "https://github.com/chainx-org/substrate" }
substrate-runtime-primitives = { git = "https://github.com/chainx-org/substrate" }
substrate-network = { git = "https://github.com/chainx-org/substrate" }
substrate-client = { git = "https://github.com/chainx-org/substrate" }
substrate-client-db = { git = "https://github.com/chainx-org/substrate" }
chainx-primitives = { path = "../primitives" }
chainx-runtime = { path = "../runtime" }
chainx-executor = {path = "../executor"}
Expand Down
1 change: 1 addition & 0 deletions pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
extern crate substrate_codec as codec;
extern crate substrate_runtime_primitives as runtime_primitives;
extern crate substrate_primitives as substrate_primitives;
extern crate substrate_client_db;
extern crate chainx_primitives;
extern crate chainx_runtime;
extern crate substrate_network;
Expand Down
5 changes: 2 additions & 3 deletions pool/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ use std::{
use codec::{Encode, Decode};
use chainx_runtime::{Address, UncheckedExtrinsic};
use runtime_primitives::traits::{Checkable};
use substrate_primitives::{KeccakHasher, RlpCodec};
use substrate_client::{self, Client};

use substrate_client_db;

use extrinsic_pool::{
Pool,
Expand All @@ -39,7 +38,7 @@ pub type CheckedExtrinsic =
&'static str,
>,
>>::Checked;
pub type Backend = substrate_client::in_mem::Backend<Block, KeccakHasher, RlpCodec>;
pub type Backend = substrate_client_db::Backend<Block>;
use chainx_executor;
pub type Executor = substrate_client::LocalCallExecutor<
Backend,
Expand Down
52 changes: 37 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ extern crate substrate_primitives;
extern crate substrate_client as client;
extern crate substrate_bft as bft;
extern crate substrate_rpc_servers as rpc_server;
extern crate substrate_client_db as client_db;
extern crate substrate_state_machine as state_machine;
extern crate substrate_state_db as state_db;

extern crate chainx_primitives;
extern crate chainx_executor;
Expand All @@ -32,6 +35,7 @@ use substrate_network::specialization::Specialization;
use substrate_network::{NodeIndex, Context, message};
use substrate_network::StatusMessage as GenericFullStatus;
use chainx_primitives::{Block, Header, Hash};
use chainx_executor::NativeExecutor;
use chainx_runtime::{GenesisConfig, ConsensusConfig, CouncilConfig, DemocracyConfig,
SessionConfig, StakingConfig, TimestampConfig};

Expand All @@ -43,13 +47,16 @@ use std::sync::Arc;
use std::iter;
use std::net::{Ipv4Addr, IpAddr, SocketAddr};
use std::time::{Duration, Instant};
use std::path::PathBuf;

use chainx_pool::pool::{TransactionPool, PoolApi};
use state_machine::ExecutionStrategy;


pub struct Protocol;

const TIMER_INTERVAL_MS: u64 = 5000;
const FINALIZATION_WINDOW: u64 = 32;

type FullStatus = GenericFullStatus<Block>;

Expand Down Expand Up @@ -99,7 +106,11 @@ pub type NetworkService = substrate_network::Service<Block, Protocol, Hash>;

pub type NetworkParam = substrate_network::Params<Block, Protocol, Hash>;

pub type TBackend = client_db::Backend<Block>;
pub type TExecutor = client::LocalCallExecutor<TBackend, NativeExecutor<chainx_executor::Executor>>;

const DOT_PROTOCOL_ID: substrate_network::ProtocolId = *b"exc";
const DB_PATH: &str = r"./chainx";

fn genesis_config() -> GenesisConfig {
let god_key = hex!("3d866ec8a9190c8343c2fc593d21d8a6d0c5c4763aaab2349de3a6111d64d124");
Expand Down Expand Up @@ -239,15 +250,26 @@ fn main() {

let _ = env_logger::try_init();

let executor = chainx_executor::NativeExecutor::with_heap_pages(8);
let client = Arc::new(
client::new_in_mem::<
chainx_executor::NativeExecutor<chainx_executor::Executor>,
Block,
_,
>(executor, genesis_config()).unwrap(),
);
let backend = Arc::new(
TBackend::new(
client_db::DatabaseSettings{
cache_size: None,
path: PathBuf::from(DB_PATH),
pruning:state_db::PruningMode::default(),},
FINALIZATION_WINDOW
).unwrap());

let executor = client::LocalCallExecutor::new(
backend.clone(),
NativeExecutor::<chainx_executor::Executor>::with_heap_pages(8));

let client = Arc::new(
client::Client::new(
backend.clone(),
executor,
genesis_config(),
ExecutionStrategy::NativeWhenPossible
).unwrap());

let (exit_send, exit) = exit_future::signal();
let mut runtime = Runtime::new().expect("failed to start runtime on current thread");
Expand Down Expand Up @@ -298,16 +320,16 @@ fn main() {
let block_header = block.header.clone();
let hash = block_header.hash();
let justification = fake_justify(&block.header);
let justified = client
.check_justification(block.header, justification)
.unwrap();
client
.import_block(
let justified = client.check_justification(
block.header,
justification).unwrap();

client.import_block(
client::BlockOrigin::NetworkBroadcast,
justified,
Some(block.extrinsics),
)
.unwrap();
).unwrap();

network.on_block_imported(hash, &block_header);
}
Ok(())
Expand Down

0 comments on commit dd6df51

Please sign in to comment.