-
Notifications
You must be signed in to change notification settings - Fork 625
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
Add in-place and genesis migrations #205
Changes from 1 commit
4f874d9
f8ca013
29c1c48
db0db21
972ca3c
314aaba
40a30fa
863fdbd
a36801f
863b50a
545056e
40484bb
a24618d
6d9bcdd
6c6fc52
a67102f
7e1f40f
3896a2a
7edac1e
e326f74
a970700
7ceca61
ca070bd
0fcd0c4
900b907
fed9443
e595b11
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 |
---|---|---|
|
@@ -17,7 +17,7 @@ import ( | |
// - Update solo machine client state protobuf definition (v1 to v2) | ||
// - Remove all solo machine consensus states | ||
// - Remove all expired tendermint consensus states | ||
func MigrateGenesis(cdc codec.BinaryCodec, clientGenState *types.GenesisState, genesisBlockTime time.Time) (*types.GenesisState, error) { | ||
func MigrateGenesis(cdc codec.BinaryCodec, clientGenState *types.GenesisState, genesisBlockTime time.Time, selfHeight exported.Height) (*types.GenesisState, error) { | ||
// To prune the consensus states, we will create new clientsConsensus | ||
// and clientsMetadata. These slices will be filled up with consensus states | ||
// which should not be pruned. No solo machine consensus states should be added | ||
|
@@ -102,9 +102,14 @@ func MigrateGenesis(cdc codec.BinaryCodec, clientGenState *types.GenesisState, g | |
// only unexpired consensus state heights should be added | ||
var clientMetadata []types.GenesisMetadata | ||
for _, metadata := range identifiedGenMetadata.ClientMetadata { | ||
if bytes.Equal(metadata.Key, ibctmtypes.IterationKey(height)) || | ||
bytes.Equal(metadata.Key, ibctmtypes.ProcessedTimeKey(height)) || | ||
bytes.Equal(metadata.Key, ibctmtypes.ProcessedHeightKey(height)) { | ||
// the previous version of IBC only contained the processed time metadata | ||
// if we find the processed time metadata for an unexpired height, add the | ||
// iteration key and processed height keys. | ||
if bytes.Equal(metadata.Key, ibctmtypes.ProcessedTimeKey(height)) { | ||
clientMetadata = append(clientMetadata, types.GenesisMetadata{ | ||
Key: ibctmtypes.ProcessedHeightKey(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. Add godoc saying we're setting processed height to self height, which is still safe |
||
Value: []byte(selfHeight.String()), | ||
}) | ||
clientMetadata = append(clientMetadata, metadata) | ||
AdityaSripal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,7 +87,16 @@ func MigrateStore(ctx sdk.Context, storeKey sdk.StoreKey, cdc codec.BinaryCodec) | |
return sdkerrors.Wrap(err, "failed to unmarshal client state bytes into tendermint client state") | ||
} | ||
|
||
if err = ibctmtypes.PruneAllExpiredConsensusStates(ctx, clientStore, cdc, clientState.(*ibctmtypes.ClientState)); err != nil { | ||
tmClientState, ok := clientState.(*ibctmtypes.ClientState) | ||
if !ok { | ||
return sdkerrors.Wrap(types.ErrInvalidClient, "client state is not tendermint even though client id contains 07-tendermint") | ||
} | ||
|
||
if err = ibctmtypes.PruneAllExpiredConsensusStates(ctx, clientStore, cdc, tmClientState); err != nil { | ||
return err | ||
} | ||
|
||
if err = addConsensusMetadata(ctx, clientStore, cdc, tmClientState); err != nil { | ||
return err | ||
} | ||
|
||
|
@@ -139,3 +148,36 @@ func pruneSolomachineConsensusStates(clientStore sdk.KVStore) { | |
clientStore.Delete(host.ConsensusStateKey(height)) | ||
} | ||
} | ||
|
||
// addConsensusMetadata adds the iteration key and processed height for all unexpired tendermint consensus states | ||
// These keys were not included in the previous release of the IBC module. | ||
func addConsensusMetadata(ctx sdk.Context, clientStore sdk.KVStore, cdc codec.BinaryCodec, clientState *ibctmtypes.ClientState) error { | ||
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 decided not to modify |
||
var heights []exported.Height | ||
|
||
metadataCb := func(height exported.Height) bool { | ||
consState, err := ibctmtypes.GetConsensusState(clientStore, cdc, height) | ||
// this error should never occur | ||
if err != nil { | ||
return true | ||
} | ||
colin-axner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if !clientState.IsExpired(consState.Timestamp, ctx.BlockTime()) { | ||
heights = append(heights, height) | ||
} | ||
|
||
return false | ||
} | ||
|
||
if err := ibctmtypes.IterateConsensusStateAscending(clientStore, metadataCb); err != nil { | ||
return err | ||
} | ||
|
||
for _, height := range heights { | ||
// set the iteration key and processed height | ||
// these keys were not included in the SDK v0.42.0 release | ||
ibctmtypes.SetProcessedHeight(clientStore, height, clienttypes.GetSelfHeight(ctx)) | ||
ibctmtypes.SetIterationKey(clientStore, height) | ||
} | ||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -185,4 +185,21 @@ func (suite *LegacyTestSuite) TestMigrateStoreTendermint() { | |
expectedConsKey := ibctmtypes.GetIterationKey(clientStore, pruneHeight) | ||
suite.Require().Nil(expectedConsKey, i) | ||
} | ||
|
||
// ensure metadata is set for unexpired consensus state | ||
height := path.EndpointA.GetClientState().GetLatestHeight() | ||
consState, ok := path.EndpointA.Chain.GetConsensusState(path.EndpointA.ClientID, height) | ||
suite.Require().True(ok) | ||
suite.Require().NotNil(consState) | ||
|
||
processedTime, ok := ibctmtypes.GetProcessedTime(clientStore, height) | ||
suite.Require().True(ok) | ||
suite.Require().NotEqual(uint64(0), processedTime) | ||
|
||
processedHeight, ok := ibctmtypes.GetProcessedHeight(clientStore, height) | ||
suite.Require().True(ok) | ||
suite.Require().Equal(types.GetSelfHeight(path.EndpointA.Chain.GetContext()), processedHeight) | ||
|
||
consKey := ibctmtypes.GetIterationKey(clientStore, 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. Didn't this metadata already get created in original client update? I guess you proved metadata did something by checking value of processed 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. Yea, the purpose of this to ensure the unexpired consensus state metadata did not get pruned. I wrote this before adding the 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. found a bug, since I was using the iteration keys for iterating, it wasn't updating the metadata since the iteration keys hadn't been set changed the structure, set new metadata for all consensus states (using traditional iteration), then prune expired consensus states |
||
suite.Require().Equal(host.ConsensusStateKey(height), consKey) | ||
} |
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.
Couldn't you do this with just one append call??
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.
wow, I've always worked under the impression
append()
took only 2 arguments