Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch solver that created settlement transaction #3003

Merged
merged 14 commits into from
Sep 26, 2024
1 change: 0 additions & 1 deletion crates/autopilot/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ pub mod fee_policies;
pub mod onchain_order_events;
pub mod order_events;
mod quotes;
pub mod recent_settlements;

#[derive(Debug, Clone)]
pub struct Config {
Expand Down
19 changes: 0 additions & 19 deletions crates/autopilot/src/database/recent_settlements.rs

This file was deleted.

26 changes: 15 additions & 11 deletions crates/autopilot/src/infra/persistence/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use {
},
futures::{StreamExt, TryStreamExt},
number::conversions::{big_decimal_to_u256, u256_to_big_decimal, u256_to_big_uint},
primitive_types::H256,
primitive_types::{H160, H256},
shared::db_order_conversions::full_order_into_model_order,
std::{
collections::{HashMap, HashSet},
Expand Down Expand Up @@ -179,20 +179,24 @@ impl Persistence {
ex.commit().await.context("commit")
}

/// For a given auction, finds all settlements and returns their transaction
/// hashes.
/// For a given auction, finds all settlements and returns their associated
/// pairs of <transaction_hash, solver>.
pub async fn find_settlement_transactions(
&self,
auction_id: i64,
) -> Result<Vec<eth::TxId>, DatabaseError> {
Ok(self
.postgres
.find_settlement_transactions(auction_id)
.await
.map_err(DatabaseError)?
) -> Result<Vec<(eth::TxId, eth::Address)>, DatabaseError> {
let _timer = Metrics::get()
.database_queries
.with_label_values(&["find_settlement_transactions"])
.start_timer();

let mut ex = self.postgres.pool.acquire().await.context("acquire")?;
let hashes = database::settlements::get_transactions_by_auction_id(&mut ex, auction_id)
.await?
.into_iter()
.map(eth::TxId)
.collect())
.map(|(hash, solver)| (H256(hash.0).into(), H160(solver.0).into()))
.collect();
Ok(hashes)
}

/// Checks if an auction already has an accociated settlement.
Expand Down
42 changes: 16 additions & 26 deletions crates/autopilot/src/run_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,25 +619,9 @@ impl RunLoop {
solution_id: solved.id(),
submission_deadline_latest_block,
};
let tx_hash = self
.wait_for_settlement(driver, auction_id, request)
.await?;
tracing::debug!(?tx_hash, "solution settled");

Ok(())
}

/// Wait for either the settlement transaction to be mined or the driver
/// returned a result.
async fn wait_for_settlement(
&self,
driver: &infra::Driver,
auction_id: i64,
request: settle::Request,
) -> Result<eth::TxId, SettleError> {
match futures::future::select(
let (tx_hash, solver) = match futures::future::select(
Box::pin(self.wait_for_settlement_transaction(auction_id, self.submission_deadline)),
Box::pin(driver.settle(&request, self.max_settlement_transaction_wait)),
Box::pin(driver.settle(&request.clone(), self.max_settlement_transaction_wait)),
)
.await
{
Expand All @@ -649,7 +633,16 @@ impl RunLoop {
})?;
onchain_task.await
}
}?;
if solved.solver() != solver {
sunce86 marked this conversation as resolved.
Show resolved Hide resolved
tracing::warn!(
?solver,
solution = %solved.id(),
"solver mismatch in settlement transaction"
);
}
tracing::debug!(?tx_hash, "solution settled");
Ok(())
}

/// Tries to find a `settle` contract call with calldata ending in `tag`.
Expand All @@ -660,7 +653,7 @@ impl RunLoop {
&self,
auction_id: i64,
max_blocks_wait: u64,
) -> Result<eth::TxId, SettleError> {
) -> Result<(eth::TxId, eth::Address), SettleError> {
let current = self.eth.current_block().borrow().number;
let deadline = current.saturating_add(max_blocks_wait);
tracing::debug!(%current, %deadline, %auction_id, "waiting for tag");
Expand All @@ -674,15 +667,12 @@ impl RunLoop {
.persistence
.find_settlement_transactions(auction_id)
.await
.map(|mut hashes| hashes.pop())
{
Ok(hashes) if hashes.is_empty() => {}
Ok(hashes) => {
if let Some(hash) = hashes.into_iter().next() {
return Ok(hash);
}
}
Ok(Some(transaction)) => return Ok(transaction),
Ok(None) => {}
Err(err) => {
tracing::warn!(?err, "failed to fetch recent settlement tx hashes");
tracing::warn!(?err, "failed to fetch recent settlement transactions");
}
}
if block.number >= deadline {
Expand Down
26 changes: 4 additions & 22 deletions crates/database/src/settlements.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
use {
crate::{events::EventIndex, PgTransaction, TransactionHash},
crate::{Address, PgTransaction, TransactionHash},
sqlx::{Executor, PgConnection},
};

pub async fn get_hash_by_event(
ex: &mut PgConnection,
event: &EventIndex,
) -> Result<TransactionHash, sqlx::Error> {
const QUERY: &str = r#"
SELECT tx_hash
FROM settlements
WHERE
block_number = $1 AND
log_index = $2
"#;
sqlx::query_scalar::<_, TransactionHash>(QUERY)
.bind(event.block_number)
.bind(event.log_index)
.fetch_one(ex)
.await
}

pub async fn get_hashes_by_auction_id(
pub async fn get_transactions_by_auction_id(
ex: &mut PgConnection,
auction_id: i64,
) -> Result<Vec<TransactionHash>, sqlx::Error> {
) -> Result<Vec<(TransactionHash, Address)>, sqlx::Error> {
const QUERY: &str = r#"
SELECT tx_hash
SELECT tx_hash, solver
FROM settlements
WHERE
auction_id = $1
Expand Down
Loading