Skip to content

Commit

Permalink
Merge pull request #1298 from ainblockchain/feature/platfowner/feature
Browse files Browse the repository at this point in the history
Support SET_CUSTOM_CLIENT_ID event message type
platfowner authored Aug 12, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 447575d + feb42f3 commit 0ed88e8
Showing 16 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions blockchain-configs/afan-shard/node_params.json
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@
"EVENT_HANDLER_CHANNEL_LIFE_TIME_LIMIT_SECS": 3600,
"EVENT_HANDLER_FILTER_DELETION_TIMEOUT_MS": 15000,
"EVENT_HANDLER_HEARTBEAT_INTERVAL_MS": 15000,
"EVENT_HANDLER_MAX_CUSTOM_CLIENT_ID_LENGTH": 100,
"EVENT_HANDLER_PORT": 5100,
"EXPRESS_RATE_LIMIT_WINDOW_SECS": 60,
"FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1,
1 change: 1 addition & 0 deletions blockchain-configs/base/node_params.json
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@
"EVENT_HANDLER_CHANNEL_LIFE_TIME_LIMIT_SECS": 3600,
"EVENT_HANDLER_FILTER_DELETION_TIMEOUT_MS": 100000,
"EVENT_HANDLER_HEARTBEAT_INTERVAL_MS": 15000,
"EVENT_HANDLER_MAX_CUSTOM_CLIENT_ID_LENGTH": 100,
"EVENT_HANDLER_PORT": 5100,
"EXPRESS_RATE_LIMIT_WINDOW_SECS": 60,
"FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1,
1 change: 1 addition & 0 deletions blockchain-configs/he-shard/node_params.json
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@
"EVENT_HANDLER_CHANNEL_LIFE_TIME_LIMIT_SECS": 3600,
"EVENT_HANDLER_FILTER_DELETION_TIMEOUT_MS": 15000,
"EVENT_HANDLER_HEARTBEAT_INTERVAL_MS": 15000,
"EVENT_HANDLER_MAX_CUSTOM_CLIENT_ID_LENGTH": 100,
"EVENT_HANDLER_PORT": 5100,
"EXPRESS_RATE_LIMIT_WINDOW_SECS": 60,
"FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1,
1 change: 1 addition & 0 deletions blockchain-configs/mainnet-prod/node_params.json
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@
"EVENT_HANDLER_CHANNEL_LIFE_TIME_LIMIT_SECS": 3600,
"EVENT_HANDLER_FILTER_DELETION_TIMEOUT_MS": 100000,
"EVENT_HANDLER_HEARTBEAT_INTERVAL_MS": 15000,
"EVENT_HANDLER_MAX_CUSTOM_CLIENT_ID_LENGTH": 100,
"EVENT_HANDLER_PORT": 5100,
"EXPRESS_RATE_LIMIT_WINDOW_SECS": 60,
"FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1,
1 change: 1 addition & 0 deletions blockchain-configs/sim-shard/node_params.json
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@
"EVENT_HANDLER_CHANNEL_LIFE_TIME_LIMIT_SECS": 3600,
"EVENT_HANDLER_FILTER_DELETION_TIMEOUT_MS": 100000,
"EVENT_HANDLER_HEARTBEAT_INTERVAL_MS": 15000,
"EVENT_HANDLER_MAX_CUSTOM_CLIENT_ID_LENGTH": 100,
"EVENT_HANDLER_PORT": 5100,
"EXPRESS_RATE_LIMIT_WINDOW_SECS": 60,
"FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1,
1 change: 1 addition & 0 deletions blockchain-configs/testnet-dev/node_params.json
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@
"EVENT_HANDLER_CHANNEL_LIFE_TIME_LIMIT_SECS": 3600,
"EVENT_HANDLER_FILTER_DELETION_TIMEOUT_MS": 100000,
"EVENT_HANDLER_HEARTBEAT_INTERVAL_MS": 15000,
"EVENT_HANDLER_MAX_CUSTOM_CLIENT_ID_LENGTH": 100,
"EVENT_HANDLER_PORT": 5100,
"EXPRESS_RATE_LIMIT_WINDOW_SECS": 60,
"FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1,
1 change: 1 addition & 0 deletions blockchain-configs/testnet-exp/node_params.json
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@
"EVENT_HANDLER_CHANNEL_LIFE_TIME_LIMIT_SECS": 3600,
"EVENT_HANDLER_FILTER_DELETION_TIMEOUT_MS": 100000,
"EVENT_HANDLER_HEARTBEAT_INTERVAL_MS": 15000,
"EVENT_HANDLER_MAX_CUSTOM_CLIENT_ID_LENGTH": 100,
"EVENT_HANDLER_PORT": 5100,
"EXPRESS_RATE_LIMIT_WINDOW_SECS": 60,
"FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1,
1 change: 1 addition & 0 deletions blockchain-configs/testnet-prod/node_params.json
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@
"EVENT_HANDLER_CHANNEL_LIFE_TIME_LIMIT_SECS": 3600,
"EVENT_HANDLER_FILTER_DELETION_TIMEOUT_MS": 100000,
"EVENT_HANDLER_HEARTBEAT_INTERVAL_MS": 15000,
"EVENT_HANDLER_MAX_CUSTOM_CLIENT_ID_LENGTH": 100,
"EVENT_HANDLER_PORT": 5100,
"EXPRESS_RATE_LIMIT_WINDOW_SECS": 60,
"FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1,
1 change: 1 addition & 0 deletions blockchain-configs/testnet-sandbox/node_params.json
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@
"EVENT_HANDLER_CHANNEL_LIFE_TIME_LIMIT_SECS": 3600,
"EVENT_HANDLER_FILTER_DELETION_TIMEOUT_MS": 100000,
"EVENT_HANDLER_HEARTBEAT_INTERVAL_MS": 15000,
"EVENT_HANDLER_MAX_CUSTOM_CLIENT_ID_LENGTH": 100,
"EVENT_HANDLER_PORT": 5100,
"EXPRESS_RATE_LIMIT_WINDOW_SECS": 60,
"FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1,
1 change: 1 addition & 0 deletions blockchain-configs/testnet-staging/node_params.json
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@
"EVENT_HANDLER_CHANNEL_LIFE_TIME_LIMIT_SECS": 3600,
"EVENT_HANDLER_FILTER_DELETION_TIMEOUT_MS": 100000,
"EVENT_HANDLER_HEARTBEAT_INTERVAL_MS": 15000,
"EVENT_HANDLER_MAX_CUSTOM_CLIENT_ID_LENGTH": 100,
"EVENT_HANDLER_PORT": 5100,
"EXPRESS_RATE_LIMIT_WINDOW_SECS": 60,
"FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1,
1 change: 1 addition & 0 deletions common/constants.js
Original file line number Diff line number Diff line change
@@ -677,6 +677,7 @@ const BlockchainEventTypes = {
};

const BlockchainEventMessageTypes = {
SET_CUSTOM_CLIENT_ID: 'SET_CUSTOM_CLIENT_ID',
REGISTER_FILTER: 'REGISTER_FILTER',
DEREGISTER_FILTER: 'DEREGISTER_FILTER',
EMIT_EVENT: 'EMIT_EVENT',
1 change: 1 addition & 0 deletions common/result-code.js
Original file line number Diff line number Diff line change
@@ -226,6 +226,7 @@ const EventHandlerErrorCode = {
EVENT_FILTER_EXCEEDS_SIZE_LIMIT_PER_CHANNEL: 70016,
FAILED_TO_REGISTER_FILTER: 70020,
FAILED_TO_DEREGISTER_FILTER: 70030,
INVALID_CUSTOM_CLIENT_ID: 70040,
// BLOCK_FINALIZED (701XX)
NEGATIVE_BLOCK_NUMBER: 70100,
INVALID_BLOCK_NUMBER_TYPE: 70101,
19 changes: 18 additions & 1 deletion event-handler/event-channel-manager.js
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ const {
BlockchainEventTypes,
FilterDeletionReasons,
} = require('../common/constants');
const CommonUtil = require('../common/common-util');
const EventHandlerError = require('./event-handler-error');
const { EventHandlerErrorCode } = require('../common/result-code');
const BlockchainEvent = require('./blockchain-event');
@@ -40,6 +41,7 @@ class EventChannelManager {
return {
maxNumEventChannels: NodeConfigs.MAX_NUM_EVENT_CHANNELS,
numEventChannels: this.getNumEventChannels(),
channelLifeTimeLimitSecs: NodeConfigs.EVENT_HANDLER_CHANNEL_LIFE_TIME_LIMIT_SECS,
channelIdleTimeLimitSecs: NodeConfigs.EVENT_HANDLER_CHANNEL_IDLE_TIME_LIMIT_SECS,
maxChannelLifeTimeMs: channelStats.maxLifeTimeMs,
maxChannelIdleTimeMs: channelStats.maxIdleTimeMs,
@@ -157,6 +159,18 @@ class EventChannelManager {
}
}

handleSetCustomClientId(channel, messageData) {
const LOG_HEADER = 'handleSetCustomClientId';
const customClientId = messageData.customClientId;
if (!CommonUtil.isString(customClientId)) {
throw new EventHandlerError(EventHandlerErrorCode.INVALID_CUSTOM_CLIENT_ID,
`Invalid custom client id: ${customClientId}`);
}
const normalized = customClientId.slice(0, NodeConfigs.EVENT_HANDLER_MAX_CUSTOM_CLIENT_ID_LENGTH);
logger.info(`[${LOG_HEADER}] Setting custom client id: ${normalized} for channel: ${channel.id}`);
channel.setCustomClientId(normalized);
}

handleRegisterFilterMessage(channel, messageData) {
const LOG_HEADER = 'handleRegisterFilterMessage';
const clientFilterId = messageData.id;
@@ -292,6 +306,9 @@ class EventChannelManager {
// TODO(cshcomcom): Manage EVENT_PROTOCOL_VERSION.
handleMessage(channel, messageType, messageData) {
switch (messageType) {
case BlockchainEventMessageTypes.SET_CUSTOM_CLIENT_ID:
this.handleSetCustomClientId(channel, messageData);
break;
case BlockchainEventMessageTypes.REGISTER_FILTER:
this.handleRegisterFilterMessage(channel, messageData);
break;
@@ -300,7 +317,7 @@ class EventChannelManager {
break;
default:
throw new EventHandlerError(EventHandlerErrorCode.INVALID_MESSAGE_TYPE,
`Invalid message type (${messageType})`);
`Invalid message type: ${messageType}`);
}
}

7 changes: 7 additions & 0 deletions event-handler/event-channel.js
Original file line number Diff line number Diff line change
@@ -5,12 +5,17 @@ class EventChannel {
this.id = id;
this.webSocket = webSocket;
this.remoteUrl = buildRemoteUrlFromSocket(webSocket);
this.customClientId = '';
this.eventFilterIds = new Set();
const curTimeMs = Date.now();
this.creationTimeMs = curTimeMs;
this.lastMessagingTimeMs = curTimeMs;
}

setCustomClientId(customClientId) {
this.customClientId = customClientId;
}

setLastMessagingTimeMs(timeMs) {
this.lastMessagingTimeMs = timeMs;
}
@@ -43,7 +48,9 @@ class EventChannel {
return {
id: this.id,
remoteUrl: this.remoteUrl,
customClientId: this.customClientId,
eventFilterIds: [...this.eventFilterIds],
creationTimeMs: this.creationTimeMs,
lastMessagingTimeMs: this.lastMessagingTimeMs,
lifeTimeMs: this.getLifeTimeMs(),
idleTimeMs: this.getIdleTimeMs(),
1 change: 1 addition & 0 deletions event-handler/index.js
Original file line number Diff line number Diff line change
@@ -48,6 +48,7 @@ class EventHandler {
channelStatus: {
maxNumEventChannels: NodeConfigs.MAX_NUM_EVENT_CHANNELS,
numEventChannels: 0,
channelLifeTimeLimitSecs: NodeConfigs.EVENT_HANDLER_CHANNEL_LIFE_TIME_LIMIT_SECS,
channelIdleTimeLimitSecs: NodeConfigs.EVENT_HANDLER_CHANNEL_IDLE_TIME_LIMIT_SECS,
maxChannelLifeTimeMs: 0,
maxChannelIdleTimeMs: 0,
1 change: 1 addition & 0 deletions test/unit/p2p.test.js
Original file line number Diff line number Diff line change
@@ -235,6 +235,7 @@ describe("P2p", () => {
channelStatus: {
maxNumEventChannels: 20,
numEventChannels: 0,
channelLifeTimeLimitSecs: 3600,
channelIdleTimeLimitSecs: 600,
maxChannelIdleTimeMs: 0,
maxChannelLifeTimeMs: 0,

0 comments on commit 0ed88e8

Please sign in to comment.