Skip to content

Commit

Permalink
Merge PR #5240: x/evidence module implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbez authored Nov 6, 2019
1 parent 82a2c5d commit 95ddc24
Show file tree
Hide file tree
Showing 44 changed files with 2,373 additions and 78 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ increased significantly due to modular `AnteHandler` support. Increase GasLimit

### Features

* (x/evidence) [\#5240](https://github.com/cosmos/cosmos-sdk/pull/5240) Initial implementation of the `x/evidence` module.
* (cli) [\#5212](https://github.com/cosmos/cosmos-sdk/issues/5212) The `q gov proposals` command now supports pagination.
* (store) [\#4724](https://github.com/cosmos/cosmos-sdk/issues/4724) Multistore supports substore migrations upon load. New `rootmulti.Store.LoadLatestVersionAndUpgrade` method in
`Baseapp` supports `StoreLoader` to enable various upgrade strategies. It no
Expand Down
13 changes: 8 additions & 5 deletions docs/architecture/adr-009-evidence-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
## Changelog

- 2019 July 31: Initial draft
- 2019 October 24: Initial implementation

## Status

Proposed
Accepted

## Context

Expand Down Expand Up @@ -55,7 +56,8 @@ type Evidence interface {
Route() string
Type() string
String() string
ValidateBasic() Error
Hash() HexBytes
ValidateBasic() error

// The consensus address of the malicious validator at time of infraction
GetConsensusAddress() ConsAddress
Expand All @@ -78,7 +80,7 @@ the `x/evidence` module. It accomplishes this through the `Router` implementatio

```go
type Router interface {
AddRoute(r string, h Handler)
AddRoute(r string, h Handler) Router
HasRoute(r string) bool
GetRoute(path string) Handler
Seal()
Expand All @@ -97,7 +99,7 @@ necessary in order for the `Handler` to make the necessary state transitions.
If no error is returned, the `Evidence` is considered valid.

```go
type Handler func(Context, Evidence) Error
type Handler func(Context, Evidence) error
```

### Submission
Expand Down Expand Up @@ -128,7 +130,7 @@ the module's router and invoking the corresponding `Handler` which may include
slashing and jailing the validator. Upon success, the submitted evidence is persisted.

```go
func (k Keeper) SubmitEvidence(ctx Context, evidence Evidence) Error {
func (k Keeper) SubmitEvidence(ctx Context, evidence Evidence) error {
handler := keeper.router.GetRoute(evidence.Route())
if err := handler(ctx, evidence); err != nil {
return ErrInvalidEvidence(keeper.codespace, err)
Expand Down Expand Up @@ -177,3 +179,4 @@ due to the inability to introduce the new evidence type's corresponding handler

- [ICS](https://github.com/cosmos/ics)
- [IBC Architecture](https://github.com/cosmos/ics/blob/master/ibc/1_IBC_ARCHITECTURE.md)
- [Tendermint Fork Accountability](https://github.com/tendermint/tendermint/blob/master/docs/spec/consensus/fork-accountability.md)
75 changes: 36 additions & 39 deletions docs/building-modules/README.md
Original file line number Diff line number Diff line change
@@ -1,78 +1,75 @@
# Auth
# Modules

The `x/auth` modules is used for accounts
## Auth

See the [API docs](https://godoc.org/github.com/cosmos/cosmos-sdk/x/auth)
The `x/auth` modules is used for accounts

See the [specification](https://github.com/cosmos/cosmos-sdk/tree/master/docs/spec/auth)
- [API docs](https://godoc.org/github.com/cosmos/cosmos-sdk/x/auth)
- [Specification](https://github.com/cosmos/cosmos-sdk/tree/master/docs/spec/auth)

# Bank
## Bank

The `x/bank` module is for transferring coins between accounts.

See the [API docs](https://godoc.org/github.com/cosmos/cosmos-sdk/x/bank).

See the [specification](https://github.com/cosmos/cosmos-sdk/tree/master/docs/spec/bank)
- [API docs](https://godoc.org/github.com/cosmos/cosmos-sdk/x/bank)
- [Specification](https://github.com/cosmos/cosmos-sdk/tree/master/docs/spec/bank)

# Stake
## Staking

The `x/staking` module is for Cosmos Delegated-Proof-of-Stake.

See the [API docs](https://godoc.org/github.com/cosmos/cosmos-sdk/x/staking).
- [API docs](https://godoc.org/github.com/cosmos/cosmos-sdk/x/staking)
- [Specification](https://github.com/cosmos/cosmos-sdk/tree/master/docs/spec/staking)

See the
[specification](https://github.com/cosmos/cosmos-sdk/tree/master/docs/spec/staking)

# Slashing
## Slashing

The `x/slashing` module is for Cosmos Delegated-Proof-of-Stake.

See the [API docs](https://godoc.org/github.com/cosmos/cosmos-sdk/x/slashing)

See the
[specification](https://github.com/cosmos/cosmos-sdk/tree/master/docs/spec/slashing)
- [API docs](https://godoc.org/github.com/cosmos/cosmos-sdk/x/slashing)
- [Specification](https://github.com/cosmos/cosmos-sdk/tree/master/docs/spec/slashing)

# Distribution
## Distribution

The `x/distribution` module is for distributing fees and inflation across bonded
stakeholders.

See the [API docs](https://godoc.org/github.com/cosmos/cosmos-sdk/x/distribution)

See the
[specification](https://github.com/cosmos/cosmos-sdk/tree/master/docs/spec/distribution)
- [API docs](https://godoc.org/github.com/cosmos/cosmos-sdk/x/distribution)
- [Specification](https://github.com/cosmos/cosmos-sdk/tree/master/docs/spec/distribution)

# Governance
## Governance

The `x/gov` module is for bonded stakeholders to make proposals and vote on them.

See the [API docs](https://godoc.org/github.com/cosmos/cosmos-sdk/x/gov)

See the
[specification](https://github.com/cosmos/cosmos-sdk/tree/master/docs/spec/governance)
- [API docs](https://godoc.org/github.com/cosmos/cosmos-sdk/x/gov)
- [Specification](https://github.com/cosmos/cosmos-sdk/tree/master/docs/spec/governance)

To keep up with the current status of IBC, follow and contribute to [ICS](https://github.com/cosmos/ics)

# Crisis
## Crisis

The `x/crisis` module is for halting the blockchain under certain circumstances.

See the [API Docs](https://godoc.org/github.com/cosmos/cosmos-sdk/x/crisis)

See the [specification](https://github.com/cosmos/cosmos-sdk/blob/master/docs/spec/crisis)
- [API Docs](https://godoc.org/github.com/cosmos/cosmos-sdk/x/crisis)
- [Specification](https://github.com/cosmos/cosmos-sdk/blob/master/docs/spec/crisis)

# Mint
## Mint

The `x/mint` module is for flexible inflation rates and effect a balance between market liquidity and staked supply.

See the [API Docs](https://godoc.org/github.com/cosmos/cosmos-sdk/x/mint)

See the [specification](https://github.com/cosmos/cosmos-sdk/blob/master/docs/spec/mint)
- [API Docs](https://godoc.org/github.com/cosmos/cosmos-sdk/x/mint)
- [Specification](https://github.com/cosmos/cosmos-sdk/blob/master/docs/spec/mint)

# Params
## Params

The `x/params` module provides a globally available parameter store.

See the [API Docs](https://godoc.org/github.com/cosmos/cosmos-sdk/x/params)
- [API Docs](https://godoc.org/github.com/cosmos/cosmos-sdk/x/params)
- [Specification](https://github.com/cosmos/cosmos-sdk/blob/master/docs/spec/params)

## Evidence

The `x/evidence` modules provides a mechanism for defining and submitting arbitrary
events of misbehavior and a means to execute custom business logic for such misbehavior.

See the [specification](https://github.com/cosmos/cosmos-sdk/blob/master/docs/spec/params)
- [API Docs](https://godoc.org/github.com/cosmos/cosmos-sdk/x/evidence)
- [Specification](https://github.com/cosmos/cosmos-sdk/blob/master/docs/spec/evidence)
114 changes: 82 additions & 32 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/crisis"
distr "github.com/cosmos/cosmos-sdk/x/distribution"
"github.com/cosmos/cosmos-sdk/x/evidence"
"github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/mint"
Expand Down Expand Up @@ -54,6 +55,7 @@ var (
params.AppModuleBasic{},
crisis.AppModuleBasic{},
slashing.AppModuleBasic{},
evidence.AppModuleBasic{},
)

// module account permissions
Expand Down Expand Up @@ -90,6 +92,9 @@ type SimApp struct {
keys map[string]*sdk.KVStoreKey
tkeys map[string]*sdk.TransientStoreKey

// subspaces
subspaces map[string]params.Subspace

// keepers
AccountKeeper auth.AccountKeeper
BankKeeper bank.Keeper
Expand All @@ -101,6 +106,7 @@ type SimApp struct {
GovKeeper gov.Keeper
CrisisKeeper crisis.Keeper
ParamsKeeper params.Keeper
EvidenceKeeper evidence.Keeper

// the module manager
mm *module.Manager
Expand All @@ -121,9 +127,10 @@ func NewSimApp(
bApp.SetCommitMultiStoreTracer(traceStore)
bApp.SetAppVersion(version.Version)

keys := sdk.NewKVStoreKeys(bam.MainStoreKey, auth.StoreKey, staking.StoreKey,
supply.StoreKey, mint.StoreKey, distr.StoreKey, slashing.StoreKey,
gov.StoreKey, params.StoreKey)
keys := sdk.NewKVStoreKeys(
bam.MainStoreKey, auth.StoreKey, staking.StoreKey, supply.StoreKey, mint.StoreKey,
distr.StoreKey, slashing.StoreKey, gov.StoreKey, params.StoreKey, evidence.StoreKey,
)
tkeys := sdk.NewTransientStoreKeys(params.TStoreKey)

app := &SimApp{
Expand All @@ -132,39 +139,68 @@ func NewSimApp(
invCheckPeriod: invCheckPeriod,
keys: keys,
tkeys: tkeys,
subspaces: make(map[string]params.Subspace),
}

// init params keeper and subspaces
app.ParamsKeeper = params.NewKeeper(app.cdc, keys[params.StoreKey], tkeys[params.TStoreKey], params.DefaultCodespace)
authSubspace := app.ParamsKeeper.Subspace(auth.DefaultParamspace)
bankSubspace := app.ParamsKeeper.Subspace(bank.DefaultParamspace)
stakingSubspace := app.ParamsKeeper.Subspace(staking.DefaultParamspace)
mintSubspace := app.ParamsKeeper.Subspace(mint.DefaultParamspace)
distrSubspace := app.ParamsKeeper.Subspace(distr.DefaultParamspace)
slashingSubspace := app.ParamsKeeper.Subspace(slashing.DefaultParamspace)
govSubspace := app.ParamsKeeper.Subspace(gov.DefaultParamspace).WithKeyTable(gov.ParamKeyTable())
crisisSubspace := app.ParamsKeeper.Subspace(crisis.DefaultParamspace)
app.subspaces[auth.ModuleName] = app.ParamsKeeper.Subspace(auth.DefaultParamspace)
app.subspaces[bank.ModuleName] = app.ParamsKeeper.Subspace(bank.DefaultParamspace)
app.subspaces[staking.ModuleName] = app.ParamsKeeper.Subspace(staking.DefaultParamspace)
app.subspaces[mint.ModuleName] = app.ParamsKeeper.Subspace(mint.DefaultParamspace)
app.subspaces[distr.ModuleName] = app.ParamsKeeper.Subspace(distr.DefaultParamspace)
app.subspaces[slashing.ModuleName] = app.ParamsKeeper.Subspace(slashing.DefaultParamspace)
app.subspaces[gov.ModuleName] = app.ParamsKeeper.Subspace(gov.DefaultParamspace).WithKeyTable(gov.ParamKeyTable())
app.subspaces[crisis.ModuleName] = app.ParamsKeeper.Subspace(crisis.DefaultParamspace)
app.subspaces[evidence.ModuleName] = app.ParamsKeeper.Subspace(evidence.DefaultParamspace)

// add keepers
app.AccountKeeper = auth.NewAccountKeeper(app.cdc, keys[auth.StoreKey], authSubspace, auth.ProtoBaseAccount)
app.BankKeeper = bank.NewBaseKeeper(app.AccountKeeper, bankSubspace, bank.DefaultCodespace, app.ModuleAccountAddrs())
app.SupplyKeeper = supply.NewKeeper(app.cdc, keys[supply.StoreKey], app.AccountKeeper, app.BankKeeper, maccPerms)
stakingKeeper := staking.NewKeeper(app.cdc, keys[staking.StoreKey],
app.SupplyKeeper, stakingSubspace, staking.DefaultCodespace)
app.MintKeeper = mint.NewKeeper(app.cdc, keys[mint.StoreKey], mintSubspace, &stakingKeeper, app.SupplyKeeper, auth.FeeCollectorName)
app.DistrKeeper = distr.NewKeeper(app.cdc, keys[distr.StoreKey], distrSubspace, &stakingKeeper,
app.SupplyKeeper, distr.DefaultCodespace, auth.FeeCollectorName, app.ModuleAccountAddrs())
app.SlashingKeeper = slashing.NewKeeper(app.cdc, keys[slashing.StoreKey], &stakingKeeper,
slashingSubspace, slashing.DefaultCodespace)
app.CrisisKeeper = crisis.NewKeeper(crisisSubspace, invCheckPeriod, app.SupplyKeeper, auth.FeeCollectorName)
app.AccountKeeper = auth.NewAccountKeeper(
app.cdc, keys[auth.StoreKey], app.subspaces[auth.ModuleName], auth.ProtoBaseAccount,
)
app.BankKeeper = bank.NewBaseKeeper(
app.AccountKeeper, app.subspaces[bank.ModuleName], bank.DefaultCodespace,
app.ModuleAccountAddrs(),
)
app.SupplyKeeper = supply.NewKeeper(
app.cdc, keys[supply.StoreKey], app.AccountKeeper, app.BankKeeper, maccPerms,
)
stakingKeeper := staking.NewKeeper(
app.cdc, keys[staking.StoreKey], app.SupplyKeeper, app.subspaces[staking.ModuleName],
staking.DefaultCodespace)
app.MintKeeper = mint.NewKeeper(
app.cdc, keys[mint.StoreKey], app.subspaces[mint.ModuleName], &stakingKeeper,
app.SupplyKeeper, auth.FeeCollectorName,
)
app.DistrKeeper = distr.NewKeeper(
app.cdc, keys[distr.StoreKey], app.subspaces[distr.ModuleName], &stakingKeeper,
app.SupplyKeeper, distr.DefaultCodespace, auth.FeeCollectorName, app.ModuleAccountAddrs(),
)
app.SlashingKeeper = slashing.NewKeeper(
app.cdc, keys[slashing.StoreKey], &stakingKeeper, app.subspaces[slashing.ModuleName], slashing.DefaultCodespace,
)
app.CrisisKeeper = crisis.NewKeeper(
app.subspaces[crisis.ModuleName], invCheckPeriod, app.SupplyKeeper, auth.FeeCollectorName,
)

// create evidence keeper with router
evidenceKeeper := evidence.NewKeeper(
app.cdc, keys[evidence.StoreKey], app.subspaces[evidence.ModuleName], evidence.DefaultCodespace,
)
evidenceRouter := evidence.NewRouter()
// TODO: Register evidence routes.
evidenceKeeper.SetRouter(evidenceRouter)
app.EvidenceKeeper = *evidenceKeeper

// register the proposal types
govRouter := gov.NewRouter()
govRouter.AddRoute(gov.RouterKey, gov.ProposalHandler).
AddRoute(params.RouterKey, params.NewParamChangeProposalHandler(app.ParamsKeeper)).
AddRoute(distr.RouterKey, distr.NewCommunityPoolSpendProposalHandler(app.DistrKeeper))
app.GovKeeper = gov.NewKeeper(app.cdc, keys[gov.StoreKey], govSubspace,
app.SupplyKeeper, &stakingKeeper, gov.DefaultCodespace, govRouter)
app.GovKeeper = gov.NewKeeper(
app.cdc, keys[gov.StoreKey], app.subspaces[gov.ModuleName], app.SupplyKeeper,
&stakingKeeper, gov.DefaultCodespace, govRouter,
)

// register the staking hooks
// NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks
Expand All @@ -185,22 +221,21 @@ func NewSimApp(
distr.NewAppModule(app.DistrKeeper, app.SupplyKeeper),
slashing.NewAppModule(app.SlashingKeeper, app.StakingKeeper),
staking.NewAppModule(app.StakingKeeper, app.AccountKeeper, app.SupplyKeeper),
evidence.NewAppModule(app.EvidenceKeeper),
)

// During begin block slashing happens after distr.BeginBlocker so that
// there is nothing left over in the validator fee pool, so as to keep the
// CanWithdrawInvariant invariant.
app.mm.SetOrderBeginBlockers(mint.ModuleName, distr.ModuleName, slashing.ModuleName)

app.mm.SetOrderEndBlockers(crisis.ModuleName, gov.ModuleName, staking.ModuleName)

// NOTE: The genutils moodule must occur after staking so that pools are
// properly initialized with tokens from genesis accounts.
app.mm.SetOrderInitGenesis(
auth.ModuleName, distr.ModuleName, staking.ModuleName,
bank.ModuleName, slashing.ModuleName, gov.ModuleName,
mint.ModuleName, supply.ModuleName, crisis.ModuleName,
genutil.ModuleName,
auth.ModuleName, distr.ModuleName, staking.ModuleName, bank.ModuleName,
slashing.ModuleName, gov.ModuleName, mint.ModuleName, supply.ModuleName,
crisis.ModuleName, genutil.ModuleName, evidence.ModuleName,
)

app.mm.RegisterInvariants(&app.CrisisKeeper)
Expand Down Expand Up @@ -239,6 +274,7 @@ func NewSimApp(
cmn.Exit(err.Error())
}
}

return app
}

Expand Down Expand Up @@ -274,21 +310,35 @@ func (app *SimApp) ModuleAccountAddrs() map[string]bool {
return modAccAddrs
}

// Codec returns simapp's codec
// Codec returns SimApp's codec.
//
// NOTE: This is solely to be used for testing purposes as it may be desirable
// for modules to register their own custom testing types.
func (app *SimApp) Codec() *codec.Codec {
return app.cdc
}

// GetKey returns the KVStoreKey for the provided store key
// GetKey returns the KVStoreKey for the provided store key.
//
// NOTE: This is solely to be used for testing purposes.
func (app *SimApp) GetKey(storeKey string) *sdk.KVStoreKey {
return app.keys[storeKey]
}

// GetTKey returns the TransientStoreKey for the provided store key
// GetTKey returns the TransientStoreKey for the provided store key.
//
// NOTE: This is solely to be used for testing purposes.
func (app *SimApp) GetTKey(storeKey string) *sdk.TransientStoreKey {
return app.tkeys[storeKey]
}

// GetSubspace returns a param subspace for a given module name.
//
// NOTE: This is solely to be used for testing purposes.
func (app *SimApp) GetSubspace(moduleName string) params.Subspace {
return app.subspaces[moduleName]
}

// GetMaccPerms returns a copy of the module account permissions
func GetMaccPerms() map[string][]string {
dupMaccPerms := make(map[string][]string)
Expand Down
Loading

0 comments on commit 95ddc24

Please sign in to comment.