Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove peer cache #2786

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/peer-store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
"dependencies": {
"@libp2p/crypto": "^5.0.5",
"@libp2p/interface": "^2.1.3",
"@libp2p/peer-collections": "^6.0.9",
"@libp2p/peer-id": "^5.0.6",
"@libp2p/peer-record": "^8.0.9",
"@multiformats/multiaddr": "^12.2.3",
Expand Down
27 changes: 7 additions & 20 deletions packages/peer-store/src/store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { InvalidParametersError } from '@libp2p/interface'
import { PeerMap } from '@libp2p/peer-collections'
import { peerIdFromCID } from '@libp2p/peer-id'
import mortice, { type Mortice } from 'mortice'
import { base32 } from 'multiformats/bases/base32'
Expand All @@ -20,37 +19,27 @@
updated: boolean
}

function decodePeer (key: Key, value: Uint8Array, cache: PeerMap<Peer>): Peer {
function decodePeer (key: Key, value: Uint8Array): Peer {
// /peers/${peer-id-as-libp2p-key-cid-string-in-base-32}
const base32Str = key.toString().split('/')[2]
const buf = CID.parse(base32Str, base32)
const peerId = peerIdFromCID(buf)

const cached = cache.get(peerId)

if (cached != null) {
return cached
}

const peer = bytesToPeer(peerId, value)

cache.set(peerId, peer)

return peer
return bytesToPeer(peerId, value)
}

function mapQuery (query: PeerQuery, cache: PeerMap<Peer>): Query {
function mapQuery (query: PeerQuery): Query {
if (query == null) {
return {}
}

return {
prefix: NAMESPACE_COMMON,
filters: (query.filters ?? []).map(fn => ({ key, value }) => {
return fn(decodePeer(key, value, cache))
return fn(decodePeer(key, value))
}),
orders: (query.orders ?? []).map(fn => (a, b) => {
return fn(decodePeer(a.key, a.value, cache), decodePeer(b.key, b.value, cache))
return fn(decodePeer(a.key, a.value), decodePeer(b.key, b.value))

Check warning on line 42 in packages/peer-store/src/store.ts

View check run for this annotation

Codecov / codecov/patch

packages/peer-store/src/store.ts#L42

Added line #L42 was not covered by tests
})
}
}
Expand Down Expand Up @@ -131,10 +120,8 @@
}

async * all (query?: PeerQuery): AsyncGenerator<Peer, void, unknown> {
const peerCache = new PeerMap<Peer>()

for await (const { key, value } of this.datastore.query(mapQuery(query ?? {}, peerCache))) {
const peer = decodePeer(key, value, peerCache)
for await (const { key, value } of this.datastore.query(mapQuery(query ?? {}))) {
const peer = decodePeer(key, value)

if (peer.id.equals(this.peerId)) {
// Skip self peer if present
Expand Down
Loading