From 626b5c22ae64024a4909b4c58cfa0f5e123c71de Mon Sep 17 00:00:00 2001 From: Teo Gebhard Date: Thu, 28 Nov 2024 14:34:59 +0200 Subject: [PATCH] add version field --- .../src/plugins/operator/heartbeatUtils.ts | 9 ++++++--- .../operator/announceNodeToStream.test.ts | 4 +++- .../plugins/operator/heartbeatUtils.test.ts | 18 +++++++++++++++++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/packages/node/src/plugins/operator/heartbeatUtils.ts b/packages/node/src/plugins/operator/heartbeatUtils.ts index 44891469b4..fef66562b2 100644 --- a/packages/node/src/plugins/operator/heartbeatUtils.ts +++ b/packages/node/src/plugins/operator/heartbeatUtils.ts @@ -1,5 +1,6 @@ -import { z } from 'zod' import { NetworkNodeType, NetworkPeerDescriptor } from '@streamr/sdk' +import { z } from 'zod' +import { version as applicationVersion } from '../../../package.json' export const HeartbeatMessageSchema = z.object({ msgType: z.enum(['heartbeat']), @@ -12,7 +13,8 @@ export const HeartbeatMessageSchema = z.object({ tls: z.boolean() })), region: z.optional(z.number()) - }) + }), + version: z.optional(z.string()) // optional for backward compatibility (written from v102 onward) }) export type HeartbeatMessage = z.infer @@ -20,6 +22,7 @@ export type HeartbeatMessage = z.infer export function createHeartbeatMessage(peerDescriptor: NetworkPeerDescriptor): HeartbeatMessage { return { msgType: 'heartbeat', - peerDescriptor + peerDescriptor, + version: applicationVersion } } diff --git a/packages/node/test/integration/plugins/operator/announceNodeToStream.test.ts b/packages/node/test/integration/plugins/operator/announceNodeToStream.test.ts index fa37e8dc2a..9552bf7afa 100644 --- a/packages/node/test/integration/plugins/operator/announceNodeToStream.test.ts +++ b/packages/node/test/integration/plugins/operator/announceNodeToStream.test.ts @@ -1,6 +1,7 @@ import { _operatorContractUtils } from '@streamr/sdk' import { fastPrivateKey } from '@streamr/test-utils' import { collect, toEthereumAddress } from '@streamr/utils' +import { version as applicationVersion } from '../../../../package.json' import { announceNodeToStream } from '../../../../src/plugins/operator/announceNodeToStream' import { formCoordinationStreamId } from '../../../../src/plugins/operator/formCoordinationStreamId' import { createClient } from '../../../utils' @@ -25,7 +26,8 @@ describe('announceNodeToStream', () => { const [{ content }] = await collect(subscription, 1) expect(content).toEqual({ msgType: 'heartbeat', - peerDescriptor: await client.getPeerDescriptor() + peerDescriptor: await client.getPeerDescriptor(), + version: applicationVersion }) await anonymousClient.destroy() diff --git a/packages/node/test/unit/plugins/operator/heartbeatUtils.test.ts b/packages/node/test/unit/plugins/operator/heartbeatUtils.test.ts index 0c6cc34a0b..13b1d5631a 100644 --- a/packages/node/test/unit/plugins/operator/heartbeatUtils.test.ts +++ b/packages/node/test/unit/plugins/operator/heartbeatUtils.test.ts @@ -1,5 +1,6 @@ -import { createHeartbeatMessage, HeartbeatMessageSchema } from '../../../../src/plugins/operator/heartbeatUtils' +import { omit } from 'lodash' import { ZodError } from 'zod' +import { createHeartbeatMessage, HeartbeatMessageSchema } from '../../../../src/plugins/operator/heartbeatUtils' describe('heartbeatUtils', () => { it('messages created with createHeartbeatMessage pass validation', () => { @@ -14,6 +15,21 @@ describe('heartbeatUtils', () => { expect(() => HeartbeatMessageSchema.parse(msg)).not.toThrow() }) + it('messages without version pass validation', () => { + const msg = omit( + createHeartbeatMessage({ + nodeId: 'nodeId', + websocket: { + port: 31313, + host: '127.0.0.1', + tls: false + } + }), + 'version' + ) + expect(() => HeartbeatMessageSchema.parse(msg)).not.toThrow() + }) + it('invalid message does not pass validation', () => { const msg = createHeartbeatMessage({ foo: 'bar'