-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: auto-confirm relay addresses (#2886)
After we have created a reservation on a relay, automatically confirm that it is publicly dialable. Fixes #2883
- Loading branch information
1 parent
58542c6
commit 5c4a79e
Showing
6 changed files
with
186 additions
and
33 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
101 changes: 101 additions & 0 deletions
101
packages/transport-circuit-relay-v2/test/listener.spec.ts
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,101 @@ | ||
import { generateKeyPair } from '@libp2p/crypto/keys' | ||
import { defaultLogger } from '@libp2p/logger' | ||
import { peerIdFromPrivateKey } from '@libp2p/peer-id' | ||
import { multiaddr } from '@multiformats/multiaddr' | ||
import { expect } from 'aegir/chai' | ||
import { stubInterface } from 'sinon-ts' | ||
import { createListener } from '../src/transport/listener.js' | ||
import { type ReservationStore } from '../src/transport/reservation-store.js' | ||
import type { ComponentLogger, Connection, Listener, PeerId } from '@libp2p/interface' | ||
import type { AddressManager, ConnectionManager } from '@libp2p/interface-internal' | ||
import type { StubbedInstance } from 'sinon-ts' | ||
|
||
export interface CircuitRelayTransportListenerComponents { | ||
peerId: PeerId | ||
connectionManager: StubbedInstance<ConnectionManager> | ||
addressManager: StubbedInstance<AddressManager> | ||
reservationStore: StubbedInstance<ReservationStore> | ||
logger: ComponentLogger | ||
} | ||
|
||
describe('listener', () => { | ||
let listener: Listener | ||
let components: CircuitRelayTransportListenerComponents | ||
|
||
beforeEach(async () => { | ||
components = { | ||
peerId: peerIdFromPrivateKey(await generateKeyPair('Ed25519')), | ||
connectionManager: stubInterface(), | ||
addressManager: stubInterface(), | ||
reservationStore: stubInterface(), | ||
logger: defaultLogger() | ||
} | ||
|
||
listener = createListener(components) | ||
}) | ||
|
||
it('should auto-confirm discovered relay addresses', async () => { | ||
await listener.listen(multiaddr('/p2p-circuit')) | ||
|
||
expect(components.reservationStore.reserveRelay).to.have.property('called', true, 'did not begin relay search') | ||
|
||
const relayPeer = peerIdFromPrivateKey(await generateKeyPair('Ed25519')) | ||
const relayAddr = multiaddr(`/ip4/123.123.123.123/tcp/1234/p2p/${relayPeer}`) | ||
|
||
const createdReservationListener = components.reservationStore.addEventListener.getCall(1).args[1] | ||
|
||
if (typeof createdReservationListener === 'function') { | ||
createdReservationListener( | ||
new CustomEvent('relay:created-reservation', { | ||
detail: { | ||
relay: relayPeer, | ||
details: { | ||
type: 'discovered', | ||
reservation: { | ||
addrs: [ | ||
relayAddr | ||
] | ||
} | ||
} | ||
} | ||
}) | ||
) | ||
} | ||
|
||
expect(components.addressManager.confirmObservedAddr.calledWith( | ||
relayAddr.encapsulate('/p2p-circuit') | ||
)).to.be.true() | ||
}) | ||
|
||
it('should auto-confirm configured relay addresses', async () => { | ||
const relayPeer = peerIdFromPrivateKey(await generateKeyPair('Ed25519')) | ||
const relayAddr = multiaddr(`/ip4/123.123.123.123/tcp/1234/p2p/${relayPeer}/p2p-circuit`) | ||
const conn = stubInterface<Connection>({ | ||
id: 'connection-id-1234', | ||
remotePeer: relayPeer | ||
}) | ||
|
||
components.connectionManager.openConnection.withArgs(relayAddr.decapsulate('/p2p-circuit')).resolves(conn) | ||
|
||
components.reservationStore.addRelay.withArgs(relayPeer).resolves({ | ||
relay: relayPeer, | ||
details: { | ||
type: 'configured', | ||
reservation: { | ||
addrs: [ | ||
relayAddr.bytes | ||
], | ||
expire: 100n | ||
}, | ||
timeout: 0 as any, | ||
connection: conn.id | ||
} | ||
}) | ||
|
||
await listener.listen(relayAddr) | ||
|
||
expect(components.addressManager.confirmObservedAddr.calledWith( | ||
relayAddr.encapsulate('/p2p-circuit') | ||
)).to.be.true() | ||
}) | ||
}) |