Skip to content

Commit

Permalink
Merge pull request #92 from AurevoirXavier/chainrelay
Browse files Browse the repository at this point in the history
withdraw Ring/Kton automatically
  • Loading branch information
hackfisher authored Nov 20, 2019
2 parents fb0be5d + 3b9c1a1 commit 0c88d53
Show file tree
Hide file tree
Showing 31 changed files with 4,763 additions and 2,051 deletions.
23 changes: 16 additions & 7 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ members = [

"srml/support",

"srml/chainrelay/bridge/eos",
"srml/chainrelay/bridge/ethereum",

"srml/balances",
"srml/kton",
"srml/staking",

"srml/chainrelay/bridge/ethereum",
]

exclude = ["node/runtime/wasm"]
Expand Down
7 changes: 3 additions & 4 deletions core/sr-rlp/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// RLP lib for eth transaction encoding

/// RLP lib for eth transaction encoding
#![recursion_limit = "128"]
#![cfg_attr(not(feature = "std"), no_std)]
//#![recursion_limit = "128"]
//#![cfg_attr(not(feature = "std"), no_std)]
3 changes: 2 additions & 1 deletion node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ runtime-io = { package = "sr-io", git = 'https://github.com/darwinia-network/sub
client = { package = "substrate-client", git = 'https://github.com/darwinia-network/substrate.git' }
primitives = { package = "substrate-primitives", git = 'https://github.com/darwinia-network/substrate.git' }
inherents = { package = "substrate-inherents", git = 'https://github.com/darwinia-network/substrate.git' }
node-runtime = { path = "../runtime" }
node-primitives = { path = "../primitives" }
hex-literal = "0.2.1"
substrate-basic-authorship = { git = 'https://github.com/darwinia-network/substrate.git' }
Expand Down Expand Up @@ -60,6 +59,8 @@ sr-authority-discovery = { package = "srml-authority-discovery", git = 'https://
authority-discovery = { package = "substrate-authority-discovery", git = 'https://github.com/darwinia-network/substrate.git' }
offchain = { package = "substrate-offchain", git = 'https://github.com/darwinia-network/substrate.git' }

node-runtime = { path = "../runtime" }

[dev-dependencies]
consensus-common = { package = "substrate-consensus-common", git = 'https://github.com/darwinia-network/substrate.git' }
service-test = { package = "substrate-service-test", git = 'https://github.com/darwinia-network/substrate.git' }
Expand Down
91 changes: 45 additions & 46 deletions node/cli/src/factory_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@
//! using the cli to manufacture transactions and distribute them
//! to accounts.
use rand::{Rng, SeedableRng};
use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};

use codec::{Encode, Decode};
use codec::{Decode, Encode};
use finality_tracker;
use inherents::InherentData;
use keyring::sr25519::Keyring;
use node_primitives::Signature;
use node_runtime::{
Call, CheckedExtrinsic, UncheckedExtrinsic, SignedExtra, BalancesCall, ExistentialDeposit,
MinimumPeriod
BalancesCall, Call, CheckedExtrinsic, ExistentialDeposit, MinimumPeriod, SignedExtra, UncheckedExtrinsic,
};
use node_primitives::Signature;
use primitives::{sr25519, crypto::Pair};
use primitives::{crypto::Pair, sr25519};
use sr_primitives::{
generic::Era, traits::{Block as BlockT, Header as HeaderT, SignedExtension, Verify, IdentifyAccount}
generic::Era,
traits::{Block as BlockT, Header as HeaderT, IdentifyAccount, SignedExtension, Verify},
};
use transaction_factory::RuntimeAdapter;
use transaction_factory::modes::Mode;
use inherents::InherentData;
use timestamp;
use finality_tracker;
use transaction_factory::modes::Mode;
use transaction_factory::RuntimeAdapter;

type AccountPublic = <Signature as Verify>::Signer;

Expand Down Expand Up @@ -77,11 +77,7 @@ impl RuntimeAdapter for FactoryState<Number> {

type Number = Number;

fn new(
mode: Mode,
num: u64,
rounds: u64,
) -> FactoryState<Self::Number> {
fn new(mode: Mode, num: u64, rounds: u64) -> FactoryState<Self::Number> {
FactoryState {
mode,
num: num as u32,
Expand Down Expand Up @@ -145,24 +141,28 @@ impl RuntimeAdapter for FactoryState<Number> {
) -> <Self::Block as BlockT>::Extrinsic {
let index = self.extract_index(&sender, prior_block_hash);
let phase = self.extract_phase(*prior_block_hash);
sign::<Self>(CheckedExtrinsic {
signed: Some((sender.clone(), Self::build_extra(index, phase))),
function: Call::Balances(
BalancesCall::transfer(
sign::<Self>(
CheckedExtrinsic {
signed: Some((sender.clone(), Self::build_extra(index, phase))),
function: Call::Balances(BalancesCall::transfer(
indices::address::Address::Id(destination.clone().into()),
(*amount).into()
)
)
}, key, (version, genesis_hash.clone(), prior_block_hash.clone(), (), (), (), ()))
(*amount).into(),
)),
},
key,
(version, genesis_hash.clone(), prior_block_hash.clone(), (), (), (), ()),
)
}

fn inherent_extrinsics(&self) -> InherentData {
let timestamp = (self.block_no as u64 + 1) * MinimumPeriod::get();

let mut inherent = InherentData::new();
inherent.put_data(timestamp::INHERENT_IDENTIFIER, &timestamp)
inherent
.put_data(timestamp::INHERENT_IDENTIFIER, &timestamp)
.expect("Failed putting timestamp inherent");
inherent.put_data(finality_tracker::INHERENT_IDENTIFIER, &self.block_no)
inherent
.put_data(finality_tracker::INHERENT_IDENTIFIER, &self.block_no)
.expect("Failed putting finalized number inherent");
inherent
}
Expand Down Expand Up @@ -191,11 +191,7 @@ impl RuntimeAdapter for FactoryState<Number> {
pair
}

fn extract_index(
&self,
_account_id: &Self::AccountId,
_block_hash: &<Self::Block as BlockT>::Hash,
) -> Self::Index {
fn extract_index(&self, _account_id: &Self::AccountId, _block_hash: &<Self::Block as BlockT>::Hash) -> Self::Index {
// TODO get correct index for account via api. See #2587.
// This currently prevents the factory from being used
// without a preceding purge of the database.
Expand All @@ -204,20 +200,21 @@ impl RuntimeAdapter for FactoryState<Number> {
} else {
match self.round() {
0 =>
// if round is 0 all transactions will be done with master as a sender
self.block_no() as Self::Index,
// if round is 0 all transactions will be done with master as a sender
{
self.block_no() as Self::Index
}
_ =>
// if round is e.g. 1 every sender account will be new and not yet have
// any transactions done
// if round is e.g. 1 every sender account will be new and not yet have
// any transactions done
{
0
}
}
}
}

fn extract_phase(
&self,
_block_hash: <Self::Block as BlockT>::Hash
) -> Self::Phase {
fn extract_phase(&self, _block_hash: <Self::Block as BlockT>::Hash) -> Self::Phase {
// TODO get correct phase via api. See #2587.
// This currently prevents the factory from being used
// without a preceding purge of the database.
Expand Down Expand Up @@ -245,13 +242,15 @@ fn sign<RA: RuntimeAdapter>(
let s = match xt.signed {
Some((signed, extra)) => {
let payload = (xt.function, extra.clone(), additional_signed);
let signature = payload.using_encoded(|b| {
if b.len() > 256 {
key.sign(&runtime_io::hashing::blake2_256(b))
} else {
key.sign(b)
}
}).into();
let signature = payload
.using_encoded(|b| {
if b.len() > 256 {
key.sign(&runtime_io::hashing::blake2_256(b))
} else {
key.sign(b)
}
})
.into();
UncheckedExtrinsic {
signature: Some((indices::address::Address::Id(signed), signature, extra)),
function: payload.0,
Expand Down
56 changes: 24 additions & 32 deletions node/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ where
ParseAndPrepare::Run(cmd) => cmd.run(
load_spec,
exit,
|exit, _cli_args, _custom_args, config: Config<_,_>| {
|exit, _cli_args, _custom_args, config: Config<_, _>| {
info!("{}", version.name);
info!(" version {}", config.full_version());
info!(" _____ _ _ ");
Expand All @@ -180,37 +180,31 @@ where
info!("Chain specification: {}", config.chain_spec.name());
info!("Node name: {}", config.name);
info!("Roles: {:?}", cli::display_role(&config));
let runtime = RuntimeBuilder::new().name_prefix("main-tokio-").build()
let runtime = RuntimeBuilder::new()
.name_prefix("main-tokio-")
.build()
.map_err(|e| format!("{:?}", e))?;
match config.roles {
ServiceRoles::LIGHT => run_until_exit(
runtime,
service::new_light(config)?,
exit
),
_ => run_until_exit(
runtime,
service::new_full(config)?,
exit
),
ServiceRoles::LIGHT => run_until_exit(runtime, service::new_light(config)?, exit),
_ => run_until_exit(runtime, service::new_full(config)?, exit),
}
},
),

ParseAndPrepare::BuildSpec(cmd) => cmd.run::<NoCustom, _, _, _>(load_spec),
ParseAndPrepare::ExportBlocks(cmd) => cmd.run_with_builder(|config: Config<_,_>|
Ok(new_full_start!(config).0), load_spec, exit),
ParseAndPrepare::ImportBlocks(cmd) => cmd.run_with_builder(|config: Config<_,_>|
Ok(new_full_start!(config).0), load_spec, exit),
ParseAndPrepare::ExportBlocks(cmd) => {
cmd.run_with_builder(|config: Config<_, _>| Ok(new_full_start!(config).0), load_spec, exit)
}
ParseAndPrepare::ImportBlocks(cmd) => {
cmd.run_with_builder(|config: Config<_, _>| Ok(new_full_start!(config).0), load_spec, exit)
}
ParseAndPrepare::PurgeChain(cmd) => cmd.run(load_spec),
ParseAndPrepare::RevertChain(cmd) => cmd.run_with_builder(|config: Config<_,_>|
Ok(new_full_start!(config).0), load_spec),
ParseAndPrepare::RevertChain(cmd) => {
cmd.run_with_builder(|config: Config<_, _>| Ok(new_full_start!(config).0), load_spec)
}
ParseAndPrepare::CustomCommand(CustomSubcommands::Factory(cli_args)) => {
let mut config: Config<_, _> = cli::create_config_with_db_path(
load_spec,
&cli_args.shared_params,
&version,
)?;
let mut config: Config<_, _> =
cli::create_config_with_db_path(load_spec, &cli_args.shared_params, &version)?;
config.execution_strategies = ExecutionStrategies {
importing: cli_args.execution.into(),
block_construction: cli_args.execution.into(),
Expand All @@ -219,23 +213,21 @@ where
};

match ChainSpec::from(config.chain_spec.id()) {
Some(ref c) if c == &ChainSpec::Development || c == &ChainSpec::LocalTestnet => {},
Some(ref c) if c == &ChainSpec::Development || c == &ChainSpec::LocalTestnet => {}
_ => panic!("Factory is only supported for development and local testnet."),
}

let factory_state = FactoryState::new(
cli_args.mode.clone(),
cli_args.num,
cli_args.rounds,
);
let factory_state = FactoryState::new(cli_args.mode.clone(), cli_args.num, cli_args.rounds);

let service_builder = new_full_start!(config).0;
transaction_factory::factory::<FactoryState<_>, _, _, _, _, _>(
factory_state,
service_builder.client(),
service_builder.select_chain()
.expect("The select_chain is always initialized by new_full_start!; QED")
).map_err(|e| format!("Error in transaction factory: {}", e))?;
service_builder
.select_chain()
.expect("The select_chain is always initialized by new_full_start!; QED"),
)
.map_err(|e| format!("Error in transaction factory: {}", e))?;

Ok(())
}
Expand Down
Loading

0 comments on commit 0c88d53

Please sign in to comment.