Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 into hieu/simapp-audit
  • Loading branch information
hieuvubk committed Aug 27, 2024
2 parents 98f70e9 + 819afd2 commit 08ebc82
Show file tree
Hide file tree
Showing 39 changed files with 440 additions and 255 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i
* (client) [#19905](https://github.com/cosmos/cosmos-sdk/pull/19905) Add grpc client config to `client.toml`.
* (genutil) [#19971](https://github.com/cosmos/cosmos-sdk/pull/19971) Allow manually setting the consensus key type in genesis
* (runtime) [#19953](https://github.com/cosmos/cosmos-sdk/pull/19953) Implement `core/transaction.Service` in runtime.
* (runtime) [#18475](https://github.com/cosmos/cosmos-sdk/pull/18475) Adds an implementation for `core.branch.Service`.
* (runtime) [#19004](https://github.com/cosmos/cosmos-sdk/pull/19004) Adds an implementation for `core/header.Service` in runtime.
* (runtime) [#20238](https://github.com/cosmos/cosmos-sdk/pull/20238) Adds an implementation for `core/comet.Service` in runtime.
* (tests) [#20013](https://github.com/cosmos/cosmos-sdk/pull/20013) Introduce system tests to run multi node local testnet in CI
* (crypto/keyring) [#20212](https://github.com/cosmos/cosmos-sdk/pull/20212) Expose the db keyring used in the keystore.
* (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.
Expand Down
20 changes: 18 additions & 2 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,22 @@ need to remove them both from your app.go code, they will yield to unresolvable

The Cosmos SDK now supports unordered transactions. This means that transactions
can be executed in any order and doesn't require the client to deal with or manage
nonces. This also means the order of execution is not guaranteed. To enable unordered
transactions in your application:
nonces. This also means the order of execution is not guaranteed.

Unordered transactions are automatically enabled when using `depinject` / app di, simply supply the `servertypes.AppOptions` in `app.go`:

```diff
depinject.Supply(
+ // supply the application options
+ appOpts,
// supply the logger
logger,
)
```

<details>
<summary>Step-by-step Wiring </summary>
If you are still using the legacy wiring, you must enable unordered transactions manually:

* Update the `App` constructor to create, load, and save the unordered transaction
manager.
Expand Down Expand Up @@ -184,6 +198,8 @@ transactions in your application:
}
```

</details>

To submit an unordered transaction, the client must set the `unordered` flag to
`true` and ensure a reasonable `timeout_height` is set. The `timeout_height` is
used as a TTL for the transaction and is used to provide replay protection. See
Expand Down
2 changes: 0 additions & 2 deletions api/cosmos/base/abci/v1beta1/abci.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions proto/cosmos/base/abci/v1beta1/abci.proto
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ message TxResponse {
// these events include those emitted by processing all the messages and those
// emitted from the ante. Whereas Logs contains the events, with
// additional metadata, emitted only by processing the messages.
//
// Since: cosmos-sdk 0.42.11, 0.44.5, 0.45
repeated cometbft.abci.v1.Event events = 13
[(gogoproto.nullable) = false, (cosmos_proto.field_added_in) = "cosmos-sdk 0.45"];
}
Expand Down
47 changes: 37 additions & 10 deletions runtime/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"cosmossdk.io/core/legacy"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/auth/ante/unorderedtx"
authtx "cosmossdk.io/x/auth/tx"

"github.com/cosmos/cosmos-sdk/baseapp"
Expand Down Expand Up @@ -41,7 +42,9 @@ import (
type App struct {
*baseapp.BaseApp

ModuleManager *module.Manager
ModuleManager *module.Manager
UnorderedTxManager *unorderedtx.Manager

configurator module.Configurator // nolint:staticcheck // SA1019: Configurator is deprecated but still used in runtime v1.
config *runtimev1alpha1.Module
storeKeys []storetypes.StoreKey
Expand Down Expand Up @@ -154,6 +157,20 @@ func (a *App) Load(loadLatest bool) error {
return nil
}

// Close closes all necessary application resources.
// It implements servertypes.Application.
func (a *App) Close() error {
// the unordered tx manager could be nil (unlikely but possible)
// if the app has no app options supplied.
if a.UnorderedTxManager != nil {
if err := a.UnorderedTxManager.Close(); err != nil {
return err
}
}

return a.BaseApp.Close()
}

// PreBlocker application updates every pre block
func (a *App) PreBlocker(ctx sdk.Context, _ *abci.FinalizeBlockRequest) error {
return a.ModuleManager.PreBlock(ctx)
Expand All @@ -171,16 +188,14 @@ func (a *App) EndBlocker(ctx sdk.Context) (sdk.EndBlock, error) {

// Precommiter application updates every commit
func (a *App) Precommiter(ctx sdk.Context) {
err := a.ModuleManager.Precommit(ctx)
if err != nil {
if err := a.ModuleManager.Precommit(ctx); err != nil {
panic(err)
}
}

// PrepareCheckStater application updates every commit
func (a *App) PrepareCheckStater(ctx sdk.Context) {
err := a.ModuleManager.PrepareCheckState(ctx)
if err != nil {
if err := a.ModuleManager.PrepareCheckState(ctx); err != nil {
panic(err)
}
}
Expand Down Expand Up @@ -247,18 +262,30 @@ func (a *App) DefaultGenesis() map[string]json.RawMessage {
return a.ModuleManager.DefaultGenesis()
}

// GetStoreKeys returns all the stored store keys.
func (a *App) GetStoreKeys() []storetypes.StoreKey {
return a.storeKeys
}

// SetInitChainer sets the init chainer function
// It wraps `BaseApp.SetInitChainer` to allow setting a custom init chainer from an app.
func (a *App) SetInitChainer(initChainer sdk.InitChainer) {
a.initChainer = initChainer
a.BaseApp.SetInitChainer(initChainer)
}

// GetStoreKeys returns all the stored store keys.
func (a *App) GetStoreKeys() []storetypes.StoreKey {
return a.storeKeys
}

// GetKey returns the KVStoreKey for the provided store key.
//
// NOTE: This should only be used in testing.
func (a *App) GetKey(storeKey string) *storetypes.KVStoreKey {
sk := a.UnsafeFindStoreKey(storeKey)
kvStoreKey, ok := sk.(*storetypes.KVStoreKey)
if !ok {
return nil
}
return kvStoreKey
}

// UnsafeFindStoreKey fetches a registered StoreKey from the App in linear time.
//
// NOTE: This should only be used in testing.
Expand Down
64 changes: 64 additions & 0 deletions runtime/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ package runtime

import (
"encoding/json"
"fmt"
"io"
"path/filepath"

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

storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/auth/ante/unorderedtx"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client/flags"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/version"
)
Expand All @@ -16,6 +24,8 @@ import (
// the existing app.go initialization conventions.
type AppBuilder struct {
app *App

appOptions servertypes.AppOptions
}

// DefaultGenesis returns a default genesis from the registered modules.
Expand All @@ -40,9 +50,63 @@ func (a *AppBuilder) Build(db dbm.DB, traceStore io.Writer, baseAppOptions ...fu
a.app.BaseApp = bApp
a.app.configurator = module.NewConfigurator(a.app.cdc, a.app.MsgServiceRouter(), a.app.GRPCQueryRouter())

if a.appOptions != nil {
// register unordered tx manager
if err := a.registerUnorderedTxManager(); err != nil {
panic(err)
}

// register indexer if enabled
if err := a.registerIndexer(); err != nil {
panic(err)
}
}

// register services
if err := a.app.ModuleManager.RegisterServices(a.app.configurator); err != nil {
panic(err)
}

return a.app
}

// register unordered tx manager
func (a *AppBuilder) registerUnorderedTxManager() error {
// create, start, and load the unordered tx manager
utxDataDir := filepath.Join(cast.ToString(a.appOptions.Get(flags.FlagHome)), "data")
a.app.UnorderedTxManager = unorderedtx.NewManager(utxDataDir)
a.app.UnorderedTxManager.Start()

if err := a.app.UnorderedTxManager.OnInit(); err != nil {
return fmt.Errorf("failed to initialize unordered tx manager: %w", err)
}

return nil
}

// register indexer
func (a *AppBuilder) registerIndexer() error {
// if we have indexer options in app.toml, then enable the built-in indexer framework
if indexerOpts := a.appOptions.Get("indexer"); indexerOpts != nil {
moduleSet := map[string]any{}
for modName, mod := range a.app.ModuleManager.Modules {
moduleSet[modName] = mod
}

return a.app.EnableIndexer(indexerOpts, a.kvStoreKeys(), moduleSet)
}

// register legacy streaming services if we don't have the built-in indexer enabled
return a.app.RegisterStreamingServices(a.appOptions, a.kvStoreKeys())
}

func (a *AppBuilder) kvStoreKeys() map[string]*storetypes.KVStoreKey {
keys := make(map[string]*storetypes.KVStoreKey)
for _, k := range a.app.GetStoreKeys() {
if kv, ok := k.(*storetypes.KVStoreKey); ok {
keys[kv.Name()] = kv
}
}

return keys
}
6 changes: 3 additions & 3 deletions runtime/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func EnvWithMemStoreService(memStoreService store.MemoryStoreService) EnvOption
}

// failingMsgRouter is a message router that panics when accessed
// this is to ensure all fields are set by in environment
// this is to ensure all fields are set in environment
type failingMsgRouter struct {
baseapp.MessageRouter
}
Expand All @@ -86,7 +86,7 @@ func (failingMsgRouter) HybridHandlerByMsgName(msgName string) func(ctx context.
}

// failingQueryRouter is a query router that panics when accessed
// this is to ensure all fields are set by in environment
// this is to ensure all fields are set in environment
type failingQueryRouter struct {
baseapp.QueryRouter
}
Expand All @@ -112,7 +112,7 @@ func (failingQueryRouter) SetInterfaceRegistry(interfaceRegistry codectypes.Inte
}

// failingMemStore is a memstore that panics when accessed
// this is to ensure all fields are set by in environment
// this is to ensure all fields are set in environment
type failingMemStore struct {
store.MemoryStoreService
}
Expand Down
11 changes: 8 additions & 3 deletions runtime/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/std"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/types/msgservice"
Expand Down Expand Up @@ -120,7 +121,6 @@ func ProvideApp(
appmodule.AppModule,
protodesc.Resolver,
protoregistry.MessageTypeResolver,
error,
) {
protoFiles := proto.HybridResolver
protoTypes := protoregistry.GlobalTypes
Expand All @@ -145,9 +145,9 @@ func ProvideApp(
msgServiceRouter: msgServiceRouter,
grpcQueryRouter: grpcQueryRouter,
}
appBuilder := &AppBuilder{app}
appBuilder := &AppBuilder{app: app}

return appBuilder, msgServiceRouter, grpcQueryRouter, appModule{app}, protoFiles, protoTypes, nil
return appBuilder, msgServiceRouter, grpcQueryRouter, appModule{app}, protoFiles, protoTypes
}

type AppInputs struct {
Expand All @@ -160,6 +160,7 @@ type AppInputs struct {
BaseAppOptions []BaseAppOption
InterfaceRegistry codectypes.InterfaceRegistry
LegacyAmino legacy.Amino
AppOptions servertypes.AppOptions `optional:"true"` // can be nil in client wiring
}

func SetupAppBuilder(inputs AppInputs) {
Expand All @@ -170,6 +171,10 @@ func SetupAppBuilder(inputs AppInputs) {
app.ModuleManager = inputs.ModuleManager
app.ModuleManager.RegisterInterfaces(inputs.InterfaceRegistry)
app.ModuleManager.RegisterLegacyAminoCodec(inputs.LegacyAmino)

if inputs.AppOptions != nil {
inputs.AppBuilder.appOptions = inputs.AppOptions
}
}

func registerStoreKey(wrapper *AppBuilder, key storetypes.StoreKey) {
Expand Down
4 changes: 2 additions & 2 deletions runtime/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/cosmos/cosmos-sdk/baseapp"
)

// NewMsgRouterService implements router.Service.
// NewMsgRouterService return new implementation of router.Service.
func NewMsgRouterService(msgRouter baseapp.MessageRouter) router.Service {
return &msgRouterService{
router: msgRouter,
Expand Down Expand Up @@ -75,7 +75,7 @@ func (m *msgRouterService) Invoke(ctx context.Context, msg gogoproto.Message) (g
return msgResp, nil
}

// NewQueryRouterService implements router.Service.
// NewQueryRouterService return new implementation of router.Service.
func NewQueryRouterService(queryRouter baseapp.QueryRouter) router.Service {
return &queryRouterService{
router: queryRouter,
Expand Down
10 changes: 4 additions & 6 deletions runtime/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,34 +63,32 @@ func (failingStoreService) OpenTransientStore(ctx context.Context) store.KVStore
}

// CoreKVStore is a wrapper of Core/Store kvstore interface
// Remove after https://github.com/cosmos/cosmos-sdk/issues/14714 is closed
type coreKVStore struct {
kvStore storetypes.KVStore
}

// NewKVStore returns a wrapper of Core/Store kvstore interface
// Remove once store migrates to core/store kvstore interface
func newKVStore(store storetypes.KVStore) store.KVStore {
return coreKVStore{kvStore: store}
}

// Get returns nil iff key doesn't exist. Errors on nil key.
// Get returns value corresponding to the key. Panics on nil key.
func (store coreKVStore) Get(key []byte) ([]byte, error) {
return store.kvStore.Get(key), nil
}

// Has checks if a key exists. Errors on nil key.
// Has checks if a key exists. Panics on nil key.
func (store coreKVStore) Has(key []byte) (bool, error) {
return store.kvStore.Has(key), nil
}

// Set sets the key. Errors on nil key or value.
// Set sets the key. Panics on nil key or value.
func (store coreKVStore) Set(key, value []byte) error {
store.kvStore.Set(key, value)
return nil
}

// Delete deletes the key. Errors on nil key.
// Delete deletes the key. Panics on nil key.
func (store coreKVStore) Delete(key []byte) error {
store.kvStore.Delete(key)
return nil
Expand Down
5 changes: 0 additions & 5 deletions runtime/v2/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,6 @@ func (a *App[T]) GetStore() Store {
return a.db
}

// GetLogger returns the app logger.
func (a *App[T]) GetLogger() log.Logger {
return a.logger
}

func (a *App[T]) GetAppManager() *appmanager.AppManager[T] {
return a.AppManager
}
Expand Down
Loading

0 comments on commit 08ebc82

Please sign in to comment.