Skip to content

Commit 306a9a7

Browse files
authored
feat!: consensus module (#12905)
1 parent 87e46b2 commit 306a9a7

File tree

54 files changed

+541
-308
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+541
-308
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
5151
* (x/bank) [#11981](https://github.com/cosmos/cosmos-sdk/pull/11981) Create the `SetSendEnabled` endpoint for managing the bank's SendEnabled settings.
5252
* (x/auth) [#13210](https://github.com/cosmos/cosmos-sdk/pull/13210) Add `Query/AccountInfo` endpoint for simplified access to basic account info.
5353
* (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`.
54+
* (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.
5455

5556
### Improvements
5657

UPGRADING.md

+51-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ Please use the `ghcr.io/cosmos/proto-builder` image (version >= `0.11.0`) for ge
5252

5353
#### Broadcast Mode
5454

55-
Broadcast mode `block` was deprecated and has been removed. Please use `sync` mode instead.
56-
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.
55+
Broadcast mode `block` was deprecated and has been removed. Please use `sync` mode
56+
instead. When upgrading your tests from `block` to `sync` and checking for a
57+
transaction code, you need to query the transaction first (with its hash) to get
58+
the correct code.
5759

5860
### Modules
5961

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

74+
#### `x/consensus`
75+
76+
Introducing a new `x/consensus` module to handle managing Tendermint consensus
77+
parameters. For migration it is required to call a specific migration to migrate
78+
existing parameters from the deprecated `x/params` to `x/consensus` module. App
79+
developers should ensure to call `baseapp.MigrateParams` in their upgrade handler.
80+
81+
Example:
82+
83+
```go
84+
func (app SimApp) RegisterUpgradeHandlers() {
85+
----> baseAppLegacySS := app.ParamsKeeper.Subspace(baseapp.Paramspace).WithKeyTable(paramstypes.ConsensusParamsKeyTable()) <----
86+
87+
app.UpgradeKeeper.SetUpgradeHandler(
88+
UpgradeName,
89+
func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
90+
// Migrate Tendermint consensus parameters from x/params module to a
91+
// dedicated x/consensus module.
92+
----> baseapp.MigrateParams(ctx, baseAppLegacySS, &app.ConsensusParamsKeeper) <----
93+
94+
// ...
95+
96+
return app.ModuleManager.RunMigrations(ctx, app.Configurator(), fromVM)
97+
},
98+
)
99+
100+
// ...
101+
}
102+
```
103+
104+
The old params module is required to still be imported in your app.go in order to handle this migration.
105+
106+
##### App.go Changes
107+
108+
Previous:
109+
110+
```go
111+
bApp.SetParamStore(app.ParamsKeeper.Subspace(baseapp.Paramspace).WithKeyTable(paramstypes.ConsensusParamsKeyTable()))
112+
```
113+
114+
After:
115+
116+
```go
117+
app.ConsensusParamsKeeper = consensusparamkeeper.NewKeeper(appCodec, keys[upgradetypes.StoreKey], authtypes.NewModuleAddress(govtypes.ModuleName).String())
118+
bApp.SetParamStore(&app.ConsensusParamsKeeper)
119+
```
120+
72121
### Ledger
73122

74123
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.

baseapp/abci_test.go

+16-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package baseapp
22

33
import (
44
"encoding/json"
5+
"errors"
56
"os"
67
"testing"
78

@@ -177,35 +178,41 @@ type paramStore struct {
177178
db *dbm.MemDB
178179
}
179180

180-
func (ps *paramStore) Set(_ sdk.Context, key []byte, value interface{}) {
181+
var ParamstoreKey = []byte("paramstore")
182+
183+
func (ps *paramStore) Set(_ sdk.Context, value *tmproto.ConsensusParams) {
181184
bz, err := json.Marshal(value)
182185
if err != nil {
183186
panic(err)
184187
}
185188

186-
ps.db.Set(key, bz)
189+
ps.db.Set(ParamstoreKey, bz)
187190
}
188191

189-
func (ps *paramStore) Has(_ sdk.Context, key []byte) bool {
190-
ok, err := ps.db.Has(key)
192+
func (ps *paramStore) Has(_ sdk.Context) bool {
193+
ok, err := ps.db.Has(ParamstoreKey)
191194
if err != nil {
192195
panic(err)
193196
}
194197

195198
return ok
196199
}
197200

198-
func (ps *paramStore) Get(_ sdk.Context, key []byte, ptr interface{}) {
199-
bz, err := ps.db.Get(key)
201+
func (ps paramStore) Get(_ sdk.Context) (*tmproto.ConsensusParams, error) {
202+
bz, err := ps.db.Get(ParamstoreKey)
200203
if err != nil {
201-
panic(err)
204+
return nil, err
202205
}
203206

204207
if len(bz) == 0 {
205-
return
208+
return nil, errors.New("no consensus params")
206209
}
207210

208-
if err := json.Unmarshal(bz, ptr); err != nil {
211+
var params tmproto.ConsensusParams
212+
213+
if err := json.Unmarshal(bz, &params); err != nil {
209214
panic(err)
210215
}
216+
217+
return &params, nil
211218
}

baseapp/baseapp.go

+12-33
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ import (
55
"sort"
66
"strings"
77

8+
"github.com/cosmos/gogoproto/proto"
89
abci "github.com/tendermint/tendermint/abci/types"
910
"github.com/tendermint/tendermint/crypto/tmhash"
1011
"github.com/tendermint/tendermint/libs/log"
1112
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
1213
dbm "github.com/tendermint/tm-db"
1314
"golang.org/x/exp/maps"
1415

15-
"github.com/cosmos/gogoproto/proto"
16-
1716
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
1817
"github.com/cosmos/cosmos-sdk/snapshots"
1918
"github.com/cosmos/cosmos-sdk/store"
@@ -407,39 +406,14 @@ func (app *BaseApp) GetConsensusParams(ctx sdk.Context) *tmproto.ConsensusParams
407406
return nil
408407
}
409408

410-
cp := new(tmproto.ConsensusParams)
411-
412-
if app.paramStore.Has(ctx, ParamStoreKeyBlockParams) {
413-
var bp tmproto.BlockParams
414-
415-
app.paramStore.Get(ctx, ParamStoreKeyBlockParams, &bp)
416-
cp.Block = &bp
417-
}
418-
419-
if app.paramStore.Has(ctx, ParamStoreKeyEvidenceParams) {
420-
var ep tmproto.EvidenceParams
421-
422-
app.paramStore.Get(ctx, ParamStoreKeyEvidenceParams, &ep)
423-
cp.Evidence = &ep
424-
}
425-
426-
if app.paramStore.Has(ctx, ParamStoreKeyValidatorParams) {
427-
var vp tmproto.ValidatorParams
428-
429-
app.paramStore.Get(ctx, ParamStoreKeyValidatorParams, &vp)
430-
cp.Validator = &vp
409+
cp, err := app.paramStore.Get(ctx)
410+
if err != nil {
411+
panic(err)
431412
}
432413

433414
return cp
434415
}
435416

436-
// AddRunTxRecoveryHandler adds custom app.runTx method panic handlers.
437-
func (app *BaseApp) AddRunTxRecoveryHandler(handlers ...RecoveryHandler) {
438-
for _, h := range handlers {
439-
app.runTxRecoveryMiddleware = newRecoveryMiddleware(h, app.runTxRecoveryMiddleware)
440-
}
441-
}
442-
443417
// StoreConsensusParams sets the consensus parameters to the baseapp's param store.
444418
func (app *BaseApp) StoreConsensusParams(ctx sdk.Context, cp *tmproto.ConsensusParams) {
445419
if app.paramStore == nil {
@@ -450,13 +424,18 @@ func (app *BaseApp) StoreConsensusParams(ctx sdk.Context, cp *tmproto.ConsensusP
450424
return
451425
}
452426

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

432+
// AddRunTxRecoveryHandler adds custom app.runTx method panic handlers.
433+
func (app *BaseApp) AddRunTxRecoveryHandler(handlers ...RecoveryHandler) {
434+
for _, h := range handlers {
435+
app.runTxRecoveryMiddleware = newRecoveryMiddleware(h, app.runTxRecoveryMiddleware)
436+
}
437+
}
438+
460439
// getMaximumBlockGas gets the maximum gas from the consensus params. It panics
461440
// if maximum block gas is less than negative one and returns zero if negative
462441
// one.

baseapp/block_gas_test.go

-6
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ import (
3232
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
3333
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
3434
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
35-
paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
36-
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
3735
)
3836

3937
var blockMaxGas = uint64(simtestutil.DefaultConsensusParams.Block.MaxGas)
@@ -73,8 +71,6 @@ func TestBaseApp_BlockGas(t *testing.T) {
7371
var (
7472
bankKeeper bankkeeper.Keeper
7573
accountKeeper authkeeper.AccountKeeper
76-
paramsKeeper paramskeeper.Keeper
77-
stakingKeeper *stakingkeeper.Keeper
7874
appBuilder *runtime.AppBuilder
7975
txConfig client.TxConfig
8076
cdc codec.Codec
@@ -88,8 +84,6 @@ func TestBaseApp_BlockGas(t *testing.T) {
8884
err = depinject.Inject(appConfig,
8985
&bankKeeper,
9086
&accountKeeper,
91-
&paramsKeeper,
92-
&stakingKeeper,
9387
&interfaceRegistry,
9488
&txConfig,
9589
&cdc,

baseapp/deliver_tx_test.go

+35-18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"encoding/binary"
77
"encoding/json"
8+
"errors"
89
"fmt"
910
"math/rand"
1011
"net/url"
@@ -17,6 +18,14 @@ import (
1718
"unsafe"
1819

1920
"cosmossdk.io/depinject"
21+
"github.com/cosmos/gogoproto/jsonpb"
22+
"github.com/stretchr/testify/assert"
23+
"github.com/stretchr/testify/require"
24+
abci "github.com/tendermint/tendermint/abci/types"
25+
"github.com/tendermint/tendermint/libs/log"
26+
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
27+
dbm "github.com/tendermint/tm-db"
28+
2029
"github.com/cosmos/cosmos-sdk/baseapp"
2130
baseapptestutil "github.com/cosmos/cosmos-sdk/baseapp/testutil"
2231
"github.com/cosmos/cosmos-sdk/client"
@@ -34,13 +43,6 @@ import (
3443
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
3544
"github.com/cosmos/cosmos-sdk/x/auth/signing"
3645
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
37-
"github.com/cosmos/gogoproto/jsonpb"
38-
"github.com/stretchr/testify/assert"
39-
"github.com/stretchr/testify/require"
40-
abci "github.com/tendermint/tendermint/abci/types"
41-
"github.com/tendermint/tendermint/libs/log"
42-
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
43-
dbm "github.com/tendermint/tm-db"
4446
)
4547

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

103105
// patch in TxConfig instead of using an output from x/auth/tx
104106
txConfig := authtx.NewTxConfig(cdc, authtx.DefaultSignModes)
107+
105108
// set the TxDecoder in the BaseApp for minimal tx simulations
106109
app.SetTxDecoder(txConfig.TxDecoder())
107110

108-
app.InitChain(abci.RequestInitChain{})
111+
app.InitChain(abci.RequestInitChain{
112+
ConsensusParams: &tmproto.ConsensusParams{},
113+
})
109114

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

574-
app.InitChain(abci.RequestInitChain{})
579+
app.InitChain(abci.RequestInitChain{
580+
ConsensusParams: &tmproto.ConsensusParams{},
581+
})
582+
575583
header := tmproto.Header{Height: app.LastBlockHeight() + 1}
576584
app.BeginBlock(abci.RequestBeginBlock{Header: header})
585+
577586
app.Commit()
578587

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

1889-
app.InitChain(abci.RequestInitChain{})
1898+
app.InitChain(abci.RequestInitChain{
1899+
ConsensusParams: &tmproto.ConsensusParams{},
1900+
})
18901901

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

2155-
func (ps *paramStore) Set(_ sdk.Context, key []byte, value interface{}) {
2166+
var ParamstoreKey = []byte("paramstore")
2167+
2168+
func (ps *paramStore) Set(_ sdk.Context, value *tmproto.ConsensusParams) {
21562169
bz, err := json.Marshal(value)
21572170
if err != nil {
21582171
panic(err)
21592172
}
21602173

2161-
ps.db.Set(key, bz)
2174+
ps.db.Set(ParamstoreKey, bz)
21622175
}
21632176

2164-
func (ps *paramStore) Has(_ sdk.Context, key []byte) bool {
2165-
ok, err := ps.db.Has(key)
2177+
func (ps *paramStore) Has(_ sdk.Context) bool {
2178+
ok, err := ps.db.Has(ParamstoreKey)
21662179
if err != nil {
21672180
panic(err)
21682181
}
21692182

21702183
return ok
21712184
}
21722185

2173-
func (ps *paramStore) Get(_ sdk.Context, key []byte, ptr interface{}) {
2174-
bz, err := ps.db.Get(key)
2186+
func (ps paramStore) Get(ctx sdk.Context) (*tmproto.ConsensusParams, error) {
2187+
bz, err := ps.db.Get(ParamstoreKey)
21752188
if err != nil {
21762189
panic(err)
21772190
}
21782191

21792192
if len(bz) == 0 {
2180-
return
2193+
return nil, errors.New("params not found")
21812194
}
21822195

2183-
if err := json.Unmarshal(bz, ptr); err != nil {
2196+
var params tmproto.ConsensusParams
2197+
2198+
if err := json.Unmarshal(bz, &params); err != nil {
21842199
panic(err)
21852200
}
2201+
2202+
return &params, nil
21862203
}

baseapp/grpcrouter.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,5 @@ func (qrt *GRPCQueryRouter) SetInterfaceRegistry(interfaceRegistry codectypes.In
114114
qrt.cdc = codec.NewProtoCodec(interfaceRegistry).GRPCCodec()
115115
// Once we have an interface registry, we can register the interface
116116
// registry reflection gRPC service.
117-
reflection.RegisterReflectionServiceServer(
118-
qrt,
119-
reflection.NewReflectionServiceServer(interfaceRegistry),
120-
)
117+
reflection.RegisterReflectionServiceServer(qrt, reflection.NewReflectionServiceServer(interfaceRegistry))
121118
}

0 commit comments

Comments
 (0)