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

dex: fix frontier cache incoherence #4283

Merged
merged 3 commits into from
Apr 30, 2024
Merged

Conversation

erwanor
Copy link
Member

@erwanor erwanor commented Apr 29, 2024

Description

This PR fix a bug in the Frontier position cache that caused a position execution guard to be hit.

Context

During routing execution, the DEX engine assemble frontiers of positions to execute against. It maintains an internal cache with a position stream for each hop in the path, along with the ids and state of the current loaded set. The position cache is persisted in its entirety when a round of routing execution has completed (via Frontier::save), and partially, if a position needs to be replaced (Frontier::replace_position).

Bug

The bug occurs when the following conditions align:

  • a position needs to be replaced
  • that position is auto-closing, and set to Closed when recording its execution
  • no replacements are available

In that case, since the position rotation logic does not update the internal state of the position, it will attempt to record execution of a stale position when saving the frontier.

Fix

The fix is two-fold, first, PositionManager::position_execution should make it clear that it can mutate its input and return the updated position state to allow callers to inspect its state. Once that's done, the position rotation logic will be able to immediately update its internal cache with the actual position state so that there are no incoherences between its internal view and the actual chain state.

Checklist before requesting a review

  • If this code contains consensus-breaking changes, I have added the "consensus-breaking" label. Otherwise, I declare my belief that there are not consensus-breaking changes, for the following reason:

    This is consensus breaking.

@erwanor erwanor added C-bug Category: a bug A-dex Area: Relates to the dex labels Apr 29, 2024
@erwanor erwanor requested review from hdevalence and zbuc April 29, 2024 16:47
@erwanor erwanor self-assigned this Apr 29, 2024
@erwanor erwanor mentioned this pull request Apr 29, 2024
65 tasks
@erwanor erwanor added _P-V1 Priority: slated for V1 release _P-high High priority labels Apr 29, 2024
@erwanor erwanor added this to the Sprint 5 milestone Apr 29, 2024
@conorsch conorsch added the consensus-breaking breaking change to execution of on-chain data label Apr 29, 2024
@conorsch
Copy link
Contributor

This is consensus breaking.

Added label. This means we'll need to write a migration in order to release this, and provide validators with instructions, right?

@erwanor
Copy link
Member Author

erwanor commented Apr 29, 2024

Ooops thanks I missed that, no migration needed, it's an internal structure and only used during execution

//
// If so, skip the write, but more importantly, skip emitting an event,
// so tooling doesn't get confused about a no-op execution.
if prev_state == new_state {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: state is a bit overridden here and it took me a couple reads to understand that prev_state/new_state aren't instances of the position::State enum but instead Positions.

Copy link
Member

@zbuc zbuc left a comment

Choose a reason for hiding this comment

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

LGTM

@hdevalence
Copy link
Member

Can we fix #4123 as part of these changes?

Copy link
Member

@hdevalence hdevalence left a comment

Choose a reason for hiding this comment

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

LGTM but we should fix #4123 while we're making these changes.

@erwanor erwanor force-pushed the erwan/dex_frontier_inspect branch from 0fa692e to d088689 Compare April 29, 2024 23:09
@erwanor
Copy link
Member Author

erwanor commented Apr 29, 2024

@hdevalence added a fix/event for #4123

@erwanor erwanor requested a review from hdevalence April 29, 2024 23:32
@erwanor erwanor merged commit 9dc5834 into main Apr 30, 2024
13 checks passed
@erwanor erwanor deleted the erwan/dex_frontier_inspect branch April 30, 2024 16:51
zbuc added a commit that referenced this pull request May 30, 2024
zbuc added a commit that referenced this pull request May 30, 2024
zbuc added a commit that referenced this pull request May 30, 2024
@zbuc zbuc mentioned this pull request May 30, 2024
1 task
zbuc added a commit that referenced this pull request May 31, 2024
zbuc added a commit that referenced this pull request Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-dex Area: Relates to the dex C-bug Category: a bug consensus-breaking breaking change to execution of on-chain data _P-high High priority _P-V1 Priority: slated for V1 release
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants