-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #228 from Uniswap/compliance-provider
feat: add filler compliance provider
- Loading branch information
Showing
9 changed files
with
247 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export interface FillerComplianceConfiguration { | ||
endpoints: string[]; | ||
addresses: string[]; | ||
} | ||
|
||
export interface FillerComplianceConfigurationProvider { | ||
getConfigs(): Promise<FillerComplianceConfiguration[]>; | ||
// getExcludedAddrToEndpointsMap(): Promise<Map<string, Set<string>>>; | ||
getEndpointToExcludedAddrsMap(): Promise<Map<string, Set<string>>>; | ||
} | ||
|
||
export * from './mock'; | ||
export * from './s3'; |
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,25 @@ | ||
import { FillerComplianceConfiguration, FillerComplianceConfigurationProvider } from '.'; | ||
|
||
export class MockFillerComplianceConfigurationProvider implements FillerComplianceConfigurationProvider { | ||
constructor(private configs: FillerComplianceConfiguration[]) {} | ||
|
||
async getConfigs(): Promise<FillerComplianceConfiguration[]> { | ||
return this.configs; | ||
} | ||
|
||
async getEndpointToExcludedAddrsMap(): Promise<Map<string, Set<string>>> { | ||
const map = new Map<string, Set<string>>(); | ||
this.configs.forEach((config) => { | ||
config.endpoints.forEach((endpoint) => { | ||
if (!map.has(endpoint)) { | ||
map.set(endpoint, new Set<string>()); | ||
} | ||
config.addresses.forEach((address) => { | ||
map.get(endpoint)?.add(address); | ||
}); | ||
}); | ||
}) | ||
return map; | ||
} | ||
|
||
} |
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,60 @@ | ||
|
||
import { GetObjectCommand, S3Client } from '@aws-sdk/client-s3'; | ||
import { default as Logger } from 'bunyan'; | ||
|
||
import { checkDefined } from '../../preconditions/preconditions'; | ||
import { FillerComplianceConfiguration, FillerComplianceConfigurationProvider } from '.'; | ||
|
||
|
||
export class S3FillerComplianceConfigurationProvider implements FillerComplianceConfigurationProvider { | ||
private log: Logger; | ||
private configs: FillerComplianceConfiguration[]; | ||
private endpointToExcludedAddrsMap: Map<string, Set<string>>; | ||
|
||
constructor(_log: Logger, private bucket: string, private key: string) { | ||
this.configs = []; | ||
this.log = _log.child({ quoter: 'S3FillerComplianceConfigurationProvider' }); | ||
this.endpointToExcludedAddrsMap = new Map<string, Set<string>>(); | ||
} | ||
async getEndpointToExcludedAddrsMap(): Promise<Map<string, Set<string>>> { | ||
if (this.configs.length === 0) { | ||
await this.fetchConfigs(); | ||
} | ||
if (this.endpointToExcludedAddrsMap.size > 0) { | ||
return this.endpointToExcludedAddrsMap; | ||
} | ||
this.configs.forEach((config) => { | ||
config.endpoints.forEach((endpoint) => { | ||
if (!this.endpointToExcludedAddrsMap.has(endpoint)) { | ||
this.endpointToExcludedAddrsMap.set(endpoint, new Set<string>()); | ||
} | ||
config.addresses.forEach((address) => { | ||
this.endpointToExcludedAddrsMap.get(endpoint)?.add(address); | ||
}); | ||
}); | ||
}) | ||
return this.endpointToExcludedAddrsMap; | ||
} | ||
|
||
async getConfigs(): Promise<FillerComplianceConfiguration[]> { | ||
if ( | ||
this.configs.length === 0 | ||
) { | ||
await this.fetchConfigs(); | ||
} | ||
return this.configs; | ||
} | ||
|
||
async fetchConfigs(): Promise<void> { | ||
const s3Client = new S3Client({}); | ||
const s3Res = await s3Client.send( | ||
new GetObjectCommand({ | ||
Bucket: this.bucket, | ||
Key: this.key, | ||
}) | ||
); | ||
const s3Body = checkDefined(s3Res.Body, 's3Res.Body is undefined'); | ||
this.configs = JSON.parse(await s3Body.transformToString()) as FillerComplianceConfiguration[]; | ||
this.log.info({ configsLength: this.configs.map((c) => c.addresses.length) }, `Fetched configs`); | ||
} | ||
} |
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,59 @@ | ||
import { S3Client } from '@aws-sdk/client-s3'; | ||
import { default as Logger } from 'bunyan'; | ||
|
||
import { FillerComplianceConfiguration,S3FillerComplianceConfigurationProvider } from '../../../lib/providers/compliance'; | ||
|
||
const mockConfigs = [ | ||
{ | ||
endpoints: ['https://google.com'], | ||
addresses: ['0x1234'], | ||
}, | ||
{ | ||
endpoints: ['https://meta.com'], | ||
addresses: ['0x1234', '0x5678'], | ||
}, | ||
]; | ||
|
||
|
||
function applyMock(configs: FillerComplianceConfiguration[]) { | ||
jest.spyOn(S3Client.prototype, 'send').mockImplementationOnce(() => | ||
Promise.resolve({ | ||
Body: { | ||
transformToString: () => Promise.resolve(JSON.stringify(configs)), | ||
}, | ||
}) | ||
); | ||
} | ||
|
||
|
||
// silent logger in tests | ||
const logger = Logger.createLogger({ name: 'test' }); | ||
logger.level(Logger.FATAL); | ||
|
||
describe('S3ComplianceConfigurationProvider', () => { | ||
const bucket = 'test-bucket'; | ||
const key = 'test-key'; | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('fetches configs', async () => { | ||
applyMock(mockConfigs); | ||
const provider = new S3FillerComplianceConfigurationProvider(logger, bucket, key); | ||
const endpoints = await provider.getConfigs(); | ||
expect(endpoints).toEqual(mockConfigs); | ||
}); | ||
|
||
it('generates endpoint to addrs map', async () => { | ||
applyMock(mockConfigs); | ||
const provider = new S3FillerComplianceConfigurationProvider(logger, bucket, key); | ||
const map = await provider.getEndpointToExcludedAddrsMap(); | ||
expect(map).toMatchObject( | ||
new Map([ | ||
['https://google.com', new Set(['0x1234'])], | ||
['https://meta.com', new Set(['0x1234', '0x5678'])], | ||
]) | ||
) | ||
}); | ||
}); |
Oops, something went wrong.