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

remove mw functionaity from uch #104

Merged
merged 1 commit into from
May 7, 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
2 changes: 1 addition & 1 deletion contracts/base/GeneralMiddleware.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

pragma solidity ^0.8.9;

import {Ibc, UniversalPacket, AckPacket} from "../libs/Ibc.sol";

Check warning on line 21 in contracts/base/GeneralMiddleware.sol

View workflow job for this annotation

GitHub Actions / lint

imported name Ibc is not used
import {IbcUtils} from "../libs/IbcUtils.sol";
import {
IbcUniversalPacketReceiver,
Expand All @@ -35,7 +35,7 @@
* @notice GeneralMiddleware is a starting point for developers to implement their own middleware logic. It is not
* intended to be directly deployed, but rather only used for testing and development
*/
contract GeneralMiddleware is IbcMwUser, IbcMiddleware, IbcMwEventsEmitter {
contract GeneralMiddleware is IbcMwUser, IbcMiddleware, IbcMwEventsEmitter, IbcMwPacketSender {
/**
* @dev MW_ID is the ID of MW contract on all supported virtual chains.
* MW_ID must:
Expand All @@ -45,7 +45,7 @@
* one MW stack.
* - be 1 << N, where N is a non-negative integer, and not in conflict with other MWs.
*/
uint256 public MW_ID;

Check warning on line 48 in contracts/base/GeneralMiddleware.sol

View workflow job for this annotation

GitHub Actions / lint

Variable name must be in mixedCase

event UCHPacketSent(address source, bytes32 destination);
/**
Expand All @@ -63,7 +63,7 @@
uint64 timeoutTimestamp
) external override {
emit UCHPacketSent(msg.sender, destPortAddr);
_sendPacket(channelId, IbcUtils.toBytes32(msg.sender), destPortAddr, 0, appData, timeoutTimestamp);

Check warning on line 66 in contracts/base/GeneralMiddleware.sol

View workflow job for this annotation

GitHub Actions / lint

Named parameters missing. MIN unnamed argumenst is 4
}

function sendMWPacket(
Expand All @@ -74,10 +74,10 @@
bytes calldata appData,
uint64 timeoutTimestamp
) external override {
_sendPacket(channelId, srcPortAddr, destPortAddr, srcMwIds, appData, timeoutTimestamp);

Check warning on line 77 in contracts/base/GeneralMiddleware.sol

View workflow job for this annotation

GitHub Actions / lint

Named parameters missing. MIN unnamed argumenst is 4
}

function onRecvMWPacket(

Check warning on line 80 in contracts/base/GeneralMiddleware.sol

View workflow job for this annotation

GitHub Actions / lint

All public or external methods in a contract must override a definition from an interface
bytes32 channelId,
UniversalPacket calldata ucPacket,
// address srcPortAddr,
Expand All @@ -93,7 +93,7 @@
// extra MW custom logic here to process packet, eg. emit MW events, mutate state, etc.
// implementer can emit custom data fields suitable for their use cases.
// Here we use MW_ID as the custom MW data field.
emit RecvMWPacket(

Check warning on line 96 in contracts/base/GeneralMiddleware.sol

View workflow job for this annotation

GitHub Actions / lint

Named parameters missing. MIN unnamed argumenst is 4
channelId, ucPacket.srcPortAddr, ucPacket.destPortAddr, MW_ID, ucPacket.appData, abi.encodePacked(MW_ID)
);

Expand Down Expand Up @@ -122,7 +122,7 @@
// extra MW custom logic here to process packet, eg. emit MW events, mutate state, etc.
// implementer can emit custom data fields suitable for their use cases.
// Here we use MW_ID as the custom MW data field.
emit RecvMWAck(

Check warning on line 125 in contracts/base/GeneralMiddleware.sol

View workflow job for this annotation

GitHub Actions / lint

Named parameters missing. MIN unnamed argumenst is 4
channelId,
ucPacket.srcPortAddr,
ucPacket.destPortAddr,
Expand All @@ -139,7 +139,7 @@
);
} else {
// send ack to next MW
IbcMwPacketReceiver(mwAddrs[mwIndex + 1]).onRecvMWAck(channelId, ucPacket, mwIndex + 1, mwAddrs, ack);

Check warning on line 142 in contracts/base/GeneralMiddleware.sol

View workflow job for this annotation

GitHub Actions / lint

Named parameters missing. MIN unnamed argumenst is 4
}
}

Expand All @@ -152,7 +152,7 @@
// extra MW custom logic here to process packet, eg. emit MW events, mutate state, etc.
// implementer can emit custom data fields suitable for their use cases.
// Here we use MW_ID as the custom MW data field.
emit RecvMWTimeout(

Check warning on line 155 in contracts/base/GeneralMiddleware.sol

View workflow job for this annotation

GitHub Actions / lint

Named parameters missing. MIN unnamed argumenst is 4
channelId, ucPacket.srcPortAddr, ucPacket.destPortAddr, MW_ID, ucPacket.appData, abi.encodePacked(MW_ID)
);

Expand All @@ -172,7 +172,7 @@
override
onlyIbcMw
returns (AckPacket memory ackPacket)
{}

Check warning on line 175 in contracts/base/GeneralMiddleware.sol

View workflow job for this annotation

GitHub Actions / lint

Code contains empty blocks

function onUniversalAcknowledgement(bytes32 channelId, UniversalPacket memory packet, AckPacket calldata ack)
external
Expand Down
82 changes: 12 additions & 70 deletions contracts/core/UniversalChannelHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ contract UniversalChannelHandler is IbcReceiverBaseUpgradeable, UUPSUpgradeable,
string public constant VERSION = "1.0";
uint256 public constant MW_ID = 1;

// Key: middleware bitmap, Value: middleware address from receiver(chain B)'s perspective
mapping(uint256 => address[]) public mwStackAddrs;

event UCHPacketSent(address source, bytes32 destination);

constructor() {
Expand Down Expand Up @@ -103,30 +100,6 @@ contract UniversalChannelHandler is IbcReceiverBaseUpgradeable, UUPSUpgradeable,
dispatcher.sendPacket(channelId, packetData, timeoutTimestamp);
}

/**
* @notice Sends a middleware packet over an IBC channel. This is intended to be called by another middleware
* contract, rather than an end Dapp itself.
* @param channelId The channel ID through which the packet is sent on the dispatcher
* @param destPortAddr The destination port address
* @param srcMwIds The mwId bitmap of the middleware stack
* @param appData The packet data to be sent
* @param timeoutTimestamp of when the packet can timeout
*/
function sendMWPacket(
bytes32 channelId,
// original source address of the packet
bytes32 srcPortAddr,
bytes32 destPortAddr,
// source middleware ids bit AND
uint256 srcMwIds,
bytes calldata appData,
uint64 timeoutTimestamp
) external {
bytes memory packetData =
IbcUtils.toUniversalPacketBytes(UniversalPacket(srcPortAddr, srcMwIds | MW_ID, destPortAddr, appData));
dispatcher.sendPacket(channelId, packetData, timeoutTimestamp);
}

/**
* @notice Handles the reception of an IBC packet from the counterparty
* @param packet The received IBC packet
Expand All @@ -139,16 +112,10 @@ contract UniversalChannelHandler is IbcReceiverBaseUpgradeable, UUPSUpgradeable,
returns (AckPacket memory ackPacket)
{
UniversalPacket memory ucPacket = IbcUtils.fromUniversalPacketBytes(packet.data);
address[] storage mwAddrs = mwStackAddrs[ucPacket.mwBitmap];
if (mwAddrs.length == 0) {
// no other middleware stack registered for this packet. Deliver packet to dApp directly.
return IbcUniversalPacketReceiver(IbcUtils.toAddress(ucPacket.destPortAddr)).onRecvUniversalPacket(
packet.dest.channelId, ucPacket
);
} else {
// send packet to first MW in the stack
return IbcMwPacketReceiver(mwAddrs[0]).onRecvMWPacket(packet.dest.channelId, ucPacket, 0, mwAddrs);
}
// no other middleware stack registered for this packet. Deliver packet to dApp directly.
return IbcUniversalPacketReceiver(IbcUtils.toAddress(ucPacket.destPortAddr)).onRecvUniversalPacket(
packet.dest.channelId, ucPacket
);
}

/**
Expand All @@ -162,16 +129,10 @@ contract UniversalChannelHandler is IbcReceiverBaseUpgradeable, UUPSUpgradeable,
onlyIbcDispatcher
{
UniversalPacket memory ucPacket = IbcUtils.fromUniversalPacketBytes(packet.data);
address[] storage mwAddrs = mwStackAddrs[ucPacket.mwBitmap];
if (mwAddrs.length == 0) {
// no other middleware stack registered for this packet. Deliver ack to dApp directly.
IbcUniversalPacketReceiver(IbcUtils.toAddress(ucPacket.srcPortAddr)).onUniversalAcknowledgement(
packet.src.channelId, ucPacket, ack
);
} else {
// send ack to last MW in the stack
IbcMwPacketReceiver(mwAddrs[0]).onRecvMWAck(packet.src.channelId, ucPacket, 0, mwAddrs, ack);
}
// no other middleware stack registered for this packet. Deliver ack to dApp directly.
IbcUniversalPacketReceiver(IbcUtils.toAddress(ucPacket.srcPortAddr)).onUniversalAcknowledgement(
packet.src.channelId, ucPacket, ack
);
}

/**
Expand All @@ -180,29 +141,10 @@ contract UniversalChannelHandler is IbcReceiverBaseUpgradeable, UUPSUpgradeable,
*/
function onTimeoutPacket(IbcPacket calldata packet) external override onlyIbcDispatcher {
UniversalPacket memory ucPacketData = IbcUtils.fromUniversalPacketBytes(packet.data);
address[] storage mwAddrs = mwStackAddrs[ucPacketData.mwBitmap];
if (mwAddrs.length == 0) {
// no other middleware stack registered for this packet. Deliver timeout to dApp directly.
IbcUniversalPacketReceiver(IbcUtils.toAddress(ucPacketData.srcPortAddr)).onTimeoutUniversalPacket(
packet.src.channelId, ucPacketData
);
} else {
// send timeout to last MW in the stack
IbcMwPacketReceiver(mwAddrs[0]).onRecvMWTimeout(packet.src.channelId, ucPacketData, 0, mwAddrs);
}
}

/**
* @dev Register a middleware stack for universal packet routing.
* This is a temporary solution for testing only.
* Polymer chain will maintain a global registry of middleware stacks.
* @param mwBitmap Bit OR of all MW IDs in the stack, excluding this MW's ID
* @param mwAddrs addresses in the stack, from the perspective of the receiver (chain B)
* MW closer to UniversalChannel MW has smaller index. MW stack must be in the same order on both chains.
*/
function registerMwStack(uint256 mwBitmap, address[] calldata mwAddrs) external onlyOwner {
if (mwBitmap == 0) revert MwBitmpaCannotBeZero();
mwStackAddrs[mwBitmap] = mwAddrs;
// no other middleware stack registered for this packet. Deliver timeout to dApp directly.
IbcUniversalPacketReceiver(IbcUtils.toAddress(ucPacketData.srcPortAddr)).onTimeoutUniversalPacket(
packet.src.channelId, ucPacketData
);
}

function onChanOpenConfirm(bytes32 channelId) external onlyIbcDispatcher {}
Expand Down
2 changes: 1 addition & 1 deletion contracts/interfaces/IbcMiddleware.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ interface IbcUniversalPacketReceiver {
function onTimeoutUniversalPacket(bytes32 channelId, UniversalPacket calldata packet) external;
}

interface IbcMiddlwareProvider is IbcUniversalPacketSender, IbcMwPacketSender {
interface IbcMiddlwareProvider is IbcUniversalPacketSender {
RnkSngh marked this conversation as resolved.
Show resolved Hide resolved
/**
* @dev MW_ID is the ID of MW contract on all supported virtual chains.
* MW_ID must:
Expand Down
2 changes: 2 additions & 0 deletions test/universal.channel.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,14 @@ contract UniversalChannelPacketTest is Base, IbcMwEventsEmitter {

// packet flow: Earth -> MW1 -> UC -> Dispatcher -> (Relayer) -> Dispatcher -> UC -> MW1 -> Earth
function test_packetFlow_via_mw1_ok() public {
vm.skip(true); // uch as middleware sender is not yet supported
uint256 mwBitmap = register_mw1();
verifyPacketFlow(5, mwBitmap);
}

// packet flow: Earth -> MW1 -> MW2 -> UC -> Dispatcher -> (Relayer) -> Dispatcher -> UC -> MW2 -> MW1 -> Earth
function test_packetFlow_via_mw2_ok() public {
vm.skip(true); // uch as middleware sender is not yet supported
uint256 mwBitmap = register_mw1_mw2();
verifyPacketFlow(5, mwBitmap);
}
Expand Down
39 changes: 9 additions & 30 deletions test/upgradeableProxy/DispatcherRC4.upgrade.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -172,19 +172,6 @@ contract DispatcherRC4MidwayUpgradeTest is ChannelHandShakeUpgradeUtil, UpgradeT
IUniversalChannelHandler uch;
LocalEnd _localUch;
Earth earth;
uint256 mwBitmap;

GeneralMiddleware mw1;

event RecvMWPacket(
bytes32 indexed channelId,
bytes32 indexed srcPortAddr,
bytes32 indexed destPortAddr,
// middleware UID
uint256 mwId,
bytes appData,
bytes mwData
);

function setUp() public override {
ChannelHandshakeSetting memory setting = ChannelHandshakeSetting(ChannelOrder.ORDERED, false, true, validProof);
Expand All @@ -198,12 +185,6 @@ contract DispatcherRC4MidwayUpgradeTest is ChannelHandShakeUpgradeUtil, UpgradeT
mars = new Mars(dispatcherProxy);
uch = deployUCHV2ProxyAndImpl(address(dispatcherProxy));
earth = new Earth(address(uch));
// Register mw stack and test it isn't corrupted after the upgrade
mw1 = new GeneralMiddleware(1 << 1, address(uch));
mwBitmap = uch.MW_ID() | mw1.MW_ID();
address[] memory mwAddrs = new address[](1);
mwAddrs[0] = address(mw1);
uch.registerMwStack(mwBitmap, mwAddrs);
receivingMars = new Mars(dispatcherProxy);

sendingPortId = IbcUtils.addressToPortId(portPrefix, address(mars));
Expand All @@ -222,7 +203,7 @@ contract DispatcherRC4MidwayUpgradeTest is ChannelHandShakeUpgradeUtil, UpgradeT

channelOpenTry(_re, _le, setting, true);

// Do channel handshake via uch using mw set before the upgrade, to ensure that mw addrs state is still useful
// Do channel handshake via uch
doProofChannelHandshake(_localUch, _remote);
earth.greet(address(mars), _localUch.channelId, bytes("hello mars"), UINT64_MAX);
}
Expand Down Expand Up @@ -279,10 +260,9 @@ contract DispatcherRC4MidwayUpgradeTest is ChannelHandShakeUpgradeUtil, UpgradeT
assertEq(2, nextSequenceSendValue);

// Now recv uch packet

bytes memory appData = abi.encodePacked("hello using mw stack");
UniversalPacket memory ucPacket =
UniversalPacket(IbcUtils.toBytes32(address(mars)), mwBitmap, IbcUtils.toBytes32(address(earth)), appData);
UniversalPacket(IbcUtils.toBytes32(address(mars)), uch.MW_ID(), IbcUtils.toBytes32(address(earth)), appData);
bytes memory packetData = IbcUtils.toUniversalPacketBytes(ucPacket);
IbcPacket memory uchPacket = IbcPacket(
IbcEndpoint(sendingPortId, _local.channelId),
Expand All @@ -292,15 +272,14 @@ contract DispatcherRC4MidwayUpgradeTest is ChannelHandShakeUpgradeUtil, UpgradeT
Height(0, 0),
maxTimeout
);

AckPacket memory earthAck = earth.generateAckPacket(0x0, address(mars), appData);
vm.expectEmit(true, true, true, true);
emit RecvMWPacket(
_localUch.channelId,
ucPacket.srcPortAddr,
ucPacket.destPortAddr,
mw1.MW_ID(),
ucPacket.appData,
abi.encodePacked(mw1.MW_ID())
);
emit WriteAckPacket(address(uch), _localUch.channelId, 1, earthAck);
dispatcherProxy.recvPacket(uchPacket, validProof);

(bytes32 actualChannelId, UniversalPacket memory storedUcPacket) = earth.recvedPackets(0);
assertEq(actualChannelId, _localUch.channelId);
assertEq(storedUcPacket.appData, ucPacket.appData);
}
}
Loading