Skip to content

Commit f17957b

Browse files
authored
Merge pull request #1303 from ainblockchain/bugfix/platfowner/bugfix
Do not use exceptions for deregistration errors
2 parents 9153188 + 895752d commit f17957b

File tree

5 files changed

+45
-67
lines changed

5 files changed

+45
-67
lines changed

common/result-code.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,15 @@ const EventHandlerErrorCode = {
217217
MISSING_CONFIG_IN_MSG_DATA: 70007,
218218
DUPLICATED_GLOBAL_FILTER_ID: 70008,
219219
INVALID_EVENT_TYPE_IN_VALIDATE_FUNC: 70009,
220-
NO_MATCHED_FILTERS: 70010,
221-
MISSING_FILTER_ID_IN_TYPE_TO_FILTER_IDS: 70011,
222-
PARSING_GLOBAL_FILTER_ID_FAILURE: 70012,
220+
NO_MATCHED_FILTERS: 70010, // Deprecated (2024-08-14).
221+
MISSING_FILTER_ID_IN_TYPE_TO_FILTER_IDS: 70011, // Deprecated (2024-08-14)
222+
PARSING_GLOBAL_FILTER_ID_FAILURE: 70012, // Deprecated (2024-08-14).
223223
DUPLICATED_CHANNEL_ID: 70013,
224224
EVENT_CHANNEL_EXCEEDS_SIZE_LIMIT: 70014,
225225
EVENT_FILTER_EXCEEDS_SIZE_LIMIT: 70015,
226226
EVENT_FILTER_EXCEEDS_SIZE_LIMIT_PER_CHANNEL: 70016,
227227
FAILED_TO_REGISTER_FILTER: 70020,
228-
FAILED_TO_DEREGISTER_FILTER: 70030,
228+
FAILED_TO_DEREGISTER_FILTER: 70030, // Deprecated (2024-08-14).
229229
INVALID_CUSTOM_CLIENT_ID: 70040,
230230
// BLOCK_FINALIZED (701XX)
231231
NEGATIVE_BLOCK_NUMBER: 70100,
@@ -234,9 +234,9 @@ const EventHandlerErrorCode = {
234234
MISSING_PATH_IN_CONFIG: 70200,
235235
INVALID_FORMAT_PATH: 70201,
236236
// VALUE_CHANGED & StateEventTreeManager (7025X)
237-
MISSING_FILTER_ID_IN_FILTER_ID_TO_PARSED_PATH: 70250,
238-
MISSING_FILTER_ID_SET: 70251,
239-
MISSING_FILTER_ID_IN_FILTER_ID_SET: 70252,
237+
MISSING_FILTER_ID_IN_FILTER_ID_TO_PARSED_PATH: 70250, // Deprecated (2024-08-14).
238+
MISSING_FILTER_ID_SET: 70251, // Deprecated (2024-08-14).
239+
MISSING_FILTER_ID_IN_FILTER_ID_SET: 70252, // Deprecated (2024-08-14).
240240
// TX_STATE_CHANGED (703XX)
241241
MISSING_TX_HASH_IN_CONFIG: 70300,
242242
INVALID_TX_HASH: 70301,

event-handler/event-channel-manager.js

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -236,25 +236,15 @@ class EventChannelManager {
236236

237237
deregisterFilter(channel, clientFilterId) {
238238
const filter = this.node.eh.deregisterEventFilter(clientFilterId, channel.id);
239+
if (!filter) {
240+
return;
241+
}
239242
channel.deleteEventFilterId(filter.id);
240243
delete this.filterIdToChannelId[filter.id];
241244
}
242245

243246
deregisterFilterAndEmitEvent(channel, clientFilterId, filterDeletionReason) {
244-
const LOG_HEADER = 'deregisterFilterAndEmitEvent';
245-
try {
246-
this.deregisterFilter(channel, clientFilterId);
247-
} catch (err) {
248-
logger.error(`[${LOG_HEADER}] Can't deregister event filter ` +
249-
`(clientFilterId: ${clientFilterId}, channelId: ${channel.id}, ` +
250-
`err: ${err.message} at ${err.stack})`);
251-
throw new EventHandlerError(
252-
EventHandlerErrorCode.FAILED_TO_DEREGISTER_FILTER,
253-
`Failed to deregister filter with filter ID: ${clientFilterId} ` +
254-
`due to error: ${err.message}`,
255-
clientFilterId
256-
);
257-
}
247+
this.deregisterFilter(channel, clientFilterId);
258248
const blockchainEvent = new BlockchainEvent(
259249
BlockchainEventTypes.FILTER_DELETED,
260250
{
@@ -344,6 +334,9 @@ class EventChannelManager {
344334
// TODO(ehgmsdk20): reuse same object for memory
345335
const eventObj = event.toObject();
346336
const clientFilterId = this.node.eh.getClientFilterIdFromGlobalFilterId(eventFilterId);
337+
if (!clientFilterId) {
338+
return;
339+
}
347340
Object.assign(eventObj, { filter_id: clientFilterId });
348341
this.transmitEventObj(channel, eventObj);
349342
}
@@ -369,20 +362,18 @@ class EventChannelManager {
369362

370363
closeChannel(channel) {
371364
const LOG_HEADER = 'closeChannel';
372-
try {
373-
logger.info(`[${LOG_HEADER}] Closing channel ${channel.id}`);
374-
channel.webSocket.terminate();
375-
const filterIds = channel.getAllFilterIds();
376-
for (const filterId of filterIds) {
377-
const clientFilterId = this.node.eh.getClientFilterIdFromGlobalFilterId(filterId);
378-
// NOTE(ehgmsdk20): Do not emit filter_deleted event because the channel is already closed.
379-
this.deregisterFilter(channel, clientFilterId);
365+
logger.info(`[${LOG_HEADER}] Closing channel ${channel.id}`);
366+
channel.webSocket.terminate();
367+
const filterIds = channel.getAllFilterIds();
368+
for (const filterId of filterIds) {
369+
const clientFilterId = this.node.eh.getClientFilterIdFromGlobalFilterId(filterId);
370+
if (!clientFilterId) {
371+
continue;
380372
}
381-
delete this.channels[channel.id];
382-
} catch (err) {
383-
logger.error(`[${LOG_HEADER}] Error while closing channel (channelId: ${channel.id}, ` +
384-
`message:${err.message})`);
373+
// NOTE(ehgmsdk20): Do not emit filter_deleted event because the channel is already closed.
374+
this.deregisterFilter(channel, clientFilterId);
385375
}
376+
delete this.channels[channel.id];
386377
}
387378

388379
startHeartbeat(wsServer) {

event-handler/index.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ class EventHandler {
191191
if (isEndState(afterState)) {
192192
const channel = this.eventChannelManager.getChannelByEventFilterId(eventFilterId);
193193
const clientFilterId = this.getClientFilterIdFromGlobalFilterId(eventFilterId);
194+
if (!clientFilterId) {
195+
continue;
196+
}
194197
this.eventChannelManager.deregisterFilterAndEmitEvent(
195198
channel, clientFilterId, FilterDeletionReasons.END_STATE_REACHED
196199
);
@@ -213,17 +216,21 @@ class EventHandler {
213216
this.eventFilterIdToTimeoutCallback.set(eventFilterId, setTimeout(() => {
214217
const channel = this.eventChannelManager.getChannelByEventFilterId(eventFilterId);
215218
const clientFilterId = this.getClientFilterIdFromGlobalFilterId(eventFilterId);
219+
if (!clientFilterId) {
220+
return;
221+
}
216222
this.eventChannelManager.deregisterFilterAndEmitEvent(
217223
channel, clientFilterId, FilterDeletionReasons.FILTER_TIMEOUT
218224
);
219225
}, NodeConfigs.EVENT_HANDLER_FILTER_DELETION_TIMEOUT_MS));
220226
}
221227

222228
getClientFilterIdFromGlobalFilterId(globalFilterId) {
229+
const LOG_HEADER = 'getClientFilterIdFromGlobalFilterId';
223230
const clientFilterId = globalFilterId.split(':')[1];
224231
if (!clientFilterId) {
225-
throw new EventHandlerError(EventHandlerErrorCode.PARSING_GLOBAL_FILTER_ID_FAILURE,
226-
`Can't get client filter ID from global filter ID (globalFilterId: ${globalFilterId})`);
232+
logger.error(`[${LOG_HEADER}] Can't get client filter ID from global filter ID (globalFilterId: ${globalFilterId})`);
233+
return null;
227234
}
228235
return clientFilterId;
229236
}
@@ -304,13 +311,13 @@ class EventHandler {
304311
const eventFilterId = this.getGlobalFilterId(channelId, clientFilterId);
305312
const eventFilter = this.eventFilters[eventFilterId];
306313
if (!eventFilter) {
307-
throw new EventHandlerError(EventHandlerErrorCode.NO_MATCHED_FILTERS,
308-
`Can't find filter by filter id`, eventFilterId);
314+
logger.error(`[${LOG_HEADER}] Can't find filter by filter id (eventFilterId: ${eventFilterId})`);
315+
return null;
309316
}
310317
delete this.eventFilters[eventFilterId];
311318
if (!this.eventTypeToEventFilterIds[eventFilter.type].delete(eventFilterId)) {
312-
throw new EventHandlerError(EventHandlerErrorCode.MISSING_FILTER_ID_IN_TYPE_TO_FILTER_IDS,
313-
`Can't delete filter Id from eventTypeToEventFilterIds (${eventFilterId})`);
319+
logger.error(`[${LOG_HEADER}] Can't delete filter Id from eventTypeToEventFilterIds (eventFilterId: ${eventFilterId})`);
320+
return null;
314321
}
315322
if (eventFilter.type === BlockchainEventTypes.VALUE_CHANGED) {
316323
this.stateEventTreeManager.deregisterEventFilterId(eventFilterId);

event-handler/state-event-tree-manager.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,24 +87,24 @@ class StateEventTreeManager {
8787
}
8888

8989
deleteFilterIdFromEventNode(eventNode, filterId) {
90+
const LOG_HEADER = 'deleteFilterIdFromEventNode';
9091
if (!eventNode || !eventNode.filterIdSet) {
91-
throw new EventHandlerError(EventHandlerErrorCode.MISSING_FILTER_ID_SET,
92-
`Can't find filterIdSet (eventNode: ${JSON.stringify(eventNode)})`);
92+
logger.error(`[${LOG_HEADER}] Can't find filterIdSet (eventNode: ${JSON.stringify(eventNode)})`);
93+
return;
9394
}
9495
if (!eventNode.filterIdSet.delete(filterId)) {
95-
throw new EventHandlerError(EventHandlerErrorCode.MISSING_FILTER_ID_IN_FILTER_ID_SET,
96-
`Can't delete filter id (${filterId}) from filterIdSet ` +
96+
logger.error(`[${LOG_HEADER}] Can't delete filter id (${filterId}) from filterIdSet ` +
9797
`(${JSON.stringify(eventNode.filterIdSet.values())})`);
98+
return;
9899
}
99100
}
100101

101102
deregisterEventFilterId(filterId) {
103+
const LOG_HEADER = 'deregisterEventFilterId';
102104
const parsedPath = this.filterIdToParsedPath[filterId];
103105
if (!parsedPath) {
104-
throw new EventHandlerError(
105-
EventHandlerErrorCode.MISSING_FILTER_ID_IN_FILTER_ID_TO_PARSED_PATH,
106-
`Can't find parsedPath from filterIdToParsedPath (${filterId})`
107-
);
106+
logger.error(`[${LOG_HEADER}] Can't find parsedPath from filterIdToParsedPath (filterId: ${filterId})`);
107+
return;
108108
}
109109
delete this.filterIdToParsedPath[filterId];
110110

test/integration/event_handler.test.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -254,26 +254,6 @@ describe('Event Handler Test', function() {
254254
deregisterFilter(wsClient, filterId);
255255
});
256256

257-
it('Deregister filter already deregisterd filter', function(done) {
258-
// Deregister filter
259-
wsClient.on('message', (message) => {
260-
try {
261-
const parsedMessage = JSON.parse(message);
262-
const messageType = parsedMessage.type;
263-
const errorCode = _.get(parsedMessage, 'data.code');
264-
const errorMessage = _.get(parsedMessage, 'data.message');
265-
if (messageType === BlockchainEventMessageTypes.EMIT_ERROR) {
266-
expect(errorCode).to.equal(EventHandlerErrorCode.FAILED_TO_DEREGISTER_FILTER);
267-
expect(errorMessage).to.equal(`Failed to deregister filter with filter ID: ${filterId} due to error: Can't find filter by filter id`)
268-
done();
269-
}
270-
} catch (err) {
271-
done(err);
272-
}
273-
});
274-
deregisterFilter(wsClient, filterId);
275-
});
276-
277257
it('Register too many filters', function(done) {
278258
let exceededCnt = 0;
279259
wsClient.on('message', (message) => {

0 commit comments

Comments
 (0)