erigon-pulse/cl/sentinel/msg_id_test.go
Giulio rebuffo 57bcbaa21f
Adds flags to enable/disable backfilling and enable full historical beacon node (#8813)
* Correct naming of hash func in Eth2
* Customizable mode of operation for Caplin
2023-11-22 13:24:35 +01:00

51 lines
1.6 KiB
Go

package sentinel
import (
"context"
"fmt"
"testing"
"github.com/golang/snappy"
"github.com/ledgerwatch/erigon/cl/clparams"
"github.com/ledgerwatch/erigon/cl/utils"
pubsubpb "github.com/libp2p/go-libp2p-pubsub/pb"
"github.com/stretchr/testify/require"
)
func TestMsgID(t *testing.T) {
g := clparams.GenesisConfigs[clparams.MainnetNetwork]
n := clparams.NetworkConfigs[clparams.MainnetNetwork]
s := &Sentinel{
ctx: context.TODO(),
cfg: &SentinelConfig{
BeaconConfig: &clparams.MainnetBeaconConfig,
GenesisConfig: &g,
NetworkConfig: &n,
},
}
d := [4]byte{108, 122, 33, 65}
tpc := fmt.Sprintf("/eth2/%x/beacon_block", d)
topicLen := uint64(len(tpc))
topicLenBytes := utils.Uint64ToLE(topicLen)
invalidSnappy := [32]byte{'J', 'U', 'N', 'K'}
pMsg := &pubsubpb.Message{Data: invalidSnappy[:], Topic: &tpc}
// Create object to hash
combinedObj := append(n.MessageDomainInvalidSnappy[:], topicLenBytes...)
combinedObj = append(combinedObj, tpc...)
combinedObj = append(combinedObj, pMsg.Data...)
hashedData := utils.Sha256(combinedObj)
msgID := string(hashedData[:20])
require.Equal(t, msgID, s.msgId(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(n.MessageDomainValidSnappy[:], topicLenBytes...)
combinedObj = append(combinedObj, tpc...)
combinedObj = append(combinedObj, validObj[:]...)
hashedData = utils.Sha256(combinedObj)
msgID = string(hashedData[:20])
require.Equal(t, msgID, s.msgId(nMsg), "Got incorrect msg id")
}