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

Save log index #1027

Merged
merged 8 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
5 changes: 5 additions & 0 deletions .changeset/many-zebras-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@api3/airnode-node': minor
---

Add request sorting by logIndex
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('aggregate (API calls)', () => {
currentBlock: 10716090,
minConfirmations: 0,
transactionHash: 'logTransactionHash',
logIndex: 0,
},
requestCount: '12',
requestType: 'template',
Expand Down Expand Up @@ -69,6 +70,7 @@ describe('aggregate (API calls)', () => {
currentBlock: 10716090,
minConfirmations: 0,
transactionHash: 'logTransactionHash',
logIndex: 0,
},
requestCount: '12',
requestType: 'template',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('fetchPendingRequests', () => {
currentBlock: null,
minConfirmations: 0,
transactionHash: '0x420ebda3f246256ced7a58fb72d28d99548eb30de6d2e4d5c767fb547ff795ff',
logIndex: 0,
},
parameters: {
_path: 'result',
Expand All @@ -56,6 +57,7 @@ describe('fetchPendingRequests', () => {
currentBlock: null,
minConfirmations: 0,
transactionHash: '0xcc764cd8c569d23b8b2246c80ec8e2091772140e1aafe9e326dfe37cd73454c4',
logIndex: 0,
},
sponsorAddress: '0x2479808b1216E998309A727df8A0A98A1130A162',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ describe('initializeProvider', () => {
currentBlock: currentBlockNumber,
minConfirmations: 0,
transactionHash: '0x40b93a1e81c7162460af066be96266ff692515a2f6b54bd622aa9f82ee00670f',
logIndex: 0,
},
parameters: {
from: 'ETH',
Expand Down Expand Up @@ -92,6 +93,7 @@ describe('initializeProvider', () => {
currentBlock: currentBlockNumber,
minConfirmations: 0,
transactionHash: '0x420ebda3f246256ced7a58fb72d28d99548eb30de6d2e4d5c767fb547ff795ff',
logIndex: 0,
},
parameters: {
from: 'ETH',
Expand Down
8 changes: 8 additions & 0 deletions packages/airnode-node/src/evm/requests/api-calls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('initialize (ApiCall)', () => {
currentBlock: 10716085,
minConfirmations: 0,
transactionHash: event.transactionHash,
logIndex: 0,
};

expect(apiCalls.initialize(parsedLogWithMetadata)).toEqual({
Expand All @@ -35,6 +36,7 @@ describe('initialize (ApiCall)', () => {
currentBlock: 10716085,
minConfirmations: 0,
transactionHash: event.transactionHash,
logIndex: 0,
},
parameters: {},
requestCount: '1',
Expand All @@ -53,6 +55,7 @@ describe('initialize (ApiCall)', () => {
currentBlock: 10716085,
minConfirmations: 0,
transactionHash: event.transactionHash,
logIndex: 0,
};
const template = {
...base,
Expand Down Expand Up @@ -81,6 +84,7 @@ describe('applyParameters', () => {
currentBlock: 10716085,
minConfirmations: 0,
transactionHash: event.transactionHash,
logIndex: 0,
};
});

Expand Down Expand Up @@ -149,6 +153,7 @@ describe('mapRequests (ApiCall)', () => {
currentBlock: 10716085,
minConfirmations: 0,
transactionHash: event.transactionHash,
logIndex: 0,
};
const [logs, res] = apiCalls.mapRequests([parsedLogWithMetadata]);
expect(logs).toEqual([]);
Expand All @@ -171,6 +176,7 @@ describe('mapRequests (ApiCall)', () => {
currentBlock: 10716085,
minConfirmations: 0,
transactionHash: event.transactionHash,
logIndex: 0,
},
parameters: { from: 'ETH' },
requestCount: '1',
Expand All @@ -193,6 +199,7 @@ describe('mapRequests (ApiCall)', () => {
currentBlock: 10716085,
minConfirmations: 0,
transactionHash: requestEvent.transactionHash,
logIndex: 0,
};
const fulfillLogWithMetadata = {
parsedLog: fulfillLog,
Expand All @@ -201,6 +208,7 @@ describe('mapRequests (ApiCall)', () => {
currentBlock: 10716087,
minConfirmations: 0,
transactionHash: fulfillEvent.transactionHash,
logIndex: 0,
};

const [logs, requests] = apiCalls.mapRequests([requestLogWithMetadata, fulfillLogWithMetadata]);
Expand Down
1 change: 1 addition & 0 deletions packages/airnode-node/src/evm/requests/api-calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function initialize(log: EVMMadeRequestLog): Request<ApiCall> {
currentBlock: log.currentBlock,
minConfirmations: log.minConfirmations,
transactionHash: log.transactionHash,
logIndex: log.logIndex,
},
// Parameters are decoded separately
parameters: {},
Expand Down
23 changes: 20 additions & 3 deletions packages/airnode-node/src/evm/requests/event-logs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@ describe('EVM event logs - fetch', () => {
blockNumber: 10716082,
topic: '0xeb39930cdcbb560e6422558a2468b93a215af60063622e63cbb165eba14c3203',
transactionHash: '0x1',
logIndex: 0,
};
const fulfilledApiCallEvent = {
blockNumber: 10716083,
topic: '0x1bdbe9e5d42a025a741fc3582eb3cad4ef61ac742d83cc87e545fbd481b926b5',
transactionHash: '0x2',
logIndex: 0,
};
const unknownEvent = {
blockNumber: 10716082,
topic: '0xa3c071367f90badae4981bd81d1e0a407fe9ad80e35d4c95ffdd4e4f7850280b',
transactionHash: '0x3',
logIndex: 1,
};

const getLogs = jest.spyOn(ethers.providers.JsonRpcProvider.prototype, 'getLogs') as any;
Expand All @@ -41,9 +44,9 @@ describe('EVM event logs - fetch', () => {
// a stable way to have the ABI accessible in the tests
const contractInterface = new ethers.utils.Interface('abi here');
const parseLog = contractInterface.parseLog as jest.Mock;
parseLog.mockReturnValueOnce(removeKeys(newApiCallEvent, ['blockNumber', 'transactionHash']));
parseLog.mockReturnValueOnce(removeKeys(fulfilledApiCallEvent, ['blockNumber', 'transactionHash']));
parseLog.mockReturnValueOnce(removeKeys(unknownEvent, ['blockNumber', 'transactionHash']));
parseLog.mockReturnValueOnce(removeKeys(newApiCallEvent, ['blockNumber', 'transactionHash', 'logIndex']));
parseLog.mockReturnValueOnce(removeKeys(fulfilledApiCallEvent, ['blockNumber', 'transactionHash', 'logIndex']));
parseLog.mockReturnValueOnce(removeKeys(unknownEvent, ['blockNumber', 'transactionHash', 'logIndex']));

const fetchOptions = {
address: '0xe60b966B798f9a0C41724f111225A5586ff30656',
Expand All @@ -62,20 +65,23 @@ describe('EVM event logs - fetch', () => {
minConfirmations: 1,
parsedLog: { topic: '0xeb39930cdcbb560e6422558a2468b93a215af60063622e63cbb165eba14c3203' },
transactionHash: '0x1',
logIndex: 0,
},
{
blockNumber: 10716083,
currentBlock: 10716084,
minConfirmations: 1,
parsedLog: { topic: '0x1bdbe9e5d42a025a741fc3582eb3cad4ef61ac742d83cc87e545fbd481b926b5' },
transactionHash: '0x2',
logIndex: 0,
},
{
blockNumber: 10716082,
currentBlock: 10716084,
minConfirmations: 1,
parsedLog: { topic: '0xa3c071367f90badae4981bd81d1e0a407fe9ad80e35d4c95ffdd4e4f7850280b' },
transactionHash: '0x3',
logIndex: 1,
},
]);
expect(getLogs).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -109,6 +115,7 @@ describe('EVM event logs - fetch', () => {
blockNumber: 10716082,
topic: '0xinvalidtopic',
transactionHash: '0x1',
logIndex: 0,
};
const getLogs = jest.spyOn(ethers.providers.JsonRpcProvider.prototype, 'getLogs') as any;
getLogs.mockResolvedValueOnce([newApiCallEvent]);
Expand Down Expand Up @@ -185,18 +192,21 @@ describe('EVM event logs - group', () => {
blockNumber: 10716082,
parsedLog: { topic: '0xeb39930cdcbb560e6422558a2468b93a215af60063622e63cbb165eba14c3203' },
transactionHash: '0x1',
logIndex: 0,
},
// Fulfillment
{
blockNumber: 10716083,
parsedLog: { topic: '0xc0977dab79883641ece94bb6a932ca83049f561ffff8d8daaeafdbc1acce9e0a' },
transactionHash: '0x2',
logIndex: 0,
},
// Unknown event
{
blockNumber: 10716082,
parsedLog: { topic: '0xa3c071367f90badae4981bd81d1e0a407fe9ad80e35d4c95ffdd4e4f7850280b' },
transactionHash: '0x3',
logIndex: 1,
},
];

Expand All @@ -207,11 +217,13 @@ describe('EVM event logs - group', () => {
blockNumber: 10716082,
parsedLog: { topic: '0xeb39930cdcbb560e6422558a2468b93a215af60063622e63cbb165eba14c3203' },
transactionHash: '0x1',
logIndex: 0,
},
{
blockNumber: 10716083,
parsedLog: { topic: '0xc0977dab79883641ece94bb6a932ca83049f561ffff8d8daaeafdbc1acce9e0a' },
transactionHash: '0x2',
logIndex: 0,
},
],
withdrawals: [],
Expand All @@ -224,17 +236,20 @@ describe('EVM event logs - group', () => {
blockNumber: 10716082,
parsedLog: { topic: '0xd48d52c7c6d0c940f3f8d07591e1800ef3a70daf79929a97ccd80b4494769fc7' },
transactionHash: '0x1',
logIndex: 0,
},
{
blockNumber: 10716083,
parsedLog: { topic: '0xadb4840bbd5f924665ae7e0e0c83de5c0fb40a98c9b57dba53a6c978127a622e' },
transactionHash: '0x2',
logIndex: 0,
},
// Unknown event
{
blockNumber: 10716082,
parsedLog: { topic: '0xa3c071367f90badae4981bd81d1e0a407fe9ad80e35d4c95ffdd4e4f7850280b' },
transactionHash: '0x3',
logIndex: 1,
},
];

Expand All @@ -246,11 +261,13 @@ describe('EVM event logs - group', () => {
blockNumber: 10716082,
parsedLog: { topic: '0xd48d52c7c6d0c940f3f8d07591e1800ef3a70daf79929a97ccd80b4494769fc7' },
transactionHash: '0x1',
logIndex: 0,
},
{
blockNumber: 10716083,
parsedLog: { topic: '0xadb4840bbd5f924665ae7e0e0c83de5c0fb40a98c9b57dba53a6c978127a622e' },
transactionHash: '0x2',
logIndex: 0,
},
],
});
Expand Down
1 change: 1 addition & 0 deletions packages/airnode-node/src/evm/requests/event-logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export async function fetch(options: FetchOptions): Promise<EVMEventLog[]> {
currentBlock: options.currentBlock,
minConfirmations: options.minConfirmations,
transactionHash: log.transactionHash,
logIndex: log.logIndex,
// If the provider returns a bad response, mapping logs could also throw
parsedLog: parseAirnodeRrpLog(log),
})) as EVMEventLog[];
Expand Down
6 changes: 6 additions & 0 deletions packages/airnode-node/src/evm/requests/withdrawals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('initialize (Withdrawal)', () => {
currentBlock: 10716085,
minConfirmations: 0,
transactionHash: event.transactionHash,
logIndex: 0,
};
const res = withdrawals.initialize(parseLogWithMetadata);
expect(res).toEqual({
Expand All @@ -26,6 +27,7 @@ describe('initialize (Withdrawal)', () => {
currentBlock: 10716085,
minConfirmations: 0,
transactionHash: event.transactionHash,
logIndex: 0,
},
sponsorAddress: '0x2479808b1216E998309A727df8A0A98A1130A162',
});
Expand Down Expand Up @@ -65,6 +67,7 @@ describe('mapRequests (Withdrawal)', () => {
currentBlock: 10716085,
minConfirmations: 0,
transactionHash: event.transactionHash,
logIndex: 0,
};
const [logs, res] = withdrawals.mapRequests([parsedLogWithMetadata]);
expect(logs).toEqual([]);
Expand All @@ -79,6 +82,7 @@ describe('mapRequests (Withdrawal)', () => {
currentBlock: 10716085,
minConfirmations: 0,
transactionHash: event.transactionHash,
logIndex: 0,
},
sponsorAddress: '0x2479808b1216E998309A727df8A0A98A1130A162',
},
Expand All @@ -98,6 +102,7 @@ describe('mapRequests (Withdrawal)', () => {
currentBlock: 10716085,
minConfirmations: 0,
transactionHash: requestEvent.transactionHash,
logIndex: 0,
};
const fulfillLogWithMetadata = {
parsedLog: fulfillLog,
Expand All @@ -106,6 +111,7 @@ describe('mapRequests (Withdrawal)', () => {
currentBlock: 10716087,
minConfirmations: 0,
transactionHash: fulfillEvent.transactionHash,
logIndex: 0,
};

const [logs, requests] = withdrawals.mapRequests([requestLogWithMetadata, fulfillLogWithMetadata]);
Expand Down
1 change: 1 addition & 0 deletions packages/airnode-node/src/evm/requests/withdrawals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function initialize(logWithMetadata: EVMRequestedWithdrawalLog): Request<
currentBlock: logWithMetadata.currentBlock,
minConfirmations: logWithMetadata.minConfirmations,
transactionHash: logWithMetadata.transactionHash,
logIndex: logWithMetadata.logIndex,
},
sponsorAddress: parsedLog.args.sponsor,
};
Expand Down
12 changes: 6 additions & 6 deletions packages/airnode-node/src/requests/nonces.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ describe('assign', () => {
});

it('sorts and assigns nonces requests API calls', () => {
const firstMeta = fixtures.requests.buildMetadata({ blockNumber: 100, transactionHash: '0xa' });
const secondMeta = fixtures.requests.buildMetadata({ blockNumber: 101, transactionHash: '0xb' });
const thirdMeta = fixtures.requests.buildMetadata({ blockNumber: 101, transactionHash: '0xc' });
const firstMeta = fixtures.requests.buildMetadata({ blockNumber: 100, transactionHash: '0xa', logIndex: 0 });
const secondMeta = fixtures.requests.buildMetadata({ blockNumber: 101, transactionHash: '0xb', logIndex: 1 });
const thirdMeta = fixtures.requests.buildMetadata({ blockNumber: 101, transactionHash: '0xc', logIndex: 2 });

const sponsorAddress = '0x69e2B095fbAc6C3f9E528Ef21882b86BF1595181';
const first = fixtures.requests.buildApiCall({
Expand Down Expand Up @@ -55,9 +55,9 @@ describe('assign', () => {
});

it('sorts and assigns nonces requests withdrawals', () => {
const firstMeta = fixtures.requests.buildMetadata({ blockNumber: 100, transactionHash: '0xa' });
const secondMeta = fixtures.requests.buildMetadata({ blockNumber: 101, transactionHash: '0xb' });
const thirdMeta = fixtures.requests.buildMetadata({ blockNumber: 101, transactionHash: '0xc' });
const firstMeta = fixtures.requests.buildMetadata({ blockNumber: 100, transactionHash: '0xa', logIndex: 0 });
const secondMeta = fixtures.requests.buildMetadata({ blockNumber: 101, transactionHash: '0xb', logIndex: 1 });
const thirdMeta = fixtures.requests.buildMetadata({ blockNumber: 101, transactionHash: '0xc', logIndex: 2 });

const sponsorAddress = '0x1d822613f7cC57Be9c9b6C3cC0Bf41b4FB4D97f9';
const first = fixtures.requests.buildWithdrawal({
Expand Down
Loading