-
Notifications
You must be signed in to change notification settings - Fork 8
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
Allow API to serve delayed data #34
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
43f4653
Save all beacon signed data, not just the freshest
Siegrift 3e4a012
Implement delayed endpoint
Siegrift 9a00579
Remove .DS_Store
Siegrift 45d1134
Improve error messages
Siegrift 64e0445
Use cache value from config
Siegrift f9b0c40
Implement in-memory-api tests
Siegrift 16a7a39
Add better error messages
Siegrift 349eaca
Verify that express json body parser works
Siegrift 2ad4b6f
Fix docker compose, edit documentation
Siegrift 2c4e32f
Remove unused dependency
Siegrift b424638
Make each endpoint unique, document configuration
Siegrift e1c1560
Fix tests
Siegrift File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,5 @@ | |
**/coverage | ||
**/pusher.json | ||
**/secrets.env | ||
**/signed-api.json | ||
**/.DS_Store |
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 |
---|---|---|
|
@@ -11,3 +11,5 @@ node_modules | |
coverage | ||
pusher.json | ||
secrets.env | ||
signed-api.json | ||
.DS_Store |
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 was deleted.
Oops, something went wrong.
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,17 @@ | ||
{ | ||
"endpoints": [ | ||
{ | ||
"urlPath": "/real-time", | ||
"delaySeconds": 0 | ||
}, | ||
{ | ||
"urlPath": "/delayed", | ||
"delaySeconds": 15 | ||
} | ||
], | ||
"maxBatchSize": 10, | ||
"port": 8090, | ||
"cache": { | ||
"maxAgeSeconds": 300 | ||
} | ||
} |
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,18 @@ | ||
import { SignedData } from './schema'; | ||
|
||
type SignedDataCache = Record< | ||
string, // Airnode ID. | ||
Record< | ||
string, // Template ID. | ||
SignedData[] // Signed data is ordered by timestamp (oldest first). | ||
> | ||
>; | ||
|
||
let signedDataCache: SignedDataCache = {}; | ||
|
||
// Making this a getter function makes it easier to mock the cache in storage. | ||
export const getCache = () => signedDataCache; | ||
|
||
export const setCache = (cache: SignedDataCache) => { | ||
signedDataCache = cache; | ||
}; |
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,165 @@ | ||
import { readFileSync } from 'fs'; | ||
import { join } from 'path'; | ||
import { omit } from 'lodash'; | ||
import * as cacheModule from './cache'; | ||
import * as utilsModule from './utils'; | ||
import { batchInsertData, getData, listAirnodeAddresses } from './handlers'; | ||
import { createSignedData, generateRandomWallet } from '../test/utils'; | ||
|
||
afterEach(() => { | ||
cacheModule.setCache({}); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest | ||
.spyOn(utilsModule, 'getConfig') | ||
.mockImplementation(() => JSON.parse(readFileSync(join(__dirname, '../config/signed-api.example.json'), 'utf8'))); | ||
}); | ||
|
||
describe(batchInsertData.name, () => { | ||
it('drops the batch if it is invalid', async () => { | ||
const invalidData = await createSignedData({ signature: '0xInvalid' }); | ||
const batchData = [await createSignedData(), invalidData]; | ||
|
||
const result = await batchInsertData(batchData); | ||
|
||
expect(result).toEqual({ | ||
body: JSON.stringify({ | ||
message: 'Unable to recover signer address', | ||
detail: | ||
'signature missing v and recoveryParam (argument="signature", value="0xInvalid", code=INVALID_ARGUMENT, version=bytes/5.7.0)', | ||
extra: invalidData, | ||
}), | ||
headers: { | ||
'access-control-allow-methods': '*', | ||
'access-control-allow-origin': '*', | ||
'content-type': 'application/json', | ||
}, | ||
statusCode: 400, | ||
}); | ||
expect(cacheModule.getCache()).toEqual({}); | ||
}); | ||
|
||
it('inserts the batch if data is valid', async () => { | ||
const batchData = [await createSignedData(), await createSignedData()]; | ||
|
||
const result = await batchInsertData(batchData); | ||
|
||
expect(result).toEqual({ | ||
body: JSON.stringify({ count: 2 }), | ||
headers: { | ||
'access-control-allow-methods': '*', | ||
'access-control-allow-origin': '*', | ||
'content-type': 'application/json', | ||
}, | ||
statusCode: 201, | ||
}); | ||
expect(cacheModule.getCache()).toEqual({ | ||
[batchData[0]!.airnode]: { | ||
[batchData[0]!.templateId]: [batchData[0]], | ||
}, | ||
[batchData[1]!.airnode]: { | ||
[batchData[1]!.templateId]: [batchData[1]], | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
describe(getData.name, () => { | ||
it('drops the request if the airnode address is invalid', async () => { | ||
const batchData = [await createSignedData(), await createSignedData()]; | ||
await batchInsertData(batchData); | ||
|
||
const result = await getData('0xInvalid', 0); | ||
|
||
expect(result).toEqual({ | ||
body: JSON.stringify({ message: 'Invalid request, airnode address must be an EVM address' }), | ||
headers: { | ||
'access-control-allow-methods': '*', | ||
'access-control-allow-origin': '*', | ||
'content-type': 'application/json', | ||
}, | ||
statusCode: 400, | ||
}); | ||
}); | ||
|
||
it('returns the live data', async () => { | ||
const airnodeWallet = generateRandomWallet(); | ||
const batchData = [await createSignedData({ airnodeWallet }), await createSignedData({ airnodeWallet })]; | ||
await batchInsertData(batchData); | ||
|
||
const result = await getData(airnodeWallet.address, 0); | ||
|
||
expect(result).toEqual({ | ||
body: JSON.stringify({ | ||
count: 2, | ||
data: { | ||
[batchData[0]!.beaconId]: omit(batchData[0], 'beaconId'), | ||
[batchData[1]!.beaconId]: omit(batchData[1], 'beaconId'), | ||
}, | ||
}), | ||
headers: { | ||
'access-control-allow-methods': '*', | ||
'access-control-allow-origin': '*', | ||
'cache-control': 'no-store', | ||
'cdn-cache-control': 'max-age=10', | ||
'content-type': 'application/json', | ||
}, | ||
statusCode: 200, | ||
}); | ||
}); | ||
|
||
it('returns the delayed data', async () => { | ||
const airnodeWallet = generateRandomWallet(); | ||
const delayTimestamp = (Math.floor(Date.now() / 1000) - 60).toString(); // Delayed by 60 seconds | ||
const batchData = [ | ||
await createSignedData({ airnodeWallet, timestamp: delayTimestamp }), | ||
await createSignedData({ airnodeWallet }), | ||
]; | ||
await batchInsertData(batchData); | ||
|
||
const result = await getData(airnodeWallet.address, 30); | ||
|
||
expect(result).toEqual({ | ||
body: JSON.stringify({ | ||
count: 1, | ||
data: { | ||
[batchData[0]!.beaconId]: omit(batchData[0], 'beaconId'), | ||
}, | ||
}), | ||
headers: { | ||
'access-control-allow-methods': '*', | ||
'access-control-allow-origin': '*', | ||
'cache-control': 'no-store', | ||
'cdn-cache-control': 'max-age=10', | ||
'content-type': 'application/json', | ||
}, | ||
statusCode: 200, | ||
}); | ||
}); | ||
}); | ||
|
||
describe(listAirnodeAddresses.name, () => { | ||
it('returns the list of airnode addresses', async () => { | ||
const airnodeWallet = generateRandomWallet(); | ||
const batchData = [await createSignedData({ airnodeWallet }), await createSignedData({ airnodeWallet })]; | ||
await batchInsertData(batchData); | ||
|
||
const result = await listAirnodeAddresses(); | ||
|
||
expect(result).toEqual({ | ||
body: JSON.stringify({ | ||
count: 1, | ||
'available-airnodes': [airnodeWallet.address], | ||
}), | ||
headers: { | ||
'access-control-allow-methods': '*', | ||
'access-control-allow-origin': '*', | ||
'cache-control': 'no-store', | ||
'cdn-cache-control': 'max-age=300', | ||
'content-type': 'application/json', | ||
}, | ||
statusCode: 200, | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏻