Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R: Add Stake Genesis Intra-Tx Counter #1724

Merged
merged 7 commits into from
Jul 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we set this value right away so its consistent ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only the power store key uses this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ebuchman as in set it in the genesis file as opposed to in this ordering? Yeah could be done, might be better to keep it more explicit. But then again. who get's to decide the order! I almost think for that organizational reason it should be set where it is currently.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still effectively set by the genesis file though, since we iterate through the validator array.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I guess it's alphabetical - honestly I don't think this is a real concern it's an extreme edge case, we just need something in there for key collision

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
}