From 4204fdb486d5730ee8093aad9153734b7859f747 Mon Sep 17 00:00:00 2001 From: zeroXbrock <2791467+zeroXbrock@users.noreply.github.com> Date: Fri, 4 Oct 2024 12:17:29 -0700 Subject: [PATCH] log target contract name for setup steps --- sqlite_db/src/lib.rs | 21 ++++++++++++++++++ src/db/mod.rs | 10 +++++++++ src/test_scenario.rs | 53 +++++++++++++++++++++++++++++++++++--------- 3 files changed, 74 insertions(+), 10 deletions(-) diff --git a/sqlite_db/src/lib.rs b/sqlite_db/src/lib.rs index b185abb..a25859a 100644 --- a/sqlite_db/src/lib.rs +++ b/sqlite_db/src/lib.rs @@ -237,6 +237,27 @@ impl DbOps for SqliteDb { Ok(res.into()) } + fn get_named_tx_by_address(&self, address: &Address) -> Result { + let pool = self.get_pool()?; + let mut stmt = pool + .prepare( + "SELECT name, tx_hash, contract_address FROM named_txs WHERE contract_address = ?1 ORDER BY id DESC LIMIT 1", + ) + .map_err(|e| ContenderError::with_err(e, "failed to prepare statement"))?; + + let row = stmt + .query_map(params![address.encode_hex()], |row| { + NamedTxRow::from_row(row) + }) + .map_err(|e| ContenderError::with_err(e, "failed to map row"))?; + let res = row + .last() + .transpose() + .map_err(|e| ContenderError::with_err(e, "no row found"))? + .ok_or(ContenderError::DbError("no existing row", None))?; + Ok(res.into()) + } + fn insert_run_txs(&self, run_id: u64, run_txs: Vec) -> Result<()> { let pool = self.get_pool()?; let stmts = run_txs.iter().map(|tx| { diff --git a/src/db/mod.rs b/src/db/mod.rs index 9dc529d..1fd19ff 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -50,6 +50,8 @@ pub trait DbOps { fn get_named_tx(&self, name: &str) -> Result; + fn get_named_tx_by_address(&self, address: &Address) -> Result; + fn insert_run_txs(&self, run_id: u64, run_txs: Vec) -> Result<()>; fn get_run_txs(&self, run_id: u64) -> Result>; @@ -78,6 +80,14 @@ impl DbOps for MockDb { Ok(NamedTx::new(String::default(), TxHash::default(), None)) } + fn get_named_tx_by_address(&self, address: &Address) -> Result { + Ok(NamedTx::new( + String::default(), + TxHash::default(), + Some(*address), + )) + } + fn insert_run_txs(&self, _run_id: u64, _run_txs: Vec) -> Result<()> { Ok(()) } diff --git a/src/test_scenario.rs b/src/test_scenario.rs index da27e37..6086203 100644 --- a/src/test_scenario.rs +++ b/src/test_scenario.rs @@ -1,6 +1,7 @@ use crate::db::{DbOps, NamedTx}; use crate::error::ContenderError; use crate::generator::templater::Templater; +use crate::generator::NamedTxRequest; use crate::generator::{seeder::Seeder, types::PlanType, Generator, PlanConfig}; use alloy::hex::ToHexExt; use alloy::network::{EthereumWallet, TransactionBuilder}; @@ -90,7 +91,10 @@ where .wallet(wallet_conf) .on_http(self.rpc_url.to_owned()); - println!("deploying contract: {:?}", tx_req.name.as_ref().unwrap_or(&"".to_string())); + println!( + "deploying contract: {:?}", + tx_req.name.as_ref().unwrap_or(&"".to_string()) + ); let handle = tokio::task::spawn(async move { // estimate gas limit let gas_limit = wallet @@ -110,16 +114,15 @@ where .await .expect("failed to send tx"); let receipt = res.get_receipt().await.expect("failed to get receipt"); - println!("contract address: {:?}", receipt.contract_address - .as_ref() - .map(|a| a.encode_hex()) - .unwrap_or("".to_string())); - let contract_address = receipt.contract_address; + println!( + "contract address: {}", + receipt.contract_address.unwrap_or_default() + ); db.insert_named_txs( NamedTx::new( tx_req.name.unwrap_or_default(), receipt.transaction_hash, - contract_address, + receipt.contract_address, ) .into(), ) @@ -135,12 +138,13 @@ where pub async fn run_setup(&self) -> Result<(), ContenderError> { self.load_txs(PlanType::Setup(|tx_req| { /* callback */ + println!("{}", self.format_setup_log(&tx_req)); + // copy data/refs from self before spawning the task let from = tx_req.tx.from.as_ref().ok_or(ContenderError::SetupError( "failed to get 'from' address", None, ))?; - println!("from: {:?}", from); let wallet = self .wallet_map .get(from) @@ -151,8 +155,6 @@ where .to_owned(); let db = self.db.clone(); let rpc_url = self.rpc_url.clone(); - - println!("running setup: {:?}", tx_req.name.as_ref().unwrap_or(&"".to_string())); let handle = tokio::task::spawn(async move { let wallet = ProviderBuilder::new() .with_simple_nonce_management() @@ -194,6 +196,37 @@ where Ok(()) } + + fn format_setup_log(&self, tx_req: &NamedTxRequest) -> String { + let to_address = tx_req.tx.to.unwrap_or_default(); + let to_address = to_address.to(); + + // lookup name of contract if it exists + let to_name = to_address.map(|a| { + let named_tx = self.db.get_named_tx_by_address(&a); + named_tx.map(|t| t.name).unwrap_or_default() + }); + + format!( + "running setup: from={} to={} {}", + tx_req + .tx + .from + .as_ref() + .map(|a| a.encode_hex()) + .unwrap_or_default(), + if let Some(to) = to_name { + to + } else { + to_address.map(|a| a.encode_hex()).unwrap_or_default() + }, + if let Some(kind) = &tx_req.kind { + format!("kind={}", kind) + } else { + "".to_string() + }, + ) + } } impl Generator for TestScenario