Skip to content

Commit

Permalink
Merge PR #2394: Split up UpdateValidator into distinct state transiti…
Browse files Browse the repository at this point in the history
…ons applied only in EndBlock
  • Loading branch information
rigelrozanski authored and cwgoes committed Oct 3, 2018
1 parent a7fabbe commit 324bdaf
Show file tree
Hide file tree
Showing 24 changed files with 855 additions and 1,123 deletions.
16 changes: 8 additions & 8 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ BREAKING CHANGES
* [x/auth] \#2377 auth.StdSignMsg -> txbuilder.StdSignMsg
* [x/staking] \#2244 staking now holds a consensus-address-index instead of a consensus-pubkey-index
* [x/staking] \#2236 more distribution hooks for distribution
* [x/stake] \#2394 Split up UpdateValidator into distinct state transitions applied only in EndBlock

* Tendermint

Expand Down
16 changes: 16 additions & 0 deletions x/gov/tally_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func TestTallyNoOneVotes(t *testing.T) {
}

createValidators(t, stakeHandler, ctx, valAddrs, []int64{5, 5})
stake.EndBlocker(ctx, sk)

proposal := keeper.NewTextProposal(ctx, "Test", "description", ProposalTypeText)
proposalID := proposal.GetProposalID()
Expand All @@ -69,6 +70,7 @@ func TestTallyOnlyValidatorsAllYes(t *testing.T) {
}

createValidators(t, stakeHandler, ctx, valAddrs, []int64{5, 5})
stake.EndBlocker(ctx, sk)

proposal := keeper.NewTextProposal(ctx, "Test", "description", ProposalTypeText)
proposalID := proposal.GetProposalID()
Expand Down Expand Up @@ -98,6 +100,7 @@ func TestTallyOnlyValidators51No(t *testing.T) {
}

createValidators(t, stakeHandler, ctx, valAddrs, []int64{5, 6})
stake.EndBlocker(ctx, sk)

proposal := keeper.NewTextProposal(ctx, "Test", "description", ProposalTypeText)
proposalID := proposal.GetProposalID()
Expand Down Expand Up @@ -126,6 +129,7 @@ func TestTallyOnlyValidators51Yes(t *testing.T) {
}

createValidators(t, stakeHandler, ctx, valAddrs, []int64{6, 6, 7})
stake.EndBlocker(ctx, sk)

proposal := keeper.NewTextProposal(ctx, "Test", "description", ProposalTypeText)
proposalID := proposal.GetProposalID()
Expand Down Expand Up @@ -157,6 +161,7 @@ func TestTallyOnlyValidatorsVetoed(t *testing.T) {
}

createValidators(t, stakeHandler, ctx, valAddrs, []int64{6, 6, 7})
stake.EndBlocker(ctx, sk)

proposal := keeper.NewTextProposal(ctx, "Test", "description", ProposalTypeText)
proposalID := proposal.GetProposalID()
Expand Down Expand Up @@ -188,6 +193,7 @@ func TestTallyOnlyValidatorsAbstainPasses(t *testing.T) {
}

createValidators(t, stakeHandler, ctx, valAddrs, []int64{6, 6, 7})
stake.EndBlocker(ctx, sk)

proposal := keeper.NewTextProposal(ctx, "Test", "description", ProposalTypeText)
proposalID := proposal.GetProposalID()
Expand Down Expand Up @@ -219,6 +225,7 @@ func TestTallyOnlyValidatorsAbstainFails(t *testing.T) {
}

createValidators(t, stakeHandler, ctx, valAddrs, []int64{6, 6, 7})
stake.EndBlocker(ctx, sk)

proposal := keeper.NewTextProposal(ctx, "Test", "description", ProposalTypeText)
proposalID := proposal.GetProposalID()
Expand Down Expand Up @@ -250,6 +257,7 @@ func TestTallyOnlyValidatorsNonVoter(t *testing.T) {
}

createValidators(t, stakeHandler, ctx, valAddrs, []int64{6, 6, 7})
stake.EndBlocker(ctx, sk)

proposal := keeper.NewTextProposal(ctx, "Test", "description", ProposalTypeText)
proposalID := proposal.GetProposalID()
Expand Down Expand Up @@ -279,6 +287,7 @@ func TestTallyDelgatorOverride(t *testing.T) {
}

createValidators(t, stakeHandler, ctx, valAddrs, []int64{5, 6, 7})
stake.EndBlocker(ctx, sk)

delegator1Msg := stake.NewMsgDelegate(addrs[3], sdk.ValAddress(addrs[2]), sdk.NewInt64Coin("steak", 30))
stakeHandler(ctx, delegator1Msg)
Expand Down Expand Up @@ -315,6 +324,7 @@ func TestTallyDelgatorInherit(t *testing.T) {
}

createValidators(t, stakeHandler, ctx, valAddrs, []int64{5, 6, 7})
stake.EndBlocker(ctx, sk)

delegator1Msg := stake.NewMsgDelegate(addrs[3], sdk.ValAddress(addrs[2]), sdk.NewInt64Coin("steak", 30))
stakeHandler(ctx, delegator1Msg)
Expand Down Expand Up @@ -349,6 +359,7 @@ func TestTallyDelgatorMultipleOverride(t *testing.T) {
}

createValidators(t, stakeHandler, ctx, valAddrs, []int64{5, 6, 7})
stake.EndBlocker(ctx, sk)

delegator1Msg := stake.NewMsgDelegate(addrs[3], sdk.ValAddress(addrs[2]), sdk.NewInt64Coin("steak", 10))
stakeHandler(ctx, delegator1Msg)
Expand Down Expand Up @@ -402,6 +413,8 @@ func TestTallyDelgatorMultipleInherit(t *testing.T) {
delegator1Msg2 := stake.NewMsgDelegate(addrs[3], sdk.ValAddress(addrs[1]), sdk.NewInt64Coin("steak", 10))
stakeHandler(ctx, delegator1Msg2)

stake.EndBlocker(ctx, sk)

proposal := keeper.NewTextProposal(ctx, "Test", "description", ProposalTypeText)
proposalID := proposal.GetProposalID()
proposal.SetStatus(StatusVotingPeriod)
Expand Down Expand Up @@ -432,6 +445,7 @@ func TestTallyJailedValidator(t *testing.T) {
}

createValidators(t, stakeHandler, ctx, valAddrs, []int64{25, 6, 7})
stake.EndBlocker(ctx, sk)

delegator1Msg := stake.NewMsgDelegate(addrs[3], sdk.ValAddress(addrs[2]), sdk.NewInt64Coin("steak", 10))
stakeHandler(ctx, delegator1Msg)
Expand All @@ -443,6 +457,8 @@ func TestTallyJailedValidator(t *testing.T) {
require.True(t, found)
sk.Jail(ctx, sdk.ConsAddress(val2.ConsPubKey.Address()))

stake.EndBlocker(ctx, sk)

proposal := keeper.NewTextProposal(ctx, "Test", "description", ProposalTypeText)
proposalID := proposal.GetProposalID()
proposal.SetStatus(StatusVotingPeriod)
Expand Down
17 changes: 7 additions & 10 deletions x/mock/simulation/random_simulate_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,8 @@ func initChain(r *rand.Rand, accounts []Account, setups []RandSetup, app *baseap
res := app.InitChain(abci.RequestInitChain{AppStateBytes: appStateFn(r, accounts)})
validators = make(map[string]mockValidator)
for _, validator := range res.Validators {
pubkey, err := tmtypes.PB2TM.PubKey(validator.PubKey)
if err != nil {
panic(err)
}
address := pubkey.Address()
validators[string(address)] = mockValidator{validator, GetMemberOfInitialState(r, initialLivenessWeightings)}
str := fmt.Sprintf("%v", validator.PubKey)
validators[str] = mockValidator{validator, GetMemberOfInitialState(r, initialLivenessWeightings)}
}

for i := 0; i < len(setups); i++ {
Expand Down Expand Up @@ -400,22 +396,23 @@ func RandomRequestBeginBlock(r *rand.Rand, validators map[string]mockValidator,
func updateValidators(tb testing.TB, r *rand.Rand, current map[string]mockValidator, updates []abci.ValidatorUpdate, event func(string)) map[string]mockValidator {

for _, update := range updates {
str := fmt.Sprintf("%v", update.PubKey)
switch {
case update.Power == 0:
if _, ok := current[string(update.PubKey.Data)]; !ok {
if _, ok := current[str]; !ok {
tb.Fatalf("tried to delete a nonexistent validator")
}

event("endblock/validatorupdates/kicked")
delete(current, string(update.PubKey.Data))
delete(current, str)
default:
// Does validator already exist?
if mVal, ok := current[string(update.PubKey.Data)]; ok {
if mVal, ok := current[str]; ok {
mVal.val = update
event("endblock/validatorupdates/updated")
} else {
// Set this new validator
current[string(update.PubKey.Data)] = mockValidator{update, GetMemberOfInitialState(r, initialLivenessWeightings)}
current[str] = mockValidator{update, GetMemberOfInitialState(r, initialLivenessWeightings)}
event("endblock/validatorupdates/added")
}
}
Expand Down
3 changes: 3 additions & 0 deletions x/slashing/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ func TestJailedValidatorDelegations(t *testing.T) {
got := stake.NewHandler(stakeKeeper)(ctx, msgCreateVal)
require.True(t, got.IsOK(), "expected create validator msg to be ok, got: %v", got)

// end block
stake.EndBlocker(ctx, stakeKeeper)

// set dummy signing info
newInfo := ValidatorSigningInfo{
StartHeight: int64(0),
Expand Down
27 changes: 26 additions & 1 deletion x/slashing/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,16 @@ func TestSlashingPeriodCap(t *testing.T) {

// double sign less than max age
keeper.handleDoubleSign(ctx, valConsPubKey.Address(), 0, time.Unix(0, 0), amtInt)

// should be jailed
require.True(t, sk.Validator(ctx, addr).GetJailed())
// end block
stake.EndBlocker(ctx, sk)
// update block height
ctx = ctx.WithBlockHeight(int64(1))
// unjail to measure power
sk.Unjail(ctx, valConsAddr)
// end block
stake.EndBlocker(ctx, sk)
// power should be reduced
expectedPower := sdk.NewDecFromInt(amt).Mul(sdk.NewDec(19).Quo(sdk.NewDec(20)))
require.Equal(t, expectedPower, sk.Validator(ctx, addr).GetPower())
Expand All @@ -100,10 +103,14 @@ func TestSlashingPeriodCap(t *testing.T) {
keeper.handleDoubleSign(ctx, valConsPubKey.Address(), 0, time.Unix(0, 0), amtInt)
// should be jailed
require.True(t, sk.Validator(ctx, addr).GetJailed())
// end block
stake.EndBlocker(ctx, sk)
// update block height
ctx = ctx.WithBlockHeight(int64(2))
// unjail to measure power
sk.Unjail(ctx, valConsAddr)
// end block
stake.EndBlocker(ctx, sk)
// power should be equal, no more should have been slashed
expectedPower = sdk.NewDecFromInt(amt).Mul(sdk.NewDec(19).Quo(sdk.NewDec(20)))
require.Equal(t, expectedPower, sk.Validator(ctx, addr).GetPower())
Expand All @@ -114,6 +121,8 @@ func TestSlashingPeriodCap(t *testing.T) {
require.True(t, sk.Validator(ctx, addr).GetJailed())
// unjail to measure power
sk.Unjail(ctx, valConsAddr)
// end block
stake.EndBlocker(ctx, sk)
// power should be reduced
expectedPower = sdk.NewDecFromInt(amt).Mul(sdk.NewDec(18).Quo(sdk.NewDec(20)))
require.Equal(t, expectedPower, sk.Validator(ctx, addr).GetPower())
Expand Down Expand Up @@ -180,6 +189,9 @@ func TestHandleAbsentValidator(t *testing.T) {
require.Equal(t, int64(0), info.StartHeight)
require.Equal(t, keeper.SignedBlocksWindow(ctx)-keeper.MinSignedPerWindow(ctx)-1, info.SignedBlocksCounter)

// end block
stake.EndBlocker(ctx, sk)

// validator should have been jailed
validator, _ = sk.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(val))
require.Equal(t, sdk.Unbonding, validator.GetStatus())
Expand All @@ -193,6 +205,9 @@ func TestHandleAbsentValidator(t *testing.T) {
got = slh(ctx, NewMsgUnjail(addr))
require.True(t, got.IsOK())

// end block
stake.EndBlocker(ctx, sk)

// validator should be rebonded now
validator, _ = sk.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(val))
require.Equal(t, sdk.Bonded, validator.GetStatus())
Expand Down Expand Up @@ -222,12 +237,19 @@ func TestHandleAbsentValidator(t *testing.T) {
keeper.handleValidatorSignature(ctx, val.Address(), amtInt, false)
}

// end block
stake.EndBlocker(ctx, sk)

// validator should be jailed again after 500 unsigned blocks
nextHeight = height + keeper.MinSignedPerWindow(ctx) + 1
for ; height <= nextHeight; height++ {
ctx = ctx.WithBlockHeight(height)
keeper.handleValidatorSignature(ctx, val.Address(), amtInt, false)
}

// end block
stake.EndBlocker(ctx, sk)

validator, _ = sk.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(val))
require.Equal(t, sdk.Unbonding, validator.GetStatus())
}
Expand Down Expand Up @@ -296,6 +318,9 @@ func TestHandleAlreadyJailed(t *testing.T) {
keeper.handleValidatorSignature(ctx, val.Address(), amtInt, false)
}

// end block
stake.EndBlocker(ctx, sk)

// validator should have been jailed and slashed
validator, _ := sk.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(val))
require.Equal(t, sdk.Unbonding, validator.GetStatus())
Expand Down
3 changes: 3 additions & 0 deletions x/slashing/tick_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ func TestBeginBlocker(t *testing.T) {
BeginBlocker(ctx, req, keeper)
}

// end block
stake.EndBlocker(ctx, sk)

// validator should be jailed
validator, found := sk.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(pk))
require.True(t, found)
Expand Down
16 changes: 3 additions & 13 deletions x/stake/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
// Returns final validator set after applying all declaration and delegations
func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) (res []abci.ValidatorUpdate, err error) {
keeper.SetPool(ctx, data.Pool)
keeper.SetNewParams(ctx, data.Params)
keeper.SetParams(ctx, data.Params)
keeper.InitIntraTxCounter(ctx)

for i, validator := range data.Validators {
Expand All @@ -31,26 +31,16 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) (res [
return res, errors.Errorf("genesis validator cannot have zero delegator shares, validator: %v", validator)
}

// Manually set indexes for the first time
// Manually set indices for the first time
keeper.SetValidatorByConsAddr(ctx, validator)
keeper.SetValidatorByPowerIndex(ctx, validator, data.Pool)

if validator.Status == sdk.Bonded {
keeper.SetValidatorBondedIndex(ctx, validator)
}
}

for _, bond := range data.Bonds {
keeper.SetDelegation(ctx, bond)
}

keeper.UpdateBondedValidatorsFull(ctx)

vals := keeper.GetValidatorsBonded(ctx)
res = make([]abci.ValidatorUpdate, len(vals))
for i, val := range vals {
res[i] = val.ABCIValidatorUpdate()
}
res = keeper.ApplyAndReturnValidatorSetUpdates(ctx)
return
}

Expand Down
Loading

0 comments on commit 324bdaf

Please sign in to comment.