Skip to content

Commit

Permalink
feat(modrpc): list pubsub topics RPC (#3744)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan committed Sep 18, 2024
1 parent 1a1286f commit 62771f0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
25 changes: 25 additions & 0 deletions nodebuilder/p2p/cmd/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func init() {
peerBandwidthCmd,
bandwidthForProtocolCmd,
pubsubPeersCmd,
pubsubTopicsCmd,
)
}

Expand Down Expand Up @@ -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)
},
}
15 changes: 15 additions & 0 deletions nodebuilder/p2p/mocks/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions nodebuilder/p2p/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
//
Expand All @@ -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"`
}
}

Expand Down Expand Up @@ -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)
}

0 comments on commit 62771f0

Please sign in to comment.