forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR cosmos#5380: ADR 17 Implementation: Historical Module
- Loading branch information
1 parent
908fd2b
commit fca4cbe
Showing
30 changed files
with
827 additions
and
175 deletions.
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.