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

Update settlement contract balances in background task #2108

Merged
merged 5 commits into from
Dec 4, 2023
Merged

Conversation

fleupold
Copy link
Contributor

@fleupold fleupold commented Dec 1, 2023

Description

Addresses the bug that we currently fetch settlement buffer balances once per token and don't update them in between auctions, which can lead to solutions relying on outdate internal buffer balance information and thus incorrectly marking interactions as internalizable causing simulation to fail.

Changes

  • Add an e2e showcasing the problematic behavior
  • Spawn a task when creating a new token fetcher, which on arrival of new blocks update the token balances of all cached tokens.

This approach is a bit wasteful, as token balances only really change when a settlement trading this token is included on-chain. However, getting that information in this component is non-trivial. Moreover, some tokens exhibit custom balance logic (e.g. rebasing tokens) which could update the contract balance even without a settlement taking place.

If we deem this approach too inefficient, I would suggest we restrict the number of tokens we cache (using an LRU cache) and re-fetch all relevant information (including decimals and symbol) in case of a cache miss.
If we don't want to lose caching of decimals and symbols for some tokens, I'd suggest splitting the fetcher into two components, one for static information, and one more RecentBlockCache like component for the settlement balances.

Let me know what you think.

How to test

Added an end to end test which fails without the changes in driver::infra::tokens

Related Issues

Fixes #2093

@fleupold fleupold marked this pull request as ready for review December 1, 2023 14:37
@fleupold fleupold requested a review from a team as a code owner December 1, 2023 14:37
crates/driver/src/infra/tokens.rs Outdated Show resolved Hide resolved
crates/driver/src/infra/tokens.rs Outdated Show resolved Hide resolved
crates/driver/src/infra/tokens.rs Outdated Show resolved Hide resolved
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.

Top-Notch

if let Some(balance) = balances.remove(key) {
entry.balance = balance;
} else {
tracing::info!(?key, "key without balance update");
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the purpose of the INFO level here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's supposed to help us to debug the rare race condition where we add a new token balance to the cache while the cache update is in flight.
The new item may not be updated (it should be up to date but in theory the block could also have just changed) and therefore we could pass a slightly outdated buffer list to the solvers. Again, I think this is extremely rare, but if it happens we can at least see if from the logs now.

@fleupold fleupold enabled auto-merge (squash) December 4, 2023 10:32
@fleupold fleupold merged commit 39f4f4a into main Dec 4, 2023
7 of 8 checks passed
@fleupold fleupold deleted the fix/2093 branch December 4, 2023 10:36
@github-actions github-actions bot locked and limited conversation to collaborators Dec 4, 2023
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.

bug: Settlement contract balances get cached too long
3 participants