1
1
import { MetadataCodec } from "@waku/core" ;
2
+ import { decodeRelayShard } from "@waku/enr" ;
2
3
import type { LightNode , ShardInfo } from "@waku/interfaces" ;
3
4
import { createLightNode } from "@waku/sdk" ;
4
5
import { shardInfoToPubsubTopics } from "@waku/utils" ;
@@ -11,7 +12,7 @@ import { NimGoNode } from "../src/node/node.js";
11
12
12
13
chai . use ( chaiAsPromised ) ;
13
14
14
- describe . only ( "Metadata Protocol" , ( ) => {
15
+ describe ( "Metadata Protocol" , ( ) => {
15
16
let waku : LightNode ;
16
17
let nwaku1 : NimGoNode ;
17
18
@@ -24,142 +25,180 @@ describe.only("Metadata Protocol", () => {
24
25
await tearDownNodes ( [ nwaku1 ] , waku ) ;
25
26
} ) ;
26
27
27
- it ( "same cluster, same shard: nodes connect" , async function ( ) {
28
- this . timeout ( 55_000 ) ;
29
-
30
- const shardInfo : ShardInfo = {
31
- clusterId : 1 ,
32
- shards : [ 1 ]
33
- } ;
28
+ describe ( "connections" , function ( ) {
29
+ it ( "same cluster, same shard: nodes connect" , async function ( ) {
30
+ this . timeout ( 55_000 ) ;
31
+
32
+ const shardInfo : ShardInfo = {
33
+ clusterId : 1 ,
34
+ shards : [ 1 ]
35
+ } ;
36
+
37
+ await nwaku1 . start ( {
38
+ relay : true ,
39
+ discv5Discovery : true ,
40
+ peerExchange : true ,
41
+ clusterId : shardInfo . clusterId ,
42
+ pubsubTopic : shardInfoToPubsubTopics ( shardInfo )
43
+ } ) ;
44
+
45
+ const nwaku1Ma = await nwaku1 . getMultiaddrWithId ( ) ;
46
+ const nwaku1PeerId = await nwaku1 . getPeerId ( ) ;
47
+
48
+ waku = await createLightNode ( { shardInfo } ) ;
49
+ await waku . start ( ) ;
50
+ await waku . libp2p . dialProtocol ( nwaku1Ma , MetadataCodec ) ;
51
+
52
+ const shardInfoRes =
53
+ await waku . libp2p . services . metadata ?. query ( nwaku1PeerId ) ;
54
+ expect ( shardInfoRes ) . to . not . be . undefined ;
55
+ expect ( shardInfoRes ?. clusterId ) . to . equal ( shardInfo . clusterId ) ;
56
+ expect ( shardInfoRes ?. shards ) . to . deep . equal ( shardInfo . shards ) ;
57
+
58
+ const activeConnections = waku . libp2p . getConnections ( ) ;
59
+ expect ( activeConnections . length ) . to . equal ( 1 ) ;
60
+ } ) ;
34
61
35
- await nwaku1 . start ( {
36
- relay : true ,
37
- discv5Discovery : true ,
38
- peerExchange : true ,
39
- clusterId : shardInfo . clusterId ,
40
- pubsubTopic : shardInfoToPubsubTopics ( shardInfo )
62
+ it ( "same cluster, different shard: nodes connect" , async function ( ) {
63
+ this . timeout ( 55_000 ) ;
64
+
65
+ const shardInfo1 : ShardInfo = {
66
+ clusterId : 1 ,
67
+ shards : [ 1 ]
68
+ } ;
69
+
70
+ const shardInfo2 : ShardInfo = {
71
+ clusterId : 1 ,
72
+ shards : [ 2 ]
73
+ } ;
74
+
75
+ await nwaku1 . start ( {
76
+ relay : true ,
77
+ discv5Discovery : true ,
78
+ peerExchange : true ,
79
+ clusterId : shardInfo1 . clusterId ,
80
+ pubsubTopic : shardInfoToPubsubTopics ( shardInfo1 )
81
+ } ) ;
82
+
83
+ const nwaku1Ma = await nwaku1 . getMultiaddrWithId ( ) ;
84
+ const nwaku1PeerId = await nwaku1 . getPeerId ( ) ;
85
+
86
+ waku = await createLightNode ( { shardInfo : shardInfo2 } ) ;
87
+ await waku . start ( ) ;
88
+ await waku . libp2p . dialProtocol ( nwaku1Ma , MetadataCodec ) ;
89
+
90
+ const shardInfoRes =
91
+ await waku . libp2p . services . metadata ?. query ( nwaku1PeerId ) ;
92
+ expect ( shardInfoRes ) . to . not . be . undefined ;
93
+ expect ( shardInfoRes ?. clusterId ) . to . equal ( shardInfo1 . clusterId ) ;
94
+ expect ( shardInfoRes ?. shards ) . to . deep . equal ( shardInfo1 . shards ) ;
95
+
96
+ const activeConnections = waku . libp2p . getConnections ( ) ;
97
+ expect ( activeConnections . length ) . to . equal ( 1 ) ;
41
98
} ) ;
42
99
43
- const nwaku1Ma = await nwaku1 . getMultiaddrWithId ( ) ;
44
- const nwaku1PeerId = await nwaku1 . getPeerId ( ) ;
100
+ it ( "different cluster, same shard: nodes don't connect" , async function ( ) {
101
+ this . timeout ( 55_000 ) ;
45
102
46
- waku = await createLightNode ( { shardInfo } ) ;
47
- await waku . start ( ) ;
48
- await waku . libp2p . dialProtocol ( nwaku1Ma , MetadataCodec ) ;
103
+ const shardInfo1 : ShardInfo = {
104
+ clusterId : 1 ,
105
+ shards : [ 1 ]
106
+ } ;
49
107
50
- const shardInfoRes =
51
- await waku . libp2p . services . metadata ?. query ( nwaku1PeerId ) ;
52
- expect ( shardInfoRes ) . to . not . be . undefined ;
53
- expect ( shardInfoRes ?. clusterId ) . to . equal ( shardInfo . clusterId ) ;
54
- expect ( shardInfoRes ?. shards ) . to . deep . equal ( shardInfo . shards ) ;
108
+ const shardInfo2 : ShardInfo = {
109
+ clusterId : 2 ,
110
+ shards : [ 1 ]
111
+ } ;
55
112
56
- const activeConnections = waku . libp2p . getConnections ( ) ;
57
- expect ( activeConnections . length ) . to . equal ( 1 ) ;
58
- } ) ;
113
+ await nwaku1 . start ( {
114
+ relay : true ,
115
+ discv5Discovery : true ,
116
+ peerExchange : true ,
117
+ clusterId : shardInfo1 . clusterId ,
118
+ pubsubTopic : shardInfoToPubsubTopics ( shardInfo1 )
119
+ } ) ;
59
120
60
- it ( "same cluster, different shard: nodes connect" , async function ( ) {
61
- this . timeout ( 55_000 ) ;
121
+ const nwaku1Ma = await nwaku1 . getMultiaddrWithId ( ) ;
62
122
63
- const shardInfo1 : ShardInfo = {
64
- clusterId : 1 ,
65
- shards : [ 1 ]
66
- } ;
123
+ waku = await createLightNode ( { shardInfo : shardInfo2 } ) ;
124
+ await waku . start ( ) ;
125
+ await waku . libp2p . dialProtocol ( nwaku1Ma , MetadataCodec ) ;
67
126
68
- const shardInfo2 : ShardInfo = {
69
- clusterId : 1 ,
70
- shards : [ 2 ]
71
- } ;
127
+ // add a delay to make sure the connection is closed from the other side
128
+ await delay ( 100 ) ;
72
129
73
- await nwaku1 . start ( {
74
- relay : true ,
75
- discv5Discovery : true ,
76
- peerExchange : true ,
77
- clusterId : shardInfo1 . clusterId ,
78
- pubsubTopic : shardInfoToPubsubTopics ( shardInfo1 )
130
+ const activeConnections = waku . libp2p . getConnections ( ) ;
131
+ expect ( activeConnections . length ) . to . equal ( 0 ) ;
79
132
} ) ;
80
133
81
- const nwaku1Ma = await nwaku1 . getMultiaddrWithId ( ) ;
82
- const nwaku1PeerId = await nwaku1 . getPeerId ( ) ;
134
+ it ( "different cluster, different shard: nodes don't connect" , async function ( ) {
135
+ this . timeout ( 55_000 ) ;
83
136
84
- waku = await createLightNode ( { shardInfo : shardInfo2 } ) ;
85
- await waku . start ( ) ;
86
- await waku . libp2p . dialProtocol ( nwaku1Ma , MetadataCodec ) ;
137
+ const shardInfo1 : ShardInfo = {
138
+ clusterId : 1 ,
139
+ shards : [ 1 ]
140
+ } ;
87
141
88
- const shardInfoRes =
89
- await waku . libp2p . services . metadata ?. query ( nwaku1PeerId ) ;
90
- expect ( shardInfoRes ) . to . not . be . undefined ;
91
- expect ( shardInfoRes ?. clusterId ) . to . equal ( shardInfo1 . clusterId ) ;
92
- expect ( shardInfoRes ?. shards ) . to . deep . equal ( shardInfo1 . shards ) ;
142
+ const shardInfo2 : ShardInfo = {
143
+ clusterId : 2 ,
144
+ shards : [ 2 ]
145
+ } ;
93
146
94
- const activeConnections = waku . libp2p . getConnections ( ) ;
95
- expect ( activeConnections . length ) . to . equal ( 1 ) ;
96
- } ) ;
147
+ await nwaku1 . start ( {
148
+ relay : true ,
149
+ discv5Discovery : true ,
150
+ peerExchange : true ,
151
+ clusterId : shardInfo1 . clusterId ,
152
+ pubsubTopic : shardInfoToPubsubTopics ( shardInfo1 )
153
+ } ) ;
97
154
98
- it ( "different cluster, same shard: nodes don't connect" , async function ( ) {
99
- this . timeout ( 55_000 ) ;
155
+ const nwaku1Ma = await nwaku1 . getMultiaddrWithId ( ) ;
100
156
101
- const shardInfo1 : ShardInfo = {
102
- clusterId : 1 ,
103
- shards : [ 1 ]
104
- } ;
157
+ waku = await createLightNode ( { shardInfo : shardInfo2 } ) ;
158
+ await waku . start ( ) ;
159
+ await waku . libp2p . dialProtocol ( nwaku1Ma , MetadataCodec ) ;
105
160
106
- const shardInfo2 : ShardInfo = {
107
- clusterId : 2 ,
108
- shards : [ 1 ]
109
- } ;
161
+ // add a delay to make sure the connection is closed from the other side
162
+ await delay ( 100 ) ;
110
163
111
- await nwaku1 . start ( {
112
- relay : true ,
113
- discv5Discovery : true ,
114
- peerExchange : true ,
115
- clusterId : shardInfo1 . clusterId ,
116
- pubsubTopic : shardInfoToPubsubTopics ( shardInfo1 )
164
+ const activeConnections = waku . libp2p . getConnections ( ) ;
165
+ expect ( activeConnections . length ) . to . equal ( 0 ) ;
117
166
} ) ;
118
-
119
- const nwaku1Ma = await nwaku1 . getMultiaddrWithId ( ) ;
120
-
121
- waku = await createLightNode ( { shardInfo : shardInfo2 } ) ;
122
- await waku . start ( ) ;
123
- await waku . libp2p . dialProtocol ( nwaku1Ma , MetadataCodec ) ;
124
-
125
- // add a delay to make sure the connection is closed from the other side
126
- await delay ( 100 ) ;
127
-
128
- const activeConnections = waku . libp2p . getConnections ( ) ;
129
- expect ( activeConnections . length ) . to . equal ( 0 ) ;
130
167
} ) ;
131
168
132
- it ( "different cluster, different shard: nodes don't connect" , async function ( ) {
133
- this . timeout ( 55_000 ) ;
134
-
135
- const shardInfo1 : ShardInfo = {
169
+ it ( "PeerStore has remote peer's shard info after successful connection" , async function ( ) {
170
+ const shardInfo : ShardInfo = {
136
171
clusterId : 1 ,
137
172
shards : [ 1 ]
138
173
} ;
139
174
140
- const shardInfo2 : ShardInfo = {
141
- clusterId : 2 ,
142
- shards : [ 2 ]
143
- } ;
144
-
145
175
await nwaku1 . start ( {
146
176
relay : true ,
147
177
discv5Discovery : true ,
148
178
peerExchange : true ,
149
- clusterId : shardInfo1 . clusterId ,
150
- pubsubTopic : shardInfoToPubsubTopics ( shardInfo1 )
179
+ clusterId : shardInfo . clusterId ,
180
+ pubsubTopic : shardInfoToPubsubTopics ( shardInfo )
151
181
} ) ;
152
182
153
183
const nwaku1Ma = await nwaku1 . getMultiaddrWithId ( ) ;
184
+ const nwaku1PeerId = await nwaku1 . getPeerId ( ) ;
154
185
155
- waku = await createLightNode ( { shardInfo : shardInfo2 } ) ;
186
+ waku = await createLightNode ( { shardInfo } ) ;
156
187
await waku . start ( ) ;
157
188
await waku . libp2p . dialProtocol ( nwaku1Ma , MetadataCodec ) ;
158
189
159
- // add a delay to make sure the connection is closed from the other side
160
- await delay ( 100 ) ;
190
+ // delay to ensure the connection is estabilished and shardInfo is updated
191
+ await delay ( 500 ) ;
192
+
193
+ const encodedShardInfo = (
194
+ await waku . libp2p . peerStore . get ( nwaku1PeerId )
195
+ ) . metadata . get ( "shardInfo" ) ;
196
+ expect ( encodedShardInfo ) . to . not . be . undefined ;
197
+
198
+ const metadataShardInfo = decodeRelayShard ( encodedShardInfo ! ) ;
199
+ expect ( metadataShardInfo ) . not . be . undefined ;
161
200
162
- const activeConnections = waku . libp2p . getConnections ( ) ;
163
- expect ( activeConnections . length ) . to . equal ( 0 ) ;
201
+ expect ( metadataShardInfo ! . clusterId ) . to . eq ( shardInfo . clusterId ) ;
202
+ expect ( metadataShardInfo . shards ) . to . deep . eq ( shardInfo . shards ) ;
164
203
} ) ;
165
204
} ) ;
0 commit comments