Skip to content

Commit

Permalink
log target contract name for setup steps
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroXbrock committed Oct 4, 2024
1 parent eb1c56f commit 4204fdb
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 10 deletions.
21 changes: 21 additions & 0 deletions sqlite_db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,27 @@ impl DbOps for SqliteDb {
Ok(res.into())
}

fn get_named_tx_by_address(&self, address: &Address) -> Result<NamedTx> {
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<RunTx>) -> Result<()> {
let pool = self.get_pool()?;
let stmts = run_txs.iter().map(|tx| {
Expand Down
10 changes: 10 additions & 0 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub trait DbOps {

fn get_named_tx(&self, name: &str) -> Result<NamedTx>;

fn get_named_tx_by_address(&self, address: &Address) -> Result<NamedTx>;

fn insert_run_txs(&self, run_id: u64, run_txs: Vec<RunTx>) -> Result<()>;

fn get_run_txs(&self, run_id: u64) -> Result<Vec<RunTx>>;
Expand Down Expand Up @@ -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<NamedTx> {
Ok(NamedTx::new(
String::default(),
TxHash::default(),
Some(*address),
))
}

fn insert_run_txs(&self, _run_id: u64, _run_txs: Vec<RunTx>) -> Result<()> {
Ok(())
}
Expand Down
53 changes: 43 additions & 10 deletions src/test_scenario.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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
Expand All @@ -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(),
)
Expand All @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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<D, S, P> Generator<String, D, P> for TestScenario<D, S, P>
Expand Down

0 comments on commit 4204fdb

Please sign in to comment.