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

ADR 17 Implementation: Historical Module #5380

Merged
merged 31 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
17fcad3
add history module and some tests
AdityaSripal Dec 10, 2019
5f916a4
Merge branch 'aditya/history' of https://github.com/cosmos/cosmos-sdk…
AdityaSripal Dec 10, 2019
2b052b4
complete query and write tests
AdityaSripal Dec 10, 2019
481ee0d
complete query and write tests
AdityaSripal Dec 10, 2019
954c8b1
add abci test
AdityaSripal Dec 10, 2019
da266e8
docs and CHANGELOG
AdityaSripal Dec 10, 2019
813c634
add client query methods and CHANGELOG
AdityaSripal Dec 11, 2019
5c634a3
fix merge
AdityaSripal Dec 11, 2019
33154f0
Merge branch 'master' into aditya/history
cwgoes Dec 11, 2019
aa34dc7
implement edge case for when historicalentries param gets reduced
AdityaSripal Dec 11, 2019
fb2e660
Merge branch 'aditya/history' of https://github.com/cosmos/cosmos-sdk…
AdityaSripal Dec 11, 2019
d0e8d88
Update x/staking/abci.go
cwgoes Dec 11, 2019
5997e15
Apply suggestions from code review
AdityaSripal Dec 12, 2019
82d386f
apply codereview suggestions
AdityaSripal Dec 12, 2019
a45a5fa
appease linter
AdityaSripal Dec 13, 2019
ef43df5
Update x/staking/client/cli/query.go
fedekunze Dec 16, 2019
093dcf5
Merge branch 'master' into aditya/history
fedekunze Dec 16, 2019
c3fc30b
Apply suggestions from code review
AdityaSripal Dec 16, 2019
e4d2c34
Update x/staking/keeper/historical_info.go
AdityaSripal Dec 16, 2019
74b3f19
update spec, alias, and fix minor bug
AdityaSripal Dec 17, 2019
73cbb7e
cleanup changelog
AdityaSripal Dec 17, 2019
277054b
Merge branch 'master' into aditya/history
AdityaSripal Dec 17, 2019
a61d28a
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into ad…
AdityaSripal Dec 17, 2019
1fdc269
Merge branch 'aditya/history' of https://github.com/cosmos/cosmos-sdk…
AdityaSripal Dec 17, 2019
3f59d8c
fix merge
AdityaSripal Dec 17, 2019
9364ba4
Make BeginBlocker/EndBlocker methods on keeper
AdityaSripal Dec 17, 2019
c0f2ac7
Merge branch 'master' into aditya/history
alexanderbez Dec 17, 2019
4402252
move beginblock/endblock logic into keeper, maintain API
AdityaSripal Dec 18, 2019
c3ad793
remove redundant abci tests
AdityaSripal Dec 18, 2019
b1a9ac5
Merge branch 'aditya/history' of https://github.com/cosmos/cosmos-sdk…
AdityaSripal Dec 18, 2019
ffc64bf
Merge branch 'master' into aditya/history
AdityaSripal Dec 18, 2019
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
Prev Previous commit
Next Next commit
Make BeginBlocker/EndBlocker methods on keeper
  • Loading branch information
AdityaSripal committed Dec 17, 2019
commit 9364ba43a4eebb757326605847299fd5b39cbfce
2 changes: 1 addition & 1 deletion x/staking/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func getMockApp(t *testing.T) (*mock.App, Keeper) {
// 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 := keeper.EndBlocker(ctx)

return abci.ResponseEndBlock{
ValidatorUpdates: validatorUpdates,
Expand Down
68 changes: 34 additions & 34 deletions x/staking/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ func TestValidatorByPowerIndex(t *testing.T) {
types.ModuleCdc.MustUnmarshalBinaryLengthPrefixed(got.Data, &finishTime)

ctx = ctx.WithBlockTime(finishTime)
EndBlocker(ctx, keeper)
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)
keeper.EndBlocker(ctx)

// verify that by power key nolonger exists
_, found = keeper.GetValidator(ctx, validatorAddr)
Expand Down Expand Up @@ -216,7 +216,7 @@ func TestLegacyValidatorDelegations(t *testing.T) {
var finishTime time.Time
types.ModuleCdc.MustUnmarshalBinaryLengthPrefixed(got.Data, &finishTime)
ctx = ctx.WithBlockTime(finishTime)
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)

// verify the validator record still exists, is jailed, and has correct tokens
validator, found = keeper.GetValidator(ctx, valAddr)
Expand Down Expand Up @@ -438,7 +438,7 @@ func TestIncrementsMsgUnbond(t *testing.T) {
var finishTime time.Time
types.ModuleCdc.MustUnmarshalBinaryLengthPrefixed(got.Data, &finishTime)
ctx = ctx.WithBlockTime(finishTime)
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)

// check that the accounts and the bond account have the appropriate values
validator, found = keeper.GetValidator(ctx, validatorAddr)
Expand Down Expand Up @@ -532,7 +532,7 @@ func TestMultipleMsgCreateValidator(t *testing.T) {
require.Equal(t, balanceExpd, balanceGot, "expected account to have %d, got %d", balanceExpd, balanceGot)
}

EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)

// unbond them all by removing delegation
for i, validatorAddr := range validatorAddrs {
Expand All @@ -548,10 +548,10 @@ func TestMultipleMsgCreateValidator(t *testing.T) {
types.ModuleCdc.MustUnmarshalBinaryLengthPrefixed(got.Data, &finishTime)

// adds validator into unbonding queue
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)

// removes validator from queue and set
EndBlocker(ctx.WithBlockTime(blockTime.Add(params.UnbondingTime)), keeper)
keeper.EndBlocker(ctx.WithBlockTime(blockTime.Add(params.UnbondingTime)))

// Check that the validator is deleted from state
validators := keeper.GetValidators(ctx, 100)
Expand Down Expand Up @@ -599,7 +599,7 @@ func TestMultipleMsgDelegate(t *testing.T) {
types.ModuleCdc.MustUnmarshalBinaryLengthPrefixed(got.Data, &finishTime)

ctx = ctx.WithBlockTime(finishTime)
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)

// check that the account is unbonded
_, found := keeper.GetDelegation(ctx, delegatorAddr, validatorAddr)
Expand Down Expand Up @@ -631,7 +631,7 @@ func TestJailValidator(t *testing.T) {
types.ModuleCdc.MustUnmarshalBinaryLengthPrefixed(got.Data, &finishTime)

ctx = ctx.WithBlockTime(finishTime)
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)

validator, found := keeper.GetValidator(ctx, validatorAddr)
require.True(t, found)
Expand All @@ -645,7 +645,7 @@ func TestJailValidator(t *testing.T) {
types.ModuleCdc.MustUnmarshalBinaryLengthPrefixed(got.Data, &finishTime)

ctx = ctx.WithBlockTime(finishTime)
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)

// verify that the pubkey can now be reused
got = handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
Expand Down Expand Up @@ -673,7 +673,7 @@ func TestValidatorQueue(t *testing.T) {
got = handleMsgDelegate(ctx, msgDelegate, keeper)
require.True(t, got.IsOK(), "expected ok, got %v", got)

EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)

// unbond the all self-delegation to put validator in unbonding state
unbondAmt := sdk.NewCoin(sdk.DefaultBondDenom, delTokens)
Expand All @@ -685,7 +685,7 @@ func TestValidatorQueue(t *testing.T) {
types.ModuleCdc.MustUnmarshalBinaryLengthPrefixed(got.Data, &finishTime)

ctx = ctx.WithBlockTime(finishTime)
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)

origHeader := ctx.BlockHeader()

Expand All @@ -695,15 +695,15 @@ func TestValidatorQueue(t *testing.T) {

// should still be unbonding at time 6 seconds later
ctx = ctx.WithBlockTime(origHeader.Time.Add(time.Second * 6))
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)

validator, found = keeper.GetValidator(ctx, validatorAddr)
require.True(t, found)
require.True(t, validator.IsUnbonding(), "%v", validator)

// should be in unbonded state at time 7 seconds later
ctx = ctx.WithBlockTime(origHeader.Time.Add(time.Second * 7))
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)

validator, found = keeper.GetValidator(ctx, validatorAddr)
require.True(t, found)
Expand All @@ -725,7 +725,7 @@ func TestUnbondingPeriod(t *testing.T) {
got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator")

EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)

// begin unbonding
unbondAmt := sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromConsensusPower(10))
Expand All @@ -739,19 +739,19 @@ func TestUnbondingPeriod(t *testing.T) {
require.True(t, found, "should not have unbonded")

// cannot complete unbonding at same time
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)
_, found = keeper.GetUnbondingDelegation(ctx, sdk.AccAddress(validatorAddr), validatorAddr)
require.True(t, found, "should not have unbonded")

// cannot complete unbonding at time 6 seconds later
ctx = ctx.WithBlockTime(origHeader.Time.Add(time.Second * 6))
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)
_, found = keeper.GetUnbondingDelegation(ctx, sdk.AccAddress(validatorAddr), validatorAddr)
require.True(t, found, "should not have unbonded")

// can complete unbonding at time 7 seconds later
ctx = ctx.WithBlockTime(origHeader.Time.Add(time.Second * 7))
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)
_, found = keeper.GetUnbondingDelegation(ctx, sdk.AccAddress(validatorAddr), validatorAddr)
require.False(t, found, "should have unbonded")
}
Expand Down Expand Up @@ -789,7 +789,7 @@ func TestUnbondingFromUnbondingValidator(t *testing.T) {
ctx = ctx.WithBlockTime(ctx.BlockHeader().Time.Add(keeper.UnbondingTime(ctx)))

// Run the EndBlocker
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)

// Check to make sure that the unbonding delegation is no longer in state
// (meaning it was deleted in the above EndBlocker)
Expand Down Expand Up @@ -839,19 +839,19 @@ func TestRedelegationPeriod(t *testing.T) {
origHeader := ctx.BlockHeader()

// cannot complete redelegation at same time
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)
_, found := keeper.GetRedelegation(ctx, sdk.AccAddress(validatorAddr), validatorAddr, validatorAddr2)
require.True(t, found, "should not have unbonded")

// cannot complete redelegation at time 6 seconds later
ctx = ctx.WithBlockTime(origHeader.Time.Add(time.Second * 6))
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)
_, found = keeper.GetRedelegation(ctx, sdk.AccAddress(validatorAddr), validatorAddr, validatorAddr2)
require.True(t, found, "should not have unbonded")

// can complete redelegation at time 7 seconds later
ctx = ctx.WithBlockTime(origHeader.Time.Add(time.Second * 7))
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)
_, found = keeper.GetRedelegation(ctx, sdk.AccAddress(validatorAddr), validatorAddr, validatorAddr2)
require.False(t, found, "should have unbonded")
}
Expand Down Expand Up @@ -893,7 +893,7 @@ func TestTransitiveRedelegation(t *testing.T) {
ctx = ctx.WithBlockTime(blockTime.Add(params.UnbondingTime))

// complete first redelegation
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)

// now should be able to redelegate from the second validator to the third
got = handleMsgBeginRedelegate(ctx, msgBeginRedelegate, keeper)
Expand Down Expand Up @@ -921,7 +921,7 @@ func TestMultipleRedelegationAtSameTime(t *testing.T) {
require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator")

// end block to bond them
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)

// begin a redelegate
selfDelAddr := sdk.AccAddress(valAddr) // (the validator is it's own delegator)
Expand All @@ -946,7 +946,7 @@ func TestMultipleRedelegationAtSameTime(t *testing.T) {

// move forward in time, should complete both redelegations
ctx = ctx.WithBlockTime(ctx.BlockHeader().Time.Add(1 * time.Second))
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)

rd, found = keeper.GetRedelegation(ctx, selfDelAddr, valAddr, valAddr2)
require.False(t, found)
Expand All @@ -973,7 +973,7 @@ func TestMultipleRedelegationAtUniqueTimes(t *testing.T) {
require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator")

// end block to bond them
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)

// begin a redelegate
selfDelAddr := sdk.AccAddress(valAddr) // (the validator is it's own delegator)
Expand All @@ -994,14 +994,14 @@ func TestMultipleRedelegationAtUniqueTimes(t *testing.T) {

// move forward in time, should complete the first redelegation, but not the second
ctx = ctx.WithBlockTime(ctx.BlockHeader().Time.Add(5 * time.Second))
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)
rd, found = keeper.GetRedelegation(ctx, selfDelAddr, valAddr, valAddr2)
require.True(t, found)
require.Len(t, rd.Entries, 1)

// move forward in time, should complete the second redelegation
ctx = ctx.WithBlockTime(ctx.BlockHeader().Time.Add(5 * time.Second))
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)
rd, found = keeper.GetRedelegation(ctx, selfDelAddr, valAddr, valAddr2)
require.False(t, found)
}
Expand All @@ -1022,7 +1022,7 @@ func TestMultipleUnbondingDelegationAtSameTime(t *testing.T) {
require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator")

// end block to bond
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)

// begin an unbonding delegation
selfDelAddr := sdk.AccAddress(valAddr) // (the validator is it's own delegator)
Expand All @@ -1047,7 +1047,7 @@ func TestMultipleUnbondingDelegationAtSameTime(t *testing.T) {

// move forwaubd in time, should complete both ubds
ctx = ctx.WithBlockTime(ctx.BlockHeader().Time.Add(1 * time.Second))
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)

ubd, found = keeper.GetUnbondingDelegation(ctx, selfDelAddr, valAddr)
require.False(t, found)
Expand All @@ -1069,7 +1069,7 @@ func TestMultipleUnbondingDelegationAtUniqueTimes(t *testing.T) {
require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator")

// end block to bond
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)

// begin an unbonding delegation
selfDelAddr := sdk.AccAddress(valAddr) // (the validator is it's own delegator)
Expand All @@ -1095,14 +1095,14 @@ func TestMultipleUnbondingDelegationAtUniqueTimes(t *testing.T) {

// move forwaubd in time, should complete the first redelegation, but not the second
ctx = ctx.WithBlockTime(ctx.BlockHeader().Time.Add(5 * time.Second))
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)
ubd, found = keeper.GetUnbondingDelegation(ctx, selfDelAddr, valAddr)
require.True(t, found)
require.Len(t, ubd.Entries, 1)

// move forwaubd in time, should complete the second redelegation
ctx = ctx.WithBlockTime(ctx.BlockHeader().Time.Add(5 * time.Second))
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)
ubd, found = keeper.GetUnbondingDelegation(ctx, selfDelAddr, valAddr)
require.False(t, found)
}
Expand Down Expand Up @@ -1254,7 +1254,7 @@ func TestBondUnbondRedelegateSlashTwice(t *testing.T) {
require.Equal(t, sdk.NewDecFromInt(redAmt.Amount.QuoRaw(2)), delegation.Shares)

// end blocker
EndBlocker(ctx, keeper)
keeper.EndBlocker(ctx)

// validator power should have been reduced to zero
// validator should be in unbonding state
Expand Down
7 changes: 3 additions & 4 deletions x/staking/abci.go → x/staking/keeper/abci.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package staking
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/cosmos/cosmos-sdk/x/staking/types"
abci "github.com/tendermint/tendermint/abci/types"
)

// BeginBlocker will persist the current header and validator set as a historical entry
// and prune the oldest entry based on the HistoricalEntries parameter
func BeginBlocker(ctx sdk.Context, k Keeper) {
func (k Keeper) BeginBlocker(ctx sdk.Context) {
entryNum := k.HistoricalEntries(ctx)

// Prune store to ensure we only have parameter-defined historical entries.
Expand Down Expand Up @@ -42,7 +41,7 @@ func BeginBlocker(ctx sdk.Context, k Keeper) {
}

// Called every block, update validator set
func EndBlocker(ctx sdk.Context, k keeper.Keeper) []abci.ValidatorUpdate {
func (k Keeper) EndBlocker(ctx sdk.Context) []abci.ValidatorUpdate {
// Calculate validator set changes.
//
// NOTE: ApplyAndReturnValidatorSetUpdates has to come before
Expand Down
17 changes: 8 additions & 9 deletions x/staking/abci_test.go → x/staking/keeper/abci_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package staking
package keeper

import (
"sort"
Expand All @@ -7,13 +7,12 @@ import (
"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/cosmos/cosmos-sdk/x/staking/types"
abci "github.com/tendermint/tendermint/abci/types"
)

func TestBeginBlocker(t *testing.T) {
ctx, _, k, _ := keeper.CreateTestInput(t, false, 10)
ctx, _, k, _ := CreateTestInput(t, false, 10)

// set historical entries in params to 5
params := types.DefaultParams()
Expand All @@ -30,9 +29,9 @@ func TestBeginBlocker(t *testing.T) {
ChainID: "HelloChain",
Height: 5,
}
valSet := []Validator{
types.NewValidator(sdk.ValAddress(keeper.Addrs[0]), keeper.PKs[0], types.Description{}),
types.NewValidator(sdk.ValAddress(keeper.Addrs[1]), keeper.PKs[1], types.Description{}),
valSet := []types.Validator{
types.NewValidator(sdk.ValAddress(Addrs[0]), PKs[0], types.Description{}),
types.NewValidator(sdk.ValAddress(Addrs[1]), PKs[1], types.Description{}),
}
hi4 := types.NewHistoricalInfo(h4, valSet)
hi5 := types.NewHistoricalInfo(h5, valSet)
Expand All @@ -46,10 +45,10 @@ func TestBeginBlocker(t *testing.T) {
require.Equal(t, hi5, recv)

// Set last validators in keeper
val1 := types.NewValidator(sdk.ValAddress(keeper.Addrs[2]), keeper.PKs[2], types.Description{})
val1 := types.NewValidator(sdk.ValAddress(Addrs[2]), PKs[2], types.Description{})
k.SetValidator(ctx, val1)
k.SetLastValidatorPower(ctx, val1.OperatorAddress, 10)
val2 := types.NewValidator(sdk.ValAddress(keeper.Addrs[3]), keeper.PKs[3], types.Description{})
val2 := types.NewValidator(sdk.ValAddress(Addrs[3]), PKs[3], types.Description{})
vals := []types.Validator{val1, val2}
sort.Sort(types.Validators(vals))
k.SetValidator(ctx, val2)
Expand All @@ -62,7 +61,7 @@ func TestBeginBlocker(t *testing.T) {
}
ctx = ctx.WithBlockHeader(header)

BeginBlocker(ctx, k)
k.BeginBlocker(ctx)

// Check HistoricalInfo at height 10 is persisted
expected := types.HistoricalInfo{
Expand Down
4 changes: 2 additions & 2 deletions x/staking/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,13 @@ func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage {

// BeginBlock returns the begin blocker for the staking module.
func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
BeginBlocker(ctx, am.keeper)
am.keeper.BeginBlocker(ctx)
}

// 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 {
return EndBlocker(ctx, am.keeper)
return am.keeper.EndBlocker(ctx)
}

//____________________________________________________________________________
Expand Down