Skip to content

Commit

Permalink
Add client ICQ impl
Browse files Browse the repository at this point in the history
  • Loading branch information
agouin committed Jan 29, 2023
1 parent b3424f5 commit 9b359d4
Show file tree
Hide file tree
Showing 14 changed files with 501 additions and 41 deletions.
3 changes: 2 additions & 1 deletion proto/stride/interchainquery/v1/messages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.proto";
import "tendermint/crypto/proof.proto";

option go_package = "github.com/Stride-Labs/stride/v5/x/interchainquery/types";
// NOTE: copied into this repository from "github.com/Stride-Labs/stride/v5/x/interchainquery/types"
option go_package = "github.com/cosmos/relayer/relayer/chains/cosmos/stride";

// MsgSubmitQueryResponse represents a message type to fulfil a query request.
message MsgSubmitQueryResponse {
Expand Down
65 changes: 65 additions & 0 deletions relayer/chains/cosmos/event_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cosmos

import (
"encoding/hex"
"fmt"
"strconv"
"strings"

Expand Down Expand Up @@ -103,6 +104,17 @@ func parseIBCMessageFromEvent(
eventType: event.Type,
info: ci,
}

case processor.ClientICQTypeRequest, processor.ClientICQTypeResponse:
ci := &clientICQInfo{
Height: height,
Source: chainID,
}
ci.parseAttrs(log, event.Attributes)
return &ibcMessage{
eventType: event.Type,
info: ci,
}
}
return nil
}
Expand Down Expand Up @@ -379,3 +391,56 @@ func (res *connectionInfo) parseConnectionAttribute(attr sdk.Attribute) {
res.CounterpartyClientID = attr.Value
}
}

type clientICQInfo struct {
Source string
Connection string
Chain string
QueryID provider.ClientICQQueryID
Type string
Request []byte
Height uint64
}

func (res *clientICQInfo) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddString("connection_id", res.Connection)
enc.AddString("chain_id", res.Chain)
enc.AddString("query_id", string(res.QueryID))
enc.AddString("type", res.Type)
enc.AddString("request", hex.EncodeToString(res.Request))
enc.AddUint64("height", res.Height)

return nil
}

func (res *clientICQInfo) parseAttrs(log *zap.Logger, attrs []sdk.Attribute) {
for _, attr := range attrs {
if err := res.parseAttribute(attr); err != nil {
panic(fmt.Errorf("failed to parse attributes from client ICQ message: %w", err))
}
}
}

func (res *clientICQInfo) parseAttribute(attr sdk.Attribute) (err error) {
switch attr.Key {
case "connection_id":
res.Connection = attr.Value
case "chain_id":
res.Chain = attr.Value
case "query_id":
res.QueryID = provider.ClientICQQueryID(attr.Value)
case "type":
res.Type = attr.Value
case "request":
res.Request, err = hex.DecodeString(attr.Value)
if err != nil {
return err
}
case "height":
res.Height, err = strconv.ParseUint(attr.Value, 10, 64)
if err != nil {
return err
}
}
return nil
}
23 changes: 23 additions & 0 deletions relayer/chains/cosmos/message_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cosmos

import (
"context"
"encoding/hex"

conntypes "github.com/cosmos/ibc-go/v5/modules/core/03-connection/types"
chantypes "github.com/cosmos/ibc-go/v5/modules/core/04-channel/types"
Expand All @@ -21,6 +22,8 @@ func (ccp *CosmosChainProcessor) handleMessage(ctx context.Context, m ibcMessage
ccp.handleConnectionMessage(m.eventType, provider.ConnectionInfo(*t), c)
case *clientInfo:
ccp.handleClientMessage(ctx, m.eventType, *t)
case *clientICQInfo:
ccp.handleClientICQMessage(m.eventType, provider.ClientICQInfo(*t), c)
}
}

Expand Down Expand Up @@ -133,6 +136,15 @@ func (ccp *CosmosChainProcessor) handleClientMessage(ctx context.Context, eventT
ccp.logObservedIBCMessage(eventType, zap.String("client_id", ci.clientID))
}

func (ccp *CosmosChainProcessor) handleClientICQMessage(
eventType string,
ci provider.ClientICQInfo,
c processor.IBCMessagesCache,
) {
c.ClientICQ.Retain(processor.ClientICQType(eventType), ci)
ccp.logClientICQMessage(eventType, ci)
}

func (ccp *CosmosChainProcessor) logObservedIBCMessage(m string, fields ...zap.Field) {
ccp.log.With(zap.String("event_type", m)).Debug("Observed IBC message", fields...)
}
Expand Down Expand Up @@ -178,3 +190,14 @@ func (ccp *CosmosChainProcessor) logConnectionMessage(message string, ci provide
zap.String("counterparty_connection_id", ci.CounterpartyConnID),
)
}

func (ccp *CosmosChainProcessor) logClientICQMessage(icqType string, ci provider.ClientICQInfo) {
ccp.logObservedIBCMessage(icqType,
zap.String("type", ci.Type),
zap.String("query_id", string(ci.QueryID)),
zap.String("request", hex.EncodeToString(ci.Request)),
zap.String("chain_id", ci.Chain),
zap.String("connection_id", ci.Connection),
zap.Uint64("height", ci.Height),
)
}
24 changes: 24 additions & 0 deletions relayer/chains/cosmos/stride/codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package stride

import (
"github.com/cosmos/cosmos-sdk/codec"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
)

// Originally sourced from https://github.com/Stride-Labs/stride/blob/v5.1.1/x/interchainquery/types/codec.go
// Needed for cosmos sdk Msg implementation in messages.go.

var (
amino = codec.NewLegacyAmino()
ModuleCdc = codec.NewAminoCodec(amino)
)

func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgSubmitQueryResponse{}, "/stride.interchainquery.MsgSubmitQueryResponse", nil)
}

func init() {
RegisterLegacyAminoCodec(amino)
cryptocodec.RegisterCrypto(amino)
amino.Seal()
}
51 changes: 51 additions & 0 deletions relayer/chains/cosmos/stride/messages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package stride

import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// Originally sourced from https://github.com/Stride-Labs/stride/blob/v5.1.1/x/interchainquery/types/msgs.go
// Needed for cosmos sdk Msg implementation.

// interchainquery message types
const (
TypeMsgSubmitQueryResponse = "submitqueryresponse"

// RouterKey is the message route for icq
RouterKey = "interchainquery"
)

var _ sdk.Msg = &MsgSubmitQueryResponse{}

// Route Implements Msg.
func (msg MsgSubmitQueryResponse) Route() string { return RouterKey }

// Type Implements Msg.
func (msg MsgSubmitQueryResponse) Type() string { return TypeMsgSubmitQueryResponse }

// ValidateBasic Implements Msg.
func (msg MsgSubmitQueryResponse) ValidateBasic() error {
// check from address
_, err := sdk.AccAddressFromBech32(msg.FromAddress)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid fromAddress in ICQ response (%s)", err)
}
// check chain_id is not empty
if msg.ChainId == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "chain_id cannot be empty in ICQ response")
}

return nil
}

// GetSignBytes Implements Msg.
func (msg MsgSubmitQueryResponse) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}

// GetSigners Implements Msg.
func (msg MsgSubmitQueryResponse) GetSigners() []sdk.AccAddress {
fromAddress, _ := sdk.AccAddressFromBech32(msg.FromAddress)
return []sdk.AccAddress{fromAddress}
}
57 changes: 28 additions & 29 deletions relayer/chains/cosmos/stride/messages.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions relayer/chains/cosmos/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import (
host "github.com/cosmos/ibc-go/v5/modules/core/24-host"
ibcexported "github.com/cosmos/ibc-go/v5/modules/core/exported"
tmclient "github.com/cosmos/ibc-go/v5/modules/light-clients/07-tendermint/types"
strideicqtypes "github.com/cosmos/relayer/v2/relayer/chains/cosmos/stride"
"github.com/cosmos/relayer/v2/relayer/provider"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/light"
tmtypes "github.com/tendermint/tendermint/types"
"go.uber.org/zap"
Expand Down Expand Up @@ -891,6 +893,43 @@ func (cc *CosmosProvider) MsgUpdateClientHeader(latestHeader provider.IBCHeader,
}, nil
}

func (cc *CosmosProvider) QueryICQWithProof(ctx context.Context, path string, request []byte, height uint64) (provider.ICQProof, error) {
slashSplit := strings.Split(path, "/")
req := abci.RequestQuery{
Path: path,
Height: int64(height),
Data: request,
Prove: slashSplit[len(slashSplit)-1] == "key",
}

res, err := cc.QueryABCI(ctx, req)
if err != nil {
return provider.ICQProof{}, fmt.Errorf("failed to execute interchain query: %w", err)
}
return provider.ICQProof{
Result: res.Value,
ProofOps: res.ProofOps,
Height: res.Height,
}, nil
}

func (cc *CosmosProvider) MsgSubmitQueryResponse(chainID string, queryID provider.ClientICQQueryID, proof provider.ICQProof) (provider.RelayerMessage, error) {
signer, err := cc.Address()
if err != nil {
return nil, err
}
msg := &strideicqtypes.MsgSubmitQueryResponse{
ChainId: chainID,
QueryId: string(queryID),
Result: proof.Result,
ProofOps: proof.ProofOps,
Height: proof.Height,
FromAddress: signer,
}

return NewCosmosMessage(msg), nil
}

// RelayPacketFromSequence relays a packet with a given seq on src and returns recvPacket msgs, timeoutPacketmsgs and error
func (cc *CosmosProvider) RelayPacketFromSequence(
ctx context.Context,
Expand Down
Loading

0 comments on commit 9b359d4

Please sign in to comment.