github.com/amazechain/amc@v0.1.3/internal/p2p/gossip_topic_mappings.go (about)

     1  package p2p
     2  
     3  import (
     4  	"github.com/amazechain/amc/api/protocol/types_pb"
     5  	"reflect"
     6  
     7  	"google.golang.org/protobuf/proto"
     8  )
     9  
    10  // gossipTopicMappings represent the protocol ID to protobuf message type map for easy
    11  // lookup.
    12  var gossipTopicMappings = map[string]proto.Message{
    13  	BlockTopicFormat:       &types_pb.Block{},
    14  	TransactionTopicFormat: &types_pb.Transaction{},
    15  }
    16  
    17  // GossipTopicMappings is a function to return the assigned data type
    18  // versioned by epoch.
    19  func GossipTopicMappings(topic string) proto.Message {
    20  	return gossipTopicMappings[topic]
    21  }
    22  
    23  // AllTopics returns all topics stored in our
    24  // gossip mapping.
    25  func AllTopics() []string {
    26  	var topics []string
    27  	for k := range gossipTopicMappings {
    28  		topics = append(topics, k)
    29  	}
    30  	return topics
    31  }
    32  
    33  // GossipTypeMapping is the inverse of GossipTopicMappings so that an arbitrary protobuf message
    34  // can be mapped to a protocol ID string.
    35  var GossipTypeMapping = make(map[reflect.Type]string, len(gossipTopicMappings))
    36  
    37  func init() {
    38  	for k, v := range gossipTopicMappings {
    39  		GossipTypeMapping[reflect.TypeOf(v)] = k
    40  	}
    41  }