Skip to content

Commit

Permalink
fix: address book guarantees no replicated entries are added (#927)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos authored Apr 27, 2021
1 parent ef43936 commit ac370fc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/peer-store/address-book.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/peer-store/address-book.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit ac370fc

Please sign in to comment.