-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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 #20779
Merged
Merged
Changes from 6 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
0836685
module hash by height query
czarcas7ic a289320
add changelog
czarcas7ic 0f9d159
correct comment
czarcas7ic 91e9438
lint
czarcas7ic cebf849
lint order
czarcas7ic 268af2e
add more detail to changelog entry
czarcas7ic 39ef821
move to server and make default
czarcas7ic c5bc58e
Merge branch 'main' into adam/module-hash-by-height
czarcas7ic 8e2f8bb
output flag to format json
czarcas7ic d9f8467
remove comments
czarcas7ic 49e89be
use appd for comment name
czarcas7ic 0d25c7a
Merge branch 'main' into adam/module-hash-by-height
czarcas7ic 06dd7cd
use cmd.Println
czarcas7ic 8354731
Merge branch 'main' into adam/module-hash-by-height
czarcas7ic 86c0af5
Merge branch 'main' into adam/module-hash-by-height
facundomedica f983727
Merge branch 'main' into adam/module-hash-by-height
czarcas7ic 2b586e1
rename verbose file
czarcas7ic 425db57
use example attribute
czarcas7ic bd42bd8
add more context to error
czarcas7ic 9c01f7c
use existing OpenDB method
czarcas7ic ccb2582
remove osmosisd from error message
czarcas7ic 660aa8d
add timestamp
czarcas7ic 3dc6775
use print proto instead of manually defining prints
czarcas7ic f883653
Merge branch 'main' into adam/module-hash-by-height
tac0turtle 61dd9e3
Merge branch 'main' into adam/module-hash-by-height
czarcas7ic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package cli | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"path/filepath" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
|
||
dbm "github.com/cosmos/cosmos-db" | ||
"github.com/spf13/cobra" | ||
|
||
"cosmossdk.io/store/rootmulti" | ||
storetypes "cosmossdk.io/store/types" | ||
|
||
"github.com/cosmos/cosmos-sdk/server" | ||
servertypes "github.com/cosmos/cosmos-sdk/server/types" | ||
) | ||
|
||
// 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: | ||
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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: allow json There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added json functionality here, I believe it was done to standard but would appreciate an ack 8e2f8bb |
||
|
||
return nil | ||
}, | ||
} | ||
|
||
return cmd | ||
czarcas7ic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func getModuleHashesAtHeight[T servertypes.Application](svrCtx *server.Context, appCreator servertypes.AppCreator[T], 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 | ||
} | ||
czarcas7ic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
func openDB(rootDir string, backendType dbm.BackendType) (dbm.DB, error) { | ||
dataDir := filepath.Join(rootDir, "data") | ||
return dbm.NewDB("application", backendType, dataDir) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where is this command being added?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not add it as I wasn't sure if this was desired as a default and instead have the apps import it themselves, but I can add as a default query for all apps if desired!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved it to server here to have access to appCreator and registered it as a default CLI cmd 39ef821