-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: add interop tests * chore: update @libp2p/interop version * fix depcheck Co-authored-by: Cayman <[email protected]>
- Loading branch information
1 parent
aa85698
commit 78921b9
Showing
3 changed files
with
138 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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,114 @@ | ||
import { connectInteropTests } from '@libp2p/interop' | ||
import type { SpawnOptions, Daemon, DaemonFactory } from '@libp2p/interop' | ||
import { createServer } from '@libp2p/daemon-server' | ||
import { createClient } from '@libp2p/daemon-client' | ||
import { createLibp2p, Libp2pOptions } from 'libp2p' | ||
import { TCP } from '@libp2p/tcp' | ||
import { Multiaddr } from '@multiformats/multiaddr' | ||
import { path as p2pd } from 'go-libp2p' | ||
import { execa } from 'execa' | ||
import pDefer from 'p-defer' | ||
import { logger } from '@libp2p/logger' | ||
import { Mplex } from '@libp2p/mplex' | ||
import fs from 'fs' | ||
import { unmarshalPrivateKey } from '@libp2p/crypto/keys' | ||
import type { PeerId } from '@libp2p/interface-peer-id' | ||
import { peerIdFromKeys } from '@libp2p/peer-id' | ||
import { Noise } from '../src/index.js' | ||
|
||
async function createGoPeer (options: SpawnOptions): Promise<Daemon> { | ||
const controlPort = Math.floor(Math.random() * (50000 - 10000 + 1)) + 10000 | ||
const apiAddr = new Multiaddr(`/ip4/0.0.0.0/tcp/${controlPort}`) | ||
|
||
const log = logger(`go-libp2p:${controlPort}`) | ||
|
||
const opts = [ | ||
`-listen=${apiAddr.toString()}`, | ||
'-hostAddrs=/ip4/0.0.0.0/tcp/0' | ||
] | ||
|
||
if (options.noise === true) { | ||
opts.push('-noise=true') | ||
} | ||
|
||
if (options.key != null) { | ||
opts.push(`-id=${options.key}`) | ||
} | ||
|
||
const deferred = pDefer() | ||
const proc = execa(p2pd(), opts) | ||
|
||
proc.stdout?.on('data', (buf: Buffer) => { | ||
const str = buf.toString() | ||
log(str) | ||
|
||
// daemon has started | ||
if (str.includes('Control socket:')) { | ||
deferred.resolve() | ||
} | ||
}) | ||
|
||
proc.stderr?.on('data', (buf) => { | ||
log.error(buf.toString()) | ||
}) | ||
|
||
await deferred.promise | ||
|
||
return { | ||
client: createClient(apiAddr), | ||
stop: async () => { | ||
await proc.kill() | ||
} | ||
} | ||
} | ||
|
||
async function createJsPeer (options: SpawnOptions): Promise<Daemon> { | ||
let peerId: PeerId | undefined | ||
|
||
if (options.key != null) { | ||
const keyFile = fs.readFileSync(options.key) | ||
const privateKey = await unmarshalPrivateKey(keyFile) | ||
peerId = await peerIdFromKeys(privateKey.public.bytes, privateKey.bytes) | ||
} | ||
|
||
const opts: Libp2pOptions = { | ||
peerId, | ||
addresses: { | ||
listen: ['/ip4/0.0.0.0/tcp/0'] | ||
}, | ||
transports: [new TCP()], | ||
streamMuxers: [new Mplex()], | ||
connectionEncryption: [new Noise()] | ||
} | ||
|
||
const node = await createLibp2p(opts) | ||
const server = await createServer(new Multiaddr('/ip4/0.0.0.0/tcp/0'), node as any) | ||
await server.start() | ||
|
||
return { | ||
client: createClient(server.getMultiaddr()), | ||
stop: async () => { | ||
await server.stop() | ||
await node.stop() | ||
} | ||
} | ||
} | ||
|
||
async function main (): Promise<void> { | ||
const factory: DaemonFactory = { | ||
async spawn (options: SpawnOptions) { | ||
if (options.type === 'go') { | ||
return await createGoPeer(options) | ||
} | ||
|
||
return await createJsPeer(options) | ||
} | ||
} | ||
|
||
await connectInteropTests(factory) | ||
} | ||
|
||
main().catch(err => { | ||
console.error(err) // eslint-disable-line no-console | ||
process.exit(1) | ||
}) |