-
Notifications
You must be signed in to change notification settings - Fork 124
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
chore(lib/grandpa): update grandpa message tracker to store messages with map of maps, also store commit messages #1769
Changes from all commits
348cb52
9516893
a762b37
76174d8
2c76a4e
2257fdb
1550040
2946d6b
72da44e
a48b299
8310b18
7c0c25a
77a5ce0
0b221fd
2601406
28a8d74
9c9796d
6ad6be6
38587fc
a27a87f
cea99e9
1ec6d9f
44f0560
b283215
07c9b8e
92262a7
1982566
a574e51
7525524
39277b7
05b53b8
dede83e
ce7ebf5
362ef19
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,35 +21,38 @@ import ( | |
|
||
"github.com/ChainSafe/gossamer/dot/types" | ||
"github.com/ChainSafe/gossamer/lib/common" | ||
"github.com/ChainSafe/gossamer/lib/crypto/ed25519" | ||
) | ||
|
||
// tracker keeps track of messages that have been received that have failed to validate with ErrBlockDoesNotExist | ||
// these messages may be needed again in the case that we are slightly out of sync with the rest of the network | ||
type tracker struct { | ||
blockState BlockState | ||
messages map[common.Hash][]*networkVoteMessage // map of vote block hash -> array of VoteMessages for that hash | ||
mapLock sync.Mutex | ||
in chan *types.Block // receive imported block from BlockState | ||
chanID byte // BlockState channel ID | ||
out chan<- *networkVoteMessage // send a VoteMessage back to grandpa. corresponds to grandpa's in channel | ||
stopped chan struct{} | ||
blockState BlockState | ||
handler *MessageHandler | ||
voteMessages map[common.Hash]map[ed25519.PublicKeyBytes]*networkVoteMessage // map of vote block hash -> array of VoteMessages for that hash | ||
commitMessages map[common.Hash]*CommitMessage // map of commit block hash to commit message | ||
mapLock sync.Mutex | ||
in chan *types.Block // receive imported block from BlockState | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This channel is used to receive data
instead of
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah I think this relies on Ed's refactor in #1736 so I'd rather not change it here, I agree with you though that the whole notifier code needs to be refactored. I can change it here if you'd like but it overlaps with Ed's PR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I commented on this on Ed's PR #1736 (comment) |
||
chanID byte // BlockState channel ID | ||
stopped chan struct{} | ||
} | ||
|
||
func newTracker(bs BlockState, out chan<- *networkVoteMessage) (*tracker, error) { | ||
func newTracker(bs BlockState, handler *MessageHandler) (*tracker, error) { | ||
in := make(chan *types.Block, 16) | ||
id, err := bs.RegisterImportedChannel(in) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &tracker{ | ||
blockState: bs, | ||
messages: make(map[common.Hash][]*networkVoteMessage), | ||
mapLock: sync.Mutex{}, | ||
in: in, | ||
chanID: id, | ||
out: out, | ||
stopped: make(chan struct{}), | ||
blockState: bs, | ||
handler: handler, | ||
voteMessages: make(map[common.Hash]map[ed25519.PublicKeyBytes]*networkVoteMessage), | ||
commitMessages: make(map[common.Hash]*CommitMessage), | ||
mapLock: sync.Mutex{}, | ||
in: in, | ||
chanID: id, | ||
stopped: make(chan struct{}), | ||
}, nil | ||
} | ||
|
||
|
@@ -63,15 +66,27 @@ func (t *tracker) stop() { | |
close(t.in) | ||
} | ||
|
||
func (t *tracker) add(v *networkVoteMessage) { | ||
func (t *tracker) addVote(v *networkVoteMessage) { | ||
if v.msg == nil { | ||
return | ||
} | ||
|
||
t.mapLock.Lock() | ||
// TODO: change to map of maps, this allows duplicates | ||
t.messages[v.msg.Message.Hash] = append(t.messages[v.msg.Message.Hash], v) | ||
t.mapLock.Unlock() | ||
defer t.mapLock.Unlock() | ||
|
||
msgs, has := t.voteMessages[v.msg.Message.Hash] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change to
Code to verify
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added a test for the maps in |
||
if !has { | ||
msgs = make(map[ed25519.PublicKeyBytes]*networkVoteMessage) | ||
t.voteMessages[v.msg.Message.Hash] = msgs | ||
} | ||
|
||
msgs[v.msg.Message.AuthorityID] = v | ||
} | ||
|
||
func (t *tracker) addCommit(cm *CommitMessage) { | ||
t.mapLock.Lock() | ||
defer t.mapLock.Unlock() | ||
t.commitMessages[cm.Vote.Hash] = cm | ||
} | ||
|
||
func (t *tracker) handleBlocks() { | ||
|
@@ -82,18 +97,35 @@ func (t *tracker) handleBlocks() { | |
continue | ||
} | ||
|
||
t.mapLock.Lock() | ||
t.handleBlock(b) | ||
case <-t.stopped: | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (t *tracker) handleBlock(b *types.Block) { | ||
t.mapLock.Lock() | ||
defer t.mapLock.Unlock() | ||
|
||
h := b.Header.Hash() | ||
if t.messages[h] != nil { | ||
for _, v := range t.messages[h] { | ||
t.out <- v | ||
} | ||
h := b.Header.Hash() | ||
if vms, has := t.voteMessages[h]; has { | ||
for _, v := range vms { | ||
_, err := t.handler.handleMessage(v.from, v.msg) | ||
if err != nil { | ||
logger.Warn("failed to handle vote message", "message", v, "error", err) | ||
} | ||
} | ||
|
||
t.mapLock.Unlock() | ||
case <-t.stopped: | ||
return | ||
delete(t.voteMessages, h) | ||
} | ||
|
||
if cm, has := t.commitMessages[h]; has { | ||
_, err := t.handler.handleMessage("", cm) | ||
if err != nil { | ||
logger.Warn("failed to handle commit message", "message", cm, "error", err) | ||
} | ||
|
||
delete(t.commitMessages, h) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Issue created to this TODO -> #1771
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice, I'm still unsure about this todo, after the sync refactor it hopefully won't be necessary, but we'll see