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

neutrino: Added ResetRanking method to PeerRanking. #288

Merged
merged 1 commit into from
Jan 12, 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
10 changes: 10 additions & 0 deletions query/peer_rank.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,13 @@ func (p *peerRanking) Reward(peer string) {

p.rank[peer] = score - 1
}

// ResetRanking sets the score of the passed peer to the defaultScore.
func (p *peerRanking) ResetRanking(peer string) {
Roasbeef marked this conversation as resolved.
Show resolved Hide resolved
_, ok := p.rank[peer]
if !ok {
return
}

p.rank[peer] = defaultScore
}
27 changes: 24 additions & 3 deletions query/peer_rank_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,35 @@ func TestPeerRank(t *testing.T) {
}
}

// Lastly, reward the lowest scored one a bunch, which should move it
// This is the lowest scored peer after punishment.
const lowestScoredPeer = "peer0"

// Reward the lowest scored one a bunch, which should move it
// to the front.
for i := 0; i < 10; i++ {
ranking.Reward("peer0")
ranking.Reward(lowestScoredPeer)
}

ranking.Order(peers)
if peers[0] != "peer0" {
if peers[0] != lowestScoredPeer {
t.Fatalf("peer0 was not first")
}

// Punish the peer a bunch to make it the lowest scored one.
for i := 0; i < 10; i++ {
ranking.Punish(lowestScoredPeer)
}

ranking.Order(peers)
if peers[len(peers)-1] != lowestScoredPeer {
t.Fatalf("peer0 should be last")
}

// Reset its ranking. It should have the default score now
// and should not be the lowest ranked peer.
ranking.ResetRanking(lowestScoredPeer)
ranking.Order(peers)
if peers[len(peers)-1] == lowestScoredPeer {
t.Fatalf("peer0 should not be last.")
}
}
10 changes: 9 additions & 1 deletion query/workmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ type PeerRanking interface {
// queries.
Punish(peer string)

// Order sorst the slice of peers according to their ranking.
// Order sorts the slice of peers according to their ranking.
Order(peers []string)

// ResetRanking sets the score of the passed peer to the defaultScore.
ResetRanking(peerAddr string)
Copy link
Member

Choose a reason for hiding this comment

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

Missing a godoc comment.

}

// activeWorker wraps a Worker that is currently running, together with the job
Expand Down Expand Up @@ -377,6 +380,11 @@ Loop:
result.job.timeout = newTimeout
}

// Refresh peer rank on disconnect.
if result.err == ErrPeerDisconnected {
w.cfg.Ranking.ResetRanking(result.peer.Addr())
Copy link
Member

Choose a reason for hiding this comment

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

Line should fold to 80 char columns.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please I checked this in my IDE and it is less than 80 char column

Copy link
Member

Choose a reason for hiding this comment

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

@Chinwendu20 did you configure your IDE to use 8 spaces for a tab character? If not, this will probably pop up in future PRs as well (in lnd the line length is even enforced by a linter).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for pointing this out, this was one of my earlier PRs. I have now configured my IDE to this. Does it still appear as going beyond the limit?

Copy link
Member

Choose a reason for hiding this comment

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

This particular line as it was merged, yes. But no big deal, just wanted to let you know for future PRs.

}

heap.Push(work, result.job)
currentQueries[result.job.index] = batchNum

Expand Down
3 changes: 3 additions & 0 deletions query/workmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ func (p *mockPeerRanking) Punish(peer string) {
func (p *mockPeerRanking) Reward(peer string) {
}

func (p *mockPeerRanking) ResetRanking(peer string) {
}

// startWorkManager starts a new workmanager with the given number of mock
// workers.
func startWorkManager(t *testing.T, numWorkers int) (WorkManager,
Expand Down
Loading