Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Evidence Age Util + Governable Consensus Params #5952

Merged
merged 36 commits into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
00b21da
Prep baseapp
alexanderbez Apr 7, 2020
740ddcb
Update BaseApp consensus params to use param store
alexanderbez Apr 8, 2020
0942c39
Update simapp
alexanderbez Apr 8, 2020
ea505fe
Merge branch 'master' into bez/5532-evidence-age-refactor
alexanderbez Apr 12, 2020
d898418
Update baseapp tests
alexanderbez Apr 12, 2020
e6bccde
rename method
alexanderbez Apr 12, 2020
c3a9e13
Minor updates
alexanderbez Apr 12, 2020
4373a03
Fix build
alexanderbez Apr 12, 2020
fe70d0a
Set default consensus params
alexanderbez Apr 12, 2020
d0d97d7
Fix TestGetMaximumBlockGas
alexanderbez Apr 12, 2020
6bd301b
Add ABCIPubKeyTypeSecp256k1 to types
alexanderbez Apr 12, 2020
c587c16
Fix module unit tests
alexanderbez Apr 13, 2020
c510173
Use Has before querying params
alexanderbez Apr 13, 2020
dc545c4
Add nil check to storeConsensusParams
alexanderbez Apr 13, 2020
0a79042
Fix GetIfExists
alexanderbez Apr 13, 2020
fc108c0
Remove params from x/evidence
alexanderbez Apr 13, 2020
40a6db6
Add unit tests for consensus param validation
alexanderbez Apr 13, 2020
7a896d6
Add changelog entries
alexanderbez Apr 13, 2020
3351643
Update x/evidence/keeper/infraction.go
alexanderbez Apr 13, 2020
d87f06e
Update baseapp/params.go
alexanderbez Apr 13, 2020
9a87180
Update baseapp/params.go
fedekunze Apr 14, 2020
38ca5b4
Update simapp/app.go
alexanderbez Apr 14, 2020
fc0c70a
Update simapp/app.go
alexanderbez Apr 14, 2020
6daa445
Update app exporting to handle consensus params
alexanderbez Apr 14, 2020
01485b9
Add changelog entries
alexanderbez Apr 14, 2020
0e80ecd
Merge branch 'master' into bez/5532-evidence-age-refactor
alexanderbez Apr 14, 2020
a8f6915
Fix merge
alexanderbez Apr 14, 2020
2e34b91
Merge branch 'master' into bez/5532-evidence-age-refactor
alexanderbez Apr 14, 2020
7ed776c
Lint
alexanderbez Apr 14, 2020
71e197b
Merge branch 'bez/5532-evidence-age-refactor' of github.com:cosmos/co…
alexanderbez Apr 14, 2020
faae5c3
create std pkg and move ConsensusParamsKeyTable
alexanderbez Apr 15, 2020
b0c426f
Update docs
alexanderbez Apr 15, 2020
6b17043
Update std/doc.go
alexanderbez Apr 15, 2020
d33ce02
Update std/doc.go
alexanderbez Apr 15, 2020
38b6d38
Merge branch 'master' into bez/5532-evidence-age-refactor
alexanderbez Apr 16, 2020
3f65406
Merge branch 'master' into bez/5532-evidence-age-refactor
alexanderbez Apr 16, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ that parse log messages.
older clients.
* (x/auth) [\#5844](https://github.com/cosmos/cosmos-sdk/pull/5844) `tx sign` command now returns an error when signing is attempted with offline/multisig keys.
* (client/keys) [\#5889](https://github.com/cosmos/cosmos-sdk/pull/5889) Remove `keys update` command.
<<<<<<< HEAD
* (x/evidence) [\#5952](https://github.com/cosmos/cosmos-sdk/pull/5952) Remove CLI and REST handlers for querying `x/evidence` parameters.
=======
* (server) [\#5982](https://github.com/cosmos/cosmos-sdk/pull/5982) `--pruning` now must be set to `custom` if you want to customise the granular options.
>>>>>>> master

### API Breaking Changes

Expand Down Expand Up @@ -87,6 +91,8 @@ constructor is provided [\#5889](https://github.com/cosmos/cosmos-sdk/pull/5889)
to new keyring backends. Plus, the package and the new keyring no longer depends on the sdk.Config singleton. Please consult the package documentation for more
information on how to implement the new `Keyring` interface.
* [\#5858](https://github.com/cosmos/cosmos-sdk/pull/5858) Make Keyring store keys by name and address's hexbytes representation.
* (x/evidence) [\#5952](https://github.com/cosmos/cosmos-sdk/pull/5952) Remove APIs for getting and setting `x/evidence` parameters. `BaseApp` now uses a `ParamStore` to manage Tendermint consensus parameters which is managed via the `x/params` `Substore` type.
* (export) [\#5952](https://github.com/cosmos/cosmos-sdk/pull/5952) `AppExporter` now returns ABCI consensus parameters to be included in marshaled exported state. These parameters must be returned from the application via the `BaseApp`.

### Features

Expand Down Expand Up @@ -185,6 +191,7 @@ Buffers for state serialization instead of Amino.
* The module now accepts a `Codec` interface which extends the `codec.Marshaler` interface by
requiring a concrete codec to know how to serialize `Proposal` types.
* (codec) [\#5799](https://github.com/cosmos/cosmos-sdk/pull/5799) Now we favor the use of `(Un)MarshalBinaryBare` instead of `(Un)MarshalBinaryLengthPrefixed` in all cases that are not needed.
* (x/evidence) [\#5952](https://github.com/cosmos/cosmos-sdk/pull/5952) Remove parameters from `x/evidence` genesis and module state. The `x/evidence` module now solely uses Tendermint consensus parameters to determine of evidence is valid or not.

### Improvements

Expand All @@ -207,6 +214,7 @@ functionality that requires an online connection.
* (client) [\#5856](https://github.com/cosmos/cosmos-sdk/pull/5856) Added the possibility to set `--offline` flag with config command.
* (client) [\#5895](https://github.com/cosmos/cosmos-sdk/issues/5895) show config options in the config command's help screen.
* (types/rest) [\#5900](https://github.com/cosmos/cosmos-sdk/pull/5900) Add Check*Error function family to spare developers from replicating tons of boilerplate code.
* (x/evidence) [\#5952](https://github.com/cosmos/cosmos-sdk/pull/5952) Tendermint Consensus parameters can now be changed via parameter change proposals through `x/gov`.

## [v0.38.3] - 2020-04-09

Expand Down
15 changes: 8 additions & 7 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ import (
// InitChain implements the ABCI interface. It runs the initialization logic
// directly on the CommitMultiStore.
func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitChain) {
// stash the consensus params in the cms main store and memoize
if req.ConsensusParams != nil {
app.setConsensusParams(req.ConsensusParams)
app.storeConsensusParams(req.ConsensusParams)
}

initHeader := abci.Header{ChainID: req.ChainId, Time: req.Time}

// initialize the deliver state and check state with a correct header
app.setDeliverState(initHeader)
app.setCheckState(initHeader)

// Store the consensus params in the BaseApp's paramstore. Note, this must be
// done after the deliver state and context have been set as it's persisted
// to state.
if req.ConsensusParams != nil {
app.storeConsensusParams(app.deliverState.ctx, req.ConsensusParams)
}

if app.initChainer == nil {
return
}
Expand Down Expand Up @@ -124,7 +125,7 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg

// add block gas meter
var gasMeter sdk.GasMeter
if maxGas := app.getMaximumBlockGas(); maxGas > 0 {
if maxGas := app.getMaximumBlockGas(app.deliverState.ctx); maxGas > 0 {
gasMeter = sdk.NewGasMeter(maxGas)
} else {
gasMeter = sdk.NewInfiniteGasMeter()
Expand Down
118 changes: 59 additions & 59 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package baseapp

import (
"errors"
"fmt"
"reflect"
"runtime/debug"
"strings"

"github.com/gogo/protobuf/proto"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/tmhash"
"github.com/tendermint/tendermint/libs/log"
Expand All @@ -23,9 +21,6 @@ const (
runTxModeReCheck // Recheck a (pending) transaction after a commit
runTxModeSimulate // Simulate a transaction
runTxModeDeliver // Deliver a transaction

// MainStoreKey is the string representation of the main store
MainStoreKey = "main"
)

var (
Expand Down Expand Up @@ -59,9 +54,6 @@ type BaseApp struct { // nolint: maligned
queryRouter sdk.QueryRouter // router for redirecting query calls
txDecoder sdk.TxDecoder // unmarshal []byte into sdk.Tx

// set upon LoadVersion or LoadLatestVersion.
baseKey *sdk.KVStoreKey // Main KVStore in cms

anteHandler sdk.AnteHandler // ante handler for fee and auth
initChainer sdk.InitChainer // initialize state with validators and state blob
beginBlocker sdk.BeginBlocker // logic to run before any txs
Expand All @@ -83,9 +75,9 @@ type BaseApp struct { // nolint: maligned
// absent validators from begin block
voteInfos []abci.VoteInfo

// consensus params
// TODO: Move this in the future to baseapp param store on main store.
consensusParams *abci.ConsensusParams
// paramStore is used to query for ABCI consensus parameters from an
// application parameter store.
paramStore ParamStore

// The minimum gas prices a validator is willing to accept for processing a
// transaction. This is mainly used for DoS and spam prevention.
Expand Down Expand Up @@ -209,12 +201,13 @@ func (app *BaseApp) MountStore(key sdk.StoreKey, typ sdk.StoreType) {

// LoadLatestVersion loads the latest application version. It will panic if
// called more than once on a running BaseApp.
func (app *BaseApp) LoadLatestVersion(baseKey *sdk.KVStoreKey) error {
func (app *BaseApp) LoadLatestVersion() error {
err := app.storeLoader(app.cms)
if err != nil {
return err
return fmt.Errorf("failed to load latest version: %w", err)
}
return app.initFromMainStore(baseKey)

return app.init()
}

// DefaultStoreLoader will be used by default and loads the latest version
Expand All @@ -224,12 +217,13 @@ func DefaultStoreLoader(ms sdk.CommitMultiStore) error {

// LoadVersion loads the BaseApp application version. It will panic if called
// more than once on a running baseapp.
func (app *BaseApp) LoadVersion(version int64, baseKey *sdk.KVStoreKey) error {
func (app *BaseApp) LoadVersion(version int64) error {
err := app.cms.LoadVersion(version)
if err != nil {
return err
return fmt.Errorf("failed to load version %d: %w", version, err)
}
return app.initFromMainStore(baseKey)

return app.init()
}

// LastCommitID returns the last CommitID of the multistore.
Expand All @@ -242,33 +236,9 @@ func (app *BaseApp) LastBlockHeight() int64 {
return app.cms.LastCommitID().Version
}

// initializes the remaining logic from app.cms
func (app *BaseApp) initFromMainStore(baseKey *sdk.KVStoreKey) error {
mainStore := app.cms.GetKVStore(baseKey)
if mainStore == nil {
return errors.New("baseapp expects MultiStore with 'main' KVStore")
}

// memoize baseKey
if app.baseKey != nil {
panic("app.baseKey expected to be nil; duplicate init?")
}
app.baseKey = baseKey

// Load the consensus params from the main store. If the consensus params are
// nil, it will be saved later during InitChain.
//
// TODO: assert that InitChain hasn't yet been called.
consensusParamsBz := mainStore.Get(mainConsensusParamsKey)
if consensusParamsBz != nil {
var consensusParams = &abci.ConsensusParams{}

err := proto.Unmarshal(consensusParamsBz, consensusParams)
if err != nil {
panic(err)
}

app.setConsensusParams(consensusParams)
func (app *BaseApp) init() error {
if app.sealed {
panic("cannot call initFromMainStore: baseapp already sealed")
}

// needed for the export command which inits from store but never calls initchain
Expand Down Expand Up @@ -337,30 +307,59 @@ func (app *BaseApp) setDeliverState(header abci.Header) {
}
}

// setConsensusParams memoizes the consensus params.
func (app *BaseApp) setConsensusParams(consensusParams *abci.ConsensusParams) {
app.consensusParams = consensusParams
// GetConsensusParams returns the current consensus parameters from the BaseApp's
// ParamStore. If the BaseApp has no ParamStore defined, nil is returned.
func (app *BaseApp) GetConsensusParams(ctx sdk.Context) *abci.ConsensusParams {
if app.paramStore == nil {
return nil
}

cp := new(abci.ConsensusParams)

if app.paramStore.Has(ctx, ParamStoreKeyBlockParams) {
var bp abci.BlockParams
app.paramStore.Get(ctx, ParamStoreKeyBlockParams, &bp)
cp.Block = &bp
}

if app.paramStore.Has(ctx, ParamStoreKeyEvidenceParams) {
var ep abci.EvidenceParams
app.paramStore.Get(ctx, ParamStoreKeyEvidenceParams, &ep)
cp.Evidence = &ep
}

if app.paramStore.Has(ctx, ParamStoreKeyValidatorParams) {
var vp abci.ValidatorParams
app.paramStore.Get(ctx, ParamStoreKeyValidatorParams, &vp)
cp.Validator = &vp
}

return cp
}

// setConsensusParams stores the consensus params to the main store.
func (app *BaseApp) storeConsensusParams(consensusParams *abci.ConsensusParams) {
consensusParamsBz, err := proto.Marshal(consensusParams)
if err != nil {
panic(err)
func (app *BaseApp) storeConsensusParams(ctx sdk.Context, cp *abci.ConsensusParams) {
if app.paramStore == nil {
panic("cannot store consensus params with no params store set")
}
mainStore := app.cms.GetKVStore(app.baseKey)
mainStore.Set(mainConsensusParamsKey, consensusParamsBz)
if cp == nil {
return
}

app.paramStore.Set(ctx, ParamStoreKeyBlockParams, cp.Block)
app.paramStore.Set(ctx, ParamStoreKeyEvidenceParams, cp.Evidence)
app.paramStore.Set(ctx, ParamStoreKeyValidatorParams, cp.Validator)
}

// 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.
func (app *BaseApp) getMaximumBlockGas() uint64 {
if app.consensusParams == nil || app.consensusParams.Block == nil {
func (app *BaseApp) getMaximumBlockGas(ctx sdk.Context) uint64 {
cp := app.GetConsensusParams(ctx)
if cp == nil || cp.Block == nil {
return 0
}

maxGas := app.consensusParams.Block.MaxGas
maxGas := cp.Block.MaxGas
switch {
case maxGas < -1:
panic(fmt.Sprintf("invalid maximum block gas: %d", maxGas))
Expand Down Expand Up @@ -416,8 +415,9 @@ func (app *BaseApp) getState(mode runTxMode) *state {
func (app *BaseApp) getContextForTx(mode runTxMode, txBytes []byte) sdk.Context {
ctx := app.getState(mode).ctx.
WithTxBytes(txBytes).
WithVoteInfos(app.voteInfos).
WithConsensusParams(app.consensusParams)
WithVoteInfos(app.voteInfos)

ctx = ctx.WithConsensusParams(app.GetConsensusParams(ctx))

if mode == runTxModeReCheck {
ctx = ctx.WithIsReCheckTx(true)
Expand Down
Loading