From 5962448be2a8fb60fe62e20f13d200681a8dd939 Mon Sep 17 00:00:00 2001 From: Lucas Meier Date: Fri, 19 Apr 2024 12:27:40 -0700 Subject: [PATCH] Read epoch from state when creating batch swap output This moves the epoch read directly to the route_and_fill method. --- .../src/component/circuit_breaker/value.rs | 11 +-------- .../core/component/dex/src/component/dex.rs | 3 --- .../src/component/router/route_and_fill.rs | 8 +++---- .../dex/src/component/router/tests.rs | 23 ++----------------- .../core/component/dex/src/component/tests.rs | 23 ++----------------- 5 files changed, 9 insertions(+), 59 deletions(-) diff --git a/crates/core/component/dex/src/component/circuit_breaker/value.rs b/crates/core/component/dex/src/component/circuit_breaker/value.rs index 5337549861..04feb0f6a1 100644 --- a/crates/core/component/dex/src/component/circuit_breaker/value.rs +++ b/crates/core/component/dex/src/component/circuit_breaker/value.rs @@ -253,16 +253,7 @@ mod tests { let routing_params = state.routing_params().await.unwrap(); // This call should panic due to the outflow of gn not being covered by the circuit breaker. state - .handle_batch_swaps( - trading_pair, - swap_flow, - 0, - Epoch { - index: 0, - start_height: 0, - }, - routing_params, - ) + .handle_batch_swaps(trading_pair, swap_flow, 0, routing_params) .await .expect("unable to process batch swaps"); } diff --git a/crates/core/component/dex/src/component/dex.rs b/crates/core/component/dex/src/component/dex.rs index 1bbaf4e41e..0172e34c62 100644 --- a/crates/core/component/dex/src/component/dex.rs +++ b/crates/core/component/dex/src/component/dex.rs @@ -7,7 +7,6 @@ use cnidarium_component::Component; use penumbra_asset::{asset, Value, STAKING_TOKEN_ASSET_ID}; use penumbra_num::Amount; use penumbra_proto::{StateReadProto, StateWriteProto}; -use penumbra_sct::component::clock::EpochRead; use tendermint::v0_37::abci; use tracing::instrument; @@ -56,7 +55,6 @@ impl Component for Dex { // 2. For each batch swap during the block, calculate clearing prices and set in the JMT. - let current_epoch = state.get_current_epoch().await.expect("epoch is set"); let routing_params = state.routing_params().await.expect("dex params are set"); for (trading_pair, swap_flows) in state.swap_flows() { @@ -69,7 +67,6 @@ impl Component for Dex { .height .try_into() .expect("height is part of the end block data"), - current_epoch, // Always include both ends of the target pair as fixed candidates. routing_params .clone() diff --git a/crates/core/component/dex/src/component/router/route_and_fill.rs b/crates/core/component/dex/src/component/router/route_and_fill.rs index 47dfd997e3..f889ae1b8a 100644 --- a/crates/core/component/dex/src/component/router/route_and_fill.rs +++ b/crates/core/component/dex/src/component/router/route_and_fill.rs @@ -5,7 +5,7 @@ use async_trait::async_trait; use cnidarium::StateWrite; use penumbra_asset::{asset, Value}; use penumbra_num::Amount; -use penumbra_sct::epoch::Epoch; +use penumbra_sct::component::clock::EpochRead; use tracing::instrument; use crate::{ @@ -24,14 +24,13 @@ use super::fill_route::FillError; /// a block's batch swap flows. #[async_trait] pub trait HandleBatchSwaps: StateWrite + Sized { - #[instrument(skip(self, trading_pair, batch_data, block_height, epoch, params))] + #[instrument(skip(self, trading_pair, batch_data, block_height, params))] async fn handle_batch_swaps( self: &mut Arc, trading_pair: TradingPair, batch_data: SwapFlow, - // TODO: why not read these 2 from the state? + // This will be read from the ABCI request block_height: u64, - epoch: Epoch, params: RoutingParams, ) -> Result<()> where @@ -89,6 +88,7 @@ pub trait HandleBatchSwaps: StateWrite + Sized { ), None => (0u64.into(), delta_2), }; + let epoch = self.get_current_epoch().await.expect("epoch is set"); let output_data = BatchSwapOutputData { height: block_height, epoch_starting_height: epoch.start_height, diff --git a/crates/core/component/dex/src/component/router/tests.rs b/crates/core/component/dex/src/component/router/tests.rs index 42048a30fd..21415e426b 100644 --- a/crates/core/component/dex/src/component/router/tests.rs +++ b/crates/core/component/dex/src/component/router/tests.rs @@ -5,7 +5,6 @@ use core::panic; use futures::StreamExt; use penumbra_asset::{asset, Value}; use penumbra_num::{fixpoint::U128x128, Amount}; -use penumbra_sct::epoch::Epoch; use rand_core::OsRng; use std::sync::Arc; @@ -1025,16 +1024,7 @@ async fn best_position_route_and_fill() -> anyhow::Result<()> { .unwrap(); let routing_params = state.routing_params().await.unwrap(); state - .handle_batch_swaps( - trading_pair, - swap_flow, - 0u32.into(), - Epoch { - index: 0, - start_height: 0, - }, - routing_params, - ) + .handle_batch_swaps(trading_pair, swap_flow, 0u32.into(), routing_params) .await .expect("unable to process batch swaps"); @@ -1175,16 +1165,7 @@ async fn multi_hop_route_and_fill() -> anyhow::Result<()> { .unwrap(); let routing_params = state.routing_params().await.unwrap(); state - .handle_batch_swaps( - trading_pair, - swap_flow, - 0u32.into(), - Epoch { - index: 0, - start_height: 0, - }, - routing_params, - ) + .handle_batch_swaps(trading_pair, swap_flow, 0u32.into(), routing_params) .await .expect("unable to process batch swaps"); diff --git a/crates/core/component/dex/src/component/tests.rs b/crates/core/component/dex/src/component/tests.rs index 72ecda092d..5e5c8d43dc 100644 --- a/crates/core/component/dex/src/component/tests.rs +++ b/crates/core/component/dex/src/component/tests.rs @@ -6,7 +6,6 @@ use cnidarium::{ArcStateDeltaExt, StateDelta, TempStorage}; use futures::StreamExt; use penumbra_asset::{asset, Value}; use penumbra_num::Amount; -use penumbra_sct::epoch::Epoch; use rand_core::OsRng; use crate::lp::action::PositionOpen; @@ -633,16 +632,7 @@ async fn swap_execution_tests() -> anyhow::Result<()> { .unwrap(); let routing_params = state.routing_params().await.unwrap(); state - .handle_batch_swaps( - trading_pair, - swap_flow, - 0, - Epoch { - index: 0, - start_height: 0, - }, - routing_params, - ) + .handle_batch_swaps(trading_pair, swap_flow, 0, routing_params) .await .expect("unable to process batch swaps"); @@ -750,16 +740,7 @@ async fn swap_execution_tests() -> anyhow::Result<()> { .unwrap(); let routing_params = state.routing_params().await.unwrap(); state - .handle_batch_swaps( - trading_pair, - swap_flow, - 0u32.into(), - Epoch { - index: 0, - start_height: 0, - }, - routing_params, - ) + .handle_batch_swaps(trading_pair, swap_flow, 0u32.into(), routing_params) .await .expect("unable to process batch swaps");