Skip to content

fix(kademlia): neighborhood rebroadcast sends wrong peer lists - #5595

Open
gacevicljubisa wants to merge 2 commits into
masterfrom
fix/kademlia-neighborhood-rebroadcast
Open

fix(kademlia): neighborhood rebroadcast sends wrong peer lists#5595
gacevicljubisa wants to merge 2 commits into
masterfrom
fix/kademlia-neighborhood-rebroadcast

Conversation

@gacevicljubisa

Copy link
Copy Markdown
Member

Checklist

  • I have read the coding guide.
  • My change requires a documentation update, and I have done it.
  • I have added tests to cover my changes.
  • I have filled out the description and linked the related issues.

Description

The 15-minute neighborhood rebroadcast in the kademlia manage loop builds each
neighbor's list with append(neighbors[:i], neighbors[i+1:]...). That appends
into the backing array of the slice the loop is still ranging over, so from the
second iteration on the loop reads already-overwritten entries. With four
neighbors [a b c d] the loop actually sends:

i=0 -> a: [b c d] (neighbors is now [b c d d])
i=1 -> c: [b d d]
i=2 -> d: [b d d]
i=3 -> d: [b d d]

Only the first neighbor gets a correct list, b is never told anything, d is
told twice and told about itself, and the lists carry duplicates. Present since
#4412.

Changes:

  • Build the list with slices.Concat, which allocates a fresh slice.
  • Move the loop body into rebroadcastNeighborhood and expose it from
    export_test.go, so it can be driven without waiting fifteen minutes.
  • Add TestRebroadcastNeighborhood: connects four peers with storage radius 0,
    waits for the connect-time announces to settle, runs one rebroadcast, and
    asserts each neighbor receives exactly one message listing the other three
    and never itself. The test fails on master with "received 6 peers, want 3".

Open API Spec Version Changes (if applicable)

Motivation and Context (Optional)

Related Issue (Optional)

Screenshots (if appropriate):

AI Disclosure

  • This PR contains code that has been generated by an LLM.
  • I have reviewed the AI generated code thoroughly.
  • I possess the technical expertise to responsibly review the code generated in this PR.

Comment thread pkg/topology/kademlia/kademlia_test.go Outdated
for j := 1; j < n; j++ {
wantAnnounces += j + 1
}
if err := spinlock.Wait(spinLockWaitTime, func() bool {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this package again... why not synctest and some other channel driven mocks or something? would really like to see this package go away from the codebase

Comment thread pkg/topology/kademlia/export_test.go Outdated

// RebroadcastNeighborhood runs the neighborhood gossip the manage loop
// performs every fifteen minutes.
func (k *Kad) RebroadcastNeighborhood(ctx context.Context) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i would test here the method that creates the set of peers to be sent to every peer separately. just unit test that the sets are correct. then calling discovery is either trivial (and can go without a test at all) or very simple to test.

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.

3 participants