Skip to content

Commit

Permalink
Merge pull request #143 from siburu/channel-upgrade
Browse files Browse the repository at this point in the history
Channel upgrade
  • Loading branch information
siburu authored Aug 30, 2024
2 parents 4ecddcb + dc23bc2 commit 94e7b78
Show file tree
Hide file tree
Showing 29 changed files with 1,841 additions and 188 deletions.
7 changes: 7 additions & 0 deletions chains/tendermint/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ func parseMsgEventLog(ev abcitypes.Event) (core.MsgEventLog, error) {
return nil, err
}
return &event, nil
case chantypes.EventTypeChannelUpgradeOpen:
var event core.EventUpgradeChannel
var err0, err1, err2 error
event.PortID, err0 = getAttributeString(ev, chantypes.AttributeKeyPortID)
event.ChannelID, err1 = getAttributeString(ev, chantypes.AttributeKeyChannelID)
event.UpgradeSequence, err2 = getAttributeUint64(ev, chantypes.AttributeKeyUpgradeSequence)
return &event, errors.Join(err0, err1, err2)
default:
return &core.EventUnknown{Value: ev}, nil
}
Expand Down
88 changes: 83 additions & 5 deletions chains/tendermint/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
committypes "github.com/cosmos/ibc-go/v8/modules/core/23-commitment/types"
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"
"github.com/hyperledger-labs/yui-relayer/core"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// QueryClientState retrevies the latest consensus state for a client in state at a given height
Expand Down Expand Up @@ -55,12 +57,12 @@ var emptyConnRes = conntypes.NewQueryConnectionResponse(
)

// QueryConnection returns the remote end of a given connection
func (c *Chain) QueryConnection(ctx core.QueryContext) (*conntypes.QueryConnectionResponse, error) {
return c.queryConnection(int64(ctx.Height().GetRevisionHeight()), false)
func (c *Chain) QueryConnection(ctx core.QueryContext, connectionID string) (*conntypes.QueryConnectionResponse, error) {
return c.queryConnection(int64(ctx.Height().GetRevisionHeight()), connectionID, false)
}

func (c *Chain) queryConnection(height int64, prove bool) (*conntypes.QueryConnectionResponse, error) {
res, err := connutils.QueryConnection(c.CLIContext(height), c.PathEnd.ConnectionID, prove)
func (c *Chain) queryConnection(height int64, connectionID string, prove bool) (*conntypes.QueryConnectionResponse, error) {
res, err := connutils.QueryConnection(c.CLIContext(height), connectionID, prove)
if err != nil && strings.Contains(err.Error(), "not found") {
return emptyConnRes, nil
} else if err != nil {
Expand Down Expand Up @@ -354,11 +356,13 @@ func (c *Chain) queryWrittenAcknowledgement(ctx core.QueryContext, seq uint64) (
}

// QueryTxs returns an array of transactions given a tag
func (c *Chain) QueryTxs(height int64, page, limit int, events []string) ([]*ctypes.ResultTx, error) {
func (c *Chain) QueryTxs(maxHeight int64, page, limit int, events []string) ([]*ctypes.ResultTx, error) {
if len(events) == 0 {
return nil, errors.New("must declare at least one event to search")
}

events = append(events, fmt.Sprintf("tx.height<=%d", maxHeight))

if page <= 0 {
return nil, errors.New("page must greater than 0")
}
Expand All @@ -374,6 +378,65 @@ func (c *Chain) QueryTxs(height int64, page, limit int, events []string) ([]*cty
return res.Txs, nil
}

func (c *Chain) QueryChannelUpgrade(ctx core.QueryContext) (*chantypes.QueryUpgradeResponse, error) {
return c.queryChannelUpgrade(int64(ctx.Height().GetRevisionHeight()), false)
}

func (c *Chain) queryChannelUpgrade(height int64, prove bool) (chanRes *chantypes.QueryUpgradeResponse, err error) {
if res, err := chanutils.QueryUpgrade(
c.CLIContext(height),
c.PathEnd.PortID,
c.PathEnd.ChannelID,
prove,
); err != nil {
if st, ok := status.FromError(err); ok && st.Code() == codes.NotFound {
return nil, nil
} else {
return nil, err
}
} else {
return res, nil
}
}

func (c *Chain) QueryChannelUpgradeError(ctx core.QueryContext) (*chantypes.QueryUpgradeErrorResponse, error) {
return c.queryChannelUpgradeError(int64(ctx.Height().GetRevisionHeight()), false)
}

func (c *Chain) queryChannelUpgradeError(height int64, prove bool) (chanRes *chantypes.QueryUpgradeErrorResponse, err error) {
if res, err := chanutils.QueryUpgradeError(
c.CLIContext(height),
c.PathEnd.PortID,
c.PathEnd.ChannelID,
prove,
); err != nil {
if st, ok := status.FromError(err); ok && st.Code() == codes.NotFound {
return nil, nil
} else {
return nil, err
}
} else {
return res, nil
}
}

func (c *Chain) QueryCanTransitionToFlushComplete(ctx core.QueryContext) (bool, error) {
return c.queryCanTransitionToFlushComplete(int64(ctx.Height().GetRevisionHeight()))
}

func (c *Chain) queryCanTransitionToFlushComplete(height int64) (bool, error) {
queryClient := chantypes.NewQueryClient(c.CLIContext(height))
req := chantypes.QueryPacketCommitmentsRequest{
PortId: c.PathEnd.PortID,
ChannelId: c.PathEnd.ChannelID,
}
if res, err := queryClient.PacketCommitments(context.TODO(), &req); err != nil {
return false, err
} else {
return len(res.Commitments) == 0, nil
}
}

/////////////////////////////////////
// STAKING -> HistoricalInfo //
/////////////////////////////////////
Expand Down Expand Up @@ -470,3 +533,18 @@ func recvPacketQuery(channelID string, seq int) []string {
func writeAckQuery(channelID string, seq int) []string {
return []string{fmt.Sprintf("%s.packet_dst_channel='%s'", waTag, channelID), fmt.Sprintf("%s.packet_sequence='%d'", waTag, seq)}
}

func channelUpgradeErrorQuery(channelID string, upgradeSequence uint64) []string {
return []string{
fmt.Sprintf("%s.%s='%s'",
chantypes.EventTypeChannelUpgradeError,
chantypes.AttributeKeyChannelID,
channelID,
),
fmt.Sprintf("%s.%s='%d'",
chantypes.EventTypeChannelUpgradeError,
chantypes.AttributeKeyUpgradeSequence,
upgradeSequence,
),
}
}
56 changes: 50 additions & 6 deletions cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (

"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/gogoproto/jsonpb"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
chantypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"
"github.com/hyperledger-labs/yui-relayer/config"
"github.com/hyperledger-labs/yui-relayer/core"
"github.com/hyperledger-labs/yui-relayer/helpers"
"github.com/spf13/cobra"
"github.com/cosmos/gogoproto/jsonpb"
)

// queryCmd represents the chain command
Expand All @@ -33,6 +34,7 @@ func queryCmd(ctx *config.Context) *cobra.Command {
queryClientCmd(ctx),
queryConnection(ctx),
queryChannel(ctx),
queryChannelUpgrade(ctx),
)

return cmd
Expand Down Expand Up @@ -69,7 +71,7 @@ func queryClientCmd(ctx *config.Context) *cobra.Command {
}
marshaler := jsonpb.Marshaler{}
if json, err := marshaler.MarshalToString(cs); err != nil {
fmt.Println(cs.String())
return err
} else {
fmt.Println(json)
}
Expand Down Expand Up @@ -101,13 +103,13 @@ func queryConnection(ctx *config.Context) *cobra.Command {
return err
}
queryHeight := clienttypes.NewHeight(latestHeight.GetRevisionNumber(), uint64(height))
res, err := c.QueryConnection(core.NewQueryContext(context.TODO(), queryHeight))
res, err := c.QueryConnection(core.NewQueryContext(context.TODO(), queryHeight), c.Path().ConnectionID)
if err != nil {
return err
}
marshaler := jsonpb.Marshaler{}
if json, err := marshaler.MarshalToString(res.Connection); err != nil {
fmt.Println(res.Connection.String())
return err
} else {
fmt.Println(json)
}
Expand All @@ -121,7 +123,7 @@ func queryConnection(ctx *config.Context) *cobra.Command {
func queryChannel(ctx *config.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "channel [path-name] [chain-id]",
Short: "Query the connection state for the given connection id",
Short: "Query the channel state for the given connection id",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
chains, _, _, err := ctx.Config.ChainsFromPath(args[0])
Expand All @@ -143,9 +145,51 @@ func queryChannel(ctx *config.Context) *cobra.Command {
if err != nil {
return err
}

marshaler := jsonpb.Marshaler{}
if json, err := marshaler.MarshalToString(res.Channel); err != nil {
fmt.Println(res.Channel.String())
return err
} else {
fmt.Println(json)
}
return nil
},
}

return heightFlag(cmd)
}

func queryChannelUpgrade(ctx *config.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "channel-upgrade [path-name] [chain-id]",
Short: "Query the channel upgrade state for the given channel id",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
chains, _, _, err := ctx.Config.ChainsFromPath(args[0])
if err != nil {
return err
}
c := chains[args[1]]

height, err := cmd.Flags().GetUint64(flags.FlagHeight)
if err != nil {
return err
}
latestHeight, err := c.LatestHeight()
if err != nil {
return err
}
queryHeight := clienttypes.NewHeight(latestHeight.GetRevisionNumber(), uint64(height))
res, err := c.QueryChannelUpgrade(core.NewQueryContext(context.TODO(), queryHeight))
if err != nil {
return err
} else if res == nil {
res = &chantypes.QueryUpgradeResponse{}
}

marshaler := jsonpb.Marshaler{}
if json, err := marshaler.MarshalToString(&res.Upgrade); err != nil {
return err
} else {
fmt.Println(json)
}
Expand Down
Loading

0 comments on commit 94e7b78

Please sign in to comment.