Merge PR #3010: Add Missing genesis checks in Gaia

This commit is contained in:
frog power 4000 2018-12-06 19:22:24 -05:00 committed by Christopher Goes
parent f11a65dee7
commit 49da96bc09
9 changed files with 104 additions and 14 deletions

View File

@ -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

View File

@ -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"
@ -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 {

View File

@ -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]
@ -178,7 +200,7 @@ func validateGenesisStateAccounts(accs []GenesisAccount) (err error) {
}
addrMap[strAddr] = true
}
return
return nil
}
// GaiaAppGenState but with JSON

View File

@ -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,
)
}

View File

@ -49,6 +49,7 @@ var (
InitialFeePool = types.InitialFeePool
NewGenesisState = types.NewGenesisState
ValidateGenesis = types.ValidateGenesis
DefaultGenesisState = types.DefaultGenesisState
DefaultGenesisWithValidators = types.DefaultGenesisWithValidators

View File

@ -1,6 +1,8 @@
package types
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
)
@ -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
}

View File

@ -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
@ -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()
}

View File

@ -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)

View File

@ -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) {