From b48336652584e5a0e9aa87f8fb4814d315aa428e Mon Sep 17 00:00:00 2001 From: Akash Date: Fri, 11 Apr 2025 17:02:40 +0530 Subject: [PATCH 1/2] fix: open transmitter option, appGatewayId --- contracts/base/PlugBase.sol | 17 ++++++------- contracts/interfaces/IContractFactoryPlug.sol | 2 +- contracts/interfaces/IPlug.sol | 2 +- contracts/interfaces/ISocket.sol | 10 ++++---- .../interfaces/IWatcherPrecompileConfig.sol | 2 +- .../payload-delivery/ContractFactoryPlug.sol | 8 +++---- .../protocol/payload-delivery/FeesPlug.sol | 4 ++-- contracts/protocol/socket/Socket.sol | 15 ++++++------ contracts/protocol/socket/SocketConfig.sol | 12 +++++----- contracts/protocol/socket/SocketUtils.sol | 12 ++++------ .../socket/switchboard/SwitchboardBase.sol | 2 +- contracts/protocol/utils/common/Structs.sol | 8 +++---- .../watcherPrecompile/WatcherPrecompile.sol | 2 +- .../WatcherPrecompileConfig.sol | 23 +++++++++--------- .../WatcherPrecompileCore.sol | 14 ++++++----- .../WatcherPrecompileUtils.sol | 12 ++++++++++ test/DeliveryHelper.t.sol | 24 +++++++++++-------- test/Inbox.t.sol | 6 ++--- test/SetupTest.t.sol | 10 +++++++- test/mock/MockSocket.sol | 16 ++++++------- test/mock/MockWatcherPrecompile.sol | 4 ++-- 21 files changed, 114 insertions(+), 91 deletions(-) create mode 100644 contracts/protocol/watcherPrecompile/WatcherPrecompileUtils.sol diff --git a/contracts/base/PlugBase.sol b/contracts/base/PlugBase.sol index 64dff39d..e86daa9f 100644 --- a/contracts/base/PlugBase.sol +++ b/contracts/base/PlugBase.sol @@ -9,7 +9,7 @@ import {NotSocket} from "../protocol/utils/common/Errors.sol"; /// @notice Abstract contract for plugs abstract contract PlugBase is IPlug { ISocket public socket__; - address public appGateway; + bytes32 public appGatewayId; uint256 public isSocketInitialized; bytes public overrides; @@ -31,19 +31,20 @@ abstract contract PlugBase is IPlug { } /// @notice Connects the plug to the app gateway and switchboard - /// @param appGateway_ The app gateway address + /// @param appGatewayId_ The app gateway id + /// @param socket_ The socket address /// @param switchboard_ The switchboard address - function _connectSocket(address appGateway_, address socket_, address switchboard_) internal { + function _connectSocket(bytes32 appGatewayId_, address socket_, address switchboard_) internal { _setSocket(socket_); - appGateway = appGateway_; + appGatewayId = appGatewayId_; - socket__.connect(appGateway_, switchboard_); + socket__.connect(appGatewayId_, switchboard_); } /// @notice Disconnects the plug from the socket function _disconnectSocket() internal { (, address switchboard) = socket__.getPlugConfig(address(this)); - socket__.connect(address(0), switchboard); + socket__.connect(bytes32(0), switchboard); emit ConnectorPlugDisconnected(); } @@ -60,10 +61,10 @@ abstract contract PlugBase is IPlug { } function initSocket( - address appGateway_, + bytes32 appGatewayId_, address socket_, address switchboard_ ) external virtual socketInitializer { - _connectSocket(appGateway_, socket_, switchboard_); + _connectSocket(appGatewayId_, socket_, switchboard_); } } diff --git a/contracts/interfaces/IContractFactoryPlug.sol b/contracts/interfaces/IContractFactoryPlug.sol index a3b5bac0..89de42e3 100644 --- a/contracts/interfaces/IContractFactoryPlug.sol +++ b/contracts/interfaces/IContractFactoryPlug.sol @@ -14,7 +14,7 @@ interface IContractFactoryPlug { function deployContract( IsPlug isPlug_, bytes32 salt_, - address appGateway_, + bytes32 appGatewayId_, address switchboard_, bytes memory creationCode_, bytes memory initCallData_ diff --git a/contracts/interfaces/IPlug.sol b/contracts/interfaces/IPlug.sol index 552ae8f0..c7cad4aa 100644 --- a/contracts/interfaces/IPlug.sol +++ b/contracts/interfaces/IPlug.sol @@ -6,7 +6,7 @@ pragma solidity ^0.8.21; * @notice Interface for a plug contract that executes the payload received from a source chain. */ interface IPlug { - function initSocket(address appGateway_, address socket_, address switchboard_) external; + function initSocket(bytes32 appGatewayId_, address socket_, address switchboard_) external; function overrides() external view returns (bytes memory); } diff --git a/contracts/interfaces/ISocket.sol b/contracts/interfaces/ISocket.sol index 42713610..dc924f87 100644 --- a/contracts/interfaces/ISocket.sol +++ b/contracts/interfaces/ISocket.sol @@ -27,10 +27,10 @@ interface ISocket { /** * @notice emits the config set by a plug for a remoteChainSlug * @param plug address of plug on current chain - * @param appGateway address of plug on sibling chain + * @param appGatewayId address of plug on sibling chain * @param switchboard outbound switchboard (select from registered options) */ - event PlugConnected(address plug, address appGateway, address switchboard); + event PlugConnected(address plug, bytes32 appGatewayId, address switchboard); /** * @notice emits the message details when a new message arrives at outbound @@ -58,10 +58,10 @@ interface ISocket { /** * @notice sets the config specific to the plug - * @param appGateway_ address of plug present at sibling chain + * @param appGatewayId_ address of plug present at sibling chain * @param switchboard_ the address of switchboard to use for executing payloads */ - function connect(address appGateway_, address switchboard_) external; + function connect(bytes32 appGatewayId_, address switchboard_) external; function registerSwitchboard() external; @@ -71,5 +71,5 @@ interface ISocket { */ function getPlugConfig( address plugAddress_ - ) external view returns (address appGateway, address switchboard); + ) external view returns (bytes32 appGatewayId, address switchboard); } diff --git a/contracts/interfaces/IWatcherPrecompileConfig.sol b/contracts/interfaces/IWatcherPrecompileConfig.sol index 85831eca..bf4ccf6f 100644 --- a/contracts/interfaces/IWatcherPrecompileConfig.sol +++ b/contracts/interfaces/IWatcherPrecompileConfig.sol @@ -43,7 +43,7 @@ interface IWatcherPrecompileConfig { function getPlugConfigs( uint32 chainSlug_, address plug_ - ) external view returns (address, address); + ) external view returns (bytes32, address); /// @notice Verifies connections between components function verifyConnections( diff --git a/contracts/protocol/payload-delivery/ContractFactoryPlug.sol b/contracts/protocol/payload-delivery/ContractFactoryPlug.sol index 54e4fdac..94634e8b 100644 --- a/contracts/protocol/payload-delivery/ContractFactoryPlug.sol +++ b/contracts/protocol/payload-delivery/ContractFactoryPlug.sol @@ -29,7 +29,7 @@ contract ContractFactoryPlug is PlugBase, AccessControl, IContractFactoryPlug { function deployContract( IsPlug isPlug_, bytes32 salt_, - address appGateway_, + bytes32 appGatewayId_, address switchboard_, bytes memory creationCode_, bytes memory initCallData_ @@ -47,7 +47,7 @@ contract ContractFactoryPlug is PlugBase, AccessControl, IContractFactoryPlug { } } - if (isPlug_ == IsPlug.YES) IPlug(addr).initSocket(appGateway_, msg.sender, switchboard_); + if (isPlug_ == IsPlug.YES) IPlug(addr).initSocket(appGatewayId_, msg.sender, switchboard_); bytes memory returnData; if (initCallData_.length > 0) { @@ -89,11 +89,11 @@ contract ContractFactoryPlug is PlugBase, AccessControl, IContractFactoryPlug { } function connectSocket( - address appGateway_, + bytes32 appGatewayId_, address socket_, address switchboard_ ) external onlyOwner { - _connectSocket(appGateway_, socket_, switchboard_); + _connectSocket(appGatewayId_, socket_, switchboard_); } /** diff --git a/contracts/protocol/payload-delivery/FeesPlug.sol b/contracts/protocol/payload-delivery/FeesPlug.sol index 77778f05..c73200e2 100644 --- a/contracts/protocol/payload-delivery/FeesPlug.sol +++ b/contracts/protocol/payload-delivery/FeesPlug.sol @@ -103,11 +103,11 @@ contract FeesPlug is PlugBase, AccessControl { } function connectSocket( - address appGateway_, + bytes32 appGatewayId_, address socket_, address switchboard_ ) external onlyOwner { - _connectSocket(appGateway_, socket_, switchboard_); + _connectSocket(appGatewayId_, socket_, switchboard_); } /// @notice Adds a token to the whitelist diff --git a/contracts/protocol/socket/Socket.sol b/contracts/protocol/socket/Socket.sol index e223b28d..1506f056 100644 --- a/contracts/protocol/socket/Socket.sol +++ b/contracts/protocol/socket/Socket.sol @@ -52,21 +52,20 @@ contract Socket is SocketUtils { ) external payable returns (bytes memory) { if (executeParams_.deadline < block.timestamp) revert DeadlinePassed(); PlugConfig memory plugConfig = _plugConfigs[executeParams_.target]; - if (plugConfig.appGateway == address(0)) revert PlugDisconnected(); + if (plugConfig.appGatewayId == bytes32(0)) revert PlugDisconnected(); if (msg.value < executeParams_.value) revert InsufficientMsgValue(); bytes32 payloadId = _createPayloadId(plugConfig.switchboard, executeParams_); _validateExecutionStatus(payloadId); - address transmitter = _recoverSigner( - keccak256(abi.encode(address(this), payloadId)), - transmitterSignature_ - ); + address transmitter = transmitterSignature_.length > 0 + ? _recoverSigner(keccak256(abi.encode(address(this), payloadId)), transmitterSignature_) + : address(0); bytes32 digest = _createDigest( transmitter, payloadId, - plugConfig.appGateway, + plugConfig.appGatewayId, executeParams_ ); _verify(digest, payloadId, plugConfig.switchboard); @@ -135,10 +134,10 @@ contract Socket is SocketUtils { PlugConfig memory plugConfig = _plugConfigs[plug_]; // if no sibling plug is found for the given chain slug, revert - if (plugConfig.appGateway == address(0)) revert PlugDisconnected(); + if (plugConfig.appGatewayId == bytes32(0)) revert PlugDisconnected(); // creates a unique ID for the message - triggerId = _encodeTriggerId(plugConfig.appGateway); + triggerId = _encodeTriggerId(plugConfig.appGatewayId); emit AppGatewayCallRequested(triggerId, chainSlug, plug_, overrides_, payload_); } diff --git a/contracts/protocol/socket/SocketConfig.sol b/contracts/protocol/socket/SocketConfig.sol index 09aaf19f..4ccc6a82 100644 --- a/contracts/protocol/socket/SocketConfig.sol +++ b/contracts/protocol/socket/SocketConfig.sol @@ -47,16 +47,16 @@ abstract contract SocketConfig is ISocket, AccessControl { /** * @notice connects Plug to Socket and sets the config for given `siblingChainSlug_` */ - function connect(address appGateway_, address switchboard_) external override { + function connect(bytes32 appGatewayId_, address switchboard_) external override { if (isValidSwitchboard[switchboard_] != SwitchboardStatus.REGISTERED) revert InvalidSwitchboard(); PlugConfig storage _plugConfig = _plugConfigs[msg.sender]; - _plugConfig.appGateway = appGateway_; + _plugConfig.appGatewayId = appGatewayId_; _plugConfig.switchboard = switchboard_; - emit PlugConnected(msg.sender, appGateway_, switchboard_); + emit PlugConnected(msg.sender, appGatewayId_, switchboard_); } function setMaxCopyBytes(uint16 maxCopyBytes_) external onlyRole(GOVERNANCE_ROLE) { @@ -64,13 +64,13 @@ abstract contract SocketConfig is ISocket, AccessControl { } /** - * @notice returns the config for given `plugAddress_` and `siblingChainSlug_` + * @notice returns the config for given `plugAddress_` * @param plugAddress_ address of plug present at current chain */ function getPlugConfig( address plugAddress_ - ) external view returns (address appGateway, address switchboard) { + ) external view returns (bytes32 appGatewayId, address switchboard) { PlugConfig memory _plugConfig = _plugConfigs[plugAddress_]; - return (_plugConfig.appGateway, _plugConfig.switchboard); + return (_plugConfig.appGatewayId, _plugConfig.switchboard); } } diff --git a/contracts/protocol/socket/SocketUtils.sol b/contracts/protocol/socket/SocketUtils.sol index ce97e8a2..0d905a3c 100644 --- a/contracts/protocol/socket/SocketUtils.sol +++ b/contracts/protocol/socket/SocketUtils.sol @@ -52,14 +52,14 @@ abstract contract SocketUtils is SocketConfig { * @notice creates the digest for the payload * @param transmitter_ The address of the transmitter * @param payloadId_ The ID of the payload - * @param appGateway_ The address of the app gateway + * @param appGatewayId_ The id of the app gateway * @param executeParams_ The parameters of the payload * @return The packed payload as a bytes32 hash */ function _createDigest( address transmitter_, bytes32 payloadId_, - address appGateway_, + bytes32 appGatewayId_, ExecuteParams memory executeParams_ ) internal view returns (bytes32) { return @@ -76,7 +76,7 @@ abstract contract SocketUtils is SocketConfig { executeParams_.readAt, executeParams_.payload, executeParams_.target, - appGateway_, + appGatewayId_, executeParams_.prevDigestsHash ) ); @@ -116,12 +116,10 @@ abstract contract SocketUtils is SocketConfig { // Packs the local plug, local chain slug, remote chain slug and nonce // triggerCounter++ will take care of call id overflow as well // triggerId(256) = localChainSlug(32) | appGateway_(160) | nonce(64) - function _encodeTriggerId(address appGateway_) internal returns (bytes32) { + function _encodeTriggerId(bytes32 appGatewayId_) internal returns (bytes32) { return bytes32( - (uint256(chainSlug) << 224) | - (uint256(uint160(appGateway_)) << 64) | - triggerCounter++ + (uint256(chainSlug) << 224) | (uint256(appGatewayId_) << 64) | triggerCounter++ ); } diff --git a/contracts/protocol/socket/switchboard/SwitchboardBase.sol b/contracts/protocol/socket/switchboard/SwitchboardBase.sol index 7b78aae8..2f5249a5 100644 --- a/contracts/protocol/socket/switchboard/SwitchboardBase.sol +++ b/contracts/protocol/socket/switchboard/SwitchboardBase.sol @@ -13,7 +13,7 @@ abstract contract SwitchboardBase is ISwitchboard, AccessControl { // chain slug of deployed chain uint32 public immutable chainSlug; - + /** * @dev Constructor of SwitchboardBase * @param chainSlug_ Chain slug of deployment chain diff --git a/contracts/protocol/utils/common/Structs.sol b/contracts/protocol/utils/common/Structs.sol index 02ed6140..a3ffce79 100644 --- a/contracts/protocol/utils/common/Structs.sol +++ b/contracts/protocol/utils/common/Structs.sol @@ -66,13 +66,13 @@ struct UpdateLimitParams { } struct AppGatewayConfig { address plug; - address appGateway; + bytes32 appGatewayId; address switchboard; uint32 chainSlug; } // Plug config: struct PlugConfig { - address appGateway; + bytes32 appGatewayId; address switchboard; } //trigger: @@ -80,7 +80,7 @@ struct TriggerParams { bytes32 triggerId; bytes32 params; address plug; - address appGateway; + bytes32 appGatewayId; uint32 chainSlug; bytes payload; } @@ -143,7 +143,7 @@ struct DigestParams { uint256 readAt; bytes payload; address target; - address appGateway; + bytes32 appGatewayId; bytes32 prevDigestsHash; // should be id? hash of hashes } diff --git a/contracts/protocol/watcherPrecompile/WatcherPrecompile.sol b/contracts/protocol/watcherPrecompile/WatcherPrecompile.sol index a05aaff3..7983f415 100644 --- a/contracts/protocol/watcherPrecompile/WatcherPrecompile.sol +++ b/contracts/protocol/watcherPrecompile/WatcherPrecompile.sol @@ -259,7 +259,7 @@ contract WatcherPrecompile is RequestHandler { for (uint256 i = 0; i < params_.length; i++) { if (appGatewayCalled[params_[i].triggerId]) revert AppGatewayAlreadyCalled(); - address appGateway = _decodeAppGateway(params_[i].triggerId); + address appGateway = _decodeAppGatewayId(params_[i].appGatewayId); if ( !watcherPrecompileConfig__.isValidPlug( appGateway, diff --git a/contracts/protocol/watcherPrecompile/WatcherPrecompileConfig.sol b/contracts/protocol/watcherPrecompile/WatcherPrecompileConfig.sol index 881b3706..e9b30baa 100644 --- a/contracts/protocol/watcherPrecompile/WatcherPrecompileConfig.sol +++ b/contracts/protocol/watcherPrecompile/WatcherPrecompileConfig.sol @@ -5,7 +5,7 @@ import "./WatcherPrecompileLimits.sol"; import {ECDSA} from "solady/utils/ECDSA.sol"; import "solady/utils/Initializable.sol"; import "../../interfaces/IWatcherPrecompileConfig.sol"; - +import "./WatcherPrecompileUtils.sol"; /// @title WatcherPrecompileConfig /// @notice Configuration contract for the Watcher Precompile system /// @dev Handles the mapping between networks, plugs, and app gateways for payload execution @@ -13,7 +13,8 @@ contract WatcherPrecompileConfig is IWatcherPrecompileConfig, Initializable, AccessControl, - AddressResolverUtil + AddressResolverUtil, + WatcherPrecompileUtils { // slot 52: evmxSlug /// @notice The chain slug of the watcher precompile @@ -54,10 +55,10 @@ contract WatcherPrecompileConfig is mapping(address => mapping(uint32 => mapping(address => bool))) public isValidPlug; /// @notice Emitted when a new plug is configured for an app gateway - /// @param appGateway The address of the app gateway + /// @param appGatewayId The id of the app gateway /// @param chainSlug The identifier of the destination network /// @param plug The address of the plug - event PlugAdded(address appGateway, uint32 chainSlug, address plug); + event PlugAdded(bytes32 appGatewayId, uint32 chainSlug, address plug); /// @notice Emitted when a switchboard is set for a network /// @param chainSlug The identifier of the network @@ -114,11 +115,11 @@ contract WatcherPrecompileConfig is for (uint256 i = 0; i < configs_.length; i++) { // Store the plug configuration for this network and plug _plugConfigs[configs_[i].chainSlug][configs_[i].plug] = PlugConfig({ - appGateway: configs_[i].appGateway, + appGatewayId: configs_[i].appGatewayId, switchboard: configs_[i].switchboard }); - emit PlugAdded(configs_[i].appGateway, configs_[i].chainSlug, configs_[i].plug); + emit PlugAdded(configs_[i].appGatewayId, configs_[i].chainSlug, configs_[i].plug); } } @@ -157,14 +158,14 @@ contract WatcherPrecompileConfig is /// @notice Retrieves the configuration for a specific plug on a network /// @param chainSlug_ The identifier of the network /// @param plug_ The address of the plug - /// @return The app gateway address and switchboard address for the plug + /// @return The app gateway id and switchboard address for the plug /// @dev Returns zero addresses if configuration doesn't exist function getPlugConfigs( uint32 chainSlug_, address plug_ - ) public view returns (address, address) { + ) public view returns (bytes32, address) { return ( - _plugConfigs[chainSlug_][plug_].appGateway, + _plugConfigs[chainSlug_][plug_].appGatewayId, _plugConfigs[chainSlug_][plug_].switchboard ); } @@ -179,8 +180,8 @@ contract WatcherPrecompileConfig is // if target is contractFactoryPlug, return if (target_ == contractFactoryPlug[chainSlug_]) return; - (address appGateway, address switchboard) = getPlugConfigs(chainSlug_, target_); - if (appGateway != appGateway_) revert InvalidGateway(); + (bytes32 appGatewayId, address switchboard) = getPlugConfigs(chainSlug_, target_); + if (appGatewayId != _encodeAppGatewayId(appGateway_)) revert InvalidGateway(); if (switchboard != switchboard_) revert InvalidSwitchboard(); } diff --git a/contracts/protocol/watcherPrecompile/WatcherPrecompileCore.sol b/contracts/protocol/watcherPrecompile/WatcherPrecompileCore.sol index 2ee056f7..f195217a 100644 --- a/contracts/protocol/watcherPrecompile/WatcherPrecompileCore.sol +++ b/contracts/protocol/watcherPrecompile/WatcherPrecompileCore.sol @@ -6,6 +6,7 @@ import {ECDSA} from "solady/utils/ECDSA.sol"; import {AccessControl} from "../utils/AccessControl.sol"; import "solady/utils/Initializable.sol"; import {AddressResolverUtil} from "../utils/AddressResolverUtil.sol"; +import "./WatcherPrecompileUtils.sol"; /// @title WatcherPrecompile /// @notice Contract that handles payload verification, execution and app configurations @@ -14,7 +15,8 @@ abstract contract WatcherPrecompileCore is WatcherPrecompileStorage, Initializable, AccessControl, - AddressResolverUtil + AddressResolverUtil, + WatcherPrecompileUtils { using DumpDecoder for bytes32; @@ -81,7 +83,7 @@ abstract contract WatcherPrecompileCore is params_.readAt, params_.payload, params_.target, - params_.appGateway, + _encodeAppGatewayId(params_.appGateway), prevDigestsHash ); @@ -129,7 +131,7 @@ abstract contract WatcherPrecompileCore is params_.readAt, params_.payload, params_.target, - params_.appGateway, + params_.appGatewayId, params_.prevDigestsHash ) ); @@ -163,7 +165,7 @@ abstract contract WatcherPrecompileCore is p.readAt, p.payload, p.target, - p.appGateway, + _encodeAppGatewayId(p.appGateway), p.prevDigestsHash ); prevDigestsHash = keccak256(abi.encodePacked(prevDigestsHash, getDigest(digestParams))); @@ -188,11 +190,11 @@ abstract contract WatcherPrecompileCore is // if target is contractFactoryPlug, return if (target_ == watcherPrecompileConfig__.contractFactoryPlug(chainSlug_)) return; - (address appGateway, address switchboard) = watcherPrecompileConfig__.getPlugConfigs( + (bytes32 appGatewayId, address switchboard) = watcherPrecompileConfig__.getPlugConfigs( chainSlug_, target_ ); - if (appGateway != appGateway_) revert InvalidGateway(); + if (appGatewayId != _encodeAppGatewayId(appGateway_)) revert InvalidGateway(); if (switchboard != switchboard_) revert InvalidSwitchboard(); } diff --git a/contracts/protocol/watcherPrecompile/WatcherPrecompileUtils.sol b/contracts/protocol/watcherPrecompile/WatcherPrecompileUtils.sol new file mode 100644 index 00000000..c47786e8 --- /dev/null +++ b/contracts/protocol/watcherPrecompile/WatcherPrecompileUtils.sol @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.22; + +abstract contract WatcherPrecompileUtils { + function _encodeAppGatewayId(address appGateway_) internal pure returns (bytes32) { + return bytes32(uint256(uint160(appGateway_))); + } + + function _decodeAppGatewayId(bytes32 appGatewayId_) internal pure returns (address) { + return address(uint160(uint256(appGatewayId_))); + } +} diff --git a/test/DeliveryHelper.t.sol b/test/DeliveryHelper.t.sol index ea346775..eead05aa 100644 --- a/test/DeliveryHelper.t.sol +++ b/test/DeliveryHelper.t.sol @@ -116,23 +116,23 @@ contract DeliveryHelperTest is SetupTest { function connectDeliveryHelper() internal { vm.startPrank(owner); arbConfig.contractFactoryPlug.initSocket( - address(deliveryHelper), + _encodeAppGatewayId(address(deliveryHelper)), address(arbConfig.socket), address(arbConfig.switchboard) ); optConfig.contractFactoryPlug.initSocket( - address(deliveryHelper), + _encodeAppGatewayId(address(deliveryHelper)), address(optConfig.socket), address(optConfig.switchboard) ); arbConfig.feesPlug.initSocket( - address(feesManager), + _encodeAppGatewayId(address(feesManager)), address(arbConfig.socket), address(arbConfig.switchboard) ); optConfig.feesPlug.initSocket( - address(feesManager), + _encodeAppGatewayId(address(feesManager)), address(optConfig.socket), address(optConfig.switchboard) ); @@ -142,25 +142,25 @@ contract DeliveryHelperTest is SetupTest { gateways[0] = AppGatewayConfig({ plug: address(arbConfig.contractFactoryPlug), chainSlug: arbChainSlug, - appGateway: address(deliveryHelper), + appGatewayId: _encodeAppGatewayId(address(deliveryHelper)), switchboard: address(arbConfig.switchboard) }); gateways[1] = AppGatewayConfig({ plug: address(optConfig.contractFactoryPlug), chainSlug: optChainSlug, - appGateway: address(deliveryHelper), + appGatewayId: _encodeAppGatewayId(address(deliveryHelper)), switchboard: address(optConfig.switchboard) }); gateways[2] = AppGatewayConfig({ plug: address(arbConfig.feesPlug), chainSlug: arbChainSlug, - appGateway: address(feesManager), + appGatewayId: _encodeAppGatewayId(address(feesManager)), switchboard: address(arbConfig.switchboard) }); gateways[3] = AppGatewayConfig({ plug: address(optConfig.feesPlug), chainSlug: optChainSlug, - appGateway: address(feesManager), + appGatewayId: _encodeAppGatewayId(address(feesManager)), switchboard: address(optConfig.switchboard) }); @@ -250,7 +250,7 @@ contract DeliveryHelperTest is SetupTest { gateways[i] = AppGatewayConfig({ plug: plug, chainSlug: chainSlug_, - appGateway: address(appGateway_), + appGatewayId: _encodeAppGatewayId(address(appGateway_)), switchboard: address(socketConfig.switchboard) }); } @@ -279,7 +279,11 @@ contract DeliveryHelperTest is SetupTest { function endAuction(uint40 requestCount_) internal { if (auctionEndDelaySeconds == 0) return; - bytes32 timeoutId = _encodeTimeoutId(evmxSlug, address(watcherPrecompile), timeoutIdCounter++); + bytes32 timeoutId = _encodeTimeoutId( + evmxSlug, + address(watcherPrecompile), + timeoutIdCounter++ + ); bytes memory watcherSignature = _createWatcherSignature( address(watcherPrecompile), diff --git a/test/Inbox.t.sol b/test/Inbox.t.sol index 6da8f8dc..d1763190 100644 --- a/test/Inbox.t.sol +++ b/test/Inbox.t.sol @@ -31,7 +31,7 @@ contract TriggerTest is DeliveryHelperTest { // Connect the counter to the gateway and socket counter.initSocket( - address(gateway), + _encodeAppGatewayId(address(gateway)), address(arbConfig.socket), address(arbConfig.switchboard) ); @@ -41,7 +41,7 @@ contract TriggerTest is DeliveryHelperTest { gateways[0] = AppGatewayConfig({ plug: address(counter), chainSlug: arbChainSlug, - appGateway: address(gateway), + appGatewayId: _encodeAppGatewayId(address(gateway)), switchboard: address(arbConfig.switchboard) }); @@ -76,7 +76,7 @@ contract TriggerTest is DeliveryHelperTest { params[0] = TriggerParams({ triggerId: triggerId, chainSlug: arbChainSlug, - appGateway: address(gateway), + appGatewayId: _encodeAppGatewayId(address(gateway)), plug: address(counter), payload: payload, params: bytes32(0) diff --git a/test/SetupTest.t.sol b/test/SetupTest.t.sol index 55a7a7bd..b752a6c7 100644 --- a/test/SetupTest.t.sol +++ b/test/SetupTest.t.sol @@ -303,7 +303,7 @@ contract SetupTest is Test { params_.readAt, params_.payload, params_.target, - params_.appGateway, + _encodeAppGatewayId(params_.appGateway), params_.prevDigestsHash ); bytes32 digest = watcherPrecompile.getDigest(digestParams_); @@ -401,4 +401,12 @@ contract SetupTest is Test { mstore(add(sig, 64), sigS) } } + + function _encodeAppGatewayId(address appGateway_) internal pure returns (bytes32) { + return bytes32(uint256(uint160(appGateway_))); + } + + function _decodeAppGatewayId(bytes32 appGatewayId_) internal pure returns (address) { + return address(uint160(uint256(appGatewayId_))); + } } diff --git a/test/mock/MockSocket.sol b/test/mock/MockSocket.sol index 1776c491..26650c35 100644 --- a/test/mock/MockSocket.sol +++ b/test/mock/MockSocket.sol @@ -16,7 +16,7 @@ import "../../contracts/interfaces/ISocket.sol"; contract MockSocket is ISocket { struct PlugConfig { // address of the sibling plug on the remote chain - address appGateway; + bytes32 appGatewayId; // switchboard instance for the plug connection ISwitchboard switchboard__; } @@ -26,12 +26,12 @@ contract MockSocket is ISocket { function getPlugConfig( address plugAddress_ - ) external view returns (address appGateway, address switchboard__) { + ) external view returns (bytes32 appGatewayId, address switchboard__) { PlugConfig memory _plugConfig = _plugConfigs[plugAddress_]; - return (_plugConfig.appGateway, address(_plugConfig.switchboard__)); + return (_plugConfig.appGatewayId, address(_plugConfig.switchboard__)); } - function connect(address appGateway_, address switchboard_) external override {} + function connect(bytes32 appGatewayId_, address switchboard_) external override {} function registerSwitchboard() external override {} @@ -96,7 +96,7 @@ contract MockSocket is ISocket { ) external returns (bytes32 triggerId) { PlugConfig memory plugConfig = _plugConfigs[msg.sender]; // creates a unique ID for the message - triggerId = _encodeTriggerId(plugConfig.appGateway); + triggerId = _encodeTriggerId(plugConfig.appGatewayId); emit AppGatewayCallRequested(triggerId, chainSlug, msg.sender, overrides, payload); } @@ -167,12 +167,10 @@ contract MockSocket is ISocket { // Packs the local plug, local chain slug, remote chain slug and nonce // triggerCounter++ will take care of call id overflow as well // triggerId(256) = localChainSlug(32) | appGateway_(160) | nonce(64) - function _encodeTriggerId(address appGateway_) internal returns (bytes32) { + function _encodeTriggerId(bytes32 appGatewayId_) internal returns (bytes32) { return bytes32( - (uint256(chainSlug) << 224) | - (uint256(uint160(appGateway_)) << 64) | - triggerCounter++ + (uint256(chainSlug) << 224) | (uint256(appGatewayId_) << 64) | triggerCounter++ ); } } diff --git a/test/mock/MockWatcherPrecompile.sol b/test/mock/MockWatcherPrecompile.sol index 7d886e6c..12c4c720 100644 --- a/test/mock/MockWatcherPrecompile.sol +++ b/test/mock/MockWatcherPrecompile.sol @@ -190,9 +190,9 @@ contract MockWatcherPrecompile { function getPlugConfigs( uint32 chainSlug_, address plug_ - ) public view returns (address, address) { + ) public view returns (bytes32, address) { return ( - _plugConfigs[chainSlug_][plug_].appGateway, + _plugConfigs[chainSlug_][plug_].appGatewayId, _plugConfigs[chainSlug_][plug_].switchboard ); } From b4bd4b04708b8d99baf5f70832a29404a81f9ae4 Mon Sep 17 00:00:00 2001 From: Akash Date: Tue, 15 Apr 2025 15:40:38 +0530 Subject: [PATCH 2/2] fix: triggerId --- contracts/protocol/socket/Socket.sol | 2 +- contracts/protocol/socket/SocketUtils.sol | 6 ++++-- contracts/protocol/watcherPrecompile/WatcherPrecompile.sol | 4 ---- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/contracts/protocol/socket/Socket.sol b/contracts/protocol/socket/Socket.sol index 1506f056..eb8313e7 100644 --- a/contracts/protocol/socket/Socket.sol +++ b/contracts/protocol/socket/Socket.sol @@ -137,7 +137,7 @@ contract Socket is SocketUtils { if (plugConfig.appGatewayId == bytes32(0)) revert PlugDisconnected(); // creates a unique ID for the message - triggerId = _encodeTriggerId(plugConfig.appGatewayId); + triggerId = _encodeTriggerId(); emit AppGatewayCallRequested(triggerId, chainSlug, plug_, overrides_, payload_); } diff --git a/contracts/protocol/socket/SocketUtils.sol b/contracts/protocol/socket/SocketUtils.sol index 0d905a3c..f7d8583d 100644 --- a/contracts/protocol/socket/SocketUtils.sol +++ b/contracts/protocol/socket/SocketUtils.sol @@ -116,10 +116,12 @@ abstract contract SocketUtils is SocketConfig { // Packs the local plug, local chain slug, remote chain slug and nonce // triggerCounter++ will take care of call id overflow as well // triggerId(256) = localChainSlug(32) | appGateway_(160) | nonce(64) - function _encodeTriggerId(bytes32 appGatewayId_) internal returns (bytes32) { + function _encodeTriggerId() internal returns (bytes32) { return bytes32( - (uint256(chainSlug) << 224) | (uint256(appGatewayId_) << 64) | triggerCounter++ + (uint256(chainSlug) << 224) | + (uint256(uint160(address(this))) << 64) | + triggerCounter++ ); } diff --git a/contracts/protocol/watcherPrecompile/WatcherPrecompile.sol b/contracts/protocol/watcherPrecompile/WatcherPrecompile.sol index 7983f415..11def831 100644 --- a/contracts/protocol/watcherPrecompile/WatcherPrecompile.sol +++ b/contracts/protocol/watcherPrecompile/WatcherPrecompile.sol @@ -296,8 +296,4 @@ contract WatcherPrecompile is RequestHandler { function getRequestParams(uint40 requestCount) external view returns (RequestParams memory) { return requestParams[requestCount]; } - - function _decodeAppGateway(bytes32 triggerId_) internal pure returns (address) { - return address(uint160(uint256(triggerId_) >> 64)); - } }