Skip to content

Commit

Permalink
Read epoch from state when creating batch swap output
Browse files Browse the repository at this point in the history
This moves the epoch read directly to the route_and_fill method.
  • Loading branch information
cronokirby committed Apr 19, 2024
1 parent 873b1c7 commit 5962448
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 59 deletions.
11 changes: 1 addition & 10 deletions crates/core/component/dex/src/component/circuit_breaker/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
3 changes: 0 additions & 3 deletions crates/core/component/dex/src/component/dex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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() {
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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<Self>,
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
Expand Down Expand Up @@ -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,
Expand Down
23 changes: 2 additions & 21 deletions crates/core/component/dex/src/component/router/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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");

Expand Down
23 changes: 2 additions & 21 deletions crates/core/component/dex/src/component/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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");

Expand Down

0 comments on commit 5962448

Please sign in to comment.