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: use xor-compare for finding closer peers #2538

Merged
merged 4 commits into from
May 13, 2024
Merged

Conversation

achingbrain
Copy link
Member

Switches from array compare to xor compare as this is how we're supposed to measure distance between two peers.

Also stores PeerInfos in peer distance list instead of PeerIds so to not lose the peers found in a query that are not in the peer store yet.

Change checklist

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation if necessary (this includes comments as well)
  • I have added tests that prove my fix is effective or that my feature works

Switches from array compare to xor compare as this is how we're
supposed to measure distance between two peers.

Also stores `PeerInfo`s in peer distance list instead of `PeerId`s
so to not lose the peers found in a query that are not in the peer
store yet.
@achingbrain achingbrain requested a review from a team as a code owner May 10, 2024 18:41
import { convertPeerId, getDistance } from '../utils.js'
import type { PeerId } from '@libp2p/interface'
import { xor as uint8ArrayXor } from 'uint8arrays/xor'
import { xorCompare as uint8ArrayXorCompare } from 'uint8arrays/xor-compare'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for future reference, this was added in achingbrain/uint8arrays#89

Copy link
Member

@maschad maschad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the change to XOR comparison overall but left some comments.

peerId: PeerId
distance: bigint
peer: PeerInfo
distance: Uint8Array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this ends up getting converted to a bigint what's the rationale behind changing these to Uint8Array ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're linking to main there - it no longer gets converted to a bigint, instead the Uint8Arrays are the xor distances that get compared using xorCompare.

packages/kad-dht/src/peer-routing/index.ts Outdated Show resolved Hide resolved
@achingbrain achingbrain merged commit 83c14d0 into main May 13, 2024
22 checks passed
@achingbrain achingbrain deleted the fix/use-xor-compare branch May 13, 2024 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants