This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add peer store/records, and streams are just streams (#160)
The peer store is used by other libp2p components, so split it out from libp2p so those components can break their dependencies on libp2p and still run their tests. Also makes `MuxedStream`s `Stream`s as the muxing should be invisible to the caller.
- Loading branch information
1 parent
fa5ce71
commit 8860a0c
Showing
76 changed files
with
6,798 additions
and
228 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
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 |
---|---|---|
|
@@ -15,6 +15,9 @@ | |
}, | ||
{ | ||
"path": "../libp2p-peer-id-factory" | ||
}, | ||
{ | ||
"path": "../libp2p-interface-compliance-tests" | ||
} | ||
] | ||
} |
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
14 changes: 14 additions & 0 deletions
14
packages/libp2p-interface-compliance-tests/src/utils/mock-connection-gater.ts
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,14 @@ | ||
|
||
export function mockConnectionGater () { | ||
return { | ||
denyDialPeer: async () => await Promise.resolve(false), | ||
denyDialMultiaddr: async () => await Promise.resolve(false), | ||
denyInboundConnection: async () => await Promise.resolve(false), | ||
denyOutboundConnection: async () => await Promise.resolve(false), | ||
denyInboundEncryptedConnection: async () => await Promise.resolve(false), | ||
denyOutboundEncryptedConnection: async () => await Promise.resolve(false), | ||
denyInboundUpgradedConnection: async () => await Promise.resolve(false), | ||
denyOutboundUpgradedConnection: async () => await Promise.resolve(false), | ||
filterMultiaddrForPeer: async () => await Promise.resolve(true) | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
packages/libp2p-interface-compliance-tests/src/utils/mock-connection.ts
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,66 @@ | ||
import { PeerId } from '@libp2p/peer-id' | ||
import { createEd25519PeerId } from '@libp2p/peer-id-factory' | ||
import { pipe } from 'it-pipe' | ||
import type { MultiaddrConnection } from '@libp2p/interfaces/transport' | ||
import type { Connection, Stream, Metadata, ProtocolStream } from '@libp2p/interfaces/connection' | ||
import type { Muxer } from '@libp2p/interfaces/stream-muxer' | ||
|
||
export async function mockConnection (maConn: MultiaddrConnection, direction: 'inbound' | 'outbound', muxer: Muxer): Promise<Connection> { | ||
const remoteAddr = maConn.remoteAddr | ||
const remotePeerIdStr = remoteAddr.getPeerId() | ||
const remotePeer = remotePeerIdStr != null ? PeerId.fromString(remotePeerIdStr) : await createEd25519PeerId() | ||
|
||
const streams: Stream[] = [] | ||
let streamId = 0 | ||
|
||
const registry = new Map() | ||
|
||
void pipe( | ||
maConn, muxer, maConn | ||
) | ||
|
||
return { | ||
id: 'mock-connection', | ||
remoteAddr, | ||
remotePeer, | ||
stat: { | ||
status: 'OPEN', | ||
direction, | ||
timeline: maConn.timeline, | ||
multiplexer: 'test-multiplexer', | ||
encryption: 'yes-yes-very-secure' | ||
}, | ||
registry, | ||
tags: [], | ||
streams, | ||
newStream: async (protocols) => { | ||
if (!Array.isArray(protocols)) { | ||
protocols = [protocols] | ||
} | ||
|
||
if (protocols.length === 0) { | ||
throw new Error('protocols must have a length') | ||
} | ||
|
||
const id = `${streamId++}` | ||
const stream: Stream = muxer.newStream(id) | ||
const streamData: ProtocolStream = { | ||
protocol: protocols[0], | ||
stream | ||
} | ||
|
||
registry.set(id, streamData) | ||
|
||
return streamData | ||
}, | ||
addStream: (stream: Stream, metadata: Metadata) => { | ||
|
||
}, | ||
removeStream: (id: string) => { | ||
registry.delete(id) | ||
}, | ||
close: async () => { | ||
await maConn.close() | ||
} | ||
} | ||
} |
Oops, something went wrong.