Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: module hash by height query #8427

Merged
merged 4 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions cmd/osmosisd/cmd/module_hash_by_height.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package cmd

// DONTCOVER

import (
"encoding/hex"
"fmt"
"sort"
"strconv"
"strings"

"github.com/spf13/cobra"

"path/filepath"

dbm "github.com/cosmos/cosmos-db"

"github.com/cosmos/cosmos-sdk/server"
servertypes "github.com/cosmos/cosmos-sdk/server/types"

"cosmossdk.io/store/rootmulti"
storetypes "cosmossdk.io/store/types"
)

// ModuleHashByHeightQuery retrieves the module hashes at a given height.
func moduleHashByHeightQuery(appCreator servertypes.AppCreator) *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.
Example:
osmosisd module-hash-by-height 16841115,
`,
Args: cobra.ExactArgs(1), // Ensure exactly one argument is provided
RunE: func(cmd *cobra.Command, args []string) error {
heightToRetrieveString := args[0]

serverCtx := server.GetServerContextFromCmd(cmd)

height, err := strconv.ParseInt(heightToRetrieveString, 10, 64)
if err != nil {
return err
}

commitInfoForHeight, err := getModuleHashesAtHeight(serverCtx, appCreator, height)
if err != nil {
return err
}

// Print the CommitInfo to the console.
fmt.Println(commitInfoForHeight.String())

return nil
},
czarcas7ic marked this conversation as resolved.
Show resolved Hide resolved
}

return cmd
}

func getModuleHashesAtHeight(svrCtx *server.Context, appCreator servertypes.AppCreator, height int64) (*storetypes.CommitInfo, error) {
home := svrCtx.Config.RootDir
db, err := openDB(home, server.GetAppDBBackend(svrCtx.Viper))
if err != nil {
return nil, fmt.Errorf("error opening DB, make sure osmosisd 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,
}

return commitInfoForHeight, nil
}

func openDB(rootDir string, backendType dbm.BackendType) (dbm.DB, error) {
dataDir := filepath.Join(rootDir, "data")
return dbm.NewDB("application", backendType, dataDir)
}
1 change: 1 addition & 0 deletions cmd/osmosisd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig, t
rootCmd.AddCommand(
// genutilcli.InitCmd(tempApp.ModuleBasics, osmosis.DefaultNodeHome),
forceprune(),
moduleHashByHeightQuery(newApp),
InitCmd(tempApp.ModuleBasics, osmosis.DefaultNodeHome),
genutilcli.CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, osmosis.DefaultNodeHome, genutiltypes.DefaultMessageValidator, valOperAddressCodec),
ExportDeriveBalancesCmd(),
Expand Down