Skip to content

Commit a32e70b

Browse files
authored
fix: add status property (#2269)
To tell if the node has been started or not.
1 parent 06e6d23 commit a32e70b

File tree

3 files changed

+69
-11
lines changed

3 files changed

+69
-11
lines changed

packages/interface/src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ export interface PendingDial {
321321
multiaddrs: Multiaddr[]
322322
}
323323

324+
export type Libp2pStatus = 'starting' | 'started' | 'stopping' | 'stopped'
325+
324326
/**
325327
* Libp2p nodes implement this interface.
326328
*/
@@ -410,8 +412,16 @@ export interface Libp2p<T extends ServiceMap = ServiceMap> extends Startable, Ty
410412
*/
411413
metrics?: Metrics
412414

415+
/**
416+
* The logger used by this libp2p node
417+
*/
413418
logger: ComponentLogger
414419

420+
/**
421+
* The current status of the libp2p node
422+
*/
423+
status: Libp2pStatus
424+
415425
/**
416426
* Get a deduplicated list of peer advertising multiaddrs by concatenating
417427
* the listen addresses used by transports with any configured

packages/libp2p/src/libp2p.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { DefaultUpgrader } from './upgrader.js'
2323
import * as pkg from './version.js'
2424
import type { Components } from './components.js'
2525
import type { Libp2p, Libp2pInit, Libp2pOptions } from './index.js'
26-
import type { PeerRouting, ContentRouting, Libp2pEvents, PendingDial, ServiceMap, AbortOptions, ComponentLogger, Logger, Connection, NewStreamOptions, Stream, Metrics, PeerId, PeerInfo, PeerStore, Topology } from '@libp2p/interface'
26+
import type { PeerRouting, ContentRouting, Libp2pEvents, PendingDial, ServiceMap, AbortOptions, ComponentLogger, Logger, Connection, NewStreamOptions, Stream, Metrics, PeerId, PeerInfo, PeerStore, Topology, Libp2pStatus } from '@libp2p/interface'
2727
import type { StreamHandler, StreamHandlerOptions } from '@libp2p/interface-internal'
2828

2929
export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends TypedEventEmitter<Libp2pEvents> implements Libp2p<T> {
@@ -34,14 +34,16 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
3434
public metrics?: Metrics
3535
public services: T
3636
public logger: ComponentLogger
37+
public status: Libp2pStatus
3738

3839
public components: Components
39-
#started: boolean
4040
private readonly log: Logger
4141

4242
constructor (init: Libp2pInit<T>) {
4343
super()
4444

45+
this.status = 'stopped'
46+
4547
// event bus - components can listen to this emitter to be notified of system events
4648
// and also cause them to be emitted
4749
const events = new TypedEventEmitter<Libp2pEvents>()
@@ -58,7 +60,6 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
5860
// This emitter gets listened to a lot
5961
setMaxListeners(Infinity, events)
6062

61-
this.#started = false
6263
this.peerId = init.peerId
6364
this.logger = init.logger ?? defaultLogger()
6465
this.log = this.logger.forComponent('libp2p')
@@ -196,11 +197,11 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
196197
* Starts the libp2p node and all its subsystems
197198
*/
198199
async start (): Promise<void> {
199-
if (this.#started) {
200+
if (this.status !== 'stopped') {
200201
return
201202
}
202203

203-
this.#started = true
204+
this.status = 'starting'
204205

205206
this.log('libp2p is starting')
206207

@@ -209,6 +210,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
209210
await this.components.start()
210211
await this.components.afterStart?.()
211212

213+
this.status = 'started'
212214
this.safeDispatchEvent('start', { detail: this })
213215
this.log('libp2p has started')
214216
} catch (err: any) {
@@ -222,26 +224,23 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
222224
* Stop the libp2p node by closing its listeners and open connections
223225
*/
224226
async stop (): Promise<void> {
225-
if (!this.#started) {
227+
if (this.status !== 'started') {
226228
return
227229
}
228230

229231
this.log('libp2p is stopping')
230232

231-
this.#started = false
233+
this.status = 'stopping'
232234

233235
await this.components.beforeStop?.()
234236
await this.components.stop()
235237
await this.components.afterStop?.()
236238

239+
this.status = 'stopped'
237240
this.safeDispatchEvent('stop', { detail: this })
238241
this.log('libp2p has stopped')
239242
}
240243

241-
isStarted (): boolean {
242-
return this.#started
243-
}
244-
245244
getConnections (peerId?: PeerId): Connection[] {
246245
return this.components.connectionManager.getConnections(peerId)
247246
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* eslint-env mocha */
2+
3+
import { plaintext } from '@libp2p/plaintext'
4+
import { tcp } from '@libp2p/tcp'
5+
import { expect } from 'aegir/chai'
6+
import { createLibp2pNode, type Libp2pNode } from '../../src/libp2p.js'
7+
8+
const listenAddr = '/ip4/0.0.0.0/tcp/0'
9+
10+
describe('status', () => {
11+
let libp2p: Libp2pNode
12+
13+
after(async () => {
14+
await libp2p.stop()
15+
})
16+
17+
it('should have status', async () => {
18+
libp2p = await createLibp2pNode({
19+
start: false,
20+
addresses: {
21+
listen: [listenAddr]
22+
},
23+
transports: [
24+
tcp()
25+
],
26+
connectionEncryption: [
27+
plaintext()
28+
]
29+
})
30+
31+
expect(libp2p).to.have.property('status', 'stopped')
32+
33+
const startP = libp2p.start()
34+
35+
expect(libp2p).to.have.property('status', 'starting')
36+
37+
await startP
38+
39+
expect(libp2p).to.have.property('status', 'started')
40+
41+
const stopP = libp2p.stop()
42+
43+
expect(libp2p).to.have.property('status', 'stopping')
44+
45+
await stopP
46+
47+
expect(libp2p).to.have.property('status', 'stopped')
48+
})
49+
})

0 commit comments

Comments
 (0)