Skip to content

Commit

Permalink
Add testing for GetTimestampAtHeight (#4960)
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitrisJim authored Oct 26, 2023
1 parent a48b631 commit 90027e4
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions modules/light-clients/08-wasm/types/client_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,84 @@ func (suite *TypesTestSuite) TestStatus() {
}
}

func (suite *TypesTestSuite) TestGetTimestampAtHeight() {
var height exported.Height

expectedTimestamp := uint64(time.Now().UnixNano())

testCases := []struct {
name string
malleate func()
expErr error
}{
{
"success",
func() {
suite.mockVM.RegisterQueryCallback(types.TimestampAtHeightMsg{}, func(_ wasmvm.Checksum, _ wasmvmtypes.Env, queryMsg []byte, _ wasmvm.KVStore, _ wasmvm.GoAPI, _ wasmvm.Querier, _ wasmvm.GasMeter, _ uint64, _ wasmvmtypes.UFraction) ([]byte, uint64, error) {
var payload types.QueryMsg
err := json.Unmarshal(queryMsg, &payload)
suite.Require().NoError(err)

suite.Require().NotNil(payload.TimestampAtHeight)
suite.Require().Nil(payload.CheckForMisbehaviour)
suite.Require().Nil(payload.Status)
suite.Require().Nil(payload.ExportMetadata)
suite.Require().Nil(payload.VerifyClientMessage)

resp, err := json.Marshal(types.TimestampAtHeightResult{Timestamp: expectedTimestamp})
suite.Require().NoError(err)

return resp, types.DefaultGasUsed, nil
})
},
nil,
},
{
"failure: contract returns error",
func() {
suite.mockVM.RegisterQueryCallback(types.TimestampAtHeightMsg{}, func(_ wasmvm.Checksum, _ wasmvmtypes.Env, _ []byte, _ wasmvm.KVStore, _ wasmvm.GoAPI, _ wasmvm.Querier, _ wasmvm.GasMeter, _ uint64, _ wasmvmtypes.UFraction) ([]byte, uint64, error) {
return nil, 0, wasmtesting.ErrMockContract
})
},
wasmtesting.ErrMockContract,
},
{
"error: invalid height",
func() {
height = ibcmock.Height{}
},
ibcerrors.ErrInvalidType,
},
}

for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
suite.SetupWasmWithMockVM()

endpoint := wasmtesting.NewWasmEndpoint(suite.chainA)
err := endpoint.CreateClient()
suite.Require().NoError(err)

clientStore := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), endpoint.ClientID)
clientState := endpoint.GetClientState().(*types.ClientState)
height = clientState.GetLatestHeight()

tc.malleate()

timestamp, err := clientState.GetTimestampAtHeight(suite.chainA.GetContext(), clientStore, suite.chainA.App.AppCodec(), height)

expPass := tc.expErr == nil
if expPass {
suite.Require().NoError(err)
suite.Require().Equal(expectedTimestamp, timestamp)
} else {
suite.Require().ErrorIs(err, tc.expErr)
}
})
}
}

func (suite *TypesTestSuite) TestValidate() {
testCases := []struct {
name string
Expand Down

0 comments on commit 90027e4

Please sign in to comment.