Skip to content

Commit

Permalink
add db function to batch-insert txs
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroXbrock committed Sep 25, 2024
1 parent a4fb355 commit ea39af3
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
12 changes: 11 additions & 1 deletion src/db/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,19 @@ pub trait DbOps {
contract_address: Option<Address>,
) -> Result<()>;

fn insert_named_txs(&self, named_txs: Vec<(String, TxHash, Option<Address>)>) -> Result<()>;

fn get_named_tx(&self, name: &str) -> Result<(TxHash, Option<Address>)>;

fn insert_run_tx(&self, run_id: u64, tx_hash: TxHash, start_timestamp: usize, end_timestamp: usize, block_number: u64, gas_used: u128) -> Result<()>;
fn insert_run_tx(
&self,
run_id: u64,
tx_hash: TxHash,
start_timestamp: usize,
end_timestamp: usize,
block_number: u64,
gas_used: u128,
) -> Result<()>;

fn get_run_txs(&self, run_id: u64) -> Result<Vec<RunTx>>;
}
53 changes: 52 additions & 1 deletion src/db/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,28 @@ impl DbOps for SqliteDb {
)
}

fn insert_named_txs(&self, named_txs: Vec<(String, TxHash, Option<Address>)>) -> Result<()> {
let pool = self.get_pool()?;
let stmts = named_txs.iter().map(|(name, tx_hash, contract_address)| {
format!(
"INSERT INTO named_txs (name, tx_hash, contract_address) VALUES ('{}', '{}', '{}');",
name,
tx_hash.encode_hex(),
contract_address.map(|a| a.encode_hex()).unwrap_or_default()
)
});
pool.execute_batch(&format!(
"BEGIN;
{}
COMMIT;",
stmts
.reduce(|ac, c| format!("{}\n{}", ac, c))
.unwrap_or_default(),
))
.map_err(|e| ContenderError::with_err(e, "failed to execute batch"))?;
Ok(())
}

fn get_named_tx(&self, name: &str) -> Result<(TxHash, Option<Address>)> {
let pool = self.get_pool()?;
let mut stmt = pool
Expand Down Expand Up @@ -223,7 +245,15 @@ impl DbOps for SqliteDb {
Ok((tx_hash, contract_address))
}

fn insert_run_tx(&self, run_id: u64, tx_hash: TxHash, start_timestamp: usize, end_timestamp: usize, block_number: u64, gas_used: u128) -> Result<()> {
fn insert_run_tx(
&self,
run_id: u64,
tx_hash: TxHash,
start_timestamp: usize,
end_timestamp: usize,
block_number: u64,
gas_used: u128,
) -> Result<()> {
self.execute(
"INSERT INTO run_txs (run_id, tx_hash, start_timestamp, end_timestamp, block_number, gas_used) VALUES (?, ?, ?, ?, ?, ?)",
params![run_id, tx_hash.encode_hex(), start_timestamp, end_timestamp, block_number, gas_used.to_string()],
Expand Down Expand Up @@ -271,4 +301,25 @@ mod tests {
.unwrap();
assert_eq!(count, 1);
}

#[test]
fn insert_named_txs() {
let db = SqliteDb::new_memory();
db.create_tables().unwrap();
let tx_hash = TxHash::from_slice(&[0u8; 32]);
let contract_address = Some(Address::from_slice(&[0u8; 20]));
db.insert_named_txs(vec![
("test_tx".to_string(), tx_hash, contract_address),
("test_tx2".to_string(), tx_hash, contract_address),
])
.unwrap();
let count: i64 = db
.get_pool()
.unwrap()
.query_row("SELECT COUNT(*) FROM named_txs", params![], |row| {
row.get(0)
})
.unwrap();
assert_eq!(count, 2);
}
}
2 changes: 1 addition & 1 deletion src/generator/testfile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ pub mod tests {
assert_eq!(spam[0].fuzz.as_ref().unwrap()[0].param, "amountIn");
assert_eq!(
spam[1].fuzz.as_ref().unwrap()[0].min,
Some(U256::from(100000000))
Some(U256::from(100000))
);
}

Expand Down
2 changes: 1 addition & 1 deletion univ2ConfigTest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,4 @@ args = [
[[spam.fuzz]]
param = "amountIn"
min = "100000"
max = "10000000000000"
max = "10000000000"

0 comments on commit ea39af3

Please sign in to comment.