-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Driver] Allow stale liquidity for quote requests (#1924)
# Description When giving production traffic to the colocated setup, we noticed a lot of price estimators (even fast ones like baseline) getting rate limited frequently. This is due to the driver requesting liquidity for the most recent block and blocking on fetching it if it's not available. For the legacy setup, there was a code path specifically for quotes, allowing to use a "recent" instead of the latest block for fetching liquidity. This PR recreates this path for the co-located setup # Changes <!-- List of detailed changes (how the change is accomplished) --> - Adds a flag to the liquidity fetcher indicating whether stale liquidity is allowed - If this flag is set, quote for _recent_ instead of _latest_ block (which should already be cached) ## How to test Run this benchmark script against before and after: ```sh SECONDS=0 while (( SECONDS < 60 )); do time curl -H 'content-type: application/json' --data '{"from": "0xc3792470cee7e0d42c2be8e9552bd651766c5178","buyToken": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48","sellToken": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2","kind": "sell","sellAmountAfterFee": "100000000000000000", "quality":"optimal"}' http://localhost:8080/api/v1/quote done ``` observe http://localhost:11088/metrics in both cases and look for liquidity cache hits: Before: ``` driver_recent_block_cache_hits{cache_type="uniswapv2"} 34300 driver_recent_block_cache_misses{cache_type="uniswapv2"} 840 ``` After ``` driver_recent_block_cache_hits{cache_type="uniswapv2"} 39200 driver_recent_block_cache_misses{cache_type="uniswapv2"} 140 ``` Note that we now only have 1 cache miss (140 pools) on cold start vs 1 cache miss on each new block and higher overall throughput ## Related issues #1672 --------- Co-authored-by: Nicholas Rodrigues Lordello <[email protected]>
- Loading branch information
Showing
8 changed files
with
175 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use { | ||
e2e::{setup::*, tx, tx_value}, | ||
ethcontract::U256, | ||
model::quote::{OrderQuoteRequest, OrderQuoteSide, SellAmount}, | ||
number::nonzero::U256 as NonZeroU256, | ||
shared::ethrpc::Web3, | ||
}; | ||
|
||
#[tokio::test] | ||
#[ignore] | ||
async fn local_node_uses_stale_liquidity() { | ||
run_test(uses_stale_liquidity).await; | ||
} | ||
|
||
async fn uses_stale_liquidity(web3: Web3) { | ||
tracing::info!("Setting up chain state."); | ||
let mut onchain = OnchainComponents::deploy(web3.clone()).await; | ||
|
||
let [solver] = onchain.make_solvers(to_wei(10)).await; | ||
let [trader] = onchain.make_accounts(to_wei(2)).await; | ||
let [token] = onchain | ||
.deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) | ||
.await; | ||
|
||
tx!( | ||
trader.account(), | ||
onchain | ||
.contracts() | ||
.weth | ||
.approve(onchain.contracts().allowance, to_wei(1)) | ||
); | ||
tx_value!( | ||
trader.account(), | ||
to_wei(1), | ||
onchain.contracts().weth.deposit() | ||
); | ||
|
||
tracing::info!("Starting services."); | ||
let solver_endpoint = colocation::start_solver(onchain.contracts().weth.address()).await; | ||
colocation::start_driver(onchain.contracts(), &solver_endpoint, &solver); | ||
|
||
let services = Services::new(onchain.contracts()).await; | ||
services.start_autopilot(vec![ | ||
"--enable-colocation=true".to_string(), | ||
"--drivers=http://localhost:11088/test_solver".to_string(), | ||
]); | ||
services | ||
.start_api(vec![ | ||
"--price-estimation-drivers=solver|http://localhost:11088/test_solver".to_string(), | ||
]) | ||
.await; | ||
|
||
let quote = OrderQuoteRequest { | ||
from: trader.address(), | ||
sell_token: onchain.contracts().weth.address(), | ||
buy_token: token.address(), | ||
side: OrderQuoteSide::Sell { | ||
sell_amount: SellAmount::AfterFee { | ||
value: NonZeroU256::new(to_wei(1)).unwrap(), | ||
}, | ||
}, | ||
..Default::default() | ||
}; | ||
|
||
tracing::info!("performining initial quote"); | ||
let first = services.submit_quote("e).await.unwrap(); | ||
|
||
// Now, we want to manually unbalance the pools and assert that the quote | ||
// doesn't change (as the price estimation will use stale pricing data). | ||
onchain | ||
.mint_token_to_weth_uni_v2_pool(&token, to_wei(1_000)) | ||
.await; | ||
|
||
tracing::info!("performining second quote, which should match first"); | ||
let second = services.submit_quote("e).await.unwrap(); | ||
assert_eq!(first.quote.buy_amount, second.quote.buy_amount); | ||
|
||
tracing::info!("waiting for liquidity state to update"); | ||
wait_for_condition(TIMEOUT, || async { | ||
let next = services.submit_quote("e).await.unwrap(); | ||
next.quote.buy_amount != first.quote.buy_amount | ||
}) | ||
.await | ||
.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters