Skip to content

Commit

Permalink
Problem: no register support for payee and counterpartyPayee
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Oct 25, 2024
1 parent 7dd3362 commit 5a42afb
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 313 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## UNRELEASED

### Features

* [#1665](https://github.com/crypto-org-chain/cronos/pull/1665) Support register for payee and counterpartyPayee in relayer precompile.

*Oct 24, 2024*

## v1.4.0-rc2
Expand Down
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ func New(
evmS,
[]evmkeeper.CustomContractFn{
func(_ sdk.Context, rules ethparams.Rules) vm.PrecompiledContract {
return cronosprecompiles.NewRelayerContract(app.IBCKeeper, appCodec, rules, app.Logger())
return cronosprecompiles.NewRelayerContract(app.IBCKeeper, app.IBCFeeKeeper, appCodec, rules, app.Logger())
},
func(ctx sdk.Context, rules ethparams.Rules) vm.PrecompiledContract {
return cronosprecompiles.NewIcaContract(ctx, app.ICAControllerKeeper, &app.CronosKeeper, appCodec, gasConfig)
Expand Down
23 changes: 21 additions & 2 deletions integration_tests/contracts/contracts/TestRelayer.sol
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import {IRelayerFunctions} from "./src/RelayerFunctions.sol";

contract TestRelayer {
address constant relayer = 0x0000000000000000000000000000000000000065;
address constant relayerContract = 0x0000000000000000000000000000000000000065;
IRelayerFunctions relayer = IRelayerFunctions(relayerContract);
address payee;
address counterpartyPayee;

function batchCall(bytes[] memory payloads) public {
for (uint256 i = 0; i < payloads.length; i++) {
(bool success,) = relayer.call(payloads[i]);
(bool success,) = relayerContract.call(payloads[i]);
require(success);
}
}

function callRegisterPayee(string calldata portID, string calldata channelID, address relayerAddr) public returns (bool) {
require(payee == address(0) || payee == msg.sender, "register fail");
bool result = relayer.registerPayee(portID, channelID, relayerAddr);
require(result, "call failed");
payee = msg.sender;
}

function callRegisterCounterpartyPayee(string calldata portID, string calldata channelID, address relayerAddr) public returns (bool) {
require(counterpartyPayee == address(0) || counterpartyPayee == msg.sender, "register fail");
bool result = relayer.registerCounterpartyPayee(portID, channelID, relayerAddr);
require(result, "call failed");
counterpartyPayee = msg.sender;
}
}
21 changes: 16 additions & 5 deletions integration_tests/ibc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ def prepare_network(
tx = {"to": sender, "value": fund, "gasPrice": w3.eth.gas_price}
send_transaction(w3, tx)
assert w3.eth.get_balance(sender, "latest") == fund
caller = deploy_contract(w3, CONTRACTS["TestRelayer"], key=acc.key).address
contract = deploy_contract(w3, CONTRACTS["TestRelayer"], key=acc.key)
caller = contract.address
assert caller == RELAYER_CALLER, caller
if is_hermes:
hermes = Hermes(path.with_suffix(".toml"))
Expand All @@ -203,7 +204,17 @@ def prepare_network(
call_rly_cmd(path, connection_only, version)

if incentivized:
register_fee_payee(cronos.cosmos_cli(), chainmain.cosmos_cli())
port_id = "transfer"
channel_id = "channel-0"
register_fee_payee(
cronos.cosmos_cli(), chainmain.cosmos_cli(), port_id, channel_id
)
data = {"from": acc.address}
tx = contract.functions.callRegisterPayee(
port_id, channel_id, ADDRS["signer1"]
).build_transaction(data)
receipt = send_transaction(w3, tx, acc.key)
assert receipt.status == 1, receipt

port = None
if is_relay:
Expand All @@ -217,10 +228,10 @@ def prepare_network(
wait_for_port(port)


def register_fee_payee(src_chain, dst_chain):
def register_fee_payee(src_chain, dst_chain, port_id, channel_id):
rsp = dst_chain.register_counterparty_payee(
"transfer",
"channel-0",
port_id,
channel_id,
dst_chain.address("relayer"),
src_chain.address("signer1"),
from_="relayer",
Expand Down

Large diffs are not rendered by default.

15 changes: 2 additions & 13 deletions x/cronos/events/bindings/src/RelayerFunctions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,6 @@ interface IRelayerFunctions {
function acknowledgement(bytes calldata data) external payable returns (bytes calldata);
function timeout(bytes calldata data) external payable returns (bytes calldata);
function timeoutOnClose(bytes calldata data) external payable returns (bytes calldata);
function updateClientAndConnectionOpenInit(bytes calldata data1, bytes calldata data2) external payable returns (bool);
function updateClientAndConnectionOpenTry(bytes calldata data1, bytes calldata data2) external payable returns (bool);
function updateClientAndConnectionOpenAck(bytes calldata data1, bytes calldata data2) external payable returns (bool);
function updateClientAndConnectionOpenConfirm(bytes calldata data1, bytes calldata data2) external payable returns (bool);
function updateClientAndChannelOpenInit(bytes calldata data1, bytes calldata data2) external payable returns (bool);
function updateClientAndChannelOpenTry(bytes calldata data1, bytes calldata data2) external payable returns (bool);
function updateClientAndChannelOpenAck(bytes calldata data1, bytes calldata data2) external payable returns (bool);
function updateClientAndChannelOpenConfirm(bytes calldata data1, bytes calldata data2) external payable returns (bool);
function updateClientAndRecvPacket(bytes calldata data1, bytes calldata data2) external payable returns (bool);
function updateClientAndAcknowledgement(bytes calldata data1, bytes calldata data2) external payable returns (bool);
function updateClientAndTimeout(bytes calldata data1, bytes calldata data2) external payable returns (bool);
function updateClientAndChannelCloseInit(bytes calldata data1, bytes calldata data2) external payable returns (bool);
function updateClientAndChannelCloseConfirm(bytes calldata data1, bytes calldata data2) external payable returns (bool);
function registerPayee(string calldata portID, string calldata channelID, address relayerAddr) external payable returns (bool);
function registerCounterpartyPayee(string calldata portID, string calldata channelID, address relayerAddr) external payable returns (bool);
}
59 changes: 41 additions & 18 deletions x/cronos/keeper/precompiles/relayer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package precompiles

import (
"context"
"errors"
"fmt"

Expand All @@ -15,6 +14,7 @@ import (
"github.com/ethereum/go-ethereum/params"

authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
ibcfeetypes "github.com/cosmos/ibc-go/v8/modules/apps/29-fee/types"
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
cronosevents "github.com/crypto-org-chain/cronos/v2/x/cronos/events"
Expand Down Expand Up @@ -48,7 +48,9 @@ const (
Acknowledgement = "acknowledgement"
Timeout = "timeout"
TimeoutOnClose = "timeoutOnClose"

// ibc fee
RegisterPayee = "registerPayee"
RsegisterCounterpartyPayee = "registerCounterpartyPayee"
GasWhenReceiverChainIsSource = 51705
GasWhenReceiverChainIsNotSource = 144025
)
Expand Down Expand Up @@ -101,18 +103,20 @@ func init() {
type RelayerContract struct {
BaseContract

cdc codec.Codec
ibcKeeper types.IbcKeeper
logger log.Logger
isHomestead bool
isIstanbul bool
isShanghai bool
cdc codec.Codec
ibcKeeper types.IbcKeeper
ibcFeeKeeper types.IbcFeeKeeper
logger log.Logger
isHomestead bool
isIstanbul bool
isShanghai bool
}

func NewRelayerContract(ibcKeeper types.IbcKeeper, cdc codec.Codec, rules params.Rules, logger log.Logger) vm.PrecompiledContract {
func NewRelayerContract(ibcKeeper types.IbcKeeper, ibcFeeKeeper types.IbcFeeKeeper, cdc codec.Codec, rules params.Rules, logger log.Logger) vm.PrecompiledContract {
return &RelayerContract{
BaseContract: NewBaseContract(relayerContractAddress),
ibcKeeper: ibcKeeper,
ibcFeeKeeper: ibcFeeKeeper,
cdc: cdc,
isHomestead: rules.IsHomestead,
isIstanbul: rules.IsIstanbul,
Expand Down Expand Up @@ -191,8 +195,33 @@ func (bc *RelayerContract) Run(evm *vm.EVM, contract *vm.Contract, readonly bool
if err != nil {
return nil, errors.New("fail to unpack input arguments")
}
input := args[0].([]byte)
converter := cronosevents.RelayerConvertEvent
if method.Name == RegisterPayee || method.Name == RsegisterCounterpartyPayee {
execErr := stateDB.ExecuteNativeAction(precompileAddr, converter, func(ctx sdk.Context) error {
portID := args[0].(string)
channelID := args[1].(string)
relayerAddr := sdk.AccAddress(args[2].(common.Address).Bytes()).String()
caller := sdk.AccAddress(contract.CallerAddress.Bytes()).String()
if method.Name == RegisterPayee {
_, err := bc.ibcFeeKeeper.RegisterPayee(
ctx,
ibcfeetypes.NewMsgRegisterPayee(portID, channelID, caller, relayerAddr),
)
return err
} else {
_, err = bc.ibcFeeKeeper.RegisterCounterpartyPayee(
ctx,
ibcfeetypes.NewMsgRegisterCounterpartyPayee(portID, channelID, caller, relayerAddr),
)
return err
}

Check warning on line 217 in x/cronos/keeper/precompiles/relayer.go

View check run for this annotation

Codecov / codecov/patch

x/cronos/keeper/precompiles/relayer.go#L199-L217

Added lines #L199 - L217 were not covered by tests
})
if execErr != nil {
return nil, execErr
}
return method.Outputs.Pack(true)

Check warning on line 222 in x/cronos/keeper/precompiles/relayer.go

View check run for this annotation

Codecov / codecov/patch

x/cronos/keeper/precompiles/relayer.go#L219-L222

Added lines #L219 - L222 were not covered by tests
}
input := args[0].([]byte)

Check warning on line 224 in x/cronos/keeper/precompiles/relayer.go

View check run for this annotation

Codecov / codecov/patch

x/cronos/keeper/precompiles/relayer.go#L224

Added line #L224 was not covered by tests
e := &Executor{
cdc: bc.cdc,
stateDB: stateDB,
Expand Down Expand Up @@ -231,15 +260,9 @@ func (bc *RelayerContract) Run(evm *vm.EVM, contract *vm.Contract, readonly bool
case RecvPacket:
res, err = exec(e, bc.ibcKeeper.RecvPacket)
case Acknowledgement:
res, err = exec(e, func(goCtx context.Context, msg *channeltypes.MsgAcknowledgement) (*channeltypes.MsgAcknowledgementResponse, error) {
msg.Signer = sdk.AccAddress(evm.TxContext.Origin.Bytes()).String()
return bc.ibcKeeper.Acknowledgement(goCtx, msg)
})
res, err = exec(e, bc.ibcKeeper.Acknowledgement)

Check warning on line 263 in x/cronos/keeper/precompiles/relayer.go

View check run for this annotation

Codecov / codecov/patch

x/cronos/keeper/precompiles/relayer.go#L263

Added line #L263 was not covered by tests
case Timeout:
res, err = exec(e, func(goCtx context.Context, msg *channeltypes.MsgTimeout) (*channeltypes.MsgTimeoutResponse, error) {
msg.Signer = sdk.AccAddress(evm.TxContext.Origin.Bytes()).String()
return bc.ibcKeeper.Timeout(goCtx, msg)
})
res, err = exec(e, bc.ibcKeeper.Timeout)

Check warning on line 265 in x/cronos/keeper/precompiles/relayer.go

View check run for this annotation

Codecov / codecov/patch

x/cronos/keeper/precompiles/relayer.go#L265

Added line #L265 was not covered by tests
case TimeoutOnClose:
res, err = exec(e, bc.ibcKeeper.TimeoutOnClose)
default:
Expand Down
6 changes: 6 additions & 0 deletions x/cronos/types/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/ethereum/go-ethereum/params"
evmtypes "github.com/evmos/ethermint/x/evm/types"

ibcfeetypes "github.com/cosmos/ibc-go/v8/modules/apps/29-fee/types"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
connectiontypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
Expand Down Expand Up @@ -94,3 +95,8 @@ type IbcKeeper interface {
Timeout(goCtx context.Context, msg *channeltypes.MsgTimeout) (*channeltypes.MsgTimeoutResponse, error)
TimeoutOnClose(goCtx context.Context, msg *channeltypes.MsgTimeoutOnClose) (*channeltypes.MsgTimeoutOnCloseResponse, error)
}

type IbcFeeKeeper interface {
RegisterPayee(goCtx context.Context, msg *ibcfeetypes.MsgRegisterPayee) (*ibcfeetypes.MsgRegisterPayeeResponse, error)
RegisterCounterpartyPayee(goCtx context.Context, msg *ibcfeetypes.MsgRegisterCounterpartyPayee) (*ibcfeetypes.MsgRegisterCounterpartyPayeeResponse, error)
}

0 comments on commit 5a42afb

Please sign in to comment.