Skip to content

Commit eea5989

Browse files
committed
fix: update append announce addresses
We allow transports to update announce addresses to add certhashes and other data but updating append announce addresses was missed. Fixes #3080
1 parent b936324 commit eea5989

File tree

2 files changed

+75
-10
lines changed

2 files changed

+75
-10
lines changed

packages/libp2p/src/address-manager/index.ts

+18-9
Original file line numberDiff line numberDiff line change
@@ -404,16 +404,25 @@ export class AddressManager implements AddressManagerInterface {
404404
.map(multiaddr => this.transportAddresses.get(multiaddr, this.addressVerificationTTL))
405405
)
406406

407+
const appendAnnounceMultiaddrs = this.getAppendAnnounceAddrs()
408+
407409
// add append announce addresses
408-
addresses = addresses.concat(
409-
this.getAppendAnnounceAddrs().map(multiaddr => ({
410-
multiaddr,
411-
verified: true,
412-
type: 'announce',
413-
expires: Date.now() + this.addressVerificationTTL,
414-
lastVerified: Date.now()
415-
}))
416-
)
410+
if (appendAnnounceMultiaddrs.length > 0) {
411+
// allow transports to add certhashes and other runtime information
412+
this.components.transportManager.getListeners().forEach(listener => {
413+
listener.updateAnnounceAddrs(appendAnnounceMultiaddrs)
414+
})
415+
416+
addresses = addresses.concat(
417+
appendAnnounceMultiaddrs.map(multiaddr => ({
418+
multiaddr,
419+
verified: true,
420+
type: 'announce',
421+
expires: Date.now() + this.addressVerificationTTL,
422+
lastVerified: Date.now()
423+
}))
424+
)
425+
}
417426

418427
// add observed addresses
419428
addresses = addresses.concat(

packages/libp2p/test/addresses/address-manager.spec.ts

+57-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ describe('Address Manager', () => {
8686
})
8787

8888
it('should add appendAnnounce multiaddrs on get', () => {
89-
const transportManager = stubInterface<TransportManager>()
89+
const transportManager = stubInterface<TransportManager>({
90+
getListeners: Sinon.stub().returns([])
91+
})
9092
const am = new AddressManager({
9193
peerId,
9294
transportManager,
@@ -810,6 +812,60 @@ describe('Address Manager', () => {
810812
type: 'observed'
811813
}])
812814
})
815+
816+
it('should allow updating announce addresses', async () => {
817+
const listener = stubInterface<Listener>({
818+
updateAnnounceAddrs: Sinon.stub().returnsArg(0)
819+
})
820+
821+
const am = new AddressManager({
822+
peerId,
823+
transportManager: stubInterface<TransportManager>({
824+
getListeners: Sinon.stub().returns([
825+
listener
826+
])
827+
}),
828+
peerStore,
829+
events,
830+
logger: defaultLogger()
831+
}, {
832+
announce: [
833+
'/ip4/123.123.123.123/tcp/1234'
834+
]
835+
})
836+
837+
expect(am.getAddresses()).to.have.lengthOf(1)
838+
expect(listener.updateAnnounceAddrs.called).to.be.true()
839+
})
840+
841+
it('should allow updating appendAnnounce addresses', async () => {
842+
const listener = stubInterface<Listener>({
843+
updateAnnounceAddrs: Sinon.stub().returnsArg(0)
844+
})
845+
846+
const am = new AddressManager({
847+
peerId,
848+
transportManager: stubInterface<TransportManager>({
849+
getAddrs: Sinon.stub().returns([
850+
multiaddr('/ip4/127.0.0.1/tcp/1234'),
851+
multiaddr('/ip4/192.168.1.123/tcp/1234')
852+
]),
853+
getListeners: Sinon.stub().returns([
854+
listener
855+
])
856+
}),
857+
peerStore,
858+
events,
859+
logger: defaultLogger()
860+
}, {
861+
appendAnnounce: [
862+
'/ip4/123.123.123.123/tcp/1234'
863+
]
864+
})
865+
866+
expect(am.getAddresses()).to.have.lengthOf(3)
867+
expect(listener.updateAnnounceAddrs.called).to.be.true()
868+
})
813869
})
814870

815871
function mapAddress (addr: NodeAddress): Pick<NodeAddress, 'multiaddr' | 'verified' | 'type'> {

0 commit comments

Comments
 (0)