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

feat(node): [NET-1381] Add version to operator heartbeats #2918

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 6 additions & 3 deletions packages/node/src/plugins/operator/heartbeatUtils.ts
Original file line number Diff line number Diff line change
@@ -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']),
Expand All @@ -12,14 +13,16 @@ 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<typeof HeartbeatMessageSchema>

export function createHeartbeatMessage(peerDescriptor: NetworkPeerDescriptor): HeartbeatMessage {
return {
msgType: 'heartbeat',
peerDescriptor
peerDescriptor,
version: applicationVersion
}
}
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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()
Expand Down
18 changes: 17 additions & 1 deletion packages/node/test/unit/plugins/operator/heartbeatUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createHeartbeatMessage, HeartbeatMessageSchema } from '../../../../src/plugins/operator/heartbeatUtils'
import { omit } from 'lodash'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe

import omit from 'lodash/omit'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mondoreale Is this needed anymore when we start to use rollup? We have multiple import { ... } from 'lodash' style imports currently in the codebase. If we change this, should we change all other occurences, too?

import { ZodError } from 'zod'
import { createHeartbeatMessage, HeartbeatMessageSchema } from '../../../../src/plugins/operator/heartbeatUtils'

describe('heartbeatUtils', () => {
it('messages created with createHeartbeatMessage pass validation', () => {
Expand All @@ -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'
)
teogeb marked this conversation as resolved.
Show resolved Hide resolved
expect(() => HeartbeatMessageSchema.parse(msg)).not.toThrow()
})

it('invalid message does not pass validation', () => {
const msg = createHeartbeatMessage({
foo: 'bar'
Expand Down