-
Notifications
You must be signed in to change notification settings - Fork 91
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
Await for a settlement tx in autopilot #2630
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b1bc38d
Await for a settlement tx in autopilot
squadgazzz 89d7a4b
max_blocks_wait config
squadgazzz 08205ff
Redundant clone
squadgazzz b0e943b
Task cancellation error log
squadgazzz 0643fb4
Redundant param
squadgazzz 45296b9
Redundant Arcs
squadgazzz 11cda11
Futures select
squadgazzz 682e956
Minor
squadgazzz 1526995
Review comments
squadgazzz cecd99f
Fetch by auction_id
squadgazzz 8f0c208
Query fix
squadgazzz 7522b12
Lower timeout
squadgazzz 011ff18
Merge branch 'main' into 2609/autopilot-tracks-for-settlements
squadgazzz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,19 @@ | ||
use {anyhow::Context, primitive_types::H256, std::ops::Range}; | ||
use {anyhow::Context, primitive_types::H256}; | ||
|
||
impl super::Postgres { | ||
pub async fn recent_settlement_tx_hashes( | ||
pub async fn find_tx_hash_by_auction_id( | ||
&self, | ||
block_range: Range<u64>, | ||
) -> anyhow::Result<Vec<H256>> { | ||
let start: i64 = block_range.start.try_into()?; | ||
let end: i64 = block_range.end.try_into()?; | ||
let block_range = start..end; | ||
|
||
auction_id: i64, | ||
) -> anyhow::Result<Option<H256>> { | ||
let _timer = super::Metrics::get() | ||
.database_queries | ||
.with_label_values(&["recent_settlement_tx_hashes"]) | ||
.with_label_values(&["find_tx_hash_by_auction_id"]) | ||
.start_timer(); | ||
|
||
let mut ex = self.pool.acquire().await.context("acquire")?; | ||
let hashes = database::settlements::recent_settlement_tx_hashes(&mut ex, block_range) | ||
let hash = database::settlements::get_hash_by_auction_id(&mut ex, auction_id) | ||
.await | ||
.context("recent_settlement_tx_hashes")?; | ||
Ok(hashes.into_iter().map(|hash| H256(hash.0)).collect()) | ||
.context("get_hash_by_auction_id")?; | ||
Ok(hash.map(|hash| H256(hash.0))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -277,7 +277,7 @@ impl RunLoop { | |
|
||
tracing::info!(driver = %driver.name, "settling"); | ||
let submission_start = Instant::now(); | ||
match self.settle(driver, solution).await { | ||
match self.settle(driver, solution, auction_id).await { | ||
Ok(()) => Metrics::settle_ok(driver, submission_start.elapsed()), | ||
Err(err) => { | ||
Metrics::settle_err(driver, &err, submission_start.elapsed()); | ||
|
@@ -405,21 +405,22 @@ impl RunLoop { | |
|
||
/// Execute the solver's solution. Returns Ok when the corresponding | ||
/// transaction has been mined. | ||
async fn settle(&self, driver: &infra::Driver, solved: &Solution) -> Result<(), SettleError> { | ||
async fn settle( | ||
&self, | ||
driver: &infra::Driver, | ||
solved: &Solution, | ||
auction_id: i64, | ||
) -> Result<(), SettleError> { | ||
let order_ids = solved.order_ids().copied().collect(); | ||
self.persistence | ||
.store_order_events(order_ids, OrderEventLabel::Executing); | ||
|
||
let request = settle::Request { | ||
solution_id: solved.id, | ||
}; | ||
|
||
let tx_hash = driver | ||
.settle(&request, self.max_settlement_transaction_wait) | ||
.await | ||
.map_err(SettleError::Failure)? | ||
.tx_hash; | ||
|
||
let tx_hash = self | ||
.wait_for_settlement(driver, auction_id, request) | ||
.await?; | ||
*self.in_flight_orders.lock().await = Some(InFlightOrders { | ||
tx_hash, | ||
orders: solved.orders.keys().copied().collect(), | ||
|
@@ -429,6 +430,62 @@ impl RunLoop { | |
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<H256, 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)), | ||
) | ||
.await | ||
{ | ||
futures::future::Either::Left((res, _)) => res, | ||
futures::future::Either::Right((res, _)) => { | ||
res.map_err(SettleError::Failure).map(|tx| tx.tx_hash) | ||
} | ||
} | ||
} | ||
|
||
/// Tries to find a `settle` contract call with calldata ending in `tag`. | ||
/// | ||
/// Returns None if no transaction was found within the deadline or the task | ||
/// is cancelled. | ||
async fn wait_for_settlement_transaction( | ||
&self, | ||
auction_id: i64, | ||
max_blocks_wait: u64, | ||
) -> Result<H256, 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"); | ||
loop { | ||
if self.eth.current_block().borrow().number > deadline { | ||
break; | ||
} | ||
|
||
match self | ||
.persistence | ||
.find_tx_hash_by_auction_id(auction_id) | ||
.await | ||
{ | ||
Ok(Some(hash)) => return Ok(hash), | ||
Err(err) => { | ||
tracing::warn!(?err, "failed to fetch recent settlement tx hashes"); | ||
} | ||
Ok(None) => {} | ||
} | ||
tokio::time::sleep(Duration::from_secs(3)).await; | ||
} | ||
Err(SettleError::Failure(anyhow::anyhow!( | ||
"settlement transaction await reached deadline" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: we could define a separate variant |
||
))) | ||
} | ||
|
||
/// Removes orders that are currently being settled to avoid solvers trying | ||
/// to fill an order a second time. | ||
async fn remove_in_flight_orders(&self, mut auction: domain::Auction) -> domain::Auction { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to test this in e2e or unit test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I couldn't find a quick way to achieve that. That would require either an artificial delay in the driver's response or creating unit tests with mocks. Someone may have a better idea of how to test it.