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

route_and_fill proptests #4509

Closed
wants to merge 11 commits into from
Closed
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/bin/pcli/src/command/query/dex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ impl DexCmd {
swap_execution.output.format(&cache),
);

println!("Traces len: {}", swap_execution.traces.len());
// Try to make a nice table of execution traces. To do this, first find
// the max length of any subtrace:
let max_trace_len = swap_execution
Expand Down
1 change: 1 addition & 0 deletions crates/core/component/dex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,4 @@ rand = {workspace = true}
tracing-subscriber = {workspace = true}
rand_chacha = {workspace = true}
itertools = "0.11"
penumbra-test-subscriber = { workspace = true }
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Seeds for failure cases proptest has generated in the past. It is
# automatically read and these particular cases re-run before any
# novel cases are generated.
#
# It is recommended to check this file in to source control so that
# everyone who runs the test benefits from these saved cases.
cc f2dc95dae6b7bbc7082a5aff58b8bbceebe679b754995af2a914b9f3f6f69632 # shrinks to input_amount = 7265245127025560547, num_paths_1 = 11, num_paths_2 = 7, num_paths_3 = 19
8 changes: 8 additions & 0 deletions crates/core/component/dex/src/component/arb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ pub trait Arbitrage: StateWrite + Sized {
// output from the route-and-fill, plus the unfilled input.
let total_output = output + unfilled_input;

tracing::debug!(
?filled_input,
?output,
?unfilled_input,
?total_output,
"arb: finished route-and-fill"
);

// Now "repay" the flash loan by subtracting it from the total output.
let Some(arb_profit) = total_output.checked_sub(&flash_loan.amount) else {
// This shouldn't happen, but because route-and-fill prioritizes
Expand Down
23 changes: 18 additions & 5 deletions crates/core/component/dex/src/component/router/fill_route.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
collections::{BTreeMap, BTreeSet},
collections::{BTreeMap, BTreeSet, HashSet},
pin::Pin,
};

Expand Down Expand Up @@ -74,7 +74,7 @@ pub trait FillRoute: StateWrite + Sized {
input: Value,
hops: &[asset::Id],
spill_price: Option<U128x128>,
) -> Result<SwapExecution, FillError> {
) -> Result<(SwapExecution, HashSet<position::Id>), FillError> {
fill_route_inner(self, input, hops, spill_price, true).await
}
}
Expand All @@ -87,7 +87,8 @@ async fn fill_route_inner<S: StateWrite + Sized>(
hops: &[asset::Id],
spill_price: Option<U128x128>,
ensure_progress: bool,
) -> Result<SwapExecution, FillError> {
) -> Result<(SwapExecution, HashSet<position::Id>), FillError> {
tracing::debug!(?spill_price, ?ensure_progress, "filling route inner");
let fill_start = std::time::Instant::now();

// Build a transaction for this execution, so if we error out at any
Expand Down Expand Up @@ -121,7 +122,13 @@ async fn fill_route_inner<S: StateWrite + Sized>(
};

let mut frontier = Frontier::load(&mut this, pairs).await?;
tracing::debug!(?frontier, "assembled initial frontier");
tracing::debug!(?frontier, positions = ?frontier.positions.len(), "assembled initial frontier");
tracing::debug!(positions = ?frontier.positions.iter().map(|p| p.id()).collect::<Vec<_>>(), ?route, "positions chosen for route");
let positions_set = frontier
.positions
.iter()
.map(|p| p.id())
.collect::<HashSet<_>>();

// Tracks whether we've already filled at least once, so we can skip the spill price check
// until we've consumed at least one position.
Expand Down Expand Up @@ -168,6 +175,7 @@ async fn fill_route_inner<S: StateWrite + Sized>(
} else {
true
};
tracing::debug!(?spill_price, actual_price = ?tx.actual_price(), "should_apply: {}", should_apply);

if !should_apply {
tracing::debug!(
Expand Down Expand Up @@ -267,7 +275,7 @@ async fn fill_route_inner<S: StateWrite + Sized>(
let fill_elapsed = fill_start.elapsed();
metrics::histogram!(metrics::DEX_ROUTE_FILL_DURATION).record(fill_elapsed);
// cleanup / finalization
Ok(swap_execution)
Ok((swap_execution, positions_set))
}

/// Breaksdown a route into a collection of `DirectedTradingPair`, this is mostly useful
Expand Down Expand Up @@ -671,6 +679,11 @@ impl<S: StateRead + StateWrite> Frontier<S> {
// Work forwards along the path from the constraining position.
self.fill_forward(&mut tx, constraining_index + 1, exactly_consumed_reserves);

tracing::debug!(
new_reserves = ?tx.new_reserves,
"filled constrained position, propagating changes along frontier"
);

tx
}

Expand Down
3 changes: 3 additions & 0 deletions crates/core/component/dex/src/component/router/path_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pub trait PathSearch: StateRead + Clone + 'static {
price_limit,
} = params;

// TODO: use seen_position_sets to avoid re-searching the same paths
// to prevent cycles between traces

// Initialize some metrics for calculating time spent on path searching
// vs route filling. We use vecs so we can count across iterations of the loop.
let path_start = std::time::Instant::now();
Expand Down
16 changes: 14 additions & 2 deletions crates/core/component/dex/src/component/router/route_and_fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ pub trait RouteAndFill: StateWrite + Sized {

let max_delta_1: Amount = MAX_RESERVE_AMOUNT.into();

let mut seen_position_sets = Vec::new();

// Termination conditions:
// 1. We have no more delta_1 remaining
// 2. A path can no longer be found
Expand Down Expand Up @@ -214,9 +216,10 @@ pub trait RouteAndFill: StateWrite + Sized {
.expect("expected state to have no other refs")
.fill_route(delta_1, &path, spill_price)
.await;
println!("execution: {:?}", execution);

let execution = match execution {
Ok(execution) => execution,
let (execution, positions_executed) = match execution {
Ok((execution, positions_executed)) => (execution, positions_executed),
Err(FillError::ExecutionOverflow(position_id)) => {
// We have encountered an overflow during the execution of the route.
// To route around this, we will close the position and try to route and fill again.
Expand All @@ -236,6 +239,15 @@ pub trait RouteAndFill: StateWrite + Sized {
}
};

println!("execution positions: {:?}", positions_executed);
// If we've seen this set of positions before, avoid executing against them again.
// Ideally this would be fed into path search but that's not feasible.
if seen_position_sets.contains(&positions_executed) {
tracing::debug!("repeated position set");
break;
}
seen_position_sets.push(positions_executed);

// Immediately track the execution in the state.
(total_output_2, total_unfilled_1) = {
let lambda_2 = execution.output;
Expand Down
Loading
Loading