Skip to content

Commit

Permalink
refactor!: remove isStarted method from Startable (#2145)
Browse files Browse the repository at this point in the history
We have an `isStarted` method on the `Startable` interface but we only
really use it in tests. Our implementations tend to guard on being
started twice so it just adds noise to every implementation.

BREAKING CHANGE: the `isStarted` method has been removed from the `Startable` interface
  • Loading branch information
achingbrain committed Nov 8, 2023
1 parent 9fd70ff commit 192d082
Show file tree
Hide file tree
Showing 28 changed files with 461 additions and 346 deletions.
17 changes: 17 additions & 0 deletions doc/migrations/v0.46-v1.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ A migration guide for refactoring your application code from libp2p `v0.46` to `

- [AutoNAT](#autonat)
- [KeyChain](#keychain)
- [Pnet](#pnet)
- [Metrics](#metrics)

## AutoNAT
Expand Down Expand Up @@ -68,6 +69,22 @@ const libp2p = await createLibp2p({
const keychain: Keychain = libp2p.services.keychain
```

## Pnet

The pnet module is now published in its own package.

**Before**

```ts
import { preSharedKey, generateKey } from 'libp2p/pnet'
```

**After**

```ts
import { preSharedKey, generateKey } from '@libp2p/pnet'
```

## Metrics

The following metrics were renamed:
Expand Down
2 changes: 0 additions & 2 deletions packages/interface-compliance-tests/src/pubsub/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export default (common: TestSetup<PubSub, PubSubArgs>): void => {

await start(...Object.values(components))

expect(pubsub.isStarted()).to.equal(true)
expect(components.registrar.register).to.have.property('callCount', 1)
})

Expand All @@ -62,7 +61,6 @@ export default (common: TestSetup<PubSub, PubSubArgs>): void => {
await start(...Object.values(components))
await stop(...Object.values(components))

expect(pubsub.isStarted()).to.equal(false)
expect(components.registrar.unregister).to.have.property('callCount', 1)
})

Expand Down
2 changes: 0 additions & 2 deletions packages/interface/src/startable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
* Implemented by components that have a lifecycle
*/
export interface Startable {
isStarted(): boolean

/**
* If implemented, this method will be invoked before the start method.
*
Expand Down
8 changes: 1 addition & 7 deletions packages/libp2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@
"types": "./dist/src/ping/index.d.ts",
"import": "./dist/src/ping/index.js"
},
"./pnet": {
"types": "./dist/src/pnet/index.d.ts",
"import": "./dist/src/pnet/index.js"
},
"./upnp-nat": {
"types": "./dist/src/upnp-nat/index.d.ts",
"import": "./dist/src/upnp-nat/index.js"
Expand Down Expand Up @@ -156,8 +152,7 @@
"rate-limiter-flexible": "^3.0.0",
"uint8arraylist": "^2.4.3",
"uint8arrays": "^4.0.6",
"wherearewe": "^2.0.1",
"xsalsa20": "^1.1.0"
"wherearewe": "^2.0.1"
},
"devDependencies": {
"@chainsafe/libp2p-gossipsub": "^10.0.0",
Expand All @@ -174,7 +169,6 @@
"@libp2p/mplex": "^9.0.12",
"@libp2p/tcp": "^8.0.13",
"@libp2p/websockets": "^7.0.13",
"@types/xsalsa20": "^1.1.0",
"aegir": "^41.0.2",
"execa": "^8.0.1",
"go-libp2p": "^1.1.1",
Expand Down
68 changes: 0 additions & 68 deletions packages/libp2p/src/pnet/README.md

This file was deleted.

13 changes: 5 additions & 8 deletions packages/libp2p/test/connection-manager/direct.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,11 @@ import { DefaultConnectionManager } from '../../src/connection-manager/index.js'
import { codes as ErrorCodes } from '../../src/errors.js'
import { plaintext } from '../../src/insecure/index.js'
import { createLibp2pNode, type Libp2pNode } from '../../src/libp2p.js'
import { preSharedKey } from '../../src/pnet/index.js'
import { DefaultTransportManager } from '../../src/transport-manager.js'
import swarmKey from '../fixtures/swarm.key.js'
import type { PeerId } from '@libp2p/interface/peer-id'
import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
import type { Multiaddr } from '@multiformats/multiaddr'

const swarmKeyBuffer = uint8ArrayFromString(swarmKey)
const listenAddr = multiaddr('/ip4/127.0.0.1/tcp/0')
const unsupportedAddr = multiaddr('/ip4/127.0.0.1/tcp/9999/ws/p2p/QmckxVrJw1Yo8LqvmDJNUmdAsKtSbiKWmrXJFyKmUraBoN')

Expand Down Expand Up @@ -496,9 +493,11 @@ describe('libp2p.dialer (direct, TCP)', () => {
})

it('should use the protectors when provided for connecting', async () => {
const protector: ConnectionProtector = preSharedKey({
psk: swarmKeyBuffer
})()
const protector: ConnectionProtector = {
async protect (connection) {
return connection
}
}

libp2p = await createLibp2pNode({
peerId,
Expand All @@ -517,8 +516,6 @@ describe('libp2p.dialer (direct, TCP)', () => {

const protectorProtectSpy = Sinon.spy(protector, 'protect')

remoteLibp2p.components.connectionProtector = preSharedKey({ psk: swarmKeyBuffer })()

await libp2p.start()

const connection = await libp2p.dial(remoteAddr)
Expand Down
47 changes: 0 additions & 47 deletions packages/libp2p/test/core/start.spec.ts

This file was deleted.

3 changes: 0 additions & 3 deletions packages/libp2p/test/fixtures/swarm.key.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/libp2p/test/identify/service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ describe('identify', () => {

// Wait for identify to finish
await identityServiceIdentifySpy.firstCall.returnValue
sinon.stub(libp2p, 'isStarted').returns(true)

// Cause supported protocols to change
await libp2p.handle('/echo/2.0.0', () => {})
Expand Down Expand Up @@ -251,7 +250,6 @@ describe('identify', () => {

// Wait for identify to finish
await identityServiceIdentifySpy.firstCall.returnValue
sinon.stub(libp2p, 'isStarted').returns(true)

await libp2p.peerStore.merge(libp2p.peerId, {
multiaddrs: [multiaddr('/ip4/180.0.0.1/tcp/15001/ws')]
Expand Down
3 changes: 0 additions & 3 deletions packages/libp2p/test/transports/transport-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ describe('libp2p.transportManager (dial only)', () => {
start: false
})

expect(libp2p.isStarted()).to.be.false()
await expect(libp2p.start()).to.eventually.be.rejected
.with.property('code', ErrorCodes.ERR_NO_VALID_ADDRESSES)
})
Expand All @@ -147,7 +146,6 @@ describe('libp2p.transportManager (dial only)', () => {
start: false
})

expect(libp2p.isStarted()).to.be.false()
await expect(libp2p.start()).to.eventually.be.undefined()
})

Expand All @@ -169,7 +167,6 @@ describe('libp2p.transportManager (dial only)', () => {
start: false
})

expect(libp2p.isStarted()).to.be.false()
await expect(libp2p.start()).to.eventually.be.undefined()
})
})
21 changes: 13 additions & 8 deletions packages/libp2p/test/upgrading/upgrader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ import { type Components, defaultComponents } from '../../src/components.js'
import { codes } from '../../src/errors.js'
import { createLibp2p } from '../../src/index.js'
import { plaintext } from '../../src/insecure/index.js'
import { preSharedKey } from '../../src/pnet/index.js'
import { DEFAULT_MAX_OUTBOUND_STREAMS } from '../../src/registrar.js'
import { DefaultUpgrader } from '../../src/upgrader.js'
import swarmKey from '../fixtures/swarm.key.js'
import type { Libp2p } from '@libp2p/interface'
import type { Connection, ConnectionProtector, Stream } from '@libp2p/interface/connection'
import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interface/connection-encrypter'
Expand Down Expand Up @@ -206,9 +204,12 @@ describe('Upgrader', () => {
it('should use a private connection protector when provided', async () => {
const { inbound, outbound } = mockMultiaddrConnPair({ addrs, remotePeer })

const protector = preSharedKey({
psk: uint8ArrayFromString(swarmKey)
})()
const protector: ConnectionProtector = {
async protect (connection) {
return connection
}
}

const protectorProtectSpy = sinon.spy(protector, 'protect')

localComponents.connectionProtector = protector
Expand Down Expand Up @@ -615,6 +616,12 @@ describe('libp2p.upgrader', () => {
it('should create an Upgrader', async () => {
const deferred = pDefer<Components>()

const protector: ConnectionProtector = {
async protect (connection) {
return connection
}
}

libp2p = await createLibp2p({
peerId: peers[0],
transports: [
Expand All @@ -627,9 +634,7 @@ describe('libp2p.upgrader', () => {
connectionEncryption: [
plaintext()
],
connectionProtector: preSharedKey({
psk: uint8ArrayFromString(swarmKey)
}),
connectionProtector: () => protector,
services: {
test: (components: any) => {
deferred.resolve(components)
Expand Down
1 change: 0 additions & 1 deletion packages/libp2p/typedoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"./src/identify/index.ts",
"./src/insecure/index.ts",
"./src/ping/index.ts",
"./src/pnet/index.ts",
"./src/upnp-nat/index.ts"
]
}
Loading

0 comments on commit 192d082

Please sign in to comment.