Transaction factory (#2481)

* Fix typos

* Add transaction factory

`cargo run -- purge-chain -y --chain dev && cargo run -- --dev --transaction-factory 10`

* Fix comment and remove build deps

* Move crate to test-utils

* Switch from flag to subcommand

`cargo run -- factory --dev --num 5`

* Decouple factory from node specifics

* Introduce different manufacturing modes

* Remove unrelated changes

* Update Cargo.lock

* Use SelectChain to fetch best block

* Improve expect proof

* Panic if factory executed with unsupported chain spec

* Link ToDo comments to follow-up ticket

* Address comments and improve style

* Remove unused dependencies

* Fix indent level

* Replace naked unwrap

* Update node/cli/src/factory_impl.rs

* Fix typo

* Use inherent_extrinsics instead of timestamp

* Generalize factory and remove saturated conversions

* Format imports

* Make it clearer that database needs to be empty

* Ensure factory settings

* Apply suggestions from code review

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Update test-utils/transaction-factory/src/lib.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Fix match guard syntax

* Simplify import, remove empty line

* Update node/cli/Cargo.toml

* Update lockfile
This commit is contained in:
Michael Müller
2019-05-28 17:01:43 +02:00
committed by Gavin Wood
parent 811124234d
commit a706d994cb
14 changed files with 898 additions and 12 deletions
+7
View File
@@ -30,6 +30,13 @@ sr-primitives = { path = "../../core/sr-primitives" }
node-executor = { path = "../executor" }
substrate-keystore = { path = "../../core/keystore" }
substrate-telemetry = { package = "substrate-telemetry", path = "../../core/telemetry" }
structopt = "0.2"
transaction-factory = { path = "../../test-utils/transaction-factory" }
keyring = { package = "substrate-keyring", path = "../../core/keyring" }
indices = { package = "srml-indices", path = "../../srml/indices" }
timestamp = { package = "srml-timestamp", path = "../../srml/timestamp", default-features = false }
rand = "0.6"
finality_tracker = { package = "srml-finality-tracker", path = "../../srml/finality-tracker", default-features = false }
[dev-dependencies]
service-test = { package = "substrate-service-test", path = "../../core/service/test" }
+259
View File
@@ -0,0 +1,259 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Implementation of the transaction factory trait, which enables
//! using the cli to manufacture transactions and distribute them
//! to accounts.
use rand::{Rng, SeedableRng};
use rand::rngs::StdRng;
use parity_codec::Decode;
use keyring::sr25519::Keyring;
use node_primitives::Hash;
use node_runtime::{Call, CheckedExtrinsic, UncheckedExtrinsic, BalancesCall};
use primitives::sr25519;
use primitives::crypto::Pair;
use parity_codec::Encode;
use sr_primitives::generic::Era;
use sr_primitives::traits::{Block as BlockT, Header as HeaderT};
use substrate_service::ServiceFactory;
use transaction_factory::RuntimeAdapter;
use transaction_factory::modes::Mode;
use crate::service;
use inherents::InherentData;
use timestamp;
use finality_tracker;
// TODO get via api: <timestamp::Module<T>>::minimum_period(). See #2587.
const MINIMUM_PERIOD: u64 = 99;
pub struct FactoryState<N> {
block_no: N,
mode: Mode,
start_number: u64,
rounds: u64,
round: u64,
block_in_round: u64,
num: u64,
}
type Number = <<node_primitives::Block as BlockT>::Header as HeaderT>::Number;
impl RuntimeAdapter for FactoryState<Number> {
type AccountId = node_primitives::AccountId;
type Balance = node_primitives::Balance;
type Block = node_primitives::Block;
type Phase = sr_primitives::generic::Phase;
type Secret = sr25519::Pair;
type Index = node_primitives::Index;
type Number = Number;
fn new(
mode: Mode,
num: u64,
rounds: u64,
) -> FactoryState<Self::Number> {
FactoryState {
mode,
num: num,
round: 0,
rounds,
block_in_round: 0,
block_no: 0,
start_number: 0,
}
}
fn block_no(&self) -> Self::Number {
self.block_no
}
fn block_in_round(&self) -> Self::Number {
self.block_in_round
}
fn rounds(&self) -> Self::Number {
self.rounds
}
fn num(&self) -> Self::Number {
self.num
}
fn round(&self) -> Self::Number {
self.round
}
fn start_number(&self) -> Self::Number {
self.start_number
}
fn mode(&self) -> &Mode {
&self.mode
}
fn set_block_no(&mut self, val: Self::Number) {
self.block_no = val;
}
fn set_block_in_round(&mut self, val: Self::Number) {
self.block_in_round = val;
}
fn set_round(&mut self, val: Self::Number) {
self.round = val;
}
fn transfer_extrinsic(
&self,
sender: &Self::AccountId,
key: &Self::Secret,
destination: &Self::AccountId,
amount: &Self::Number,
prior_block_hash: &<Self::Block as BlockT>::Hash,
) -> <Self::Block as BlockT>::Extrinsic {
let index = self.extract_index(&sender, prior_block_hash);
let phase = self.extract_phase(*prior_block_hash);
sign::<service::Factory, Self>(CheckedExtrinsic {
signed: Some((sender.clone(), index)),
function: Call::Balances(
BalancesCall::transfer(
indices::address::Address::Id(
destination.clone().into()
),
(*amount).into()
)
)
}, key, &prior_block_hash, phase)
}
fn inherent_extrinsics(&self) -> InherentData {
let timestamp = self.block_no * MINIMUM_PERIOD;
let mut inherent = InherentData::new();
inherent.put_data(timestamp::INHERENT_IDENTIFIER, &timestamp)
.expect("Failed putting timestamp inherent");
inherent.put_data(finality_tracker::INHERENT_IDENTIFIER, &self.block_no)
.expect("Failed putting finalized number inherent");
inherent
}
fn minimum_balance() -> Self::Number {
// TODO get correct amount via api. See #2587.
1337
}
fn master_account_id() -> Self::AccountId {
Keyring::Alice.pair().public()
}
fn master_account_secret() -> Self::Secret {
Keyring::Alice.pair()
}
/// Generates a random `AccountId` from `seed`.
fn gen_random_account_id(seed: &Self::Number) -> Self::AccountId {
let pair: sr25519::Pair = sr25519::Pair::from_seed(gen_seed_bytes(*seed));
pair.public().into()
}
/// Generates a random `Secret` from `seed`.
fn gen_random_account_secret(seed: &Self::Number) -> Self::Secret {
let pair: sr25519::Pair = sr25519::Pair::from_seed(gen_seed_bytes(*seed));
pair
}
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.
if self.mode == Mode::MasterToN || self.mode == Mode::MasterTo1 {
self.block_no()
} else {
match self.round() {
0 =>
// if round is 0 all transactions will be done with master as a sender
self.block_no(),
_ =>
// 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 {
// TODO get correct phase via api. See #2587.
// This currently prevents the factory from being used
// without a preceding purge of the database.
self.block_no
}
}
fn gen_seed_bytes(seed: u64) -> [u8; 32] {
let mut rng: StdRng = SeedableRng::seed_from_u64(seed);
let mut seed_bytes = [0u8; 32];
for i in 0..32 {
seed_bytes[i] = rng.gen::<u8>();
}
seed_bytes
}
/// Creates an `UncheckedExtrinsic` containing the appropriate signature for
/// a `CheckedExtrinsics`.
fn sign<F: ServiceFactory, RA: RuntimeAdapter>(
xt: CheckedExtrinsic,
key: &sr25519::Pair,
prior_block_hash: &Hash,
phase: u64,
) -> <RA::Block as BlockT>::Extrinsic {
let s = match xt.signed {
Some((signed, index)) => {
let era = Era::mortal(256, phase);
let payload = (index.into(), xt.function, era, prior_block_hash);
let signature = payload.using_encoded(|b| {
if b.len() > 256 {
key.sign(&sr_io::blake2_256(b))
} else {
key.sign(b)
}
}).into();
UncheckedExtrinsic {
signature: Some((indices::address::Address::Id(signed), signature, payload.0, era)),
function: payload.1,
}
}
None => UncheckedExtrinsic {
signature: None,
function: xt.function,
},
};
let e = Encode::encode(&s);
Decode::decode(&mut &e[..]).expect("Failed to decode signed unchecked extrinsic")
}
+99 -4
View File
@@ -22,16 +22,21 @@
pub use cli::error;
pub mod chain_spec;
mod service;
mod factory_impl;
use tokio::prelude::Future;
use tokio::runtime::{Builder as RuntimeBuilder, Runtime};
pub use cli::{VersionInfo, IntoExit, NoCustom};
pub use cli::{VersionInfo, IntoExit, NoCustom, SharedParams};
use substrate_service::{ServiceFactory, Roles as ServiceRoles};
use std::ops::Deref;
use log::info;
use structopt::{StructOpt, clap::App};
use cli::{AugmentClap, GetLogFilter};
use crate::factory_impl::FactoryState;
use transaction_factory::RuntimeAdapter;
/// The chain specification option.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
pub enum ChainSpec {
/// Whatever the current runtime is, with just Alice as an auth.
Development,
@@ -43,6 +48,68 @@ pub enum ChainSpec {
StagingTestnet,
}
/// Custom subcommands.
#[derive(Clone, Debug, StructOpt)]
pub enum CustomSubcommands {
/// The custom factory subcommmand for manufacturing transactions.
#[structopt(
name = "factory",
about = "Manufactures num transactions from Alice to random accounts. \
Only supported for development or local testnet."
)]
Factory(FactoryCmd),
}
impl GetLogFilter for CustomSubcommands {
fn get_log_filter(&self) -> Option<String> {
None
}
}
/// The `factory` command used to generate transactions.
/// Please note: this command currently only works on an empty database!
#[derive(Debug, StructOpt, Clone)]
pub struct FactoryCmd {
/// How often to repeat. This option only has an effect in mode `MasterToNToM`.
#[structopt(long="rounds", default_value = "1")]
pub rounds: u64,
/// MasterToN: Manufacture `num` transactions from the master account
/// to `num` randomly created accounts, one each.
///
/// MasterTo1: Manufacture `num` transactions from the master account
/// to exactly one other randomly created account.
///
/// MasterToNToM: Manufacture `num` transactions from the master account
/// to `num` randomly created accounts.
/// From each of these randomly created accounts manufacture
/// a transaction to another randomly created account.
/// Repeat this `rounds` times. If `rounds` = 1 the behavior
/// is the same as `MasterToN`.{n}
/// A -> B, A -> C, A -> D, ... x `num`{n}
/// B -> E, C -> F, D -> G, ...{n}
/// ... x `rounds`
///
/// These three modes control manufacturing.
#[structopt(long="mode", default_value = "MasterToN")]
pub mode: transaction_factory::Mode,
/// Number of transactions to generate. In mode `MasterNToNToM` this is
/// the number of transactions per round.
#[structopt(long="num", default_value = "8")]
pub num: u64,
#[allow(missing_docs)]
#[structopt(flatten)]
pub shared_params: SharedParams,
}
impl AugmentClap for FactoryCmd {
fn augment_clap<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
FactoryCmd::augment_clap(app)
}
}
/// Get a chain config from a spec setting.
impl ChainSpec {
pub(crate) fn load(self) -> Result<chain_spec::ChainSpec, String> {
@@ -78,7 +145,7 @@ pub fn run<I, T, E>(args: I, exit: E, version: cli::VersionInfo) -> error::Resul
T: Into<std::ffi::OsString> + Clone,
E: IntoExit,
{
cli::parse_and_execute::<service::Factory, NoCustom, NoCustom, _, _, _, _, _>(
let ret = cli::parse_and_execute::<service::Factory, CustomSubcommands, NoCustom, _, _, _, _, _>(
load_spec, &version, "substrate-node", args, exit,
|exit, _cli_args, _custom_args, config| {
info!("{}", version.name);
@@ -103,7 +170,35 @@ pub fn run<I, T, E>(args: I, exit: E, version: cli::VersionInfo) -> error::Resul
),
}.map_err(|e| format!("{:?}", e))
}
).map_err(Into::into).map(|_| ())
);
match &ret {
Ok(Some(CustomSubcommands::Factory(cli_args))) => {
let config = cli::create_config_with_db_path::<service::Factory, _>(
load_spec,
&cli_args.shared_params,
&version,
)?;
match ChainSpec::from(config.chain_spec.id()) {
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,
);
transaction_factory::factory::<service::Factory, FactoryState<_>>(
factory_state,
config,
).map_err(|e| format!("Error in transaction factory: {}", e))?;
Ok(())
},
_ => ret.map_err(Into::into).map(|_| ())
}
}
fn run_until_exit<T, C, E>(