Skip to content

Commit

Permalink
Merge pull request #338 from Uniswap/fix-cb-v2
Browse files Browse the repository at this point in the history
fix:CB provider to use endpoint instead of hashes
  • Loading branch information
ConjunctiveNormalForm authored Jun 7, 2024
2 parents a4d814f + 489d270 commit d799819
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 28 deletions.
5 changes: 4 additions & 1 deletion lib/handlers/hard-quote/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ export class QuoteInjector extends ApiInjector<ContainerInjected, RequestInjecte

const webhookProvider = new S3WebhookConfigurationProvider(log, `${WEBHOOK_CONFIG_BUCKET}-${stage}-1`, s3Key);
await webhookProvider.fetchEndpoints();
const circuitBreakerProvider = new DynamoCircuitBreakerConfigurationProvider(log, webhookProvider.fillers());
const circuitBreakerProvider = new DynamoCircuitBreakerConfigurationProvider(
log,
webhookProvider.fillerEndpoints()
);

const orderServiceProvider = new UniswapXServiceProvider(log, orderServiceUrl);

Expand Down
20 changes: 13 additions & 7 deletions lib/providers/circuit-breaker/dynamo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import { WebhookConfiguration } from '../webhook';

export class DynamoCircuitBreakerConfigurationProvider implements CircuitBreakerConfigurationProvider {
private log: Logger;
private fillerHashes: string[];
private fillerEndpoints: string[];
private lastUpdatedTimestamp: number;
private timestampDB: BaseTimestampRepository;
private timestamps: FillerTimestampMap = new Map();

// try to refetch endpoints every 30 seconds
private static UPDATE_PERIOD_MS = 1 * 30000;

constructor(_log: Logger, _fillerHashes: string[] = []) {
constructor(_log: Logger, _fillerEndpoints: string[] = []) {
this.log = _log.child({ quoter: 'CircuitBreakerConfigurationProvider' });
this.fillerHashes = _fillerHashes;
this.fillerEndpoints = _fillerEndpoints;
this.lastUpdatedTimestamp = Date.now();
const documentClient = DynamoDBDocumentClient.from(new DynamoDBClient({}), {
marshallOptions: {
Expand All @@ -33,7 +33,7 @@ export class DynamoCircuitBreakerConfigurationProvider implements CircuitBreaker

async getConfigurations(): Promise<FillerTimestampMap> {
if (
this.fillerHashes.length === 0 ||
this.fillerEndpoints.length === 0 ||
Date.now() - this.lastUpdatedTimestamp > DynamoCircuitBreakerConfigurationProvider.UPDATE_PERIOD_MS
) {
await this.fetchConfigurations();
Expand All @@ -44,7 +44,7 @@ export class DynamoCircuitBreakerConfigurationProvider implements CircuitBreaker
}

async fetchConfigurations(): Promise<void> {
this.timestamps = await this.timestampDB.getFillerTimestampsMap(this.fillerHashes);
this.timestamps = await this.timestampDB.getFillerTimestampsMap(this.fillerEndpoints);
}

/* add filler if it's not blocked until a future timestamp */
Expand All @@ -55,9 +55,15 @@ export class DynamoCircuitBreakerConfigurationProvider implements CircuitBreaker
if (fillerTimestamps.size) {
this.log.info({ fillerTimestamps: [...fillerTimestamps.entries()] }, `Circuit breaker config used`);
const enabledEndpoints = endpoints.filter((e) => {
return !(fillerTimestamps.has(e.hash) && fillerTimestamps.get(e.hash)!.blockUntilTimestamp > now);
return !(fillerTimestamps.has(e.endpoint) && fillerTimestamps.get(e.endpoint)!.blockUntilTimestamp > now);
});
this.log.info({ endpoints: enabledEndpoints }, `Endpoint enabled`);
const disabledEndpoints = endpoints.filter((e) => {
return fillerTimestamps.has(e.endpoint) && fillerTimestamps.get(e.endpoint)!.blockUntilTimestamp > now;
});

this.log.info({ num: enabledEndpoints.length, endpoints: enabledEndpoints }, `Endpoints enabled`);
this.log.info({ num: disabledEndpoints.length, endpoints: disabledEndpoints }, `Endpoints disabled`);

return enabledEndpoints;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/providers/circuit-breaker/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class MockV2CircuitBreakerConfigurationProvider implements CircuitBreaker
const fillerTimestamps = await this.getConfigurations();
if (fillerTimestamps.size) {
const enabledEndpoints = endpoints.filter((e) => {
return !(fillerTimestamps.has(e.hash) && fillerTimestamps.get(e.hash)!.blockUntilTimestamp > now);
return !(fillerTimestamps.has(e.endpoint) && fillerTimestamps.get(e.endpoint)!.blockUntilTimestamp > now);
});
return enabledEndpoints;
}
Expand Down
32 changes: 16 additions & 16 deletions test/providers/circuit-breaker/cb-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,28 @@ const FILLER_TIMESTAMPS: FillerTimestamps = new Map([
const WEBHOOK_CONFIGS = [
{
name: 'f1',
endpoint: 'http://localhost:3000',
hash: 'filler1',
endpoint: 'filler1',
hash: '0xfiller1',
},
{
name: 'f2',
endpoint: 'http://localhost:3000',
hash: 'filler2',
endpoint: 'filler2',
hash: '0xfiller2',
},
{
name: 'f3',
endpoint: 'http://localhost:3000',
hash: 'filler3',
endpoint: 'filler3',
hash: '0xfiller3',
},
{
name: 'f4',
endpoint: 'http://localhost:3000',
hash: 'filler4',
endpoint: 'filler4',
hash: '0xfiller4',
},
{
name: 'f5',
endpoint: 'http://localhost:3000',
hash: 'filler5',
endpoint: 'filler5',
hash: '0xfiller5',
},
];

Expand All @@ -46,18 +46,18 @@ describe('V2CircuitBreakerProvider', () => {
expect(await provider.getEligibleEndpoints(WEBHOOK_CONFIGS)).toEqual([
{
name: 'f1',
endpoint: 'http://localhost:3000',
hash: 'filler1',
endpoint: 'filler1',
hash: '0xfiller1',
},
{
name: 'f2',
endpoint: 'http://localhost:3000',
hash: 'filler2',
endpoint: 'filler2',
hash: '0xfiller2',
},
{
name: 'f4',
endpoint: 'http://localhost:3000',
hash: 'filler4',
endpoint: 'filler4',
hash: '0xfiller4',
},
]);
});
Expand Down
6 changes: 3 additions & 3 deletions test/providers/quoters/WebhookQuoter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,10 @@ describe('WebhookQuoter tests', () => {
describe('circuit breaker v2 tests', () => {
const now = Math.floor(Date.now() / 1000);
const mockCBProvider = new MockV2CircuitBreakerConfigurationProvider(
['0xuni', '0x1inch', '0xsearcher'],
[WEBHOOK_URL, WEBHOOK_URL_ONEINCH, WEBHOOK_URL_SEARCHER],
new Map([
['0x1inch', { blockUntilTimestamp: now + 100000, lastPostTimestamp: now - 10 }],
['0xsearcher', { blockUntilTimestamp: now - 10, lastPostTimestamp: now - 100 }],
[WEBHOOK_URL_ONEINCH, { blockUntilTimestamp: now + 100000, lastPostTimestamp: now - 10 }],
[WEBHOOK_URL_SEARCHER, { blockUntilTimestamp: now - 10, lastPostTimestamp: now - 100 }],
])
);
const webhookQuoter = new WebhookQuoter(
Expand Down

0 comments on commit d799819

Please sign in to comment.