-
Notifications
You must be signed in to change notification settings - Fork 443
/
index.ts
205 lines (174 loc) · 5.69 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
* @packageDocumentation
*
* The configured bootstrap peers will be discovered after the configured timeout. This will ensure there are some peers in the peer store for the node to use to discover other peers.
*
* They will be tagged with a tag with the name `'bootstrap'` tag, the value `50` and it will expire after two minutes which means the nodes connections may be closed if the maximum number of connections is reached.
*
* Clients that need constant connections to bootstrap nodes (e.g. browsers) can set the TTL to `Infinity`.
*
* @example Configuring a list of bootstrap nodes
*
* ```TypeScript
* import { createLibp2p } from 'libp2p'
* import { bootstrap } from '@libp2p/bootstrap'
*
* const libp2p = await createLibp2p({
* peerDiscovery: [
* bootstrap({
* list: [
* // a list of bootstrap peer multiaddrs to connect to on node startup
* '/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
* '/dnsaddr/bootstrap.libp2p.io/ipfs/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN',
* '/dnsaddr/bootstrap.libp2p.io/ipfs/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa'
* ]
* })
* ]
* })
*
* libp2p.addEventListener('peer:discovery', (evt) => {
* console.log('found peer: ', evt.detail.toString())
* })
* ```
*/
import { TypedEventEmitter, peerDiscoverySymbol, serviceCapabilities } from '@libp2p/interface'
import { peerIdFromString } from '@libp2p/peer-id'
import { P2P } from '@multiformats/mafmt'
import { multiaddr } from '@multiformats/multiaddr'
import type { ComponentLogger, Logger, PeerDiscovery, PeerDiscoveryEvents, PeerInfo, PeerStore, Startable } from '@libp2p/interface'
import type { ConnectionManager } from '@libp2p/interface-internal'
const DEFAULT_BOOTSTRAP_TAG_NAME = 'bootstrap'
const DEFAULT_BOOTSTRAP_TAG_VALUE = 50
const DEFAULT_BOOTSTRAP_DISCOVERY_TIMEOUT = 1000
export interface BootstrapInit {
/**
* The list of peer addresses in multi-address format
*/
list: string[]
/**
* How long to wait before discovering bootstrap nodes
*/
timeout?: number
/**
* Tag a bootstrap peer with this name before "discovering" it
*
* @default 'bootstrap'
*/
tagName?: string
/**
* The bootstrap peer tag will have this value (default: 50)
*/
tagValue?: number
/**
* Cause the bootstrap peer tag to be removed after this number of ms
*/
tagTTL?: number
}
export interface BootstrapComponents {
peerStore: PeerStore
logger: ComponentLogger
connectionManager: ConnectionManager
}
/**
* Emits 'peer' events on a regular interval for each peer in the provided list.
*/
class Bootstrap extends TypedEventEmitter<PeerDiscoveryEvents> implements PeerDiscovery, Startable {
static tag = 'bootstrap'
private readonly log: Logger
private timer?: ReturnType<typeof setTimeout>
private readonly list: PeerInfo[]
private readonly timeout: number
private readonly components: BootstrapComponents
private readonly _init: BootstrapInit
constructor (components: BootstrapComponents, options: BootstrapInit = { list: [] }) {
if (options.list == null || options.list.length === 0) {
throw new Error('Bootstrap requires a list of peer addresses')
}
super()
this.components = components
this.log = components.logger.forComponent('libp2p:bootstrap')
this.timeout = options.timeout ?? DEFAULT_BOOTSTRAP_DISCOVERY_TIMEOUT
this.list = []
for (const candidate of options.list) {
if (!P2P.matches(candidate)) {
this.log.error('Invalid multiaddr')
continue
}
const ma = multiaddr(candidate)
const peerIdStr = ma.getPeerId()
if (peerIdStr == null) {
this.log.error('Invalid bootstrap multiaddr without peer id')
continue
}
const peerData: PeerInfo = {
id: peerIdFromString(peerIdStr),
multiaddrs: [ma]
}
this.list.push(peerData)
}
this._init = options
}
readonly [peerDiscoverySymbol] = this
readonly [Symbol.toStringTag] = '@libp2p/bootstrap'
readonly [serviceCapabilities]: string[] = [
'@libp2p/peer-discovery'
]
isStarted (): boolean {
return Boolean(this.timer)
}
/**
* Start emitting events
*/
start (): void {
if (this.isStarted()) {
return
}
this.log('Starting bootstrap node discovery, discovering peers after %s ms', this.timeout)
this.timer = setTimeout(() => {
void this._discoverBootstrapPeers()
.catch(err => {
this.log.error(err)
})
}, this.timeout)
}
/**
* Emit each address in the list as a PeerInfo
*/
async _discoverBootstrapPeers (): Promise<void> {
if (this.timer == null) {
return
}
for (const peerData of this.list) {
await this.components.peerStore.merge(peerData.id, {
tags: {
[this._init.tagName ?? DEFAULT_BOOTSTRAP_TAG_NAME]: {
value: this._init.tagValue ?? DEFAULT_BOOTSTRAP_TAG_VALUE,
ttl: this._init.tagTTL
}
},
multiaddrs: peerData.multiaddrs
})
// check we are still running
if (this.timer == null) {
return
}
this.safeDispatchEvent('peer', { detail: peerData })
this.components.connectionManager.openConnection(peerData.id)
.catch(err => {
this.log.error('could not dial bootstrap peer %p', peerData.id, err)
})
}
}
/**
* Stop emitting events
*/
stop (): void {
if (this.timer != null) {
clearTimeout(this.timer)
}
this.timer = undefined
}
}
export function bootstrap (init: BootstrapInit): (components: BootstrapComponents) => PeerDiscovery {
return (components: BootstrapComponents) => new Bootstrap(components, init)
}