1
1
import type { PeerId } from "@libp2p/interface/peer-id" ;
2
+ import type { IncomingStreamData } from "@libp2p/interface-internal/registrar" ;
2
3
import type {
3
4
IMetadata ,
4
- Libp2p ,
5
+ Libp2pComponents ,
5
6
MetadataQueryParams ,
6
7
ShardInfo
7
8
} from "@waku/interfaces" ;
9
+ import { proto_metadata as proto } from "@waku/proto" ;
8
10
import { Logger } from "@waku/utils" ;
9
11
import all from "it-all" ;
10
12
import * as lp from "it-length-prefixed" ;
@@ -13,29 +15,77 @@ import { Uint8ArrayList } from "uint8arraylist";
13
15
14
16
import { BaseProtocol } from "../base_protocol.js" ;
15
17
16
- import { MetadataRpc } from "./rpc.js" ;
17
-
18
18
const log = new Logger ( "metadata" ) ;
19
19
20
20
export const MetadataCodec = "/vac/waku/metadata/1.0.0" ;
21
21
22
22
class Metadata extends BaseProtocol {
23
- constructor ( libp2p : Libp2p ) {
24
- super ( MetadataCodec , libp2p . components ) ;
23
+ constructor (
24
+ libp2pComponents : Libp2pComponents ,
25
+ private shardInfo : ShardInfo
26
+ ) {
27
+ super ( MetadataCodec , libp2pComponents ) ;
28
+
29
+ libp2pComponents . registrar
30
+ . handle ( MetadataCodec , ( streamData ) => {
31
+ return void this . handle ( streamData ) ;
32
+ } )
33
+ . then ( ( ) => "yes" )
34
+ . catch ( ( ) => "no" ) ;
35
+ }
36
+
37
+ private async handle ( streamData : IncomingStreamData ) : Promise < ShardInfo > {
38
+ const encodedRpcQuery = proto . WakuMetadataRequest . encode ( this . shardInfo ) ;
39
+
40
+ const res = await pipe (
41
+ [ encodedRpcQuery ] ,
42
+ lp . encode ,
43
+ streamData . stream ,
44
+ lp . decode ,
45
+ async ( source ) => await all ( source )
46
+ ) ;
47
+
48
+ try {
49
+ const bytes = new Uint8ArrayList ( ) ;
50
+ res . forEach ( ( chunk ) => {
51
+ bytes . append ( chunk ) ;
52
+ } ) ;
53
+
54
+ const response = proto . WakuMetadataResponse . decode ( bytes ) ;
55
+ if ( ! response ) {
56
+ throw new Error ( "WakuMetadata response is undefined" ) ;
57
+ }
58
+
59
+ const { shards, clusterId } = response ;
60
+
61
+ if ( ! clusterId ) {
62
+ throw new Error ( "WakuMetadata response clusterId is undefined" ) ;
63
+ }
64
+
65
+ return {
66
+ clusterId,
67
+ shards
68
+ } ;
69
+ } catch ( e ) {
70
+ log . error ( `Error decoding response: ${ e } ` ) ;
71
+ throw e ;
72
+ }
25
73
}
26
74
27
75
/**
28
76
* Make a metadata query to a peer
29
77
*/
30
78
async query ( params : MetadataQueryParams , peerId : PeerId ) : Promise < ShardInfo > {
31
- const rpcQuery = MetadataRpc . createRequest ( params . clusterId , params . shards ) ;
79
+ const encodedRpcQuery = proto . WakuMetadataRequest . encode ( {
80
+ clusterId : params . clusterId ,
81
+ shards : params . shards
82
+ } ) ;
32
83
33
84
const peer = await this . getPeer ( peerId ) ;
34
-
35
85
const stream = await this . getStream ( peer ) ;
36
86
37
87
const res = await pipe (
38
- [ rpcQuery . encode ( ) ] ,
88
+ [ encodedRpcQuery ] ,
39
89
lp . encode ,
40
90
stream ,
41
91
lp . decode ,
@@ -48,23 +98,30 @@ class Metadata extends BaseProtocol {
48
98
bytes . append ( chunk ) ;
49
99
} ) ;
50
100
51
- const { response } = MetadataRpc . decode ( bytes ) ;
101
+ const response = proto . WakuMetadataResponse . decode ( bytes ) ;
52
102
if ( ! response ) {
53
- throw new Error ( "No response in query " ) ;
103
+ throw new Error ( "WakuMetadata response is undefined " ) ;
54
104
}
55
105
56
106
const { shards, clusterId } = response ;
107
+ if ( ! clusterId ) {
108
+ throw new Error ( "WakuMetadata response clusterId is undefined" ) ;
109
+ }
110
+
57
111
return {
58
- cluster : clusterId ,
59
- indexList : shards
60
- } as ShardInfo ;
112
+ clusterId,
113
+ shards
114
+ } ;
61
115
} catch ( e ) {
62
116
log . error ( `Error decoding response: ${ e } ` ) ;
63
117
throw e ;
64
118
}
65
119
}
66
120
}
67
121
68
- export function wakuMetadata ( ) : ( libp2p : Libp2p ) => IMetadata {
69
- return ( libp2p : Libp2p ) => new Metadata ( libp2p ) ;
122
+ export function wakuMetadata (
123
+ shardInfo : ShardInfo
124
+ ) : ( components : Libp2pComponents ) => IMetadata {
125
+ return ( libp2pComponents : Libp2pComponents ) =>
126
+ new Metadata ( libp2pComponents , shardInfo ) ;
70
127
}
0 commit comments