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

Get_hashes_by_auction_id implementation for multiple settlements #2976

Merged
merged 4 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this function might as well be inlined in the infra module to get rid of the file.

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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks quite ugly. Could be a bit nicer with:

            match self
                .persistence
                .find_settlement_transactions(auction_id)
                .await
                .map(|mut hashes| hashes.pop())
            {
                Ok(Some(tx_hash)) => return Ok(tx_hash),
                Ok(None) => {},
                Err(err) => {
                    tracing::warn!(?err, "failed to fetch recent settlement tx hashes");
                }
            }

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
Loading