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.
fix: simplify transport interface, update interfaces for use with lib…
…p2p (#180) * Ensure implementation of interfaces (e.g. `@libp2p/connection` implements `Connection` now) * Adds mocks for missing components (connection encrypter, etc) * Introduce `Components` class to hold system components that modules require access too - this means system components do not need to depend on `libp2p` any more * Add `Initializable` interface to allow injecting `Components` instance at runtime * Make naming of interfaces less cryptic - e.g. `Muxer` becomes `StreamMuxer`, etc * Introduces peer collections - lists, maps and sets keyed by `PeerId` instead of strings * Adds `.trace` field to `@libp2p/logger` for detailed logging * Pubsub implementations are now expected to serialize their own RPC representations * PeerIds now only stringify to `base58btc`, since a stringified PeerId is a multihash digest encoded in base58btc
- Loading branch information
1 parent
eab83be
commit ec81622
Showing
119 changed files
with
2,752 additions
and
2,432 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
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
110 changes: 110 additions & 0 deletions
110
packages/libp2p-interface-compliance-tests/src/mocks/connection-encrypter.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,110 @@ | ||
import { peerIdFromBytes } from '@libp2p/peer-id' | ||
import { handshake } from 'it-handshake' | ||
import { duplexPair } from 'it-pair/duplex' | ||
import { pipe } from 'it-pipe' | ||
import { UnexpectedPeerError } from '@libp2p/interfaces/connection-encrypter/errors' | ||
import { Multiaddr } from '@multiformats/multiaddr' | ||
import type { ConnectionEncrypter } from '@libp2p/interfaces/connection-encrypter' | ||
import type { Transform, Source } from 'it-stream-types' | ||
|
||
// A basic transform that does nothing to the data | ||
const transform = (): Transform<Uint8Array, Uint8Array> => { | ||
return (source: Source<Uint8Array>) => (async function * () { | ||
for await (const chunk of source) { | ||
yield chunk | ||
} | ||
})() | ||
} | ||
|
||
export function mockConnectionEncrypter () { | ||
const encrypter: ConnectionEncrypter = { | ||
protocol: 'insecure', | ||
secureInbound: async (localPeer, duplex, expectedPeer) => { | ||
// 1. Perform a basic handshake. | ||
const shake = handshake(duplex) | ||
shake.write(localPeer.toBytes()) | ||
const remoteId = await shake.read() | ||
|
||
if (remoteId == null) { | ||
throw new Error('Could not read remote ID') | ||
} | ||
|
||
const remotePeer = peerIdFromBytes(remoteId.slice()) | ||
shake.rest() | ||
|
||
if (expectedPeer != null && !expectedPeer.equals(remotePeer)) { | ||
throw new UnexpectedPeerError() | ||
} | ||
|
||
// 2. Create your encryption box/unbox wrapper | ||
const wrapper = duplexPair<Uint8Array>() | ||
const encrypt = transform() // Use transform iterables to modify data | ||
const decrypt = transform() | ||
|
||
void pipe( | ||
wrapper[0], // We write to wrapper | ||
encrypt, // The data is encrypted | ||
shake.stream, // It goes to the remote peer | ||
decrypt, // Decrypt the incoming data | ||
wrapper[0] // Pipe to the wrapper | ||
) | ||
|
||
return { | ||
conn: { | ||
...wrapper[1], | ||
close: async () => {}, | ||
localAddr: new Multiaddr('/ip4/127.0.0.1/tcp/4001'), | ||
remoteAddr: new Multiaddr('/ip4/127.0.0.1/tcp/4002'), | ||
timeline: { | ||
open: Date.now() | ||
}, | ||
conn: true | ||
}, | ||
remotePeer, | ||
remoteEarlyData: new Uint8Array(0) | ||
} | ||
}, | ||
secureOutbound: async (localPeer, duplex, remotePeer) => { | ||
// 1. Perform a basic handshake. | ||
const shake = handshake(duplex) | ||
shake.write(localPeer.toBytes()) | ||
const remoteId = await shake.read() | ||
|
||
if (remoteId == null) { | ||
throw new Error('Could not read remote ID') | ||
} | ||
|
||
shake.rest() | ||
|
||
// 2. Create your encryption box/unbox wrapper | ||
const wrapper = duplexPair<Uint8Array>() | ||
const encrypt = transform() | ||
const decrypt = transform() | ||
|
||
void pipe( | ||
wrapper[0], // We write to wrapper | ||
encrypt, // The data is encrypted | ||
shake.stream, // It goes to the remote peer | ||
decrypt, // Decrypt the incoming data | ||
wrapper[0] // Pipe to the wrapper | ||
) | ||
|
||
return { | ||
conn: { | ||
...wrapper[1], | ||
close: async () => {}, | ||
localAddr: new Multiaddr('/ip4/127.0.0.1/tcp/4001'), | ||
remoteAddr: new Multiaddr('/ip4/127.0.0.1/tcp/4002'), | ||
timeline: { | ||
open: Date.now() | ||
}, | ||
conn: true | ||
}, | ||
remotePeer: peerIdFromBytes(remoteId.slice()), | ||
remoteEarlyData: new Uint8Array(0) | ||
} | ||
} | ||
} | ||
|
||
return encrypter | ||
} |
18 changes: 15 additions & 3 deletions
18
packages/libp2p-interface-compliance-tests/src/mocks/connection-manager.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
Oops, something went wrong.