Skip to content

Commit

Permalink
Remove duplicate code for client state queries (cosmos#495)
Browse files Browse the repository at this point in the history
* Remove duplicate code for QueryClientState

* Fix lint issues

* Fix issue

* Address suggested change
  • Loading branch information
akhilkumarpilli committed Apr 26, 2021
1 parent 8d95d12 commit 71e9d4d
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 62 deletions.
2 changes: 1 addition & 1 deletion cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ func QueryClientHandler(w http.ResponseWriter, r *http.Request) {
return
}

res, err := chain.QueryClientState(height)
res, err := chain.QueryClientStateResponse(height)
if err != nil {
helpers.WriteErrorResponse(http.StatusInternalServerError, err, w)
return
Expand Down
2 changes: 1 addition & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ func (c *Config) ValidateClient(chain *relayer.Chain, height int64, pe *relayer.
return err
}

_, err := chain.QueryClientState(height)
_, err := chain.QueryClientStateResponse(height)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ $ %s query client ibc-0 ibczeroclient --height 1205`,
return err
}

res, err := chain.QueryClientState(height)
res, err := chain.QueryClientStateResponse(height)
if err != nil {
return err
}
Expand Down
8 changes: 2 additions & 6 deletions relayer/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ func (c *Chain) GenerateConnHandshakeProof(height uint64) (clientState ibcexport
)

// query for the client state for the proof and get the height to query the consensus state at.
clientStateRes, err = c.QueryClientState(int64(height))
clientStateRes, err = c.QueryClientStateResponse(int64(height))
if err != nil {
return nil, nil, nil, nil, clienttypes.Height{}, err
}
Expand Down Expand Up @@ -689,11 +689,7 @@ func (c *Chain) UpgradeChain(dst *Chain, plan *upgradetypes.Plan, deposit sdk.Co
}
height := int64(dst.MustGetLatestLightHeight())

clientStateRes, err := dst.QueryClientState(height)
if err != nil {
return err
}
clientState, err := clienttypes.UnpackClientState(clientStateRes.ClientState)
clientState, err := dst.QueryClientState(height)
if err != nil {
return err
}
Expand Down
27 changes: 4 additions & 23 deletions relayer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (c *Chain) CreateClients(dst *Chain, allowUpdateAfterExpiry,
} else {
// Ensure client exists in the event of user inputted identifiers
// TODO: check client is not expired
_, err := c.QueryClientState(srcUpdateHeader.Header.Height)
_, err := c.QueryClientStateResponse(srcUpdateHeader.Header.Height)
if err != nil {
return false, fmt.Errorf("please ensure provided on-chain client (%s) exists on the chain (%s): %v",
c.PathEnd.ClientID, c.ChainID, err)
Expand Down Expand Up @@ -170,7 +170,7 @@ func (c *Chain) CreateClients(dst *Chain, allowUpdateAfterExpiry,
} else {
// Ensure client exists in the event of user inputted identifiers
// TODO: check client is not expired
_, err := dst.QueryClientState(dstUpdateHeader.Header.Height)
_, err := dst.QueryClientStateResponse(dstUpdateHeader.Header.Height)
if err != nil {
return false, fmt.Errorf("please ensure provided on-chain client (%s) exists on the chain (%s): %v",
dst.PathEnd.ClientID, dst.ChainID, err)
Expand Down Expand Up @@ -283,17 +283,11 @@ func FindMatchingClient(source, counterparty *Chain, clientState *ibctmtypes.Cli

for _, identifiedClientState := range clientsResp.ClientStates {
// unpack any into ibc tendermint client state
clientStateExported, err := clienttypes.UnpackClientState(identifiedClientState.ClientState)
existingClientState, err := CastClientStateToTMType(identifiedClientState.ClientState)
if err != nil {
return "", false
}

// cast from interface to concrete type
existingClientState, ok := clientStateExported.(*ibctmtypes.ClientState)
if !ok {
return "", false
}

// check if the client states match
// NOTE: IsFrozen is a sanity check, the client to be created should always
// have a zero frozen height and therefore should never match with a frozen client
Expand Down Expand Up @@ -377,24 +371,11 @@ func AutoUpdateClient(src, dst *Chain, thresholdTime time.Duration) (time.Durati
return 0, err
}

clientStateRes, err := src.QueryClientState(height)
clientState, err := src.QueryTMClientState(height)
if err != nil {
return 0, err
}

// unpack any into ibc tendermint client state
clientStateExported, err := clienttypes.UnpackClientState(clientStateRes.ClientState)
if err != nil {
return 0, err
}

// cast from interface to concrete type
clientState, ok := clientStateExported.(*ibctmtypes.ClientState)
if !ok {
return 0, fmt.Errorf("error when casting exported clientstate with clientID %s on chain: %s",
src.PathEnd.ClientID, src.PathEnd.ChainID)
}

if clientState.TrustingPeriod <= thresholdTime {
return 0, fmt.Errorf("client (%s) trusting period time is less than or equal to threshold time",
src.PathEnd.ClientID)
Expand Down
6 changes: 1 addition & 5 deletions relayer/ibc-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,10 @@ func (c *Chain) InjectTrustedFields(dst *Chain, header *tmclient.Header) (*tmcli

// retrieve counterparty client from src chain
// this is the client that will updated
counterpartyClientRes, err := dst.QueryClientState(0)
cs, err := dst.QueryClientState(0)
if err != nil {
return nil, err
}
cs, err := clienttypes.UnpackClientState(counterpartyClientRes.ClientState)
if err != nil {
panic(err)
}

// inject TrustedHeight as latest height stored on counterparty client
h.TrustedHeight = cs.GetLatestHeight().(clienttypes.Height)
Expand Down
4 changes: 2 additions & 2 deletions relayer/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ func (p *Path) QueryPathStatus(src, dst *Chain) *PathWithStatus {
}

eg.Go(func() error {
srcCs, err = src.QueryClientState(srch)
srcCs, err = src.QueryClientStateResponse(srch)
return err
})
eg.Go(func() error {
dstCs, err = dst.QueryClientState(dsth)
dstCs, err = dst.QueryClientStateResponse(dsth)
return err
})
if err = eg.Wait(); err != nil || srcCs == nil || dstCs == nil {
Expand Down
52 changes: 48 additions & 4 deletions relayer/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,66 @@ func QueryClientConsensusStatePair(
return
}

// QueryClientState retrevies the latest consensus state for a client in state at a given height
func (c *Chain) QueryClientState(height int64) (*clienttypes.QueryClientStateResponse, error) {
// QueryClientStateResponse retrevies the latest consensus state for a client in state at a given height
func (c *Chain) QueryClientStateResponse(height int64) (*clienttypes.QueryClientStateResponse, error) {
return clientutils.QueryClientStateABCI(c.CLIContext(height), c.PathEnd.ClientID)
}

// QueryClientState retrevies the latest consensus state for a client in state at a given height
// and unpacks it to exported client state interface
func (c *Chain) QueryClientState(height int64) (ibcexported.ClientState, error) {
clientStateRes, err := c.QueryClientStateResponse(height)
if err != nil {
return nil, err
}

clientStateExported, err := clienttypes.UnpackClientState(clientStateRes.ClientState)
if err != nil {
return nil, err
}

return clientStateExported, nil
}

// QueryTMClientState retrevies the latest consensus state for a client in state at a given height
// and unpacks/cast it to tendermint clientstate
func (c *Chain) QueryTMClientState(height int64) (*tmclient.ClientState, error) {
clientStateRes, err := c.QueryClientStateResponse(height)
if err != nil {
return &tmclient.ClientState{}, err
}

return CastClientStateToTMType(clientStateRes.ClientState)
}

// CastClientStateToTMType casts client state to tendermint type
func CastClientStateToTMType(cs *codectypes.Any) (*tmclient.ClientState, error) {
clientStateExported, err := clienttypes.UnpackClientState(cs)
if err != nil {
return &tmclient.ClientState{}, err
}

// cast from interface to concrete type
clientState, ok := clientStateExported.(*tmclient.ClientState)
if !ok {
return &tmclient.ClientState{},
fmt.Errorf("error when casting exported clientstate to tendermint type")
}

return clientState, nil
}

// QueryClientStatePair returns a pair of connection responses
func QueryClientStatePair(
src, dst *Chain,
srch, dsth int64) (srcCsRes, dstCsRes *clienttypes.QueryClientStateResponse, err error) {
var eg = new(errgroup.Group)
eg.Go(func() error {
srcCsRes, err = src.QueryClientState(srch)
srcCsRes, err = src.QueryClientStateResponse(srch)
return err
})
eg.Go(func() error {
dstCsRes, err = dst.QueryClientState(dsth)
dstCsRes, err = dst.QueryClientStateResponse(dsth)
return err
})
err = eg.Wait()
Expand Down
20 changes: 2 additions & 18 deletions test/relayer_gaia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,9 @@ func TestGaiaMisbehaviourMonitoring(t *testing.T) {
header, err := dst.QueryHeaderAtHeight(latestHeight)
require.NoError(t, err)

clientStateRes, err := src.QueryClientState(latestHeight)
clientState, err := src.QueryTMClientState(latestHeight)
require.NoError(t, err)

// unpack any into ibc tendermint client state
clientStateExported, err := clienttypes.UnpackClientState(clientStateRes.ClientState)
require.NoError(t, err)

// cast from interface to concrete type
clientState, ok := clientStateExported.(*ibctmtypes.ClientState)
require.True(t, ok, "error when casting exported clientstate")

height := clientState.GetLatestHeight().(clienttypes.Height)
heightPlus1 := clienttypes.NewHeight(height.RevisionNumber, height.RevisionHeight+1)

Expand Down Expand Up @@ -256,17 +248,9 @@ func TestGaiaMisbehaviourMonitoring(t *testing.T) {
// kill relayer routine
rlyDone()

clientStateRes, err = src.QueryClientState(0)
clientState, err = src.QueryTMClientState(0)
require.NoError(t, err)

// unpack any into ibc tendermint client state
clientStateExported, err = clienttypes.UnpackClientState(clientStateRes.ClientState)
require.NoError(t, err)

// cast from interface to concrete type
clientState, ok = clientStateExported.(*ibctmtypes.ClientState)
require.True(t, ok, "error when casting exported clientstate")

// clientstate should be frozen
require.True(t, clientState.IsFrozen())
}
Expand Down
2 changes: 1 addition & 1 deletion test/test_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func testClient(t *testing.T, src, dst *ry.Chain) {
client *clientypes.QueryClientStateResponse
)
if err = retry.Do(func() error {
client, err = src.QueryClientState(srch)
client, err = src.QueryClientStateResponse(srch)
if err != nil {
srch, _ = src.QueryLatestHeight()
}
Expand Down

0 comments on commit 71e9d4d

Please sign in to comment.