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

Do not count "in-market" orders for the order limit #2433 #2456

Merged
merged 4 commits into from
Mar 5, 2024

Conversation

m-lord-renkse
Copy link
Contributor

@m-lord-renkse m-lord-renkse commented Mar 1, 2024

Description

Only check for order limits when the orders are class "Limit". I kept the legacy code/behavior, in order not to make a regression breaking change.

Changes

  • Only do the order limit check if the order is class "Limit" and it is not "in-market"

How to test

  1. Unit test
  2. e2e test

Fixes #2433

@m-lord-renkse m-lord-renkse requested a review from a team as a code owner March 1, 2024 10:56
@m-lord-renkse m-lord-renkse force-pushed the do-not-count-in-market-orders-for-limit branch 3 times, most recently from 8ab5e22 to 0d0dc43 Compare March 1, 2024 11:21
@m-lord-renkse m-lord-renkse force-pushed the do-not-count-in-market-orders-for-limit branch from 43161bd to 36dc6b3 Compare March 4, 2024 14:49
@m-lord-renkse m-lord-renkse force-pushed the do-not-count-in-market-orders-for-limit branch from 36dc6b3 to f74923b Compare March 4, 2024 14:54
pub quote_sell_token_price: f64,
}

pub async fn user_orders_with_quote(
Copy link
Contributor

Choose a reason for hiding this comment

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

There is already a SQL query that counts limit orders. Instead of fetching all this data and counting in the rust code you could have the in-market check already in SQL to only return a single number.

Copy link
Contributor Author

@m-lord-renkse m-lord-renkse Mar 4, 2024

Choose a reason for hiding this comment

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

@MartinquaXD how could we do that? we need to precisely operate the f64 so we can consider the limit order as "out of market" 🤔 do you mean by doing the operation directly in SQL?

see https://github.com/cowprotocol/services/pull/2456/files#diff-d22c8612ee861d55e838fdc5487a1c139da73b795295670acdcccea91f805011R386

Copy link
Contributor

Choose a reason for hiding this comment

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

Basically reimplement the logic of is_order_outside_market_price() inside count_limit_orders_by_owner(). Hopefully there is a way in PSQL to convert the types appropriately for the computation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@MartinquaXD In my opinion, doing arithmetical operations in PSQL query involving U256 and f64 is really not a good idea (unless it is plain sum, and even so), we are going to lose precision, and we are going to push CPU overhead to the database. What's more, I don't think we will save time, since database CPU is less powerful than the backend CPU, and the rust code will be definitely faster.

I understand your concern of potential long queries in case the user placed many orders, for that please, consider adding the internal class (as I proposed here #2456 (comment)), which will tackle down all the issues. We could also do the queries by pagination in case of long queries.

Copy link
Contributor

Choose a reason for hiding this comment

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

not a good idea (unless it is plain sum

Interesting. Why do you think so?

we are going to lose precision

This is also the case in the rust code since.fee()calls U256::from_f64_lossy().

I don't think we will save time, since database CPU is less powerful than the backend CPU, and the rust code will be definitely faster.

Computations might be faster in rust but I very much doubt that returning all that data, parsing it, allocating the structs (1 Vec per BigDecimal) and doing the computation in rust will be faster than doing a computation inside the db and returning a single number.

This is not a hill I'm willing to die on (although I disagree) if you feel strongly about keeping the computation out of the DB I'd at least consider fetching the data using a stream instead of call .fetch_all() to limit the memory consumption when a user has lots of orders.

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess we would need to benchmark this in order to get a definitive answer. It's true that for accounts with a lot of orders this may become a bottleneck. In theory we could also decide the "in-marketness" of an order when it's inserted into the db (and then simply count based on a flag)?

I'd be curious how PSQL math is less accurate than rust math?

Copy link
Contributor

@fleupold fleupold left a comment

Choose a reason for hiding this comment

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

Looks good in general to me. Not too sure how much work it would be to decide the in-marketness at insertion time (we can run a DB migration to update historic orders).

pub quote_sell_token_price: f64,
}

pub async fn user_orders_with_quote(
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess we would need to benchmark this in order to get a definitive answer. It's true that for accounts with a lot of orders this may become a bottleneck. In theory we could also decide the "in-marketness" of an order when it's inserted into the db (and then simply count based on a flag)?

I'd be curious how PSQL math is less accurate than rust math?

crates/shared/src/order_validation.rs Outdated Show resolved Hide resolved
@m-lord-renkse
Copy link
Contributor Author

m-lord-renkse commented Mar 5, 2024

I'd be curious how PSQL math is less accurate than rust math?

@fleupold @MartinquaXD implementation of the same standard IEEE 754 does not guarantee exactly same behavior. For example, the rounding is the same, but when the rounding is done, may differ. The standard doesn't define many corner cases which makes each implementation slightly different. Also the compilation and optimizations impacts it a lot, actually same code of Rust can vary f64 operations depending on the architecture and on the optimization selected to compile the code.

Interesting articles (I actually didn't know many things of those articles, it is quite interesting):

That's why I do not like splitting arithmetic business logic between PSQL and Rust. Well, that's also why I don't like f64 at all, in detriment of https://docs.rs/crate/rust_decimal/latest (I may expand more about this topic in Slack). We are losing precision in the fees because of the f64 operations in Rust (but I don't know how important it is, or the impact it really has).

Copy link
Contributor

@MartinquaXD MartinquaXD left a comment

Choose a reason for hiding this comment

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

Approving since we discussed via DMs that the next task will be to refactor how we store order classes (while getting rid of liquidity orders) which will make the whole discussion obsolete.

@m-lord-renkse
Copy link
Contributor Author

@fleupold @MartinquaXD the issue to fully fix it and eliminate the order class, as agreed: #2469

@m-lord-renkse m-lord-renkse merged commit 3cda785 into main Mar 5, 2024
9 checks passed
@m-lord-renkse m-lord-renkse deleted the do-not-count-in-market-orders-for-limit branch March 5, 2024 15:51
@github-actions github-actions bot locked and limited conversation to collaborators Mar 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

chore: Don't count "in-market" limit order to the max number of limit orders per account
5 participants