-
Notifications
You must be signed in to change notification settings - Fork 5
feat(ocap-kernel): leverage libp2p v3 features in remote communications #914
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
289 changes: 289 additions & 0 deletions
289
packages/kernel-node-runtime/test/e2e/libp2p-v3-features.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,289 @@ | ||
| import type { Libp2p } from '@libp2p/interface'; | ||
| import { makeSQLKernelDatabase } from '@metamask/kernel-store/sqlite/nodejs'; | ||
| import { startRelay } from '@metamask/kernel-utils/libp2p'; | ||
| import { Kernel, kunser, makeKernelStore } from '@metamask/ocap-kernel'; | ||
| import { delay } from '@ocap/repo-tools/test-utils'; | ||
| import { mkdtemp, rm } from 'node:fs/promises'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { join } from 'node:path'; | ||
| import { | ||
| describe, | ||
| it, | ||
| expect, | ||
| beforeAll, | ||
| afterAll, | ||
| beforeEach, | ||
| afterEach, | ||
| } from 'vitest'; | ||
|
|
||
| import { makeTestKernel } from '../helpers/kernel.ts'; | ||
| import { | ||
| makeRemoteVatConfig, | ||
| restartKernelAndReloadVat, | ||
| sendRemoteMessage, | ||
| setupAliceAndBob, | ||
| } from '../helpers/remote-comms.ts'; | ||
| import { stopWithTimeout } from '../helpers/stop-with-timeout.ts'; | ||
|
|
||
| const NETWORK_TIMEOUT = 30_000; | ||
| const relayPeerId = '12D3KooWJBDqsyHQF2MWiCdU4kdqx4zTsSTLRdShg7Ui6CRWB4uc'; | ||
| const testRelays = [`/ip4/127.0.0.1/tcp/9001/ws/p2p/${relayPeerId}`]; | ||
|
|
||
| const testBackoffOptions = { | ||
| reconnectionBaseDelayMs: 10, | ||
| reconnectionMaxDelayMs: 50, | ||
| handshakeTimeoutMs: 3_000, | ||
| writeTimeoutMs: 3_000, | ||
| ackTimeoutMs: 2_000, | ||
| }; | ||
|
|
||
| describe.sequential('libp2p v3 Features E2E', () => { | ||
| let relay: Libp2p; | ||
| let kernel1: Kernel; | ||
| let kernel2: Kernel; | ||
| let dbFilename1: string; | ||
| let dbFilename2: string; | ||
| let tempDir: string; | ||
| let kernelStore1: ReturnType<typeof makeKernelStore>; | ||
| let kernelStore2: ReturnType<typeof makeKernelStore>; | ||
|
|
||
| beforeAll(async () => { | ||
| relay = await startRelay(console); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| if (relay) { | ||
| await relay.stop(); | ||
| } | ||
| }); | ||
|
|
||
| beforeEach(async () => { | ||
| tempDir = await mkdtemp(join(tmpdir(), 'ocap-v3-')); | ||
| dbFilename1 = join(tempDir, 'kernel1.db'); | ||
| dbFilename2 = join(tempDir, 'kernel2.db'); | ||
|
|
||
| const kernelDatabase1 = await makeSQLKernelDatabase({ | ||
| dbFilename: dbFilename1, | ||
| }); | ||
| kernelStore1 = makeKernelStore(kernelDatabase1); | ||
|
|
||
| const kernelDatabase2 = await makeSQLKernelDatabase({ | ||
| dbFilename: dbFilename2, | ||
| }); | ||
| kernelStore2 = makeKernelStore(kernelDatabase2); | ||
|
|
||
| kernel1 = await makeTestKernel(kernelDatabase1); | ||
| kernel2 = await makeTestKernel(kernelDatabase2); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| const STOP_TIMEOUT = 3000; | ||
| await Promise.all([ | ||
| kernel1 && | ||
| stopWithTimeout( | ||
| async () => kernel1.stop(), | ||
| STOP_TIMEOUT, | ||
| 'kernel1.stop', | ||
| ), | ||
| kernel2 && | ||
| stopWithTimeout( | ||
| async () => kernel2.stop(), | ||
| STOP_TIMEOUT, | ||
| 'kernel2.stop', | ||
| ), | ||
| ]); | ||
| if (tempDir) { | ||
| await rm(tempDir, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| describe('peer:disconnect Reconnection', () => { | ||
| it( | ||
| 'recovers queued message after peer:disconnect triggers reconnection', | ||
| async () => { | ||
| const { aliceRef, bobURL } = await setupAliceAndBob( | ||
| kernel1, | ||
| kernel2, | ||
| kernelStore1, | ||
| kernelStore2, | ||
| testRelays, | ||
| testBackoffOptions, | ||
| ); | ||
|
|
||
| // Establish initial communication | ||
| const initial = await sendRemoteMessage( | ||
| kernel1, | ||
| aliceRef, | ||
| bobURL, | ||
| 'hello', | ||
| ['Alice'], | ||
| ); | ||
| expect(initial).toContain('vat Bob got "hello" from Alice'); | ||
|
|
||
| // Stop kernel2 — triggers both readChannel error and peer:disconnect. | ||
| // The peer:disconnect event acts as a safety net ensuring reconnection | ||
| // is attempted even after readChannel clears the channel. | ||
| await kernel2.stop(); | ||
|
|
||
| // Queue a message while kernel2 is down — this triggers reconnection | ||
| const recoveryPromise = kernel1.queueMessage( | ||
| aliceRef, | ||
| 'sendRemoteMessage', | ||
| [bobURL, 'hello', ['Alice']], | ||
| ); | ||
|
|
||
| // Restart kernel2 — reconnection loop delivers the queued message | ||
| const bobConfig = makeRemoteVatConfig('Bob'); | ||
| const restartResult = await restartKernelAndReloadVat( | ||
| dbFilename2, | ||
| false, | ||
| testRelays, | ||
| bobConfig, | ||
| testBackoffOptions, | ||
| ); | ||
| // eslint-disable-next-line require-atomic-updates | ||
| kernel2 = restartResult.kernel; | ||
|
|
||
| const result = kunser(await recoveryPromise) as string; | ||
| expect(result).toContain('vat Bob got "hello" from Alice'); | ||
|
|
||
| // Verify ongoing connectivity after peer:disconnect recovery | ||
| const followUp = await sendRemoteMessage( | ||
| kernel1, | ||
| aliceRef, | ||
| bobURL, | ||
| 'hello', | ||
| ['Alice'], | ||
| ); | ||
| expect(followUp).toContain('vat Bob got "hello" from Alice'); | ||
| }, | ||
| NETWORK_TIMEOUT * 2, | ||
| ); | ||
| }); | ||
|
|
||
| describe('Stream Inactivity Timeout', () => { | ||
| it( | ||
| 'recovers communication after idle period exceeds inactivity timeout', | ||
| async () => { | ||
| // Use a short inactivity timeout to test the auto-abort behavior. | ||
| const shortTimeoutOptions = { | ||
| ...testBackoffOptions, | ||
| streamInactivityTimeoutMs: 2_000, | ||
| }; | ||
|
|
||
| const { aliceRef, bobURL } = await setupAliceAndBob( | ||
| kernel1, | ||
| kernel2, | ||
| kernelStore1, | ||
| kernelStore2, | ||
| testRelays, | ||
| shortTimeoutOptions, | ||
| ); | ||
|
|
||
| // Establish initial communication | ||
| const initial = await sendRemoteMessage( | ||
| kernel1, | ||
| aliceRef, | ||
| bobURL, | ||
| 'hello', | ||
| ['Alice'], | ||
| ); | ||
| expect(initial).toContain('vat Bob got "hello" from Alice'); | ||
|
|
||
| // Wait longer than the inactivity timeout (2s + buffer). | ||
| // The stream should auto-abort due to inactivityTimeout, | ||
| // triggering connection loss handling and reconnection. | ||
| await delay(4_000); | ||
|
|
||
| // Send another message — the transport layer should | ||
| // reconnect since the previous stream was aborted by inactivity. | ||
| const afterIdle = await sendRemoteMessage( | ||
| kernel1, | ||
| aliceRef, | ||
| bobURL, | ||
| 'hello', | ||
| ['Alice'], | ||
| ); | ||
| expect(afterIdle).toContain('vat Bob got "hello" from Alice'); | ||
| }, | ||
| NETWORK_TIMEOUT * 2, | ||
| ); | ||
| }); | ||
|
|
||
| describe('Fast Failure on Closed Streams', () => { | ||
| it( | ||
| 'handles rapid sends during disconnect without hanging', | ||
| async () => { | ||
| const { aliceRef, bobURL, peerId2 } = await setupAliceAndBob( | ||
| kernel1, | ||
| kernel2, | ||
| kernelStore1, | ||
| kernelStore2, | ||
| testRelays, | ||
| testBackoffOptions, | ||
| ); | ||
|
|
||
| // Establish initial connectivity | ||
| await sendRemoteMessage(kernel1, aliceRef, bobURL, 'hello', ['Alice']); | ||
|
|
||
| // Intentionally close the connection — writes should fail fast | ||
| // via stream.status check rather than waiting for timeout | ||
| await kernel1.closeConnection(peerId2); | ||
|
|
||
| const start = Date.now(); | ||
| // This should fail quickly (stream.status !== 'open') rather than | ||
| // waiting for the full write timeout (3000ms) | ||
| await expect( | ||
| kernel1.queueMessage(aliceRef, 'sendRemoteMessage', [ | ||
| bobURL, | ||
| 'hello', | ||
| ['Alice'], | ||
| ]), | ||
| ).rejects.toMatchObject({ | ||
| body: expect.stringContaining( | ||
| 'Message delivery failed after intentional close', | ||
| ), | ||
| }); | ||
| const elapsed = Date.now() - start; | ||
|
|
||
| // Should fail well under the write timeout | ||
| expect(elapsed).toBeLessThan(testBackoffOptions.writeTimeoutMs); | ||
| }, | ||
| NETWORK_TIMEOUT, | ||
| ); | ||
| }); | ||
|
|
||
| describe('Connection Type Awareness', () => { | ||
| it( | ||
| 'establishes relayed connections and communicates successfully', | ||
| async () => { | ||
| // Both kernels connect via relay — the connection.direct property | ||
| // should be false (relayed), and the log should include "relayed". | ||
| // This validates that the connection type detection works with real | ||
| // libp2p connections. | ||
| const { aliceRef, bobURL, peerId1, peerId2 } = await setupAliceAndBob( | ||
| kernel1, | ||
| kernel2, | ||
| kernelStore1, | ||
| kernelStore2, | ||
| testRelays, | ||
| testBackoffOptions, | ||
| ); | ||
|
|
||
| // Verify distinct peer IDs (confirms real libp2p nodes) | ||
| expect(peerId1).not.toBe(peerId2); | ||
|
|
||
| // Bidirectional communication through relay | ||
| const response = await sendRemoteMessage( | ||
| kernel1, | ||
| aliceRef, | ||
| bobURL, | ||
| 'hello', | ||
| ['Alice'], | ||
| ); | ||
| expect(response).toContain('vat Bob got "hello" from Alice'); | ||
| }, | ||
| NETWORK_TIMEOUT, | ||
| ); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E2E test delay shorter than clamped inactivity timeout
Medium Severity
The test sets
streamInactivityTimeoutMs: 2_000and waitsdelay(4_000), expecting the stream to have auto-aborted. However,registerChannelclamps the value viaMath.max(streamInactivityTimeoutMs, MIN_STREAM_INACTIVITY_TIMEOUT_MS), which evaluates toMath.max(2000, 5000) = 5000. Since 4s < 5s, the stream hasn't timed out yet, so the test succeeds on the existing connection without ever triggering inactivity-based reconnection. The test passes but doesn't exercise the intended code path.Additional Locations (2)
packages/ocap-kernel/src/remotes/platform/transport.ts#L287-L291packages/ocap-kernel/src/remotes/platform/constants.ts#L46-L47