From ac370fc9679b51da8cee3791b6dd268d0695d136 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Tue, 27 Apr 2021 17:14:48 +0200 Subject: [PATCH] fix: address book guarantees no replicated entries are added (#927) --- src/peer-store/address-book.js | 11 +++++++---- test/peer-store/address-book.spec.js | 8 ++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/peer-store/address-book.js b/src/peer-store/address-book.js index f04ea453de..eda90bdcc3 100644 --- a/src/peer-store/address-book.js +++ b/src/peer-store/address-book.js @@ -307,10 +307,13 @@ class AddressBook extends Book { throw errcode(new Error(`multiaddr ${addr} must be an instance of multiaddr`), ERR_INVALID_PARAMETERS) } - addresses.push({ - multiaddr: addr, - isCertified - }) + // Guarantee no replicates + if (!addresses.find((a) => a.multiaddr.equals(addr))) { + addresses.push({ + multiaddr: addr, + isCertified + }) + } }) return addresses diff --git a/test/peer-store/address-book.spec.js b/test/peer-store/address-book.spec.js index 512fc8fd35..ea71ed0b32 100644 --- a/test/peer-store/address-book.spec.js +++ b/test/peer-store/address-book.spec.js @@ -287,6 +287,14 @@ describe('addressBook', () => { await defer.promise }) + + it('does not add replicated content', () => { + // set 1 + ab.set(peerId, [addr1, addr1]) + + const addresses = ab.get(peerId) + expect(addresses).to.have.lengthOf(1) + }) }) describe('addressBook.get', () => {