Skip to content

Commit

Permalink
Mn/v1.2 (#96)
Browse files Browse the repository at this point in the history
* Add sync_state in rpc

* for revert command

* refactor pending skip

* for build test-chain shell

* add skip conf

* on_block_finalized

* refactor crfg_state_provider

* refactor import_crfg_state_providers

* for revert aux

* perform skip_justification_requests

* remove unused

* improve skip

* Update branch

* for revert aux

* for revert

* for cargo.lock

* for cargo.lock

* for cargo.lock

* for cargo.lock

* for revert and cargo.lock

* for revert

* add peer count

* for Cargo.toml and Cargo.lock

* for Cargo.toml version

Co-authored-by: MayNail <[email protected]>
  • Loading branch information
sman2013 and MayNail authored Oct 23, 2020
1 parent 2516d29 commit eabceea
Show file tree
Hide file tree
Showing 23 changed files with 749 additions and 240 deletions.
110 changes: 61 additions & 49 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "yee"
version = "1.1.1"
version = "1.2.0"
authors = ["YeeCo <[email protected]>"]
build = "build.rs"
edition = "2018"
Expand Down
8 changes: 5 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env bash
set -e

cp -f prebuilt/yee_runtime/mainnet.wasm runtime/wasm/target/wasm32-unknown-unknown/release/yee_runtime_wasm.compact.wasm

if [ "$1" == "test" ];then
cp -f prebuilt/yee_runtime/upgrade/mainnet_v5.wasm runtime/wasm/target/wasm32-unknown-unknown/release/yee_runtime_wasm.compact.wasm
else
cp -f prebuilt/yee_runtime/mainnet.wasm runtime/wasm/target/wasm32-unknown-unknown/release/yee_runtime_wasm.compact.wasm
fi
cargo build --release
4 changes: 4 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ license = "GPL-3.0"

[dependencies]
hyper = "0.11"
app_dirs = "1.2"
tokio-proto = "0.1"
serde = "1.0"
serde_json = "1.0"
Expand All @@ -27,6 +28,7 @@ tokio = "0.1"
exit-future = "0.1"
parking_lot = "0.7.1"
hex-literal = "0.1"
hex = "0.4.2"
parity-codec = "3.2"
trie-root = "0.12.0"
yee-runtime = { path = "../runtime" }
Expand All @@ -46,6 +48,7 @@ runtime_primitives = { package = "sr-primitives", git = "https://github.com/yeec
state_machine = { package = "substrate-state-machine", git = "https://github.com/yeeco/substrate.git", branch = "yee-v1.0" }
fg_primitives = { package = "crfg-primitives", path = "../core/crfg/primitives" }
crfg = { package = "yee-crfg", path = "../core/crfg" }
finality-grandpa = { package = "finality-grandpa", path = "../core/finality-grandpa" }
structopt = "0.2"
ansi_term = "0.11"
yee-switch = { package = "yee-switch", path = "../switch" }
Expand All @@ -64,6 +67,7 @@ foreign_chain = { package = "yee-foreign-chain", path = "../foreign/chain" }
signal-hook = { version = "0.1.11", features = ["tokio-support"] }
yee-context = { package = "yee-context", path = "../core/context" }
finality-tracker = { package = "finality-tracker", path = "../core/finality-tracker" }
fork-tree = { git = "https://github.com/yeeco/substrate.git", branch = "yee-v1.0"}

[features]
custom-wasm-code = []
Expand Down
240 changes: 240 additions & 0 deletions cli/src/chain_revert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
use std::io;
use std::path::{Path, PathBuf};

use app_dirs::{AppDataType, AppInfo};
use log::info;
use parity_codec::{Decode, Encode};
use parity_codec::alloc::collections::HashMap;
use primitives::{H256, ed25519::Public as AuthorityId};
use runtime_primitives::generic::{BlockId, DigestItem};
use runtime_primitives::traits::{Block as BlockT, Digest, DigestItemFor, Header as HeaderT, NumberFor};
use runtime_primitives::traits::As;
use serde_json::Value;
use structopt::StructOpt;
use substrate_cli::SharedParams;
use substrate_cli::VersionInfo;
use substrate_client::backend::AuxStore;
use substrate_service::{ChainSpec, Configuration, FactoryFullConfiguration, FactoryGenesis, new_full_client, new_light_client, RuntimeGenesis, ServiceFactory};
use fork_tree::ForkTree;
use crfg::{authorities, aux_schema, CrfgChangeDigestItem, ScheduledChange};
use finality_grandpa::round::State;
use yee_foreign_network::message::generic::Message::Status;

use crate::chain_spec;
use crate::error;

/// The `revert` command used revert the chain to a previos state.
#[derive(Debug, StructOpt, Clone)]
pub struct RevertCmd {
/// Specify shard number
#[structopt(long = "shard-num", value_name = "SHARD_NUM")]
pub shard_num: u16,

/// Number of blocks to revert.
#[structopt(long = "target", value_name = "TARGET")]
pub target: String,

#[allow(missing_docs)]
#[structopt(flatten)]
pub shared_params: SharedParams,
}

impl substrate_cli::GetLogFilter for RevertCmd {
fn get_log_filter(&self) -> Option<String> {
None
}
}

pub fn revert_chain<F, S>(cli: RevertCmd, version: VersionInfo, spec_factory: S) -> error::Result<()> where
F: ServiceFactory,
S: FnOnce(&str) -> Result<Option<ChainSpec<FactoryGenesis<F>>>, String>,
u64: From<<<F::Block as BlockT>::Header as HeaderT>::Number>,
<<<<F as ServiceFactory>::Block as BlockT>::Header as HeaderT>::Digest as Digest>::Item: CrfgChangeDigestItem<<<<F as ServiceFactory>::Block as BlockT>::Header as HeaderT>::Number>,
{
let target = match serde_json::from_str(cli.target.as_str()) {
Ok(v) => {
let r: HashMap<u16, u64> = v;
r
}
_ => {
panic!("Args: target format error.");
}
};
let (spec_id, mut config) = create_config::<F, _>(spec_factory, &cli.shared_params, &version);

let base_path = base_path(&cli.shared_params, &version);
for (shard, number) in target {
let empty: Vec<u8> = vec![];
let empty = empty.encode();
if shard == cli.shard_num {
config.database_path = db_path(&base_path, spec_id.as_str(), true, shard).to_string_lossy().into();
let client = new_full_client::<F>(&config)?;
let best: <<F::Block as BlockT>::Header as HeaderT>::Number = match client.best_block_header() {
Ok(h) => *h.number(),
Err(e) => panic!("{:?}", e)
};
if number > best.into() {
info!("Shard#{}. Target({}) > Best({})", shard, number, best);
continue;
}
let best = client.revert(As::sa(number))?;
let header = client.header(&BlockId::Number(best))?.expect("can't get header");
let hash = header.hash();

let authorities = match get_authorities::<F::Block>(header, number) {
Ok(v) => v,
Err(e) => panic!("{:?}", e)
};
client.insert_aux(&[(aux_schema::AUTHORITY_SET_KEY, authorities.encode().as_slice())], &[])?;

let state = State {
prevote_ghost: Some((hash, number)),
finalized: Some((hash, number)),
estimate: Some((hash, number)),
completable: true,
};
let set_state = aux_schema::VoterSetState::Live(0u64, state).encode();
client.insert_aux(&[(aux_schema::SET_STATE_KEY, set_state.as_slice())], &[])?;
client.insert_aux(&[(aux_schema::CONSENSUS_CHANGES_KEY, empty.as_slice())], &[])?;
client.insert_aux(&[(aux_schema::PENDING_SKIP_KEY, empty.as_slice())], &[])?;
info!("Reverted shard: {}. Best: #{}", shard, best);
} else {
config.database_path = db_path(&base_path, spec_id.as_str(), false, shard).to_string_lossy().into();
let client = new_light_client::<F>(&config)?;
let best = match client.best_block_header() {
Ok(h) => *h.number(),
Err(e) => panic!("{:?}", e)
};
if number > best.into() {
info!("Shard#{}. Target({}) > Best({})", shard, number, best);
continue;
}
let best = client.revert(As::sa(number))?;
let header = client.header(&BlockId::Number(best))?.expect("can't get header");
let hash = header.hash();
let authorities = match get_authorities::<F::Block>(header, number) {
Ok(v) => v,
Err(e) => panic!("{:?}", e)
};
client.insert_aux(&[(aux_schema::AUTHORITY_SET_KEY, authorities.encode().as_slice())], &[])?;
let state = State {
prevote_ghost: Some((hash, number)),
finalized: Some((hash, number)),
estimate: Some((hash, number)),
completable: true,
};
let set_state = aux_schema::VoterSetState::Live(0u64, state);
client.insert_aux(&[(aux_schema::SET_STATE_KEY, set_state.encode().as_slice())], &[])?;
client.insert_aux(&[(aux_schema::CONSENSUS_CHANGES_KEY, empty.as_slice())], &[])?;
client.insert_aux(&[(aux_schema::PENDING_SKIP_KEY, empty.as_slice())], &[])?;
info!("Reverted shard: {}. Best: #{}", shard, best);
}
}

Ok(())
}

fn create_config<F, S>(
spec_factory: S, cli: &SharedParams, _version: &VersionInfo,
) -> (String, FactoryFullConfiguration<F>)
where
F: ServiceFactory,
S: FnOnce(&str) -> Result<Option<ChainSpec<FactoryGenesis<F>>>, String>,
{
let spec = match load_spec(cli, spec_factory) {
Ok(v) => v,
Err(e) => {
panic!("{:?}", e);
}
};

(spec.id().to_string(), Configuration::default_with_spec(spec.clone()))
}

fn load_spec<F, G>(cli: &SharedParams, factory: F) -> error::Result<ChainSpec<G>>
where G: RuntimeGenesis, F: FnOnce(&str) -> Result<Option<ChainSpec<G>>, String>,
{
let chain_key = get_chain_key(cli);
let spec = match factory(&chain_key)? {
Some(spec) => spec,
None => ChainSpec::from_json_file(PathBuf::from(chain_key))?
};
Ok(spec)
}

fn base_path(cli: &SharedParams, version: &VersionInfo) -> PathBuf {
cli.base_path.clone()
.unwrap_or_else(||
app_dirs::get_app_root(
AppDataType::UserData,
&AppInfo {
name: version.executable_name,
author: version.author,
},
).expect("app directories exist on all supported platforms; qed")
)
}

fn get_chain_key(cli: &SharedParams) -> String {
match cli.chain {
Some(ref chain) => chain.clone(),
None => if cli.dev { "dev".into() } else { "".into() }
}
}

fn db_path(base_path: &Path, chain_id: &str, is_full: bool, shard_num: u16) -> PathBuf {
let mut path = base_path.to_owned();
path.push("chains");
path.push(chain_id);
if is_full {
path.push("db");
} else {
path.push(format!("db-{}", shard_num));
}
path
}

type Hash = [u8; 32];

fn get_authorities<B: BlockT>(header: B::Header, number: u64) -> Result<authorities::AuthoritySet<Hash, u64>, String>
where
DigestItemFor<B>: CrfgChangeDigestItem<NumberFor<B>>,
<<<B as BlockT>::Header as HeaderT>::Digest as Digest>::Item: CrfgChangeDigestItem<<<B as BlockT>::Header as HeaderT>::Number>,
{
let change: Option<ScheduledChange<<B::Header as HeaderT>::Number>> = header.digest().logs().iter()
.filter_map(CrfgChangeDigestItem::as_change)
.next();

match change {
Some(v) => {
let authorities = aggregate_authorities(v.next_authorities);
let mut authority_set = authorities::AuthoritySet::<Hash, u64> {
current_authorities: authorities,
set_id: number,
pending_standard_changes: ForkTree::new(),
pending_forced_changes: Vec::new(),
};
authority_set.pending_standard_changes.best_finalized_number = Some(number);
Ok(authority_set)
}
None => Err(String::from("can't get ScheduledChange"))
}
}

fn aggregate_authorities(authorities: Vec<(AuthorityId, u64)>) -> Vec<(AuthorityId, u64)> {
let mut polymer: Vec<(AuthorityId, u64)> = Vec::new();

for author in authorities {
match polymer.iter().position(|x| x.0 == author.0){
Some(pos) => {
let reappear = polymer.get_mut(pos);
reappear.unwrap().1 += 1;
},
None => {
polymer.push((author.0, author.1));
}
}
}

polymer
}
22 changes: 20 additions & 2 deletions cli/src/custom_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@ use structopt::{StructOpt, clap::{SubCommand}};
use yee_switch;
use yee_bootnodes_router;
use substrate_cli::VersionInfo;
use substrate_service::{ChainSpec, FactoryGenesis, ServiceFactory};
use crate::chain_revert::{RevertCmd, revert_chain};
use runtime_primitives::traits::{Block as BlockT, Digest, DigestItemFor, Header as HeaderT, NumberFor};
use crfg::{aux_schema, authorities, CrfgChangeDigestItem, ScheduledChange};

//use yee_switch::SwitchCommandCmd;
#[derive(Clone, Debug)]
pub enum CustomCommand {
SwitchCommandCmd(yee_switch::params::SwitchCommandCmd),
BootnodesRouterCommandCmd(yee_bootnodes_router::params::BootnodesRouterCommandCmd),
Revert(RevertCmd),
None,
}

Expand All @@ -39,6 +44,10 @@ impl StructOpt for CustomCommand {
.subcommand(
yee_bootnodes_router::params::BootnodesRouterCommandCmd::augment_clap(SubCommand::with_name("bootnodes-router"))
.about("Yee bootnodes router"))
.subcommand(
RevertCmd::augment_clap(SubCommand::with_name("revert"))
.about("Yee revert block")
)
}

fn from_clap(matches: &::structopt::clap::ArgMatches) -> Self {
Expand All @@ -47,6 +56,8 @@ impl StructOpt for CustomCommand {
CustomCommand::SwitchCommandCmd(yee_switch::params::SwitchCommandCmd::from_clap(matches)),
("bootnodes-router", Some(matches)) =>
CustomCommand::BootnodesRouterCommandCmd(yee_bootnodes_router::params::BootnodesRouterCommandCmd::from_clap(matches)),
("revert", Some(matches)) =>
CustomCommand::Revert(RevertCmd::from_clap(matches)),
(_, Some(_)) => CustomCommand::None,
(_, None) => CustomCommand::None,
}
Expand All @@ -58,17 +69,24 @@ impl GetLogFilter for CustomCommand {
match self {
CustomCommand::SwitchCommandCmd(cmd) => cmd.get_log_filter(),
CustomCommand::BootnodesRouterCommandCmd(cmd) => cmd.get_log_filter(),
CustomCommand::Revert(cmd) => cmd.get_log_filter(),
CustomCommand::None => None
}
}
}

pub fn run_custom_command<F, E, S>(params : Option<(CustomCommand, S, E, VersionInfo)>) -> substrate_cli::error::Result<()> {
pub fn run_custom_command<F, E, S>(params : Option<(CustomCommand, S, E, VersionInfo)>) -> substrate_cli::error::Result<()> where
F: ServiceFactory,
S: FnOnce(&str) -> Result<Option<ChainSpec<FactoryGenesis<F>>>, String>,
u64: From<<<F::Block as BlockT>::Header as HeaderT>::Number>,
<<<<F as ServiceFactory>::Block as BlockT>::Header as HeaderT>::Digest as Digest>::Item: CrfgChangeDigestItem<<<<F as ServiceFactory>::Block as BlockT>::Header as HeaderT>::Number>,
{

match params{
Some((custom_command, _spec_factory, _exit, version))=> match custom_command{
Some((custom_command, spec_factory, _exit, version))=> match custom_command{
CustomCommand::SwitchCommandCmd(cmd) => Ok(yee_switch::run(cmd, version).map_err(|e| format!("{:?}", e))?),
CustomCommand::BootnodesRouterCommandCmd(cmd) => Ok(yee_bootnodes_router::run(cmd, version).map_err(|e| format!("{:?}", e))?),
CustomCommand::Revert(cmd) => Ok(revert_chain::<F, S>(cmd, version, spec_factory).map_err(|e|format!("{:?}", e))?),
CustomCommand::None => Ok(())
},
None => Ok(())
Expand Down
8 changes: 7 additions & 1 deletion cli/src/custom_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use substrate_service::{
use substrate_client::ChainHead;
use runtime_primitives::{
generic::BlockId,
traits::{ProvideRuntimeApi, Header, Digest as DigestT, DigestItemFor, Zero, Block as BlockT, NumberFor},
traits::{ProvideRuntimeApi, Header, Digest as DigestT, DigestItemFor, Zero, Block as BlockT, NumberFor, As},
};
use crate::error;
use crate::service::{NodeConfig, NativeExecutor};
Expand Down Expand Up @@ -96,6 +96,10 @@ pub struct YeeCliConfig {
#[structopt(long = "import-until")]
pub import_until: Option<String>,

/// Max leading blocks.
#[structopt(long = "import-leading")]
pub import_leading: Option<u64>,

/// Pow job cache size
#[structopt(long = "job-cache-size")]
pub job_cache_size: Option<u32>,
Expand Down Expand Up @@ -216,6 +220,7 @@ where
config.custom.foreign_in_peers = custom_args.foreign_in_peers;
config.custom.mine = custom_args.mine;
config.custom.import_until = get_import_until::<F::Block>(&custom_args.import_until).ok();
config.custom.import_leading = custom_args.import_leading.map(As::sa);
config.custom.job_cache_size = custom_args.job_cache_size;

config.custom.context = Some(context);
Expand All @@ -231,6 +236,7 @@ where
info!(" bootnodes router conf: {:?}", config.custom.bootnodes_router_conf);
info!(" mine: {:?}", config.custom.mine);
info!(" import_until: {:?}", config.custom.import_until);
info!(" import_leading: {:?}", config.custom.import_leading);

register_inherent_data_provider(&config.custom.inherent_data_providers, shard_num, shard_count, scale_out)
.map_err(|e| format!("Inherent data error: {:?}", e))?;
Expand Down
Loading

0 comments on commit eabceea

Please sign in to comment.