Skip to content

Commit

Permalink
Merge branch 'facu/fix-unordered-part2' of https://github.com/cosmos/…
Browse files Browse the repository at this point in the history
…cosmos-sdk into facu/fix-unordered-part2
  • Loading branch information
facundomedica committed Jan 24, 2025
2 parents 2f4c25d + 9b61d0d commit 5104784
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 55 deletions.
2 changes: 1 addition & 1 deletion runtime/v2/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (a *AppBuilder[T]) Build(opts ...AppBuilderOption[T]) (*App[T], error) {
}
a.app.stf = stf

a.app.AppManager = appmanager.New[T](
a.app.AppManager = appmanager.New(
appmanager.Config{
ValidateTxGasLimit: a.app.config.GasConfig.ValidateTxGasLimit,
QueryGasLimit: a.app.config.GasConfig.QueryGasLimit,
Expand Down
2 changes: 1 addition & 1 deletion server/v2/appmanager/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package appmanager

// Config represents the configuration options for the app manager.
type Config struct {
ValidateTxGasLimit uint64 `mapstructure:"validate-tx-gas-limit"` // TODO: check how this works on app mempool
ValidateTxGasLimit uint64 `mapstructure:"validate-tx-gas-limit"`
QueryGasLimit uint64 `mapstructure:"query-gas-limit"`
SimulationGasLimit uint64 `mapstructure:"simulation-gas-limit"`
}
10 changes: 5 additions & 5 deletions server/v2/cometbft/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,6 @@ func (c *consensus[T]) Info(ctx context.Context, _ *abciproto.InfoRequest) (*abc
// Query implements types.Application.
// It is called by cometbft to query application state.
func (c *consensus[T]) Query(ctx context.Context, req *abciproto.QueryRequest) (resp *abciproto.QueryResponse, err error) {
resp, isGRPC, err := c.maybeRunGRPCQuery(ctx, req)
if isGRPC {
return resp, err
}

// when a client did not provide a query height, manually inject the latest
// for modules queries, AppManager does it automatically
if req.Height == 0 {
Expand All @@ -189,6 +184,11 @@ func (c *consensus[T]) Query(ctx context.Context, req *abciproto.QueryRequest) (
req.Height = int64(latestVersion)
}

resp, isGRPC, err := c.maybeRunGRPCQuery(ctx, req)
if isGRPC {
return resp, err
}

// this error most probably means that we can't handle it with a proto message, so
// it must be an app/p2p/store query
path := splitABCIQueryPath(req.Path)
Expand Down
90 changes: 90 additions & 0 deletions server/v2/store/commands.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package store

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

"github.com/spf13/cobra"
"github.com/spf13/viper"

"cosmossdk.io/log"
serverv2 "cosmossdk.io/server/v2"
storev2 "cosmossdk.io/store/v2"
"cosmossdk.io/store/v2/proof"
"cosmossdk.io/store/v2/root"
)

Expand Down Expand Up @@ -74,6 +80,90 @@ Supported app-db-backend types include 'goleveldb', 'pebbledb'.`,
return cmd
}

// ModuleHashByHeightQuery retrieves the module hashes at a given height.
func (s *Server[T]) ModuleHashByHeightQuery() *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: "<appd module-hash-by-height 16841115",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
vp := serverv2.GetViperFromCmd(cmd)
if err := vp.BindPFlags(cmd.Flags()); err != nil {
return err
}
if err := vp.BindPFlags(cmd.PersistentFlags()); err != nil {
return err
}

heightToRetrieveString := args[0]
height, err := strconv.ParseInt(heightToRetrieveString, 10, 64)
if err != nil {
return fmt.Errorf("invalid height: %w", err)
}

commitInfoForHeight, err := getModuleHashesAtHeight(vp, serverv2.GetLoggerFromCmd(cmd), uint64(height))
if err != nil {
return err
}

bytes, err := json.Marshal(commitInfoForHeight)
if err != nil {
return fmt.Errorf("failed to marshal commit info: %w", err)
}

cmd.Println(string(bytes))
return nil
},
}

return cmd
}

func getModuleHashesAtHeight(vp *viper.Viper, logger log.Logger, height uint64) (*proof.CommitInfo, error) {
rootStore, _, err := createRootStore(vp, logger)
if err != nil {
return nil, fmt.Errorf("can not create root store %w", err)
}

commitInfoForHeight, err := rootStore.GetStateCommitment().GetCommitInfo(height)
if err != nil {
return nil, err
}

// Create a new slice of StoreInfos for storing the modified hashes.
storeInfos := make([]*proof.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] = &proof.StoreInfo{
Name: storeInfo.Name,
CommitId: &proof.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 = &proof.CommitInfo{
Version: commitInfoForHeight.Version,
StoreInfos: storeInfos,
Timestamp: commitInfoForHeight.Timestamp,
}

return commitInfoForHeight, nil
}

func createRootStore(v *viper.Viper, logger log.Logger) (storev2.RootStore, root.Options, error) {
storeConfig, err := UnmarshalConfig(v.AllSettings())
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions server/v2/store/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (s *Server[T]) CLICommands() serverv2.CLIConfig {
s.DumpArchiveCmd(),
s.LoadArchiveCmd(),
s.RestoreSnapshotCmd(),
s.ModuleHashByHeightQuery(),
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion server/v2/store/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (s *Server[T]) ExportSnapshotCmd() *cobra.Command {
if err != nil {
return err
}
height = int64(lastCommitId.Version)
height = lastCommitId.Version
}

cmd.Printf("Exporting snapshot for height %d\n", height)
Expand Down
9 changes: 5 additions & 4 deletions simapp/v2/app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
validatemodulev1 "cosmossdk.io/api/cosmos/validate/module/v1"
vestingmodulev1 "cosmossdk.io/api/cosmos/vesting/module/v1"
"cosmossdk.io/depinject/appconfig"
runtimev2types "cosmossdk.io/runtime/v2"
"cosmossdk.io/x/accounts"
"cosmossdk.io/x/authz"
_ "cosmossdk.io/x/authz/module" // import for side-effects
Expand Down Expand Up @@ -66,7 +67,6 @@ import (
_ "cosmossdk.io/x/upgrade" // import for side-effects
upgradetypes "cosmossdk.io/x/upgrade/types"

"github.com/cosmos/cosmos-sdk/runtime"
_ "github.com/cosmos/cosmos-sdk/x/auth" // import for side-effects
authtxconfig "github.com/cosmos/cosmos-sdk/x/auth/tx/config"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down Expand Up @@ -108,7 +108,7 @@ var (
ModuleConfig = &appv1alpha1.Config{
Modules: []*appv1alpha1.ModuleConfig{
{
Name: runtime.ModuleName,
Name: runtimev2types.ModuleName,
Config: appconfig.WrapAny(&runtimev2.Module{
AppName: "SimAppV2",
// NOTE: upgrade module is required to be prioritized
Expand Down Expand Up @@ -197,7 +197,8 @@ var (
},
// Uncomment if you want to set a custom migration order here.
// OrderMigrations: []string{},
// TODO GasConfig was added to the config in runtimev2. Where/how was it set in v1?
// GasConfig is used to set the gas configuration for the queries and transactions.
// This config is aimed to app-wide and shouldn't be overridden by individual validators.
GasConfig: &runtimev2.GasConfig{
ValidateTxGasLimit: 10_000_000,
QueryGasLimit: 100_000,
Expand All @@ -209,7 +210,7 @@ var (
authtxconfig.DepinjectModuleName,
validate.ModuleName,
genutiltypes.ModuleName,
runtime.ModuleName,
runtimev2types.ModuleName,
vestingtypes.ModuleName,
},
}),
Expand Down
64 changes: 27 additions & 37 deletions simapp/v2/simdv2/cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ type CommandDependencies[T transaction.Tx] struct {
TxConfig client.TxConfig
ModuleManager *runtimev2.MM[T]
SimApp *simapp.SimApp[T]
// could generally be more generic with serverv2.ServerComponent[T]
// however, we want to register extra grpc handlers
ConsensusServer *cometbft.CometBFTServer[T]
ClientContext client.Context
ClientContext client.Context
}

func InitRootCmd[T transaction.Tx](
Expand All @@ -76,16 +73,13 @@ func InitRootCmd[T transaction.Tx](

// build CLI skeleton for initial config parsing or a client application invocation
if deps.SimApp == nil {
if deps.ConsensusServer == nil {
deps.ConsensusServer = cometbft.NewWithConfigOptions[T](initCometConfig())
}
return serverv2.AddCommands[T](
rootCmd,
logger,
io.NopCloser(nil),
deps.GlobalConfig,
initServerConfig(),
deps.ConsensusServer,
cometbft.NewWithConfigOptions[T](initCometConfig()),
&grpcserver.Server[T]{},
&serverstore.Server[T]{},
&telemetry.Server[T]{},
Expand All @@ -108,26 +102,24 @@ func InitRootCmd[T transaction.Tx](
}

// consensus component
if deps.ConsensusServer == nil {
deps.ConsensusServer, err = cometbft.New(
logger,
simApp.Name(),
simApp.Store(),
simApp.App.AppManager,
cometbft.AppCodecs[T]{
AppCodec: simApp.AppCodec(),
TxCodec: &client.DefaultTxDecoder[T]{TxConfig: deps.TxConfig},
LegacyAmino: deps.ClientContext.LegacyAmino,
ConsensusAddressCodec: deps.ClientContext.ConsensusAddressCodec,
},
simApp.App.QueryHandlers(),
simApp.App.SchemaDecoderResolver(),
initCometOptions[T](),
deps.GlobalConfig,
)
if err != nil {
return nil, err
}
consensusServer, err := cometbft.New(
logger,
simApp.Name(),
simApp.Store(),
simApp.App.AppManager,
cometbft.AppCodecs[T]{
AppCodec: simApp.AppCodec(),
TxCodec: &client.DefaultTxDecoder[T]{TxConfig: deps.TxConfig},
LegacyAmino: deps.ClientContext.LegacyAmino,
ConsensusAddressCodec: deps.ClientContext.ConsensusAddressCodec,
},
simApp.App.QueryHandlers(),
simApp.App.SchemaDecoderResolver(),
initCometOptions[T](),
deps.GlobalConfig,
)
if err != nil {
return nil, err
}

telemetryServer, err := telemetry.New[T](deps.GlobalConfig, logger, sdktelemetry.EnableTelemetry)
Expand All @@ -142,7 +134,7 @@ func InitRootCmd[T transaction.Tx](
simApp.Query,
deps.GlobalConfig,
grpcserver.WithExtraGRPCHandlers[T](
deps.ConsensusServer.GRPCServiceRegistrar(
consensusServer.GRPCServiceRegistrar(
deps.ClientContext,
deps.GlobalConfig,
),
Expand All @@ -161,7 +153,7 @@ func InitRootCmd[T transaction.Tx](
if err != nil {
return nil, err
}
registerGRPCGatewayRoutes[T](deps, grpcgatewayServer)
registerGRPCGatewayRoutes(deps.ClientContext, grpcgatewayServer)

// wire server commands
return serverv2.AddCommands[T](
Expand All @@ -170,7 +162,7 @@ func InitRootCmd[T transaction.Tx](
simApp,
deps.GlobalConfig,
initServerConfig(),
deps.ConsensusServer,
consensusServer,
grpcServer,
storeComponent,
telemetryServer,
Expand Down Expand Up @@ -268,14 +260,12 @@ func RootCommandPersistentPreRun(clientCtx client.Context) func(*cobra.Command,
}

// registerGRPCGatewayRoutes registers the gRPC gateway routes for all modules and other components
// TODO(@julienrbrt): Eventually, this should removed and directly done within the grpcgateway.Server
// ref: https://github.com/cosmos/cosmos-sdk/pull/22701#pullrequestreview-2470651390
func registerGRPCGatewayRoutes[T transaction.Tx](
deps CommandDependencies[T],
clientContext client.Context,
server *grpcgateway.Server[T],
) {
// those are the extra services that the CometBFT server implements (server/v2/cometbft/grpc.go)
cmtservice.RegisterGRPCGatewayRoutes(deps.ClientContext, server.GRPCGatewayRouter)
_ = nodeservice.RegisterServiceHandlerClient(context.Background(), server.GRPCGatewayRouter, nodeservice.NewServiceClient(deps.ClientContext))
_ = txtypes.RegisterServiceHandlerClient(context.Background(), server.GRPCGatewayRouter, txtypes.NewServiceClient(deps.ClientContext))
cmtservice.RegisterGRPCGatewayRoutes(clientContext, server.GRPCGatewayRouter)
_ = nodeservice.RegisterServiceHandlerClient(context.Background(), server.GRPCGatewayRouter, nodeservice.NewServiceClient(clientContext))
_ = txtypes.RegisterServiceHandlerClient(context.Background(), server.GRPCGatewayRouter, txtypes.NewServiceClient(clientContext))
}
3 changes: 1 addition & 2 deletions x/group/internal/orm/auto_uint64.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"github.com/cosmos/gogoproto/proto"

"cosmossdk.io/core/address"
"cosmossdk.io/core/codec"
storetypes "cosmossdk.io/core/store"
"cosmossdk.io/errors"

"github.com/cosmos/cosmos-sdk/codec"
)

var (
Expand Down
3 changes: 2 additions & 1 deletion x/group/internal/orm/example_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package orm

import (
"github.com/cosmos/cosmos-sdk/codec"
"cosmossdk.io/core/codec"

"github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
)
Expand Down
3 changes: 1 addition & 2 deletions x/group/internal/orm/primary_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"github.com/cosmos/gogoproto/proto"

"cosmossdk.io/core/address"
"cosmossdk.io/core/codec"
storetypes "cosmossdk.io/core/store"

"github.com/cosmos/cosmos-sdk/codec"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion x/group/internal/orm/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"github.com/cosmos/gogoproto/proto"

"cosmossdk.io/core/address"
"cosmossdk.io/core/codec"
storetypes "cosmossdk.io/core/store"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/store/types"
"cosmossdk.io/x/group/errors"
"cosmossdk.io/x/group/internal/orm/prefixstore"

"github.com/cosmos/cosmos-sdk/codec"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

Expand Down

0 comments on commit 5104784

Please sign in to comment.