Skip to content

Commit

Permalink
fix ProposeUpgrade & AllowTransitionToFlushComplete to log revert rea…
Browse files Browse the repository at this point in the history
…son and other information

Signed-off-by: Masanori Yoshida <masanori.yoshida@datachain.jp>
  • Loading branch information
siburu committed Aug 26, 2024
1 parent 91f370e commit 2c0371d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 22 deletions.
29 changes: 17 additions & 12 deletions pkg/relay/ethereum/log.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package ethereum

const (
logAttrMsgIndexFrom = "msg_index_from"
logAttrMsgCount = "msg_count"
logAttrMsgType = "msg_type"
logAttrRevertReason = "revert_reason"
logAttrRawErrorData = "raw_error_data"
logAttrRawTxData = "raw_tx_data"
logAttrTxHash = "tx_hash"
logAttrBlockHash = "block_hash"
logAttrBlockNumber = "block_number"
logAttrTxIndex = "tx_index"
logAttrEstimatedGas = "estimated_gas"
logAttrMaxGasLimit = "max_gas_limit"
logAttrMsgIndexFrom = "msg_index_from"
logAttrMsgCount = "msg_count"
logAttrMsgType = "msg_type"
logAttrRevertReason = "revert_reason"
logAttrRawErrorData = "raw_error_data"
logAttrRawTxData = "raw_tx_data"
logAttrTxHash = "tx_hash"
logAttrBlockHash = "block_hash"
logAttrBlockNumber = "block_number"
logAttrTxIndex = "tx_index"
logAttrEstimatedGas = "estimated_gas"
logAttrMaxGasLimit = "max_gas_limit"
logAttrPortID = "port_id"
logAttrChannelID = "channel_id"
logAttrUpgradeFields = "upgrade_fields"
logAttrTimeout = "timeout"
logAttrUpgradeSequence = "upgrade_sequence"
)
68 changes: 58 additions & 10 deletions pkg/relay/ethereum/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package ethereum

import (
"context"
"encoding/hex"
"fmt"

"github.com/datachainlab/ethereum-ibc-relay-chain/pkg/contract/iibcchannelupgradablemodule"
gethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/hyperledger-labs/yui-relayer/log"
)

func (c *Chain) ProposeUpgrade(
Expand All @@ -15,6 +17,14 @@ func (c *Chain) ProposeUpgrade(
upgradeFields iibcchannelupgradablemodule.UpgradeFieldsData,
timeout iibcchannelupgradablemodule.TimeoutData,
) error {
logger := c.GetChainLogger()
logger = &log.RelayLogger{Logger: logger.With(
logAttrPortID, portID,
logAttrChannelID, channelID,
logAttrUpgradeFields, upgradeFields,
logAttrTimeout, timeout,
)}

appAddr, err := c.ibcHandler.GetIBCModuleByChannel(c.CallOpts(ctx, 0), portID, channelID)
if err != nil {
return err
Expand All @@ -40,17 +50,8 @@ func (c *Chain) ProposeUpgrade(
upgradeFields,
timeout,
)
if err != nil {
return err
}

if receipt, err := c.client.WaitForReceiptAndGet(ctx, tx.Hash()); err != nil {
return err
} else if receipt.Status == gethtypes.ReceiptStatusFailed {
return fmt.Errorf("tx execution reverted")
}

return nil
return processSendTxResult(ctx, logger, c, tx, err)
}

func (c *Chain) AllowTransitionToFlushComplete(
Expand All @@ -59,6 +60,13 @@ func (c *Chain) AllowTransitionToFlushComplete(
channelID string,
upgradeSequence uint64,
) error {
logger := c.GetChainLogger()
logger = &log.RelayLogger{Logger: logger.With(
logAttrPortID, portID,
logAttrChannelID, channelID,
logAttrUpgradeSequence, upgradeSequence,
)}

appAddr, err := c.ibcHandler.GetIBCModuleByChannel(c.CallOpts(ctx, 0), portID, channelID)
if err != nil {
return err
Expand Down Expand Up @@ -87,9 +95,49 @@ func (c *Chain) AllowTransitionToFlushComplete(
return err
}

return processSendTxResult(ctx, logger, c, tx, err)
}

func processSendTxResult(ctx context.Context, logger *log.RelayLogger, c *Chain, tx *gethtypes.Transaction, err error) error {
if err != nil {
if revertReason, returnData, err := c.getRevertReasonFromRpcError(err); err != nil {
logger = &log.RelayLogger{Logger: logger.With(
logAttrRawErrorData, hex.EncodeToString(returnData),
)}
logger.Error("failed to get revert reason from RPC error", err)
} else {
logger = &log.RelayLogger{Logger: logger.With(
logAttrRevertReason, revertReason,
logAttrRawErrorData, hex.EncodeToString(returnData),
)}
}
logger.Error("failed to send tx", err)
return err
}

if rawTxData, err := tx.MarshalBinary(); err != nil {
logger.Error("failed to encode tx", err)
} else {
logger = &log.RelayLogger{Logger: logger.With(
logAttrRawTxData, hex.EncodeToString(rawTxData),
)}
}

if receipt, err := c.client.WaitForReceiptAndGet(ctx, tx.Hash()); err != nil {
logger.Error("failed to wait for tx receipt", err)
return err
} else if receipt.Status == gethtypes.ReceiptStatusFailed {
if revertReason, returnData, err := c.getRevertReasonFromReceipt(ctx, receipt); err != nil {
logger = &log.RelayLogger{Logger: logger.With(
logAttrRawErrorData, hex.EncodeToString(returnData),
)}
logger.Error("failed to get revert reason from receipt", err)
} else {
logger = &log.RelayLogger{Logger: logger.With(
logAttrRevertReason, revertReason,
logAttrRawErrorData, hex.EncodeToString(returnData),
)}
}
return fmt.Errorf("tx execution reverted")
}

Expand Down

0 comments on commit 2c0371d

Please sign in to comment.