File tree Expand file tree Collapse file tree 4 files changed +21
-14
lines changed Expand file tree Collapse file tree 4 files changed +21
-14
lines changed Original file line number Diff line number Diff line change @@ -77,7 +77,7 @@ export class DPT extends EventEmitter {
77
77
} )
78
78
this . _server . once ( 'listening' , ( ) => this . emit ( 'listening' ) )
79
79
this . _server . once ( 'close' , ( ) => this . emit ( 'close' ) )
80
- this . _server . on ( 'peers' , ( peers , remote ) => this . _onServerPeers ( peers ) )
80
+ this . _server . on ( 'peers' , ( peers ) => this . _onServerPeers ( peers ) )
81
81
this . _server . on ( 'error' , ( err ) => this . emit ( 'error' , err ) )
82
82
83
83
const refreshIntervalSubdivided = Math . floor ( ( options . refreshInterval ?? ms ( '60s' ) ) / 10 )
@@ -115,12 +115,16 @@ export class DPT extends EventEmitter {
115
115
}
116
116
117
117
_onServerPeers ( peers : PeerInfo [ ] ) : void {
118
- const ms = 0
118
+ const DIFF_TIME_MS = 200
119
+ let ms = 0
119
120
for ( const peer of peers ) {
120
- this . addPeer ( peer ) . catch ( ( error ) => {
121
- this . emit ( 'error' , error )
122
- } )
123
- }
121
+ setTimeout ( ( ) => {
122
+ this . addPeer ( peer ) . catch ( ( error ) => {
123
+ this . emit ( 'error' , error )
124
+ } )
125
+ } , ms )
126
+ ms += DIFF_TIME_MS
127
+ }
124
128
}
125
129
126
130
async bootstrap ( peer : PeerInfo ) : Promise < void > {
Original file line number Diff line number Diff line change @@ -32,7 +32,7 @@ export class KBucket extends EventEmitter {
32
32
this . emit ( 'removed' , peer )
33
33
} )
34
34
35
- this . _kbucket . on ( 'ping' , ( oldPeers : PeerInfo [ ] , newPeer : PeerInfo | undefined ) => {
35
+ this . _kbucket . on ( 'ping' , ( oldPeers : PeerInfo [ ] , newPeer : PeerInfo | undefined ) => {
36
36
this . emit ( 'ping' , oldPeers , newPeer )
37
37
} )
38
38
}
Original file line number Diff line number Diff line change @@ -32,7 +32,7 @@ test('DPT: remove node', async (t) => {
32
32
async . series (
33
33
[
34
34
function ( cb ) {
35
- dpts [ 0 ] . on ( 'peer:added' , function ( peer : any ) {
35
+ dpts [ 0 ] . on ( 'peer:added' , function ( peer ) {
36
36
dpts [ 0 ] . removePeer ( peer )
37
37
cb ( null )
38
38
} )
@@ -64,13 +64,13 @@ test('DPT: ban node', async (t) => {
64
64
async . series (
65
65
[
66
66
function ( cb ) {
67
- dpts [ 0 ] . on ( 'peer:added' , function ( peer : any ) {
67
+ dpts [ 0 ] . on ( 'peer:added' , function ( peer ) {
68
68
dpts [ 0 ] . banPeer ( peer )
69
69
cb ( null )
70
70
} )
71
71
} ,
72
72
function ( cb ) {
73
- dpts [ 0 ] . on ( 'peer:removed' , function ( peer : any ) {
73
+ dpts [ 0 ] . on ( 'peer:removed' , function ( peer ) {
74
74
t . equal ( dpts [ 0 ] . banlist . has ( peer ) , true , 'ban-list should contain peer' )
75
75
t . equal (
76
76
dpts [ 0 ] . getPeers ( ) . length ,
@@ -150,11 +150,14 @@ test('DPT: simulate bootstrap', async (t) => {
150
150
}
151
151
152
152
await delay ( 250 )
153
- util . destroyDPTs ( dpts )
154
153
155
154
// dpts.forEach((dpt, i) => console.log(`${i}:${dpt.getPeers().length}`))
156
- for ( const dpt of dpts )
155
+ for ( const dpt of dpts ) {
157
156
t . equal ( dpt . getPeers ( ) . length , numDPTs , 'Peers should be distributed to all DPTs' )
157
+ }
158
+ await delay ( 1000 )
159
+
160
+ util . destroyDPTs ( dpts )
158
161
159
162
t . end ( )
160
163
} )
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ import Common from '@ethereumjs/common'
5
5
export const localhost = '127.0.0.1'
6
6
export const basePort = 30306
7
7
8
- export function getTestDPTs ( numDPTs : any ) {
8
+ export function getTestDPTs ( numDPTs : number ) {
9
9
const dpts = [ ]
10
10
11
11
for ( let i = 0 ; i < numDPTs ; ++ i ) {
@@ -30,7 +30,7 @@ export function initTwoPeerDPTSetup() {
30
30
return dpts
31
31
}
32
32
33
- export function destroyDPTs ( dpts : any ) {
33
+ export function destroyDPTs ( dpts : DPT [ ] ) {
34
34
for ( const dpt of dpts ) dpt . destroy ( )
35
35
}
36
36
You can’t perform that action at this time.
0 commit comments