Skip to content

Commit

Permalink
fix: fixed setting WS_NEW_HEADS_ENABLED true as default logic at Rela…
Browse files Browse the repository at this point in the history
…y level

Signed-off-by: Logan Nguyen <[email protected]>
  • Loading branch information
quiet-node committed May 4, 2024
1 parent bce8278 commit e3dec45
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/relay/src/lib/poller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export class Poller {
this.logger = logger;
this.polls = [];
this.pollingInterval = Number(process.env.WS_POLLING_INTERVAL) || 500;
this.newHeadsEnabled = process.env.WS_NEW_HEADS_ENABLED ? Boolean(Number(process.env.WS_NEW_HEADS_ENABLED)) : true;
this.newHeadsEnabled =
typeof process.env.WS_NEW_HEADS_ENABLED !== 'undefined' ? process.env.WS_NEW_HEADS_ENABLED === 'true' : true;

const activePollsGaugeName = 'rpc_websocket_active_polls';
register.removeSingleMetric(activePollsGaugeName);
Expand Down
4 changes: 2 additions & 2 deletions packages/ws-server/src/controllers/eth_subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ const handleEthSubscribeNewHeads = (
requestIdPrefix: string,
): { response: any; subscriptionId: any } => {
const wsNewHeadsEnabled =
typeof process.env.WS_NEW_HEADS_ENABLED !== 'undefined' ? process.env.WS_NEW_HEADS_ENABLED : 'true';
typeof process.env.WS_NEW_HEADS_ENABLED !== 'undefined' ? process.env.WS_NEW_HEADS_ENABLED === 'true' : true;

if (wsNewHeadsEnabled === 'true') {
if (wsNewHeadsEnabled) {
({ response, subscriptionId } = subscribeToNewHeads(filters, response, subscriptionId, ctx, event, relay, logger));
} else {
logger.warn(
Expand Down
30 changes: 30 additions & 0 deletions packages/ws-server/tests/acceptance/subscribeNewHeads.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,36 @@ describe('@release @web-socket-batch-3 eth_subscribe newHeads', async function (

await new Promise((resolve) => setTimeout(resolve, 500));
});

it('should subscribe to newHeads even when WS_NEW_HEADS_ENABLED=undefined, and receive a valid JSON RPC response', async (done) => {
expect(process.env.WS_NEW_HEADS_ENABLED).to.eq('undefined');

const webSocket = new WebSocket(WS_RELAY_URL);
const subscriptionId = 1;
webSocket.on('open', function open() {
webSocket.send(
JSON.stringify({
id: subscriptionId,
jsonrpc: '2.0',
method: 'eth_subscribe',
params: ['newHeads', { includeTransactions: true }],
}),
);
});

let responseCounter = 0;

Utils.sendTransaction(ONE_TINYBAR, CHAIN_ID, accounts, rpcServer, requestId, mirrorNodeServer);
webSocket.on('message', function incoming(data) {
const response = JSON.parse(data);
responseCounter++;
verifyResponse(response, done, webSocket, true);
if (responseCounter > 1) {
webSocket.close();
}
});
done();
});
});

describe('Subscriptions for newHeads', async function () {
Expand Down

0 comments on commit e3dec45

Please sign in to comment.