-
Notifications
You must be signed in to change notification settings - Fork 759
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Client Discovery Improvements (#3120)
* Client: add explicit discovery startup logging * Client: add bootnodes format hint on CLI option help * Client: add option to pass in bootnode.txt file to --bootnodes CLI param, add CLI test * Client: replace goerli -> holesky in list with networks with activated DNS discovery * Devp2p: add new confirmed-peer mechanism for a more fine grained peer discovery, reactivated discV4 for client * Devp2p: add test setup for DPT, initialization, bootstrap(), addPeer() and confirmed/unconfirmed refresh() tests, fix bug in getClosestPeers() * Client: make onlyConfirmed exception for mainnet (since most peers are mainnet peers and peering then goes quicker) * Devp2p: increase network resilience for the case that no initial confirmation is possible * Devp2p: remove peer from confirmed peers list when being removed from DPT, fix tests * Fix tests * Client: add missing bootnode.txt test file
- Loading branch information
Showing
11 changed files
with
225 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
enode://abc@127.0.0.1:30303 | ||
enode://abc@127.0.0.1:30304 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { hexToBytes } from '@ethereumjs/util' | ||
import { afterEach, assert, describe, expect, it, vi } from 'vitest' | ||
|
||
import { DPT } from '../src/dpt/index.js' | ||
|
||
import type { PeerInfo } from '../src/types.js' | ||
|
||
describe('DPT', () => { | ||
afterEach(() => { | ||
vi.restoreAllMocks() | ||
}) | ||
|
||
const privateKey1 = hexToBytes( | ||
'0x012e930448c53e0b73edbbbc433e8a741e978cda79be2be039905f538d6247c2' | ||
) | ||
|
||
const peers: PeerInfo[] = [] | ||
|
||
for (let i = 1; i <= 5; i++) { | ||
const id = new Uint8Array([i]) | ||
const address = '127.0.0.1' | ||
const udpPort = 5000 + i | ||
const peer: PeerInfo = { | ||
id, | ||
address, | ||
udpPort, | ||
} | ||
peers.push(peer) | ||
} | ||
|
||
class Server { | ||
ping() {} | ||
findneighbours() {} | ||
destroy() {} | ||
} | ||
|
||
it('should initialize and add peers', async () => { | ||
const dpt = new DPT(privateKey1, {}) | ||
dpt['_server'] = new Server() as any | ||
assert.equal(dpt['_dnsAddr'], '8.8.8.8', 'should initialize with default values') | ||
|
||
dpt['_server'].ping = vi.fn().mockResolvedValue(peers[0]) | ||
await dpt.bootstrap(peers[0]) | ||
assert.equal(dpt.numPeers(), 1, 'should add peer on bootstrap()') | ||
|
||
// Attention! Not all peers are called by default in refresh() | ||
// (take into account on test design) | ||
const spy = vi.spyOn(dpt['_server'], 'findneighbours') | ||
await dpt.refresh() | ||
expect(spy).toHaveBeenCalledTimes(1) | ||
|
||
dpt['_server'].ping = vi.fn().mockResolvedValue(peers[1]) | ||
await dpt.addPeer(peers[1]) | ||
assert.equal(dpt.numPeers(), 2, 'should add another peer on addPeer()') | ||
|
||
assert.equal( | ||
dpt.getClosestPeers(peers[0].id!).length, | ||
2, | ||
'should return all peers on getClosestPeers()' | ||
) | ||
|
||
dpt.destroy() | ||
}) | ||
|
||
it('should only call to confirmed peers on activated flag', async () => { | ||
const dpt = new DPT(privateKey1, { onlyConfirmed: true }) | ||
dpt['_server'] = new Server() as any | ||
|
||
dpt['_server'].ping = vi.fn().mockResolvedValue(peers[0]) | ||
await dpt.addPeer(peers[0]) | ||
|
||
const spy = vi.spyOn(dpt['_server'], 'findneighbours') | ||
await dpt.refresh() | ||
expect( | ||
spy, | ||
'call findneighbours on unconfirmed if no confirmed peers yet' | ||
).toHaveBeenCalledTimes(1) | ||
|
||
dpt['_refreshIntervalSelectionCounter'] = 0 | ||
dpt.confirmPeer('01') | ||
await dpt.refresh() | ||
expect(spy, 'call findneighbours on confirmed').toHaveBeenCalledTimes(2) | ||
|
||
dpt['_server'].ping = vi.fn().mockResolvedValue(peers[1]) | ||
await dpt.addPeer(peers[1]) | ||
assert.equal( | ||
dpt.getClosestPeers(peers[0].id!).length, | ||
1, | ||
'should not return unconfirmed on getClosestPeers()' | ||
) | ||
|
||
dpt.confirmPeer('02') | ||
assert.equal( | ||
dpt.getClosestPeers(peers[0].id!).length, | ||
2, | ||
'should return confirmed on getClosestPeers()' | ||
) | ||
|
||
dpt.removePeer(peers[1]) | ||
assert.equal( | ||
dpt.getClosestPeers(peers[0].id!).length, | ||
1, | ||
'should work after peers being removed' | ||
) | ||
|
||
dpt.destroy() | ||
}) | ||
}) |