2018-07-17 18:39:04 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
|
2018-07-31 18:54:45 +00:00
|
|
|
beaconpb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
|
|
shardpb "github.com/prysmaticlabs/prysm/proto/sharding/p2p/v1"
|
2018-07-17 18:39:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Mapping of message topic enums to protobuf types.
|
2018-07-31 18:54:45 +00:00
|
|
|
var topicTypeMapping = map[shardpb.Topic]reflect.Type{
|
2018-08-07 12:12:10 +00:00
|
|
|
shardpb.Topic_BEACON_BLOCK_HASH_ANNOUNCE: reflect.TypeOf(beaconpb.BeaconBlockHashAnnounce{}),
|
|
|
|
shardpb.Topic_BEACON_BLOCK_REQUEST: reflect.TypeOf(beaconpb.BeaconBlockRequest{}),
|
|
|
|
shardpb.Topic_BEACON_BLOCK_REQUEST_BY_SLOT_NUMBER: reflect.TypeOf(beaconpb.BeaconBlockRequestBySlotNumber{}),
|
|
|
|
shardpb.Topic_BEACON_BLOCK_RESPONSE: reflect.TypeOf(beaconpb.BeaconBlockResponse{}),
|
|
|
|
shardpb.Topic_COLLATION_BODY_REQUEST: reflect.TypeOf(shardpb.CollationBodyRequest{}),
|
|
|
|
shardpb.Topic_COLLATION_BODY_RESPONSE: reflect.TypeOf(shardpb.CollationBodyResponse{}),
|
|
|
|
shardpb.Topic_TRANSACTIONS: reflect.TypeOf(shardpb.Transaction{}),
|
|
|
|
shardpb.Topic_CRYSTALLIZED_STATE_HASH_ANNOUNCE: reflect.TypeOf(beaconpb.CrystallizedStateHashAnnounce{}),
|
|
|
|
shardpb.Topic_CRYSTALLIZED_STATE_REQUEST: reflect.TypeOf(beaconpb.CrystallizedStateRequest{}),
|
|
|
|
shardpb.Topic_CRYSTALLIZED_STATE_RESPONSE: reflect.TypeOf(beaconpb.CrystallizedStateResponse{}),
|
|
|
|
shardpb.Topic_ACTIVE_STATE_HASH_ANNOUNCE: reflect.TypeOf(beaconpb.ActiveStateHashAnnounce{}),
|
|
|
|
shardpb.Topic_ACTIVE_STATE_REQUEST: reflect.TypeOf(beaconpb.ActiveStateRequest{}),
|
|
|
|
shardpb.Topic_ACTIVE_STATE_RESPONSE: reflect.TypeOf(beaconpb.ActiveStateResponse{}),
|
2018-07-17 18:39:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mapping of message types to topic enums.
|
|
|
|
var typeTopicMapping = reverseMapping(topicTypeMapping)
|
|
|
|
|
|
|
|
// ReverseMapping from K,V to V,K
|
2018-07-31 18:54:45 +00:00
|
|
|
func reverseMapping(m map[shardpb.Topic]reflect.Type) map[reflect.Type]shardpb.Topic {
|
|
|
|
n := make(map[reflect.Type]shardpb.Topic)
|
2018-07-17 18:39:04 +00:00
|
|
|
for k, v := range m {
|
|
|
|
n[v] = k
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2018-07-31 18:54:45 +00:00
|
|
|
// These functions return the given topic for a given interface. This is the preferred
|
2018-07-17 18:39:04 +00:00
|
|
|
// way to resolve a topic from an value. The msg could be a pointer or value
|
|
|
|
// argument to resolve to the correct topic.
|
2018-07-31 18:54:45 +00:00
|
|
|
func topic(msg interface{}) shardpb.Topic {
|
2018-07-17 18:39:04 +00:00
|
|
|
msgType := reflect.TypeOf(msg)
|
|
|
|
if msgType.Kind() == reflect.Ptr {
|
|
|
|
msgType = reflect.Indirect(reflect.ValueOf(msg)).Type()
|
|
|
|
}
|
|
|
|
return typeTopicMapping[msgType]
|
|
|
|
}
|