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

PeerStore Ring Move? Possible Bug? #72

Open
qwtsc opened this issue Dec 10, 2020 · 1 comment
Open

PeerStore Ring Move? Possible Bug? #72

qwtsc opened this issue Dec 10, 2020 · 1 comment

Comments

@qwtsc
Copy link

qwtsc commented Dec 10, 2020

When I looked at the code of peerContactsSet, this function seems to be wrong.

func (p *peerContactsSet) next() []string {
	count := kNodes
	if count > len(p.set) {
		count = len(p.set)
	}
	x := make([]string, 0, count)
	xx := make(map[string]bool) //maps are easier to dedupe
	for range p.set {
		nid := p.ring.Move(1).Value.(string)  <----this line will always return the first next item since p.ring never change its pos.
		if _, ok := xx[nid]; p.set[nid] && !ok {
			xx[nid] = true
		}
		if len(xx) >= count {
			break
		}
	}
...

The move function will return a new pointer rather than change the original pointer as demonstrated in this playground.

func (r *Ring) Move(n int) *Ring {
	if r.next == nil {
		return r.init()
	}
	switch {
	case n < 0:
		for ; n < 0; n++ {
			r = r.prev
		}
	case n > 0:
		for ; n > 0; n-- {
			r = r.next
		}
	}
	return r
}

Need a PR to fix it?

@nictuku
Copy link
Owner

nictuku commented Dec 26, 2020

It's been a while since I've written this, but if you could write a unit test demonstrating the fix, a PR would be great, yes.

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

No branches or pull requests

2 participants