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

Use blocks to determine other environment settlements #3053

Merged
merged 11 commits into from
Oct 17, 2024

Conversation

sunce86
Copy link
Contributor

@sunce86 sunce86 commented Oct 11, 2024

Description

This PR updates how the settlement::Observer distinguishes between settlements from staging and production environments.

Staging is usually deployed first, resulting in higher auction IDs, while production lags behind. Since both environments share a settlement contract, they must:

  • Correctly process settlements from their own environment.
  • Skip processing settlements from the other environment, while still saving the settlement to auction_id relationship in the database.

Detecting staging settlements in production is straightforward and handled by the "AuctionNotFound" error, as staging auctions have higher IDs that don't exist in production yet.

However, detecting production settlements in staging has been problematic. Previously, we used the "auction_has_settlement" logic, but this wasn’t reliable. For example, if a staging auction is saved to the database but not settled by solvers, production might settle the same auction after 270 days, leading to incorrect detection.

This PR introduces a new approach: if a settlement refers to a VERY old auction (in terms of block time), it’s classified as from the other environment. While this may also catch settlements that violated auction block deadline, from the same environment, this issue hasn't occurred in practice.

This solution isn’t perfect but is a reasonable improvement for now.

Changes

  • Use block numbers to detect settlements with very old auction ids

How to test

Will observe the effects on staging.

@sunce86 sunce86 added the E:6.2 Time to Happy Moo See https://github.com/cowprotocol/pm/issues/77 for details label Oct 11, 2024
@sunce86 sunce86 self-assigned this Oct 11, 2024
@sunce86 sunce86 requested a review from a team as a code owner October 11, 2024 10:43
Copy link
Contributor

@squadgazzz squadgazzz left a comment

Choose a reason for hiding this comment

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

IIUIC, the approach works as far as both prod and staging update the solver_competition table with the same speed. But looks like this is not the case for most of the cases. So, at some point, both staging and prod will work with the same auction_id, and the block number diff will be less than the hardcoded value. Did I miss anything?

@sunce86
Copy link
Contributor Author

sunce86 commented Oct 14, 2024

IIUIC, the approach works as far as both prod and staging update the solver_competition table with the same speed. But looks like this is not the case for most of the cases. So, at some point, both staging and prod will work with the same auction_id, and the block number diff will be less than the hardcoded value. Did I miss anything?

I don't anticipate this happening before we fix the auction ids with #2848

Copy link
Contributor

@squadgazzz squadgazzz left a comment

Choose a reason for hiding this comment

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

LGTM

@sunce86 sunce86 requested a review from m-lord-renkse October 16, 2024 09:56

/// A supported Ethereum Chain ID.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ChainId {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe not to do in this PR, but we have logic depending on the chain id scattered all over the place. Maybe it could be unified in one place, and implement all functions related to the chain (e.i. default_amount_to_estimate_native_prices_with())

Copy link
Contributor Author

@sunce86 sunce86 Oct 16, 2024

Choose a reason for hiding this comment

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

I am not against it. Although I think we agreed somewhere that we are fine with some level of code duplication to avoid sharing too much code between autopilot, driver, solvers etc, and to support DDD way of organizing code.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, but for this particular case I think it comes handy. So, in the future, if we were to support more networks, it is all in one place.

But no need for action in this PR, I just wanted to open the debate. This is something I want to do in the upcoming weeks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In that case, I'd suggest opening an issue for this (if we don't have it already). Some people might want to discuss it further. 👍

crates/autopilot/src/domain/settlement/mod.rs Show resolved Hide resolved
@@ -20,7 +20,7 @@ pub struct Addresses {
}

impl Contracts {
pub async fn new(web3: &DynWeb3, chain: &ChainId, addresses: Addresses) -> Self {
pub async fn new(web3: &DynWeb3, chain: &domain::eth::chain::Id, addresses: Addresses) -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: domain::eth::chain seems weird.
A structure like this make more sense to me:
chain
----> id
----> mod

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, not following, how would the path go in your case?

Copy link
Contributor

Choose a reason for hiding this comment

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

domain::chain::Id instead of domain::eth::chain::Id.
It doesn't make much sense to me to have the less specific chain as a child of the more specific eth module.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, to me chain is not that important concept to live directly under domain. Previously it lived under infra::blockchain, maybe we should move it there, so we would have infra::blockchain::Id?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved to infra, probably better now

@sunce86 sunce86 merged commit 0ed77b5 into main Oct 17, 2024
11 checks passed
@sunce86 sunce86 deleted the detect-settlements-from-other-environments branch October 17, 2024 13:42
@github-actions github-actions bot locked and limited conversation to collaborators Oct 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
E:6.2 Time to Happy Moo See https://github.com/cowprotocol/pm/issues/77 for details
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants