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

Add additional signed data checks #168

Closed
wants to merge 4 commits into from
Closed
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
74 changes: 73 additions & 1 deletion packages/api/src/handlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,84 @@ describe(batchInsertData.name, () => {
statusCode: 201,
});
expect(logger.debug).toHaveBeenCalledWith(
'Not storing signed data because signed data with the same timestamp already exists.',
'Not storing signed data because the signed data is not newer than what we already have.',
expect.any(Object)
);
expect(cacheModule.getCache()[storedSignedData.airnode]![storedSignedData.templateId]!).toHaveLength(1);
});

it('skips signed data if newer data is available from the store', async () => {
const airnodeWallet = generateRandomWallet();
const storedSignedData = await createSignedData({ airnodeWallet });
cacheModule.setCache({
[storedSignedData.airnode]: {
[storedSignedData.templateId]: [storedSignedData],
},
});
const batchData = [
await createSignedData({
airnodeWallet,
templateId: storedSignedData.templateId,
timestamp: (Number.parseInt(storedSignedData.timestamp, 10) - 10).toString(),
}),
await createSignedData(),
];
jest.spyOn(logger, 'debug');

const result = await batchInsertData(batchData);

expect(result).toStrictEqual({
body: JSON.stringify({ count: 1, skipped: 1 }),
headers: {
'access-control-allow-methods': '*',
'access-control-allow-origin': '*',
'content-type': 'application/json',
},
statusCode: 201,
});
expect(logger.debug).toHaveBeenCalledWith(
'Not storing signed data because the signed data is not newer than what we already have.',
expect.any(Object)
);
expect(cacheModule.getCache()[storedSignedData.airnode]![storedSignedData.templateId]!).toHaveLength(1);
});

it('skips signed data if the data is older than an hour', async () => {
const airnodeWallet = generateRandomWallet();
const storedSignedData = await createSignedData({ airnodeWallet });
cacheModule.setCache({
[storedSignedData.airnode]: {
[storedSignedData.templateId]: [],
},
});
const batchData = [
await createSignedData({
airnodeWallet,
templateId: storedSignedData.templateId,
timestamp: (Number.parseInt(storedSignedData.timestamp, 10) - 60 * 61).toString(),
}),
await createSignedData(),
];
jest.spyOn(logger, 'debug');

const result = await batchInsertData(batchData);

expect(result).toStrictEqual({
body: JSON.stringify({ count: 1, skipped: 1 }),
headers: {
'access-control-allow-methods': '*',
'access-control-allow-origin': '*',
'content-type': 'application/json',
},
statusCode: 201,
});
expect(logger.debug).toHaveBeenCalledWith(
'Not storing signed data because the timestamp is older than one hour.',
expect.any(Object)
);
expect(cacheModule.getCache()[storedSignedData.airnode]![storedSignedData.templateId]!).toHaveLength(0);
});

it('rejects a batch if there is a beacon with timestamp too far in the future', async () => {
const batchData = [await createSignedData({ timestamp: (Math.floor(Date.now() / 1000) + 60 * 60 * 2).toString() })];

Expand Down
14 changes: 11 additions & 3 deletions packages/api/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,17 @@ export const batchInsertData = async (requestBody: unknown): Promise<ApiResponse
// is acceptable, but we only want to store one data for each timestamp.
for (const signedData of batchSignedData) {
const requestTimestamp = Number.parseInt(signedData.timestamp, 10);
const goReadDb = await go(async () => get(signedData.airnode, signedData.templateId, requestTimestamp));
if (goReadDb.data && requestTimestamp === Number.parseInt(goReadDb.data.timestamp, 10)) {
logger.debug('Not storing signed data because signed data with the same timestamp already exists.', {
const goReadDb = await go(async () => get(signedData.airnode, signedData.templateId));

if (goReadDb.data && requestTimestamp <= Number.parseInt(goReadDb.data.timestamp, 10)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to store even older data we have because it might be served by other (delayed) endpoint. To address the issue we would need to check whether the signed data will be pruned, but checking that is more complex than I initially thought (and probably not worth relative to the added benefit).

logger.debug('Not storing signed data because the signed data is not newer than what we already have.', {
signedData,
});
continue;
}

if (Date.now() / 1000 - requestTimestamp > 60 * 60) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mhh, what is wrong with >1h old signed data?

logger.debug('Not storing signed data because the timestamp is older than one hour.', {
signedData,
});
continue;
Expand Down
8 changes: 6 additions & 2 deletions packages/api/src/in-memory-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ export const ignoreTooFreshData = (signedDatas: SignedData[], ignoreAfterTimesta
signedDatas.filter((data) => !isIgnored(data, ignoreAfterTimestamp));

// The API is deliberately asynchronous to mimic a database call.
// eslint-disable-next-line @typescript-eslint/require-await
export const get = async (airnodeAddress: string, templateId: string, ignoreAfterTimestamp: number) => {
export const get = async (
airnodeAddress: string,
templateId: string,
ignoreAfterTimestamp = Number.MAX_SAFE_INTEGER
// eslint-disable-next-line @typescript-eslint/require-await
) => {
logger.debug('Getting signed data.', { airnodeAddress, templateId, ignoreAfterTimestamp });

const signedDataCache = getCache();
Expand Down