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

Revert "chore: bump sor to 4.7.5 - fix: cached routes cache invalidation (#869)" #890

Closed
wants to merge 1 commit 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
14 changes: 3 additions & 11 deletions lib/handlers/injector-sor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ import {
emptyV4FeeTickSpacingsHookAddresses,
EXTRA_V4_FEE_TICK_SPACINGS_HOOK_ADDRESSES,
} from '../util/extraV4FeeTiersTickSpacingsHookAddresses'
import { NEW_CACHED_ROUTES_ROLLOUT_PERCENT } from '../util/newCachedRoutesRolloutPercent'

export const SUPPORTED_CHAINS: ChainId[] = [
ChainId.MAINNET,
Expand Down Expand Up @@ -145,10 +144,6 @@ export type ContainerDependencies = {
tokenValidatorProvider: TokenValidatorProvider
tokenPropertiesProvider: ITokenPropertiesProvider
v2Supported: ChainId[]
v4Supported?: ChainId[]
mixedSupported?: ChainId[]
v4PoolParams?: Array<[number, number, string]>
cachedRoutesCacheInvalidationFixRolloutPercentage?: number
}

export interface ContainerInjected {
Expand Down Expand Up @@ -302,7 +297,7 @@ export abstract class InjectorSOR<Router, QueryParams> extends Injector<
underlyingV2PoolProvider,
new V2DynamoCache(V2_PAIRS_CACHE_TABLE_NAME!)
)
const v4PoolParams = getApplicableV4FeesTickspacingsHooks(chainId).concat(
const v4PoolsParams = getApplicableV4FeesTickspacingsHooks(chainId).concat(
EXTRA_V4_FEE_TICK_SPACINGS_HOOK_ADDRESSES[chainId] ?? emptyV4FeeTickSpacingsHookAddresses
)

Expand All @@ -321,7 +316,7 @@ export abstract class InjectorSOR<Router, QueryParams> extends Injector<
POOL_CACHE_BUCKET_3!,
POOL_CACHE_GZIP_KEY!,
v4PoolProvider,
v4PoolParams
v4PoolsParams
)) as V4AWSSubgraphProvider,
(await this.instantiateSubgraphProvider(
chainId,
Expand Down Expand Up @@ -492,8 +487,6 @@ export abstract class InjectorSOR<Router, QueryParams> extends Injector<

const mixedSupported = [ChainId.MAINNET, ChainId.SEPOLIA, ChainId.GOERLI]

const cachedRoutesCacheInvalidationFixRolloutPercentage = NEW_CACHED_ROUTES_ROLLOUT_PERCENT[chainId]

return {
chainId,
dependencies: {
Expand Down Expand Up @@ -527,8 +520,7 @@ export abstract class InjectorSOR<Router, QueryParams> extends Injector<
v2Supported,
v4Supported,
mixedSupported,
v4PoolParams,
cachedRoutesCacheInvalidationFixRolloutPercentage,
v4PoolsParams,
},
}
})
Expand Down
8 changes: 0 additions & 8 deletions lib/handlers/quote/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ export class QuoteHandlerInjector extends InjectorSOR<
simulator,
routeCachingProvider,
v2Supported,
v4Supported,
mixedSupported,
v4PoolParams,
cachedRoutesCacheInvalidationFixRolloutPercentage,
} = dependencies[chainIdEnum]!

let onChainQuoteProvider = dependencies[chainIdEnum]!.onChainQuoteProvider
Expand Down Expand Up @@ -139,10 +135,6 @@ export class QuoteHandlerInjector extends InjectorSOR<
tokenValidatorProvider,
tokenPropertiesProvider,
v2Supported,
v4Supported,
mixedSupported,
v4PoolParams,
cachedRoutesCacheInvalidationFixRolloutPercentage,
})
break
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
CachedRoutes,
CacheMode,
ID_TO_NETWORK_NAME,
INTENT,
IRouteCachingProvider,
log,
metric,
Expand All @@ -17,7 +16,6 @@ import { Protocol } from '@uniswap/router-sdk'
import { PairTradeTypeChainId } from './model/pair-trade-type-chain-id'
import { CachedRoutesMarshaller } from '../../marshalling/cached-routes-marshaller'
import { PromiseResult } from 'aws-sdk/lib/request'
import { DEFAULT_BLOCKS_TO_LIVE_ROUTES_DB } from '../../../util/defaultBlocksToLiveRoutesDB'

interface ConstructorParams {
/**
Expand Down Expand Up @@ -45,6 +43,43 @@ export class DynamoRouteCachingProvider extends IRouteCachingProvider {
private readonly ROUTES_DB_TTL = 24 * 60 * 60 // 24 hours
private readonly ROUTES_DB_FLAG_TTL = 2 * 60 // 2 minutes

// heuristic is within 30 seconds we find a route.
// we know each chain block time
// divide those two
private readonly DEFAULT_BLOCKS_TO_LIVE_ROUTES_DB = (chainId: ChainId) => {
switch (chainId) {
// https://dune.com/queries/2138021
case ChainId.ARBITRUM_ONE:
return 100

// https://dune.com/queries/2009572
case ChainId.BASE:
case ChainId.OPTIMISM:
return 60

// https://snowtrace.io/chart/blocktime
case ChainId.AVALANCHE:
return 15

// https://dune.com/KARTOD/blockchains-analysis
case ChainId.BNB:
return 10

// https://dune.com/KARTOD/blockchains-analysis
case ChainId.POLYGON:
return 15

// https://explorer.celo.org/mainnet/
case ChainId.CELO:
return 6

// https://dune.com/KARTOD/blockchains-analysis
case ChainId.MAINNET:
default:
return 2
}
}
// For the Ratio we are approximating Phi (Golden Ratio) by creating a fraction with 2 consecutive Fibonacci numbers
private readonly ROUTES_DB_BUCKET_RATIO: Fraction = new Fraction(514229, 317811)
private readonly ROUTES_TO_TAKE_FROM_ROUTES_DB = 8
private readonly BLOCKS_DIFF_BETWEEN_CACHING_QUOTES: Map<ChainId, number> = new Map([[ChainId.MAINNET, 3]])
Expand Down Expand Up @@ -79,7 +114,7 @@ export class DynamoRouteCachingProvider extends IRouteCachingProvider {
* @protected
*/
protected async _getBlocksToLive(cachedRoutes: CachedRoutes, _: CurrencyAmount<Currency>): Promise<number> {
return DEFAULT_BLOCKS_TO_LIVE_ROUTES_DB[cachedRoutes.chainId]
return this.DEFAULT_BLOCKS_TO_LIVE_ROUTES_DB(cachedRoutes.chainId)
}

/**
Expand Down Expand Up @@ -307,7 +342,7 @@ export class DynamoRouteCachingProvider extends IRouteCachingProvider {
amount: amount.quotient.toString(),
type: partitionKey.tradeType === 0 ? 'exactIn' : 'exactOut',
protocols: protocols.map((protocol) => protocol.toLowerCase()).join(','),
intent: INTENT.CACHING,
intent: 'caching',
requestSource: 'routing-api',
},
}
Expand Down Expand Up @@ -440,10 +475,14 @@ export class DynamoRouteCachingProvider extends IRouteCachingProvider {
_blockNumber: number,
_optimistic: boolean
): CachedRoutes | undefined {
// we can revert it back to never filter expired cached routes in read path
// we will use blocks-to-live in the write cached routes path
// also we can verify that cached routes cache invalidation bug error is gone in sepolia, as we removed hardcoding for sepolia
return cachedRoutes
// if it's on sepolia, then we want to filter expired routes by blocks to live
// this is to unblock v4 routing tests on sepolia
if (cachedRoutes?.chainId === ChainId.SEPOLIA) {
return cachedRoutes?.notExpired(_blockNumber, _optimistic) ? cachedRoutes : undefined
} else {
// otherwise, we keep it here, but we need a better plan for how to fix filtering expired cached routes
return cachedRoutes
}
}

/**
Expand Down
8 changes: 1 addition & 7 deletions lib/handlers/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { ChainId, Currency, CurrencyAmount, Percent } from '@uniswap/sdk-core'
import {
AlphaRouterConfig,
CacheMode,
INTENT,
LowerCaseStringArray,
MapWithLowerCaseKey,
ProtocolPoolSelection,
Expand Down Expand Up @@ -239,7 +238,6 @@ export const QUOTE_SPEED_CONFIG: { [key: string]: QuoteSpeedConfig } = {
}

export type IntentSpecificConfig = {
intent?: INTENT
useCachedRoutes?: boolean
overwriteCacheMode?: CacheMode
optimisticCachedRoutes?: boolean
Expand All @@ -248,27 +246,23 @@ export type IntentSpecificConfig = {
export const INTENT_SPECIFIC_CONFIG: { [key: string]: IntentSpecificConfig } = {
caching: {
// When the intent is to create a cache entry, we will use cachedRoutes with Tapcompare to track accuracy
intent: INTENT.CACHING,
useCachedRoutes: true,
// overwriteCacheMode: CacheMode.Tapcompare,
overwriteCacheMode: CacheMode.Tapcompare,
// This optimistic=false is *super* important to avoid an infinite loop of caching quotes calling themselves
optimisticCachedRoutes: false,
},
quote: {
// When the intent is to get a quote, we should use the cache and optimistic cached routes
intent: INTENT.QUOTE,
useCachedRoutes: true,
optimisticCachedRoutes: true,
},
swap: {
// When the intent is to prepare the swap, we can use cache, but it should not be optimistic
intent: INTENT.SWAP,
useCachedRoutes: true,
optimisticCachedRoutes: false,
},
pricing: {
// When the intent is to get pricing, we should use the cache and optimistic cached routes
intent: INTENT.PRICING,
useCachedRoutes: true,
optimisticCachedRoutes: true,
},
Expand Down
48 changes: 0 additions & 48 deletions lib/util/defaultBlocksToLiveRoutesDB.ts

This file was deleted.

34 changes: 0 additions & 34 deletions lib/util/newCachedRoutesRolloutPercent.ts

This file was deleted.

32 changes: 16 additions & 16 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@
"@uniswap/default-token-list": "^11.13.0",
"@uniswap/permit2-sdk": "^1.3.0",
"@uniswap/router-sdk": "^1.14.0",
"@uniswap/sdk-core": "^5.9.0",
"@uniswap/sdk-core": "^5.8.5",
"@types/semver": "^7.5.8",
"@uniswap/smart-order-router": "4.7.5",
"@uniswap/smart-order-router": "4.7.3",
"@uniswap/token-lists": "^1.0.0-beta.33",
"@uniswap/universal-router-sdk": "^4.6.1",
"@uniswap/v2-sdk": "^4.6.1",
Expand Down
Loading