diff --git a/CHANGELOG.md b/CHANGELOG.md index c5b25f03e..3e0016324 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -113,6 +113,7 @@ to detail this new feature and how state transitions occur. * (cli) [\#4763](https://github.com/cosmos/cosmos-sdk/issues/4763) Fix flag `--min-self-delegation` for staking `EditValidator` * (keys) Fix ledger custom coin type support bug +* (genesis) [\#5095](https://github.com/cosmos/cosmos-sdk/issues/5095) Fix genesis file migration from v0.34 to v0.36 not converting validator consensus pubkey to bech32 format ## [v0.37.1] - 2019-09-19 diff --git a/x/staking/legacy/v0_36/types.go b/x/staking/legacy/v0_36/types.go index e58422804..b5edcc048 100644 --- a/x/staking/legacy/v0_36/types.go +++ b/x/staking/legacy/v0_36/types.go @@ -5,10 +5,10 @@ package v0_36 import ( "time" - "github.com/tendermint/tendermint/crypto" - + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" v034staking "github.com/cosmos/cosmos-sdk/x/staking/legacy/v0_34" + "github.com/tendermint/tendermint/crypto" ) const ( @@ -41,6 +41,20 @@ type ( MinSelfDelegation sdk.Int `json:"min_self_delegation" yaml:"min_self_delegation"` } + bechValidator struct { + OperatorAddress sdk.ValAddress `json:"operator_address" yaml:"operator_address"` + ConsPubKey string `json:"consensus_pubkey" yaml:"consensus_pubkey"` + Jailed bool `json:"jailed" yaml:"jailed"` + Status sdk.BondStatus `json:"status" yaml:"status"` + Tokens sdk.Int `json:"tokens" yaml:"tokens"` + DelegatorShares sdk.Dec `json:"delegator_shares" yaml:"delegator_shares"` + Description v034staking.Description `json:"description" yaml:"description"` + UnbondingHeight int64 `json:"unbonding_height" yaml:"unbonding_height"` + UnbondingCompletionTime time.Time `json:"unbonding_time" yaml:"unbonding_time"` + Commission Commission `json:"commission" yaml:"commission"` + MinSelfDelegation sdk.Int `json:"min_self_delegation" yaml:"min_self_delegation"` + } + Validators []Validator GenesisState struct { @@ -72,3 +86,49 @@ func NewGenesisState( Exported: exported, } } + +func (v Validator) MarshalJSON() ([]byte, error) { + bechConsPubKey, err := sdk.Bech32ifyConsPub(v.ConsPubKey) + if err != nil { + return nil, err + } + + return codec.Cdc.MarshalJSON(bechValidator{ + OperatorAddress: v.OperatorAddress, + ConsPubKey: bechConsPubKey, + Jailed: v.Jailed, + Status: v.Status, + Tokens: v.Tokens, + DelegatorShares: v.DelegatorShares, + Description: v.Description, + UnbondingHeight: v.UnbondingHeight, + UnbondingCompletionTime: v.UnbondingCompletionTime, + MinSelfDelegation: v.MinSelfDelegation, + Commission: v.Commission, + }) +} + +func (v *Validator) UnmarshalJSON(data []byte) error { + bv := &bechValidator{} + if err := codec.Cdc.UnmarshalJSON(data, bv); err != nil { + return err + } + consPubKey, err := sdk.GetConsPubKeyBech32(bv.ConsPubKey) + if err != nil { + return err + } + *v = Validator{ + OperatorAddress: bv.OperatorAddress, + ConsPubKey: consPubKey, + Jailed: bv.Jailed, + Tokens: bv.Tokens, + Status: bv.Status, + DelegatorShares: bv.DelegatorShares, + Description: bv.Description, + UnbondingHeight: bv.UnbondingHeight, + UnbondingCompletionTime: bv.UnbondingCompletionTime, + Commission: bv.Commission, + MinSelfDelegation: bv.MinSelfDelegation, + } + return nil +} diff --git a/x/staking/legacy/v0_37/types.go b/x/staking/legacy/v0_37/types.go index ae1323fe9..a293367c8 100644 --- a/x/staking/legacy/v0_37/types.go +++ b/x/staking/legacy/v0_37/types.go @@ -5,6 +5,7 @@ package v0_37 import ( "time" + "github.com/cosmos/cosmos-sdk/codec" "github.com/tendermint/tendermint/crypto" sdk "github.com/cosmos/cosmos-sdk/types" @@ -39,6 +40,20 @@ type ( MinSelfDelegation sdk.Int `json:"min_self_delegation" yaml:"min_self_delegation"` } + bechValidator struct { + OperatorAddress sdk.ValAddress `json:"operator_address" yaml:"operator_address"` + ConsPubKey string `json:"consensus_pubkey" yaml:"consensus_pubkey"` + Jailed bool `json:"jailed" yaml:"jailed"` + Status sdk.BondStatus `json:"status" yaml:"status"` + Tokens sdk.Int `json:"tokens" yaml:"tokens"` + DelegatorShares sdk.Dec `json:"delegator_shares" yaml:"delegator_shares"` + Description Description `json:"description" yaml:"description"` + UnbondingHeight int64 `json:"unbonding_height" yaml:"unbonding_height"` + UnbondingCompletionTime time.Time `json:"unbonding_time" yaml:"unbonding_time"` + Commission v036staking.Commission `json:"commission" yaml:"commission"` + MinSelfDelegation sdk.Int `json:"min_self_delegation" yaml:"min_self_delegation"` + } + Validators []Validator GenesisState struct { @@ -84,3 +99,51 @@ func NewGenesisState( Exported: exported, } } + +// MarshalJSON marshals the validator to JSON using Bech32 +func (v Validator) MarshalJSON() ([]byte, error) { + bechConsPubKey, err := sdk.Bech32ifyConsPub(v.ConsPubKey) + if err != nil { + return nil, err + } + + return codec.Cdc.MarshalJSON(bechValidator{ + OperatorAddress: v.OperatorAddress, + ConsPubKey: bechConsPubKey, + Jailed: v.Jailed, + Status: v.Status, + Tokens: v.Tokens, + DelegatorShares: v.DelegatorShares, + Description: v.Description, + UnbondingHeight: v.UnbondingHeight, + UnbondingCompletionTime: v.UnbondingCompletionTime, + MinSelfDelegation: v.MinSelfDelegation, + Commission: v.Commission, + }) +} + +// UnmarshalJSON unmarshals the validator from JSON using Bech32 +func (v *Validator) UnmarshalJSON(data []byte) error { + bv := &bechValidator{} + if err := codec.Cdc.UnmarshalJSON(data, bv); err != nil { + return err + } + consPubKey, err := sdk.GetConsPubKeyBech32(bv.ConsPubKey) + if err != nil { + return err + } + *v = Validator{ + OperatorAddress: bv.OperatorAddress, + ConsPubKey: consPubKey, + Jailed: bv.Jailed, + Tokens: bv.Tokens, + Status: bv.Status, + DelegatorShares: bv.DelegatorShares, + Description: bv.Description, + UnbondingHeight: bv.UnbondingHeight, + UnbondingCompletionTime: bv.UnbondingCompletionTime, + Commission: bv.Commission, + MinSelfDelegation: bv.MinSelfDelegation, + } + return nil +}