@@ -407,7 +407,7 @@ class Gossipsub extends Pubsub {
407
407
async _processRpc ( id : string , peerStreams : PeerStreams , rpc : RPC ) : Promise < boolean > {
408
408
if ( await super . _processRpc ( id , peerStreams , rpc ) ) {
409
409
if ( rpc . control ) {
410
- this . _processRpcControlMessage ( id , rpc . control )
410
+ await this . _processRpcControlMessage ( id , rpc . control )
411
411
}
412
412
return true
413
413
}
@@ -420,14 +420,14 @@ class Gossipsub extends Pubsub {
420
420
* @param {RPC.IControlMessage } controlMsg
421
421
* @returns {void }
422
422
*/
423
- _processRpcControlMessage ( id : string , controlMsg : RPC . IControlMessage ) : void {
423
+ async _processRpcControlMessage ( id : string , controlMsg : RPC . IControlMessage ) : Promise < void > {
424
424
if ( ! controlMsg ) {
425
425
return
426
426
}
427
427
428
428
const iwant = controlMsg . ihave ? this . _handleIHave ( id , controlMsg . ihave ) : [ ]
429
429
const ihave = controlMsg . iwant ? this . _handleIWant ( id , controlMsg . iwant ) : [ ]
430
- const prune = controlMsg . graft ? this . _handleGraft ( id , controlMsg . graft ) : [ ]
430
+ const prune = controlMsg . graft ? await this . _handleGraft ( id , controlMsg . graft ) : [ ]
431
431
controlMsg . prune && this . _handlePrune ( id , controlMsg . prune )
432
432
433
433
if ( ! iwant . length && ! ihave . length && ! prune . length ) {
@@ -652,9 +652,9 @@ class Gossipsub extends Pubsub {
652
652
* Handles Graft messages
653
653
* @param {string } id peer id
654
654
* @param {Array<RPC.IControlGraft> } graft
655
- * @return {Array <RPC.IControlPrune> }
655
+ * @return {Promise <RPC.IControlPrune[] > }
656
656
*/
657
- _handleGraft ( id : string , graft : RPC . IControlGraft [ ] ) : RPC . IControlPrune [ ] {
657
+ async _handleGraft ( id : string , graft : RPC . IControlGraft [ ] ) : Promise < RPC . IControlPrune [ ] > {
658
658
const prune : string [ ] = [ ]
659
659
const score = this . score . score ( id )
660
660
const now = this . _now ( )
@@ -741,7 +741,7 @@ class Gossipsub extends Pubsub {
741
741
return [ ]
742
742
}
743
743
744
- return prune . map ( topic => this . _makePrune ( id , topic , doPX ) )
744
+ return Promise . all ( prune . map ( topic => this . _makePrune ( id , topic , doPX ) ) )
745
745
}
746
746
747
747
/**
@@ -939,10 +939,10 @@ class Gossipsub extends Pubsub {
939
939
* Mounts the gossipsub protocol onto the libp2p node and sends our
940
940
* our subscriptions to every peer connected
941
941
* @override
942
- * @returns {void }
942
+ * @returns {Promise< void> }
943
943
*/
944
- start ( ) : void {
945
- super . start ( )
944
+ async start ( ) : Promise < void > {
945
+ await super . start ( )
946
946
this . heartbeat . start ( )
947
947
this . score . start ( )
948
948
// connect to direct peers
@@ -956,10 +956,10 @@ class Gossipsub extends Pubsub {
956
956
/**
957
957
* Unmounts the gossipsub protocol and shuts down every connection
958
958
* @override
959
- * @returns {void }
959
+ * @returns {Promise< void> }
960
960
*/
961
- stop ( ) : void {
962
- super . stop ( )
961
+ async stop ( ) : Promise < void > {
962
+ await super . stop ( )
963
963
this . heartbeat . stop ( )
964
964
this . score . stop ( )
965
965
@@ -1188,11 +1188,11 @@ class Gossipsub extends Pubsub {
1188
1188
* Sends a PRUNE message to a peer
1189
1189
* @param {string } id peer id
1190
1190
* @param {string } topic
1191
- * @returns {void }
1191
+ * @returns {Promise< void> }
1192
1192
*/
1193
- _sendPrune ( id : string , topic : string ) : void {
1193
+ async _sendPrune ( id : string , topic : string ) : Promise < void > {
1194
1194
const prune = [
1195
- this . _makePrune ( id , topic , this . _options . doPX )
1195
+ await this . _makePrune ( id , topic , this . _options . doPX )
1196
1196
]
1197
1197
1198
1198
const out = createGossipRpc ( [ ] , { prune } )
@@ -1255,23 +1255,23 @@ class Gossipsub extends Pubsub {
1255
1255
* @param {Map<string, Array<string>> } tograft peer id => topic[]
1256
1256
* @param {Map<string, Array<string>> } toprune peer id => topic[]
1257
1257
*/
1258
- _sendGraftPrune ( tograft : Map < string , string [ ] > , toprune : Map < string , string [ ] > , noPX : Map < string , boolean > ) : void {
1258
+ async _sendGraftPrune ( tograft : Map < string , string [ ] > , toprune : Map < string , string [ ] > , noPX : Map < string , boolean > ) : Promise < void > {
1259
1259
const doPX = this . _options . doPX
1260
1260
for ( const [ id , topics ] of tograft ) {
1261
1261
const graft = topics . map ( ( topicID ) => ( { topicID } ) )
1262
1262
let prune : RPC . IControlPrune [ ] = [ ]
1263
1263
// If a peer also has prunes, process them now
1264
1264
const pruning = toprune . get ( id )
1265
1265
if ( pruning ) {
1266
- prune = pruning . map ( ( topicID ) => this . _makePrune ( id , topicID , doPX && ! noPX . get ( id ) ) )
1266
+ prune = await Promise . all ( pruning . map ( ( topicID ) => this . _makePrune ( id , topicID , doPX && ! noPX . get ( id ) ) ) )
1267
1267
toprune . delete ( id )
1268
1268
}
1269
1269
1270
1270
const outRpc = createGossipRpc ( [ ] , { graft, prune } )
1271
1271
this . _sendRpc ( id , outRpc )
1272
1272
}
1273
1273
for ( const [ id , topics ] of toprune ) {
1274
- const prune = topics . map ( ( topicID ) => this . _makePrune ( id , topicID , doPX && ! noPX . get ( id ) ) )
1274
+ const prune = await Promise . all ( topics . map ( ( topicID ) => this . _makePrune ( id , topicID , doPX && ! noPX . get ( id ) ) ) )
1275
1275
const outRpc = createGossipRpc ( [ ] , { prune } )
1276
1276
this . _sendRpc ( id , outRpc )
1277
1277
}
@@ -1392,9 +1392,9 @@ class Gossipsub extends Pubsub {
1392
1392
* @param {string } id
1393
1393
* @param {string } topic
1394
1394
* @param {boolean } doPX
1395
- * @returns {RPC.IControlPrune }
1395
+ * @returns {Promise< RPC.IControlPrune> }
1396
1396
*/
1397
- _makePrune ( id : string , topic : string , doPX : boolean ) : RPC . IControlPrune {
1397
+ async _makePrune ( id : string , topic : string , doPX : boolean ) : Promise < RPC . IControlPrune > {
1398
1398
if ( this . peers . get ( id ) ! . protocol === constants . GossipsubIDv10 ) {
1399
1399
// Gossipsub v1.0 -- no backoff, the peer won't be able to parse it anyway
1400
1400
return {
@@ -1411,17 +1411,17 @@ class Gossipsub extends Pubsub {
1411
1411
const peers = getGossipPeers ( this , topic , constants . GossipsubPrunePeers , ( xid : string ) : boolean => {
1412
1412
return xid !== id && this . score . score ( xid ) >= 0
1413
1413
} )
1414
- peers . forEach ( p => {
1414
+ for ( const p of peers ) {
1415
1415
// see if we have a signed record to send back; if we don't, just send
1416
1416
// the peer ID and let the pruned peer find them in the DHT -- we can't trust
1417
1417
// unsigned address records through PX anyways
1418
1418
// Finding signed records in the DHT is not supported at the time of writing in js-libp2p
1419
1419
const peerId = PeerId . createFromB58String ( p )
1420
1420
px . push ( {
1421
1421
peerID : peerId . toBytes ( ) ,
1422
- signedPeerRecord : this . _libp2p . peerStore . addressBook . getRawEnvelope ( peerId )
1422
+ signedPeerRecord : await this . _libp2p . peerStore . addressBook . getRawEnvelope ( peerId )
1423
1423
} )
1424
- } )
1424
+ }
1425
1425
}
1426
1426
return {
1427
1427
topicID : topic ,
0 commit comments