From 5bf3d038deb48b2908b148c5a6fd94c6ddc4d679 Mon Sep 17 00:00:00 2001 From: Aton Date: Mon, 11 Feb 2019 18:03:27 +0800 Subject: [PATCH] update msgbus-redis for new substrate --- Cargo.lock | 2 + core/cli/src/lib.rs | 103 +++++------------------- core/cli/src/params.rs | 4 +- core/client/src/block_builder/mod.rs | 16 ---- core/service/src/lib.rs | 2 +- node/cli/Cargo.toml | 2 +- node/cli/src/service.rs | 5 +- srml/support/src/lib.rs | 25 ------ srml/support/src/storage/blocknumber.rs | 14 ++-- srml/support/src/storage/mod.rs | 16 ++-- srml/support/src/storage/redis.rs | 13 ++- 11 files changed, 53 insertions(+), 149 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1dfc43ac21210..efff803e83126 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,3 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. [[package]] name = "MacTypes-sys" version = "2.1.0" diff --git a/core/cli/src/lib.rs b/core/cli/src/lib.rs index fd092aacf31c2..0a7dddff6fb78 100644 --- a/core/cli/src/lib.rs +++ b/core/cli/src/lib.rs @@ -21,22 +21,6 @@ #[macro_use] mod traits; -#[macro_use] -extern crate substrate_telemetry; -extern crate exit_future; - -#[macro_use] -extern crate lazy_static; -extern crate clap; -#[macro_use] -extern crate error_chain; -#[macro_use] -extern crate log; -extern crate structopt; - -#[cfg(any(feature = "msgbus-redis", feature = "cache-lru"))] -extern crate srml_support; - mod params; pub mod error; pub mod informant; @@ -365,6 +349,7 @@ where ), }; + #[cfg(not(feature = "msgbus-redis"))] let role = if cli.light { config.block_execution_strategy = service::ExecutionStrategy::NativeWhenPossible; @@ -372,34 +357,22 @@ where } else if cli.validator || cli.shared_params.dev { config.block_execution_strategy = service::ExecutionStrategy::Both; service::Roles::AUTHORITY -/* } else if matches.is_present("validator") || matches.is_present("dev") { - if cfg!(feature = "msgbus-redis") { - config.block_execution_strategy = service::ExecutionStrategy::NativeWhenPossible; - service::Roles::FULL - } else { - config.block_execution_strategy = service::ExecutionStrategy::Both; - service::Roles::AUTHORITY - }*/ } else { config.block_execution_strategy = service::ExecutionStrategy::NativeWhenPossible; service::Roles::FULL }; - config.block_execution_strategy = cli.execution.into(); - /*if let Some(s) = matches.value_of("execution") { - config.block_execution_strategy = match s { - "both" => service::ExecutionStrategy::Both, - "native" => service::ExecutionStrategy::NativeWhenPossible, - "wasm" => { - if cfg!(feature = "msgbus-redis") { - bail!(create_input_err("When in `msgbus` mod, can't use wasm strategy")) - } else { - service::ExecutionStrategy::AlwaysWasm - } - }, - _ => bail!(create_input_err("Invalid execution mode specified")), + #[cfg(feature = "msgbus-redis")] + let role = + if cli.light { + config.block_execution_strategy = service::ExecutionStrategy::NativeWhenPossible; + service::Roles::LIGHT + } else { + config.block_execution_strategy = service::ExecutionStrategy::NativeWhenPossible; + service::Roles::FULL }; - }*/ + + config.block_execution_strategy = cli.execution.into(); config.roles = role; let client_id = config.client_id(); @@ -459,17 +432,17 @@ where S: FnOnce(&str) -> Result>>, String>, RS: FnOnce(E, RP, FactoryFullConfiguration) -> Result<(), String>, { - let config = create_run_node_config::(cli.left, spec_factory, impl_name, version)?; + #[cfg(feature = "msgbus-redis")] { + let connect = cli.left.redis.clone().unwrap_or("127.0.0.1".to_string()); + let connect = format!("redis://{}/", connect); + println!("redis {:?}", connect); + use srml_support::storage::redis::init_redis; + if let Err(e) = init_redis(&connect){ + bail!(create_input_err(format!("Redis error!\n{}", e))) + }; + } - #[cfg(feature = "msgbus-redis")] { - /*let connect = matches.value_of("redis").unwrap_or("127.0.0.1"); - let connect = format!("redis://{}/", connect); - println!("redis {:?}", connect); - use srml_support::storage::redis::init_redis; - if let Err(e) = init_redis(&connect){ - bail!(create_input_err(format!("Redis error!\n{}", format!("redis error! \n{}", e)))) - };*/ - } + let config = create_run_node_config::(cli.left, spec_factory, impl_name, version)?; run_service(exit, cli.right, config).map_err(Into::into) } @@ -588,39 +561,7 @@ where config.block_execution_strategy = cli.execution.into(); config.api_execution_strategy = cli.api_execution.into(); - /*let mut config = service::Configuration::default_with_spec(spec); - config.database_path = db_path.to_string(); - - if let Some(s) = matches.value_of("execution") { - config.block_execution_strategy = match s { - "both" => service::ExecutionStrategy::Both, - "native" => service::ExecutionStrategy::NativeWhenPossible, - "wasm" => { - if cfg!(feature = "msgbus-redis") { - bail!(create_input_err("When in `msgbus` mod, can't use wasm strategy")) - } else { - service::ExecutionStrategy::AlwaysWasm - } - }, - _ => return Err(error::ErrorKind::Input("Invalid block execution mode specified".to_owned()).into()), - }; - } - - if let Some(s) = matches.value_of("api-execution") { - config.api_execution_strategy = match s { - "both" => service::ExecutionStrategy::Both, - "native" => service::ExecutionStrategy::NativeWhenPossible, - "wasm" => { - if cfg!(feature = "msgbus-redis") { - bail!(create_input_err("When in `msgbus` mod, can't use wasm strategy")) - } else { - service::ExecutionStrategy::AlwaysWasm - } - }, - _ => return Err(error::ErrorKind::Input("Invalid API execution mode specified".to_owned()).into()), - }; - }*/ - +//TODO let file: Box = match cli.input { Some(filename) => Box::new(File::open(filename)?), None => Box::new(stdin()), diff --git a/core/cli/src/params.rs b/core/cli/src/params.rs index 35244652d4cf7..e8a8287247a42 100644 --- a/core/cli/src/params.rs +++ b/core/cli/src/params.rs @@ -131,7 +131,7 @@ pub struct RunCmd { #[structopt(long = "key", value_name = "STRING")] pub key: Option, - //#[cfg(not(feature = "msgbus-redis"))] + #[cfg(not(feature = "msgbus-redis"))] /// Enable validator mode #[structopt(long = "validator")] pub validator: bool, @@ -139,7 +139,7 @@ pub struct RunCmd { #[cfg(feature = "msgbus-redis")] /// Specify redis connect addr. default is (127.0.0.1) #[structopt(long = "redis", value_name = "URL")] - redis: Option, + pub redis: Option, /// Run in light client mode #[structopt(long = "light")] diff --git a/core/client/src/block_builder/mod.rs b/core/client/src/block_builder/mod.rs index aaaeae04c4445..f22f599ffdd9a 100644 --- a/core/client/src/block_builder/mod.rs +++ b/core/client/src/block_builder/mod.rs @@ -21,19 +21,3 @@ mod block_builder; #[cfg(feature = "std")] pub use self::block_builder::*; pub mod api; -/// Extend params for Node -#[derive(Debug/*, StructOpt*/)] -pub struct Params { - /*#[cfg(all(not(feature = "msgbus-redis"), not(feature = "msgbus-redis-keyhash")))] - /// Should run as a GRANDPA authority node - #[structopt(long = "grandpa-authority", help = "Run Node as a GRANDPA authority, implies --validator")] - grandpa_authority: bool, - - #[cfg(all(not(feature = "msgbus-redis"), not(feature = "msgbus-redis-keyhash")))] - /// Should run as a GRANDPA authority node only - #[structopt(long = "grandpa-authority-only", help = "Run Node as a GRANDPA authority only, don't as a usual validator, implies --grandpa-authority")] - grandpa_authority_only: bool, - - #[structopt(flatten)] - core: CoreParams*/ -} diff --git a/core/service/src/lib.rs b/core/service/src/lib.rs index b975cf57f4b38..d2b1e92520e13 100644 --- a/core/service/src/lib.rs +++ b/core/service/src/lib.rs @@ -304,7 +304,7 @@ impl Service { //_rpc: Box::new(rpc), _telemetry: telemetry, }) -} + } /// give the authority key, if we are an authority and have a key pub fn authority_key(&self) -> Option { diff --git a/node/cli/Cargo.toml b/node/cli/Cargo.toml index 3ce1e4d909b53..61c92deb6c8cb 100644 --- a/node/cli/Cargo.toml +++ b/node/cli/Cargo.toml @@ -11,7 +11,7 @@ log = "0.4" tokio = "0.1.7" futures = "0.1" exit-future = "0.1" -cli = { package = "substrate-cli", path = "../../core/cli", features = ["msgbus-redis", "msgbus-redis-keyhash", "cache-lru"] } +cli = { package = "substrate-cli", path = "../../core/cli" } parity-codec = { version = "3.0" } slog = "^2" sr-io = { path = "../../core/sr-io" } diff --git a/node/cli/src/service.rs b/node/cli/src/service.rs index a773e3cf954aa..50f35c08c1ce2 100644 --- a/node/cli/src/service.rs +++ b/node/cli/src/service.rs @@ -101,7 +101,8 @@ construct_service_factory! { info!("Running Grandpa session as Authority {}", key.public()); } - + #[cfg(not(feature = "msgbus-redis"))] { + // remove grandpa in msgbus mod for revert block executor.spawn(grandpa::run_grandpa( grandpa::Config { local_key, @@ -114,7 +115,7 @@ construct_service_factory! { grandpa::NetworkBridge::new(service.network()), service.on_exit(), )?); - + } Ok(service) } }, diff --git a/srml/support/src/lib.rs b/srml/support/src/lib.rs index 0e0962f04d684..bba2dcb2211d3 100644 --- a/srml/support/src/lib.rs +++ b/srml/support/src/lib.rs @@ -25,31 +25,6 @@ pub use serde; pub use sr_std as rstd; #[doc(hidden)] pub use parity_codec as codec; -extern crate srml_metadata; - -//extern crate mashup; -#[cfg_attr(test, macro_use)] -extern crate srml_support_procedural; -//extern crate substrate_inherents as inherents; - -#[cfg(test)] -#[macro_use] -extern crate pretty_assertions; -#[cfg(feature = "std")] -#[macro_use] -extern crate serde_derive; -#[cfg(test)] -#[macro_use] -extern crate parity_codec_derive; - -#[cfg(all(feature = "std", any(feature = "msgbus-redis", feature = "cache-lru")))] -#[macro_use] -extern crate lazy_static; - -#[cfg(all(feature = "std", any(feature = "msgbus-redis", feature = "cache-lru")))] -#[macro_use] -extern crate log; - #[doc(hidden)] pub use parity_codec_derive; #[cfg(feature = "std")] diff --git a/srml/support/src/storage/blocknumber.rs b/srml/support/src/storage/blocknumber.rs index 981de3767e64b..88e0edc2acff2 100644 --- a/srml/support/src/storage/blocknumber.rs +++ b/srml/support/src/storage/blocknumber.rs @@ -1,13 +1,15 @@ -use super::runtime_io::twox_128; static mut BLOCKNUMBER_KEY: &'static [u8] = b""; static mut BLOCKNUMBER_HASHED_KEY: [u8; 16] = [0; 16]; -pub fn set_blocknumber_key(key: &'static [u8]) { - let hash_key = twox_128(key); - unsafe { - BLOCKNUMBER_KEY = key; - BLOCKNUMBER_HASHED_KEY = hash_key; +pub fn set_blocknumber_key(_key: &'static [u8]) { + #[cfg(all(feature = "std", any(feature = "msgbus-redis", feature = "cache-lru")))] { + use super::runtime_io::twox_128; + let hash_key = twox_128(_key); + unsafe { + BLOCKNUMBER_KEY = _key; + BLOCKNUMBER_HASHED_KEY = hash_key; + } } } diff --git a/srml/support/src/storage/mod.rs b/srml/support/src/storage/mod.rs index 356b99d806f37..69502717e1f82 100644 --- a/srml/support/src/storage/mod.rs +++ b/srml/support/src/storage/mod.rs @@ -24,20 +24,19 @@ use crate::codec::{Codec, Decode, KeyedVec, Input}; #[macro_use] pub mod generator; +// related with msgbus & cache #[cfg(all(feature = "std", feature = "cache-lru"))] -extern crate lru; +use lru; #[cfg(all(feature = "std", feature = "msgbus-redis"))] pub mod redis; -//#[cfg(all(feature = "std", feature = "msgbus-redis"))] +//#[cfg(all(feature = "std", any(feature = "msgbus-redis", feature = "cache-lru")))] pub mod blocknumber; -#[cfg(all(feature = "std", feature = "blocknumber"))] -use self::blocknumber::blocknumber_hashedkey; - -#[cfg(all(feature = "std", feature = "blocknumber"))] +#[cfg(all(feature = "std", any(feature = "msgbus-redis", feature = "cache-lru")))] pub fn get_blocknumber() -> Option { + use self::blocknumber::blocknumber_hashedkey; let key = blocknumber_hashedkey(); runtime_io::read_storage(&key[..], &mut [0; 0][..], 0).map(|_| { @@ -48,8 +47,7 @@ pub fn get_blocknumber() -> Option { Decode::decode(&mut input).expect("storage is not null, therefore must be a valid type") }) } - -// TODO: consider using blake256 to avoid possible preimage attack. +// modify end struct IncrementalInput<'a> { key: &'a [u8], @@ -103,6 +101,7 @@ pub fn put(key: &[u8], value: &T) { value.using_encoded(|slice| { runtime_io::set_storage(&hash[..], slice); #[cfg(all(feature = "std", feature = "msgbus-redis"))] { + use log::info; let blocknumebr = match get_blocknumber() { None => { info!("[redis] get_blocknumber in [put] is None"); @@ -157,6 +156,7 @@ pub fn kill(key: &[u8]) { // runtime_io::clear_storage(&twox_128(key)[..]); let hash = twox_128(key); #[cfg(all(feature = "std", feature = "msgbus-redis"))] { + use log::info; match get_blocknumber() { None => { info!("[redis] get_blocknumber in [kill] is None"); diff --git a/srml/support/src/storage/redis.rs b/srml/support/src/storage/redis.rs index c2644da4b01e1..51d139f66abb7 100644 --- a/srml/support/src/storage/redis.rs +++ b/srml/support/src/storage/redis.rs @@ -1,13 +1,12 @@ -extern crate parking_lot; -extern crate redis; - -use self::parking_lot::Mutex; - use std::error::Error; use std::string::ToString; -use self::redis::{Connection, RedisError}; -use self::redis::Commands; +use parking_lot::Mutex; +use redis::{Connection, RedisError}; +use redis::Commands; + +use lazy_static::lazy_static; +use log::error; struct RedisClient { conn: Option