Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 50 additions & 48 deletions utils/sender/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ fn main() -> Result<(), Box<dyn Error>> {

let sender_accounts = funder_lib::derive_accounts(n_tx_sender, SENDER_SEED.to_owned());
let receiver_accounts = funder_lib::derive_accounts(n_tx_sender, RECEIVER_SEED.to_owned());
let alice = <SrPair as Pair>::from_string(&ALICE_SEED, None).unwrap();
let alice_signer = PairSigner::new(alice.clone());

async fn create_api(node_url: String) -> OnlineClient<PolkadotConfig> {
let node_url = url::Url::parse(&node_url).unwrap();
Expand All @@ -133,56 +131,58 @@ fn main() -> Result<(), Box<dyn Error>> {
OnlineClient::from_backend(backend).await.unwrap()
}

if args.seed {
log::info!("Seeding accounts");
async fn seed_senders(node_url: String, sender_accounts: &[SrPair]) {
let alice = <SrPair as Pair>::from_string(&ALICE_SEED, None).unwrap();

let alice_signer = PairSigner::new(alice.clone());

log::info!("Seeding accounts");
let node_url = node_url.clone();
let api = create_api(node_url.clone()).await;
let mut best_block_stream =
api.blocks().subscribe_best().await.expect("Subscribe to best block failed");
let best_block = best_block_stream.next().await.unwrap().unwrap();
let block_ref: BlockRef<subxt::utils::H256> = BlockRef::from_hash(best_block.hash());
let mut nonce = get_account_nonce(&api, block_ref.clone(), &alice).await;
for sender in sender_accounts.iter() {
let payload = subxt::dynamic::tx(
"Balances",
"transfer_keep_alive",
vec![
Value::unnamed_variant("Id", [Value::from_bytes(sender.public())]),
Value::u128(100000000000000000000),
],
);
let tx_params = Params::new().nonce(nonce as u64).build();
let tx: SubmittableTransaction<_, OnlineClient<_>> = api
.tx()
.create_partial(&payload, &alice_signer.account_id(), tx_params)
.await
.unwrap()
.sign(&alice_signer);
let _ = match tx.submit_and_watch().await {
Ok(watch) => {
log::info!("Seeded account");
nonce += 1;
watch
},
Err(err) => {
log::warn!("{:?}", err);
continue;
},
};
}
}
fn seed_senders_sync(node_url: String, sender_accounts: &[SrPair]) {
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
let node_url = args.node_url.clone();
let api = create_api(node_url.clone()).await;
let mut best_block_stream =
api.blocks().subscribe_best().await.expect("Subscribe to best block failed");
let best_block = best_block_stream.next().await.unwrap().unwrap();
let block_ref: BlockRef<subxt::utils::H256> =
BlockRef::from_hash(best_block.hash());

let mut nonce = get_account_nonce(&api, block_ref.clone(), &alice).await;

for sender in sender_accounts.iter() {
let payload = subxt::dynamic::tx(
"Balances",
"transfer_keep_alive",
vec![
Value::unnamed_variant("Id", [Value::from_bytes(sender.public())]),
Value::u128(100000000000000000000),
],
);

let tx_params = Params::new().nonce(nonce as u64).build();

let tx: SubmittableTransaction<_, OnlineClient<_>> = api
.tx()
.create_partial(&payload, &alice_signer.account_id(), tx_params)
.await
.unwrap()
.sign(&alice_signer);

let _ = match tx.submit_and_watch().await {
Ok(watch) => {
log::info!("Seeded account");
nonce += 1;
watch
},
Err(err) => {
log::warn!("{:?}", err);
continue;
},
};
}
});
.block_on(async { seed_senders(node_url, sender_accounts).await });
}

if args.seed {
seed_senders_sync(args.node_url.clone(), &sender_accounts);
}

while !args.seed {
Expand Down Expand Up @@ -290,7 +290,6 @@ fn main() -> Result<(), Box<dyn Error>> {
],
)
};
log::debug!("Sender {} using nonce {}", i, nonce);
let tx_params = Params::new().nonce(nonce as u64).build();

let tx: SubmittableTransaction::<_, OnlineClient<_>> = api
Expand Down Expand Up @@ -323,6 +322,8 @@ fn main() -> Result<(), Box<dyn Error>> {

log::info!("All senders started");

seed_senders(args.node_url.clone(), &sender_accounts).await;

let mut tps_window = VecDeque::new();
let loop_start = Instant::now();

Expand Down Expand Up @@ -406,6 +407,7 @@ fn main() -> Result<(), Box<dyn Error>> {
handle.abort();
}
log::info!("Restarting senders");

}
}
);
Expand Down