2021-09-02 21:48:14 +00:00
|
|
|
package p2p_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/snappy"
|
|
|
|
pubsubpb "github.com/libp2p/go-libp2p-pubsub/pb"
|
2021-09-27 16:19:20 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/signing"
|
2021-09-02 21:48:14 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
|
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"
|
2021-09-17 19:20:50 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/network/forks"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
2021-09-02 21:48:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMsgID_HashesCorrectly(t *testing.T) {
|
|
|
|
genesisValidatorsRoot := bytesutil.PadTo([]byte{'A'}, 32)
|
2021-09-17 19:20:50 +00:00
|
|
|
d, err := forks.CreateForkDigest(time.Now(), genesisValidatorsRoot)
|
2021-09-02 21:48:14 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
tpc := fmt.Sprintf(p2p.BlockSubnetTopicFormat, d)
|
|
|
|
invalidSnappy := [32]byte{'J', 'U', 'N', 'K'}
|
|
|
|
pMsg := &pubsubpb.Message{Data: invalidSnappy[:], Topic: &tpc}
|
2021-09-15 22:55:11 +00:00
|
|
|
hashedData := hash.Hash(append(params.BeaconNetworkConfig().MessageDomainInvalidSnappy[:], pMsg.Data...))
|
2021-09-02 21:48:14 +00:00
|
|
|
msgID := string(hashedData[:20])
|
|
|
|
assert.Equal(t, msgID, p2p.MsgID(genesisValidatorsRoot, pMsg), "Got incorrect msg id")
|
|
|
|
|
|
|
|
validObj := [32]byte{'v', 'a', 'l', 'i', 'd'}
|
|
|
|
enc := snappy.Encode(nil, validObj[:])
|
|
|
|
nMsg := &pubsubpb.Message{Data: enc, Topic: &tpc}
|
2021-09-15 22:55:11 +00:00
|
|
|
hashedData = hash.Hash(append(params.BeaconNetworkConfig().MessageDomainValidSnappy[:], validObj[:]...))
|
2021-09-02 21:48:14 +00:00
|
|
|
msgID = string(hashedData[:20])
|
|
|
|
assert.Equal(t, msgID, p2p.MsgID(genesisValidatorsRoot, nMsg), "Got incorrect msg id")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMessageIDFunction_HashesCorrectlyAltair(t *testing.T) {
|
|
|
|
genesisValidatorsRoot := bytesutil.PadTo([]byte{'A'}, 32)
|
2021-09-27 16:19:20 +00:00
|
|
|
d, err := signing.ComputeForkDigest(params.BeaconConfig().AltairForkVersion, genesisValidatorsRoot)
|
2021-09-02 21:48:14 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
tpc := fmt.Sprintf(p2p.BlockSubnetTopicFormat, d)
|
|
|
|
topicLen := uint64(len(tpc))
|
|
|
|
topicLenBytes := bytesutil.Uint64ToBytesLittleEndian(topicLen)
|
|
|
|
invalidSnappy := [32]byte{'J', 'U', 'N', 'K'}
|
|
|
|
pMsg := &pubsubpb.Message{Data: invalidSnappy[:], Topic: &tpc}
|
|
|
|
// Create object to hash
|
|
|
|
combinedObj := append(params.BeaconNetworkConfig().MessageDomainInvalidSnappy[:], topicLenBytes...)
|
2022-01-20 14:12:15 +00:00
|
|
|
combinedObj = append(combinedObj, tpc...)
|
|
|
|
combinedObj = append(combinedObj, pMsg.Data...)
|
|
|
|
hashedData := hash.Hash(combinedObj)
|
|
|
|
msgID := string(hashedData[:20])
|
|
|
|
assert.Equal(t, msgID, p2p.MsgID(genesisValidatorsRoot, pMsg), "Got incorrect msg id")
|
|
|
|
|
|
|
|
validObj := [32]byte{'v', 'a', 'l', 'i', 'd'}
|
|
|
|
enc := snappy.Encode(nil, validObj[:])
|
|
|
|
nMsg := &pubsubpb.Message{Data: enc, Topic: &tpc}
|
|
|
|
// Create object to hash
|
|
|
|
combinedObj = append(params.BeaconNetworkConfig().MessageDomainValidSnappy[:], topicLenBytes...)
|
|
|
|
combinedObj = append(combinedObj, tpc...)
|
|
|
|
combinedObj = append(combinedObj, validObj[:]...)
|
|
|
|
hashedData = hash.Hash(combinedObj)
|
|
|
|
msgID = string(hashedData[:20])
|
|
|
|
assert.Equal(t, msgID, p2p.MsgID(genesisValidatorsRoot, nMsg), "Got incorrect msg id")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMessageIDFunction_HashesCorrectlyBellatrix(t *testing.T) {
|
|
|
|
genesisValidatorsRoot := bytesutil.PadTo([]byte{'A'}, 32)
|
|
|
|
d, err := signing.ComputeForkDigest(params.BeaconConfig().BellatrixForkVersion, genesisValidatorsRoot)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
tpc := fmt.Sprintf(p2p.BlockSubnetTopicFormat, d)
|
|
|
|
topicLen := uint64(len(tpc))
|
|
|
|
topicLenBytes := bytesutil.Uint64ToBytesLittleEndian(topicLen)
|
|
|
|
invalidSnappy := [32]byte{'J', 'U', 'N', 'K'}
|
|
|
|
pMsg := &pubsubpb.Message{Data: invalidSnappy[:], Topic: &tpc}
|
|
|
|
// Create object to hash
|
|
|
|
combinedObj := append(params.BeaconNetworkConfig().MessageDomainInvalidSnappy[:], topicLenBytes...)
|
2021-09-02 21:48:14 +00:00
|
|
|
combinedObj = append(combinedObj, tpc...)
|
|
|
|
combinedObj = append(combinedObj, pMsg.Data...)
|
2021-09-15 22:55:11 +00:00
|
|
|
hashedData := hash.Hash(combinedObj)
|
2021-09-02 21:48:14 +00:00
|
|
|
msgID := string(hashedData[:20])
|
|
|
|
assert.Equal(t, msgID, p2p.MsgID(genesisValidatorsRoot, pMsg), "Got incorrect msg id")
|
|
|
|
|
|
|
|
validObj := [32]byte{'v', 'a', 'l', 'i', 'd'}
|
|
|
|
enc := snappy.Encode(nil, validObj[:])
|
|
|
|
nMsg := &pubsubpb.Message{Data: enc, Topic: &tpc}
|
|
|
|
// Create object to hash
|
|
|
|
combinedObj = append(params.BeaconNetworkConfig().MessageDomainValidSnappy[:], topicLenBytes...)
|
|
|
|
combinedObj = append(combinedObj, tpc...)
|
|
|
|
combinedObj = append(combinedObj, validObj[:]...)
|
2021-09-15 22:55:11 +00:00
|
|
|
hashedData = hash.Hash(combinedObj)
|
2021-09-02 21:48:14 +00:00
|
|
|
msgID = string(hashedData[:20])
|
|
|
|
assert.Equal(t, msgID, p2p.MsgID(genesisValidatorsRoot, nMsg), "Got incorrect msg id")
|
|
|
|
}
|
2021-09-03 06:39:54 +00:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|