2020-07-13 01:20:53 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
2021-08-13 15:53:51 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2020-07-13 01:20:53 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-10-14 07:55:28 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/types"
|
2021-09-21 19:59:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
2022-04-29 14:32:11 +00:00
|
|
|
eth2types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
|
2021-09-23 15:23:37 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
2021-07-28 21:23:44 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
2020-07-13 01:20:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestVerifyRPCMappings(t *testing.T) {
|
2022-05-20 07:16:53 +00:00
|
|
|
params.SetupTestConfigCleanup(t)
|
2021-05-15 00:58:56 +00:00
|
|
|
assert.NoError(t, VerifyTopicMapping(RPCStatusTopicV1, &pb.Status{}), "Failed to verify status rpc topic")
|
|
|
|
assert.NotNil(t, VerifyTopicMapping(RPCStatusTopicV1, new([]byte)), "Incorrect message type verified for status rpc topic")
|
2020-07-13 01:20:53 +00:00
|
|
|
|
2021-05-15 00:58:56 +00:00
|
|
|
assert.NoError(t, VerifyTopicMapping(RPCMetaDataTopicV1, new(interface{})), "Failed to verify metadata rpc topic")
|
|
|
|
assert.NotNil(t, VerifyTopicMapping(RPCStatusTopicV1, new([]byte)), "Incorrect message type verified for metadata rpc topic")
|
2020-07-13 01:20:53 +00:00
|
|
|
|
2021-05-15 00:58:56 +00:00
|
|
|
assert.NoError(t, VerifyTopicMapping(RPCBlocksByRootTopicV1, new(types.BeaconBlockByRootsReq)), "Failed to verify blocks by root rpc topic")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTopicDeconstructor(t *testing.T) {
|
2022-05-20 07:16:53 +00:00
|
|
|
params.SetupTestConfigCleanup(t)
|
2021-05-15 00:58:56 +00:00
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
topic string
|
|
|
|
expectedError string
|
|
|
|
output []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "invalid topic",
|
|
|
|
topic: "/sjdksfks/dusidsdsd/ssz",
|
|
|
|
expectedError: "unable to find a valid protocol prefix for /sjdksfks/dusidsdsd/ssz",
|
|
|
|
output: []string{"", "", ""},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid status topic",
|
2021-08-13 15:53:51 +00:00
|
|
|
topic: protocolPrefix + StatusMessageName + SchemaVersionV1,
|
2021-05-15 00:58:56 +00:00
|
|
|
expectedError: "",
|
2021-08-13 15:53:51 +00:00
|
|
|
output: []string{protocolPrefix, StatusMessageName, SchemaVersionV1},
|
2021-05-15 00:58:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "malformed status topic",
|
|
|
|
topic: protocolPrefix + "/statis" + SchemaVersionV1,
|
|
|
|
expectedError: "unable to find a valid message for /eth2/beacon_chain/req/statis/1",
|
|
|
|
output: []string{""},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid beacon block by range topic",
|
2021-08-13 15:53:51 +00:00
|
|
|
topic: protocolPrefix + BeaconBlocksByRangeMessageName + SchemaVersionV1 + "/ssz_snappy",
|
2021-05-15 00:58:56 +00:00
|
|
|
expectedError: "",
|
2021-08-13 15:53:51 +00:00
|
|
|
output: []string{protocolPrefix, BeaconBlocksByRangeMessageName, SchemaVersionV1},
|
2021-05-15 00:58:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "beacon block by range topic with malformed version",
|
2021-08-13 15:53:51 +00:00
|
|
|
topic: protocolPrefix + BeaconBlocksByRangeMessageName + "/v" + "/ssz_snappy",
|
2021-05-15 00:58:56 +00:00
|
|
|
expectedError: "unable to find a valid schema version for /eth2/beacon_chain/req/beacon_blocks_by_range/v/ssz_snappy",
|
|
|
|
output: []string{""},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tt {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
protocolPref, message, version, err := TopicDeconstructor(test.topic)
|
|
|
|
if test.expectedError != "" {
|
|
|
|
require.NotNil(t, err)
|
|
|
|
assert.Equal(t, test.expectedError, err.Error())
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, test.output[0], protocolPref)
|
|
|
|
assert.Equal(t, test.output[1], message)
|
|
|
|
assert.Equal(t, test.output[2], version)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-07-13 01:20:53 +00:00
|
|
|
}
|
2021-08-13 15:53:51 +00:00
|
|
|
|
|
|
|
func TestTopicFromMessage_CorrectType(t *testing.T) {
|
|
|
|
params.SetupTestConfigCleanup(t)
|
2022-05-20 07:16:53 +00:00
|
|
|
bCfg := params.BeaconConfig().Copy()
|
2021-08-13 15:53:51 +00:00
|
|
|
forkEpoch := eth2types.Epoch(100)
|
|
|
|
bCfg.AltairForkEpoch = forkEpoch
|
|
|
|
bCfg.ForkVersionSchedule[bytesutil.ToBytes4(bCfg.AltairForkVersion)] = eth2types.Epoch(100)
|
|
|
|
params.OverrideBeaconConfig(bCfg)
|
|
|
|
|
|
|
|
// Garbage Message
|
|
|
|
badMsg := "wljdjska"
|
|
|
|
_, err := TopicFromMessage(badMsg, 0)
|
|
|
|
assert.ErrorContains(t, fmt.Sprintf("%s: %s", invalidRPCMessageType, badMsg), err)
|
|
|
|
// Before Fork
|
|
|
|
for m := range messageMapping {
|
|
|
|
topic, err := TopicFromMessage(m, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, true, strings.Contains(topic, SchemaVersionV1))
|
|
|
|
_, _, version, err := TopicDeconstructor(topic)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, SchemaVersionV1, version)
|
|
|
|
}
|
|
|
|
|
2022-01-20 14:12:15 +00:00
|
|
|
// Altair Fork
|
2021-08-13 15:53:51 +00:00
|
|
|
for m := range messageMapping {
|
|
|
|
topic, err := TopicFromMessage(m, forkEpoch)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
if altairMapping[m] {
|
|
|
|
assert.Equal(t, true, strings.Contains(topic, SchemaVersionV2))
|
|
|
|
_, _, version, err := TopicDeconstructor(topic)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, SchemaVersionV2, version)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, true, strings.Contains(topic, SchemaVersionV1))
|
|
|
|
_, _, version, err := TopicDeconstructor(topic)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, SchemaVersionV1, version)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|