Skip to content

Commit

Permalink
feat: module hash by height query (#20779)
Browse files Browse the repository at this point in the history
Co-authored-by: Facundo Medica <[email protected]>
Co-authored-by: Marko <[email protected]>
(cherry picked from commit 4e97c49)

# Conflicts:
#	CHANGELOG.md
#	server/util.go
  • Loading branch information
czarcas7ic authored and mergify[bot] committed Aug 10, 2024
1 parent 057cd3b commit be2310a
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 0 deletions.
69 changes: 69 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,76 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [v0.50.9](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.50.9) - 2024-08-07

<<<<<<< HEAD
## Bug Fixes
=======
### Features

* (baseapp) [#20291](https://github.com/cosmos/cosmos-sdk/pull/20291) Simulate nested messages.
* (tests) [#20013](https://github.com/cosmos/cosmos-sdk/pull/20013) Introduce system tests to run multi node local testnet in CI
* (runtime) [#19953](https://github.com/cosmos/cosmos-sdk/pull/19953) Implement `core/transaction.Service` in runtime.
* (client) [#19905](https://github.com/cosmos/cosmos-sdk/pull/19905) Add grpc client config to `client.toml`.
* (runtime) [#19571](https://github.com/cosmos/cosmos-sdk/pull/19571) Implement `core/router.Service` in runtime. This service is present in all modules (when using depinject).
* (types) [#19164](https://github.com/cosmos/cosmos-sdk/pull/19164) Add a ValueCodec for the math.Uint type that can be used in collections maps.
* (types) [#19281](https://github.com/cosmos/cosmos-sdk/pull/19281) Added a new method, `IsGT`, for `types.Coin`. This method is used to check if a `types.Coin` is greater than another `types.Coin`.
* (client) [#18557](https://github.com/cosmos/cosmos-sdk/pull/18557) Add `--qrcode` flag to `keys show` command to support displaying keys address QR code.
* (client) [#18101](https://github.com/cosmos/cosmos-sdk/pull/18101) Add a `keyring-default-keyname` in `client.toml` for specifying a default key name, and skip the need to use the `--from` flag when signing transactions.
* (tests) [#17868](https://github.com/cosmos/cosmos-sdk/pull/17868) Added helper method `SubmitTestTx` in testutil to broadcast test txns to test e2e tests.
* (client) [#17513](https://github.com/cosmos/cosmos-sdk/pull/17513) Allow overwriting `client.toml`. Use `client.CreateClientConfig` in place of `client.ReadFromClientConfig` and provide a custom template and a custom config.
* (runtime) [#18475](https://github.com/cosmos/cosmos-sdk/pull/18475) Adds an implementation for core.branch.Service.
* (baseapp) [#18499](https://github.com/cosmos/cosmos-sdk/pull/18499) Add `MsgRouter` response type from message name function.
* (types) [#18768](https://github.com/cosmos/cosmos-sdk/pull/18768) Add MustValAddressFromBech32 function.
* (gRPC) [#19049](https://github.com/cosmos/cosmos-sdk/pull/19049) Add debug log prints for each gRPC request.
* (types) [#19759](https://github.com/cosmos/cosmos-sdk/pull/19759) Align SignerExtractionAdapter in PriorityNonceMempool Remove.
* (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

* (codec) [#20122](https://github.com/cosmos/cosmos-sdk/pull/20122) Added a cache to address codec.
* (types) [#19869](https://github.com/cosmos/cosmos-sdk/pull/19869) Removed `Any` type from `codec/types` and replaced it with an alias for `cosmos/gogoproto/types/any`.
* (server) [#19854](https://github.com/cosmos/cosmos-sdk/pull/19854) Add customizability to start command.
* Add `StartCmdOptions` in `server.AddCommands` instead of `servertypes.ModuleInitFlags`. To set custom flags set them in the `StartCmdOptions` struct on the `AddFlags` field.
* Add `StartCommandHandler` to `StartCmdOptions` to allow custom start command handlers. Users now have total control over how the app starts.
* (types) [#19672](https://github.com/cosmos/cosmos-sdk/pull/19672) `PreBlock` now returns only an error for consistency with server/v2. The SDK has upgraded x/upgrade accordingly. `ResponsePreBlock` hence has been removed.
* (server) [#19455](https://github.com/cosmos/cosmos-sdk/pull/19455) Allow calling back into the application struct in PostSetup.
* (types) [#19512](https://github.com/cosmos/cosmos-sdk/pull/19512) The notion of basic manager does not exist anymore (and all related helpers).
* The module manager now can do everything that the basic manager was doing.
* `AppModuleBasic` has been deprecated for extension interfaces.
* Modules can now implement `appmodule.HasRegisterInterfaces`, `module.HasGRPCGateway` and `module.HasAminoCodec` when relevant.
* SDK modules now directly implement those extension interfaces on `AppModule` instead of `AppModuleBasic`.
* (client/keys) [#18950](https://github.com/cosmos/cosmos-sdk/pull/18950) Improve `<appd> keys add`, `<appd> keys import` and `<appd> keys rename` by checking name validation.
* (client/keys) [#18745](https://github.com/cosmos/cosmos-sdk/pull/18745) Improve `<appd> keys export` and `<appd> keys mnemonic` by adding --yes option to skip interactive confirmation.
* (client/keys) [#18743](https://github.com/cosmos/cosmos-sdk/pull/18743) Improve `<appd> keys add -i` by hiding inputting of bip39 passphrase.
* (client/keys) [#18703](https://github.com/cosmos/cosmos-sdk/pull/18703) Improve `<appd> keys add` and `<appd> keys show` by checking whether there are duplicate keys in the multisig case.
* Usage of `Must...` kind of functions are avoided in keeper methods.
* (client/keys) [#18687](https://github.com/cosmos/cosmos-sdk/pull/18687) Improve `<appd> keys mnemonic` by displaying mnemonic discreetly on an alternate screen and adding `--indiscreet` option to disable it.
* (client/keys) [#18684](https://github.com/cosmos/cosmos-sdk/pull/18684) Improve `<appd> keys export` by displaying unarmored hex private key discreetly on an alternate screen and adding `--indiscreet` option to disable it.
* (client/keys) [#18663](https://github.com/cosmos/cosmos-sdk/pull/18663) Improve `<appd> keys add` by displaying mnemonic discreetly on an alternate screen and adding `--indiscreet` option to disable it.
* (types) [#18440](https://github.com/cosmos/cosmos-sdk/pull/18440) Add `AmountOfNoValidation` to `sdk.DecCoins`.
* (client) [#17503](https://github.com/cosmos/cosmos-sdk/pull/17503) Add `client.Context{}.WithAddressCodec`, `WithValidatorAddressCodec`, `WithConsensusAddressCodec` to provide address codecs to the client context. See the [UPGRADING.md](./UPGRADING.md) for more details.
* (crypto/keyring) [#17503](https://github.com/cosmos/cosmos-sdk/pull/17503) Simplify keyring interfaces to use `[]byte` instead of `sdk.Address` for addresses.
* (all) [#16537](https://github.com/cosmos/cosmos-sdk/pull/16537) Properly propagated `fmt.Errorf` errors and using `errors.New` where appropriate.
* (rpc) [#17470](https://github.com/cosmos/cosmos-sdk/pull/17470) Avoid open 0.0.0.0 to public by default and add `listen-ip-address` argument for `testnet init-files` cmd.
* (types) [#17670](https://github.com/cosmos/cosmos-sdk/pull/17670) Use `ctx.CometInfo` in place of `ctx.VoteInfos`
* [#17733](https://github.com/cosmos/cosmos-sdk/pull/17733) Ensure `buf export` exports all proto dependencies
* (crypto/keys) [#18026](https://github.com/cosmos/cosmos-sdk/pull/18026) Made public key generation constant time on `secp256k1`
* (crypto | x/auth) [#14372](https://github.com/cosmos/cosmos-sdk/pull/18194) Key checks on signatures antehandle.
* (types) [#18963](https://github.com/cosmos/cosmos-sdk/pull/18963) Swap out amino json encoding of `ABCIMessageLogs` for std lib json encoding
* (x/auth) [#19651](https://github.com/cosmos/cosmos-sdk/pull/19651) Allow empty public keys in `GetSignBytesAdapter`.
* (x/genutil) [#19735](https://github.com/cosmos/cosmos-sdk/pull/19735) Update genesis api to match new `appmodule.HasGenesis` interface.
* (server) [#19966](https://github.com/cosmos/cosmos-sdk/pull/19966) Return BlockHeader by shallow copy in server Context.
* (proto) [#20098](https://github.com/cosmos/cosmos-sdk/pull/20098) Use cosmos_proto added_in annotation instead of // Since comments.
* (baseapp) [#20208](https://github.com/cosmos/cosmos-sdk/pull/20208) Skip running validateBasic for rechecking txs.
* (baseapp) [#20380](https://github.com/cosmos/cosmos-sdk/pull/20380) Enhanced OfferSnapshot documentation.
* (client) [#20771](https://github.com/cosmos/cosmos-sdk/pull/20771) Remove `ReadDefaultValuesFromDefaultClientConfig` from `client` package. (It was introduced in `v0.50.6` as a quick fix).
* (grpcserver) [#20945](https://github.com/cosmos/cosmos-sdk/pull/20945) Adds error handling for out-of-gas panics in grpc query handlers.

### Bug Fixes
>>>>>>> 4e97c4951 (feat: module hash by height query (#20779))
* (baseapp) [#21159](https://github.com/cosmos/cosmos-sdk/pull/21159) Return PreBlocker events in FinalizeBlockResponse.
* [#20939](https://github.com/cosmos/cosmos-sdk/pull/20939) Fix collection reverse iterator to include `pagination.key` in the result.
Expand Down
101 changes: 101 additions & 0 deletions server/module_hash.go
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 5 additions & 0 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,12 @@ func AddCommands(rootCmd *cobra.Command, defaultNodeHome string, appCreator type
cometCmd,
ExportCmd(appExport, defaultNodeHome),
version.NewVersionCommand(),
<<<<<<< HEAD

Check failure on line 340 in server/util.go

View workflow job for this annotation

GitHub Actions / dependency-review

expected operand, found '<<'

Check failure on line 340 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (02)

syntax error: unexpected <<, expected expression

Check failure on line 340 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (02)

syntax error: unexpected <<, expected expression

Check failure on line 340 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (03)

syntax error: unexpected <<, expected expression

Check failure on line 340 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (03)

syntax error: unexpected <<, expected expression

Check failure on line 340 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (01)

syntax error: unexpected <<, expected expression

Check failure on line 340 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (01)

syntax error: unexpected <<, expected expression

Check failure on line 340 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (00)

syntax error: unexpected <<, expected expression

Check failure on line 340 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (00)

syntax error: unexpected <<, expected expression
NewRollbackCmd(appCreator, defaultNodeHome),
=======

Check failure on line 342 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (02)

syntax error: unexpected ==, expected expression

Check failure on line 342 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (02)

syntax error: unexpected ==, expected expression

Check failure on line 342 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (03)

syntax error: unexpected ==, expected expression

Check failure on line 342 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (03)

syntax error: unexpected ==, expected expression

Check failure on line 342 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (01)

syntax error: unexpected ==, expected expression

Check failure on line 342 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (01)

syntax error: unexpected ==, expected expression

Check failure on line 342 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (00)

syntax error: unexpected ==, expected expression

Check failure on line 342 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (00)

syntax error: unexpected ==, expected expression
NewRollbackCmd(appCreator),

Check failure on line 343 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (02)

syntax error: unexpected ), expected := or = or comma

Check failure on line 343 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (02)

syntax error: unexpected ), expected := or = or comma

Check failure on line 343 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (03)

syntax error: unexpected ), expected := or = or comma

Check failure on line 343 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (03)

syntax error: unexpected ), expected := or = or comma

Check failure on line 343 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (01)

syntax error: unexpected ), expected := or = or comma

Check failure on line 343 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (01)

syntax error: unexpected ), expected := or = or comma

Check failure on line 343 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (00)

syntax error: unexpected ), expected := or = or comma

Check failure on line 343 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (00)

syntax error: unexpected ), expected := or = or comma
ModuleHashByHeightQuery(appCreator),
>>>>>>> 4e97c4951 (feat: module hash by height query (#20779))

Check failure on line 345 in server/util.go

View workflow job for this annotation

GitHub Actions / dependency-review

illegal character U+0023 '#'

Check failure on line 345 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (02)

invalid character U+0023 '#'

Check failure on line 345 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (02)

invalid character U+0023 '#'

Check failure on line 345 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (03)

invalid character U+0023 '#'

Check failure on line 345 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (03)

invalid character U+0023 '#'

Check failure on line 345 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (01)

invalid character U+0023 '#'

Check failure on line 345 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (01)

invalid character U+0023 '#'

Check failure on line 345 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (00)

invalid character U+0023 '#'

Check failure on line 345 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (00)

invalid character U+0023 '#'
)

Check failure on line 346 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (02)

syntax error: unexpected ), expected }

Check failure on line 346 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (03)

syntax error: unexpected ), expected }

Check failure on line 346 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (01)

syntax error: unexpected ), expected }

Check failure on line 346 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (00)

syntax error: unexpected ), expected }
}

Check failure on line 347 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (02)

syntax error: non-declaration statement outside function body

Check failure on line 347 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (03)

syntax error: non-declaration statement outside function body

Check failure on line 347 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (01)

syntax error: non-declaration statement outside function body

Check failure on line 347 in server/util.go

View workflow job for this annotation

GitHub Actions / tests (00)

syntax error: non-declaration statement outside function body

Expand Down

0 comments on commit be2310a

Please sign in to comment.