Skip to content

Commit

Permalink
Extend abci EndBlock (cosmos#30)
Browse files Browse the repository at this point in the history
* Change end block to return dkg val updates

* Fix test
  • Loading branch information
jinmannwong authored Aug 5, 2020
1 parent 8e3248d commit a170877
Show file tree
Hide file tree
Showing 20 changed files with 53 additions and 48 deletions.
13 changes: 8 additions & 5 deletions types/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ type AppModule interface {

// ABCI
BeginBlock(sdk.Context, abci.RequestBeginBlock)
EndBlock(sdk.Context, abci.RequestEndBlock) []abci.ValidatorUpdate
EndBlock(sdk.Context, abci.RequestEndBlock) ([]abci.ValidatorUpdate, []abci.ValidatorUpdate)
}

//___________________________
Expand Down Expand Up @@ -181,8 +181,8 @@ func (gam GenesisOnlyAppModule) NewQuerierHandler() sdk.Querier { return nil }
func (gam GenesisOnlyAppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {}

// EndBlock returns an empty module end-block
func (GenesisOnlyAppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
return []abci.ValidatorUpdate{}
func (GenesisOnlyAppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) ([]abci.ValidatorUpdate, []abci.ValidatorUpdate) {
return []abci.ValidatorUpdate{}, []abci.ValidatorUpdate{}
}

//____________________________________________________________________________
Expand Down Expand Up @@ -308,23 +308,26 @@ func (m *Manager) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) abci.R
func (m *Manager) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
ctx = ctx.WithEventManager(sdk.NewEventManager())
validatorUpdates := []abci.ValidatorUpdate{}
dkgValidatorUpdates := []abci.ValidatorUpdate{}

for _, moduleName := range m.OrderEndBlockers {
moduleValUpdates := m.Modules[moduleName].EndBlock(ctx, req)
moduleValUpdates, moduleDKGValUpdates := m.Modules[moduleName].EndBlock(ctx, req)

// use these validator updates if provided, the module manager assumes
// only one module will update the validator set
// only one module will update the validator set and dkg validator set
if len(moduleValUpdates) > 0 {
if len(validatorUpdates) > 0 {
panic("validator EndBlock updates already set by a previous module")
}

validatorUpdates = moduleValUpdates
dkgValidatorUpdates = moduleDKGValUpdates
}
}

return abci.ResponseEndBlock{
ValidatorUpdates: validatorUpdates,
DkgValidatorUpdates: dkgValidatorUpdates,
Events: ctx.EventManager().ABCIEvents(),
}
}
4 changes: 2 additions & 2 deletions x/auth/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ func (AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}

// EndBlock returns the end blocker for the auth module. It returns no validator
// updates.
func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
return []abci.ValidatorUpdate{}
func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) ([]abci.ValidatorUpdate, []abci.ValidatorUpdate) {
return []abci.ValidatorUpdate{}, []abci.ValidatorUpdate{}
}

//____________________________________________________________________________
Expand Down
4 changes: 2 additions & 2 deletions x/bank/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ func (AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}

// EndBlock returns the end blocker for the bank module. It returns no validator
// updates.
func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
return []abci.ValidatorUpdate{}
func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) ([]abci.ValidatorUpdate, []abci.ValidatorUpdate) {
return []abci.ValidatorUpdate{}, []abci.ValidatorUpdate{}
}

//____________________________________________________________________________
Expand Down
4 changes: 2 additions & 2 deletions x/crisis/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}

// EndBlock returns the end blocker for the crisis module. It returns no validator
// updates.
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) ([]abci.ValidatorUpdate, []abci.ValidatorUpdate) {
EndBlocker(ctx, *am.keeper)
return []abci.ValidatorUpdate{}
return []abci.ValidatorUpdate{}, []abci.ValidatorUpdate{}
}
4 changes: 2 additions & 2 deletions x/distribution/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {

// EndBlock returns the end blocker for the distribution module. It returns no validator
// updates.
func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
return []abci.ValidatorUpdate{}
func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) ([]abci.ValidatorUpdate, []abci.ValidatorUpdate) {
return []abci.ValidatorUpdate{}, []abci.ValidatorUpdate{}
}

//____________________________________________________________________________
Expand Down
4 changes: 2 additions & 2 deletions x/evidence/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,6 @@ func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {

// EndBlock executes all ABCI EndBlock logic respective to the evidence module. It
// returns no validator updates.
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
return []abci.ValidatorUpdate{}
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) ([]abci.ValidatorUpdate, []abci.ValidatorUpdate) {
return []abci.ValidatorUpdate{}, []abci.ValidatorUpdate{}
}
10 changes: 5 additions & 5 deletions x/gov/keeper/tally_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func TestTallyDelgatorOverride(t *testing.T) {
_, err := sk.Delegate(ctx, TestAddrs[0], delTokens, sdk.Unbonded, val1, true)
require.NoError(t, err)

_ = staking.EndBlocker(ctx, &sk)
staking.EndBlocker(ctx, &sk)

tp := TestProposal
proposal, err := keeper.SubmitProposal(ctx, tp)
Expand Down Expand Up @@ -261,7 +261,7 @@ func TestTallyDelgatorInherit(t *testing.T) {
_, err := sk.Delegate(ctx, TestAddrs[0], delTokens, sdk.Unbonded, val3, true)
require.NoError(t, err)

_ = staking.EndBlocker(ctx, &sk)
staking.EndBlocker(ctx, &sk)

tp := TestProposal
proposal, err := keeper.SubmitProposal(ctx, tp)
Expand Down Expand Up @@ -298,7 +298,7 @@ func TestTallyDelgatorMultipleOverride(t *testing.T) {
_, err = sk.Delegate(ctx, TestAddrs[0], delTokens, sdk.Unbonded, val2, true)
require.NoError(t, err)

_ = staking.EndBlocker(ctx, &sk)
staking.EndBlocker(ctx, &sk)

tp := TestProposal
proposal, err := keeper.SubmitProposal(ctx, tp)
Expand Down Expand Up @@ -336,7 +336,7 @@ func TestTallyDelgatorMultipleInherit(t *testing.T) {
_, err = sk.Delegate(ctx, TestAddrs[0], delTokens, sdk.Unbonded, val3, true)
require.NoError(t, err)

_ = staking.EndBlocker(ctx, &sk)
staking.EndBlocker(ctx, &sk)

tp := TestProposal
proposal, err := keeper.SubmitProposal(ctx, tp)
Expand Down Expand Up @@ -373,7 +373,7 @@ func TestTallyJailedValidator(t *testing.T) {
_, err = sk.Delegate(ctx, TestAddrs[0], delTokens, sdk.Unbonded, val3, true)
require.NoError(t, err)

_ = staking.EndBlocker(ctx, &sk)
staking.EndBlocker(ctx, &sk)

sk.Jail(ctx, sdk.ConsAddress(val2.ConsPubKey.Address()))

Expand Down
2 changes: 1 addition & 1 deletion x/gov/keeper/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func createValidators(ctx sdk.Context, sk staking.Keeper, powers []int64) {

header := abci.Header{Entropy: testBlockEntropy()}
staking.BeginBlocker(ctx, abci.RequestBeginBlock{Header: header}, &sk)
_ = staking.EndBlocker(ctx, &sk)
staking.EndBlocker(ctx, &sk)
}

func testBlockEntropy() abci.BlockEntropy {
Expand Down
4 changes: 2 additions & 2 deletions x/gov/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ func (AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}

// EndBlock returns the end blocker for the gov module. It returns no validator
// updates.
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) ([]abci.ValidatorUpdate, []abci.ValidatorUpdate) {
EndBlocker(ctx, am.keeper)
return []abci.ValidatorUpdate{}
return []abci.ValidatorUpdate{}, []abci.ValidatorUpdate{}
}

//____________________________________________________________________________
Expand Down
4 changes: 2 additions & 2 deletions x/mint/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {

// EndBlock returns the end blocker for the mint module. It returns no validator
// updates.
func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
return []abci.ValidatorUpdate{}
func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) ([]abci.ValidatorUpdate, []abci.ValidatorUpdate) {
return []abci.ValidatorUpdate{}, []abci.ValidatorUpdate{}
}

//____________________________________________________________________________
Expand Down
2 changes: 1 addition & 1 deletion x/slashing/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func getBeginBlocker(keeper *staking.Keeper) sdk.BeginBlocker {
// staking endblocker
func getEndBlocker(keeper *staking.Keeper) sdk.EndBlocker {
return func(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
validatorUpdates := staking.EndBlocker(ctx, keeper)
validatorUpdates, _ := staking.EndBlocker(ctx, keeper)
return abci.ResponseEndBlock{
ValidatorUpdates: validatorUpdates,
}
Expand Down
4 changes: 2 additions & 2 deletions x/slashing/internal/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func TestValidatorDippingInAndOut(t *testing.T) {
require.NotNil(t, res)

staking.BeginBlocker(ctx, abci.RequestBeginBlock{Header: header}, &sk)
validatorUpdates := staking.EndBlocker(ctx, &sk)
validatorUpdates, _ := staking.EndBlocker(ctx, &sk)
require.Equal(t, 2, len(validatorUpdates))
validator, _ := sk.GetValidator(ctx, addr)
require.Equal(t, sdk.Unbonding, validator.Status)
Expand All @@ -167,7 +167,7 @@ func TestValidatorDippingInAndOut(t *testing.T) {
require.NotNil(t, res)

staking.BeginBlocker(ctx, abci.RequestBeginBlock{Header: header}, &sk)
validatorUpdates = staking.EndBlocker(ctx, &sk)
validatorUpdates, _ = staking.EndBlocker(ctx, &sk)
require.Equal(t, 2, len(validatorUpdates))
validator, _ = sk.GetValidator(ctx, addr)
require.Equal(t, sdk.Bonded, validator.Status)
Expand Down
4 changes: 2 additions & 2 deletions x/slashing/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {

// EndBlock returns the end blocker for the slashing module. It returns no validator
// updates.
func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
return []abci.ValidatorUpdate{}
func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) ([]abci.ValidatorUpdate, []abci.ValidatorUpdate) {
return []abci.ValidatorUpdate{}, []abci.ValidatorUpdate{}
}

//____________________________________________________________________________
Expand Down
8 changes: 6 additions & 2 deletions x/staking/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k *keeper.Keeper)
}

// Called every block, update validator set
func EndBlocker(ctx sdk.Context, k *keeper.Keeper) []abci.ValidatorUpdate {
return k.BlockValidatorUpdates(ctx)
func EndBlocker(ctx sdk.Context, k *keeper.Keeper) ([]abci.ValidatorUpdate, []abci.ValidatorUpdate) {
if !k.ChangeoverValidators {
return []abci.ValidatorUpdate{}, []abci.ValidatorUpdate{}
}
k.ChangeoverValidators = false
return k.BlockValidatorUpdates(ctx), []abci.ValidatorUpdate{}
}
3 changes: 2 additions & 1 deletion x/staking/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ func getBeginBlocker(keeper *Keeper) sdk.BeginBlocker {
// getEndBlocker returns a staking endblocker.
func getEndBlocker(keeper *Keeper) sdk.EndBlocker {
return func(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
validatorUpdates := EndBlocker(ctx, keeper)
validatorUpdates, dkgValidatorUpdates := EndBlocker(ctx, keeper)

return abci.ResponseEndBlock{
ValidatorUpdates: validatorUpdates,
DkgValidatorUpdates: dkgValidatorUpdates,
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions x/staking/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,9 +560,11 @@ func TestMultipleMsgCreateValidator(t *testing.T) {

require.Equal(t, i+1, len(validators), "expected %d validators got %d, validators: %v", i+1, len(validators), validators)
require.Equal(t, valTokens, val.DelegatorShares.RoundInt(), "expected %d shares, got %d", 10, val.DelegatorShares)
require.Equal(t, balanceExpd, balanceGot, "expected account to have %d, got %d", balanceExpd, balanceGot)
require.Equal(t, balanceExpd, balanceGot, "expected account %d to have %d, got %d", i, balanceExpd, balanceGot)
}

header := abci.Header{Entropy: testBlockEntropy()}
BeginBlocker(ctx, abci.RequestBeginBlock{Header: header}, &keeper)
EndBlocker(ctx, &keeper)

// unbond them all by removing delegation
Expand All @@ -580,11 +582,11 @@ func TestMultipleMsgCreateValidator(t *testing.T) {
types.ModuleCdc.MustUnmarshalBinaryLengthPrefixed(res.Data, &finishTime)

// adds validator into unbonding queue
header := abci.Header{Entropy: testBlockEntropy()}
BeginBlocker(ctx, abci.RequestBeginBlock{Header: header}, &keeper)
EndBlocker(ctx, &keeper)

// removes validator from queue and set
BeginBlocker(ctx, abci.RequestBeginBlock{Header: header}, &keeper)
EndBlocker(ctx.WithBlockTime(blockTime.Add(params.UnbondingTime)), &keeper)

// Check that the validator is deleted from state
Expand All @@ -596,7 +598,7 @@ func TestMultipleMsgCreateValidator(t *testing.T) {
require.False(t, found)

gotBalance := accMapper.GetAccount(ctx, delegatorAddrs[i]).GetCoins().AmountOf(params.BondDenom)
require.Equal(t, initTokens, gotBalance, "expected account to have %d, got %d", initTokens, gotBalance)
require.Equal(t, initTokens, gotBalance, "expected account %d to have %d, got %d", i, initTokens, gotBalance)
}
}

Expand Down
9 changes: 2 additions & 7 deletions x/staking/keeper/val_state_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ import (

// Calculate the ValidatorUpdates for the current block
// Called in each EndBlock
func (k Keeper) BlockValidatorUpdates(ctx sdk.Context) (validatorUpdates []abci.ValidatorUpdate) {
if !k.ChangeoverValidators {
return
}
k.ChangeoverValidators = false

func (k Keeper) BlockValidatorUpdates(ctx sdk.Context) []abci.ValidatorUpdate {
// Calculate validator set changes.
//
// NOTE: ApplyAndReturnValidatorSetUpdates has to come before
Expand All @@ -28,7 +23,7 @@ func (k Keeper) BlockValidatorUpdates(ctx sdk.Context) (validatorUpdates []abci.
// unbonded after the Endblocker (go from Bonded -> Unbonding during
// ApplyAndReturnValidatorSetUpdates and then Unbonding -> Unbonded during
// UnbondAllMatureValidatorQueue).
validatorUpdates = k.ApplyAndReturnValidatorSetUpdates(ctx)
validatorUpdates := k.ApplyAndReturnValidatorSetUpdates(ctx)

// Unbond all mature validators from the unbonding queue.
k.UnbondAllMatureValidatorQueue(ctx)
Expand Down
2 changes: 1 addition & 1 deletion x/staking/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {

// EndBlock returns the end blocker for the staking module. It returns no validator
// updates.
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) ([]abci.ValidatorUpdate, []abci.ValidatorUpdate) {
return EndBlocker(ctx, &am.keeper)
}

Expand Down
4 changes: 2 additions & 2 deletions x/supply/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}

// EndBlock returns the end blocker for the supply module. It returns no validator
// updates.
func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
return []abci.ValidatorUpdate{}
func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) ([]abci.ValidatorUpdate, []abci.ValidatorUpdate) {
return []abci.ValidatorUpdate{}, []abci.ValidatorUpdate{}
}

//____________________________________________________________________________
Expand Down
4 changes: 2 additions & 2 deletions x/upgrade/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,6 @@ func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {
}

// EndBlock does nothing
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
return []abci.ValidatorUpdate{}
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) ([]abci.ValidatorUpdate, []abci.ValidatorUpdate) {
return []abci.ValidatorUpdate{}, []abci.ValidatorUpdate{}
}

0 comments on commit a170877

Please sign in to comment.