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: hotfix for 2025-01-18 chain halt (alternative) #4994

Merged
merged 1 commit into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use anyhow::Result;
use async_trait::async_trait;
use cnidarium::StateWrite;
use cnidarium_component::ActionHandler;
use penumbra_proto::{DomainType as _, StateWriteProto as _};

use crate::{component::PositionManager, event, lp::action::PositionClose};
use crate::{component::PositionManager, lp::action::PositionClose};

#[async_trait]
/// Debits an opened position NFT and credits a closed position NFT.
Expand All @@ -22,15 +21,7 @@ impl ActionHandler for PositionClose {
// lose the ability to do block-scoped JIT liquidity, where a single
// transaction opens and closes a position, keeping liquidity live only
// during that block's batch swap execution.
state.queue_close_position(self.position_id);

// queue position close you will...
state.record_proto(
event::EventQueuePositionClose {
position_id: self.position_id,
}
.to_proto(),
);
state.queue_close_position(self.position_id).await?;
Copy link
Member

Choose a reason for hiding this comment

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

ACK.


Ok(())
}
Expand Down
35 changes: 31 additions & 4 deletions crates/core/component/dex/src/component/position_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,37 @@ pub trait PositionManager: StateWrite + PositionRead {
}

/// Queues a position to be closed at the end of the block, after batch execution.
fn queue_close_position(&mut self, id: position::Id) {
let mut to_close = self.pending_position_closures();
to_close.push_back(id);
self.object_put(state_key::pending_position_closures(), to_close);
async fn queue_close_position(&mut self, id: position::Id) -> Result<()> {
tracing::debug!(
?id,
"checking current position state before queueing for closure"
);
let current_state = self
.position_by_id(&id)
.await
.expect("fetching position should not fail")
.ok_or_else(|| anyhow::anyhow!("could not find position {} to close", id))?
.tap(|lp| tracing::trace!(prev_state = ?lp, "retrieved previous lp state"));

if current_state.state == position::State::Opened {
Copy link
Member

Choose a reason for hiding this comment

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

ACK. short-circuit on queueing.

tracing::debug!(
?current_state.state,
"queueing opened position for closure"
);
let mut to_close = self.pending_position_closures();
to_close.push_back(id);
self.object_put(state_key::pending_position_closures(), to_close);
Copy link
Member

Choose a reason for hiding this comment

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

ACK. queuing gated on valid state antecedent.


// queue position close you will...
self.record_proto(event::EventQueuePositionClose { position_id: id }.to_proto());
} else {
tracing::debug!(
Copy link
Member

Choose a reason for hiding this comment

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

ACK. no-op on contentious state antecedent.

?current_state.state,
"skipping queueing for closure of non-opened position"
);
}

Ok(())
}

/// Close all positions that have been queued for closure.
Expand Down
Loading