Skip to content

Commit 5e9ba1e

Browse files
committed
Replace forEach by networkVoteMessages
1 parent 5b117cc commit 5e9ba1e

File tree

3 files changed

+23
-46
lines changed

3 files changed

+23
-46
lines changed

lib/grandpa/message_tracker.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,20 +129,18 @@ func (t *tracker) handleTick() {
129129
t.mapLock.Lock()
130130
defer t.mapLock.Unlock()
131131

132-
var blockHashesDone []common.Hash
133-
t.votes.forEach(func(peerID peer.ID, message *VoteMessage) {
132+
for _, networkVoteMessage := range t.votes.networkVoteMessages() {
133+
peerID := networkVoteMessage.from
134+
message := networkVoteMessage.msg
134135
_, err := t.handler.handleMessage(peerID, message)
135136
if err != nil {
136137
// handleMessage would never error for vote message
137138
logger.Debugf("failed to handle vote message %v from peer id %s: %s", message, peerID, err)
138139
}
139140

140141
if message.Round < t.handler.grandpa.state.round && message.SetID == t.handler.grandpa.state.setID {
141-
blockHashesDone = append(blockHashesDone, message.Message.BlockHash)
142+
t.votes.delete(message.Message.BlockHash)
142143
}
143-
})
144-
for _, blockHashDone := range blockHashesDone {
145-
t.votes.delete(blockHashDone)
146144
}
147145

148146
for _, cm := range t.commitMessages {

lib/grandpa/votes_tracker.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,20 @@ func (vt *votesTracker) messages(blockHash common.Hash) (
158158
return messages
159159
}
160160

161-
// forEach runs the function `f` on each
162-
// peer id + message stored in the tracker.
163-
func (vt *votesTracker) forEach(
164-
f func(peerID peer.ID, message *VoteMessage)) {
161+
// networkVoteMessages returns all pairs of
162+
// peer id + message stored in the tracker
163+
// as a slice of networkVoteMessages.
164+
func (vt *votesTracker) networkVoteMessages() (
165+
messages []networkVoteMessage) {
166+
messages = make([]networkVoteMessage, 0, vt.linkedList.Len())
165167
for _, authorityIDToData := range vt.mapping {
166168
for _, data := range authorityIDToData {
167-
f(data.peerID, data.message)
169+
message := networkVoteMessage{
170+
from: data.peerID,
171+
msg: data.message,
172+
}
173+
messages = append(messages, message)
168174
}
169175
}
176+
return messages
170177
}

lib/grandpa/votes_tracker_test.go

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package grandpa
55

66
import (
7-
"bytes"
87
"container/list"
98
"sort"
109
"testing"
@@ -320,7 +319,7 @@ func Test_votesTracker_messages(t *testing.T) {
320319
}
321320
}
322321

323-
func Test_votesTracker_forEach(t *testing.T) {
322+
func Test_votesTracker_networkVoteMessages(t *testing.T) {
324323
t.Parallel()
325324

326325
const capacity = 10
@@ -340,40 +339,13 @@ func Test_votesTracker_forEach(t *testing.T) {
340339
vt.add("b", messageBlockAAuthB)
341340
vt.add("b", messageBlockBAuthA)
342341

343-
type result struct {
344-
peerID peer.ID
345-
message *VoteMessage
346-
}
347-
var results []result
348-
349-
vt.forEach(func(peerID peer.ID, message *VoteMessage) {
350-
results = append(results, result{
351-
peerID: peerID,
352-
message: message,
353-
})
354-
})
355-
356-
// Predictable messages order for assertion.
357-
// Sort by block hash then authority id then peer ID.
358-
sort.Slice(results, func(i, j int) bool {
359-
blockHashFirst := results[i].message.Message.BlockHash
360-
blockHashSecond := results[j].message.Message.BlockHash
361-
if blockHashFirst == blockHashSecond {
362-
authIDFirst := results[i].message.Message.AuthorityID
363-
authIDSecond := results[j].message.Message.AuthorityID
364-
if authIDFirst == authIDSecond {
365-
return results[i].peerID < results[j].peerID
366-
}
367-
return bytes.Compare(authIDFirst[:], authIDSecond[:]) < 0
368-
}
369-
return bytes.Compare(blockHashFirst[:], blockHashSecond[:]) < 0
370-
})
342+
networkVoteMessages := vt.networkVoteMessages()
371343

372-
expectedResults := []result{
373-
{peerID: "a", message: messageBlockAAuthA},
374-
{peerID: "b", message: messageBlockAAuthB},
375-
{peerID: "b", message: messageBlockBAuthA},
344+
expectedNetworkVoteMessages := []networkVoteMessage{
345+
{from: "a", msg: messageBlockAAuthA},
346+
{from: "b", msg: messageBlockAAuthB},
347+
{from: "b", msg: messageBlockBAuthA},
376348
}
377349

378-
assert.Equal(t, expectedResults, results)
350+
assert.ElementsMatch(t, expectedNetworkVoteMessages, networkVoteMessages)
379351
}

0 commit comments

Comments
 (0)