This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: switch to server mode automatically when addresses change (#473)
If the node is in client mode, and the `self:peer:update` event is emitted, and the event detail contains publicly routable addresses, switch to server mode. If the event is emitted and we are in server mode and the event detail contains only private addresses, switch back to client mode.
- Loading branch information
1 parent
508023d
commit de51cbe
Showing
4 changed files
with
143 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* eslint-env mocha */ | ||
/* eslint max-nested-callbacks: ["error", 8] */ | ||
|
||
import { multiaddr } from '@multiformats/multiaddr' | ||
import { expect } from 'aegir/chai' | ||
import delay from 'delay' | ||
import { TestDHT } from './utils/test-dht.js' | ||
|
||
const testCases: Array<[string, string, string]> = [ | ||
['should enable server mode when public IP4 addresses are found', '/ip4/139.178.91.71/udp/4001/quic', 'server'], | ||
['should enable server mode when public IP6 addresses are found', '/ip6/2604:1380:45e3:6e00::1/udp/4001/quic', 'server'], | ||
['should enable server mode when DNS4 addresses are found', '/dns4/sv15.bootstrap.libp2p.io/tcp/443/wss/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN', 'server'], | ||
['should enable server mode when DNS6 addresses are found', '/dns6/sv15.bootstrap.libp2p.io/tcp/443/wss/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN', 'server'], | ||
['should enable server mode when DNSADDR addresses are found', '/dnsaddr/sv15.bootstrap.libp2p.io/tcp/443/wss/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN', 'server'], | ||
['should not enable server mode when private IP4 addresses are found', '/ip4/127.0.0.1/udp/4001/quic', 'client'], | ||
['should not enable server mode when private IP6 addresses are found', '/ip6/::1/udp/4001/quic', 'client'], | ||
['should not enable server mode when otherwise public circuit relay addresses are found', '/dns4/sv15.bootstrap.libp2p.io/tcp/443/wss/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN/p2p-circuit', 'client'] | ||
] | ||
|
||
describe('enable server mode', () => { | ||
let tdht: TestDHT | ||
|
||
beforeEach(() => { | ||
tdht = new TestDHT() | ||
}) | ||
|
||
afterEach(async () => { | ||
await tdht.teardown() | ||
}) | ||
|
||
testCases.forEach(([name, addr, result]) => { | ||
it(name, async function () { | ||
const dht = await tdht.spawn() | ||
|
||
await expect(dht.getMode()).to.eventually.equal('client') | ||
|
||
dht.components.events.safeDispatchEvent('self:peer:update', { | ||
detail: { | ||
peer: { | ||
addresses: [{ | ||
multiaddr: multiaddr('/ip4/127.0.0.1/tcp/4001'), | ||
isCertified: true | ||
}, { | ||
multiaddr: multiaddr('/ip6/::1/tcp/4001'), | ||
isCertified: true | ||
}, { | ||
multiaddr: multiaddr(addr), | ||
isCertified: true | ||
}] | ||
} | ||
} | ||
}) | ||
|
||
await delay(100) | ||
|
||
await expect(dht.getMode()).to.eventually.equal(result, `did not change to "${result}" mode after updating with address ${addr}`) | ||
|
||
dht.components.events.safeDispatchEvent('self:peer:update', { | ||
detail: { | ||
peer: { | ||
addresses: [{ | ||
multiaddr: multiaddr('/ip4/127.0.0.1/tcp/4001'), | ||
isCertified: true | ||
}] | ||
} | ||
} | ||
}) | ||
|
||
await delay(100) | ||
|
||
await expect(dht.getMode()).to.eventually.equal('client', `did not reset to client mode after updating with address ${addr}`) | ||
}) | ||
}) | ||
}) |
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