@@ -8,6 +8,7 @@ import { CollectiveReqRespTimeoutError, IndiviualReqRespTimeoutError } from '../
88import {
99 MOCK_SUB_PROTOCOL_HANDLERS ,
1010 MOCK_SUB_PROTOCOL_VALIDATORS ,
11+ type ReqRespNode ,
1112 connectToPeers ,
1213 createNodes ,
1314 startNodes ,
@@ -23,15 +24,22 @@ const PING_REQUEST = RequestableBuffer.fromBuffer(Buffer.from('ping'));
2324// and ask for specific data that they missed via the traditional gossip protocol.
2425describe ( 'ReqResp' , ( ) => {
2526 let peerManager : MockProxy < PeerManager > ;
27+ let nodes : ReqRespNode [ ] ;
2628
2729 beforeEach ( ( ) => {
2830 peerManager = mock < PeerManager > ( ) ;
2931 } ) ;
3032
33+ afterEach ( async ( ) => {
34+ if ( nodes ) {
35+ await stopNodes ( nodes as ReqRespNode [ ] ) ;
36+ }
37+ } ) ;
38+
3139 it ( 'Should perform a ping request' , async ( ) => {
3240 // Create two nodes
3341 // They need to discover each other
34- const nodes = await createNodes ( peerManager , 2 ) ;
42+ nodes = await createNodes ( peerManager , 2 ) ;
3543 const { req : pinger } = nodes [ 0 ] ;
3644
3745 await startNodes ( nodes ) ;
@@ -45,12 +53,10 @@ describe('ReqResp', () => {
4553
4654 await sleep ( 500 ) ;
4755 expect ( res ?. toBuffer ( ) . toString ( 'utf-8' ) ) . toEqual ( 'pong' ) ;
48-
49- await stopNodes ( nodes ) ;
5056 } ) ;
5157
5258 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 ) ;
5460
5561 const { req : pinger } = nodes [ 0 ] ;
5662 const { req : ponger } = nodes [ 1 ] ;
@@ -66,12 +72,10 @@ describe('ReqResp', () => {
6672 const res = await pinger . sendRequest ( PING_PROTOCOL , PING_REQUEST ) ;
6773
6874 expect ( res ) . toBeUndefined ( ) ;
69-
70- await stopNodes ( nodes ) ;
7175 } ) ;
7276
7377 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 ) ;
7579
7680 await startNodes ( nodes ) ;
7781 await sleep ( 500 ) ;
@@ -86,12 +90,10 @@ describe('ReqResp', () => {
8690 const res = await nodes [ 0 ] . req . sendRequest ( PING_PROTOCOL , PING_REQUEST ) ;
8791
8892 expect ( res ?. toBuffer ( ) . toString ( 'utf-8' ) ) . toEqual ( 'pong' ) ;
89-
90- await stopNodes ( nodes ) ;
9193 } ) ;
9294
9395 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 ) ;
9597
9698 await startNodes ( nodes ) ;
9799
@@ -110,8 +112,6 @@ describe('ReqResp', () => {
110112 // Make sure the error message is logged
111113 const errorMessage = `Rate limit exceeded for ${ PING_PROTOCOL } from ${ nodes [ 0 ] . p2p . peerId . toString ( ) } ` ;
112114 expect ( loggerSpy ) . toHaveBeenCalledWith ( errorMessage ) ;
113-
114- await stopNodes ( nodes ) ;
115115 } ) ;
116116
117117 describe ( 'TX REQ PROTOCOL' , ( ) => {
@@ -120,15 +120,15 @@ describe('ReqResp', () => {
120120 const txHash = tx . getTxHash ( ) ;
121121
122122 const protocolHandlers = MOCK_SUB_PROTOCOL_HANDLERS ;
123- protocolHandlers [ TX_REQ_PROTOCOL ] = ( message : Buffer ) : Promise < Uint8Array > => {
123+ protocolHandlers [ TX_REQ_PROTOCOL ] = ( message : Buffer ) : Promise < Buffer > => {
124124 const receivedHash = TxHash . fromBuffer ( message ) ;
125125 if ( txHash . equals ( receivedHash ) ) {
126- return Promise . resolve ( Uint8Array . from ( tx . toBuffer ( ) ) ) ;
126+ return Promise . resolve ( tx . toBuffer ( ) ) ;
127127 }
128- return Promise . resolve ( Uint8Array . from ( Buffer . from ( '' ) ) ) ;
128+ return Promise . resolve ( Buffer . from ( '' ) ) ;
129129 } ;
130130
131- const nodes = await createNodes ( peerManager , 2 ) ;
131+ nodes = await createNodes ( peerManager , 2 ) ;
132132
133133 await startNodes ( nodes , protocolHandlers ) ;
134134 await sleep ( 500 ) ;
@@ -137,8 +137,6 @@ describe('ReqResp', () => {
137137
138138 const res = await nodes [ 0 ] . req . sendRequest ( TX_REQ_PROTOCOL , txHash ) ;
139139 expect ( res ) . toEqual ( tx ) ;
140-
141- await stopNodes ( nodes ) ;
142140 } ) ;
143141
144142 it ( 'Does not crash if tx hash returns undefined' , async ( ) => {
@@ -147,11 +145,11 @@ describe('ReqResp', () => {
147145
148146 const protocolHandlers = MOCK_SUB_PROTOCOL_HANDLERS ;
149147 // 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 ( '' ) ) ;
152150 } ;
153151
154- const nodes = await createNodes ( peerManager , 2 ) ;
152+ nodes = await createNodes ( peerManager , 2 ) ;
155153
156154 await startNodes ( nodes , protocolHandlers ) ;
157155 await sleep ( 500 ) ;
@@ -160,12 +158,10 @@ describe('ReqResp', () => {
160158
161159 const res = await nodes [ 0 ] . req . sendRequest ( TX_REQ_PROTOCOL , txHash ) ;
162160 expect ( res ) . toBeUndefined ( ) ;
163-
164- await stopNodes ( nodes ) ;
165161 } ) ;
166162
167163 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 ) ;
169165
170166 await startNodes ( nodes ) ;
171167
@@ -197,12 +193,10 @@ describe('ReqResp', () => {
197193 } ) ,
198194 PeerErrorSeverity . HighToleranceError ,
199195 ) ;
200-
201- await stopNodes ( nodes ) ;
202196 } ) ;
203197
204198 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 ) ;
206200
207201 await startNodes ( nodes ) ;
208202
@@ -226,8 +220,6 @@ describe('ReqResp', () => {
226220 // Make sure the error message is logged
227221 const errorMessage = `${ new CollectiveReqRespTimeoutError ( ) . message } | subProtocol: ${ TX_REQ_PROTOCOL } ` ;
228222 expect ( loggerSpy ) . toHaveBeenCalledWith ( errorMessage ) ;
229-
230- await stopNodes ( nodes ) ;
231223 } ) ;
232224
233225 it ( 'Should penalize peer if transaction validation fails' , async ( ) => {
@@ -236,12 +228,12 @@ describe('ReqResp', () => {
236228
237229 // Mock that the node will respond with the tx
238230 const protocolHandlers = MOCK_SUB_PROTOCOL_HANDLERS ;
239- protocolHandlers [ TX_REQ_PROTOCOL ] = ( message : Buffer ) : Promise < Uint8Array > => {
231+ protocolHandlers [ TX_REQ_PROTOCOL ] = ( message : Buffer ) : Promise < Buffer > => {
240232 const receivedHash = TxHash . fromBuffer ( message ) ;
241233 if ( txHash . equals ( receivedHash ) ) {
242- return Promise . resolve ( Uint8Array . from ( tx . toBuffer ( ) ) ) ;
234+ return Promise . resolve ( tx . toBuffer ( ) ) ;
243235 }
244- return Promise . resolve ( Uint8Array . from ( Buffer . from ( '' ) ) ) ;
236+ return Promise . resolve ( Buffer . from ( '' ) ) ;
245237 } ;
246238
247239 // Mock that the receiving node will find that the transaction is invalid
@@ -251,7 +243,7 @@ describe('ReqResp', () => {
251243 return Promise . resolve ( false ) ;
252244 } ;
253245
254- const nodes = await createNodes ( peerManager , 2 ) ;
246+ nodes = await createNodes ( peerManager , 2 ) ;
255247
256248 await startNodes ( nodes , protocolHandlers , protocolValidators ) ;
257249 await sleep ( 500 ) ;
@@ -268,8 +260,6 @@ describe('ReqResp', () => {
268260 } ) ,
269261 PeerErrorSeverity . LowToleranceError ,
270262 ) ;
271-
272- await stopNodes ( nodes ) ;
273263 } ) ;
274264 } ) ;
275265} ) ;
0 commit comments