Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cli command to call recv packet and ack packet #221

Merged
merged 4 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions cmd/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
registerCounterpartyCmd(a),
lineBreakCommand(),
claimFeesCmd(a),
recvPacket(a),
ackPacket(a),
)

return cmd
Expand Down Expand Up @@ -933,6 +935,126 @@
return cmd
}

func recvPacket(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "recv-packet src_chain_name dst_chain_name path_name [txn-hash]",
Short: "update block [height] of src_chain on dst_chain",
Args: withUsage(cobra.RangeArgs(4, 5)),
Example: strings.TrimSpace(
fmt.Sprintf(`
$ %s transact recv-packet icon archway icon-archway [tx_hash] [trusted_height OR {empty} OR {skip-update}]
$ %s transact recv-packet icon archway icon-archway [tx_hash] [trusted_height]
$ %s transact recv-packet icon archway icon-archway [tx_hash]
$ %s transact recv-packet icon archway icon-archway [tx_hash] [skip-update]
`, appName, appName, appName, appName),
),
RunE: func(cmd *cobra.Command, args []string) error {
src, ok := a.config.Chains[args[0]]
if !ok {
return errChainNotFound(args[0])
}
dst, ok := a.config.Chains[args[1]]
if !ok {
return errChainNotFound(args[1])
}
_, _, _, err := a.config.ChainsFromPath(args[2])
if err != nil {
return err
}

Check warning on line 963 in cmd/tx.go

View check run for this annotation

Codecov / codecov/patch

cmd/tx.go#L952-L963

Added lines #L952 - L963 were not covered by tests

// ensure that keys exist
if exists := src.ChainProvider.KeyExists(src.ChainProvider.Key()); !exists {
return fmt.Errorf("key %s not found on src chain %s", src.ChainProvider.Key(), src.ChainID())
}
if exists := dst.ChainProvider.KeyExists(dst.ChainProvider.Key()); !exists {
return fmt.Errorf("key %s not found on dst chain %s", dst.ChainProvider.Key(), dst.ChainID())
}

Check warning on line 971 in cmd/tx.go

View check run for this annotation

Codecov / codecov/patch

cmd/tx.go#L966-L971

Added lines #L966 - L971 were not covered by tests

// get transaction hash
txnHash := args[3]

// get trusted height
var trustedHeight int
var skipUpdate bool
skipUpdate = false
if len(args) == 5 {
var err error
if args[4] == "skip-update" {
skipUpdate = true
} else {
trustedHeight, err = strconv.Atoi(args[4])
if err != nil {
return fmt.Errorf("error: %w, arg: %s", err, args[4])
}

Check warning on line 988 in cmd/tx.go

View check run for this annotation

Codecov / codecov/patch

cmd/tx.go#L974-L988

Added lines #L974 - L988 were not covered by tests
}
}

return relayer.UpdateClientAndRecvMessage(cmd.Context(), src, dst, a.config.memo(cmd), txnHash, int64(trustedHeight), skipUpdate)

Check warning on line 992 in cmd/tx.go

View check run for this annotation

Codecov / codecov/patch

cmd/tx.go#L992

Added line #L992 was not covered by tests

},
}
return cmd
}

func ackPacket(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "ack-packet src_chain_name dst_chain_name path_name [txn-hash]",
Short: "update block [height] of src_chain on dst_chain, then acknowlede packet based on the txn-hash",
Args: withUsage(cobra.RangeArgs(4, 5)),
Example: strings.TrimSpace(
fmt.Sprintf(`
$ %s transact ack-packet icon archway icon-archway [tx_hash] [trusted_height OR {empty} OR {skip-update}]
$ %s transact ack-packet icon archway icon-archway [tx_hash] [trusted_height]
$ %s transact ack-packet icon archway icon-archway [tx_hash]
$ %s transact ack-packet icon archway icon-archway [tx_hash] [skip-update]
`, appName, appName, appName, appName),
),
RunE: func(cmd *cobra.Command, args []string) error {
src, ok := a.config.Chains[args[0]]
if !ok {
return errChainNotFound(args[0])
}
dst, ok := a.config.Chains[args[1]]
if !ok {
return errChainNotFound(args[1])
}
_, _, _, err := a.config.ChainsFromPath(args[2])
if err != nil {
return err
}

Check warning on line 1024 in cmd/tx.go

View check run for this annotation

Codecov / codecov/patch

cmd/tx.go#L1013-L1024

Added lines #L1013 - L1024 were not covered by tests

// ensure that keys exist
if exists := src.ChainProvider.KeyExists(src.ChainProvider.Key()); !exists {
return fmt.Errorf("key %s not found on src chain %s", src.ChainProvider.Key(), src.ChainID())
}
if exists := dst.ChainProvider.KeyExists(dst.ChainProvider.Key()); !exists {
return fmt.Errorf("key %s not found on dst chain %s", dst.ChainProvider.Key(), dst.ChainID())
}

Check warning on line 1032 in cmd/tx.go

View check run for this annotation

Codecov / codecov/patch

cmd/tx.go#L1027-L1032

Added lines #L1027 - L1032 were not covered by tests
// get transaction detail
txnHash := args[3]

// get trusted height
var trustedHeight int
var skipUpdate bool
skipUpdate = false
if len(args) == 5 {
var err error
if args[4] == "skip-update" {
skipUpdate = true
} else {
trustedHeight, err = strconv.Atoi(args[4])
if err != nil {
return fmt.Errorf("error: %w, arg: %s", err, args[4])
}

Check warning on line 1048 in cmd/tx.go

View check run for this annotation

Codecov / codecov/patch

cmd/tx.go#L1034-L1048

Added lines #L1034 - L1048 were not covered by tests
}
}

return relayer.UpdateClientAndAckMessage(cmd.Context(), src, dst, a.config.memo(cmd), txnHash, int64(trustedHeight), skipUpdate)

Check warning on line 1052 in cmd/tx.go

View check run for this annotation

Codecov / codecov/patch

cmd/tx.go#L1052

Added line #L1052 was not covered by tests
},
}
return cmd
}

func relayMsgsCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "relay-packets path_name src_channel_id",
Expand Down
40 changes: 38 additions & 2 deletions relayer/chains/icon/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,45 @@
return time.Unix(header.Timestamp, 0), nil
}

// required for cosmos only
// WARN: Handles events only for write ack and send packet
// WARN: Used to call recv packet and ack packet via cli
func (icp *IconProvider) QueryTx(ctx context.Context, hashHex string) (*provider.RelayerTxResponse, error) {
panic(fmt.Sprintf("%s%s", icp.ChainName(), NOT_IMPLEMENTED))
txRes, err := icp.client.GetTransactionResult(&types.TransactionHashParam{
Hash: types.HexBytes(hashHex),
})
if err != nil {
return nil, err
}

Check warning on line 91 in relayer/chains/icon/query.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/query.go#L86-L91

Added lines #L86 - L91 were not covered by tests

ht, err := txRes.BlockHeight.Value()
if err != nil {
return nil, err
}

Check warning on line 96 in relayer/chains/icon/query.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/query.go#L93-L96

Added lines #L93 - L96 were not covered by tests

status, _ := txRes.Status.Int()
if status != 1 {
return &provider.RelayerTxResponse{}, fmt.Errorf("transaction failed: %v", err)
}
var eventLogs []provider.RelayerEvent
events := txRes.EventLogs

for _, event := range events {
if event.Indexed[0] == EventTypeSendPacket || event.Indexed[0] == EventTypeWriteAcknowledgement {
if event.Addr == types.Address(icp.PCfg.IbcHandlerAddress) {
evt := icp.parseSendPacketAndWriteAckEvent(event)
eventLogs = append(eventLogs, evt)
}

Check warning on line 110 in relayer/chains/icon/query.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/query.go#L98-L110

Added lines #L98 - L110 were not covered by tests
}
}

response := provider.RelayerTxResponse{
Height: ht,
TxHash: hashHex,
Code: uint32(status),
Data: string(txRes.SCOREAddress),
Events: eventLogs,
}
return &response, nil

Check warning on line 121 in relayer/chains/icon/query.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/query.go#L114-L121

Added lines #L114 - L121 were not covered by tests
}

// required for cosmos only
Expand Down
44 changes: 44 additions & 0 deletions relayer/chains/icon/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"strings"
"sync"
Expand Down Expand Up @@ -606,6 +607,47 @@
return rlyResp, true, callbackErr
}

func (icp *IconProvider) parseSendPacketAndWriteAckEvent(event types.EventLogStr) provider.RelayerEvent {
eventName := event.Indexed[0]
switch eventName {
case EventTypeSendPacket, EventTypeWriteAcknowledgement:
protoPacket, err := hex.DecodeString(strings.TrimPrefix(event.Indexed[1], "0x"))
if err != nil {
icp.log.Error("Error decoding packet data ", zap.String("packet_hex", event.Data[0]))
break

Check warning on line 617 in relayer/chains/icon/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/tx.go#L610-L617

Added lines #L610 - L617 were not covered by tests
}

var packetInfo icon.Packet
err = proto.Unmarshal(protoPacket, &packetInfo)
if err != nil {
icp.log.Error("error marshaling packet", zap.String("packet_data", string(protoPacket)))
break

Check warning on line 624 in relayer/chains/icon/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/tx.go#L620-L624

Added lines #L620 - L624 were not covered by tests
}

relayerEvent := provider.RelayerEvent{
EventType: IconCosmosEventMap[eventName],
Attributes: map[string]string{
chantypes.AttributeKeySequence: fmt.Sprintf("%d", packetInfo.Sequence),
chantypes.AttributeKeyDstChannel: packetInfo.DestinationChannel,
chantypes.AttributeKeyDstPort: packetInfo.DestinationPort,
chantypes.AttributeKeySrcChannel: packetInfo.SourceChannel,
chantypes.AttributeKeySrcPort: packetInfo.SourcePort,
chantypes.AttributeKeyDataHex: fmt.Sprintf("%x", packetInfo.Data),
chantypes.AttributeKeyTimeoutHeight: fmt.Sprintf("%d-%d", packetInfo.TimeoutHeight.RevisionNumber, packetInfo.TimeoutHeight.RevisionHeight),
chantypes.AttributeKeyTimeoutTimestamp: fmt.Sprintf("%d", packetInfo.TimeoutTimestamp),
},
}
var ackData string
if eventName == EventTypeWriteAcknowledgement {
ackData = strings.TrimPrefix(event.Data[0], "0x")
relayerEvent.Attributes[chantypes.AttributeKeyAckHex] = ackData
}

Check warning on line 644 in relayer/chains/icon/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/tx.go#L627-L644

Added lines #L627 - L644 were not covered by tests

return relayerEvent

Check warning on line 646 in relayer/chains/icon/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/tx.go#L646

Added line #L646 was not covered by tests
}
return provider.RelayerEvent{}

Check warning on line 648 in relayer/chains/icon/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/tx.go#L648

Added line #L648 was not covered by tests
}

func (icp *IconProvider) parseConfirmedEventLogStr(event types.EventLogStr) provider.RelayerEvent {

eventName := event.Indexed[0]
Expand Down Expand Up @@ -793,6 +835,8 @@

step, err := icp.client.EstimateStep(txParamEst)
if err != nil {
estimate_txn_bytes, _ := json.Marshal(txParamEst)
icp.log.Warn("Transaction data during estimate step", zap.ByteString("txn_data", estimate_txn_bytes))

Check warning on line 839 in relayer/chains/icon/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/tx.go#L838-L839

Added lines #L838 - L839 were not covered by tests
return fmt.Errorf("failed estimating step: %w", err)
}
stepVal, err := step.Int()
Expand Down
Loading
Loading