libp2p 0.36.0 → 0.36.2-1473044
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +3 -21
- package/README.md +13 -10
- package/dist/src/address-manager/index.d.ts +33 -52
- package/dist/src/address-manager/index.d.ts.map +1 -1
- package/dist/src/address-manager/index.js +81 -0
- package/dist/src/address-manager/index.js.map +1 -0
- package/dist/src/circuit/auto-relay.d.ts +24 -85
- package/dist/src/circuit/auto-relay.d.ts.map +1 -1
- package/dist/src/circuit/auto-relay.js +215 -0
- package/dist/src/circuit/auto-relay.js.map +1 -0
- package/dist/src/circuit/circuit/hop.d.ts +26 -53
- package/dist/src/circuit/circuit/hop.d.ts.map +1 -1
- package/dist/src/circuit/circuit/hop.js +138 -0
- package/dist/src/circuit/circuit/hop.js.map +1 -0
- package/dist/src/circuit/circuit/stop.d.ts +18 -10
- package/dist/src/circuit/circuit/stop.d.ts.map +1 -1
- package/dist/src/circuit/circuit/stop.js +49 -0
- package/dist/src/circuit/circuit/stop.js.map +1 -0
- package/dist/src/circuit/circuit/stream-handler.d.ts +19 -38
- package/dist/src/circuit/circuit/stream-handler.d.ts.map +1 -1
- package/dist/src/circuit/circuit/stream-handler.js +59 -0
- package/dist/src/circuit/circuit/stream-handler.js.map +1 -0
- package/dist/src/circuit/circuit/utils.d.ts +3 -6
- package/dist/src/circuit/circuit/utils.d.ts.map +1 -1
- package/dist/src/circuit/circuit/utils.js +43 -0
- package/dist/src/circuit/circuit/utils.js.map +1 -0
- package/dist/src/circuit/constants.d.ts +24 -6
- package/dist/src/circuit/constants.d.ts.map +1 -1
- package/dist/src/circuit/constants.js +26 -0
- package/dist/src/circuit/constants.js.map +1 -0
- package/dist/src/circuit/index.d.ts +41 -74
- package/dist/src/circuit/index.d.ts.map +1 -1
- package/dist/src/circuit/index.js +69 -0
- package/dist/src/circuit/index.js.map +1 -0
- package/dist/src/circuit/listener.d.ts +8 -3
- package/dist/src/circuit/listener.d.ts.map +1 -1
- package/dist/src/circuit/listener.js +54 -0
- package/dist/src/circuit/listener.js.map +1 -0
- package/dist/src/circuit/multicodec.d.ts +1 -1
- package/dist/src/circuit/multicodec.d.ts.map +1 -1
- package/dist/src/circuit/multicodec.js +2 -0
- package/dist/src/circuit/multicodec.js.map +1 -0
- package/dist/src/circuit/pb/index.d.ts +51 -0
- package/dist/src/circuit/pb/index.d.ts.map +1 -0
- package/dist/src/circuit/pb/index.js +72 -0
- package/dist/src/circuit/pb/index.js.map +1 -0
- package/dist/src/circuit/transport.d.ts +18 -78
- package/dist/src/circuit/transport.d.ts.map +1 -1
- package/dist/src/circuit/transport.js +190 -0
- package/dist/src/circuit/transport.js.map +1 -0
- package/dist/src/circuit/utils.d.ts +5 -2
- package/dist/src/circuit/utils.d.ts.map +1 -1
- package/dist/src/circuit/utils.js +11 -0
- package/dist/src/circuit/utils.js.map +1 -0
- package/dist/src/config.d.ts +3 -85
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +93 -0
- package/dist/src/config.js.map +1 -0
- package/dist/src/connection-manager/auto-dialler.d.ts +24 -41
- package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
- package/dist/src/connection-manager/auto-dialler.js +105 -0
- package/dist/src/connection-manager/auto-dialler.js.map +1 -0
- package/dist/src/connection-manager/index.d.ts +79 -144
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +295 -0
- package/dist/src/connection-manager/index.js.map +1 -0
- package/dist/src/connection-manager/latency-monitor.d.ts +72 -97
- package/dist/src/connection-manager/latency-monitor.d.ts.map +1 -1
- package/dist/src/connection-manager/latency-monitor.js +220 -0
- package/dist/src/connection-manager/latency-monitor.js.map +1 -0
- package/dist/src/connection-manager/visibility-change-emitter.d.ts +15 -16
- package/dist/src/connection-manager/visibility-change-emitter.d.ts.map +1 -1
- package/dist/src/connection-manager/visibility-change-emitter.js +118 -0
- package/dist/src/connection-manager/visibility-change-emitter.js.map +1 -0
- package/dist/src/constants.d.ts +22 -10
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +27 -0
- package/dist/src/constants.js.map +1 -0
- package/dist/src/content-routing/index.d.ts +26 -81
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js +99 -0
- package/dist/src/content-routing/index.js.map +1 -0
- package/dist/src/content-routing/utils.d.ts +6 -49
- package/dist/src/content-routing/utils.d.ts.map +1 -1
- package/dist/src/content-routing/utils.js +42 -0
- package/dist/src/content-routing/utils.js.map +1 -0
- package/dist/src/dht/dht-content-routing.d.ts +10 -24
- package/dist/src/dht/dht-content-routing.d.ts.map +1 -1
- package/dist/src/dht/dht-content-routing.js +32 -0
- package/dist/src/dht/dht-content-routing.js.map +1 -0
- package/dist/src/dht/dht-peer-routing.d.ts +9 -24
- package/dist/src/dht/dht-peer-routing.d.ts.map +1 -1
- package/dist/src/dht/dht-peer-routing.js +26 -0
- package/dist/src/dht/dht-peer-routing.js.map +1 -0
- package/dist/src/dialer/auto-dialer.d.ts +14 -0
- package/dist/src/dialer/auto-dialer.d.ts.map +1 -0
- package/dist/src/dialer/auto-dialer.js +24 -0
- package/dist/src/dialer/auto-dialer.js.map +1 -0
- package/dist/src/dialer/dial-request.d.ts +18 -46
- package/dist/src/dialer/dial-request.d.ts.map +1 -1
- package/dist/src/dialer/dial-request.js +83 -0
- package/dist/src/dialer/dial-request.js.map +1 -0
- package/dist/src/dialer/index.d.ts +41 -148
- package/dist/src/dialer/index.d.ts.map +1 -1
- package/dist/src/dialer/index.js +280 -0
- package/dist/src/dialer/index.js.map +1 -0
- package/dist/src/errors.d.ts +68 -63
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +73 -0
- package/dist/src/errors.js.map +1 -0
- package/dist/src/fetch/constants.d.ts +1 -1
- package/dist/src/fetch/constants.d.ts.map +1 -1
- package/dist/src/fetch/constants.js +3 -0
- package/dist/src/fetch/constants.js.map +1 -0
- package/dist/src/fetch/index.d.ts +30 -49
- package/dist/src/fetch/index.d.ts.map +1 -1
- package/dist/src/fetch/index.js +129 -0
- package/dist/src/fetch/index.js.map +1 -0
- package/dist/src/fetch/pb/proto.d.ts +26 -0
- package/dist/src/fetch/pb/proto.d.ts.map +1 -0
- package/dist/src/fetch/pb/proto.js +44 -0
- package/dist/src/fetch/pb/proto.js.map +1 -0
- package/dist/src/get-peer.d.ts +4 -11
- package/dist/src/get-peer.d.ts.map +1 -1
- package/dist/src/get-peer.js +43 -0
- package/dist/src/get-peer.js.map +1 -0
- package/dist/src/identify/consts.d.ts +9 -9
- package/dist/src/identify/consts.d.ts.map +1 -1
- package/dist/src/identify/consts.js +11 -0
- package/dist/src/identify/consts.js.map +1 -0
- package/dist/src/identify/index.d.ts +42 -96
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +322 -0
- package/dist/src/identify/index.js.map +1 -0
- package/dist/src/identify/pb/message.d.ts +15 -0
- package/dist/src/identify/pb/message.d.ts.map +1 -0
- package/dist/src/identify/pb/message.js +24 -0
- package/dist/src/identify/pb/message.js.map +1 -0
- package/dist/src/index.d.ts +162 -581
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +9 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/insecure/index.d.ts +9 -0
- package/dist/src/insecure/index.d.ts.map +1 -0
- package/dist/src/insecure/index.js +81 -0
- package/dist/src/insecure/index.js.map +1 -0
- package/dist/src/insecure/pb/proto.d.ts +28 -0
- package/dist/src/insecure/pb/proto.d.ts.map +1 -0
- package/dist/src/insecure/pb/proto.js +46 -0
- package/dist/src/insecure/pb/proto.js.map +1 -0
- package/dist/src/keychain/cms.d.ts +6 -14
- package/dist/src/keychain/cms.d.ts.map +1 -1
- package/dist/src/keychain/cms.js +129 -0
- package/dist/src/keychain/cms.js.map +1 -0
- package/dist/src/keychain/index.d.ts +61 -81
- package/dist/src/keychain/index.d.ts.map +1 -1
- package/dist/src/keychain/index.js +493 -0
- package/dist/src/keychain/index.js.map +1 -0
- package/dist/src/keychain/util.d.ts +3 -6
- package/dist/src/keychain/util.d.ts.map +1 -1
- package/dist/src/keychain/util.js +79 -0
- package/dist/src/keychain/util.js.map +1 -0
- package/dist/src/libp2p.d.ts +78 -0
- package/dist/src/libp2p.d.ts.map +1 -0
- package/dist/src/libp2p.js +375 -0
- package/dist/src/libp2p.js.map +1 -0
- package/dist/src/metrics/index.d.ts +47 -104
- package/dist/src/metrics/index.d.ts.map +1 -1
- package/dist/src/metrics/index.js +231 -0
- package/dist/src/metrics/index.js.map +1 -0
- package/dist/src/metrics/moving-average.d.ts +14 -0
- package/dist/src/metrics/moving-average.d.ts.map +1 -0
- package/dist/src/metrics/moving-average.js +40 -0
- package/dist/src/metrics/moving-average.js.map +1 -0
- package/dist/src/metrics/stats.d.ts +41 -93
- package/dist/src/metrics/stats.d.ts.map +1 -1
- package/dist/src/metrics/stats.js +183 -0
- package/dist/src/metrics/stats.js.map +1 -0
- package/dist/src/nat-manager.d.ts +42 -99
- package/dist/src/nat-manager.d.ts.map +1 -1
- package/dist/src/nat-manager.js +114 -0
- package/dist/src/nat-manager.js.map +1 -0
- package/dist/src/peer-record-updater.d.ts +15 -0
- package/dist/src/peer-record-updater.d.ts.map +1 -0
- package/dist/src/peer-record-updater.js +44 -0
- package/dist/src/peer-record-updater.js.map +1 -0
- package/dist/src/peer-routing.d.ts +39 -84
- package/dist/src/peer-routing.d.ts.map +1 -1
- package/dist/src/peer-routing.js +108 -0
- package/dist/src/peer-routing.js.map +1 -0
- package/dist/src/ping/constants.d.ts +4 -4
- package/dist/src/ping/constants.d.ts.map +1 -1
- package/dist/src/ping/constants.js +5 -0
- package/dist/src/ping/constants.js.map +1 -0
- package/dist/src/ping/index.d.ts +26 -33
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +56 -0
- package/dist/src/ping/index.js.map +1 -0
- package/dist/src/pnet/crypto.d.ts +15 -5
- package/dist/src/pnet/crypto.d.ts.map +1 -1
- package/dist/src/pnet/crypto.js +60 -0
- package/dist/src/pnet/crypto.js.map +1 -0
- package/dist/src/pnet/errors.d.ts +5 -5
- package/dist/src/pnet/errors.d.ts.map +1 -1
- package/dist/src/pnet/errors.js +6 -0
- package/dist/src/pnet/errors.js.map +1 -0
- package/dist/src/pnet/index.d.ts +11 -20
- package/dist/src/pnet/index.d.ts.map +1 -1
- package/dist/src/pnet/index.js +66 -0
- package/dist/src/pnet/index.js.map +1 -0
- package/dist/src/pnet/key-generator.d.ts +3 -7
- package/dist/src/pnet/key-generator.d.ts.map +1 -1
- package/dist/src/pnet/key-generator.js +25 -0
- package/dist/src/pnet/key-generator.js.map +1 -0
- package/dist/src/registrar.d.ts +27 -65
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +159 -0
- package/dist/src/registrar.js.map +1 -0
- package/dist/src/transport-manager.d.ts +34 -82
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +223 -0
- package/dist/src/transport-manager.js.map +1 -0
- package/dist/src/upgrader.d.ts +49 -129
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +402 -0
- package/dist/src/upgrader.js.map +1 -0
- package/dist/src/version.d.ts +3 -0
- package/dist/src/version.d.ts.map +1 -0
- package/dist/src/version.js +3 -0
- package/dist/src/version.js.map +1 -0
- package/package.json +150 -197
- package/src/address-manager/index.ts +129 -0
- package/src/circuit/README.md +16 -11
- package/src/circuit/auto-relay.ts +284 -0
- package/src/circuit/circuit/hop.ts +211 -0
- package/src/circuit/circuit/stop.ts +78 -0
- package/src/circuit/circuit/stream-handler.ts +86 -0
- package/src/circuit/circuit/{utils.js → utils.ts} +9 -25
- package/src/circuit/constants.ts +31 -0
- package/src/circuit/index.ts +120 -0
- package/src/circuit/{listener.js → listener.ts} +21 -27
- package/src/circuit/multicodec.ts +2 -0
- package/src/circuit/{protocol → pb}/index.proto +1 -1
- package/src/circuit/pb/index.ts +88 -0
- package/src/circuit/transport.ts +216 -0
- package/src/circuit/utils.ts +12 -0
- package/src/config.ts +101 -0
- package/src/connection-manager/auto-dialler.ts +154 -0
- package/src/connection-manager/index.ts +422 -0
- package/src/connection-manager/latency-monitor.ts +319 -0
- package/src/connection-manager/{visibility-change-emitter.js → visibility-change-emitter.ts} +48 -38
- package/src/constants.ts +31 -0
- package/src/content-routing/index.ts +143 -0
- package/src/content-routing/utils.ts +54 -0
- package/src/dht/dht-content-routing.ts +43 -0
- package/src/dht/dht-peer-routing.ts +35 -0
- package/src/dialer/auto-dialer.ts +40 -0
- package/src/dialer/{dial-request.js → dial-request.ts} +60 -51
- package/src/dialer/index.ts +371 -0
- package/src/errors.ts +71 -0
- package/src/fetch/README.md +1 -1
- package/src/fetch/constants.ts +3 -0
- package/src/fetch/index.ts +167 -0
- package/src/fetch/{proto.proto → pb/proto.proto} +0 -0
- package/src/fetch/pb/proto.ts +58 -0
- package/src/get-peer.ts +57 -0
- package/src/identify/consts.ts +13 -0
- package/src/identify/index.ts +445 -0
- package/src/identify/{message.proto → pb/message.proto} +0 -0
- package/src/identify/pb/message.ts +36 -0
- package/src/index.ts +242 -0
- package/src/insecure/index.ts +101 -0
- package/src/insecure/{proto.proto → pb/proto.proto} +0 -0
- package/src/insecure/pb/proto.ts +61 -0
- package/src/keychain/{cms.js → cms.ts} +62 -58
- package/src/keychain/index.ts +588 -0
- package/src/keychain/{util.js → util.ts} +7 -17
- package/src/libp2p.ts +513 -0
- package/src/metrics/index.ts +310 -0
- package/src/metrics/moving-average.ts +53 -0
- package/src/metrics/stats.ts +238 -0
- package/src/nat-manager.ts +194 -0
- package/src/peer-record-updater.ts +55 -0
- package/src/peer-routing.ts +185 -0
- package/src/ping/README.md +1 -1
- package/src/ping/constants.ts +5 -0
- package/src/ping/index.ts +83 -0
- package/src/pnet/README.md +2 -2
- package/src/pnet/crypto.ts +67 -0
- package/src/pnet/errors.ts +5 -0
- package/src/pnet/index.ts +95 -0
- package/src/pnet/key-generator.ts +28 -0
- package/src/registrar.ts +205 -0
- package/src/transport-manager.ts +279 -0
- package/src/upgrader.ts +499 -0
- package/src/version.ts +3 -0
- package/dist/index.min.js +0 -55
- package/dist/src/circuit/protocol/index.d.ts +0 -173
- package/dist/src/fetch/proto.d.ts +0 -134
- package/dist/src/identify/message.d.ts +0 -95
- package/dist/src/insecure/plaintext.d.ts +0 -12
- package/dist/src/insecure/plaintext.d.ts.map +0 -1
- package/dist/src/insecure/proto.d.ts +0 -128
- package/dist/src/metrics/old-peers.d.ts +0 -3
- package/dist/src/metrics/old-peers.d.ts.map +0 -1
- package/dist/src/metrics/tracked-map.d.ts +0 -8
- package/dist/src/metrics/tracked-map.d.ts.map +0 -1
- package/dist/src/peer-store/address-book.d.ts +0 -67
- package/dist/src/peer-store/address-book.d.ts.map +0 -1
- package/dist/src/peer-store/index.d.ts +0 -58
- package/dist/src/peer-store/index.d.ts.map +0 -1
- package/dist/src/peer-store/key-book.d.ts +0 -40
- package/dist/src/peer-store/key-book.d.ts.map +0 -1
- package/dist/src/peer-store/metadata-book.d.ts +0 -58
- package/dist/src/peer-store/metadata-book.d.ts.map +0 -1
- package/dist/src/peer-store/pb/peer.d.ts +0 -222
- package/dist/src/peer-store/proto-book.d.ts +0 -43
- package/dist/src/peer-store/proto-book.d.ts.map +0 -1
- package/dist/src/peer-store/store.d.ts +0 -73
- package/dist/src/peer-store/store.d.ts.map +0 -1
- package/dist/src/peer-store/types.d.ts +0 -202
- package/dist/src/peer-store/types.d.ts.map +0 -1
- package/dist/src/ping/util.d.ts +0 -5
- package/dist/src/ping/util.d.ts.map +0 -1
- package/dist/src/pubsub-adapter.d.ts +0 -22
- package/dist/src/pubsub-adapter.d.ts.map +0 -1
- package/dist/src/record/envelope/envelope.d.ts +0 -77
- package/dist/src/record/envelope/index.d.ts +0 -80
- package/dist/src/record/envelope/index.d.ts.map +0 -1
- package/dist/src/record/peer-record/consts.d.ts +0 -4
- package/dist/src/record/peer-record/consts.d.ts.map +0 -1
- package/dist/src/record/peer-record/index.d.ts +0 -60
- package/dist/src/record/peer-record/index.d.ts.map +0 -1
- package/dist/src/record/peer-record/peer-record.d.ts +0 -133
- package/dist/src/record/utils.d.ts +0 -12
- package/dist/src/record/utils.d.ts.map +0 -1
- package/dist/src/types.d.ts +0 -90
- package/dist/src/types.d.ts.map +0 -1
- package/src/address-manager/index.js +0 -96
- package/src/circuit/auto-relay.js +0 -302
- package/src/circuit/circuit/hop.js +0 -205
- package/src/circuit/circuit/stop.js +0 -81
- package/src/circuit/circuit/stream-handler.js +0 -94
- package/src/circuit/constants.js +0 -12
- package/src/circuit/index.js +0 -102
- package/src/circuit/multicodec.js +0 -5
- package/src/circuit/protocol/index.d.ts +0 -173
- package/src/circuit/protocol/index.js +0 -530
- package/src/circuit/transport.js +0 -229
- package/src/circuit/utils.js +0 -17
- package/src/config.js +0 -114
- package/src/connection-manager/auto-dialler.js +0 -132
- package/src/connection-manager/index.js +0 -363
- package/src/connection-manager/latency-monitor.js +0 -264
- package/src/constants.js +0 -18
- package/src/content-routing/index.js +0 -163
- package/src/content-routing/utils.js +0 -89
- package/src/dht/dht-content-routing.js +0 -44
- package/src/dht/dht-peer-routing.js +0 -51
- package/src/dialer/index.js +0 -376
- package/src/errors.js +0 -66
- package/src/fetch/constants.js +0 -6
- package/src/fetch/index.js +0 -159
- package/src/fetch/proto.d.ts +0 -134
- package/src/fetch/proto.js +0 -333
- package/src/get-peer.js +0 -49
- package/src/identify/consts.js +0 -15
- package/src/identify/index.js +0 -384
- package/src/identify/message.d.ts +0 -95
- package/src/identify/message.js +0 -328
- package/src/index.js +0 -811
- package/src/insecure/plaintext.js +0 -95
- package/src/insecure/proto.d.ts +0 -128
- package/src/insecure/proto.js +0 -371
- package/src/keychain/index.js +0 -561
- package/src/metrics/index.js +0 -290
- package/src/metrics/old-peers.js +0 -16
- package/src/metrics/stats.js +0 -270
- package/src/metrics/tracked-map.js +0 -94
- package/src/nat-manager.js +0 -197
- package/src/peer-routing.js +0 -176
- package/src/peer-store/README.md +0 -145
- package/src/peer-store/address-book.js +0 -382
- package/src/peer-store/index.js +0 -121
- package/src/peer-store/key-book.js +0 -141
- package/src/peer-store/metadata-book.js +0 -250
- package/src/peer-store/pb/peer.d.ts +0 -222
- package/src/peer-store/pb/peer.js +0 -643
- package/src/peer-store/pb/peer.proto +0 -31
- package/src/peer-store/proto-book.js +0 -237
- package/src/peer-store/store.js +0 -263
- package/src/peer-store/types.ts +0 -245
- package/src/ping/constants.js +0 -8
- package/src/ping/index.js +0 -85
- package/src/ping/util.js +0 -18
- package/src/pnet/crypto.js +0 -84
- package/src/pnet/errors.js +0 -7
- package/src/pnet/index.js +0 -86
- package/src/pnet/key-generator.js +0 -33
- package/src/pubsub-adapter.js +0 -61
- package/src/record/README.md +0 -130
- package/src/record/envelope/envelope.d.ts +0 -77
- package/src/record/envelope/envelope.js +0 -243
- package/src/record/envelope/envelope.proto +0 -19
- package/src/record/envelope/index.js +0 -183
- package/src/record/peer-record/consts.js +0 -14
- package/src/record/peer-record/index.js +0 -113
- package/src/record/peer-record/peer-record.d.ts +0 -133
- package/src/record/peer-record/peer-record.js +0 -367
- package/src/record/peer-record/peer-record.proto +0 -18
- package/src/record/utils.js +0 -25
- package/src/registrar.js +0 -127
- package/src/transport-manager.js +0 -269
- package/src/types.ts +0 -98
- package/src/upgrader.js +0 -492
package/src/config.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import mergeOptions from 'merge-options'
|
|
2
|
+
import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers'
|
|
3
|
+
import * as Constants from './constants.js'
|
|
4
|
+
import { AGENT_VERSION } from './identify/consts.js'
|
|
5
|
+
import * as RelayConstants from './circuit/constants.js'
|
|
6
|
+
import { publicAddressesFirst } from '@libp2p/utils/address-sort'
|
|
7
|
+
import { FaultTolerance } from './transport-manager.js'
|
|
8
|
+
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
9
|
+
import type { Libp2pInit } from './index.js'
|
|
10
|
+
import { codes, messages } from './errors.js'
|
|
11
|
+
import errCode from 'err-code'
|
|
12
|
+
import type { RecursivePartial } from '@libp2p/interfaces'
|
|
13
|
+
|
|
14
|
+
const DefaultConfig: Partial<Libp2pInit> = {
|
|
15
|
+
addresses: {
|
|
16
|
+
listen: [],
|
|
17
|
+
announce: [],
|
|
18
|
+
noAnnounce: [],
|
|
19
|
+
announceFilter: (multiaddrs: Multiaddr[]) => multiaddrs
|
|
20
|
+
},
|
|
21
|
+
connectionManager: {
|
|
22
|
+
maxConnections: 300,
|
|
23
|
+
minConnections: 50,
|
|
24
|
+
autoDialInterval: 10000,
|
|
25
|
+
autoDial: true
|
|
26
|
+
},
|
|
27
|
+
connectionGater: {},
|
|
28
|
+
transportManager: {
|
|
29
|
+
faultTolerance: FaultTolerance.FATAL_ALL
|
|
30
|
+
},
|
|
31
|
+
dialer: {
|
|
32
|
+
maxParallelDials: Constants.MAX_PARALLEL_DIALS,
|
|
33
|
+
maxDialsPerPeer: Constants.MAX_PER_PEER_DIALS,
|
|
34
|
+
dialTimeout: Constants.DIAL_TIMEOUT,
|
|
35
|
+
resolvers: {
|
|
36
|
+
dnsaddr: dnsaddrResolver
|
|
37
|
+
},
|
|
38
|
+
addressSorter: publicAddressesFirst
|
|
39
|
+
},
|
|
40
|
+
host: {
|
|
41
|
+
agentVersion: AGENT_VERSION
|
|
42
|
+
},
|
|
43
|
+
metrics: {
|
|
44
|
+
enabled: false,
|
|
45
|
+
computeThrottleMaxQueueSize: 1000,
|
|
46
|
+
computeThrottleTimeout: 2000,
|
|
47
|
+
movingAverageIntervals: [
|
|
48
|
+
60 * 1000, // 1 minute
|
|
49
|
+
5 * 60 * 1000, // 5 minutes
|
|
50
|
+
15 * 60 * 1000 // 15 minutes
|
|
51
|
+
],
|
|
52
|
+
maxOldPeersRetention: 50
|
|
53
|
+
},
|
|
54
|
+
peerRouting: {
|
|
55
|
+
refreshManager: {
|
|
56
|
+
enabled: true,
|
|
57
|
+
interval: 6e5,
|
|
58
|
+
bootDelay: 10e3
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
protocolPrefix: 'ipfs',
|
|
62
|
+
nat: {
|
|
63
|
+
enabled: true,
|
|
64
|
+
ttl: 7200,
|
|
65
|
+
keepAlive: true
|
|
66
|
+
},
|
|
67
|
+
relay: {
|
|
68
|
+
enabled: true,
|
|
69
|
+
advertise: {
|
|
70
|
+
bootDelay: RelayConstants.ADVERTISE_BOOT_DELAY,
|
|
71
|
+
enabled: false,
|
|
72
|
+
ttl: RelayConstants.ADVERTISE_TTL
|
|
73
|
+
},
|
|
74
|
+
hop: {
|
|
75
|
+
enabled: false,
|
|
76
|
+
active: false
|
|
77
|
+
},
|
|
78
|
+
autoRelay: {
|
|
79
|
+
enabled: false,
|
|
80
|
+
maxListeners: 2
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function validateConfig (opts: RecursivePartial<Libp2pInit>): Libp2pInit {
|
|
86
|
+
const resultingOptions: Libp2pInit = mergeOptions(DefaultConfig, opts)
|
|
87
|
+
|
|
88
|
+
if (resultingOptions.transports == null || resultingOptions.transports.length < 1) {
|
|
89
|
+
throw errCode(new Error(messages.ERR_TRANSPORTS_REQUIRED), codes.ERR_TRANSPORTS_REQUIRED)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (resultingOptions.connectionEncryption == null || resultingOptions.connectionEncryption.length === 0) {
|
|
93
|
+
throw errCode(new Error(messages.CONN_ENCRYPTION_REQUIRED), codes.CONN_ENCRYPTION_REQUIRED)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (resultingOptions.connectionProtector === null && globalThis.process?.env?.LIBP2P_FORCE_PNET != null) { // eslint-disable-line no-undef
|
|
97
|
+
throw errCode(new Error(messages.ERR_PROTECTOR_REQUIRED), codes.ERR_PROTECTOR_REQUIRED)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return resultingOptions
|
|
101
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { logger } from '@libp2p/logger'
|
|
2
|
+
import mergeOptions from 'merge-options'
|
|
3
|
+
// @ts-expect-error retimer does not have types
|
|
4
|
+
import retimer from 'retimer'
|
|
5
|
+
import all from 'it-all'
|
|
6
|
+
import { pipe } from 'it-pipe'
|
|
7
|
+
import filter from 'it-filter'
|
|
8
|
+
import sort from 'it-sort'
|
|
9
|
+
import type { Startable } from '@libp2p/interfaces'
|
|
10
|
+
import type { Components } from '@libp2p/interfaces/components'
|
|
11
|
+
|
|
12
|
+
const log = logger('libp2p:connection-manager:auto-dialler')
|
|
13
|
+
|
|
14
|
+
export interface AutoDiallerInit {
|
|
15
|
+
/**
|
|
16
|
+
* Should preemptively guarantee connections are above the low watermark
|
|
17
|
+
*/
|
|
18
|
+
enabled?: boolean
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The minimum number of connections to avoid pruning
|
|
22
|
+
*/
|
|
23
|
+
minConnections?: number
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* How often, in milliseconds, it should preemptively guarantee connections are above the low watermark
|
|
27
|
+
*/
|
|
28
|
+
autoDialInterval?: number
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const defaultOptions: Partial<AutoDiallerInit> = {
|
|
32
|
+
enabled: true,
|
|
33
|
+
minConnections: 0,
|
|
34
|
+
autoDialInterval: 10000
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export class AutoDialler implements Startable {
|
|
38
|
+
private readonly components: Components
|
|
39
|
+
private readonly options: Required<AutoDiallerInit>
|
|
40
|
+
private running: boolean
|
|
41
|
+
private autoDialTimeout?: ReturnType<retimer>
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Proactively tries to connect to known peers stored in the PeerStore.
|
|
45
|
+
* It will keep the number of connections below the upper limit and sort
|
|
46
|
+
* the peers to connect based on wether we know their keys and protocols.
|
|
47
|
+
*/
|
|
48
|
+
constructor (components: Components, init: AutoDiallerInit) {
|
|
49
|
+
this.components = components
|
|
50
|
+
this.options = mergeOptions.call({ ignoreUndefined: true }, defaultOptions, init)
|
|
51
|
+
this.running = false
|
|
52
|
+
this._autoDial = this._autoDial.bind(this)
|
|
53
|
+
|
|
54
|
+
log('options: %j', this.options)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
isStarted () {
|
|
58
|
+
return this.running
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Starts the auto dialer
|
|
63
|
+
*/
|
|
64
|
+
async start () {
|
|
65
|
+
if (!this.options.enabled) {
|
|
66
|
+
log('not enabled')
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
this.running = true
|
|
71
|
+
|
|
72
|
+
void this._autoDial().catch(err => {
|
|
73
|
+
log.error('could start autodial', err)
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
log('started')
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Stops the auto dialler
|
|
81
|
+
*/
|
|
82
|
+
async stop () {
|
|
83
|
+
if (!this.options.enabled) {
|
|
84
|
+
log('not enabled')
|
|
85
|
+
return
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
this.running = false
|
|
89
|
+
|
|
90
|
+
if (this.autoDialTimeout != null) {
|
|
91
|
+
this.autoDialTimeout.clear()
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
log('stopped')
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async _autoDial () {
|
|
98
|
+
if (this.autoDialTimeout != null) {
|
|
99
|
+
this.autoDialTimeout.clear()
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const minConnections = this.options.minConnections
|
|
103
|
+
|
|
104
|
+
// Already has enough connections
|
|
105
|
+
if (this.components.getConnectionManager().getConnectionList().length >= minConnections) {
|
|
106
|
+
this.autoDialTimeout = retimer(this._autoDial, this.options.autoDialInterval)
|
|
107
|
+
|
|
108
|
+
return
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Sort peers on whether we know protocols or public keys for them
|
|
112
|
+
const allPeers = await this.components.getPeerStore().all()
|
|
113
|
+
|
|
114
|
+
const peers = await pipe(
|
|
115
|
+
// shuffle the peers
|
|
116
|
+
allPeers.sort(() => Math.random() > 0.5 ? 1 : -1),
|
|
117
|
+
(source) => filter(source, (peer) => !peer.id.equals(this.components.getPeerId())),
|
|
118
|
+
(source) => sort(source, (a, b) => {
|
|
119
|
+
if (b.protocols.length > a.protocols.length) {
|
|
120
|
+
return 1
|
|
121
|
+
} else if (b.id.publicKey != null && a.id.publicKey == null) {
|
|
122
|
+
return 1
|
|
123
|
+
}
|
|
124
|
+
return -1
|
|
125
|
+
}),
|
|
126
|
+
async (source) => await all(source)
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
for (let i = 0; this.running && i < peers.length && this.components.getConnectionManager().getConnectionList().length < minConnections; i++) {
|
|
130
|
+
// Connection Manager was stopped during async dial
|
|
131
|
+
if (!this.running) {
|
|
132
|
+
return
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const peer = peers[i]
|
|
136
|
+
|
|
137
|
+
if (this.components.getConnectionManager().getConnection(peer.id) == null) {
|
|
138
|
+
log('connecting to a peerStore stored peer %p', peer.id)
|
|
139
|
+
try {
|
|
140
|
+
await this.components.getDialer().dial(peer.id)
|
|
141
|
+
} catch (err: any) {
|
|
142
|
+
log.error('could not connect to peerStore stored peer', err)
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Connection Manager was stopped
|
|
148
|
+
if (!this.running) {
|
|
149
|
+
return
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
this.autoDialTimeout = retimer(this._autoDial, this.options.autoDialInterval)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
import { logger } from '@libp2p/logger'
|
|
2
|
+
import errCode from 'err-code'
|
|
3
|
+
import mergeOptions from 'merge-options'
|
|
4
|
+
import { LatencyMonitor, SummaryObject } from './latency-monitor.js'
|
|
5
|
+
// @ts-expect-error retimer does not have types
|
|
6
|
+
import retimer from 'retimer'
|
|
7
|
+
import { CustomEvent, EventEmitter, Startable } from '@libp2p/interfaces'
|
|
8
|
+
import { trackedMap } from '@libp2p/tracked-map'
|
|
9
|
+
import { codes } from '../errors.js'
|
|
10
|
+
import { isPeerId, PeerId } from '@libp2p/interfaces/peer-id'
|
|
11
|
+
// @ts-expect-error setMaxListeners is missing from the node 16 types
|
|
12
|
+
import { setMaxListeners } from 'events'
|
|
13
|
+
import type { Connection } from '@libp2p/interfaces/connection'
|
|
14
|
+
import type { ConnectionManager } from '@libp2p/interfaces/registrar'
|
|
15
|
+
import type { Components } from '@libp2p/interfaces/components'
|
|
16
|
+
import * as STATUS from '@libp2p/interfaces/connection/status'
|
|
17
|
+
|
|
18
|
+
const log = logger('libp2p:connection-manager')
|
|
19
|
+
|
|
20
|
+
const defaultOptions: Partial<ConnectionManagerInit> = {
|
|
21
|
+
maxConnections: Infinity,
|
|
22
|
+
minConnections: 0,
|
|
23
|
+
maxData: Infinity,
|
|
24
|
+
maxSentData: Infinity,
|
|
25
|
+
maxReceivedData: Infinity,
|
|
26
|
+
maxEventLoopDelay: Infinity,
|
|
27
|
+
pollInterval: 2000,
|
|
28
|
+
autoDialInterval: 10000,
|
|
29
|
+
movingAverageInterval: 60000,
|
|
30
|
+
defaultPeerValue: 1
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const METRICS_COMPONENT = 'connection-manager'
|
|
34
|
+
const METRICS_PEER_CONNECTIONS = 'peer-connections'
|
|
35
|
+
const METRICS_PEER_VALUES = 'peer-values'
|
|
36
|
+
|
|
37
|
+
export interface ConnectionManagerEvents {
|
|
38
|
+
'peer:connect': CustomEvent<PeerId>
|
|
39
|
+
'peer:disconnect': CustomEvent<PeerId>
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface ConnectionManagerInit {
|
|
43
|
+
/**
|
|
44
|
+
* The maximum number of connections allowed
|
|
45
|
+
*/
|
|
46
|
+
maxConnections?: number
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The minimum number of connections to avoid pruning
|
|
50
|
+
*/
|
|
51
|
+
minConnections?: number
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The max data (in and out), per average interval to allow
|
|
55
|
+
*/
|
|
56
|
+
maxData?: number
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The max outgoing data, per average interval to allow
|
|
60
|
+
*/
|
|
61
|
+
maxSentData?: number
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The max incoming data, per average interval to allow
|
|
65
|
+
*/
|
|
66
|
+
maxReceivedData?: number
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The upper limit the event loop can take to run
|
|
70
|
+
*/
|
|
71
|
+
maxEventLoopDelay?: number
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* How often, in milliseconds, metrics and latency should be checked
|
|
75
|
+
*/
|
|
76
|
+
pollInterval?: number
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* How often, in milliseconds, to compute averages
|
|
80
|
+
*/
|
|
81
|
+
movingAverageInterval?: number
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* The value of the peer
|
|
85
|
+
*/
|
|
86
|
+
defaultPeerValue?: number
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Should preemptively guarantee connections are above the low watermark
|
|
90
|
+
*/
|
|
91
|
+
autoDial?: boolean
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* How often, in milliseconds, it should preemptively guarantee connections are above the low watermark
|
|
95
|
+
*/
|
|
96
|
+
autoDialInterval?: number
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Responsible for managing known connections.
|
|
101
|
+
*/
|
|
102
|
+
export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEvents> implements ConnectionManager, Startable {
|
|
103
|
+
private readonly components: Components
|
|
104
|
+
private readonly init: Required<ConnectionManagerInit>
|
|
105
|
+
private readonly peerValues: Map<string, number>
|
|
106
|
+
private readonly connections: Map<string, Connection[]>
|
|
107
|
+
private started: boolean
|
|
108
|
+
private timer?: ReturnType<retimer>
|
|
109
|
+
private readonly latencyMonitor: LatencyMonitor
|
|
110
|
+
|
|
111
|
+
constructor (components: Components, init: ConnectionManagerInit = {}) {
|
|
112
|
+
super()
|
|
113
|
+
|
|
114
|
+
this.components = components
|
|
115
|
+
this.init = mergeOptions.call({ ignoreUndefined: true }, defaultOptions, init)
|
|
116
|
+
|
|
117
|
+
if (this.init.maxConnections < this.init.minConnections) {
|
|
118
|
+
throw errCode(new Error('Connection Manager maxConnections must be greater than minConnections'), codes.ERR_INVALID_PARAMETERS)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
log('options: %o', this.init)
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Map of peer identifiers to their peer value for pruning connections.
|
|
125
|
+
*
|
|
126
|
+
* @type {Map<string, number>}
|
|
127
|
+
*/
|
|
128
|
+
this.peerValues = trackedMap({
|
|
129
|
+
component: METRICS_COMPONENT,
|
|
130
|
+
metric: METRICS_PEER_VALUES,
|
|
131
|
+
metrics: this.components.getMetrics()
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Map of connections per peer
|
|
136
|
+
*/
|
|
137
|
+
this.connections = trackedMap({
|
|
138
|
+
component: METRICS_COMPONENT,
|
|
139
|
+
metric: METRICS_PEER_CONNECTIONS,
|
|
140
|
+
metrics: this.components.getMetrics()
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
this.started = false
|
|
144
|
+
this._checkMetrics = this._checkMetrics.bind(this)
|
|
145
|
+
|
|
146
|
+
this.latencyMonitor = new LatencyMonitor({
|
|
147
|
+
latencyCheckIntervalMs: init.pollInterval,
|
|
148
|
+
dataEmitIntervalMs: init.pollInterval
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
try {
|
|
152
|
+
// This emitter gets listened to a lot
|
|
153
|
+
setMaxListeners?.(Infinity, this)
|
|
154
|
+
} catch {}
|
|
155
|
+
|
|
156
|
+
this.components.getUpgrader().addEventListener('connection', (evt) => {
|
|
157
|
+
void this.onConnect(evt).catch(err => {
|
|
158
|
+
log.error(err)
|
|
159
|
+
})
|
|
160
|
+
})
|
|
161
|
+
this.components.getUpgrader().addEventListener('connectionEnd', this.onDisconnect.bind(this))
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
isStarted () {
|
|
165
|
+
return this.started
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Starts the Connection Manager. If Metrics are not enabled on libp2p
|
|
170
|
+
* only event loop and connection limits will be monitored.
|
|
171
|
+
*/
|
|
172
|
+
async start () {
|
|
173
|
+
if (this.components.getMetrics() != null) {
|
|
174
|
+
this.timer = this.timer ?? retimer(this._checkMetrics, this.init.pollInterval)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// latency monitor
|
|
178
|
+
this.latencyMonitor.start()
|
|
179
|
+
this._onLatencyMeasure = this._onLatencyMeasure.bind(this)
|
|
180
|
+
this.latencyMonitor.addEventListener('data', this._onLatencyMeasure)
|
|
181
|
+
|
|
182
|
+
this.started = true
|
|
183
|
+
log('started')
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Stops the Connection Manager
|
|
188
|
+
*/
|
|
189
|
+
async stop () {
|
|
190
|
+
this.timer?.clear()
|
|
191
|
+
|
|
192
|
+
this.latencyMonitor.removeEventListener('data', this._onLatencyMeasure)
|
|
193
|
+
this.latencyMonitor.stop()
|
|
194
|
+
|
|
195
|
+
this.started = false
|
|
196
|
+
await this._close()
|
|
197
|
+
log('stopped')
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Cleans up the connections
|
|
202
|
+
*/
|
|
203
|
+
async _close () {
|
|
204
|
+
// Close all connections we're tracking
|
|
205
|
+
const tasks = []
|
|
206
|
+
for (const connectionList of this.connections.values()) {
|
|
207
|
+
for (const connection of connectionList) {
|
|
208
|
+
tasks.push(connection.close())
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
log('closing %d connections', tasks.length)
|
|
213
|
+
await Promise.all(tasks)
|
|
214
|
+
this.connections.clear()
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Sets the value of the given peer. Peers with lower values
|
|
219
|
+
* will be disconnected first.
|
|
220
|
+
*/
|
|
221
|
+
setPeerValue (peerId: PeerId, value: number) {
|
|
222
|
+
if (value < 0 || value > 1) {
|
|
223
|
+
throw new Error('value should be a number between 0 and 1')
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
this.peerValues.set(peerId.toString(), value)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Checks the libp2p metrics to determine if any values have exceeded
|
|
231
|
+
* the configured maximums.
|
|
232
|
+
*
|
|
233
|
+
* @private
|
|
234
|
+
*/
|
|
235
|
+
async _checkMetrics () {
|
|
236
|
+
const metrics = this.components.getMetrics()
|
|
237
|
+
|
|
238
|
+
if (metrics != null) {
|
|
239
|
+
try {
|
|
240
|
+
const movingAverages = metrics.getGlobal().getMovingAverages()
|
|
241
|
+
const received = movingAverages.dataReceived[this.init.movingAverageInterval].movingAverage
|
|
242
|
+
await this._checkMaxLimit('maxReceivedData', received)
|
|
243
|
+
const sent = movingAverages.dataSent[this.init.movingAverageInterval].movingAverage
|
|
244
|
+
await this._checkMaxLimit('maxSentData', sent)
|
|
245
|
+
const total = received + sent
|
|
246
|
+
await this._checkMaxLimit('maxData', total)
|
|
247
|
+
log('metrics update', total)
|
|
248
|
+
} finally {
|
|
249
|
+
this.timer = retimer(this._checkMetrics, this.init.pollInterval)
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Tracks the incoming connection and check the connection limit
|
|
256
|
+
*/
|
|
257
|
+
async onConnect (evt: CustomEvent<Connection>) {
|
|
258
|
+
const { detail: connection } = evt
|
|
259
|
+
|
|
260
|
+
if (!this.started) {
|
|
261
|
+
// This can happen when we are in the process of shutting down the node
|
|
262
|
+
await connection.close()
|
|
263
|
+
return
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const peerId = connection.remotePeer
|
|
267
|
+
const peerIdStr = peerId.toString()
|
|
268
|
+
const storedConns = this.connections.get(peerIdStr)
|
|
269
|
+
|
|
270
|
+
this.dispatchEvent(new CustomEvent<Connection>('peer:connect', { detail: connection }))
|
|
271
|
+
|
|
272
|
+
if (storedConns != null) {
|
|
273
|
+
storedConns.push(connection)
|
|
274
|
+
} else {
|
|
275
|
+
this.connections.set(peerIdStr, [connection])
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
if (peerId.publicKey != null) {
|
|
279
|
+
await this.components.getPeerStore().keyBook.set(peerId, peerId.publicKey)
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (!this.peerValues.has(peerIdStr)) {
|
|
283
|
+
this.peerValues.set(peerIdStr, this.init.defaultPeerValue)
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
await this._checkMaxLimit('maxConnections', this.getConnectionList().length)
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Removes the connection from tracking
|
|
291
|
+
*/
|
|
292
|
+
onDisconnect (evt: CustomEvent<Connection>) {
|
|
293
|
+
const { detail: connection } = evt
|
|
294
|
+
|
|
295
|
+
if (!this.started) {
|
|
296
|
+
// This can happen when we are in the process of shutting down the node
|
|
297
|
+
return
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const peerId = connection.remotePeer.toString()
|
|
301
|
+
let storedConn = this.connections.get(peerId)
|
|
302
|
+
|
|
303
|
+
if (storedConn != null && storedConn.length > 1) {
|
|
304
|
+
storedConn = storedConn.filter((conn) => conn.id !== connection.id)
|
|
305
|
+
this.connections.set(peerId, storedConn)
|
|
306
|
+
} else if (storedConn != null) {
|
|
307
|
+
this.connections.delete(peerId)
|
|
308
|
+
this.peerValues.delete(connection.remotePeer.toString())
|
|
309
|
+
this.dispatchEvent(new CustomEvent<Connection>('peer:disconnect', { detail: connection }))
|
|
310
|
+
|
|
311
|
+
this.components.getMetrics()?.onPeerDisconnected(connection.remotePeer)
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
getConnectionMap (): Map<string, Connection[]> {
|
|
316
|
+
return this.connections
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
getConnectionList (): Connection[] {
|
|
320
|
+
let output: Connection[] = []
|
|
321
|
+
|
|
322
|
+
for (const connections of this.connections.values()) {
|
|
323
|
+
output = output.concat(connections)
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
return output
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
getConnections (peerId: PeerId): Connection[] {
|
|
330
|
+
return this.connections.get(peerId.toString()) ?? []
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Get a connection with a peer
|
|
335
|
+
*/
|
|
336
|
+
getConnection (peerId: PeerId): Connection | undefined {
|
|
337
|
+
const connections = this.getAll(peerId)
|
|
338
|
+
|
|
339
|
+
if (connections.length > 0) {
|
|
340
|
+
return connections[0]
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
return undefined
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Get all open connections with a peer
|
|
348
|
+
*/
|
|
349
|
+
getAll (peerId: PeerId): Connection[] {
|
|
350
|
+
if (!isPeerId(peerId)) {
|
|
351
|
+
throw errCode(new Error('peerId must be an instance of peer-id'), codes.ERR_INVALID_PARAMETERS)
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const id = peerId.toString()
|
|
355
|
+
const connections = this.connections.get(id)
|
|
356
|
+
|
|
357
|
+
// Return all open connections
|
|
358
|
+
if (connections != null) {
|
|
359
|
+
return connections.filter(connection => connection.stat.status === STATUS.OPEN)
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
return []
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* If the event loop is slow, maybe close a connection
|
|
367
|
+
*/
|
|
368
|
+
_onLatencyMeasure (evt: CustomEvent<SummaryObject>) {
|
|
369
|
+
const { detail: summary } = evt
|
|
370
|
+
|
|
371
|
+
this._checkMaxLimit('maxEventLoopDelay', summary.avgMs)
|
|
372
|
+
.catch(err => {
|
|
373
|
+
log.error(err)
|
|
374
|
+
})
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* If the `value` of `name` has exceeded its limit, maybe close a connection
|
|
379
|
+
*/
|
|
380
|
+
async _checkMaxLimit (name: keyof ConnectionManagerInit, value: number) {
|
|
381
|
+
const limit = this.init[name]
|
|
382
|
+
log.trace('checking limit of %s. current value: %d of %d', name, value, limit)
|
|
383
|
+
if (value > limit) {
|
|
384
|
+
log('%s: limit exceeded: %p, %d', this.components.getPeerId(), name, value)
|
|
385
|
+
await this._maybeDisconnectOne()
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* If we have more connections than our maximum, close a connection
|
|
391
|
+
* to the lowest valued peer.
|
|
392
|
+
*/
|
|
393
|
+
async _maybeDisconnectOne () {
|
|
394
|
+
if (this.init.minConnections < this.connections.size) {
|
|
395
|
+
const peerValues = Array.from(new Map([...this.peerValues.entries()].sort((a, b) => a[1] - b[1])))
|
|
396
|
+
|
|
397
|
+
log('%p: sorted peer values: %j', this.components.getPeerId(), peerValues)
|
|
398
|
+
const disconnectPeer = peerValues[0]
|
|
399
|
+
|
|
400
|
+
if (disconnectPeer != null) {
|
|
401
|
+
const peerId = disconnectPeer[0]
|
|
402
|
+
log('%p: lowest value peer is %s', this.components.getPeerId(), peerId)
|
|
403
|
+
log('%p: closing a connection to %j', this.components.getPeerId(), peerId)
|
|
404
|
+
|
|
405
|
+
for (const connections of this.connections.values()) {
|
|
406
|
+
if (connections[0].remotePeer.toString() === peerId) {
|
|
407
|
+
void connections[0].close()
|
|
408
|
+
.catch(err => {
|
|
409
|
+
log.error(err)
|
|
410
|
+
})
|
|
411
|
+
|
|
412
|
+
// TODO: should not need to invoke this manually
|
|
413
|
+
this.onDisconnect(new CustomEvent<Connection>('connectionEnd', {
|
|
414
|
+
detail: connections[0]
|
|
415
|
+
}))
|
|
416
|
+
break
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
}
|