p2p: Handle nil topic (#9522)

* fix nil topic

* fmt

Co-authored-by: nisdas <nishdas93@gmail.com>
This commit is contained in:
Preston Van Loon 2021-09-03 01:39:54 -05:00 committed by GitHub
parent 30b2adc5d6
commit 265b5feabf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View File

@ -21,7 +21,7 @@ import (
// the concatenation of `MESSAGE_DOMAIN_INVALID_SNAPPY` with the raw message data,
// i.e. `SHA256(MESSAGE_DOMAIN_INVALID_SNAPPY + message.data)[:20]`.
func MsgID(genesisValidatorsRoot []byte, pmsg *pubsub_pb.Message) string {
if pmsg == nil || pmsg.Data == nil {
if pmsg == nil || pmsg.Data == nil || pmsg.Topic == nil {
// Impossible condition that should
// never be hit.
msg := make([]byte, 20)

View File

@ -63,3 +63,16 @@ func TestMessageIDFunction_HashesCorrectlyAltair(t *testing.T) {
msgID = string(hashedData[:20])
assert.Equal(t, msgID, p2p.MsgID(genesisValidatorsRoot, nMsg), "Got incorrect msg id")
}
func TestMsgID_WithNilTopic(t *testing.T) {
msg := &pubsubpb.Message{
Data: make([]byte, 32),
Topic: nil,
}
invalid := make([]byte, 20)
copy(invalid, "invalid")
res := p2p.MsgID([]byte{0x01}, msg)
assert.Equal(t, res, string(invalid))
}