Skip to content

Only track balances of tokens with internal buffer trading#4581

Merged
MartinquaXD merged 5 commits into
mainfrom
reduce-token-info-rpc-calls
Jul 1, 2026
Merged

Only track balances of tokens with internal buffer trading#4581
MartinquaXD merged 5 commits into
mainfrom
reduce-token-info-rpc-calls

Conversation

@MartinquaXD

Copy link
Copy Markdown
Contributor

Description

The protocol allows to do internal buffer trading which means a solver is allowed to use the settlement contract's token balances as if it was an AMM. To enable solvers to make use of this the driver continuously monitors the settlement contract's token balances and writes them into the auction instance.
However, so far the driver kept monitoring token balances for every token it ever needed to fetch metadata for despite only a couple of tokens actually being allow listed for internal buffer trading.

This oversight leads to tons of RPC requests being wasted. The number scales with the tokens_seen_by_driver * block_interval which leads to especially wasteful traffic on chains like BNB which have a good amount of traffic and are very fast.
For example here we can see that after 4 days of running the driver we continuously use ~4 times as many RPC requests for fetching token balances for the settlement contract as for the traders. At the peak roughly half of the driver's RPC requests were dedicated to that when it actually should be 0 because as it turns out there aren't even any tokens on BNB where internal buffer trading is currently allowed.
Screenshot 2026-07-01 at 08 46 30

Changes

Instead of monitoring the balances for every tokens the driver ever saw we explicitly mark tokens for monitoring whenever we build a new auction.
Since the set of tokens to monitor is not expected to change often I tried to make this logic as zero overhead as possible by first taking a read lock to see which tokens need updating at all (on average 0) and I also avoided allocating vectors as much as possible.

@MartinquaXD MartinquaXD requested a review from a team as a code owner July 1, 2026 08:55
@claude

claude Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Claude finished @MartinquaXD's task in 1m 45s —— View job


PR Review: Only track balances of tokens with internal buffer trading

  • Read the diff and surrounding code
  • Analyze correctness of the new monitoring logic
  • Post inline findings

Nice, targeted optimization — the read-lock-first / avoid-allocation approach in keep_track_of_balances is clean, and gating getkeep_track_of_balances ordering guarantees the trusted tokens are already cached before the flag is set. 👍

I found one issue (posted inline):

  • update_balances write-back loop logs every non-monitored token each block (crates/driver/src/infra/tokens.rs:139). balances now only holds monitored tokens, but the loop still iterates the whole cache, so every non-monitored token lands in keys_without_balances and gets logged at info! on every block. Filtering the loop to monitor_balance entries fixes the spam and makes it cheap in the common case. Fix →

Minor (non-blocking) observations, no change required:

  • A trusted token whose metadata fetch fails in get won't be present in the cache and so will never be monitored — pre-existing edge case, fine to leave.
  • monitor_balance is monotonic (a token that stops being trusted keeps being monitored), which matches the PR's "set doesn't change often" assumption.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces continuous balance monitoring for trusted tokens by adding a monitor_balance flag to token metadata and registering these tokens during solve requests. A critical issue was identified where filtering the updated balances to only monitored tokens causes unmonitored tokens to be incorrectly flagged as missing during the cache update loop, which would spam the logs on every block. The feedback suggests updating the cache iteration logic to only check and report missing balances for tokens that are actually being monitored.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread crates/driver/src/infra/tokens.rs
Comment thread crates/driver/src/infra/tokens.rs

@jmg-duarte jmg-duarte left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, but couldn't we pass the list of trusted tokens to Inner and set monitor_balance accordingly right on insert instead of continuously updating the monitor_balance field?

Comment thread crates/driver/src/infra/tokens.rs Outdated
@MartinquaXD

Copy link
Copy Markdown
Contributor Author

couldn't we pass the list of trusted tokens to Inner and set monitor_balance accordingly right on insert instead of continuously updating the monitor_balance field?

The set of trusted tokens is defined by the protocol so configuring them in the driver means everyone has to keep those settings in sync.
For context the list of trusted tokens has 27 items on mainnet at the moment so in practice we are only paying the cost of 27 hashmap lookups which I think justifies this simpler logic.

@MartinquaXD MartinquaXD added this pull request to the merge queue Jul 1, 2026
Merged via the queue into main with commit caf06cb Jul 1, 2026
20 checks passed
@MartinquaXD MartinquaXD deleted the reduce-token-info-rpc-calls branch July 1, 2026 20:01
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 1, 2026
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.

2 participants