-
Notifications
You must be signed in to change notification settings - Fork 10
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
Store historic header information for IBC #93
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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
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,110 @@ | ||
package keeper | ||
|
||
import ( | ||
"fmt" | ||
"github.com/confio/tgrade/x/poe/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
ibccoretypes "github.com/cosmos/cosmos-sdk/x/ibc/core/02-client/types" | ||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" | ||
"strconv" | ||
) | ||
|
||
var _ ibccoretypes.StakingKeeper = &Keeper{} | ||
|
||
// GetHistoricalInfo gets the historical info at a given height | ||
func (k Keeper) GetHistoricalInfo(ctx sdk.Context, height int64) (stakingtypes.HistoricalInfo, bool) { | ||
store := ctx.KVStore(k.storeKey) | ||
key := getHistoricalInfoKey(height) | ||
|
||
value := store.Get(key) | ||
if value == nil { | ||
return stakingtypes.HistoricalInfo{}, false | ||
} | ||
|
||
return stakingtypes.MustUnmarshalHistoricalInfo(k.marshaler, value), true | ||
} | ||
|
||
// SetHistoricalInfo sets the historical info at a given height | ||
func (k Keeper) SetHistoricalInfo(ctx sdk.Context, height int64, hi *stakingtypes.HistoricalInfo) { | ||
store := ctx.KVStore(k.storeKey) | ||
key := getHistoricalInfoKey(height) | ||
value := k.marshaler.MustMarshalBinaryBare(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 := getHistoricalInfoKey(height) | ||
|
||
store.Delete(key) | ||
} | ||
|
||
// iterateHistoricalInfo provides an interator over all stored HistoricalInfo | ||
// objects. For each HistoricalInfo object, cb will be called. If the cb returns | ||
// true, the iterator will close and stop. | ||
func (k Keeper) iterateHistoricalInfo(ctx sdk.Context, cb func(stakingtypes.HistoricalInfo) bool) { | ||
store := ctx.KVStore(k.storeKey) | ||
|
||
iterator := sdk.KVStorePrefixIterator(store, types.HistoricalInfoKey) | ||
defer iterator.Close() | ||
|
||
for ; iterator.Valid(); iterator.Next() { | ||
histInfo := stakingtypes.MustUnmarshalHistoricalInfo(k.marshaler, iterator.Value()) | ||
if cb(histInfo) { | ||
break | ||
} | ||
} | ||
} | ||
|
||
// getAllHistoricalInfo returns all stored HistoricalInfo objects. | ||
func (k Keeper) getAllHistoricalInfo(ctx sdk.Context) []stakingtypes.HistoricalInfo { | ||
var infos []stakingtypes.HistoricalInfo | ||
|
||
k.iterateHistoricalInfo(ctx, func(histInfo stakingtypes.HistoricalInfo) bool { | ||
infos = append(infos, histInfo) | ||
return false | ||
}) | ||
|
||
return infos | ||
} | ||
|
||
// TrackHistoricalInfo saves the latest historical-info and deletes the oldest | ||
// heights that are below pruning height | ||
func (k Keeper) TrackHistoricalInfo(ctx sdk.Context) { | ||
alpe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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. | ||
fmt.Printf("++ height: %d", ctx.BlockHeight()) | ||
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 { | ||
alpe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return | ||
} | ||
|
||
// Create HistoricalInfo struct | ||
var valSet stakingtypes.Validators // not used by IBC so we keep it empty | ||
historicalEntry := stakingtypes.NewHistoricalInfo(ctx.BlockHeader(), valSet) | ||
|
||
// Set latest HistoricalInfo at current height | ||
k.SetHistoricalInfo(ctx, ctx.BlockHeight(), &historicalEntry) | ||
} | ||
|
||
// getHistoricalInfoKey returns a key prefix for indexing HistoricalInfo objects. | ||
func getHistoricalInfoKey(height int64) []byte { | ||
return append(types.HistoricalInfoKey, []byte(strconv.FormatInt(height, 10))...) | ||
} |
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,66 @@ | ||
package keeper | ||
|
||
import ( | ||
"github.com/confio/tgrade/x/poe/types" | ||
fuzz "github.com/google/gofuzz" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types" | ||
|
||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" | ||
) | ||
|
||
func TestGetSetHistoricalInfo(t *testing.T) { | ||
ctx, example := CreateDefaultTestInput(t) | ||
keeper := example.PoEKeeper | ||
var header tmproto.Header | ||
f := fuzz.New() | ||
f.Fuzz(&header) | ||
|
||
ctx = ctx.WithBlockHeight(1).WithBlockHeader(header) | ||
exp := stakingtypes.NewHistoricalInfo(ctx.BlockHeader(), nil) | ||
keeper.SetHistoricalInfo(ctx, 1, &exp) | ||
|
||
// when | ||
got, exists := keeper.GetHistoricalInfo(ctx, 1) | ||
|
||
// then | ||
require.True(t, exists) | ||
assert.Equal(t, exp, got) | ||
} | ||
|
||
func TestTrackHistoricalInfo(t *testing.T) { | ||
ctx, example := CreateDefaultTestInput(t) | ||
keeper := example.PoEKeeper | ||
const maxEntries = 2 | ||
keeper.setParams(ctx, types.Params{HistoricalEntries: maxEntries}) | ||
|
||
// fill all slots | ||
expEntries := make([]stakingtypes.HistoricalInfo, 0, maxEntries+1) | ||
for i := 0; i < maxEntries; i++ { | ||
var header tmproto.Header | ||
f := fuzz.New() | ||
f.Fuzz(&header) | ||
header.Height = int64(1 + i) | ||
header.Time = time.Now().UTC() | ||
keeper.TrackHistoricalInfo(ctx.WithBlockHeader(header)) | ||
expEntries = append(expEntries, stakingtypes.NewHistoricalInfo(header, nil)) | ||
} | ||
|
||
// when new element added | ||
var header tmproto.Header | ||
f := fuzz.New() | ||
f.Fuzz(&header) | ||
header.Height = int64(1 + maxEntries) | ||
header.Time = time.Now().UTC() | ||
keeper.TrackHistoricalInfo(ctx.WithBlockHeader(header)) | ||
|
||
// then only last max entries stored | ||
_, exists := keeper.GetHistoricalInfo(ctx, 1) | ||
require.False(t, exists) | ||
expEntries = append(expEntries, stakingtypes.NewHistoricalInfo(header, nil)) | ||
assert.Equal(t, expEntries[1:], keeper.getAllHistoricalInfo(ctx)) | ||
} |
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.
Ah.. and now it has state once again 😄
But... how can app.poeKeeper be used on line 272 when it is set in 352? This smells funny
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 ref is parsed to the ibc keeper that is updated later. I will add a test that provides more confidence
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 ref is passed to
app.poeKeeper
(probably the zero value).Later we set
app.poeKeeper = &stakingtypes.Adaptor{}
or something like that, which does not update the other reference.If you did
*app.poeKeeper = staking types.Adaptor{}
, then this would update the data that was pointed at by the same reference app.ibcKeeper has.At least this is my understanding of pointers. As it is now, it points into space. This is also a bit harder to reason about when most of these are just empty structs, and return errors on all calls... when there is real state and logic, it will be most difficult
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.
Please note
app.poeKeeper
is of typepoekeeper.Keeper
not a pointer.When I pass the ref here it points to the empty (not nil)
app.poeKeeper
memory address. When thepoeKeeper
is instantiated, it is still the same memory address.https://play.golang.org/p/y9VGNrLUUVC
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.
I need to look into this...
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.
That didn't quite convince me, so I played with it a bit more, to replicate more precisely how we use pointers: https://play.golang.org/p/xBlHLRh_ye_K
And that still matches your expected behaviour.
I guess I really don't understand how all these pointers and interfaces are implemented under the hood in Go, but your code is correct in spite of my confusion.