diff --git a/nodebuilder/p2p/cmd/p2p.go b/nodebuilder/p2p/cmd/p2p.go index 64c36fc9d6..391944a85f 100644 --- a/nodebuilder/p2p/cmd/p2p.go +++ b/nodebuilder/p2p/cmd/p2p.go @@ -34,6 +34,7 @@ func init() { peerBandwidthCmd, bandwidthForProtocolCmd, pubsubPeersCmd, + pubsubTopicsCmd, ) } @@ -574,3 +575,27 @@ var pubsubPeersCmd = &cobra.Command{ return cmdnode.PrintOutput(peers, err, formatter) }, } + +var pubsubTopicsCmd = &cobra.Command{ + Use: "pubsub-topics ", + Short: "Lists pubsub(GossipSub) topics the node participates in", + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, _ []string) error { + client, err := cmdnode.ParseClientFromCtx(cmd.Context()) + if err != nil { + return err + } + defer client.Close() + + topics, err := client.P2P.PubSubTopics(cmd.Context()) + formatter := func(data interface{}) interface{} { + conPeers := data.([]string) + return struct { + Topics []string `json:"topics"` + }{ + Topics: conPeers, + } + } + return cmdnode.PrintOutput(topics, err, formatter) + }, +} diff --git a/nodebuilder/p2p/mocks/api.go b/nodebuilder/p2p/mocks/api.go index aa5083199f..42e4f0a892 100644 --- a/nodebuilder/p2p/mocks/api.go +++ b/nodebuilder/p2p/mocks/api.go @@ -260,6 +260,21 @@ func (mr *MockModuleMockRecorder) PubSubPeers(arg0, arg1 interface{}) *gomock.Ca return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PubSubPeers", reflect.TypeOf((*MockModule)(nil).PubSubPeers), arg0, arg1) } +// PubSubTopics mocks base method. +func (m *MockModule) PubSubTopics(arg0 context.Context) ([]string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "PubSubTopics", arg0) + ret0, _ := ret[0].([]string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// PubSubTopics indicates an expected call of PubSubTopics. +func (mr *MockModuleMockRecorder) PubSubTopics(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PubSubTopics", reflect.TypeOf((*MockModule)(nil).PubSubTopics), arg0) +} + // ResourceState mocks base method. func (m *MockModule) ResourceState(arg0 context.Context) (rcmgr.ResourceManagerStat, error) { m.ctrl.T.Helper() diff --git a/nodebuilder/p2p/p2p.go b/nodebuilder/p2p/p2p.go index 236937e89a..15491f929a 100644 --- a/nodebuilder/p2p/p2p.go +++ b/nodebuilder/p2p/p2p.go @@ -77,6 +77,8 @@ type Module interface { // PubSubPeers returns the peer IDs of the peers joined on // the given topic. PubSubPeers(ctx context.Context, topic string) ([]peer.ID, error) + // PubSubTopics reports current PubSubTopics the node participates in. + PubSubTopics(ctx context.Context) ([]string, error) } // module contains all components necessary to access information and @@ -192,6 +194,10 @@ func (m *module) PubSubPeers(_ context.Context, topic string) ([]peer.ID, error) return m.ps.ListPeers(topic), nil } +func (m *module) PubSubTopics(_ context.Context) ([]string, error) { + return m.ps.GetTopics(), nil +} + // API is a wrapper around Module for the RPC. // TODO(@distractedm1nd): These structs need to be autogenerated. // @@ -216,6 +222,7 @@ type API struct { BandwidthForProtocol func(ctx context.Context, proto protocol.ID) (metrics.Stats, error) `perm:"admin"` ResourceState func(context.Context) (rcmgr.ResourceManagerStat, error) `perm:"admin"` PubSubPeers func(ctx context.Context, topic string) ([]peer.ID, error) `perm:"admin"` + PubSubTopics func(ctx context.Context) ([]string, error) `perm:"admin"` } } @@ -290,3 +297,7 @@ func (api *API) ResourceState(ctx context.Context) (rcmgr.ResourceManagerStat, e func (api *API) PubSubPeers(ctx context.Context, topic string) ([]peer.ID, error) { return api.Internal.PubSubPeers(ctx, topic) } + +func (api *API) PubSubTopics(ctx context.Context) ([]string, error) { + return api.Internal.PubSubTopics(ctx) +}