Some checks failed
Build SimApp / build (amd64) (push) Waiting to run
Build SimApp / build (arm64) (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
Build & Push / build (push) Waiting to run
Run Gosec / Gosec (push) Waiting to run
Lint / golangci-lint (push) Waiting to run
Checks dependencies and mocks generation / Check go mod tidy (push) Waiting to run
Checks dependencies and mocks generation / Check up to date mocks (push) Waiting to run
System Tests / setup (push) Waiting to run
System Tests / test-system (push) Blocked by required conditions
System Tests / test-system-legacy (push) Blocked by required conditions
Tests / Code Coverage / split-test-files (push) Waiting to run
Tests / Code Coverage / tests (00) (push) Blocked by required conditions
Tests / Code Coverage / tests (01) (push) Blocked by required conditions
Tests / Code Coverage / tests (02) (push) Blocked by required conditions
Tests / Code Coverage / tests (03) (push) Blocked by required conditions
Tests / Code Coverage / test-integration (push) Waiting to run
Tests / Code Coverage / test-e2e (push) Waiting to run
Tests / Code Coverage / repo-analysis (push) Blocked by required conditions
Tests / Code Coverage / test-sim-nondeterminism (push) Waiting to run
Tests / Code Coverage / test-clientv2 (push) Waiting to run
Tests / Code Coverage / test-core (push) Waiting to run
Tests / Code Coverage / test-depinject (push) Waiting to run
Tests / Code Coverage / test-errors (push) Waiting to run
Tests / Code Coverage / test-math (push) Waiting to run
Tests / Code Coverage / test-schema (push) Waiting to run
Tests / Code Coverage / test-collections (push) Waiting to run
Tests / Code Coverage / test-cosmovisor (push) Waiting to run
Tests / Code Coverage / test-confix (push) Waiting to run
Tests / Code Coverage / test-store (push) Waiting to run
Tests / Code Coverage / test-log (push) Waiting to run
Tests / Code Coverage / test-x-tx (push) Waiting to run
Tests / Code Coverage / test-x-nft (push) Waiting to run
Tests / Code Coverage / test-x-circuit (push) Waiting to run
Tests / Code Coverage / test-x-feegrant (push) Waiting to run
Tests / Code Coverage / test-x-evidence (push) Waiting to run
Tests / Code Coverage / test-x-upgrade (push) Waiting to run
Tests / Code Coverage / test-tools-benchmark (push) Waiting to run
Build & Push SDK Proto Builder / build (push) Has been cancelled
101 lines
3 KiB
Go
101 lines
3 KiB
Go
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"
|
|
"github.com/cosmos/cosmos-sdk/server/types"
|
|
"github.com/cosmos/cosmos-sdk/version"
|
|
)
|
|
|
|
// ModuleHashByHeightQuery retrieves the module hashes at a given height.
|
|
func ModuleHashByHeightQuery(appCreator types.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. 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(svrCtx *Context, appCreator types.AppCreator, 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
|
|
}
|