diff --git a/CHANGELOG.md b/CHANGELOG.md index 464ee15de6..485a484697 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### State Machine Breaking * [#1407](https://github.com/crypto-org-chain/cronos/pull/1407) Add end-to-end encryption module. +* [#1458](https://github.com/crypto-org-chain/cronos/pull/1458) Adjust require gas for recvPacket when ReceiverChainIsSource. ### Improvements diff --git a/x/cronos/keeper/precompiles/relayer.go b/x/cronos/keeper/precompiles/relayer.go index 88d9d3b17d..8556b2ab82 100644 --- a/x/cronos/keeper/precompiles/relayer.go +++ b/x/cronos/keeper/precompiles/relayer.go @@ -13,6 +13,8 @@ import ( "github.com/ethereum/go-ethereum/params" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types" + channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" cronosevents "github.com/crypto-org-chain/cronos/v2/x/cronos/events" "github.com/crypto-org-chain/cronos/v2/x/cronos/events/bindings/cosmos/precompile/relayer" "github.com/crypto-org-chain/cronos/v2/x/cronos/types" @@ -58,6 +60,10 @@ const ( UpdateClientAndAcknowledgement = "updateClientAndAcknowledgement" UpdateClientAndTimeout = "updateClientAndTimeout" UpdateClientAndTimeoutOnClose = "updateClientAndTimeoutOnClose" + + GasForUpdateClient = 111894 + GasWhenReceiverChainIsSource = 51705 + GasWhenReceiverChainIsNotSource = 144025 ) func init() { @@ -93,7 +99,7 @@ func init() { case ChannelCloseConfirm: relayerGasRequiredByMethod[methodID] = 31199 case RecvPacket: - relayerGasRequiredByMethod[methodID] = 144025 + relayerGasRequiredByMethod[methodID] = GasWhenReceiverChainIsNotSource case Acknowledgement: relayerGasRequiredByMethod[methodID] = 61781 case Timeout: @@ -107,7 +113,7 @@ func init() { case UpdateClientAndChannelOpenConfirm: relayerGasRequiredByMethod[methodID] = 132734 case UpdateClientAndRecvPacket: - relayerGasRequiredByMethod[methodID] = 257120 + relayerGasRequiredByMethod[methodID] = GasForUpdateClient + GasWhenReceiverChainIsNotSource case UpdateClientAndConnectionOpenInit: relayerGasRequiredByMethod[methodID] = 131649 case UpdateClientAndConnectionOpenAck: @@ -165,6 +171,34 @@ func (bc *RelayerContract) RequiredGas(input []byte) (gas uint64) { var methodID [4]byte copy(methodID[:], input[:4]) requiredGas, ok := relayerGasRequiredByMethod[methodID] + method, err := irelayerABI.MethodById(methodID[:]) + if err != nil { + panic(err) + } + if method.Name == RecvPacket || method.Name == UpdateClientAndRecvPacket { + args, err := method.Inputs.Unpack(input[4:]) + if err != nil { + panic(err) + } + i := args[0].([]byte) + if method.Name == UpdateClientAndRecvPacket { + i = args[1].([]byte) + } + var msg channeltypes.MsgRecvPacket + if err = bc.cdc.Unmarshal(i, &msg); err != nil { + panic(err) + } + var data ibctransfertypes.FungibleTokenPacketData + if err = ibctransfertypes.ModuleCdc.UnmarshalJSON(msg.Packet.GetData(), &data); err != nil { + panic(err) + } + if ibctransfertypes.ReceiverChainIsSource(msg.Packet.GetSourcePort(), msg.Packet.GetSourceChannel(), data.Denom) { + requiredGas = GasWhenReceiverChainIsSource + if method.Name == UpdateClientAndRecvPacket { + requiredGas += GasForUpdateClient + } + } + } intrinsicGas, _ := core.IntrinsicGas(input, nil, false, bc.isHomestead, bc.isIstanbul, bc.isShanghai) defer func() { methodName := relayerMethodNamedByMethod[methodID]