diff --git a/beacon-chain/p2p/message_id.go b/beacon-chain/p2p/message_id.go index 64e332aa3..fda26ad50 100644 --- a/beacon-chain/p2p/message_id.go +++ b/beacon-chain/p2p/message_id.go @@ -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) diff --git a/beacon-chain/p2p/message_id_test.go b/beacon-chain/p2p/message_id_test.go index cafc162ec..70a0e91d5 100644 --- a/beacon-chain/p2p/message_id_test.go +++ b/beacon-chain/p2p/message_id_test.go @@ -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)) +}