Skip to content

Commit b6ce579

Browse files
Merge pull request #375 from HathorNetwork/fix/check-missed-events-no-context-event
2 parents 6c5b4ed + 0b9bcfc commit b6ce579

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

packages/daemon/__tests__/services/services.test.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,12 +1675,42 @@ describe('checkForMissedEvents', () => {
16751675
);
16761676
});
16771677

1678-
it('should throw error when context has no event', async () => {
1678+
it('should throw error when context has no event and no initialEventId', async () => {
16791679
const context = {};
16801680

16811681
await expect(checkForMissedEvents(context as any))
16821682
.rejects
1683-
.toThrow('No event in context when checking for missed events');
1683+
.toThrow('No event in context and no initialEventId when checking for missed events');
1684+
});
1685+
1686+
it('should use initialEventId when context.event is null', async () => {
1687+
const mockResponse = {
1688+
status: 200,
1689+
data: {
1690+
events: [],
1691+
latest_event_id: 25717039,
1692+
},
1693+
};
1694+
1695+
(axios.get as jest.Mock).mockResolvedValue(mockResponse);
1696+
1697+
const context = {
1698+
event: null,
1699+
initialEventId: 25717039,
1700+
};
1701+
1702+
const result = await checkForMissedEvents(context as any);
1703+
1704+
expect(result.hasNewEvents).toBe(false);
1705+
expect(axios.get).toHaveBeenCalledWith(
1706+
expect.stringContaining('/event'),
1707+
expect.objectContaining({
1708+
params: {
1709+
last_ack_event_id: 25717039,
1710+
size: 1,
1711+
},
1712+
}),
1713+
);
16841714
});
16851715

16861716
it('should handle API response with non-array events field', async () => {

packages/daemon/src/services/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,11 +1078,11 @@ export const handleTokenCreated = async (context: Context) => {
10781078
* This is used to detect if we lost an event due to network packet loss
10791079
*/
10801080
export const checkForMissedEvents = async (context: Context): Promise<{ hasNewEvents: boolean; events: any[] }> => {
1081-
if (!context.event) {
1082-
throw new Error('No event in context when checking for missed events');
1083-
}
1081+
const lastAckEventId = context.event?.event.id ?? context.initialEventId;
10841082

1085-
const lastAckEventId = context.event.event.id;
1083+
if (lastAckEventId === null || lastAckEventId === undefined) {
1084+
throw new Error('No event in context and no initialEventId when checking for missed events');
1085+
}
10861086
const fullnodeUrl = getFullnodeHttpUrl();
10871087

10881088
logger.debug(`Checking for missed events after event ID ${lastAckEventId}`);

0 commit comments

Comments
 (0)