Skip to content

Commit

Permalink
feat: auto detect cosmos modules (#86)
Browse files Browse the repository at this point in the history
This checks on startup the available Cosmos modules on the RPC endpoint
and automatically disables features that aren't available.

In practice, this is to avoid specifying options like `--no-gov`,
`--no-staking`, `--no-slashing`, etc.
  • Loading branch information
MattKetmo authored Nov 15, 2024
1 parent 541890a commit 1bf8650
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions pkg/app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strconv"
"syscall"

upgrade "cosmossdk.io/x/upgrade/types"
"github.com/cometbft/cometbft/rpc/client/http"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/types/query"
Expand Down Expand Up @@ -81,6 +82,17 @@ func RunFunc(cCtx *cli.Context) error {
return err
}

// Detect cosmos modules to automatically disable features
modules, err := detectCosmosModules(startCtx, pool.GetSyncedNode())
if err != nil {
log.Warn().Err(err).Msg("failed to detect cosmos modules")
} else {
noGov = !ensureCosmosModule("gov", modules) || noGov
noStaking = !ensureCosmosModule("staking", modules) || noStaking
noSlashing = !ensureCosmosModule("slashing", modules) || noSlashing
noCommission = !ensureCosmosModule("distribution", modules) || noCommission
}

// Parse validators into name & address
trackedValidators, err := createTrackedValidators(ctx, pool, validators, noStaking)
if err != nil {
Expand Down Expand Up @@ -308,6 +320,36 @@ func createNodePool(ctx context.Context, nodes []string) (*rpc.Pool, error) {
return rpc.NewPool(chainID, rpcNodes), nil
}

func detectCosmosModules(ctx context.Context, node *rpc.Node) ([]*upgrade.ModuleVersion, error) {
if node == nil {
return nil, fmt.Errorf("no node available")
}

clientCtx := (client.Context{}).WithClient(node.Client)
queryClient := upgrade.NewQueryClient(clientCtx)
resp, err := queryClient.ModuleVersions(ctx, &upgrade.QueryModuleVersionsRequest{})
if err != nil {
return nil, err
}

log.Debug().Msgf("detected %d cosmos modules", len(resp.ModuleVersions))

for _, module := range resp.ModuleVersions {
log.Debug().Str("module", module.Name).Uint64("version", module.Version).Msg("detected cosmos module")
}

return resp.ModuleVersions, nil
}

func ensureCosmosModule(name string, modules []*upgrade.ModuleVersion) bool {
for _, module := range modules {
if module.Name == name {
return true
}
}
return false
}

func createTrackedValidators(ctx context.Context, pool *rpc.Pool, validators []string, noStaking bool) ([]watcher.TrackedValidator, error) {
var stakingValidators []staking.Validator
if !noStaking {
Expand Down

0 comments on commit 1bf8650

Please sign in to comment.