Skip to content

Commit d327c07

Browse files
authored
test: adding tests for 02-client GetLatestHeight and refactor tests for GetTimestamptAtHeight (#6002)
1 parent 70d78b8 commit d327c07

File tree

2 files changed

+97
-26
lines changed

2 files changed

+97
-26
lines changed

modules/core/02-client/keeper/keeper.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ func (k Keeper) GetLatestHeight(ctx sdk.Context, clientID string) types.Height {
501501
func (k Keeper) GetTimestampAtHeight(ctx sdk.Context, clientID string, height exported.Height) (uint64, error) {
502502
clientType, _, err := types.ParseClientIdentifier(clientID)
503503
if err != nil {
504-
return 0, errorsmod.Wrapf(types.ErrClientNotFound, "clientID (%s)", clientID)
504+
return 0, errorsmod.Wrapf(err, "clientID (%s)", clientID)
505505
}
506506

507507
if !k.GetParams(ctx).IsAllowedClient(clientType) {

modules/core/02-client/keeper/keeper_test.go

+96-25
Original file line numberDiff line numberDiff line change
@@ -455,59 +455,130 @@ func (suite *KeeperTestSuite) TestIterateClientStates() {
455455
}
456456
}
457457

458+
func (suite *KeeperTestSuite) TestGetLatestHeight() {
459+
var path *ibctesting.Path
460+
461+
cases := []struct {
462+
name string
463+
malleate func()
464+
expPass bool
465+
}{
466+
{
467+
"success",
468+
func() {},
469+
true,
470+
},
471+
{
472+
"invalid client type",
473+
func() {
474+
path.EndpointA.ClientID = ibctesting.InvalidID
475+
},
476+
false,
477+
},
478+
{
479+
"client type is not allowed", func() {
480+
params := types.NewParams(exported.Localhost)
481+
suite.chainA.GetSimApp().GetIBCKeeper().ClientKeeper.SetParams(suite.chainA.GetContext(), params)
482+
},
483+
false,
484+
},
485+
{
486+
"client type is not registered on router", func() {
487+
path.EndpointA.ClientID = types.FormatClientIdentifier("08-wasm", 0)
488+
},
489+
false,
490+
},
491+
}
492+
493+
for _, tc := range cases {
494+
suite.Run(tc.name, func() {
495+
suite.SetupTest() // reset
496+
497+
path = ibctesting.NewPath(suite.chainA, suite.chainB)
498+
path.SetupConnections()
499+
500+
tc.malleate()
501+
502+
height := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetLatestHeight(suite.chainA.GetContext(), path.EndpointA.ClientID)
503+
504+
if tc.expPass {
505+
suite.Require().Equal(suite.chainB.LatestCommittedHeader.GetHeight().(types.Height), height)
506+
} else {
507+
suite.Require().Equal(types.ZeroHeight(), height)
508+
}
509+
})
510+
}
511+
}
512+
458513
func (suite *KeeperTestSuite) TestGetTimestampAtHeight() {
459514
var (
460-
clientID string
461-
height exported.Height
515+
height exported.Height
516+
path *ibctesting.Path
462517
)
463518

464519
cases := []struct {
465-
msg string
520+
name string
466521
malleate func()
467-
expPass bool
522+
expError error
468523
}{
469524
{
470-
"verification success",
525+
"success",
526+
func() {},
527+
nil,
528+
},
529+
{
530+
"invalid client type",
471531
func() {
472-
path := ibctesting.NewPath(suite.chainA, suite.chainB)
473-
path.SetupConnections()
474-
475-
clientID = path.EndpointA.ClientID
476-
height = suite.chainB.LatestCommittedHeader.GetHeight()
532+
path.EndpointA.ClientID = ibctesting.InvalidID
477533
},
478-
true,
534+
host.ErrInvalidID,
479535
},
480536
{
481-
"client state not found",
482-
func() {},
483-
false,
537+
"client type is not allowed", func() {
538+
params := types.NewParams(exported.Localhost)
539+
suite.chainA.GetSimApp().GetIBCKeeper().ClientKeeper.SetParams(suite.chainA.GetContext(), params)
540+
},
541+
types.ErrInvalidClientType,
542+
},
543+
{
544+
"client type is not registered on router", func() {
545+
path.EndpointA.ClientID = types.FormatClientIdentifier("08-wasm", 0)
546+
},
547+
types.ErrRouteNotFound,
548+
},
549+
{
550+
"client state not found", func() {
551+
path.EndpointA.ClientID = types.FormatClientIdentifier(exported.Tendermint, 100)
552+
},
553+
types.ErrClientNotFound,
484554
},
485555
{
486556
"consensus state not found", func() {
487-
path := ibctesting.NewPath(suite.chainA, suite.chainB)
488-
path.SetupConnections()
489-
clientID = path.EndpointA.ClientID
490557
height = suite.chainB.LatestCommittedHeader.GetHeight().Increment()
491558
},
492-
false,
559+
types.ErrConsensusStateNotFound,
493560
},
494561
}
495562

496563
for _, tc := range cases {
497-
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
564+
suite.Run(tc.name, func() {
498565
suite.SetupTest() // reset
499566

567+
path = ibctesting.NewPath(suite.chainA, suite.chainB)
568+
path.SetupConnections()
569+
570+
height = suite.chainB.LatestCommittedHeader.GetHeight()
571+
500572
tc.malleate()
501573

502-
actualTimestamp, err := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetTimestampAtHeight(
503-
suite.chainA.GetContext(), clientID, height,
504-
)
574+
actualTimestamp, err := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetTimestampAtHeight(suite.chainA.GetContext(), path.EndpointA.ClientID, height)
505575

506-
if tc.expPass {
576+
expPass := tc.expError == nil
577+
if expPass {
507578
suite.Require().NoError(err)
508-
suite.Require().EqualValues(uint64(suite.chainB.LatestCommittedHeader.GetTime().UnixNano()), actualTimestamp)
579+
suite.Require().Equal(uint64(suite.chainB.LatestCommittedHeader.GetTime().UnixNano()), actualTimestamp)
509580
} else {
510-
suite.Require().Error(err)
581+
suite.Require().ErrorIs(err, tc.expError)
511582
}
512583
})
513584
}

0 commit comments

Comments
 (0)