@@ -6,13 +6,18 @@ import (
66)
77
88type PeerExtensions struct {
9- TestExtension bool
9+ TestExtension bool
10+ TopicTableExtension * pubsub_pb.ExtTopicTable
1011}
1112
1213type TestExtensionConfig struct {
1314 OnReceiveTestExtension func (from peer.ID )
1415}
1516
17+ type TopicTableExtensionConfig struct {
18+ topicBundles [][]string
19+ }
20+
1621func WithTestExtension (c TestExtensionConfig ) Option {
1722 return func (ps * PubSub ) error {
1823 if rt , ok := ps .rt .(* GossipSubRouter ); ok {
@@ -26,6 +31,24 @@ func WithTestExtension(c TestExtensionConfig) Option {
2631 }
2732}
2833
34+ func WithTopicTableExtension (c TopicTableExtensionConfig ) Option {
35+ return func (ps * PubSub ) error {
36+ if rt , ok := ps .rt .(* GossipSubRouter ); ok {
37+ rt .extensions .topicTableExtension = & topicTableExtension {}
38+
39+ bundleHashes := make ([][]byte , len (c .topicBundles ))
40+ for _ , topics := range c .topicBundles {
41+ hash := computeTopicBundleHash (topics )
42+ bundleHashes = append (bundleHashes , hash [:])
43+ }
44+ rt .extensions .myExtensions .TopicTableExtension = & pubsub_pb.ExtTopicTable {
45+ TopicBundleHashes : bundleHashes ,
46+ }
47+ }
48+ return nil
49+ }
50+ }
51+
2952func hasPeerExtensions (rpc * RPC ) bool {
3053 if rpc != nil && rpc .Control != nil && rpc .Control .Extensions != nil {
3154 return true
@@ -37,6 +60,7 @@ func peerExtensionsFromRPC(rpc *RPC) PeerExtensions {
3760 out := PeerExtensions {}
3861 if hasPeerExtensions (rpc ) {
3962 out .TestExtension = rpc .Control .Extensions .GetTestExtension ()
63+ out .TopicTableExtension = rpc .Control .Extensions .GetTopicTableExtension ()
4064 }
4165 return out
4266}
@@ -46,9 +70,19 @@ func (pe *PeerExtensions) ExtendRPC(rpc *RPC) *RPC {
4670 if rpc .Control == nil {
4771 rpc .Control = & pubsub_pb.ControlMessage {}
4872 }
49- rpc .Control .Extensions = & pubsub_pb. ControlExtensions {
50- TestExtension : & pe . TestExtension ,
73+ if rpc .Control .Extensions == nil {
74+ rpc . Control . Extensions = & pubsub_pb. ControlExtensions {}
5175 }
76+ rpc .Control .Extensions .TestExtension = & pe .TestExtension
77+ }
78+ if pe .TopicTableExtension != nil {
79+ if rpc .Control == nil {
80+ rpc .Control = & pubsub_pb.ControlMessage {}
81+ }
82+ if rpc .Control .Extensions == nil {
83+ rpc .Control .Extensions = & pubsub_pb.ControlExtensions {}
84+ }
85+ rpc .Control .Extensions .TopicTableExtension = pe .TopicTableExtension
5286 }
5387 return rpc
5488}
@@ -60,7 +94,8 @@ type extensionsState struct {
6094 reportMisbehavior func (peer.ID )
6195 sendRPC func (p peer.ID , r * RPC , urgent bool )
6296
63- testExtension * testExtension
97+ testExtension * testExtension
98+ topicTableExtension * topicTableExtension
6499}
65100
66101func newExtensionsState (myExtensions PeerExtensions , reportMisbehavior func (peer.ID ), sendRPC func (peer.ID , * RPC , bool )) * extensionsState {
@@ -70,7 +105,9 @@ func newExtensionsState(myExtensions PeerExtensions, reportMisbehavior func(peer
70105 sentExtensions : make (map [peer.ID ]struct {}),
71106 reportMisbehavior : reportMisbehavior ,
72107 sendRPC : sendRPC ,
73- testExtension : nil ,
108+
109+ testExtension : nil ,
110+ topicTableExtension : nil ,
74111 }
75112}
76113
0 commit comments