Skip to content

Commit

Permalink
feat: integrate client specific query endpoints for client state and …
Browse files Browse the repository at this point in the history
…consensus state
  • Loading branch information
sonhs-smartosc committed Apr 25, 2024
1 parent d0932e6 commit b70aab3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 46 deletions.
10 changes: 6 additions & 4 deletions relayer/package/services/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ func (gw *Gateway) GetLastHeight() (uint64, error) {
return res.Height, nil
}

func (gw *Gateway) QueryClientState(height uint64) (*pbclient.QueryClientStateResponse, error) {
func (gw *Gateway) QueryClientState(clientId string, height uint64) (*pbclient.QueryClientStateResponse, error) {
req := &pbclient.QueryClientStateRequest{
Height: height,
ClientId: clientId,
Height: height,
}
res, err := gw.ClientQueryService.ClientState(context.Background(), req)
if err != nil {
Expand All @@ -68,9 +69,10 @@ func (gw *Gateway) QueryClientState(height uint64) (*pbclient.QueryClientStateRe
return res, nil
}

func (gw *Gateway) QueryConsensusState(height uint64) (*pbclient.QueryConsensusStateResponse, error) {
func (gw *Gateway) QueryConsensusState(clientId string, height uint64) (*pbclient.QueryConsensusStateResponse, error) {
req := &pbclient.QueryConsensusStateRequest{
Height: height,
ClientId: clientId,
Height: height,
}
res, err := gw.ClientQueryService.ConsensusState(context.Background(), req)
if err != nil {
Expand Down
58 changes: 16 additions & 42 deletions relayer/relayer/chains/cardano/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,6 @@ func (cc *CardanoProvider) GenerateConnHandshakeProof(ctx context.Context, heigh
eg = new(errgroup.Group)
)

clientId = hex.EncodeToString([]byte(clientId))

// query for the client state for the proof and get the height to query the consensus state at.
clientStateRes, err = cc.QueryClientStateResponse(ctx, height, clientId)
if err != nil {
Expand Down Expand Up @@ -208,7 +206,7 @@ func (cc *CardanoProvider) QueryClientStateResponse(ctx context.Context, height
if err != nil {
return nil, err
}
clienStateRes, err := cc.GateWay.QueryClientState(uint64(height))
clienStateRes, err := cc.GateWay.QueryClientState(srcClientId, uint64(height))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -242,31 +240,24 @@ func (cc *CardanoProvider) QueryClientStateResponse(ctx context.Context, height

// QueryClientConsensusState retrieves the latest consensus state for a client in state at a given height
func (cc *CardanoProvider) QueryClientConsensusState(ctx context.Context, chainHeight int64, clientid string, clientHeight ibcexported.Height) (*clienttypes.QueryConsensusStateResponse, error) {
//key := host.FullConsensusStateKey(clientid, clientHeight)
//
//value, proofBz, proofHeight, err := cc.QueryTendermintProof(ctx, chainHeight, key)
value, height, err := cc.QueryConsensusState(ctx, int64(clientHeight.GetRevisionHeight()))
consensusStateRes, err := cc.GateWay.QueryConsensusState(clientid, clientHeight.GetRevisionHeight())
if err != nil {
return nil, err
}
consensusStateRes, err := cc.GateWay.QueryConsensusState(clientHeight.GetRevisionHeight())

var consensusState = tendermint.ConsensusState{}
err = consensusStateRes.GetConsensusState().UnmarshalTo(&consensusState)
if err != nil {
return nil, err
return &clienttypes.QueryConsensusStateResponse{}, err
}
timeStampSeconds := consensusState.Timestamp.Seconds / 1e6
state := &tmclient.ConsensusState{
Timestamp: time.Unix(timeStampSeconds, int64(0)).UTC(),
Root: *consensusState.Root,
NextValidatorsHash: consensusState.NextValidatorsHash,
}

// check if consensus state exists
//if len(value) == 0 {
// return nil, sdkerrors.Wrap(clienttypes.ErrConsensusStateNotFound, clientid)
//}
//
//cdc := codec.NewProtoCodec(cc.Cdc.InterfaceRegistry)
//
//cs, err := clienttypes.UnmarshalConsensusState(cdc, value.va)
//if err != nil {
// return nil, err
//}
println(height)
anyConsensusState, err := clienttypes.PackConsensusState(value)
anyConsensusState, err := clienttypes.PackConsensusState(state)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -405,7 +396,7 @@ func transformIdentifiedChannel(gwIdChannel *pbchannel.IdentifiedChannel) *chant
// QueryClientState retrieves the latest consensus state for a client in state at a given height
// and unpacks it to exported client state interface
func (cc *CardanoProvider) QueryClientState(ctx context.Context, height int64, clientid string) (ibcexported.ClientState, error) {
clientStateRes, err := cc.GateWay.QueryClientState(uint64(height))
clientStateRes, err := cc.GateWay.QueryClientState(clientid, uint64(height))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -557,25 +548,8 @@ func (cc *CardanoProvider) QueryConnectionsUsingClient(ctx context.Context, heig
// QueryConsensusState returns a consensus state for a given chain to be used as a
// client in another chain, fetches latest height when passed 0 as arg
func (cc *CardanoProvider) QueryConsensusState(ctx context.Context, height int64) (ibcexported.ConsensusState, int64, error) {
consensusStateRes, err := cc.GateWay.QueryConsensusState(uint64(height))
if err != nil {
return &tmclient.ConsensusState{}, 0, err
}

var consensusState = tendermint.ConsensusState{}
err = consensusStateRes.GetConsensusState().UnmarshalTo(&consensusState)
if err != nil {
return &tmclient.ConsensusState{}, 0, err
}
timeStampSeconds := consensusState.Timestamp.Seconds / 1e6
timea := time.Unix(timeStampSeconds, int64(0))
state := &tmclient.ConsensusState{
Timestamp: timea.UTC(),
Root: *consensusState.Root,
NextValidatorsHash: consensusState.NextValidatorsHash,
}

return state, height, nil
//not implemented
return &tmclient.ConsensusState{}, 0, nil
}

// QueryDenomTrace takes a denom from IBC and queries the information about it
Expand Down

0 comments on commit b70aab3

Please sign in to comment.