Skip to content
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

test(02-client): adding tests for GetLatestHeight and refactor tests for GetTimestamptAtHeight #6002

Merged
merged 3 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading