From 66dd65c27512a9cfbbb05e4a424e6d80e12618f9 Mon Sep 17 00:00:00 2001 From: Anthony Graignic <anthony.graignic@uca.fr> Date: Thu, 11 Jan 2024 11:23:24 +0100 Subject: [PATCH] Code cleanup Update commands in readme Optimize imports Lint code with fmt & clippy --- README.md | 33 +++++++++++++++++----------- src/main.rs | 44 ++++++++++++++++++-------------------- src/utils/miner.rs | 8 +++---- src/utils/mobility_data.rs | 12 ++++------- src/utils/txbx.rs | 6 ++---- 5 files changed, 51 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 03cb1b7..eeff552 100644 --- a/README.md +++ b/README.md @@ -16,15 +16,27 @@ This project heavily use the forked and adapted [ethers-rs](https://gitlab.limos ## Usage -### Simulate tx & bx +### Simulate bx -To simulate Behaviors from a CSV Mobility dataset, run `cargo run simulate-bx-from-csv < data/dataset-extract.csv` +To simulate Behaviors from a CSV Mobility dataset, run `cargo run simulate-bx-from-csv data/dataset-extract.csv 60` Options: - dataset e.g. CSV file - `total_simulation_duration` in seconds (or minutes ?) +### Send tx from CSV + +To send tx from accounts that got rewarded from behaviors created from a CSV Mobility dataset, run `cargo run simulate-tx-from-csv data/dataset-extract.csv 10` + +Options: + +- dataset e.g. CSV file +- `block_time` in seconds, send a tx every x seconds (avoiding to send all txs at once and in a single block) +- (optional) `unknown_addr` the tx will be send to an unknown random address +- (optional) `rpc_url` Node RPC url e.g. http://127.0.0.1:8545 +- (optional) `chain_id` Chain id e.g. 636363 + ### Send Send tx or bx @@ -33,10 +45,10 @@ Options: - `txs` Number of transactions to send - `bxs` Number of behaviors to send - -In a near future: - -- `time_period` in seconds +- (optional) `hd_index` Index of account in HD wallet, allowing to use `user_id` like done in bx simulation from csv +- (optional) `wait` Waiting period between each tx/bx +- (optional) `rpc_url` Node RPC url e.g. http://127.0.0.1:8545 +- (optional) `chain_id` Chain id e.g. 636363 ### Mine @@ -44,13 +56,8 @@ To mine a block from env vars `MINER_SIGNING_KEY` and `RPC_URL`, use `cargo run Options: -- None yet - -In a near futur: - -- quantity -- timestamp -- input +- (optional) `rpc_url` Node RPC url e.g. http://127.0.0.1:8545 +- (optional) `chain_id` Chain id e.g. 636363 ## Env vars diff --git a/src/main.rs b/src/main.rs index c4e03ca..c399ab5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,20 @@ +use ::clap::{Args, Parser, Subcommand}; use ecomobicoin_jsonrpc::types::{Behavior, Block}; use ethereum_types::U64; -use ethers::prelude::rand::Rng; -use ethers::types::transaction::ecomobicoin_behavior::EcoMobiCoinBehaviorTransactionRequest; -use ethers::{prelude::*, signers::coins_bip39::English}; +use ethers::{ + prelude::{rand::Rng, *}, + signers::coins_bip39::English, + types::transaction::ecomobicoin_behavior::EcoMobiCoinBehaviorTransactionRequest, +}; use futures::executor::block_on; -use std::str::FromStr; -use std::time::{SystemTime, UNIX_EPOCH}; -use std::{process, time::Duration}; -use testnet_injector::utils::miner::get_miner_behavior; -use testnet_injector::utils::{mobility_data::*, txbx}; +use std::{ + process, + str::FromStr, + time::{Duration, SystemTime, UNIX_EPOCH}, +}; +use testnet_injector::utils::{miner::get_miner_behavior, mobility_data::*, txbx}; use tracing::{debug, error, info, warn, Level}; -use ::clap::{Args, Parser, Subcommand}; - #[derive(Parser)] #[command(author, version)] #[command( @@ -95,10 +97,6 @@ async fn init() -> (u64, String) { tracing::subscriber::set_global_default(subscriber) .expect("setting default tracing subscriber failed"); - // dotenv().expect(".env file not found"); - // for (key, value) in std::env::vars() { - // println!("{key}: {value}"); - // } let rpc_url: String = std::env::var("RPC_URL").unwrap_or_else(|_| "http://127.0.0.1:8545".into()); let provider = Provider::<Http>::try_from(rpc_url.clone()).unwrap(); @@ -136,7 +134,7 @@ async fn main() -> Result<(), anyhow::Error> { rpc_url = params.rpc_url.clone().unwrap(); } if params.chain_id.clone().is_some() { - chain_id = params.chain_id.clone().unwrap(); + chain_id = params.chain_id.unwrap(); } let max = std::cmp::max(params.txs, params.bxs); @@ -217,7 +215,7 @@ async fn main() -> Result<(), anyhow::Error> { rpc_url = params.rpc_url.clone().unwrap(); } if params.chain_id.clone().is_some() { - chain_id = params.chain_id.clone().unwrap(); + chain_id = params.chain_id.unwrap(); } // println!("Processing {:?}", params.file); // csv::Reader::from_path(path) @@ -271,9 +269,9 @@ async fn main() -> Result<(), anyhow::Error> { .unwrap() .as_secs(); let simulation_behavior = mobility_records[i].clone(); - let relative_start_duration = mobility_records_times[i].clone(); + let relative_start_duration = mobility_records_times[i]; let rpc_url = rpc_url.clone(); - let chain_id = chain_id.clone(); + let chain_id = chain_id; let mut time_since_beginning = now; handles.push(tokio::spawn({ @@ -332,7 +330,7 @@ async fn main() -> Result<(), anyhow::Error> { rpc_url = params.rpc_url.clone().unwrap(); } if params.chain_id.clone().is_some() { - chain_id = params.chain_id.clone().unwrap(); + chain_id = params.chain_id.unwrap(); } let records = read_csv(csv::Reader::from_path(params.file.clone())?); if let Err(err) = records { @@ -357,10 +355,10 @@ async fn main() -> Result<(), anyhow::Error> { // ---------------- let mut handles = Vec::with_capacity(mobility_records.len()); - for i in 0..mobility_records.len() { - let simulation_behavior = mobility_records[i].clone(); + for (i, mobility_behavior) in mobility_records.iter().enumerate() { + let simulation_behavior = mobility_behavior.clone(); let rpc_url = rpc_url.clone(); - let chain_id = chain_id.clone(); + let chain_id = chain_id; let sleep_duration = Duration::from_secs(block_time * i as u64); handles.push(tokio::spawn({ @@ -436,7 +434,7 @@ async fn main() -> Result<(), anyhow::Error> { rpc_url = params.rpc_url.clone().unwrap(); } if params.chain_id.clone().is_some() { - chain_id = params.chain_id.clone().unwrap(); + chain_id = params.chain_id.unwrap(); } // Wallet is derived from mnemomic with user_id to ensure each user have the same wallet for the simulation. diff --git a/src/utils/miner.rs b/src/utils/miner.rs index 82fae99..48d6eaa 100644 --- a/src/utils/miner.rs +++ b/src/utils/miner.rs @@ -21,10 +21,10 @@ pub fn get_miner_behavior(wallet: LocalWallet, chain_id: u64, to: Address) -> Be let behavior_msg = BehaviorMessage{ chain_id: U64::from(chain_id), - to:Some(to.clone()), - timestamp: U64::from(now), - quantity: U256::from(210000_u64), - input: Bytes::from("0x015d8eb90000000000000000000000000000000000000000000000000000000000878c1c00000000000000000000000000000000000000000000000000000000644662bc0000000000000000000000000000000000000000000000000000001ee24fba17b7e19cc10812911dfa8a438e0a81a9933f843aa5b528899b8d9e221b649ae0df00000000000000000000000000000000000000000000000000000000000000060000000000000000000000007431310e026b69bfc676c0013e12a1a11411eec9000000000000000000000000000000000000000000000000000000000000083400000000000000000000000000000000000000000000000000000000000f4240") }; + to:Some(to), + timestamp: U64::from(now), + quantity: U256::from(210000_u64), + input: Bytes::from("0x015d8eb90000000000000000000000000000000000000000000000000000000000878c1c00000000000000000000000000000000000000000000000000000000644662bc0000000000000000000000000000000000000000000000000000001ee24fba17b7e19cc10812911dfa8a438e0a81a9933f843aa5b528899b8d9e221b649ae0df00000000000000000000000000000000000000000000000000000000000000060000000000000000000000007431310e026b69bfc676c0013e12a1a11411eec9000000000000000000000000000000000000000000000000000000000000083400000000000000000000000000000000000000000000000000000000000f4240") }; // let behavior = wallet. let tr = TransactionRequest::new() diff --git a/src/utils/mobility_data.rs b/src/utils/mobility_data.rs index 3640577..8589aec 100644 --- a/src/utils/mobility_data.rs +++ b/src/utils/mobility_data.rs @@ -1,12 +1,8 @@ -use std::{error::Error, ops::Sub, time::Duration}; - use csv::Reader; -use ethers::{ - prelude::rand::{thread_rng, Rng}, - types::Bytes, -}; +use ethers::types::Bytes; use primitive_types::U256; use serde::{Deserialize, Serialize}; +use std::{error::Error, ops::Sub, time::Duration}; use time::OffsetDateTime; /// Mobility Record from Public Transportation @@ -79,7 +75,7 @@ pub struct MobilityBehavior { pub mobility_record: MobilityRecord, } -pub fn find_earliest_mobility_record(records: &Vec<MobilityRecord>) -> OffsetDateTime { +pub fn find_earliest_mobility_record(records: &[MobilityRecord]) -> OffsetDateTime { records .iter() .min_by(|x, y| x.created_at.cmp(&y.created_at)) @@ -87,7 +83,7 @@ pub fn find_earliest_mobility_record(records: &Vec<MobilityRecord>) -> OffsetDat .created_at } -pub fn find_latest_mobility_record(records: &Vec<MobilityRecord>) -> OffsetDateTime { +pub fn find_latest_mobility_record(records: &[MobilityRecord]) -> OffsetDateTime { records .iter() .max_by(|x, y| x.created_at.cmp(&y.created_at)) diff --git a/src/utils/txbx.rs b/src/utils/txbx.rs index fb67495..767e649 100644 --- a/src/utils/txbx.rs +++ b/src/utils/txbx.rs @@ -1,5 +1,4 @@ -use ethereum_types::U256; -use ethereum_types::{Address, U64}; +use ethereum_types::{Address, U256, U64}; use ethers::{ prelude::*, types::transaction::{ @@ -7,8 +6,7 @@ use ethers::{ }, utils::keccak256, }; -use std::str::FromStr; -use std::time::SystemTime; +use std::{str::FromStr, time::SystemTime}; use tracing::debug; pub async fn send_transaction( -- GitLab