1
1
import type { PeerId } from "@libp2p/interface" ;
2
2
import { IncomingStreamData } from "@libp2p/interface" ;
3
- import { encodeRelayShard } from "@waku/enr" ;
4
3
import type {
5
4
IMetadata ,
6
5
Libp2pComponents ,
6
+ PeerIdStr ,
7
7
ShardInfo ,
8
8
ShardingParams
9
9
} from "@waku/interfaces" ;
10
10
import { proto_metadata } from "@waku/proto" ;
11
- import { Logger } from "@waku/utils" ;
11
+ import { encodeRelayShard , Logger } from "@waku/utils" ;
12
12
import all from "it-all" ;
13
13
import * as lp from "it-length-prefixed" ;
14
14
import { pipe } from "it-pipe" ;
@@ -20,13 +20,16 @@ const log = new Logger("metadata");
20
20
21
21
export const MetadataCodec = "/vac/waku/metadata/1.0.0" ;
22
22
23
- class Metadata extends BaseProtocol {
24
- private readonly shardInfo : ShardingParams ;
23
+ class Metadata extends BaseProtocol implements IMetadata {
25
24
private libp2pComponents : Libp2pComponents ;
26
- constructor ( shardInfo : ShardingParams , libp2p : Libp2pComponents ) {
27
- super ( MetadataCodec , libp2p . components , log ) ;
25
+ handshakesConfirmed : Set < PeerIdStr > = new Set ( ) ;
26
+
27
+ constructor (
28
+ public shardInfo : ShardingParams ,
29
+ libp2p : Libp2pComponents
30
+ ) {
31
+ super ( MetadataCodec , libp2p . components , log , shardInfo && { shardInfo } ) ;
28
32
this . libp2pComponents = libp2p ;
29
- this . shardInfo = shardInfo ;
30
33
void libp2p . registrar . handle ( MetadataCodec , ( streamData ) => {
31
34
void this . onRequest ( streamData ) ;
32
35
} ) ;
@@ -53,12 +56,10 @@ class Metadata extends BaseProtocol {
53
56
const remoteShardInfoResponse =
54
57
this . decodeMetadataResponse ( encodedResponse ) ;
55
58
56
- // add or update the shardInfo to peer store
57
- await this . libp2pComponents . peerStore . merge ( connection . remotePeer , {
58
- metadata : {
59
- shardInfo : encodeRelayShard ( remoteShardInfoResponse )
60
- }
61
- } ) ;
59
+ await this . savePeerShardInfo (
60
+ connection . remotePeer ,
61
+ remoteShardInfoResponse
62
+ ) ;
62
63
} catch ( error ) {
63
64
log . error ( "Error handling metadata request" , error ) ;
64
65
}
@@ -87,9 +88,19 @@ class Metadata extends BaseProtocol {
87
88
88
89
const decodedResponse = this . decodeMetadataResponse ( encodedResponse ) ;
89
90
91
+ await this . savePeerShardInfo ( peerId , decodedResponse ) ;
92
+
90
93
return decodedResponse ;
91
94
}
92
95
96
+ public async confirmOrAttemptHandshake ( peerId : PeerId ) : Promise < void > {
97
+ if ( this . handshakesConfirmed . has ( peerId . toString ( ) ) ) return ;
98
+
99
+ await this . query ( peerId ) ;
100
+
101
+ return ;
102
+ }
103
+
93
104
private decodeMetadataResponse ( encodedResponse : Uint8ArrayList [ ] ) : ShardInfo {
94
105
const bytes = new Uint8ArrayList ( ) ;
95
106
@@ -104,6 +115,20 @@ class Metadata extends BaseProtocol {
104
115
105
116
return response ;
106
117
}
118
+
119
+ private async savePeerShardInfo (
120
+ peerId : PeerId ,
121
+ shardInfo : ShardInfo
122
+ ) : Promise < void > {
123
+ // add or update the shardInfo to peer store
124
+ await this . libp2pComponents . peerStore . merge ( peerId , {
125
+ metadata : {
126
+ shardInfo : encodeRelayShard ( shardInfo )
127
+ }
128
+ } ) ;
129
+
130
+ this . handshakesConfirmed . add ( peerId . toString ( ) ) ;
131
+ }
107
132
}
108
133
109
134
export function wakuMetadata (
0 commit comments