Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

Commit

Permalink
fix: rename crypto to connection-encrypter (#179)
Browse files Browse the repository at this point in the history
* Renames interfaces/crypto to interfaces/connection-encrypter because that's what it does and we already have @libp2p/crypto
* Rename *Options to *Init as it's more idiomatic
* `Topology` class isn't exported any more, instead use `createTopology` to get an instance of the interface
  • Loading branch information
achingbrain authored Feb 27, 2022
1 parent 4a6f06f commit d197f55
Show file tree
Hide file tree
Showing 48 changed files with 191 additions and 200 deletions.
6 changes: 3 additions & 3 deletions packages/libp2p-connection/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,13 @@
"test:electron-main": "npm run test -- -t electron-main"
},
"dependencies": {
"@libp2p/interfaces": "^1.3.6",
"@libp2p/interfaces": "^1.3.12",
"@multiformats/multiaddr": "^10.1.5",
"err-code": "^3.0.1"
},
"devDependencies": {
"@libp2p/interface-compliance-tests": "^1.1.2",
"@libp2p/peer-id-factory": "^1.0.5",
"@libp2p/interface-compliance-tests": "^1.1.14",
"@libp2p/peer-id-factory": "^1.0.6",
"aegir": "^36.1.3",
"it-pair": "^2.0.2"
}
Expand Down
6 changes: 3 additions & 3 deletions packages/libp2p-connection/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { PeerId } from '@libp2p/interfaces/peer-id'

const connectionSymbol = Symbol.for('@libp2p/interface-connection/connection')

interface ConnectionOptions {
interface ConnectionInit {
localAddr: Multiaddr
remoteAddr: Multiaddr
localPeer: PeerId
Expand Down Expand Up @@ -74,8 +74,8 @@ export class Connection {
* An implementation of the js-libp2p connection.
* Any libp2p transport should use an upgrader to return this connection.
*/
constructor (options: ConnectionOptions) {
const { localAddr, remoteAddr, localPeer, remotePeer, newStream, close, getStreams, stat } = options
constructor (init: ConnectionInit) {
const { localAddr, remoteAddr, localPeer, remotePeer, newStream, close, getStreams, stat } = init

this.id = `${(parseInt(String(Math.random() * 1e9))).toString(36)}${Date.now()}`
this.localAddr = localAddr
Expand Down
18 changes: 9 additions & 9 deletions packages/libp2p-interface-compliance-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
"import": "./dist/src/connection/index.js",
"types": "./dist/src/connection/index.d.ts"
},
"./crypto": {
"import": "./dist/src/crypto/index.js",
"types": "./dist/src/crypto/index.d.ts"
"./connection-encrypter": {
"import": "./dist/src/connection-encrypter/index.js",
"types": "./dist/src/connection-encrypter/index.d.ts"
},
"./mocks": {
"import": "./dist/src/mocks/index.js",
Expand Down Expand Up @@ -199,12 +199,12 @@
},
"dependencies": {
"@libp2p/crypto": "^0.22.7",
"@libp2p/interfaces": "^1.3.6",
"@libp2p/logger": "^1.0.3",
"@libp2p/multistream-select": "^1.0.0",
"@libp2p/peer-id": "^1.0.4",
"@libp2p/peer-id-factory": "^1.0.0",
"@libp2p/pubsub": "^1.1.0",
"@libp2p/interfaces": "^1.3.12",
"@libp2p/logger": "^1.1.0",
"@libp2p/multistream-select": "^1.0.1",
"@libp2p/peer-id": "^1.1.6",
"@libp2p/peer-id-factory": "^1.0.6",
"@libp2p/pubsub": "^1.2.8",
"@multiformats/multiaddr": "^10.1.5",
"abortable-iterator": "^4.0.2",
"aegir": "^36.1.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import * as PeerIdFactory from '@libp2p/peer-id-factory'
import all from 'it-all'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import peers from '../utils/peers.js'
import { UnexpectedPeerError } from '@libp2p/interfaces/crypto/errors'
import { UnexpectedPeerError } from '@libp2p/interfaces/connection-encrypter/errors'
import { createMaConnPair } from './utils/index.js'
import type { TestSetup } from '../index.js'
import type { Crypto } from '@libp2p/interfaces/crypto'
import type { Encrypter } from '@libp2p/interfaces/connection-encrypter'
import type { PeerId } from '@libp2p/interfaces/peer-id'
import type { Source } from 'it-stream-types'

export default (common: TestSetup<Crypto>) => {
describe('interface-crypto compliance tests', () => {
let crypto: Crypto
export default (common: TestSetup<Encrypter>) => {
describe('interface-connection-encrypter compliance tests', () => {
let crypto: Encrypter
let localPeer: PeerId
let remotePeer: PeerId
let mitmPeer: PeerId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ export { mockMultiaddrConnection } from './multiaddr-connection.js'
export { mockMuxer } from './muxer.js'
export { mockRegistrar } from './registrar.js'
export { mockUpgrader } from './upgrader.js'
export type { MockUpgraderOptions } from './upgrader.js'
export type { MockUpgraderInit } from './upgrader.js'
16 changes: 8 additions & 8 deletions packages/libp2p-interface-compliance-tests/src/mocks/muxer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import errCode from 'err-code'
import { Logger, logger } from '@libp2p/logger'
import * as ndjson from 'it-ndjson'
import type { Stream } from '@libp2p/interfaces/connection'
import type { Muxer, MuxerOptions } from '@libp2p/interfaces/stream-muxer'
import type { Muxer, MuxerInit } from '@libp2p/interfaces/stream-muxer'
import type { Source } from 'it-stream-types'
import { pipe } from 'it-pipe'
import map from 'it-map'
Expand Down Expand Up @@ -54,8 +54,8 @@ class MuxedStream {
private readonly resetController: AbortController
private readonly log: Logger

constructor (opts: { id: string, type: 'initiator' | 'recipient', push: Pushable<StreamMessage>, onEnd: (err?: Error) => void }) {
const { id, type, push, onEnd } = opts
constructor (init: { id: string, type: 'initiator' | 'recipient', push: Pushable<StreamMessage>, onEnd: (err?: Error) => void }) {
const { id, type, push, onEnd } = init

this.log = logger(`libp2p:mock-muxer:stream:${id}:${type}`)

Expand Down Expand Up @@ -224,17 +224,17 @@ class MockMuxer implements Muxer {

private readonly registryInitiatorStreams: Map<string, MuxedStream>
private readonly registryRecipientStreams: Map<string, MuxedStream>
private readonly options: MuxerOptions
private readonly options: MuxerInit

private readonly log: Logger

constructor (options?: MuxerOptions) {
constructor (init?: MuxerInit) {
this.name = `muxer:${muxers++}`
this.log = logger(`libp2p:mock-muxer:${this.name}`)
this.registryInitiatorStreams = new Map()
this.registryRecipientStreams = new Map()
this.log('create muxer')
this.options = options ?? {}
this.options = init ?? {}
// receives data from the muxer at the other end of the stream
this.source = this.input = pushable<Uint8Array>({
onEnd: (err) => {
Expand Down Expand Up @@ -349,8 +349,8 @@ class MockMuxer implements Muxer {
}
}

export function mockMuxer (options?: MuxerOptions): Muxer {
const mockMuxer = new MockMuxer(options)
export function mockMuxer (init?: MuxerInit): Muxer {
const mockMuxer = new MockMuxer(init)

void Promise.resolve().then(async () => {
void pipe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { mockConnection } from './connection.js'
import type { Upgrader, MultiaddrConnection } from '@libp2p/interfaces/transport'
import type { Registrar } from '@libp2p/interfaces/registrar'

export interface MockUpgraderOptions {
export interface MockUpgraderInit {
registrar?: Registrar
}

export function mockUpgrader (options: MockUpgraderOptions = {}) {
export function mockUpgrader (init: MockUpgraderInit = {}) {
const ensureProps = (multiaddrConnection: MultiaddrConnection) => {
['sink', 'source', 'remoteAddr', 'timeline', 'close'].forEach(prop => {
expect(multiaddrConnection).to.have.property(prop)
Expand All @@ -20,14 +20,14 @@ export function mockUpgrader (options: MockUpgraderOptions = {}) {
ensureProps(multiaddrConnection)
return mockConnection(multiaddrConnection, {
direction: 'outbound',
registrar: options.registrar
registrar: init.registrar
})
},
async upgradeInbound (multiaddrConnection) {
ensureProps(multiaddrConnection)
return mockConnection(multiaddrConnection, {
direction: 'inbound',
registrar: options.registrar
registrar: init.registrar
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import { isValidTick } from '../utils/is-valid-tick.js'
import type { DeferredPromise } from 'p-defer'
import type { TestSetup } from '../index.js'
import type { Stream } from '@libp2p/interfaces/connection'
import type { Muxer, MuxerOptions } from '@libp2p/interfaces/stream-muxer'
import type { Muxer, MuxerInit } from '@libp2p/interfaces/stream-muxer'
import type { Source, Duplex } from 'it-stream-types'

async function drainAndClose (stream: Duplex<Uint8Array>) {
return await pipe([], stream, drain)
}

export default (common: TestSetup<Muxer, MuxerOptions>) => {
export default (common: TestSetup<Muxer, MuxerInit>) => {
describe('base', () => {
it('Open a stream from the dialer', async () => {
const p = duplexPair<Uint8Array>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import drain from 'it-drain'
import { expect } from 'aegir/utils/chai.js'
import delay from 'delay'
import type { TestSetup } from '../index.js'
import type { Muxer, MuxerOptions } from '@libp2p/interfaces/stream-muxer'
import type { Muxer, MuxerInit } from '@libp2p/interfaces/stream-muxer'

function randomBuffer () {
return uint8ArrayFromString(Math.random().toString())
Expand All @@ -22,7 +22,7 @@ const infiniteRandom = {
}
}

export default (common: TestSetup<Muxer, MuxerOptions>) => {
export default (common: TestSetup<Muxer, MuxerInit>) => {
describe('close', () => {
it('closing underlying socket closes streams', async () => {
let openedStreams = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import closeTest from './close-test.js'
import stressTest from './stress-test.js'
import megaStressTest from './mega-stress-test.js'
import type { TestSetup } from '../index.js'
import type { Muxer, MuxerOptions } from '@libp2p/interfaces/stream-muxer'
import type { Muxer, MuxerInit } from '@libp2p/interfaces/stream-muxer'

export default (common: TestSetup<Muxer, MuxerOptions>) => {
export default (common: TestSetup<Muxer, MuxerInit>) => {
describe('interface-stream-muxer', () => {
baseTest(common)
closeTest(common)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import spawn from './spawner.js'
import type { TestSetup } from '../index.js'
import type { Muxer, MuxerOptions } from '@libp2p/interfaces/stream-muxer'
import type { Muxer, MuxerInit } from '@libp2p/interfaces/stream-muxer'

export default (common: TestSetup<Muxer, MuxerOptions>) => {
const createMuxer = async (opts?: MuxerOptions) => await common.setup(opts)
export default (common: TestSetup<Muxer, MuxerInit>) => {
const createMuxer = async (init?: MuxerInit) => await common.setup(init)

describe.skip('mega stress test', function () {
it('10,000 streams with 10,000 msg', async () => await spawn(createMuxer, 10000, 10000, 5000))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import pLimit from 'p-limit'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import drain from 'it-drain'
import all from 'it-all'
import type { Muxer, MuxerOptions } from '@libp2p/interfaces/stream-muxer'
import type { Muxer, MuxerInit } from '@libp2p/interfaces/stream-muxer'

export default async (createMuxer: (options?: MuxerOptions) => Promise<Muxer>, nStreams: number, nMsg: number, limit?: number) => {
export default async (createMuxer: (init?: MuxerInit) => Promise<Muxer>, nStreams: number, nMsg: number, limit?: number) => {
const [dialerSocket, listenerSocket] = duplexPair<Uint8Array>()

const msg = uint8ArrayFromString('simple msg')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import spawn from './spawner.js'
import type { TestSetup } from '../index.js'
import type { Muxer, MuxerOptions } from '@libp2p/interfaces/stream-muxer'
import type { Muxer, MuxerInit } from '@libp2p/interfaces/stream-muxer'

export default (common: TestSetup<Muxer, MuxerOptions>) => {
const createMuxer = async (opts?: MuxerOptions) => await common.setup(opts)
export default (common: TestSetup<Muxer, MuxerInit>) => {
const createMuxer = async (init?: MuxerInit) => await common.setup(init)

describe('stress test', () => {
it('1 stream with 1 msg', async () => await spawn(createMuxer, 1, 1))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import tests from '../../src/crypto/index.js'
import tests from '../../src/connection-encrypter/index.js'
import mockCrypto from './mock-crypto.js'

describe('compliance tests', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ 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/crypto/errors'
import { UnexpectedPeerError } from '@libp2p/interfaces/connection-encrypter/errors'
import { Multiaddr } from '@multiformats/multiaddr'
import type { Crypto } from '@libp2p/interfaces/crypto'
import type { Encrypter } from '@libp2p/interfaces/connection-encrypter'
import type { Transform, Source } from 'it-stream-types'

// A basic transform that does nothing to the data
Expand All @@ -16,7 +16,7 @@ const transform = (): Transform<Uint8Array, Uint8Array> => {
})()
}

const crypto: Crypto = {
const crypto: Encrypter = {
protocol: 'insecure',
secureInbound: async (localPeer, duplex, expectedPeer) => {
// 1. Perform a basic handshake.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { mockMuxer } from '../../src/mocks/muxer.js'

describe('compliance tests', () => {
tests({
async setup (options) {
return mockMuxer(options)
async setup (init) {
return mockMuxer(init)
},
async teardown () {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ import * as PeerIdFactory from '@libp2p/peer-id-factory'
import { EventEmitter, CustomEvent } from '@libp2p/interfaces'
import type { PeerDiscovery, PeerDiscoveryEvents } from '@libp2p/interfaces/peer-discovery'

interface MockDiscoveryOptions {
interface MockDiscoveryInit {
discoveryDelay?: number
}

/**
* Emits 'peer' events on discovery.
*/
export class MockDiscovery extends EventEmitter<PeerDiscoveryEvents> implements PeerDiscovery {
public readonly options: MockDiscoveryOptions
public readonly options: MockDiscoveryInit
private _isRunning: boolean
private _timer: any

constructor (options = {}) {
constructor (init = {}) {
super()

this.options = options
this.options = init
this._isRunning = false
}

Expand Down
16 changes: 8 additions & 8 deletions packages/libp2p-interfaces/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@
"import": "./dist/src/connection/index.js",
"types": "./dist/src/connection/index.d.ts"
},
"./connection-encrypter": {
"import": "./dist/src/connection-encrypter/index.js",
"types": "./dist/src/connection-encrypter/index.d.ts"
},
"./connection-encrypter/errors": {
"import": "./dist/src/connection-encrypter/errors.js",
"types": "./dist/src/connection-encrypter/errors.d.ts"
},
"./connection/status": {
"import": "./dist/src/connection/status.js",
"types": "./dist/src/connection/status.d.ts"
Expand All @@ -60,14 +68,6 @@
"import": "./dist/src/content-routing/index.js",
"types": "./dist/src/content-routing/index.d.ts"
},
"./crypto": {
"import": "./dist/src/crypto/index.js",
"types": "./dist/src/crypto/index.d.ts"
},
"./crypto/errors": {
"import": "./dist/src/crypto/errors.js",
"types": "./dist/src/crypto/errors.d.ts"
},
"./dht": {
"import": "./dist/src/dht/index.js",
"types": "./dist/src/dht/index.d.ts"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
interface-crypto
interface-connection-encrypter
==================

> A test suite you can use to implement a libp2p crypto module. A libp2p crypto module is used to ensure all exchanged data between two peers is encrypted.
Expand All @@ -9,7 +9,7 @@ interface-crypto
- [js-libp2p-secio](https://github.com/NodeFactoryIo/js-libp2p-secio)

## Table of Contents
- [interface-crypto](#interface-crypto)
- [interface-connection-encrypter](#interface-connection-encrypter)
- [Table of Contents](#table-of-contents)
- [Using the Test Suite](#using-the-test-suite)
- [API](#api)
Expand All @@ -23,8 +23,8 @@ interface-crypto
You can also check out the [internal test suite](../../test/crypto/compliance.spec.js) to see the setup in action.

```js
const tests = require('libp2p-interfaces-compliance-tests/crypto')
const yourCrypto = require('./your-crypto')
const tests = require('libp2p-interfaces-compliance-tests/connection-encrypter')
const yourCrypto = require('./your-encrypter')

tests({
setup () {
Expand Down
Loading

0 comments on commit d197f55

Please sign in to comment.