-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
To make filtering peers in a memory-efficient way possible, add a peer filter based on a bloom filter using the binary representation of the peer id. --------- Co-authored-by: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com>
- Loading branch information
1 parent
e1923b0
commit 4afd7a9
Showing
5 changed files
with
73 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,26 @@ | ||
import { BloomFilter } from '@libp2p/utils/bloom-filter' | ||
import type { PeerId } from '@libp2p/interface' | ||
|
||
/** | ||
* Uses a Bloom filter to implement a mechansim for deduplicating PeerIds in a | ||
* way that uses a fixed amount of memory. | ||
*/ | ||
export class PeerFilter { | ||
private readonly filter: BloomFilter | ||
|
||
constructor (size: number, errorRate?: number) { | ||
this.filter = BloomFilter.create(size, errorRate) | ||
} | ||
|
||
has (peerId: PeerId): boolean { | ||
return this.filter.has(peerId.toBytes()) | ||
} | ||
|
||
add (peerId: PeerId): void { | ||
this.filter.add(peerId.toBytes()) | ||
} | ||
} | ||
|
||
export function peerFilter (size: number): PeerFilter { | ||
return new PeerFilter(size) | ||
} |
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,16 @@ | ||
import { createEd25519PeerId } from '@libp2p/peer-id-factory' | ||
import { expect } from 'aegir/chai' | ||
import { PeerFilter } from '../src/index.js' | ||
|
||
describe('peer-filter', () => { | ||
it('should filter a peer', async () => { | ||
const filter = new PeerFilter(1024) | ||
const peer = await createEd25519PeerId() | ||
|
||
expect(filter.has(peer)).to.be.false() | ||
|
||
filter.add(peer) | ||
|
||
expect(filter.has(peer)).to.be.true() | ||
}) | ||
}) |