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

Support SET_CUSTOM_CLIENT_ID event message type #1298

Merged
merged 4 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions blockchain-configs/afan-shard/node_params.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": 40,
"EVENT_HANDLER_PORT": 5100,
"EXPRESS_RATE_LIMIT_WINDOW_SECS": 60,
"FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1,
Expand Down
1 change: 1 addition & 0 deletions blockchain-configs/base/node_params.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": 40,
"EVENT_HANDLER_PORT": 5100,
"EXPRESS_RATE_LIMIT_WINDOW_SECS": 60,
"FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1,
Expand Down
1 change: 1 addition & 0 deletions blockchain-configs/he-shard/node_params.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": 40,
"EVENT_HANDLER_PORT": 5100,
"EXPRESS_RATE_LIMIT_WINDOW_SECS": 60,
"FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1,
Expand Down
1 change: 1 addition & 0 deletions blockchain-configs/mainnet-prod/node_params.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": 40,
"EVENT_HANDLER_PORT": 5100,
"EXPRESS_RATE_LIMIT_WINDOW_SECS": 60,
"FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1,
Expand Down
1 change: 1 addition & 0 deletions blockchain-configs/sim-shard/node_params.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": 40,
"EVENT_HANDLER_PORT": 5100,
"EXPRESS_RATE_LIMIT_WINDOW_SECS": 60,
"FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1,
Expand Down
1 change: 1 addition & 0 deletions blockchain-configs/testnet-dev/node_params.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": 40,
"EVENT_HANDLER_PORT": 5100,
"EXPRESS_RATE_LIMIT_WINDOW_SECS": 60,
"FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1,
Expand Down
1 change: 1 addition & 0 deletions blockchain-configs/testnet-exp/node_params.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": 40,
"EVENT_HANDLER_PORT": 5100,
"EXPRESS_RATE_LIMIT_WINDOW_SECS": 60,
"FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1,
Expand Down
1 change: 1 addition & 0 deletions blockchain-configs/testnet-prod/node_params.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": 40,
"EVENT_HANDLER_PORT": 5100,
"EXPRESS_RATE_LIMIT_WINDOW_SECS": 60,
"FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1,
Expand Down
1 change: 1 addition & 0 deletions blockchain-configs/testnet-sandbox/node_params.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": 40,
"EVENT_HANDLER_PORT": 5100,
"EXPRESS_RATE_LIMIT_WINDOW_SECS": 60,
"FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1,
Expand Down
1 change: 1 addition & 0 deletions blockchain-configs/testnet-staging/node_params.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": 40,
"EVENT_HANDLER_PORT": 5100,
"EXPRESS_RATE_LIMIT_WINDOW_SECS": 60,
"FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1,
Expand Down
1 change: 1 addition & 0 deletions common/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions common/result-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 18 additions & 1 deletion event-handler/event-channel-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -300,7 +317,7 @@ class EventChannelManager {
break;
default:
throw new EventHandlerError(EventHandlerErrorCode.INVALID_MESSAGE_TYPE,
`Invalid message type (${messageType})`);
`Invalid message type: ${messageType}`);
}
}

Expand Down
7 changes: 7 additions & 0 deletions event-handler/event-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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(),
Expand Down
1 change: 1 addition & 0 deletions event-handler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions test/unit/p2p.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ describe("P2p", () => {
channelStatus: {
maxNumEventChannels: 20,
numEventChannels: 0,
channelLifeTimeLimitSecs: 3600,
channelIdleTimeLimitSecs: 600,
maxChannelIdleTimeMs: 0,
maxChannelLifeTimeMs: 0,
Expand Down
Loading