Skip to content

Commit

Permalink
Get_hashes_by_auction_id implementation for multiple settlements (#2976)
Browse files Browse the repository at this point in the history
# Description
Fixes #2975

# Changes
<!-- List of detailed changes (how the change is accomplished) -->

- [ ] ...
- [ ] ...

## How to test
<!--- Include details of how to test your changes, including any
pre-requisites. If no unit tests are included, please explain why and
how to test manually
1.
2.
3.
-->

<!--
## Related Issues

Fixes #
-->
  • Loading branch information
sunce86 authored Sep 12, 2024
1 parent 61240bc commit e8a5c7a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
16 changes: 8 additions & 8 deletions crates/autopilot/src/database/recent_settlements.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use {anyhow::Context, primitive_types::H256};

impl super::Postgres {
pub async fn find_tx_hash_by_auction_id(
&self,
auction_id: i64,
) -> anyhow::Result<Option<H256>> {
pub async fn find_settlement_transactions(&self, auction_id: i64) -> anyhow::Result<Vec<H256>> {
let _timer = super::Metrics::get()
.database_queries
.with_label_values(&["find_tx_hash_by_auction_id"])
.with_label_values(&["find_settlement_transactions"])
.start_timer();

let mut ex = self.pool.acquire().await.context("acquire")?;
let hash = database::settlements::get_hash_by_auction_id(&mut ex, auction_id)
let hashes = database::settlements::get_hashes_by_auction_id(&mut ex, auction_id)
.await
.context("get_hash_by_auction_id")?;
Ok(hash.map(|hash| H256(hash.0)))
.context("get_hashes_by_auction_id")?
.into_iter()
.map(|hash| H256(hash.0))
.collect();
Ok(hashes)
}
}
18 changes: 11 additions & 7 deletions crates/autopilot/src/infra/persistence/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,20 @@ impl Persistence {
ex.commit().await.context("commit")
}

/// Retrieves the transaction hash for the settlement with the given
/// auction_id.
pub async fn find_tx_hash_by_auction_id(
/// For a given auction, finds all settlements and returns their transaction
/// hashes.
pub async fn find_settlement_transactions(
&self,
auction_id: i64,
) -> Result<Option<H256>, DatabaseError> {
self.postgres
.find_tx_hash_by_auction_id(auction_id)
) -> Result<Vec<eth::TxId>, DatabaseError> {
Ok(self
.postgres
.find_settlement_transactions(auction_id)
.await
.map_err(DatabaseError)
.map_err(DatabaseError)?
.into_iter()
.map(eth::TxId)
.collect())
}

/// Checks if an auction already has an accociated settlement.
Expand Down
15 changes: 10 additions & 5 deletions crates/autopilot/src/run_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use {
domain::{
self,
competition::{self, SolutionError, TradedAmounts},
eth,
OrderUid,
},
infra::{
Expand Down Expand Up @@ -644,7 +645,7 @@ impl RunLoop {
driver: &infra::Driver,
auction_id: i64,
request: settle::Request,
) -> Result<H256, SettleError> {
) -> Result<eth::TxId, SettleError> {
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)),
Expand All @@ -670,7 +671,7 @@ impl RunLoop {
&self,
auction_id: i64,
max_blocks_wait: u64,
) -> Result<H256, SettleError> {
) -> Result<eth::TxId, 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 @@ -682,14 +683,18 @@ impl RunLoop {

match self
.persistence
.find_tx_hash_by_auction_id(auction_id)
.find_settlement_transactions(auction_id)
.await
{
Ok(Some(hash)) => return Ok(hash),
Ok(hashes) if hashes.is_empty() => {}
Ok(hashes) => {
if let Some(hash) = hashes.into_iter().next() {
return Ok(hash);
}
}
Err(err) => {
tracing::warn!(?err, "failed to fetch recent settlement tx hashes");
}
Ok(None) => {}
}
if block.number >= deadline {
break;
Expand Down
9 changes: 3 additions & 6 deletions crates/database/src/settlements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,17 @@ WHERE
.await
}

pub async fn get_hash_by_auction_id(
pub async fn get_hashes_by_auction_id(
ex: &mut PgConnection,
auction_id: i64,
) -> Result<Option<TransactionHash>, sqlx::Error> {
) -> Result<Vec<TransactionHash>, sqlx::Error> {
const QUERY: &str = r#"
SELECT tx_hash
FROM settlements
WHERE
auction_id = $1
"#;
sqlx::query_scalar::<_, TransactionHash>(QUERY)
.bind(auction_id)
.fetch_optional(ex)
.await
sqlx::query_as(QUERY).bind(auction_id).fetch_all(ex).await
}

#[derive(Debug, sqlx::FromRow)]
Expand Down

0 comments on commit e8a5c7a

Please sign in to comment.