Skip to content

Commit

Permalink
test: adding tests for 02-client GetLatestHeight and refactor tests f…
Browse files Browse the repository at this point in the history
…or GetTimestamptAtHeight (cosmos#6002)
  • Loading branch information
damiannolan authored Mar 18, 2024
1 parent 70d78b8 commit d327c07
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 26 deletions.
2 changes: 1 addition & 1 deletion modules/core/02-client/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ func (k Keeper) GetLatestHeight(ctx sdk.Context, clientID string) types.Height {
func (k Keeper) GetTimestampAtHeight(ctx sdk.Context, clientID string, height exported.Height) (uint64, error) {
clientType, _, err := types.ParseClientIdentifier(clientID)
if err != nil {
return 0, errorsmod.Wrapf(types.ErrClientNotFound, "clientID (%s)", clientID)
return 0, errorsmod.Wrapf(err, "clientID (%s)", clientID)
}

if !k.GetParams(ctx).IsAllowedClient(clientType) {
Expand Down
121 changes: 96 additions & 25 deletions modules/core/02-client/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,59 +455,130 @@ func (suite *KeeperTestSuite) TestIterateClientStates() {
}
}

func (suite *KeeperTestSuite) TestGetLatestHeight() {
var path *ibctesting.Path

cases := []struct {
name string
malleate func()
expPass bool
}{
{
"success",
func() {},
true,
},
{
"invalid client type",
func() {
path.EndpointA.ClientID = ibctesting.InvalidID
},
false,
},
{
"client type is not allowed", func() {
params := types.NewParams(exported.Localhost)
suite.chainA.GetSimApp().GetIBCKeeper().ClientKeeper.SetParams(suite.chainA.GetContext(), params)
},
false,
},
{
"client type is not registered on router", func() {
path.EndpointA.ClientID = types.FormatClientIdentifier("08-wasm", 0)
},
false,
},
}

for _, tc := range cases {
suite.Run(tc.name, func() {
suite.SetupTest() // reset

path = ibctesting.NewPath(suite.chainA, suite.chainB)
path.SetupConnections()

tc.malleate()

height := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetLatestHeight(suite.chainA.GetContext(), path.EndpointA.ClientID)

if tc.expPass {
suite.Require().Equal(suite.chainB.LatestCommittedHeader.GetHeight().(types.Height), height)
} else {
suite.Require().Equal(types.ZeroHeight(), height)
}
})
}
}

func (suite *KeeperTestSuite) TestGetTimestampAtHeight() {
var (
clientID string
height exported.Height
height exported.Height
path *ibctesting.Path
)

cases := []struct {
msg string
name string
malleate func()
expPass bool
expError error
}{
{
"verification success",
"success",
func() {},
nil,
},
{
"invalid client type",
func() {
path := ibctesting.NewPath(suite.chainA, suite.chainB)
path.SetupConnections()

clientID = path.EndpointA.ClientID
height = suite.chainB.LatestCommittedHeader.GetHeight()
path.EndpointA.ClientID = ibctesting.InvalidID
},
true,
host.ErrInvalidID,
},
{
"client state not found",
func() {},
false,
"client type is not allowed", func() {
params := types.NewParams(exported.Localhost)
suite.chainA.GetSimApp().GetIBCKeeper().ClientKeeper.SetParams(suite.chainA.GetContext(), params)
},
types.ErrInvalidClientType,
},
{
"client type is not registered on router", func() {
path.EndpointA.ClientID = types.FormatClientIdentifier("08-wasm", 0)
},
types.ErrRouteNotFound,
},
{
"client state not found", func() {
path.EndpointA.ClientID = types.FormatClientIdentifier(exported.Tendermint, 100)
},
types.ErrClientNotFound,
},
{
"consensus state not found", func() {
path := ibctesting.NewPath(suite.chainA, suite.chainB)
path.SetupConnections()
clientID = path.EndpointA.ClientID
height = suite.chainB.LatestCommittedHeader.GetHeight().Increment()
},
false,
types.ErrConsensusStateNotFound,
},
}

for _, tc := range cases {
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
suite.Run(tc.name, func() {
suite.SetupTest() // reset

path = ibctesting.NewPath(suite.chainA, suite.chainB)
path.SetupConnections()

height = suite.chainB.LatestCommittedHeader.GetHeight()

tc.malleate()

actualTimestamp, err := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetTimestampAtHeight(
suite.chainA.GetContext(), clientID, height,
)
actualTimestamp, err := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetTimestampAtHeight(suite.chainA.GetContext(), path.EndpointA.ClientID, height)

if tc.expPass {
expPass := tc.expError == nil
if expPass {
suite.Require().NoError(err)
suite.Require().EqualValues(uint64(suite.chainB.LatestCommittedHeader.GetTime().UnixNano()), actualTimestamp)
suite.Require().Equal(uint64(suite.chainB.LatestCommittedHeader.GetTime().UnixNano()), actualTimestamp)
} else {
suite.Require().Error(err)
suite.Require().ErrorIs(err, tc.expError)
}
})
}
Expand Down

0 comments on commit d327c07

Please sign in to comment.