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

Raunak/receiver fixes #77

Merged
merged 6 commits into from
Apr 4, 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
171 changes: 74 additions & 97 deletions contracts/core/Dispatcher.sol

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion contracts/core/UniversalChannelHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
IbcMwEventsEmitter
} from "../interfaces/IbcMiddleware.sol";
import {IbcReceiver, IbcReceiverBase} from "../interfaces/IbcReceiver.sol";
import {ChannelOrder, CounterParty, IbcPacket, AckPacket, UniversalPacket, IbcUtils} from "../libs/Ibc.sol";
import {ChannelOrder, ChannelEnd, IbcPacket, AckPacket, UniversalPacket, IbcUtils} from "../libs/Ibc.sol";

contract UniversalChannelHandler is IbcReceiverBase, IbcUniversalChannelMW {
bytes32[] public connectedChannels;
Expand Down Expand Up @@ -48,6 +48,16 @@ contract UniversalChannelHandler is IbcReceiverBase, IbcUniversalChannelMW {
if (!channelFound) revert ChannelNotFound();
}

function openChannel(
string calldata version,
ChannelOrder ordering,
bool feeEnabled,
string[] calldata connectionHops,
string calldata counterpartyPortId
) external onlyOwner {
dispatcher.channelOpenInit(version, ordering, feeEnabled, connectionHops, counterpartyPortId);
}

function sendUniversalPacket(
bytes32 channelId,
bytes32 destPortAddr,
Expand Down
12 changes: 11 additions & 1 deletion contracts/examples/Mars.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

pragma solidity ^0.8.9;

import {IBCErrors, AckPacket, ChannelOrder, CounterParty} from "../libs/Ibc.sol";
import {IBCErrors, AckPacket, ChannelOrder, ChannelEnd} from "../libs/Ibc.sol";
import {IbcReceiverBase, IbcReceiver, IbcPacket} from "../interfaces/IbcReceiver.sol";
import {IbcDispatcher} from "../interfaces/IbcDispatcher.sol";

Expand All @@ -19,6 +19,16 @@ contract Mars is IbcReceiverBase, IbcReceiver {

constructor(IbcDispatcher _dispatcher) IbcReceiverBase(_dispatcher) {}

function triggerChannelInit(
string calldata version,
ChannelOrder ordering,
bool feeEnabled,
string[] calldata connectionHops,
string calldata counterpartyPortId
) external onlyOwner {
dispatcher.channelOpenInit(version, ordering, feeEnabled, connectionHops, counterpartyPortId);
}

function onRecvPacket(IbcPacket memory packet)
external
virtual
Expand Down
31 changes: 10 additions & 21 deletions contracts/interfaces/IDispatcher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {L1Header, OpL2StateProof, Ics23Proof} from "./ProofVerifier.sol";
import {IbcChannelReceiver, IbcPacketReceiver} from "./IbcReceiver.sol";
import {
Channel,
CounterParty,
ChannelEnd,
ChannelOrder,
IbcPacket,
ChannelState,
Expand All @@ -33,7 +33,6 @@ interface IDispatcher is IbcDispatcher, IbcEventsEmitter {
* will be relayed to the IBC/VIBC hub chain.
*/
function channelOpenInit(
IbcChannelReceiver portAddress,
string calldata version,
ChannelOrder ordering,
bool feeEnabled,
Expand All @@ -47,12 +46,11 @@ interface IDispatcher is IbcDispatcher, IbcEventsEmitter {
* will be relayed to the IBC/VIBC hub chain.
*/
function channelOpenTry(
IbcChannelReceiver portAddress,
CounterParty calldata local,
ChannelEnd calldata local,
ChannelOrder ordering,
bool feeEnabled,
string[] calldata connectionHops,
CounterParty calldata counterparty,
ChannelEnd calldata counterparty,
Ics23Proof calldata proof
) external;

Expand All @@ -61,12 +59,11 @@ interface IDispatcher is IbcDispatcher, IbcEventsEmitter {
* The dApp should implement the onChannelConnect method to handle the third channel handshake method: ChanOpenAck
*/
function channelOpenAck(
IbcChannelReceiver portAddress,
CounterParty calldata local,
ChannelEnd calldata local,
string[] calldata connectionHops,
ChannelOrder ordering,
bool feeEnabled,
CounterParty calldata counterparty,
ChannelEnd calldata counterparty,
Ics23Proof calldata proof
) external;

Expand All @@ -76,36 +73,28 @@ interface IDispatcher is IbcDispatcher, IbcEventsEmitter {
* ChannelOpenConfirm
*/
function channelOpenConfirm(
IbcChannelReceiver portAddress,
CounterParty calldata local,
ChannelEnd calldata local,
string[] calldata connectionHops,
ChannelOrder ordering,
bool feeEnabled,
CounterParty calldata counterparty,
ChannelEnd calldata counterparty,
Ics23Proof calldata proof
) external;

function closeIbcChannel(bytes32 channelId) external;

function sendPacket(bytes32 channelId, bytes calldata packet, uint64 timeoutTimestamp) external;

function acknowledgement(
IbcPacketReceiver receiver,
IbcPacket calldata packet,
bytes calldata ack,
Ics23Proof calldata proof
) external;
function acknowledgement(IbcPacket calldata packet, bytes calldata ack, Ics23Proof calldata proof) external;

function timeout(IbcPacketReceiver receiver, IbcPacket calldata packet, Ics23Proof calldata proof) external;
function timeout(IbcPacket calldata packet, Ics23Proof calldata proof) external;

function recvPacket(IbcPacketReceiver receiver, IbcPacket calldata packet, Ics23Proof calldata proof) external;
function recvPacket(IbcPacket calldata packet, Ics23Proof calldata proof) external;
RnkSngh marked this conversation as resolved.
Show resolved Hide resolved

function getOptimisticConsensusState(uint256 height)
external
view
returns (uint256 appHash, uint256 fraudProofEndTime, bool ended);

function portIdAddressMatch(address addr, string calldata portId) external view returns (bool isMatch);

function getChannel(address portAddress, bytes32 channelId) external view returns (Channel memory channel);
}
3 changes: 1 addition & 2 deletions contracts/interfaces/IbcDispatcher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

pragma solidity ^0.8.9;

import {Height, CounterParty, ChannelOrder, AckPacket} from "../libs/Ibc.sol";
import {Height, ChannelEnd, ChannelOrder, AckPacket} from "../libs/Ibc.sol";
import {IbcChannelReceiver} from "./IbcReceiver.sol";
import {Ics23Proof} from "./ProofVerifier.sol";

Expand All @@ -24,7 +24,6 @@ interface IbcPacketSender {
*/
interface IbcDispatcher is IbcPacketSender {
function channelOpenInit(
IbcChannelReceiver receiver,
string calldata version,
ChannelOrder ordering,
bool feeEnabled,
Expand Down
2 changes: 1 addition & 1 deletion contracts/interfaces/IbcReceiver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity ^0.8.9;

import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {IbcDispatcher} from "./IbcDispatcher.sol";
import {ChannelOrder, CounterParty, IbcPacket, AckPacket} from "../libs/Ibc.sol";
import {ChannelOrder, ChannelEnd, IbcPacket, AckPacket} from "../libs/Ibc.sol";

/**
* @title IbcChannelReceiver
Expand Down
8 changes: 4 additions & 4 deletions contracts/libs/Ibc.sol
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ struct Channel {
bytes32 counterpartyChannelId;
}

struct CounterParty {
struct ChannelEnd {
string portId;
bytes32 channelId;
string version;
Expand Down Expand Up @@ -227,7 +227,7 @@ library IbcUtils {

// For XXXX => vIBC direction, SC needs to verify the proof of membership of TRY_PENDING
// For vIBC initiated channel, SC doesn't need to verify any proof, and these should be all empty
function isChannelOpenTry(CounterParty calldata counterparty) public pure returns (bool open) {
function isChannelOpenTry(ChannelEnd calldata counterparty) public pure returns (bool open) {
if (counterparty.channelId == bytes32(0) && bytes(counterparty.version).length == 0) {
return false;
// ChanOpenInit with unknow conterparty
Expand Down Expand Up @@ -313,7 +313,7 @@ library Ibc {

// For XXXX => vIBC direction, SC needs to verify the proof of membership of TRY_PENDING
// For vIBC initiated channel, SC doesn't need to verify any proof, and these should be all empty
function _isChannelOpenTry(CounterParty calldata counterparty) external pure returns (bool open) {
function _isChannelOpenTry(ChannelEnd calldata counterparty) external pure returns (bool open) {
if (counterparty.channelId == bytes32(0) && bytes(counterparty.version).length == 0) {
open = false;
// ChanOpenInit with unknow conterparty
Expand Down Expand Up @@ -374,7 +374,7 @@ library Ibc {
ChannelOrder ordering,
string calldata version,
string[] calldata connectionHops,
CounterParty calldata counterparty
ChannelEnd calldata counterparty
) public pure returns (bytes memory proofValue) {
proofValue = ProtoChannel.encode(
ProtoChannel.Data(
Expand Down
8 changes: 1 addition & 7 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,10 @@ libs = ['lib']
test = 'test'
cache_path = 'forge-cache'
gas_reports = ['*']
fs_permissions = [{ access = 'read', path = './test/payload'}]
fs_permissions = [{ access = 'read', path = './test/payload'}, { access = 'read', path = './out'}]
RnkSngh marked this conversation as resolved.
Show resolved Hide resolved
[fmt]
wrap_comments = true
number_underscore = "thousands"
ignore = ["lib/*"]

[rpc_endpoints]
sepolia = "${SEPOLIA_RPC_URL}"

[etherscan]
sepolia = { key = "${ETHERSCAN_API_KEY}" }

# See more config options https://book.getfoundry.sh/reference/config.html
2 changes: 1 addition & 1 deletion script/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "forge-std/Script.sol";
import "../contracts/utils/DummyProofVerifier.sol";
import "../contracts/utils/DummyLightClient.sol";
import "../contracts/core/Dispatcher.sol";
import "../contracts/examples/Mars.sol";
import {Mars} from "../contracts/examples/Mars.sol";
import {IDispatcher} from "../contracts/core/Dispatcher.sol";
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import "../contracts/core/OpProofVerifier.sol";
Expand Down
26 changes: 11 additions & 15 deletions test/Dispatcher.base.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {Dispatcher} from "../contracts/core/Dispatcher.sol";
import {IDispatcher} from "../contracts/interfaces/IDispatcher.sol";
import {IbcEventsEmitter} from "../contracts/interfaces/IbcDispatcher.sol";
import {IbcChannelReceiver} from "../contracts/interfaces/IbcReceiver.sol";
import "../contracts/examples/Mars.sol";
import "../contracts/core/OpLightClient.sol";
import "../contracts/utils/DummyLightClient.sol";
import "../contracts/core/OpProofVerifier.sol";
Expand Down Expand Up @@ -74,18 +73,18 @@ contract Base is IbcEventsEmitter, ProofBase, TestUtilsTest {
* @param expPass Expected pass status of the operation.
* If expPass is false, `vm.expectRevert` should be called before this function.
*/
function channelOpenInit(LocalEnd memory le, CounterParty memory re, ChannelHandshakeSetting memory s, bool expPass)
function channelOpenInit(LocalEnd memory le, ChannelEnd memory re, ChannelHandshakeSetting memory s, bool expPass)
public
{
vm.startPrank(address(le.receiver));
if (expPass) {
vm.expectEmit(true, true, true, true);
emit ChannelOpenInit(
address(le.receiver), le.versionExpected, s.ordering, s.feeEnabled, le.connectionHops, re.portId
);
}
dispatcherProxy.channelOpenInit(
le.receiver, le.versionCall, s.ordering, s.feeEnabled, le.connectionHops, re.portId
);
dispatcherProxy.channelOpenInit(le.versionCall, s.ordering, s.feeEnabled, le.connectionHops, re.portId);
vm.stopPrank();
}

/**
Expand All @@ -96,7 +95,7 @@ contract Base is IbcEventsEmitter, ProofBase, TestUtilsTest {
* @param expPass Expected pass status of the operation.
* If expPass is false, `vm.expectRevert` should be called before this function.
*/
function channelOpenTry(LocalEnd memory le, CounterParty memory re, ChannelHandshakeSetting memory s, bool expPass)
function channelOpenTry(LocalEnd memory le, ChannelEnd memory re, ChannelHandshakeSetting memory s, bool expPass)
public
{
if (expPass) {
Expand All @@ -111,10 +110,9 @@ contract Base is IbcEventsEmitter, ProofBase, TestUtilsTest {
re.channelId
);
}
CounterParty memory cp = CounterParty(re.portId, re.channelId, re.version);
ChannelEnd memory cp = ChannelEnd(re.portId, re.channelId, re.version);
dispatcherProxy.channelOpenTry(
le.receiver,
CounterParty(le.portId, le.channelId, le.versionCall),
ChannelEnd(le.portId, le.channelId, le.versionCall),
s.ordering,
s.feeEnabled,
le.connectionHops,
Expand All @@ -131,16 +129,15 @@ contract Base is IbcEventsEmitter, ProofBase, TestUtilsTest {
* @param expPass Expected pass status of the operation.
* If expPass is false, `vm.expectRevert` should be called before this function.
*/
function channelOpenAck(LocalEnd memory le, CounterParty memory re, ChannelHandshakeSetting memory s, bool expPass)
function channelOpenAck(LocalEnd memory le, ChannelEnd memory re, ChannelHandshakeSetting memory s, bool expPass)
public
{
if (expPass) {
vm.expectEmit(true, true, true, true);
emit ChannelOpenAck(address(le.receiver), le.channelId);
}
dispatcherProxy.channelOpenAck(
le.receiver,
CounterParty(le.portId, le.channelId, le.versionCall),
ChannelEnd(le.portId, le.channelId, le.versionCall),
le.connectionHops,
s.ordering,
s.feeEnabled,
Expand All @@ -159,7 +156,7 @@ contract Base is IbcEventsEmitter, ProofBase, TestUtilsTest {
*/
function channelOpenConfirm(
LocalEnd memory le,
CounterParty memory re,
ChannelEnd memory re,
ChannelHandshakeSetting memory s,
bool expPass
) public {
Expand All @@ -168,8 +165,7 @@ contract Base is IbcEventsEmitter, ProofBase, TestUtilsTest {
emit ChannelOpenConfirm(address(le.receiver), le.channelId);
}
dispatcherProxy.channelOpenConfirm(
le.receiver,
CounterParty(le.portId, le.channelId, le.versionCall),
ChannelEnd(le.portId, le.channelId, le.versionCall),
le.connectionHops,
s.ordering,
s.feeEnabled,
Expand Down
1 change: 0 additions & 1 deletion test/Dispatcher.client.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {IDispatcher} from "../contracts/interfaces/IDispatcher.sol";
import {IbcEventsEmitter} from "../contracts/interfaces/IbcDispatcher.sol";
import {IbcReceiver} from "../contracts/interfaces/IbcReceiver.sol";
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import "../contracts/examples/Mars.sol";
import "../contracts/core/OpLightClient.sol";
import "./Dispatcher.base.t.sol";

Expand Down
Loading
Loading