Skip to content

Commit

Permalink
Merge PR cosmos#3010: Add Missing genesis checks in Gaia
Browse files Browse the repository at this point in the history
  • Loading branch information
rigelrozanski authored and cwgoes committed Dec 7, 2018
1 parent f11a65d commit 49da96b
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 14 deletions.
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ BREAKING CHANGES
* Gaia
- [#128](https://github.com/tendermint/devops/issues/128) Updated CircleCI job to trigger website build on every push to master/develop.
- [\#2994](https://github.com/cosmos/cosmos-sdk/pull/2994) Change wrong-password error message.
- \#3009 Added missing Gaia genesis verification

* SDK
- [auth] \#2952 Signatures are no longer serialized on chain with the account number and sequence number
Expand Down
17 changes: 11 additions & 6 deletions cmd/gaia/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/x/auth"
distr "github.com/cosmos/cosmos-sdk/x/distribution"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/stake"
"github.com/stretchr/testify/require"
Expand All @@ -22,12 +24,15 @@ func setGenesis(gapp *GaiaApp, accs ...*auth.BaseAccount) error {
genaccs[i] = NewGenesisAccount(acc)
}

genesisState := GenesisState{
Accounts: genaccs,
StakeData: stake.DefaultGenesisState(),
DistrData: distr.DefaultGenesisState(),
SlashingData: slashing.DefaultGenesisState(),
}
genesisState := NewGenesisState(
genaccs,
auth.DefaultGenesisState(),
stake.DefaultGenesisState(),
mint.DefaultGenesisState(),
distr.DefaultGenesisState(),
gov.DefaultGenesisState(),
slashing.DefaultGenesisState(),
)

stateBytes, err := codec.MarshalJSONIndent(gapp.cdc, genesisState)
if err != nil {
Expand Down
34 changes: 28 additions & 6 deletions cmd/gaia/app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,42 @@ func NewDefaultGenesisState() GenesisState {
// TODO: No validators are both bonded and jailed (#2088)
// TODO: Error if there is a duplicate validator (#1708)
// TODO: Ensure all state machine parameters are in genesis (#1704)
func GaiaValidateGenesisState(genesisState GenesisState) (err error) {
err = validateGenesisStateAccounts(genesisState.Accounts)
func GaiaValidateGenesisState(genesisState GenesisState) error {
err := validateGenesisStateAccounts(genesisState.Accounts)
if err != nil {
return
return err
}
// skip stakeData validation as genesis is created from txs
if len(genesisState.GenTxs) > 0 {
return nil
}
return stake.ValidateGenesis(genesisState.StakeData)

err = stake.ValidateGenesis(genesisState.StakeData)
if err != nil {
return err
}
err = mint.ValidateGenesis(genesisState.MintData)
if err != nil {
return err
}
err = distr.ValidateGenesis(genesisState.DistrData)
if err != nil {
return err
}
err = gov.ValidateGenesis(genesisState.GovData)
if err != nil {
return err
}
err = slashing.ValidateGenesis(genesisState.SlashingData)
if err != nil {
return err
}

return nil
}

// Ensures that there are no duplicate accounts in the genesis state,
func validateGenesisStateAccounts(accs []GenesisAccount) (err error) {
func validateGenesisStateAccounts(accs []GenesisAccount) error {
addrMap := make(map[string]bool, len(accs))
for i := 0; i < len(accs); i++ {
acc := accs[i]
Expand All @@ -178,7 +200,7 @@ func validateGenesisStateAccounts(accs []GenesisAccount) (err error) {
}
addrMap[strAddr] = true
}
return
return nil
}

// GaiaAppGenState but with JSON
Expand Down
2 changes: 1 addition & 1 deletion cmd/gaia/init/gentx.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func accountInGenesis(genesisState app.GenesisState, key sdk.AccAddress, coins s
// Ensure account contains enough funds of default bond denom
if coins.AmountOf(bondDenom).GT(acc.Coins.AmountOf(bondDenom)) {
return fmt.Errorf(
"Account %s is in genesis, but the only has %s%s available to stake, not %s%s",
"Account %v is in genesis, but the only has %v%v available to stake, not %v%v",
key, acc.Coins.AmountOf(bondDenom), bondDenom, coins.AmountOf(bondDenom), bondDenom,
)
}
Expand Down
1 change: 1 addition & 0 deletions x/distribution/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ var (
InitialFeePool = types.InitialFeePool

NewGenesisState = types.NewGenesisState
ValidateGenesis = types.ValidateGenesis
DefaultGenesisState = types.DefaultGenesisState
DefaultGenesisWithValidators = types.DefaultGenesisWithValidators

Expand Down
24 changes: 24 additions & 0 deletions x/distribution/types/fee_pool.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand Down Expand Up @@ -31,3 +33,25 @@ func InitialFeePool() FeePool {
CommunityPool: DecCoins{},
}
}

// ValidateGenesis validates the fee pool for a genesis state
func (f FeePool) ValidateGenesis() error {
if f.TotalValAccum.Accum.IsNegative() {
return fmt.Errorf("negative accum in distribution fee pool, is %v",
f.TotalValAccum.Accum.String())
}
if f.TotalValAccum.UpdateHeight < 0 {
return fmt.Errorf("negative update height in distribution fee pool, is %v",
f.TotalValAccum.UpdateHeight)
}
if f.ValPool.HasNegative() {
return fmt.Errorf("negative ValPool in distribution fee pool, is %v",
f.ValPool)
}
if f.CommunityPool.HasNegative() {
return fmt.Errorf("negative CommunityPool in distribution fee pool, is %v",
f.CommunityPool)
}

return nil
}
29 changes: 28 additions & 1 deletion x/distribution/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package types

import sdk "github.com/cosmos/cosmos-sdk/types"
import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
)

// the address for where distributions rewards are withdrawn to by default
// this struct is only used at genesis to feed in default withdraw addresses
Expand Down Expand Up @@ -67,3 +71,26 @@ func DefaultGenesisWithValidators(valAddrs []sdk.ValAddress) GenesisState {
DelegationDistInfos: ddis,
}
}

// ValidateGenesis validates the genesis state of distribution genesis input
func ValidateGenesis(data GenesisState) error {
if data.CommunityTax.IsNegative() || data.CommunityTax.GT(sdk.OneDec()) {
return fmt.Errorf("mint parameter CommunityTax should non-negative and "+
"less than one, is %s", data.CommunityTax.String())
}
if data.BaseProposerReward.IsNegative() {
return fmt.Errorf("mint parameter BaseProposerReward should be positive, is %s",
data.BaseProposerReward.String())
}
if data.BonusProposerReward.IsNegative() {
return fmt.Errorf("mint parameter BonusProposerReward should be positive, is %s",
data.BonusProposerReward.String())
}
if (data.BaseProposerReward.Add(data.BonusProposerReward)).
GT(sdk.OneDec()) {
return fmt.Errorf("mint parameters BaseProposerReward and "+
"BonusProposerReward cannot add to be greater than one, "+
"adds to %s", data.BaseProposerReward.Add(data.BonusProposerReward).String())
}
return data.FeePool.ValidateGenesis()
}
5 changes: 5 additions & 0 deletions x/gov/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ func DefaultGenesisState() GenesisState {
}
}

// ValidateGenesis TODO https://github.com/cosmos/cosmos-sdk/issues/3007
func ValidateGenesis(data GenesisState) error {
return nil
}

// InitGenesis - store genesis parameters
func InitGenesis(ctx sdk.Context, k Keeper, data GenesisState) {
err := k.setInitialProposalID(ctx, data.StartingProposalID)
Expand Down
5 changes: 5 additions & 0 deletions x/slashing/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func DefaultGenesisState() GenesisState {
}
}

// ValidateGenesis TODO https://github.com/cosmos/cosmos-sdk/issues/3008
func ValidateGenesis(data GenesisState) error {
return nil
}

// InitGenesis initialize default parameters
// and the keeper's address to pubkey map
func InitGenesis(ctx sdk.Context, keeper Keeper, data GenesisState, sdata types.GenesisState) {
Expand Down

0 comments on commit 49da96b

Please sign in to comment.