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

Problem: ibc callback is not used #1185

Merged
merged 26 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
10 changes: 8 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ import (
upgradeclient "github.com/cosmos/cosmos-sdk/x/upgrade/client"
upgradekeeper "github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
ibccallbacks "github.com/cosmos/ibc-go/modules/apps/callbacks"
ibcfee "github.com/cosmos/ibc-go/v7/modules/apps/29-fee"
ibcfeekeeper "github.com/cosmos/ibc-go/v7/modules/apps/29-fee/keeper"
ibcfeetypes "github.com/cosmos/ibc-go/v7/modules/apps/29-fee/types"
Expand Down Expand Up @@ -655,8 +656,13 @@ func New(
icaAuthModule := icaauth.NewAppModule(appCodec, app.ICAAuthKeeper)
icaAuthIBCModule := icaauth.NewIBCModule(app.ICAAuthKeeper)

icaControllerIBCModule := icacontroller.NewIBCMiddleware(icaAuthIBCModule, app.ICAControllerKeeper)
icaControllerStack := ibcfee.NewIBCMiddleware(icaControllerIBCModule, app.IBCFeeKeeper)
var icaControllerStack porttypes.IBCModule
icaControllerStack = icacontroller.NewIBCMiddleware(icaAuthIBCModule, app.ICAControllerKeeper)
icaControllerStack = ibcfee.NewIBCMiddleware(icaControllerStack, app.IBCFeeKeeper)
// Since the callbacks middleware itself is an ics4wrapper, it needs to be passed to the ica controller keeper
app.ICAControllerKeeper.WithICS4Wrapper(icaControllerStack.(porttypes.Middleware))
const maxCallbackGas = uint64(1000000)
mmsqe marked this conversation as resolved.
Show resolved Hide resolved
icaControllerStack = ibccallbacks.NewIBCMiddleware(icaControllerStack, app.IBCFeeKeeper, app.CronosKeeper, maxCallbackGas)

// Create static IBC router, add transfer route, then set and seal it
ibcRouter := porttypes.NewRouter()
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/cosmos/cosmos-proto v1.0.0-beta.2
github.com/cosmos/cosmos-sdk v0.47.5
github.com/cosmos/gogoproto v1.4.10
github.com/cosmos/ibc-go/modules/apps/callbacks v0.1.1-0.20230831194909-17cf1260a9cd
github.com/cosmos/ibc-go/v7 v7.3.1-0.20230920070810-c3261472c815
github.com/crypto-org-chain/cronos/store v0.0.4
github.com/crypto-org-chain/cronos/versiondb v0.0.0-00010101000000-000000000000
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,8 @@ github.com/cosmos/gogoproto v1.4.10 h1:QH/yT8X+c0F4ZDacDv3z+xE3WU1P1Z3wQoLMBRJoK
github.com/cosmos/gogoproto v1.4.10/go.mod h1:3aAZzeRWpAwr+SS/LLkICX2/kDFyaYVzckBDzygIxek=
github.com/cosmos/iavl v0.21.0-alpha.1.0.20230904092046-df3db2d96583 h1:3Matt7/LjZiZkIBPalYazOZcw2B05Ch14dU5TJyqJEc=
github.com/cosmos/iavl v0.21.0-alpha.1.0.20230904092046-df3db2d96583/go.mod h1:WO7FyvaZJoH65+HFOsDir7xU9FWk2w9cHXNW1XHcl7A=
github.com/cosmos/ibc-go/modules/apps/callbacks v0.1.1-0.20230831194909-17cf1260a9cd h1:KVnAwC1d0b+LWrVi+U1rex0e9LlyGTZ17zYhU3S4il8=
github.com/cosmos/ibc-go/modules/apps/callbacks v0.1.1-0.20230831194909-17cf1260a9cd/go.mod h1:h+JtOsdOs/ikuntjZFXOAa8qnXUfgkTcRSHaTTcAM+M=
github.com/cosmos/ibc-go/v7 v7.3.1-0.20230920070810-c3261472c815 h1:raSo7w7B3IXCb7DZozHWz8ajG7HLWZw9foiyCbEgInI=
github.com/cosmos/ibc-go/v7 v7.3.1-0.20230920070810-c3261472c815/go.mod h1:wvx4pPBofe5ZdMNV3OFRxSI4auEP5Qfqf8JXLLNV04g=
github.com/cosmos/ics23/go v0.10.0 h1:iXqLLgp2Lp+EdpIuwXTYIQU+AiHj9mOC2X9ab++bZDM=
Expand Down
3 changes: 3 additions & 0 deletions gomod2nix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ schema = 3
[mod."github.com/cosmos/iavl"]
version = "v0.21.0-alpha.1.0.20230904092046-df3db2d96583"
hash = "sha256-3Va8Ljq63IXty0oHlRpqfsC6WsMut6TWZ2R+/nYtfTU="
[mod."github.com/cosmos/ibc-go/modules/apps/callbacks"]
version = "v0.1.1-0.20230831194909-17cf1260a9cd"
hash = "sha256-kVvGNQt3A1H6pQs3YtMvx1t9nNcL6ClKnIfsR24OTi8="
[mod."github.com/cosmos/ibc-go/v7"]
version = "v7.3.1-0.20230920070810-c3261472c815"
hash = "sha256-x/D64hmU+aOc5sm8RzXMB+8y+530+CCEik/Zpj3Rf9A="
Expand Down
329 changes: 328 additions & 1 deletion x/cronos/events/bindings/cosmos/precompile/ica/i_ica_module.abigen.go

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions x/cronos/events/bindings/src/ICA.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,32 @@ pragma solidity ^0.8.4;

interface IICAModule {
event SubmitMsgsResult(uint64 seq);
event DestinationCallback(
string module,
string callbackType,
string callbackAddress,
string callbackResult,
string callbackError,
string callbackExecGasLimit,
string callbackCommitGasLimit,
string packetDestPort,
string packetDestChannel,
string packetSequence
);
event SourceCallback(
string module,
string callbackType,
string callbackAddress,
string callbackResult,
string callbackExecGasLimit,
string callbackCommitGasLimit,
string packetSrcPort,
string packetSrcChannel,
string packetSequence
);
function registerAccount(string calldata connectionID, string calldata version) external payable returns (bool);
function queryAccount(string calldata connectionID, address addr) external view returns (string memory);
function submitMsgs(string calldata connectionID, bytes calldata data, uint256 timeout) external payable returns (uint64);
function onAcknowledgementPacketCallback(uint64 seq, string calldata packetSenderAddress) external payable returns (bool);
function onTimeoutPacketCallback(uint64 seq, string calldata packetSenderAddress) external payable returns (bool);
mmsqe marked this conversation as resolved.
Show resolved Hide resolved
}
18 changes: 15 additions & 3 deletions x/cronos/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package events
import (
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
ibccallbackstypes "github.com/cosmos/ibc-go/modules/apps/callbacks/types"
ibcfeetypes "github.com/cosmos/ibc-go/v7/modules/apps/29-fee/types"
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
Expand Down Expand Up @@ -35,9 +36,20 @@ var (
transfertypes.AttributeKeyDenom: ReturnStringAsIs,
}
IcaValueDecoders = ValueDecoders{
channeltypes.AttributeKeyChannelID: ReturnStringAsIs,
channeltypes.AttributeKeyPortID: ReturnStringAsIs,
cronoseventstypes.AttributeKeySeq: ConvertUint64,
cronoseventstypes.AttributeKeySeq: ConvertUint64,
ibccallbackstypes.AttributeKeyCallbackType: ReturnStringAsIs,
ibccallbackstypes.AttributeKeyCallbackAddress: ReturnStringAsIs,
ibccallbackstypes.AttributeKeyCallbackResult: ReturnStringAsIs,
ibccallbackstypes.AttributeKeyCallbackError: ReturnStringAsIs,
ibccallbackstypes.AttributeKeyCallbackGasLimit: ReturnStringAsIs,
ibccallbackstypes.AttributeKeyCallbackCommitGasLimit: ReturnStringAsIs,
ibccallbackstypes.AttributeKeyCallbackSourcePortID: ReturnStringAsIs,
ibccallbackstypes.AttributeKeyCallbackSourceChannelID: ReturnStringAsIs,
ibccallbackstypes.AttributeKeyCallbackDestPortID: ReturnStringAsIs,
ibccallbackstypes.AttributeKeyCallbackDestChannelID: ReturnStringAsIs,
ibccallbackstypes.AttributeKeyCallbackSequence: ReturnStringAsIs,
ibccallbackstypes.AttributeValueCallbackSuccess: ReturnStringAsIs,
ibccallbackstypes.AttributeValueCallbackFailure: ReturnStringAsIs,
}
)

Expand Down
10 changes: 7 additions & 3 deletions x/cronos/keeper/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ const DefaultGasCap uint64 = 25000000

// CallEVM execute an evm message from native module
func (k Keeper) CallEVM(ctx sdk.Context, to *common.Address, data []byte, value *big.Int) (*ethtypes.Message, *evmtypes.MsgEthereumTxResponse, error) {
nonce := k.evmKeeper.GetNonce(ctx, types.EVMModuleAddress)
return k.CallEVMWithArgs(ctx, to, types.EVMModuleAddress, data, value)
}

// CallEVMWithArgs execute an evm message with args
func (k Keeper) CallEVMWithArgs(ctx sdk.Context, to *common.Address, from common.Address, data []byte, value *big.Int) (*ethtypes.Message, *evmtypes.MsgEthereumTxResponse, error) {
nonce := k.evmKeeper.GetNonce(ctx, from)
msg := ethtypes.NewMessage(
types.EVMModuleAddress,
common.Address(from.Bytes()),
to,
nonce,
value, // amount
Expand All @@ -31,7 +36,6 @@ func (k Keeper) CallEVM(ctx sdk.Context, to *common.Address, data []byte, value
nil, // accessList
false, // isFake
)

ret, err := k.evmKeeper.ApplyMessage(ctx, msg, nil, true)
if err != nil {
return nil, nil, err
Expand Down
78 changes: 78 additions & 0 deletions x/cronos/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"fmt"
"math/big"
"strings"

"cosmossdk.io/errors"
Expand All @@ -15,6 +16,10 @@
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"
cronosprecompiles "github.com/crypto-org-chain/cronos/v2/x/cronos/keeper/precompiles"
"github.com/crypto-org-chain/cronos/v2/x/cronos/types"
"github.com/ethereum/go-ethereum/common"
// this line is used by starport scaffolding # ibc/keeper/import
Expand Down Expand Up @@ -276,3 +281,76 @@

return nil
}

// IBCOnAcknowledgementPacketCallback returns nil if the gas meter has greater than
// or equal to 500_000 gas remaining.
// This function oog panics if the gas remaining is less than 500_000.
mmsqe marked this conversation as resolved.
Show resolved Hide resolved
// This function errors if the authAddress is MockCallbackUnauthorizedAddress.
func (k Keeper) IBCOnAcknowledgementPacketCallback(
ctx sdk.Context,
packet channeltypes.Packet,
acknowledgement []byte,
relayer sdk.AccAddress,
contractAddress,
packetSenderAddress string,
) error {
relayerAddr := common.BytesToAddress(relayer.Bytes())
precompileAddr := common.HexToAddress(contractAddress)
data, err := cronosprecompiles.GetOnAcknowledgementPacketCallback(packet.Sequence, packetSenderAddress)
if err != nil {
return err
}
_, _, err = k.CallEVMWithArgs(ctx, &precompileAddr, relayerAddr, data, big.NewInt(0))
return err

Check warning on line 304 in x/cronos/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/cronos/keeper/keeper.go#L296-L304

Added lines #L296 - L304 were not covered by tests
}

// IBCOnTimeoutPacketCallback returns nil if the gas meter has greater than
// or equal to 500_000 gas remaining.
// This function oog panics if the gas remaining is less than 500_000.
// This function errors if the authAddress is MockCallbackUnauthorizedAddress.
func (k Keeper) IBCOnTimeoutPacketCallback(
ctx sdk.Context,
packet channeltypes.Packet,
relayer sdk.AccAddress,
contractAddress,
packetSenderAddress string,
) error {
relayerAddr := common.BytesToAddress(relayer.Bytes())
precompileAddr := common.HexToAddress(contractAddress)
data, err := cronosprecompiles.GetOnTimeoutPacketCallbackk(packet.Sequence, packetSenderAddress)
mmsqe marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
_, _, err = k.CallEVMWithArgs(ctx, &precompileAddr, relayerAddr, data, big.NewInt(0))
return err

Check warning on line 325 in x/cronos/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/cronos/keeper/keeper.go#L317-L325

Added lines #L317 - L325 were not covered by tests
}

// IBCReceivePacketCallback returns nil if the gas meter has greater than
// or equal to 500_000 gas remaining.
// This function oog panics if the gas remaining is less than 500_000.
// This function errors if the authAddress is MockCallbackUnauthorizedAddress.
func (k Keeper) IBCReceivePacketCallback(
ctx sdk.Context,
packet ibcexported.PacketI,
ack ibcexported.Acknowledgement,
contractAddress string,
) error {
return nil

Check warning on line 338 in x/cronos/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/cronos/keeper/keeper.go#L337-L338

Added lines #L337 - L338 were not covered by tests
}

// IBCPacketSendCallback returns nil if the gas meter has greater than
// or equal to 500_000 gas remaining.
// This function oog panics if the gas remaining is less than 500_000.
// This function errors if the authAddress is MockCallbackUnauthorizedAddress.
func (k Keeper) IBCSendPacketCallback(
ctx sdk.Context,
sourcePort string,
sourceChannel string,
timeoutHeight clienttypes.Height,
timeoutTimestamp uint64,
packetData []byte,
contractAddress,
packetSenderAddress string,
) error {
return nil

Check warning on line 355 in x/cronos/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/cronos/keeper/keeper.go#L354-L355

Added lines #L354 - L355 were not covered by tests
}
22 changes: 21 additions & 1 deletion x/cronos/keeper/precompiles/ica.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ func init() {
}
}

func GetOnAcknowledgementPacketCallback(args ...interface{}) ([]byte, error) {
return icaABI.Pack("onAcknowledgementPacketCallback", args...)
}

func GetOnTimeoutPacketCallbackk(args ...interface{}) ([]byte, error) {
mmsqe marked this conversation as resolved.
Show resolved Hide resolved
return icaABI.Pack("onTimeoutPacketCallback", args...)
}

type IcaContract struct {
BaseContract

Expand Down Expand Up @@ -68,7 +76,6 @@ func (ic *IcaContract) Run(evm *vm.EVM, contract *vm.Contract, readonly bool) ([
if err != nil {
return nil, err
}

stateDB := evm.StateDB.(ExtStateDB)
precompileAddr := ic.Address()
caller := contract.CallerAddress
Expand Down Expand Up @@ -134,6 +141,7 @@ func (ic *IcaContract) Run(evm *vm.EVM, contract *vm.Contract, readonly bool) ([
icaMsgData := icatypes.InterchainAccountPacketData{
Type: icatypes.EXECUTE_TX,
Data: data,
Memo: fmt.Sprintf(`{"src_callback": {"address": "%s"}}`, icaContractAddress.String()),
}
timeoutDuration := time.Duration(timeout.Uint64())
seq := uint64(0)
Expand All @@ -160,6 +168,18 @@ func (ic *IcaContract) Run(evm *vm.EVM, contract *vm.Contract, readonly bool) ([
return nil, execErr
}
return method.Outputs.Pack(seq)
case "onAcknowledgementPacketCallback", "onTimeoutPacketCallback":
if readonly {
return nil, errors.New("the method is not readonly")
}
args, err := method.Inputs.Unpack(contract.Input[4:])
if err != nil {
return nil, errors.New("fail to unpack input arguments")
}
seq := args[0].(uint64)
packetSenderAddress := args[1].(string)
fmt.Printf("mm-%s, %d, %s\n", method.Name, seq, packetSenderAddress)
return method.Outputs.Pack(true)
default:
return nil, errors.New("unknown method")
}
Expand Down
Loading