@@ -12,6 +12,7 @@ import (
1212 "github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/keeper"
1313 "github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types"
1414 commitmenttypes "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types"
15+ "github.com/cosmos/ibc-go/v9/modules/core/exported"
1516 ibctesting "github.com/cosmos/ibc-go/v9/testing"
1617)
1718
@@ -190,6 +191,113 @@ func (suite *KeeperTestSuite) TestQueryChannelClientState() {
190191 }
191192}
192193
194+ func (suite * KeeperTestSuite ) TestQueryChannelConsensusState () {
195+ var (
196+ req * types.QueryChannelConsensusStateRequest
197+ expConsensusState exported.ConsensusState
198+ expClientID string
199+ )
200+
201+ testCases := []struct {
202+ msg string
203+ malleate func ()
204+ expError error
205+ }{
206+ {
207+ "success" ,
208+ func () {
209+ path := ibctesting .NewPath (suite .chainA , suite .chainB )
210+ path .SetupV2 ()
211+
212+ expConsensusState , _ = suite .chainA .GetConsensusState (path .EndpointA .ClientID , path .EndpointA .GetClientLatestHeight ())
213+ suite .Require ().NotNil (expConsensusState )
214+ expClientID = path .EndpointA .ClientID
215+
216+ req = & types.QueryChannelConsensusStateRequest {
217+ ChannelId : path .EndpointA .ChannelID ,
218+ RevisionNumber : path .EndpointA .GetClientLatestHeight ().GetRevisionNumber (),
219+ RevisionHeight : path .EndpointA .GetClientLatestHeight ().GetRevisionHeight (),
220+ }
221+ },
222+ nil ,
223+ },
224+ {
225+ "empty request" ,
226+ func () {
227+ req = nil
228+ },
229+ status .Error (codes .InvalidArgument , "empty request" ),
230+ },
231+ {
232+ "invalid channel ID" ,
233+ func () {
234+ req = & types.QueryChannelConsensusStateRequest {
235+ ChannelId : "" ,
236+ RevisionNumber : 0 ,
237+ RevisionHeight : 1 ,
238+ }
239+ },
240+ status .Error (codes .InvalidArgument , "identifier cannot be blank: invalid identifier" ),
241+ },
242+ {
243+ "channel not found" ,
244+ func () {
245+ req = & types.QueryChannelConsensusStateRequest {
246+ ChannelId : "test-channel-id" ,
247+ RevisionNumber : 0 ,
248+ RevisionHeight : 1 ,
249+ }
250+ },
251+ status .Error (codes .NotFound , fmt .Sprintf ("channel-id: %s: channel not found" , "test-channel-id" )),
252+ },
253+ {
254+ "consensus state for channel's connection not found" ,
255+ func () {
256+ path := ibctesting .NewPath (suite .chainA , suite .chainB )
257+ path .SetupV2 ()
258+
259+ req = & types.QueryChannelConsensusStateRequest {
260+ ChannelId : path .EndpointA .ChannelID ,
261+ RevisionNumber : 0 ,
262+ RevisionHeight : uint64 (suite .chainA .GetContext ().BlockHeight ()), // use current height
263+ }
264+ },
265+ status .Error (codes .NotFound , fmt .Sprintf ("client-id: %s: consensus state not found" , "07-tendermint-0" )),
266+ },
267+ }
268+
269+ for _ , tc := range testCases {
270+ tc := tc
271+
272+ suite .Run (fmt .Sprintf ("Case %s" , tc .msg ), func () {
273+ suite .SetupTest () // reset
274+
275+ tc .malleate ()
276+ ctx := suite .chainA .GetContext ()
277+
278+ queryServer := keeper .NewQueryServer (suite .chainA .App .GetIBCKeeper ().ChannelKeeperV2 )
279+ res , err := queryServer .ChannelConsensusState (ctx , req )
280+
281+ expPass := tc .expError == nil
282+ if expPass {
283+ suite .Require ().NoError (err )
284+ suite .Require ().NotNil (res )
285+ consensusState , err := clienttypes .UnpackConsensusState (res .ConsensusState )
286+ suite .Require ().NoError (err )
287+ suite .Require ().Equal (expConsensusState , consensusState )
288+ suite .Require ().Equal (expClientID , res .ClientId )
289+
290+ // ensure UnpackInterfaces is defined
291+ cachedValue := res .ConsensusState .GetCachedValue ()
292+ suite .Require ().NotNil (cachedValue )
293+ } else {
294+ suite .Require ().ErrorIs (err , tc .expError )
295+ suite .Require ().Nil (res )
296+ }
297+ })
298+ }
299+ }
300+
193301func (suite * KeeperTestSuite ) TestQueryPacketCommitment () {
194302 var (
195303 expCommitment []byte
0 commit comments