Skip to content

Commit

Permalink
feat!: consensus module (cosmos#12905)
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle authored and Wryhder committed Oct 26, 2022
1 parent 8fd7a49 commit 18c0d0f
Show file tree
Hide file tree
Showing 54 changed files with 541 additions and 308 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/bank) [#11981](https://github.com/cosmos/cosmos-sdk/pull/11981) Create the `SetSendEnabled` endpoint for managing the bank's SendEnabled settings.
* (x/auth) [#13210](https://github.com/cosmos/cosmos-sdk/pull/13210) Add `Query/AccountInfo` endpoint for simplified access to basic account info.
* (cli) [#13147](https://github.com/cosmos/cosmos-sdk/pull/13147) Add the `--append` flag to the `sign-batch` CLI cmd to combine the messages and sign those txs which are created with `--generate-only`.
* (x/consensus) [#12905](https://github.com/cosmos/cosmos-sdk/pull/12905) Create a new `x/consensus` module that is now responsible for maintaining Tendermint consensus parameters instead of `x/param`. Legacy types remain in order to facilitate parameter migration from the deprecated `x/params`. App developers should ensure that they execute `baseapp.MigrateParams` during their chain upgrade. These legacy types will be removed in a future release.

### Improvements

Expand Down
53 changes: 51 additions & 2 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ Please use the `ghcr.io/cosmos/proto-builder` image (version >= `0.11.0`) for ge

#### Broadcast Mode

Broadcast mode `block` was deprecated and has been removed. Please use `sync` mode instead.
When upgrading your tests from `block` to `sync` and checking for a transaction code, you need to query the transaction first (with its hash) to get the correct code.
Broadcast mode `block` was deprecated and has been removed. Please use `sync` mode
instead. When upgrading your tests from `block` to `sync` and checking for a
transaction code, you need to query the transaction first (with its hash) to get
the correct code.

### Modules

Expand All @@ -69,6 +71,53 @@ By default, the new `MinInitialDepositRatio` parameter is set to zero during mig
feature is disabled. If chains wish to utilize the minimum proposal deposits at time of submission, the migration logic needs to be
modified to set the new parameter to the desired value.

#### `x/consensus`

Introducing a new `x/consensus` module to handle managing Tendermint consensus
parameters. For migration it is required to call a specific migration to migrate
existing parameters from the deprecated `x/params` to `x/consensus` module. App
developers should ensure to call `baseapp.MigrateParams` in their upgrade handler.

Example:

```go
func (app SimApp) RegisterUpgradeHandlers() {
----> baseAppLegacySS := app.ParamsKeeper.Subspace(baseapp.Paramspace).WithKeyTable(paramstypes.ConsensusParamsKeyTable()) <----

app.UpgradeKeeper.SetUpgradeHandler(
UpgradeName,
func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
// Migrate Tendermint consensus parameters from x/params module to a
// dedicated x/consensus module.
----> baseapp.MigrateParams(ctx, baseAppLegacySS, &app.ConsensusParamsKeeper) <----

// ...

return app.ModuleManager.RunMigrations(ctx, app.Configurator(), fromVM)
},
)

// ...
}
```

The old params module is required to still be imported in your app.go in order to handle this migration.

##### App.go Changes

Previous:

```go
bApp.SetParamStore(app.ParamsKeeper.Subspace(baseapp.Paramspace).WithKeyTable(paramstypes.ConsensusParamsKeyTable()))
```

After:

```go
app.ConsensusParamsKeeper = consensusparamkeeper.NewKeeper(appCodec, keys[upgradetypes.StoreKey], authtypes.NewModuleAddress(govtypes.ModuleName).String())
bApp.SetParamStore(&app.ConsensusParamsKeeper)
```

### Ledger

Ledger support has been generalized to enable use of different apps and keytypes that use `secp256k1`. The Ledger interface remains the same, but it can now be provided through the Keyring `Options`, allowing higher-level chains to connect to different Ledger apps or use custom implementations. In addition, higher-level chains can provide custom key implementations around the Ledger public key, to enable greater flexibility with address generation and signing.
Expand Down
25 changes: 16 additions & 9 deletions baseapp/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package baseapp

import (
"encoding/json"
"errors"
"os"
"testing"

Expand Down Expand Up @@ -177,35 +178,41 @@ type paramStore struct {
db *dbm.MemDB
}

func (ps *paramStore) Set(_ sdk.Context, key []byte, value interface{}) {
var ParamstoreKey = []byte("paramstore")

func (ps *paramStore) Set(_ sdk.Context, value *tmproto.ConsensusParams) {
bz, err := json.Marshal(value)
if err != nil {
panic(err)
}

ps.db.Set(key, bz)
ps.db.Set(ParamstoreKey, bz)
}

func (ps *paramStore) Has(_ sdk.Context, key []byte) bool {
ok, err := ps.db.Has(key)
func (ps *paramStore) Has(_ sdk.Context) bool {
ok, err := ps.db.Has(ParamstoreKey)
if err != nil {
panic(err)
}

return ok
}

func (ps *paramStore) Get(_ sdk.Context, key []byte, ptr interface{}) {
bz, err := ps.db.Get(key)
func (ps paramStore) Get(_ sdk.Context) (*tmproto.ConsensusParams, error) {
bz, err := ps.db.Get(ParamstoreKey)
if err != nil {
panic(err)
return nil, err
}

if len(bz) == 0 {
return
return nil, errors.New("no consensus params")
}

if err := json.Unmarshal(bz, ptr); err != nil {
var params tmproto.ConsensusParams

if err := json.Unmarshal(bz, &params); err != nil {
panic(err)
}

return &params, nil
}
45 changes: 12 additions & 33 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import (
"sort"
"strings"

"github.com/cosmos/gogoproto/proto"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/tmhash"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"
"golang.org/x/exp/maps"

"github.com/cosmos/gogoproto/proto"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/snapshots"
"github.com/cosmos/cosmos-sdk/store"
Expand Down Expand Up @@ -407,39 +406,14 @@ func (app *BaseApp) GetConsensusParams(ctx sdk.Context) *tmproto.ConsensusParams
return nil
}

cp := new(tmproto.ConsensusParams)

if app.paramStore.Has(ctx, ParamStoreKeyBlockParams) {
var bp tmproto.BlockParams

app.paramStore.Get(ctx, ParamStoreKeyBlockParams, &bp)
cp.Block = &bp
}

if app.paramStore.Has(ctx, ParamStoreKeyEvidenceParams) {
var ep tmproto.EvidenceParams

app.paramStore.Get(ctx, ParamStoreKeyEvidenceParams, &ep)
cp.Evidence = &ep
}

if app.paramStore.Has(ctx, ParamStoreKeyValidatorParams) {
var vp tmproto.ValidatorParams

app.paramStore.Get(ctx, ParamStoreKeyValidatorParams, &vp)
cp.Validator = &vp
cp, err := app.paramStore.Get(ctx)
if err != nil {
panic(err)
}

return cp
}

// AddRunTxRecoveryHandler adds custom app.runTx method panic handlers.
func (app *BaseApp) AddRunTxRecoveryHandler(handlers ...RecoveryHandler) {
for _, h := range handlers {
app.runTxRecoveryMiddleware = newRecoveryMiddleware(h, app.runTxRecoveryMiddleware)
}
}

// StoreConsensusParams sets the consensus parameters to the baseapp's param store.
func (app *BaseApp) StoreConsensusParams(ctx sdk.Context, cp *tmproto.ConsensusParams) {
if app.paramStore == nil {
Expand All @@ -450,13 +424,18 @@ func (app *BaseApp) StoreConsensusParams(ctx sdk.Context, cp *tmproto.ConsensusP
return
}

app.paramStore.Set(ctx, ParamStoreKeyBlockParams, cp.Block)
app.paramStore.Set(ctx, ParamStoreKeyEvidenceParams, cp.Evidence)
app.paramStore.Set(ctx, ParamStoreKeyValidatorParams, cp.Validator)
app.paramStore.Set(ctx, cp)
// We're explicitly not storing the Tendermint app_version in the param store. It's
// stored instead in the x/upgrade store, with its own bump logic.
}

// AddRunTxRecoveryHandler adds custom app.runTx method panic handlers.
func (app *BaseApp) AddRunTxRecoveryHandler(handlers ...RecoveryHandler) {
for _, h := range handlers {
app.runTxRecoveryMiddleware = newRecoveryMiddleware(h, app.runTxRecoveryMiddleware)
}
}

// getMaximumBlockGas gets the maximum gas from the consensus params. It panics
// if maximum block gas is less than negative one and returns zero if negative
// one.
Expand Down
6 changes: 0 additions & 6 deletions baseapp/block_gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ import (
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
)

var blockMaxGas = uint64(simtestutil.DefaultConsensusParams.Block.MaxGas)
Expand Down Expand Up @@ -73,8 +71,6 @@ func TestBaseApp_BlockGas(t *testing.T) {
var (
bankKeeper bankkeeper.Keeper
accountKeeper authkeeper.AccountKeeper
paramsKeeper paramskeeper.Keeper
stakingKeeper *stakingkeeper.Keeper
appBuilder *runtime.AppBuilder
txConfig client.TxConfig
cdc codec.Codec
Expand All @@ -88,8 +84,6 @@ func TestBaseApp_BlockGas(t *testing.T) {
err = depinject.Inject(appConfig,
&bankKeeper,
&accountKeeper,
&paramsKeeper,
&stakingKeeper,
&interfaceRegistry,
&txConfig,
&cdc,
Expand Down
53 changes: 35 additions & 18 deletions baseapp/deliver_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"math/rand"
"net/url"
Expand All @@ -17,6 +18,14 @@ import (
"unsafe"

"cosmossdk.io/depinject"
"github.com/cosmos/gogoproto/jsonpb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"

"github.com/cosmos/cosmos-sdk/baseapp"
baseapptestutil "github.com/cosmos/cosmos-sdk/baseapp/testutil"
"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -34,13 +43,6 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/signing"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
"github.com/cosmos/gogoproto/jsonpb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"
)

var (
Expand Down Expand Up @@ -102,10 +104,13 @@ func setupBaseAppWithSnapshots(t *testing.T, config *setupConfig) (*baseapp.Base

// patch in TxConfig instead of using an output from x/auth/tx
txConfig := authtx.NewTxConfig(cdc, authtx.DefaultSignModes)

// set the TxDecoder in the BaseApp for minimal tx simulations
app.SetTxDecoder(txConfig.TxDecoder())

app.InitChain(abci.RequestInitChain{})
app.InitChain(abci.RequestInitChain{
ConsensusParams: &tmproto.ConsensusParams{},
})

r := rand.New(rand.NewSource(3920758213583))
keyCounter := 0
Expand Down Expand Up @@ -571,9 +576,13 @@ func TestGRPCQuery(t *testing.T) {
app := setupBaseApp(t, grpcQueryOpt)
app.GRPCQueryRouter().SetInterfaceRegistry(codectypes.NewInterfaceRegistry())

app.InitChain(abci.RequestInitChain{})
app.InitChain(abci.RequestInitChain{
ConsensusParams: &tmproto.ConsensusParams{},
})

header := tmproto.Header{Height: app.LastBlockHeight() + 1}
app.BeginBlock(abci.RequestBeginBlock{Header: header})

app.Commit()

req := testdata.SayHelloRequest{Name: "foo"}
Expand Down Expand Up @@ -1886,7 +1895,9 @@ func TestQuery(t *testing.T) {
app.SetTxDecoder(txConfig.TxDecoder())
app.SetParamStore(&paramStore{db: dbm.NewMemDB()})

app.InitChain(abci.RequestInitChain{})
app.InitChain(abci.RequestInitChain{
ConsensusParams: &tmproto.ConsensusParams{},
})

// NOTE: "/store/key1" tells us KVStore
// and the final "/key" says to use the data as the
Expand Down Expand Up @@ -2152,35 +2163,41 @@ type paramStore struct {
db *dbm.MemDB
}

func (ps *paramStore) Set(_ sdk.Context, key []byte, value interface{}) {
var ParamstoreKey = []byte("paramstore")

func (ps *paramStore) Set(_ sdk.Context, value *tmproto.ConsensusParams) {
bz, err := json.Marshal(value)
if err != nil {
panic(err)
}

ps.db.Set(key, bz)
ps.db.Set(ParamstoreKey, bz)
}

func (ps *paramStore) Has(_ sdk.Context, key []byte) bool {
ok, err := ps.db.Has(key)
func (ps *paramStore) Has(_ sdk.Context) bool {
ok, err := ps.db.Has(ParamstoreKey)
if err != nil {
panic(err)
}

return ok
}

func (ps *paramStore) Get(_ sdk.Context, key []byte, ptr interface{}) {
bz, err := ps.db.Get(key)
func (ps paramStore) Get(ctx sdk.Context) (*tmproto.ConsensusParams, error) {
bz, err := ps.db.Get(ParamstoreKey)
if err != nil {
panic(err)
}

if len(bz) == 0 {
return
return nil, errors.New("params not found")
}

if err := json.Unmarshal(bz, ptr); err != nil {
var params tmproto.ConsensusParams

if err := json.Unmarshal(bz, &params); err != nil {
panic(err)
}

return &params, nil
}
5 changes: 1 addition & 4 deletions baseapp/grpcrouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,5 @@ func (qrt *GRPCQueryRouter) SetInterfaceRegistry(interfaceRegistry codectypes.In
qrt.cdc = codec.NewProtoCodec(interfaceRegistry).GRPCCodec()
// Once we have an interface registry, we can register the interface
// registry reflection gRPC service.
reflection.RegisterReflectionServiceServer(
qrt,
reflection.NewReflectionServiceServer(interfaceRegistry),
)
reflection.RegisterReflectionServiceServer(qrt, reflection.NewReflectionServiceServer(interfaceRegistry))
}
Loading

0 comments on commit 18c0d0f

Please sign in to comment.