Skip to content

Commit

Permalink
Merge PR cosmos#1724: R4R: Add Stake Genesis Intra-Tx Counter
Browse files Browse the repository at this point in the history
* add revoked to human-readable validator
* changelog
* added failing test
* add intra-tx counter to the genesis validators
* changelog
  • Loading branch information
rigelrozanski authored and jaekwon committed Jul 26, 2018
1 parent 3dcec8a commit 46abe9f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 34 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ IMPROVEMENTS
* [cli] Improve error messages for all txs when the account doesn't exist
* [tendermint] Update to v0.22.6
- Updates the crypto imports/API (#1966)
* [x/stake] Add revoked to human-readable validator

BUG FIXES
* [tendermint] Update to v0.22.6
- Fixes some security vulnerabilities reported in the [Bug Bounty](https://hackerone.com/tendermint)
* \#1797 Fix off-by-one error in slashing for downtime
* \#1787 Fixed bug where Tally fails due to revoked/unbonding validator
* \#1666 Add intra-tx counter to the genesis validators

## 0.22.0

Expand Down
4 changes: 0 additions & 4 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ IMPROVEMENTS
* [baseapp] Allow any alphanumeric character in route
* [tools] Remove `rm -rf vendor/` from `make get_vendor_deps`
* [x/auth] Recover ErrorOutOfGas panic in order to set sdk.Result attributes correctly
* [x/stake] Add revoked to human-readable validator
* [tests] Add tests to example apps in docs
* [x/gov] Votes on a proposal can now be queried
* [x/bank] Unit tests are now table-driven
* [tests] Fixes ansible scripts to work with AWS too

BUG FIXES
* \#1666 Add intra-tx counter to the genesis validators
4 changes: 3 additions & 1 deletion x/stake/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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 {
Expand Down
16 changes: 14 additions & 2 deletions x/stake/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
55 changes: 28 additions & 27 deletions x/stake/types/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 46abe9f

Please sign in to comment.