-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Optimize GraphQL "coins to spend" query #2391
Comments
This issue will be delivered in at least 2 PRs:
How to achieve pt. 1:
Example DB structure:
Messages:
Corresponding
How to achieve pt. 2:
Remarks:
Questions:
The PoC implementation of the above is available here: https://github.com/rafal-ch/coins_to_spend_poc Footnotes
|
Current flow:
|
For coins the key bytes represent: For messages the key bytes represent: |
Also, to disambiguate between "coins with base asset id" and "message" we'll use the "value" in the DB. |
Closes #2391 This PR includes all changes from the [Part 1 PR](#2455), making it deprecated. ## Description Changes in this PR: #### The new `CoinsToSpend` index * This is the database that stores all coins to spend sorted by the amounts (i.e. largest-by-value coins first) * The key consists of several parts * _Retryable flag_ - to distinguish between retryable messages and other coins * _Address_ (owner) * _AssetID_ * _Amount_ - as "big-endian" bytes to leverage the RocksDB key sorting capabilities * _Foreign Key_ - this are bytes of the key from either the `Messages` or `Coins` on-chain databases * for messages this is a 32-bytes `Nonce` * for coins this is a 34-bytes `UtxoId` * The value is an instance of `IndexedCoinType` enum, so we know which on-chain database to query when returning the actual coins * This index is updated when executor events are processed * When querying for "coins to spend" the following algorithm is applied: * First, we get as many "big" coins as required to satisfy _double the amount_ from the query (respecting `max` and `excluded` params) * If we have enough coins, but there are still some "slots" in the query left (because we selected less coins than `max`) we fill the remaining slots with a **random** number of "dust" coins * If it happens that the value of selected "dust coins" is able to cover the value of some of the already selected "big coins", we remove the latter from the response * If at any step we encounter a problem (reading from database, integer conversions, etc.) we bail with an appropriate error #### Changes to `CoinsQueryError` type * The `MaxCoinsReached` variant has been removed because in the new algorithm we never query for more coins than the specified `max`, hence, without additional effort, we are not able to tell whether the query could be satisfied if user provided bigger `max` * The `InsufficientCoins` has been renamed to `InsufficientCoinsForTheMax` and it now contains the additional `max` field #### Off-chain database metadata * The metadata for off-chain database now contain the additional `IndexationKind` - `CoinsToSpend` #### Refactoring * The `indexation.rs` module was split into separate files, each per indexation type + errors + some utils. #### Other * Integration tests have to be updated to not expect the exact number of coins to spend in the response (currently, due to randomness, we're not deterministic in this regard) * The number of excluded ids in the `coinsToSpend` GraphQL query is now limited to the maximum number of inputs allowed in transaction. ### Before requesting review - [X] I have reviewed the code myself - [X] I have created follow-up issues caused by this PR and linked them here ### Follow-up issues * #2498 * #2448 * #2428 * #2499 * #2496 --------- Co-authored-by: Green Baneling <[email protected]>
The source of this issue is #623.
Initially, the idea was to remove all complex queries from
fuel-core
and make it part of the indexer's responsibility. But the SDK is deeply integrated with these basic queries, that we need to support them onfuel-core
. For that, we need to optimize the following query by aggregating some information in the off-chain worker or adding new indexes to the database:coins_to_spend
query is very complex and requiresn^2
operations wheren
is the number of coins and messages. The algorithm itself can use additional indexes from RocksDB to solve the issue with dust coins and works fast.Extracted from the #1965, which initially covered optimizations for 3 different areas: balances, coins to spend, dry run.
The text was updated successfully, but these errors were encountered: