@@ -8,6 +8,7 @@ import { CollectiveReqRespTimeoutError, IndiviualReqRespTimeoutError } from '../
8
8
import {
9
9
MOCK_SUB_PROTOCOL_HANDLERS ,
10
10
MOCK_SUB_PROTOCOL_VALIDATORS ,
11
+ type ReqRespNode ,
11
12
connectToPeers ,
12
13
createNodes ,
13
14
startNodes ,
@@ -23,15 +24,22 @@ const PING_REQUEST = RequestableBuffer.fromBuffer(Buffer.from('ping'));
23
24
// and ask for specific data that they missed via the traditional gossip protocol.
24
25
describe ( 'ReqResp' , ( ) => {
25
26
let peerManager : MockProxy < PeerManager > ;
27
+ let nodes : ReqRespNode [ ] ;
26
28
27
29
beforeEach ( ( ) => {
28
30
peerManager = mock < PeerManager > ( ) ;
29
31
} ) ;
30
32
33
+ afterEach ( async ( ) => {
34
+ if ( nodes ) {
35
+ await stopNodes ( nodes as ReqRespNode [ ] ) ;
36
+ }
37
+ } ) ;
38
+
31
39
it ( 'Should perform a ping request' , async ( ) => {
32
40
// Create two nodes
33
41
// They need to discover each other
34
- const nodes = await createNodes ( peerManager , 2 ) ;
42
+ nodes = await createNodes ( peerManager , 2 ) ;
35
43
const { req : pinger } = nodes [ 0 ] ;
36
44
37
45
await startNodes ( nodes ) ;
@@ -45,12 +53,10 @@ describe('ReqResp', () => {
45
53
46
54
await sleep ( 500 ) ;
47
55
expect ( res ?. toBuffer ( ) . toString ( 'utf-8' ) ) . toEqual ( 'pong' ) ;
48
-
49
- await stopNodes ( nodes ) ;
50
56
} ) ;
51
57
52
58
it ( 'Should handle gracefully if a peer connected peer is offline' , async ( ) => {
53
- const nodes = await createNodes ( peerManager , 2 ) ;
59
+ nodes = await createNodes ( peerManager , 2 ) ;
54
60
55
61
const { req : pinger } = nodes [ 0 ] ;
56
62
const { req : ponger } = nodes [ 1 ] ;
@@ -66,12 +72,10 @@ describe('ReqResp', () => {
66
72
const res = await pinger . sendRequest ( PING_PROTOCOL , PING_REQUEST ) ;
67
73
68
74
expect ( res ) . toBeUndefined ( ) ;
69
-
70
- await stopNodes ( nodes ) ;
71
75
} ) ;
72
76
73
77
it ( 'Should request from a later peer if other peers are offline' , async ( ) => {
74
- const nodes = await createNodes ( peerManager , 4 ) ;
78
+ nodes = await createNodes ( peerManager , 4 ) ;
75
79
76
80
await startNodes ( nodes ) ;
77
81
await sleep ( 500 ) ;
@@ -86,12 +90,10 @@ describe('ReqResp', () => {
86
90
const res = await nodes [ 0 ] . req . sendRequest ( PING_PROTOCOL , PING_REQUEST ) ;
87
91
88
92
expect ( res ?. toBuffer ( ) . toString ( 'utf-8' ) ) . toEqual ( 'pong' ) ;
89
-
90
- await stopNodes ( nodes ) ;
91
93
} ) ;
92
94
93
95
it ( 'Should hit a rate limit if too many requests are made in quick succession' , async ( ) => {
94
- const nodes = await createNodes ( peerManager , 2 ) ;
96
+ nodes = await createNodes ( peerManager , 2 ) ;
95
97
96
98
await startNodes ( nodes ) ;
97
99
@@ -110,8 +112,6 @@ describe('ReqResp', () => {
110
112
// Make sure the error message is logged
111
113
const errorMessage = `Rate limit exceeded for ${ PING_PROTOCOL } from ${ nodes [ 0 ] . p2p . peerId . toString ( ) } ` ;
112
114
expect ( loggerSpy ) . toHaveBeenCalledWith ( errorMessage ) ;
113
-
114
- await stopNodes ( nodes ) ;
115
115
} ) ;
116
116
117
117
describe ( 'TX REQ PROTOCOL' , ( ) => {
@@ -120,15 +120,15 @@ describe('ReqResp', () => {
120
120
const txHash = tx . getTxHash ( ) ;
121
121
122
122
const protocolHandlers = MOCK_SUB_PROTOCOL_HANDLERS ;
123
- protocolHandlers [ TX_REQ_PROTOCOL ] = ( message : Buffer ) : Promise < Uint8Array > => {
123
+ protocolHandlers [ TX_REQ_PROTOCOL ] = ( message : Buffer ) : Promise < Buffer > => {
124
124
const receivedHash = TxHash . fromBuffer ( message ) ;
125
125
if ( txHash . equals ( receivedHash ) ) {
126
- return Promise . resolve ( Uint8Array . from ( tx . toBuffer ( ) ) ) ;
126
+ return Promise . resolve ( tx . toBuffer ( ) ) ;
127
127
}
128
- return Promise . resolve ( Uint8Array . from ( Buffer . from ( '' ) ) ) ;
128
+ return Promise . resolve ( Buffer . from ( '' ) ) ;
129
129
} ;
130
130
131
- const nodes = await createNodes ( peerManager , 2 ) ;
131
+ nodes = await createNodes ( peerManager , 2 ) ;
132
132
133
133
await startNodes ( nodes , protocolHandlers ) ;
134
134
await sleep ( 500 ) ;
@@ -137,8 +137,6 @@ describe('ReqResp', () => {
137
137
138
138
const res = await nodes [ 0 ] . req . sendRequest ( TX_REQ_PROTOCOL , txHash ) ;
139
139
expect ( res ) . toEqual ( tx ) ;
140
-
141
- await stopNodes ( nodes ) ;
142
140
} ) ;
143
141
144
142
it ( 'Does not crash if tx hash returns undefined' , async ( ) => {
@@ -147,11 +145,11 @@ describe('ReqResp', () => {
147
145
148
146
const protocolHandlers = MOCK_SUB_PROTOCOL_HANDLERS ;
149
147
// Return nothing
150
- protocolHandlers [ TX_REQ_PROTOCOL ] = ( _message : Buffer ) : Promise < Uint8Array > => {
151
- return Promise . resolve ( Uint8Array . from ( Buffer . from ( '' ) ) ) ;
148
+ protocolHandlers [ TX_REQ_PROTOCOL ] = ( _message : Buffer ) : Promise < Buffer > => {
149
+ return Promise . resolve ( Buffer . from ( '' ) ) ;
152
150
} ;
153
151
154
- const nodes = await createNodes ( peerManager , 2 ) ;
152
+ nodes = await createNodes ( peerManager , 2 ) ;
155
153
156
154
await startNodes ( nodes , protocolHandlers ) ;
157
155
await sleep ( 500 ) ;
@@ -160,12 +158,10 @@ describe('ReqResp', () => {
160
158
161
159
const res = await nodes [ 0 ] . req . sendRequest ( TX_REQ_PROTOCOL , txHash ) ;
162
160
expect ( res ) . toBeUndefined ( ) ;
163
-
164
- await stopNodes ( nodes ) ;
165
161
} ) ;
166
162
167
163
it ( 'Should hit individual timeout if nothing is returned over the stream' , async ( ) => {
168
- const nodes = await createNodes ( peerManager , 2 ) ;
164
+ nodes = await createNodes ( peerManager , 2 ) ;
169
165
170
166
await startNodes ( nodes ) ;
171
167
@@ -197,12 +193,10 @@ describe('ReqResp', () => {
197
193
} ) ,
198
194
PeerErrorSeverity . HighToleranceError ,
199
195
) ;
200
-
201
- await stopNodes ( nodes ) ;
202
196
} ) ;
203
197
204
198
it ( 'Should hit collective timeout if nothing is returned over the stream from multiple peers' , async ( ) => {
205
- const nodes = await createNodes ( peerManager , 4 ) ;
199
+ nodes = await createNodes ( peerManager , 4 ) ;
206
200
207
201
await startNodes ( nodes ) ;
208
202
@@ -226,8 +220,6 @@ describe('ReqResp', () => {
226
220
// Make sure the error message is logged
227
221
const errorMessage = `${ new CollectiveReqRespTimeoutError ( ) . message } | subProtocol: ${ TX_REQ_PROTOCOL } ` ;
228
222
expect ( loggerSpy ) . toHaveBeenCalledWith ( errorMessage ) ;
229
-
230
- await stopNodes ( nodes ) ;
231
223
} ) ;
232
224
233
225
it ( 'Should penalize peer if transaction validation fails' , async ( ) => {
@@ -236,12 +228,12 @@ describe('ReqResp', () => {
236
228
237
229
// Mock that the node will respond with the tx
238
230
const protocolHandlers = MOCK_SUB_PROTOCOL_HANDLERS ;
239
- protocolHandlers [ TX_REQ_PROTOCOL ] = ( message : Buffer ) : Promise < Uint8Array > => {
231
+ protocolHandlers [ TX_REQ_PROTOCOL ] = ( message : Buffer ) : Promise < Buffer > => {
240
232
const receivedHash = TxHash . fromBuffer ( message ) ;
241
233
if ( txHash . equals ( receivedHash ) ) {
242
- return Promise . resolve ( Uint8Array . from ( tx . toBuffer ( ) ) ) ;
234
+ return Promise . resolve ( tx . toBuffer ( ) ) ;
243
235
}
244
- return Promise . resolve ( Uint8Array . from ( Buffer . from ( '' ) ) ) ;
236
+ return Promise . resolve ( Buffer . from ( '' ) ) ;
245
237
} ;
246
238
247
239
// Mock that the receiving node will find that the transaction is invalid
@@ -251,7 +243,7 @@ describe('ReqResp', () => {
251
243
return Promise . resolve ( false ) ;
252
244
} ;
253
245
254
- const nodes = await createNodes ( peerManager , 2 ) ;
246
+ nodes = await createNodes ( peerManager , 2 ) ;
255
247
256
248
await startNodes ( nodes , protocolHandlers , protocolValidators ) ;
257
249
await sleep ( 500 ) ;
@@ -268,8 +260,6 @@ describe('ReqResp', () => {
268
260
} ) ,
269
261
PeerErrorSeverity . LowToleranceError ,
270
262
) ;
271
-
272
- await stopNodes ( nodes ) ;
273
263
} ) ;
274
264
} ) ;
275
265
} ) ;
0 commit comments