|
| 1 | +package tendermint_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + clienttypes "github.com/cosmos/ibc-go/v6/modules/core/02-client/types" |
| 7 | + host "github.com/cosmos/ibc-go/v6/modules/core/24-host" |
| 8 | + "github.com/cosmos/ibc-go/v6/modules/core/exported" |
| 9 | + ibctm "github.com/cosmos/ibc-go/v6/modules/light-clients/07-tendermint" |
| 10 | + ibctesting "github.com/cosmos/ibc-go/v6/testing" |
| 11 | +) |
| 12 | + |
| 13 | +// test pruning of multiple expired tendermint consensus states |
| 14 | +func (suite *TendermintTestSuite) TestPruneTendermintConsensusStates() { |
| 15 | + // create multiple tendermint clients and a solo machine client |
| 16 | + // the solo machine is used to verify this pruning function only modifies |
| 17 | + // the tendermint store. |
| 18 | + |
| 19 | + numTMClients := 3 |
| 20 | + paths := make([]*ibctesting.Path, numTMClients) |
| 21 | + |
| 22 | + for i := 0; i < numTMClients; i++ { |
| 23 | + path := ibctesting.NewPath(suite.chainA, suite.chainB) |
| 24 | + suite.coordinator.SetupClients(path) |
| 25 | + |
| 26 | + paths[i] = path |
| 27 | + } |
| 28 | + |
| 29 | + solomachine := ibctesting.NewSolomachine(suite.T(), suite.chainA.Codec, "06-solomachine-0", "testing", 1) |
| 30 | + smClientStore := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), solomachine.ClientID) |
| 31 | + |
| 32 | + // set client state |
| 33 | + bz, err := suite.chainA.App.AppCodec().MarshalInterface(solomachine.ClientState()) |
| 34 | + suite.Require().NoError(err) |
| 35 | + smClientStore.Set(host.ClientStateKey(), bz) |
| 36 | + |
| 37 | + bz, err = suite.chainA.App.AppCodec().MarshalInterface(solomachine.ConsensusState()) |
| 38 | + suite.Require().NoError(err) |
| 39 | + smHeight := clienttypes.NewHeight(0, 1) |
| 40 | + smClientStore.Set(host.ConsensusStateKey(smHeight), bz) |
| 41 | + |
| 42 | + pruneHeightMap := make(map[*ibctesting.Path][]exported.Height) |
| 43 | + unexpiredHeightMap := make(map[*ibctesting.Path][]exported.Height) |
| 44 | + |
| 45 | + for _, path := range paths { |
| 46 | + // collect all heights expected to be pruned |
| 47 | + var pruneHeights []exported.Height |
| 48 | + pruneHeights = append(pruneHeights, path.EndpointA.GetClientState().GetLatestHeight()) |
| 49 | + |
| 50 | + // these heights will be expired and also pruned |
| 51 | + for i := 0; i < 3; i++ { |
| 52 | + err := path.EndpointA.UpdateClient() |
| 53 | + suite.Require().NoError(err) |
| 54 | + |
| 55 | + pruneHeights = append(pruneHeights, path.EndpointA.GetClientState().GetLatestHeight()) |
| 56 | + } |
| 57 | + |
| 58 | + // double chedck all information is currently stored |
| 59 | + for _, pruneHeight := range pruneHeights { |
| 60 | + consState, ok := suite.chainA.GetConsensusState(path.EndpointA.ClientID, pruneHeight) |
| 61 | + suite.Require().True(ok) |
| 62 | + suite.Require().NotNil(consState) |
| 63 | + |
| 64 | + ctx := suite.chainA.GetContext() |
| 65 | + clientStore := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(ctx, path.EndpointA.ClientID) |
| 66 | + |
| 67 | + processedTime, ok := ibctm.GetProcessedTime(clientStore, pruneHeight) |
| 68 | + suite.Require().True(ok) |
| 69 | + suite.Require().NotNil(processedTime) |
| 70 | + |
| 71 | + processedHeight, ok := ibctm.GetProcessedHeight(clientStore, pruneHeight) |
| 72 | + suite.Require().True(ok) |
| 73 | + suite.Require().NotNil(processedHeight) |
| 74 | + |
| 75 | + expectedConsKey := ibctm.GetIterationKey(clientStore, pruneHeight) |
| 76 | + suite.Require().NotNil(expectedConsKey) |
| 77 | + } |
| 78 | + pruneHeightMap[path] = pruneHeights |
| 79 | + } |
| 80 | + |
| 81 | + // Increment the time by a week |
| 82 | + suite.coordinator.IncrementTimeBy(7 * 24 * time.Hour) |
| 83 | + |
| 84 | + for _, path := range paths { |
| 85 | + // create the consensus state that can be used as trusted height for next update |
| 86 | + var unexpiredHeights []exported.Height |
| 87 | + err := path.EndpointA.UpdateClient() |
| 88 | + suite.Require().NoError(err) |
| 89 | + unexpiredHeights = append(unexpiredHeights, path.EndpointA.GetClientState().GetLatestHeight()) |
| 90 | + |
| 91 | + err = path.EndpointA.UpdateClient() |
| 92 | + suite.Require().NoError(err) |
| 93 | + unexpiredHeights = append(unexpiredHeights, path.EndpointA.GetClientState().GetLatestHeight()) |
| 94 | + |
| 95 | + unexpiredHeightMap[path] = unexpiredHeights |
| 96 | + } |
| 97 | + |
| 98 | + // Increment the time by another week, then update the client. |
| 99 | + // This will cause the consensus states created before the first time increment |
| 100 | + // to be expired |
| 101 | + suite.coordinator.IncrementTimeBy(7 * 24 * time.Hour) |
| 102 | + err = ibctm.PruneTendermintConsensusStates(suite.chainA.GetContext(), suite.chainA.App.AppCodec(), suite.chainA.GetSimApp().GetKey(host.StoreKey)) |
| 103 | + suite.Require().NoError(err) |
| 104 | + |
| 105 | + for _, path := range paths { |
| 106 | + ctx := suite.chainA.GetContext() |
| 107 | + clientStore := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(ctx, path.EndpointA.ClientID) |
| 108 | + |
| 109 | + // ensure everything has been pruned |
| 110 | + for i, pruneHeight := range pruneHeightMap[path] { |
| 111 | + consState, ok := suite.chainA.GetConsensusState(path.EndpointA.ClientID, pruneHeight) |
| 112 | + suite.Require().False(ok, i) |
| 113 | + suite.Require().Nil(consState, i) |
| 114 | + |
| 115 | + processedTime, ok := ibctm.GetProcessedTime(clientStore, pruneHeight) |
| 116 | + suite.Require().False(ok, i) |
| 117 | + suite.Require().Equal(uint64(0), processedTime, i) |
| 118 | + |
| 119 | + processedHeight, ok := ibctm.GetProcessedHeight(clientStore, pruneHeight) |
| 120 | + suite.Require().False(ok, i) |
| 121 | + suite.Require().Nil(processedHeight, i) |
| 122 | + |
| 123 | + expectedConsKey := ibctm.GetIterationKey(clientStore, pruneHeight) |
| 124 | + suite.Require().Nil(expectedConsKey, i) |
| 125 | + } |
| 126 | + |
| 127 | + // ensure metadata is set for unexpired consensus state |
| 128 | + for _, height := range unexpiredHeightMap[path] { |
| 129 | + consState, ok := suite.chainA.GetConsensusState(path.EndpointA.ClientID, height) |
| 130 | + suite.Require().True(ok) |
| 131 | + suite.Require().NotNil(consState) |
| 132 | + |
| 133 | + processedTime, ok := ibctm.GetProcessedTime(clientStore, height) |
| 134 | + suite.Require().True(ok) |
| 135 | + suite.Require().NotEqual(uint64(0), processedTime) |
| 136 | + |
| 137 | + processedHeight, ok := ibctm.GetProcessedHeight(clientStore, height) |
| 138 | + suite.Require().True(ok) |
| 139 | + suite.Require().NotEqual(clienttypes.ZeroHeight(), processedHeight) |
| 140 | + |
| 141 | + consKey := ibctm.GetIterationKey(clientStore, height) |
| 142 | + suite.Require().Equal(host.ConsensusStateKey(height), consKey) |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + // verify that solomachine client and consensus state were not removed |
| 147 | + smClientStore = suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), solomachine.ClientID) |
| 148 | + bz = smClientStore.Get(host.ClientStateKey()) |
| 149 | + suite.Require().NotEmpty(bz) |
| 150 | + |
| 151 | + bz = smClientStore.Get(host.ConsensusStateKey(smHeight)) |
| 152 | + suite.Require().NotEmpty(bz) |
| 153 | +} |
0 commit comments