From c5eeae7a9715a11153eb739bf9ff2a36fd380e70 Mon Sep 17 00:00:00 2001 From: ConjunctiveNormalForm Date: Fri, 7 Jun 2024 15:14:59 -0400 Subject: [PATCH 1/4] fix cb-provider-v2 to use endpoint instead of hash --- lib/handlers/hard-quote/injector.ts | 5 ++- lib/providers/circuit-breaker/dynamo.ts | 20 ++++++++---- lib/providers/circuit-breaker/mock.ts | 2 +- .../circuit-breaker/cb-provider.test.ts | 32 +++++++++---------- 4 files changed, 34 insertions(+), 25 deletions(-) diff --git a/lib/handlers/hard-quote/injector.ts b/lib/handlers/hard-quote/injector.ts index 8045b3c..b03649f 100644 --- a/lib/handlers/hard-quote/injector.ts +++ b/lib/handlers/hard-quote/injector.ts @@ -56,7 +56,10 @@ export class QuoteInjector extends ApiInjector { if ( - this.fillerHashes.length === 0 || + this.fillerEndpoints.length === 0 || Date.now() - this.lastUpdatedTimestamp > DynamoCircuitBreakerConfigurationProvider.UPDATE_PERIOD_MS ) { await this.fetchConfigurations(); @@ -44,7 +44,7 @@ export class DynamoCircuitBreakerConfigurationProvider implements CircuitBreaker } async fetchConfigurations(): Promise { - 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 */ @@ -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 }, `Endpoint enabled`); + this.log.info({ num: disabledEndpoints.length, endpoints: disabledEndpoints }, `Endpoint disabled`); + return enabledEndpoints; } diff --git a/lib/providers/circuit-breaker/mock.ts b/lib/providers/circuit-breaker/mock.ts index 7f6aa91..1c6f8e8 100644 --- a/lib/providers/circuit-breaker/mock.ts +++ b/lib/providers/circuit-breaker/mock.ts @@ -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; } diff --git a/test/providers/circuit-breaker/cb-provider.test.ts b/test/providers/circuit-breaker/cb-provider.test.ts index 4a19079..cbc4d81 100644 --- a/test/providers/circuit-breaker/cb-provider.test.ts +++ b/test/providers/circuit-breaker/cb-provider.test.ts @@ -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', }, ]; @@ -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', }, ]); }); From 30c6d49b13ea377bfdfa4b2c82f89ba195d64724 Mon Sep 17 00:00:00 2001 From: ConjunctiveNormalForm Date: Fri, 7 Jun 2024 15:23:00 -0400 Subject: [PATCH 2/4] fix unit tests --- test/providers/quoters/WebhookQuoter.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/providers/quoters/WebhookQuoter.test.ts b/test/providers/quoters/WebhookQuoter.test.ts index 9e56309..7b11320 100644 --- a/test/providers/quoters/WebhookQuoter.test.ts +++ b/test/providers/quoters/WebhookQuoter.test.ts @@ -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( From d6fbb059cf14d2ec77e2d9dbde29e8dcc6f2698c Mon Sep 17 00:00:00 2001 From: ConjunctiveNormalForm Date: Fri, 7 Jun 2024 17:27:49 -0400 Subject: [PATCH 3/4] fix typo --- lib/providers/circuit-breaker/dynamo.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/providers/circuit-breaker/dynamo.ts b/lib/providers/circuit-breaker/dynamo.ts index efdcddb..e8cdd42 100644 --- a/lib/providers/circuit-breaker/dynamo.ts +++ b/lib/providers/circuit-breaker/dynamo.ts @@ -16,9 +16,9 @@ export class DynamoCircuitBreakerConfigurationProvider implements CircuitBreaker // try to refetch endpoints every 30 seconds private static UPDATE_PERIOD_MS = 1 * 30000; - constructor(_log: Logger, _fillerEdnpoints: string[] = []) { + constructor(_log: Logger, _fillerEndpoints: string[] = []) { this.log = _log.child({ quoter: 'CircuitBreakerConfigurationProvider' }); - this.fillerEndpoints = _fillerEdnpoints; + this.fillerEndpoints = _fillerEndpoints; this.lastUpdatedTimestamp = Date.now(); const documentClient = DynamoDBDocumentClient.from(new DynamoDBClient({}), { marshallOptions: { From 489d27030a4f7d86ed8ab57bda8dbe844653a851 Mon Sep 17 00:00:00 2001 From: ConjunctiveNormalForm Date: Fri, 7 Jun 2024 17:41:31 -0400 Subject: [PATCH 4/4] typo --- lib/providers/circuit-breaker/dynamo.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/providers/circuit-breaker/dynamo.ts b/lib/providers/circuit-breaker/dynamo.ts index e8cdd42..c7439db 100644 --- a/lib/providers/circuit-breaker/dynamo.ts +++ b/lib/providers/circuit-breaker/dynamo.ts @@ -61,8 +61,8 @@ export class DynamoCircuitBreakerConfigurationProvider implements CircuitBreaker return fillerTimestamps.has(e.endpoint) && fillerTimestamps.get(e.endpoint)!.blockUntilTimestamp > now; }); - this.log.info({ num: enabledEndpoints.length, endpoints: enabledEndpoints }, `Endpoint enabled`); - this.log.info({ num: disabledEndpoints.length, endpoints: disabledEndpoints }, `Endpoint disabled`); + this.log.info({ num: enabledEndpoints.length, endpoints: enabledEndpoints }, `Endpoints enabled`); + this.log.info({ num: disabledEndpoints.length, endpoints: disabledEndpoints }, `Endpoints disabled`); return enabledEndpoints; }