Skip to content

Commit fca208f

Browse files
committed
refactor!: remove isStarted method from Startable (#2145)
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
1 parent 5468cd1 commit fca208f

File tree

28 files changed

+461
-346
lines changed

28 files changed

+461
-346
lines changed

doc/migrations/v0.46-v1.0.0.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ A migration guide for refactoring your application code from libp2p `v0.46` to `
77

88
- [AutoNAT](#autonat)
99
- [KeyChain](#keychain)
10+
- [Pnet](#pnet)
1011
- [Metrics](#metrics)
1112

1213
## AutoNAT
@@ -68,6 +69,22 @@ const libp2p = await createLibp2p({
6869
const keychain: Keychain = libp2p.services.keychain
6970
```
7071

72+
## Pnet
73+
74+
The pnet module is now published in its own package.
75+
76+
**Before**
77+
78+
```ts
79+
import { preSharedKey, generateKey } from 'libp2p/pnet'
80+
```
81+
82+
**After**
83+
84+
```ts
85+
import { preSharedKey, generateKey } from '@libp2p/pnet'
86+
```
87+
7188
## Metrics
7289

7390
The following metrics were renamed:

packages/interface-compliance-tests/src/pubsub/api.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ export default (common: TestSetup<PubSub, PubSubArgs>): void => {
4848

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

51-
expect(pubsub.isStarted()).to.equal(true)
5251
expect(components.registrar.register).to.have.property('callCount', 1)
5352
})
5453

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

65-
expect(pubsub.isStarted()).to.equal(false)
6664
expect(components.registrar.unregister).to.have.property('callCount', 1)
6765
})
6866

packages/interface/src/startable.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
* Implemented by components that have a lifecycle
33
*/
44
export interface Startable {
5-
isStarted(): boolean
6-
75
/**
86
* If implemented, this method will be invoked before the start method.
97
*

packages/libp2p/package.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,6 @@
7272
"types": "./dist/src/ping/index.d.ts",
7373
"import": "./dist/src/ping/index.js"
7474
},
75-
"./pnet": {
76-
"types": "./dist/src/pnet/index.d.ts",
77-
"import": "./dist/src/pnet/index.js"
78-
},
7975
"./upnp-nat": {
8076
"types": "./dist/src/upnp-nat/index.d.ts",
8177
"import": "./dist/src/upnp-nat/index.js"
@@ -156,8 +152,7 @@
156152
"rate-limiter-flexible": "^3.0.0",
157153
"uint8arraylist": "^2.4.3",
158154
"uint8arrays": "^4.0.6",
159-
"wherearewe": "^2.0.1",
160-
"xsalsa20": "^1.1.0"
155+
"wherearewe": "^2.0.1"
161156
},
162157
"devDependencies": {
163158
"@chainsafe/libp2p-gossipsub": "^10.0.0",
@@ -174,7 +169,6 @@
174169
"@libp2p/mplex": "^9.0.12",
175170
"@libp2p/tcp": "^8.0.13",
176171
"@libp2p/websockets": "^7.0.13",
177-
"@types/xsalsa20": "^1.1.0",
178172
"aegir": "^41.0.2",
179173
"execa": "^8.0.1",
180174
"go-libp2p": "^1.1.1",

packages/libp2p/src/pnet/README.md

Lines changed: 0 additions & 68 deletions
This file was deleted.

packages/libp2p/test/connection-manager/direct.node.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,11 @@ import { DefaultConnectionManager } from '../../src/connection-manager/index.js'
3232
import { codes as ErrorCodes } from '../../src/errors.js'
3333
import { plaintext } from '../../src/insecure/index.js'
3434
import { createLibp2pNode, type Libp2pNode } from '../../src/libp2p.js'
35-
import { preSharedKey } from '../../src/pnet/index.js'
3635
import { DefaultTransportManager } from '../../src/transport-manager.js'
37-
import swarmKey from '../fixtures/swarm.key.js'
3836
import type { PeerId } from '@libp2p/interface/peer-id'
3937
import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
4038
import type { Multiaddr } from '@multiformats/multiaddr'
4139

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

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

498495
it('should use the protectors when provided for connecting', async () => {
499-
const protector: ConnectionProtector = preSharedKey({
500-
psk: swarmKeyBuffer
501-
})()
496+
const protector: ConnectionProtector = {
497+
async protect (connection) {
498+
return connection
499+
}
500+
}
502501

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

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

520-
remoteLibp2p.components.connectionProtector = preSharedKey({ psk: swarmKeyBuffer })()
521-
522519
await libp2p.start()
523520

524521
const connection = await libp2p.dial(remoteAddr)

packages/libp2p/test/core/start.spec.ts

Lines changed: 0 additions & 47 deletions
This file was deleted.

packages/libp2p/test/fixtures/swarm.key.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/libp2p/test/identify/service.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ describe('identify', () => {
153153

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

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

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

256254
await libp2p.peerStore.merge(libp2p.peerId, {
257255
multiaddrs: [multiaddr('/ip4/180.0.0.1/tcp/15001/ws')]

packages/libp2p/test/transports/transport-manager.spec.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ describe('libp2p.transportManager (dial only)', () => {
124124
start: false
125125
})
126126

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

150-
expect(libp2p.isStarted()).to.be.false()
151149
await expect(libp2p.start()).to.eventually.be.undefined()
152150
})
153151

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

172-
expect(libp2p.isStarted()).to.be.false()
173170
await expect(libp2p.start()).to.eventually.be.undefined()
174171
})
175172
})

0 commit comments

Comments
 (0)