Refactor tx factory 1 (#4870)

* Remove modes.

* Refactor.
This commit is contained in:
Marcio Diaz
2020-02-11 13:59:52 +01:00
committed by GitHub
parent b0a749548d
commit a90b4fdb8c
7 changed files with 113 additions and 540 deletions
+6 -27
View File
@@ -52,34 +52,13 @@ pub enum Subcommand {
/// 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,
/// Number of blocks to generate.
#[structopt(long="blocks", default_value = "1")]
pub blocks: u32,
/// 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: node_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,
/// Number of transactions to push per block.
#[structopt(long="transactions", default_value = "8")]
pub transactions: u32,
#[allow(missing_docs)]
#[structopt(flatten)]
+2 -3
View File
@@ -67,9 +67,8 @@ where
}
let factory_state = FactoryState::new(
cli_args.mode.clone(),
cli_args.num,
cli_args.rounds,
cli_args.blocks,
cli_args.transactions,
);
let service_builder = new_full_start!(config).0;
+30 -94
View File
@@ -33,7 +33,6 @@ use sp_runtime::{
generic::Era, traits::{Block as BlockT, Header as HeaderT, SignedExtension, Verify, IdentifyAccount}
};
use node_transaction_factory::RuntimeAdapter;
use node_transaction_factory::modes::Mode;
use sp_inherents::InherentData;
use sp_timestamp;
use sp_finality_tracker;
@@ -41,14 +40,10 @@ use sp_finality_tracker;
type AccountPublic = <Signature as Verify>::Signer;
pub struct FactoryState<N> {
block_no: N,
mode: Mode,
start_number: u32,
rounds: u32,
round: u32,
block_in_round: u32,
num: u32,
blocks: u32,
transactions: u32,
block_number: N,
index: u32,
}
type Number = <<node_primitives::Block as BlockT>::Header as HeaderT>::Number;
@@ -78,63 +73,35 @@ impl RuntimeAdapter for FactoryState<Number> {
type Number = Number;
fn new(
mode: Mode,
num: u64,
rounds: u64,
blocks: u32,
transactions: u32,
) -> FactoryState<Self::Number> {
FactoryState {
mode,
num: num as u32,
round: 0,
rounds: rounds as u32,
block_in_round: 0,
block_no: 0,
start_number: 0,
blocks,
transactions,
block_number: 0,
index: 0,
}
}
fn block_no(&self) -> Self::Number {
self.block_no
fn block_number(&self) -> u32 {
self.block_number
}
fn block_in_round(&self) -> Self::Number {
self.block_in_round
fn blocks(&self) -> u32 {
self.blocks
}
fn rounds(&self) -> Self::Number {
self.rounds
fn transactions(&self) -> u32 {
self.transactions
}
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 set_block_number(&mut self, value: u32) {
self.block_number = value;
}
fn transfer_extrinsic(
&self,
&mut self,
sender: &Self::AccountId,
key: &Self::Secret,
destination: &Self::AccountId,
@@ -143,10 +110,12 @@ impl RuntimeAdapter for FactoryState<Number> {
genesis_hash: &<Self::Block as BlockT>::Hash,
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);
let phase = self.block_number() as Self::Phase;
let extra = Self::build_extra(self.index, phase);
self.index += 1;
sign::<Self>(CheckedExtrinsic {
signed: Some((sender.clone(), Self::build_extra(index, phase))),
signed: Some((sender.clone(), extra)),
function: Call::Balances(
BalancesCall::transfer(
pallet_indices::address::Address::Id(destination.clone().into()),
@@ -157,12 +126,12 @@ impl RuntimeAdapter for FactoryState<Number> {
}
fn inherent_extrinsics(&self) -> InherentData {
let timestamp = (self.block_no as u64 + 1) * MinimumPeriod::get();
let timestamp = (self.block_number as u64 + 1) * MinimumPeriod::get();
let mut inherent = InherentData::new();
inherent.put_data(sp_timestamp::INHERENT_IDENTIFIER, &timestamp)
.expect("Failed putting timestamp inherent");
inherent.put_data(sp_finality_tracker::INHERENT_IDENTIFIER, &self.block_no)
inherent.put_data(sp_finality_tracker::INHERENT_IDENTIFIER, &self.block_number)
.expect("Failed putting finalized number inherent");
inherent
}
@@ -180,49 +149,16 @@ impl RuntimeAdapter for FactoryState<Number> {
}
/// 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));
fn gen_random_account_id(seed: u32) -> Self::AccountId {
let pair: sr25519::Pair = sr25519::Pair::from_seed(&gen_seed_bytes(seed));
AccountPublic::from(pair.public()).into_account()
}
/// 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));
fn gen_random_account_secret(seed: u32) -> 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() as Self::Index
} 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 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() as Self::Phase
}
}
fn gen_seed_bytes(seed: u32) -> [u8; 32] {