From 4e97c4951ef4792eae5b73c1c82765e1099b9904 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Fri, 9 Aug 2024 22:41:27 -0600 Subject: [PATCH] feat: module hash by height query (#20779) Co-authored-by: Facundo Medica <14063057+facundomedica@users.noreply.github.com> Co-authored-by: Marko --- CHANGELOG.md | 1 + server/module_hash.go | 101 ++++++++++++++++++++++++++++++++++++++++++ server/util.go | 1 + 3 files changed, 103 insertions(+) create mode 100644 server/module_hash.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 32fe54e08ea8..e220eb440016 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -61,6 +61,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i * (client) [#19870](https://github.com/cosmos/cosmos-sdk/pull/19870) Add new query command `wait-tx`. Alias `event-query-tx-for` to `wait-tx` for backward compatibility. * (crypto/keyring) [#20212](https://github.com/cosmos/cosmos-sdk/pull/20212) Expose the db keyring used in the keystore. * (genutil) [#19971](https://github.com/cosmos/cosmos-sdk/pull/19971) Allow manually setting the consensus key type in genesis +* (cli) [#20779](https://github.com/cosmos/cosmos-sdk/pull/20779) Added `module-hash-by-height` command to query and retrieve module hashes at a specified blockchain height, enhancing debugging capabilities. * (client/tx) [#20870](https://github.com/cosmos/cosmos-sdk/pull/20870) Add `timeout-timestamp` field for tx body defines time based timeout.Add `WithTimeoutTimestamp` to tx factory. Increased gas cost for processing newly added timeout timestamp field in tx body. ### Improvements diff --git a/server/module_hash.go b/server/module_hash.go new file mode 100644 index 000000000000..966a29139038 --- /dev/null +++ b/server/module_hash.go @@ -0,0 +1,101 @@ +package server + +import ( + "encoding/hex" + "fmt" + "sort" + "strconv" + "strings" + + "github.com/spf13/cobra" + + "cosmossdk.io/store/rootmulti" + storetypes "cosmossdk.io/store/types" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + servertypes "github.com/cosmos/cosmos-sdk/server/types" + "github.com/cosmos/cosmos-sdk/version" +) + +// ModuleHashByHeightQuery retrieves the module hashes at a given height. +func ModuleHashByHeightQuery[T servertypes.Application](appCreator servertypes.AppCreator[T]) *cobra.Command { + cmd := &cobra.Command{ + Use: "module-hash-by-height [height]", + Short: "Get module hashes at a given height", + Long: "Get module hashes at a given height. This command is useful for debugging and verifying the state of the application at a given height. Daemon should not be running when calling this command.", + Example: fmt.Sprintf("%s module-hash-by-height 16841115", version.AppName), + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + heightToRetrieveString := args[0] + + serverCtx := GetServerContextFromCmd(cmd) + + height, err := strconv.ParseInt(heightToRetrieveString, 10, 64) + if err != nil { + return fmt.Errorf("invalid height: %w", err) + } + + commitInfoForHeight, err := getModuleHashesAtHeight(serverCtx, appCreator, height) + if err != nil { + return err + } + + clientCtx := client.GetClientContextFromCmd(cmd) + return clientCtx.PrintProto(commitInfoForHeight) + }, + } + + flags.AddQueryFlagsToCmd(cmd) + + return cmd +} + +func getModuleHashesAtHeight[T servertypes.Application](svrCtx *Context, appCreator servertypes.AppCreator[T], height int64) (*storetypes.CommitInfo, error) { + home := svrCtx.Config.RootDir + db, err := OpenDB(home, GetAppDBBackend(svrCtx.Viper)) + if err != nil { + return nil, fmt.Errorf("error opening DB, make sure daemon is not running when calling this query: %w", err) + } + app := appCreator(svrCtx.Logger, db, nil, svrCtx.Viper) + rms, ok := app.CommitMultiStore().(*rootmulti.Store) + if !ok { + return nil, fmt.Errorf("expected rootmulti.Store, got %T", app.CommitMultiStore()) + } + + commitInfoForHeight, err := rms.GetCommitInfo(height) + if err != nil { + return nil, err + } + + // Create a new slice of StoreInfos for storing the modified hashes. + storeInfos := make([]storetypes.StoreInfo, len(commitInfoForHeight.StoreInfos)) + + for i, storeInfo := range commitInfoForHeight.StoreInfos { + // Convert the hash to a hexadecimal string. + hash := strings.ToUpper(hex.EncodeToString(storeInfo.CommitId.Hash)) + + // Create a new StoreInfo with the modified hash. + storeInfos[i] = storetypes.StoreInfo{ + Name: storeInfo.Name, + CommitId: storetypes.CommitID{ + Version: storeInfo.CommitId.Version, + Hash: []byte(hash), + }, + } + } + + // Sort the storeInfos slice based on the module name. + sort.Slice(storeInfos, func(i, j int) bool { + return storeInfos[i].Name < storeInfos[j].Name + }) + + // Create a new CommitInfo with the modified StoreInfos. + commitInfoForHeight = &storetypes.CommitInfo{ + Version: commitInfoForHeight.Version, + StoreInfos: storeInfos, + Timestamp: commitInfoForHeight.Timestamp, + } + + return commitInfoForHeight, nil +} diff --git a/server/util.go b/server/util.go index c23668341d43..5e77127b5a04 100644 --- a/server/util.go +++ b/server/util.go @@ -353,6 +353,7 @@ func AddCommands[T types.Application](rootCmd *cobra.Command, appCreator types.A cometCmd, version.NewVersionCommand(), NewRollbackCmd(appCreator), + ModuleHashByHeightQuery(appCreator), ) }