From cfe78027f22fdc9b96e43b8671af07a2e11e0f31 Mon Sep 17 00:00:00 2001 From: Rigel Date: Wed, 18 Jul 2018 16:09:40 -0400 Subject: [PATCH] Merge PR #1724: R4R: Add Stake Genesis Intra-Tx Counter * add revoked to human-readable validator * changelog * added failing test * add intra-tx counter to the genesis validators * changelog --- CHANGELOG.md | 2 ++ x/stake/genesis.go | 4 ++- x/stake/genesis_test.go | 16 +++++++++-- x/stake/types/validator.go | 55 +++++++++++++++++++------------------- 4 files changed, 47 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f8fc927c9..fc69ff9d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,10 +10,12 @@ FEATURES IMPROVEMENTS * [baseapp] Allow any alphanumeric character in route +* [x/stake] Add revoked to human-readable validator * [cli] Improve error messages for all txs when the account doesn't exist * [tools] Remove `rm -rf vendor/` from `make get_vendor_deps` BUG FIXES +* \#1666 Add intra-tx counter to the genesis validators ## 0.22.0 diff --git a/x/stake/genesis.go b/x/stake/genesis.go index e54517fa5..b4ed80e51 100644 --- a/x/stake/genesis.go +++ b/x/stake/genesis.go @@ -17,7 +17,7 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) error keeper.SetNewParams(ctx, data.Params) keeper.InitIntraTxCounter(ctx) - for _, validator := range data.Validators { + for i, validator := range data.Validators { keeper.SetValidator(ctx, validator) if validator.Tokens.IsZero() { @@ -29,6 +29,8 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) error // Manually set indexes for the first time keeper.SetValidatorByPubKeyIndex(ctx, validator) + + validator.BondIntraTxCounter = int16(i) // set the intra-tx counter to the order the validators are presented keeper.SetValidatorByPowerIndex(ctx, validator, data.Pool) if validator.Status == sdk.Bonded { diff --git a/x/stake/genesis_test.go b/x/stake/genesis_test.go index 2faff5bc0..e27c7fed2 100644 --- a/x/stake/genesis_test.go +++ b/x/stake/genesis_test.go @@ -14,23 +14,35 @@ func TestInitGenesis(t *testing.T) { ctx, _, keeper := keep.CreateTestInput(t, false, 1000) pool := keeper.GetPool(ctx) - pool.LooseTokens = sdk.OneRat() + pool.LooseTokens = sdk.NewRat(2) params := keeper.GetParams(ctx) var delegations []Delegation validators := []Validator{ NewValidator(keep.Addrs[0], keep.PKs[0], Description{Moniker: "hoop"}), + NewValidator(keep.Addrs[1], keep.PKs[1], Description{Moniker: "bloop"}), } - genesisState := types.NewGenesisState(pool, params, validators, delegations) err := InitGenesis(ctx, keeper, genesisState) require.Error(t, err) + // initialize the validators validators[0].Tokens = sdk.OneRat() validators[0].DelegatorShares = sdk.OneRat() + validators[1].Tokens = sdk.OneRat() + validators[1].DelegatorShares = sdk.OneRat() genesisState = types.NewGenesisState(pool, params, validators, delegations) err = InitGenesis(ctx, keeper, genesisState) require.NoError(t, err) + + // now make sure the validators are bonded + resVal, found := keeper.GetValidator(ctx, keep.Addrs[0]) + require.True(t, found) + require.Equal(t, sdk.Bonded, resVal.Status) + + resVal, found = keeper.GetValidator(ctx, keep.Addrs[1]) + require.True(t, found) + require.Equal(t, sdk.Bonded, resVal.Status) } diff --git a/x/stake/types/validator.go b/x/stake/types/validator.go index ed109830f..f177c123d 100644 --- a/x/stake/types/validator.go +++ b/x/stake/types/validator.go @@ -147,6 +147,34 @@ func UnmarshalValidator(cdc *wire.Codec, ownerAddr, value []byte) (validator Val }, nil } +// HumanReadableString returns a human readable string representation of a +// validator. An error is returned if the owner or the owner's public key +// cannot be converted to Bech32 format. +func (v Validator) HumanReadableString() (string, error) { + bechVal, err := sdk.Bech32ifyValPub(v.PubKey) + if err != nil { + return "", err + } + + resp := "Validator \n" + resp += fmt.Sprintf("Owner: %s\n", v.Owner) + resp += fmt.Sprintf("Validator: %s\n", bechVal) + resp += fmt.Sprintf("Revoked: %v\n", v.Revoked) + resp += fmt.Sprintf("Status: %s\n", sdk.BondStatusToString(v.Status)) + resp += fmt.Sprintf("Tokens: %s\n", v.Tokens.FloatString()) + resp += fmt.Sprintf("Delegator Shares: %s\n", v.DelegatorShares.FloatString()) + resp += fmt.Sprintf("Description: %s\n", v.Description) + resp += fmt.Sprintf("Bond Height: %d\n", v.BondHeight) + resp += fmt.Sprintf("Proposer Reward Pool: %s\n", v.ProposerRewardPool.String()) + resp += fmt.Sprintf("Commission: %s\n", v.Commission.String()) + resp += fmt.Sprintf("Max Commission Rate: %s\n", v.CommissionMax.String()) + resp += fmt.Sprintf("Commission Change Rate: %s\n", v.CommissionChangeRate.String()) + resp += fmt.Sprintf("Commission Change Today: %s\n", v.CommissionChangeToday.String()) + resp += fmt.Sprintf("Previous Bonded Tokens: %s\n", v.LastBondedTokens.String()) + + return resp, nil +} + //___________________________________________________________________ // validator struct for bech output @@ -408,30 +436,3 @@ func (v Validator) GetPubKey() crypto.PubKey { return v.PubKey } func (v Validator) GetPower() sdk.Rat { return v.BondedTokens() } func (v Validator) GetDelegatorShares() sdk.Rat { return v.DelegatorShares } func (v Validator) GetBondHeight() int64 { return v.BondHeight } - -// HumanReadableString returns a human readable string representation of a -// validator. An error is returned if the owner or the owner's public key -// cannot be converted to Bech32 format. -func (v Validator) HumanReadableString() (string, error) { - bechVal, err := sdk.Bech32ifyValPub(v.PubKey) - if err != nil { - return "", err - } - - resp := "Validator \n" - resp += fmt.Sprintf("Owner: %s\n", v.Owner) - resp += fmt.Sprintf("Validator: %s\n", bechVal) - resp += fmt.Sprintf("Status: %s\n", sdk.BondStatusToString(v.Status)) - resp += fmt.Sprintf("Tokens: %s\n", v.Tokens.FloatString()) - resp += fmt.Sprintf("Delegator Shares: %s\n", v.DelegatorShares.FloatString()) - resp += fmt.Sprintf("Description: %s\n", v.Description) - resp += fmt.Sprintf("Bond Height: %d\n", v.BondHeight) - resp += fmt.Sprintf("Proposer Reward Pool: %s\n", v.ProposerRewardPool.String()) - resp += fmt.Sprintf("Commission: %s\n", v.Commission.String()) - resp += fmt.Sprintf("Max Commission Rate: %s\n", v.CommissionMax.String()) - resp += fmt.Sprintf("Commission Change Rate: %s\n", v.CommissionChangeRate.String()) - resp += fmt.Sprintf("Commission Change Today: %s\n", v.CommissionChangeToday.String()) - resp += fmt.Sprintf("Previous Bonded Tokens: %s\n", v.LastBondedTokens.String()) - - return resp, nil -}