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

fix: use plaintext routes column #904

Merged
merged 3 commits into from
Nov 7, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,24 @@ export class DynamoRouteCachingProvider extends IRouteCachingProvider {
): CachedRoutes {
metric.putMetric(`RoutesDbEntriesFound`, result.Items!.length, MetricLoggerUnit.Count)
const cachedRoutesArr: CachedRoutes[] = result.Items!.map((record) => {
// If we got a response with more than 1 item, we extract the binary field from the response
const itemBinary = record.item
// Then we convert it into a Buffer
const cachedRoutesBuffer = Buffer.from(itemBinary)
// We convert that buffer into string and parse as JSON (it was encoded as JSON when it was inserted into cache)
const cachedRoutesJson = JSON.parse(cachedRoutesBuffer.toString())
// Finally we unmarshal that JSON into a `CachedRoutes` object
return CachedRoutesMarshaller.unmarshal(cachedRoutesJson)
if (record.plainRoutes && record.plainRoutes?.toString().trim() !== '') {
metric.putMetric(`RoutesDbEntryPlainTextRouteFound`, 1, MetricLoggerUnit.Count)

const cachedRoutesJson = JSON.parse(record.plainRoutes)
return CachedRoutesMarshaller.unmarshal(cachedRoutesJson)
} else {
// Once this metric drops to zero, then we can stop writing binaryCachedRoutes into the item column
metric.putMetric(`RoutesDbEntrySerializedRouteFound`, 1, MetricLoggerUnit.Count)

// If we got a response with more than 1 item, we extract the binary field from the response
const itemBinary = record.item
// Then we convert it into a Buffer
const cachedRoutesBuffer = Buffer.from(itemBinary)
// We convert that buffer into string and parse as JSON (it was encoded as JSON when it was inserted into cache)
const cachedRoutesJson = JSON.parse(cachedRoutesBuffer.toString())
// Finally we unmarshal that JSON into a `CachedRoutes` object
return CachedRoutesMarshaller.unmarshal(cachedRoutesJson)
}
})

const routesMap: Map<string, CachedRoute<SupportedRoutes>> = new Map()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import chai, { expect } from 'chai'
import chaiAsPromised from 'chai-as-promised'
import 'reflect-metadata'
import { setupTables } from '../../../../dbSetup'
import { DynamoRouteCachingProvider } from '../../../../../../lib/handlers/router-entities/route-caching'
import {
DynamoRouteCachingProvider,
PairTradeTypeChainId,
} from '../../../../../../lib/handlers/router-entities/route-caching'
import { ADDRESS_ZERO, Protocol } from '@uniswap/router-sdk'
import { ChainId, CurrencyAmount, Ether, TradeType } from '@uniswap/sdk-core'
import JSBI from 'jsbi'
Expand All @@ -24,6 +27,7 @@ import { V4Route } from '@uniswap/smart-order-router/build/main/routers'
import { NEW_CACHED_ROUTES_ROLLOUT_PERCENT } from '../../../../../../lib/util/newCachedRoutesRolloutPercent'
import sinon, { SinonSpy } from 'sinon'
import { metric } from '@uniswap/smart-order-router/build/main/util/metric'
import { DynamoDB } from 'aws-sdk'

chai.use(chaiAsPromised)

Expand Down Expand Up @@ -175,6 +179,8 @@ describe('DynamoRouteCachingProvider', async () => {

it('Cached routes hits new cached routes lambda', async () => {
spy.withArgs('CachingQuoteForRoutesDbRequestSentToLambdanewcachinglambda', 1, MetricLoggerUnit.Count)
spy.withArgs('RoutesDbEntryPlainTextRouteFound', 1, MetricLoggerUnit.Count)
spy.withArgs('RoutesDbEntrySerializedRouteFound', 1, MetricLoggerUnit.Count)

// testnet rolls out at 100%
const newCachedRoutesRolloutPercent = NEW_CACHED_ROUTES_ROLLOUT_PERCENT[ChainId.SEPOLIA]
Expand Down Expand Up @@ -210,6 +216,62 @@ describe('DynamoRouteCachingProvider', async () => {
)
expect(route).to.not.be.undefined

const queryParams = {
TableName: DynamoDBTableProps.RoutesDbTable.Name,
KeyConditionExpression: '#pk = :pk',
ExpressionAttributeNames: {
'#pk': 'pairTradeTypeChainId',
},
ExpressionAttributeValues: {
':pk': PairTradeTypeChainId.fromCachedRoutes(TEST_CACHED_ROUTES).toString(),
},
}
const cachedRoutes = await new DynamoDB.DocumentClient({
maxRetries: 1,
retryDelayOptions: {
base: 20,
},
httpOptions: {
timeout: 100,
},
})
.query(queryParams)
.promise()

cachedRoutes.Items?.forEach(async (item) => {
expect(item).to.not.be.undefined
// We nullify the plainRoutes column and update the Item in-place in the table,
// so that we make sure when we get cached routes again, we will hit the serialized route path.
item.plainRoutes = undefined

const putRequest = {
TableName: DynamoDBTableProps.RoutesDbTable.Name,
Item: item,
}

await new DynamoDB.DocumentClient({
maxRetries: 1,
retryDelayOptions: {
base: 20,
},
httpOptions: {
timeout: 100,
},
})
.put(putRequest)
.promise()
})

const updatedRoute = await dynamoRouteCache.getCachedRoute(
ChainId.MAINNET,
currencyAmount,
USDC_MAINNET,
TradeType.EXACT_INPUT,
[Protocol.V3],
TEST_CACHED_ROUTES.blockNumber
)
expect(updatedRoute).to.not.be.undefined

sinon.assert.called(spy)
})

Expand Down
Loading