2021-09-02 21:48:14 +00:00
package p2p
import (
pubsub_pb "github.com/libp2p/go-libp2p-pubsub/pb"
2022-01-20 14:12:15 +00:00
types "github.com/prysmaticlabs/eth2-types"
2021-09-02 21:48:14 +00:00
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/encoder"
2021-09-21 19:59:25 +00:00
"github.com/prysmaticlabs/prysm/config/params"
2021-09-15 22:55:11 +00:00
"github.com/prysmaticlabs/prysm/crypto/hash"
2021-09-23 15:23:37 +00:00
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
2022-03-11 09:34:30 +00:00
"github.com/prysmaticlabs/prysm/math"
2021-09-17 19:20:50 +00:00
"github.com/prysmaticlabs/prysm/network/forks"
2021-09-02 21:48:14 +00:00
)
// MsgID is a content addressable ID function.
//
// Ethereum Beacon Chain spec defines the message ID as:
// The `message-id` of a gossipsub message MUST be the following 20 byte value computed from the message data:
// If `message.data` has a valid snappy decompression, set `message-id` to the first 20 bytes of the `SHA256` hash of
// the concatenation of `MESSAGE_DOMAIN_VALID_SNAPPY` with the snappy decompressed message data,
// i.e. `SHA256(MESSAGE_DOMAIN_VALID_SNAPPY + snappy_decompress(message.data))[:20]`.
//
// Otherwise, set `message-id` to the first 20 bytes of the `SHA256` hash of
// 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 {
2021-09-03 06:39:54 +00:00
if pmsg == nil || pmsg . Data == nil || pmsg . Topic == nil {
2021-09-02 21:48:14 +00:00
// Impossible condition that should
// never be hit.
msg := make ( [ ] byte , 20 )
copy ( msg , "invalid" )
return string ( msg )
}
digest , err := ExtractGossipDigest ( * pmsg . Topic )
if err != nil {
// Impossible condition that should
// never be hit.
msg := make ( [ ] byte , 20 )
copy ( msg , "invalid" )
return string ( msg )
}
2021-09-17 19:20:50 +00:00
_ , fEpoch , err := forks . RetrieveForkDataFromDigest ( digest , genesisValidatorsRoot )
2021-09-02 21:48:14 +00:00
if err != nil {
// Impossible condition that should
// never be hit.
msg := make ( [ ] byte , 20 )
copy ( msg , "invalid" )
return string ( msg )
}
if fEpoch >= params . BeaconConfig ( ) . AltairForkEpoch {
2022-01-20 14:12:15 +00:00
return postAltairMsgID ( pmsg , fEpoch )
2021-09-02 21:48:14 +00:00
}
decodedData , err := encoder . DecodeSnappy ( pmsg . Data , params . BeaconNetworkConfig ( ) . GossipMaxSize )
if err != nil {
combinedData := append ( params . BeaconNetworkConfig ( ) . MessageDomainInvalidSnappy [ : ] , pmsg . Data ... )
2021-09-15 22:55:11 +00:00
h := hash . Hash ( combinedData )
2021-09-02 21:48:14 +00:00
return string ( h [ : 20 ] )
}
combinedData := append ( params . BeaconNetworkConfig ( ) . MessageDomainValidSnappy [ : ] , decodedData ... )
2021-09-15 22:55:11 +00:00
h := hash . Hash ( combinedData )
2021-09-02 21:48:14 +00:00
return string ( h [ : 20 ] )
}
// Spec:
// The derivation of the message-id has changed starting with Altair to incorporate the message topic along with the message data.
// These are fields of the Message Protobuf, and interpreted as empty byte strings if missing. The message-id MUST be the following
// 20 byte value computed from the message:
//
// If message.data has a valid snappy decompression, set message-id to the first 20 bytes of the SHA256 hash of the concatenation of
// the following data: MESSAGE_DOMAIN_VALID_SNAPPY, the length of the topic byte string (encoded as little-endian uint64), the topic
// byte string, and the snappy decompressed message data: i.e. SHA256(MESSAGE_DOMAIN_VALID_SNAPPY + uint_to_bytes(uint64(len(message.topic)))
// + message.topic + snappy_decompress(message.data))[:20]. Otherwise, set message-id to the first 20 bytes of the SHA256 hash of the concatenation
// of the following data: MESSAGE_DOMAIN_INVALID_SNAPPY, the length of the topic byte string (encoded as little-endian uint64),
// the topic byte string, and the raw message data: i.e. SHA256(MESSAGE_DOMAIN_INVALID_SNAPPY + uint_to_bytes(uint64(len(message.topic))) + message.topic + message.data)[:20].
2022-01-20 14:12:15 +00:00
func postAltairMsgID ( pmsg * pubsub_pb . Message , fEpoch types . Epoch ) string {
2021-09-02 21:48:14 +00:00
topic := * pmsg . Topic
2022-03-11 09:34:30 +00:00
topicLen := len ( topic )
topicLenBytes := bytesutil . Uint64ToBytesLittleEndian ( uint64 ( topicLen ) ) // topicLen cannot be negative
2021-09-02 21:48:14 +00:00
2022-01-26 07:24:47 +00:00
// beyond Bellatrix epoch, allow 10 Mib gossip data size
2022-01-20 14:12:15 +00:00
gossipPubSubSize := params . BeaconNetworkConfig ( ) . GossipMaxSize
if fEpoch >= params . BeaconConfig ( ) . BellatrixForkEpoch {
gossipPubSubSize = params . BeaconNetworkConfig ( ) . GossipMaxSizeBellatrix
}
decodedData , err := encoder . DecodeSnappy ( pmsg . Data , gossipPubSubSize )
2021-09-02 21:48:14 +00:00
if err != nil {
2022-03-11 09:34:30 +00:00
totalLength , err := math . AddInt (
len ( params . BeaconNetworkConfig ( ) . MessageDomainValidSnappy ) ,
len ( topicLenBytes ) ,
topicLen ,
len ( pmsg . Data ) ,
)
if err != nil {
log . WithError ( err ) . Error ( "Failed to sum lengths of message domain and topic" )
// should never happen
msg := make ( [ ] byte , 20 )
copy ( msg , "invalid" )
return string ( msg )
}
if uint64 ( totalLength ) > gossipPubSubSize {
// this should never happen
msg := make ( [ ] byte , 20 )
copy ( msg , "invalid" )
return string ( msg )
}
2021-09-02 21:48:14 +00:00
combinedData := make ( [ ] byte , 0 , totalLength )
combinedData = append ( combinedData , params . BeaconNetworkConfig ( ) . MessageDomainInvalidSnappy [ : ] ... )
combinedData = append ( combinedData , topicLenBytes ... )
combinedData = append ( combinedData , topic ... )
combinedData = append ( combinedData , pmsg . Data ... )
2021-09-15 22:55:11 +00:00
h := hash . Hash ( combinedData )
2021-09-02 21:48:14 +00:00
return string ( h [ : 20 ] )
}
2022-03-11 09:34:30 +00:00
totalLength , err := math . AddInt (
len ( params . BeaconNetworkConfig ( ) . MessageDomainValidSnappy ) ,
len ( topicLenBytes ) ,
topicLen ,
len ( decodedData ) ,
)
if err != nil {
log . WithError ( err ) . Error ( "Failed to sum lengths of message domain and topic" )
// should never happen
msg := make ( [ ] byte , 20 )
copy ( msg , "invalid" )
return string ( msg )
}
2021-09-02 21:48:14 +00:00
combinedData := make ( [ ] byte , 0 , totalLength )
combinedData = append ( combinedData , params . BeaconNetworkConfig ( ) . MessageDomainValidSnappy [ : ] ... )
combinedData = append ( combinedData , topicLenBytes ... )
combinedData = append ( combinedData , topic ... )
combinedData = append ( combinedData , decodedData ... )
2021-09-15 22:55:11 +00:00
h := hash . Hash ( combinedData )
2021-09-02 21:48:14 +00:00
return string ( h [ : 20 ] )
}