-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
17fcad3
add history module and some tests
AdityaSripal 5f916a4
Merge branch 'aditya/history' of https://github.com/cosmos/cosmos-sdk…
AdityaSripal 2b052b4
complete query and write tests
AdityaSripal 481ee0d
complete query and write tests
AdityaSripal 954c8b1
add abci test
AdityaSripal da266e8
docs and CHANGELOG
AdityaSripal 813c634
add client query methods and CHANGELOG
AdityaSripal 5c634a3
fix merge
AdityaSripal 33154f0
Merge branch 'master' into aditya/history
cwgoes aa34dc7
implement edge case for when historicalentries param gets reduced
AdityaSripal fb2e660
Merge branch 'aditya/history' of https://github.com/cosmos/cosmos-sdk…
AdityaSripal d0e8d88
Update x/staking/abci.go
cwgoes 5997e15
Apply suggestions from code review
AdityaSripal 82d386f
apply codereview suggestions
AdityaSripal a45a5fa
appease linter
AdityaSripal ef43df5
Update x/staking/client/cli/query.go
fedekunze 093dcf5
Merge branch 'master' into aditya/history
fedekunze c3fc30b
Apply suggestions from code review
AdityaSripal e4d2c34
Update x/staking/keeper/historical_info.go
AdityaSripal 74b3f19
update spec, alias, and fix minor bug
AdityaSripal 73cbb7e
cleanup changelog
AdityaSripal 277054b
Merge branch 'master' into aditya/history
AdityaSripal a61d28a
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into ad…
AdityaSripal 1fdc269
Merge branch 'aditya/history' of https://github.com/cosmos/cosmos-sdk…
AdityaSripal 3f59d8c
fix merge
AdityaSripal 9364ba4
Make BeginBlocker/EndBlocker methods on keeper
AdityaSripal c0f2ac7
Merge branch 'master' into aditya/history
alexanderbez 4402252
move beginblock/endblock logic into keeper, maintain API
AdityaSripal c3ad793
remove redundant abci tests
AdityaSripal b1a9ac5
Merge branch 'aditya/history' of https://github.com/cosmos/cosmos-sdk…
AdityaSripal ffc64bf
Merge branch 'master' into aditya/history
AdityaSripal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package staking | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/staking/keeper" | ||
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.Keeper) { | ||
k.TrackHistoricalInfo(ctx) | ||
} | ||
|
||
// Called every block, update validator set | ||
func EndBlocker(ctx sdk.Context, k keeper.Keeper) []abci.ValidatorUpdate { | ||
return k.BlockValidatorUpdates(ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package keeper | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/staking/types" | ||
) | ||
|
||
// GetHistoricalInfo gets the historical info at a given height | ||
func (k Keeper) GetHistoricalInfo(ctx sdk.Context, height int64) (types.HistoricalInfo, bool) { | ||
store := ctx.KVStore(k.storeKey) | ||
key := types.GetHistoricalInfoKey(height) | ||
|
||
value := store.Get(key) | ||
if value == nil { | ||
return types.HistoricalInfo{}, false | ||
} | ||
|
||
hi := types.MustUnmarshalHistoricalInfo(k.cdc, value) | ||
return hi, true | ||
} | ||
|
||
// SetHistoricalInfo sets the historical info at a given height | ||
func (k Keeper) SetHistoricalInfo(ctx sdk.Context, height int64, hi types.HistoricalInfo) { | ||
store := ctx.KVStore(k.storeKey) | ||
key := types.GetHistoricalInfoKey(height) | ||
|
||
value := types.MustMarshalHistoricalInfo(k.cdc, hi) | ||
store.Set(key, value) | ||
} | ||
|
||
// DeleteHistoricalInfo deletes the historical info at a given height | ||
func (k Keeper) DeleteHistoricalInfo(ctx sdk.Context, height int64) { | ||
store := ctx.KVStore(k.storeKey) | ||
key := types.GetHistoricalInfoKey(height) | ||
|
||
store.Delete(key) | ||
} | ||
|
||
// TrackHistoricalInfo saves the latest historical-info and deletes the oldest | ||
// heights that are below pruning height | ||
func (k Keeper) TrackHistoricalInfo(ctx sdk.Context) { | ||
entryNum := k.HistoricalEntries(ctx) | ||
|
||
// Prune store to ensure we only have parameter-defined historical entries. | ||
// In most cases, this will involve removing a single historical entry. | ||
// In the rare scenario when the historical entries gets reduced to a lower value k' | ||
// from the original value k. k - k' entries must be deleted from the store. | ||
// Since the entries to be deleted are always in a continuous range, we can iterate | ||
// over the historical entries starting from the most recent version to be pruned | ||
// and then return at the first empty entry. | ||
for i := ctx.BlockHeight() - int64(entryNum); i >= 0; i-- { | ||
_, found := k.GetHistoricalInfo(ctx, i) | ||
if found { | ||
k.DeleteHistoricalInfo(ctx, i) | ||
} else { | ||
break | ||
} | ||
} | ||
|
||
// if there is no need to persist historicalInfo, return | ||
if entryNum == 0 { | ||
return | ||
} | ||
|
||
// Create HistoricalInfo struct | ||
lastVals := k.GetLastValidators(ctx) | ||
historicalEntry := types.NewHistoricalInfo(ctx.BlockHeader(), lastVals) | ||
|
||
// Set latest HistoricalInfo at current height | ||
k.SetHistoricalInfo(ctx, ctx.BlockHeight(), historicalEntry) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A single changelog entry should be made with bullet points if need be.