-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,10 @@ import ( | |
sdk "github.com/cosmos/cosmos-sdk/types" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
ibcclienttypes "github.com/cosmos/cosmos-sdk/x/ibc/core/02-client/types" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/tendermint/tendermint/libs/rand" | ||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types" | ||
"os" | ||
"testing" | ||
"time" | ||
|
@@ -22,7 +25,6 @@ import ( | |
var emptyWasmOpts []wasm.Option = nil | ||
|
||
func TestTgradeExport(t *testing.T) { | ||
t.Skip("Alex, this is not implemented") | ||
db := db.NewMemDB() | ||
gapp := NewTgradeApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, EmptyBaseAppOptions{}, emptyWasmOpts) | ||
genesisState := NewDefaultGenesisState() | ||
|
@@ -44,6 +46,8 @@ func TestTgradeExport(t *testing.T) { | |
|
||
// Making a new app object with the db, so that initchain hasn't been called | ||
newGapp := NewTgradeApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, EmptyBaseAppOptions{}, emptyWasmOpts) | ||
|
||
t.Skip("Alex, export is not implemented") | ||
_, err = newGapp.ExportAppStateAndValidators(false, []string{}) | ||
require.NoError(t, err, "ExportAppStateAndValidators should not have an error") | ||
} | ||
|
@@ -119,3 +123,40 @@ func setGenesis(gapp *TgradeApp) error { | |
gapp.Commit() | ||
return nil | ||
} | ||
|
||
func TestIBCKeeperLazyInitialization(t *testing.T) { | ||
db := db.NewMemDB() | ||
gapp := NewTgradeApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, EmptyBaseAppOptions{}, emptyWasmOpts) | ||
genesisState := NewDefaultGenesisState() | ||
|
||
setupWithSingleValidatorGenTX(t, genesisState) | ||
|
||
stateBytes, err := json.MarshalIndent(genesisState, "", " ") | ||
require.NoError(t, err) | ||
|
||
// Initialize the chain | ||
now := time.Now().UTC() | ||
gapp.InitChain( | ||
abci.RequestInitChain{ | ||
Time: now, | ||
Validators: []abci.ValidatorUpdate{}, | ||
AppStateBytes: stateBytes, | ||
}, | ||
) | ||
gapp.Commit() | ||
// store some historic information | ||
header := tmproto.Header{ChainID: "testing-1", Height: 2, Time: now, AppHash: []byte("myAppHash")} | ||
gapp.BaseApp.BeginBlock(abci.RequestBeginBlock{Header: header}) | ||
gapp.Commit() | ||
|
||
ctx := gapp.BaseApp.NewContext(true, header) | ||
height := ibcclienttypes.Height{RevisionNumber: 1, RevisionHeight: 2} | ||
|
||
// when | ||
// https://github.com/cosmos/cosmos-sdk/blob/v0.42.9/x/ibc/core/02-client/keeper/keeper.go#L252 | ||
state, found := gapp.ibcKeeper.ClientKeeper.GetSelfConsensusState(ctx, height) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This uses the staking adaptor? Can you link to where that call happens? Can we assert that data that we expect to be returned from such an adaptor (unbonding period?) is actually set properly? Not just that it returns "some data" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have extended the test to verify that the data returned is what was persisted before on begin block There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you. Looks convincing. |
||
// then | ||
require.True(t, found) | ||
assert.Equal(t, []byte("myAppHash"), state.GetRoot().GetHash()) | ||
assert.Equal(t, uint64(now.UnixNano()), state.GetTimestamp()) | ||
} |
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))...) | ||
} |
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.