Skip to content

Commit 143fa4d

Browse files
authored
Merge pull request #1306 from ainblockchain/release/v1.4.2
Release/v1.4.2
2 parents 91c48e4 + c988cb7 commit 143fa4d

File tree

7 files changed

+54
-69
lines changed

7 files changed

+54
-69
lines changed

client/protocol_versions.json

+3
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,8 @@
152152
},
153153
"1.4.1": {
154154
"min": "1.0.0"
155+
},
156+
"1.4.2": {
157+
"min": "1.0.0"
155158
}
156159
}

common/result-code.js

+7-7
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

+22-27
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const EventHandlerError = require('./event-handler-error');
1313
const { EventHandlerErrorCode } = require('../common/result-code');
1414
const BlockchainEvent = require('./blockchain-event');
1515

16+
const CHANNEL_ID_RANDOM_NUMBER_RANGE = 1000;
17+
1618
class EventChannelManager {
1719
constructor(node) {
1820
this.node = node;
@@ -107,7 +109,9 @@ class EventChannelManager {
107109
`The number of event channels exceeds its limit ` +
108110
`(${NodeConfigs.MAX_NUM_EVENT_CHANNELS})`);
109111
}
110-
const channelId = Date.now(); // NOTE: Only used in blockchain
112+
// NOTE: Only used in blockchain
113+
const channelId
114+
= String(Date.now() + Math.floor(Math.random() * CHANNEL_ID_RANDOM_NUMBER_RANGE));
111115
if (this.channels[channelId]) { // TODO(cshcomcom): Retry logic.
112116
webSocket.terminate();
113117
throw new EventHandlerError(EventHandlerErrorCode.DUPLICATED_CHANNEL_ID,
@@ -236,25 +240,15 @@ class EventChannelManager {
236240

237241
deregisterFilter(channel, clientFilterId) {
238242
const filter = this.node.eh.deregisterEventFilter(clientFilterId, channel.id);
243+
if (!filter) {
244+
return;
245+
}
239246
channel.deleteEventFilterId(filter.id);
240247
delete this.filterIdToChannelId[filter.id];
241248
}
242249

243250
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-
}
251+
this.deregisterFilter(channel, clientFilterId);
258252
const blockchainEvent = new BlockchainEvent(
259253
BlockchainEventTypes.FILTER_DELETED,
260254
{
@@ -344,6 +338,9 @@ class EventChannelManager {
344338
// TODO(ehgmsdk20): reuse same object for memory
345339
const eventObj = event.toObject();
346340
const clientFilterId = this.node.eh.getClientFilterIdFromGlobalFilterId(eventFilterId);
341+
if (!clientFilterId) {
342+
return;
343+
}
347344
Object.assign(eventObj, { filter_id: clientFilterId });
348345
this.transmitEventObj(channel, eventObj);
349346
}
@@ -369,20 +366,18 @@ class EventChannelManager {
369366

370367
closeChannel(channel) {
371368
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);
369+
logger.info(`[${LOG_HEADER}] Closing channel ${channel.id}`);
370+
channel.webSocket.terminate();
371+
const filterIds = channel.getAllFilterIds();
372+
for (const filterId of filterIds) {
373+
const clientFilterId = this.node.eh.getClientFilterIdFromGlobalFilterId(filterId);
374+
if (!clientFilterId) {
375+
continue;
380376
}
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})`);
377+
// NOTE(ehgmsdk20): Do not emit filter_deleted event because the channel is already closed.
378+
this.deregisterFilter(channel, clientFilterId);
385379
}
380+
delete this.channels[channel.id];
386381
}
387382

388383
startHeartbeat(wsServer) {

event-handler/index.js

+13-6
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

+8-8
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

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ain-blockchain",
33
"description": "AI Network Blockchain",
4-
"version": "1.4.1",
4+
"version": "1.4.2",
55
"private": true,
66
"license": "MIT",
77
"author": "[email protected]",

test/integration/event_handler.test.js

-20
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)