Skip to content

Commit

Permalink
update msgbus-redis for new substrate
Browse files Browse the repository at this point in the history
  • Loading branch information
Aton committed Feb 11, 2019
1 parent 474ee9d commit 5bf3d03
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 149 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

103 changes: 22 additions & 81 deletions core/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -365,41 +349,30 @@ where
),
};

#[cfg(not(feature = "msgbus-redis"))]
let role =
if cli.light {
config.block_execution_strategy = service::ExecutionStrategy::NativeWhenPossible;
service::Roles::LIGHT
} 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();
Expand Down Expand Up @@ -459,17 +432,17 @@ where
S: FnOnce(&str) -> Result<Option<ChainSpec<FactoryGenesis<F>>>, String>,
RS: FnOnce(E, RP, FactoryFullConfiguration<F>) -> Result<(), String>,
{
let config = create_run_node_config::<F, _>(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::<F, _>(cli.left, spec_factory, impl_name, version)?;

run_service(exit, cli.right, config).map_err(Into::into)
}
Expand Down Expand Up @@ -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<Read> = match cli.input {
Some(filename) => Box::new(File::open(filename)?),
None => Box::new(stdin()),
Expand Down
4 changes: 2 additions & 2 deletions core/cli/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ pub struct RunCmd {
#[structopt(long = "key", value_name = "STRING")]
pub key: Option<String>,

//#[cfg(not(feature = "msgbus-redis"))]
#[cfg(not(feature = "msgbus-redis"))]
/// Enable validator mode
#[structopt(long = "validator")]
pub validator: bool,

#[cfg(feature = "msgbus-redis")]
/// Specify redis connect addr. default is (127.0.0.1)
#[structopt(long = "redis", value_name = "URL")]
redis: Option<String>,
pub redis: Option<String>,

/// Run in light client mode
#[structopt(long = "light")]
Expand Down
16 changes: 0 additions & 16 deletions core/client/src/block_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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*/
}
2 changes: 1 addition & 1 deletion core/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ impl<Components: components::Components> Service<Components> {
//_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<primitives::ed25519::Pair> {
Expand Down
2 changes: 1 addition & 1 deletion node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
5 changes: 3 additions & 2 deletions node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -114,7 +115,7 @@ construct_service_factory! {
grandpa::NetworkBridge::new(service.network()),
service.on_exit(),
)?);

}
Ok(service)
}
},
Expand Down
25 changes: 0 additions & 25 deletions srml/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
14 changes: 8 additions & 6 deletions srml/support/src/storage/blocknumber.rs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

Expand Down
16 changes: 8 additions & 8 deletions srml/support/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64> {
use self::blocknumber::blocknumber_hashedkey;
let key = blocknumber_hashedkey();

runtime_io::read_storage(&key[..], &mut [0; 0][..], 0).map(|_| {
Expand All @@ -48,8 +47,7 @@ pub fn get_blocknumber() -> Option<u64> {
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],
Expand Down Expand Up @@ -103,6 +101,7 @@ pub fn put<T: Codec>(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");
Expand Down Expand Up @@ -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");
Expand Down
13 changes: 6 additions & 7 deletions srml/support/src/storage/redis.rs
Original file line number Diff line number Diff line change
@@ -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<Connection>
Expand Down

0 comments on commit 5bf3d03

Please sign in to comment.