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

chore: cherry-picked #2449 #2450

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
33 changes: 32 additions & 1 deletion packages/ws-server/tests/acceptance/subscribeNewHeads.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ describe('@release @web-socket-batch-3 eth_subscribe newHeads', async function (

beforeEach(async () => {
process.env.WS_NEW_HEADS_ENABLED = originalWsNewHeadsEnabledValue;

process.env.WS_SUBSCRIPTION_LIMIT = '10';

wsProvider = await new ethers.WebSocketProvider(WS_RELAY_URL);
Expand Down Expand Up @@ -197,6 +196,38 @@ describe('@release @web-socket-batch-3 eth_subscribe newHeads', async function (
}

await new Promise((resolve) => setTimeout(resolve, 500));
process.env.WS_NEW_HEADS_ENABLED = originalWsNewHeadsEnabledValue;
});

it('should subscribe to newHeads even when WS_NEW_HEADS_ENABLED=undefined, and receive a valid JSON RPC response', async (done) => {
delete process.env.WS_NEW_HEADS_ENABLED;
expect(process.env.WS_NEW_HEADS_ENABLED).to.be.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();
});
});

Expand Down
Loading