From 746c0ca2182b62a57c00a646f61d262d09e8153f Mon Sep 17 00:00:00 2001 From: Ameesha Agrawal Date: Wed, 30 Apr 2025 17:19:34 +0530 Subject: [PATCH] feat: memory to calldata --- contracts/interfaces/ISocket.sol | 6 ++-- contracts/interfaces/ISocketBatcher.sol | 2 +- contracts/protocol/socket/Socket.sol | 28 +++++++++++-------- contracts/protocol/socket/SocketBatcher.sol | 2 +- contracts/protocol/socket/SocketUtils.sol | 10 +++---- contracts/protocol/utils/common/Structs.sol | 22 +++++++-------- .../watcherPrecompile/core/WatcherIdUtils.sol | 2 +- test/SetupTest.t.sol | 4 +-- test/apps/Counter.t.sol | 2 +- test/mock/MockSocket.sol | 6 ++-- 10 files changed, 44 insertions(+), 40 deletions(-) diff --git a/contracts/interfaces/ISocket.sol b/contracts/interfaces/ISocket.sol index 58fa8dab..8c5ac2db 100644 --- a/contracts/interfaces/ISocket.sol +++ b/contracts/interfaces/ISocket.sol @@ -52,9 +52,9 @@ interface ISocket { * @notice executes a payload */ function execute( - ExecuteParams memory executeParams_, - TransmissionParams memory transmissionParams_ - ) external payable returns (bool, bool, bytes memory); + ExecuteParams calldata executeParams_, + TransmissionParams calldata transmissionParams_ + ) external payable returns (bool, bytes memory); /** * @notice sets the config specific to the plug diff --git a/contracts/interfaces/ISocketBatcher.sol b/contracts/interfaces/ISocketBatcher.sol index aba5a990..b8cb2270 100644 --- a/contracts/interfaces/ISocketBatcher.sol +++ b/contracts/interfaces/ISocketBatcher.sol @@ -23,5 +23,5 @@ interface ISocketBatcher { bytes calldata proof_, bytes calldata transmitterSignature_, address refundAddress_ - ) external payable returns (bool, bool, bytes memory); + ) external payable returns (bool, bytes memory); } diff --git a/contracts/protocol/socket/Socket.sol b/contracts/protocol/socket/Socket.sol index 5564153b..3c299bb8 100644 --- a/contracts/protocol/socket/Socket.sol +++ b/contracts/protocol/socket/Socket.sol @@ -19,7 +19,7 @@ contract Socket is SocketUtils { mapping(bytes32 => bytes32) public payloadIdToDigest; // @notice buffer to account for gas used by current contract execution - uint256 public constant GAS_LIMIT_BUFFER = 105; + uint256 private constant GAS_LIMIT_BUFFER = 105; //////////////////////////////////////////////////////// ////////////////////// ERRORS ////////////////////////// @@ -69,9 +69,9 @@ contract Socket is SocketUtils { * @notice Executes a payload that has been delivered by transmitters and authenticated by switchboards */ function execute( - ExecuteParams memory executeParams_, - TransmissionParams memory transmissionParams_ - ) external payable returns (bool, bool, bytes memory) { + ExecuteParams calldata executeParams_, + TransmissionParams calldata transmissionParams_ + ) external payable returns (bool, bytes memory) { // check if the deadline has passed if (executeParams_.deadline < block.timestamp) revert DeadlinePassed(); // check if the call type is valid @@ -83,6 +83,7 @@ contract Socket is SocketUtils { if (msg.value < executeParams_.value + transmissionParams_.socketFees) revert InsufficientMsgValue(); + bytes32 payloadId = _createPayloadId(plugConfig.switchboard, executeParams_); // validate the execution status @@ -107,6 +108,7 @@ contract Socket is SocketUtils { // verify the digest _verify(digest, payloadId, plugConfig.switchboard); + return _execute(payloadId, executeParams_, transmissionParams_); } @@ -129,15 +131,16 @@ contract Socket is SocketUtils { */ function _execute( bytes32 payloadId_, - ExecuteParams memory executeParams_, - TransmissionParams memory transmissionParams_ - ) internal returns (bool, bool, bytes memory) { + ExecuteParams calldata executeParams_, + TransmissionParams calldata transmissionParams_ + ) internal returns (bool success, bytes memory returnData) { // check if the gas limit is sufficient // bump by 5% to account for gas used by current contract execution if (gasleft() < (executeParams_.gasLimit * GAS_LIMIT_BUFFER) / 100) revert LowGasLimit(); // NOTE: external un-trusted call - (bool success, bool exceededMaxCopy, bytes memory returnData) = executeParams_ + bool exceededMaxCopy; + (success, exceededMaxCopy, returnData) = executeParams_ .target .tryCall( executeParams_.value, @@ -148,6 +151,7 @@ contract Socket is SocketUtils { if (success) { emit ExecutionSuccess(payloadId_, exceededMaxCopy, returnData); + if (address(socketFeeManager) != address(0)) { socketFeeManager.payAndCheckFees{value: transmissionParams_.socketFees}( executeParams_, @@ -156,20 +160,20 @@ contract Socket is SocketUtils { } } else { payloadExecuted[payloadId_] = ExecutionStatus.Reverted; + address receiver = transmissionParams_.refundAddress == address(0) ? msg.sender : transmissionParams_.refundAddress; SafeTransferLib.forceSafeTransferETH(receiver, msg.value); - emit ExecutionFailed(payloadId_, exceededMaxCopy, returnData); } - - return (success, exceededMaxCopy, returnData); + return (success, returnData); } function _validateExecutionStatus(bytes32 payloadId_) internal { if (payloadExecuted[payloadId_] == ExecutionStatus.Executed) revert PayloadAlreadyExecuted(payloadExecuted[payloadId_]); + payloadExecuted[payloadId_] = ExecutionStatus.Executed; } @@ -180,7 +184,7 @@ contract Socket is SocketUtils { * @notice To trigger to a connected remote chain. Should only be called by a plug. */ function _triggerAppGateway(address plug_) internal returns (bytes32 triggerId) { - PlugConfig storage plugConfig = _plugConfigs[plug_]; + PlugConfig memory plugConfig = _plugConfigs[plug_]; // if no sibling plug is found for the given chain slug, revert // sends the trigger to connected app gateway diff --git a/contracts/protocol/socket/SocketBatcher.sol b/contracts/protocol/socket/SocketBatcher.sol index ad37696f..d996a722 100644 --- a/contracts/protocol/socket/SocketBatcher.sol +++ b/contracts/protocol/socket/SocketBatcher.sol @@ -41,7 +41,7 @@ contract SocketBatcher is ISocketBatcher, Ownable { bytes calldata proof_, bytes calldata transmitterSignature_, address refundAddress_ - ) external payable returns (bool, bool, bytes memory) { + ) external payable returns (bool, bytes memory) { ISwitchboard(switchboard_).attest(digest_, proof_); return socket__.execute{value: msg.value}( diff --git a/contracts/protocol/socket/SocketUtils.sol b/contracts/protocol/socket/SocketUtils.sol index 7000b771..ccd8db5d 100644 --- a/contracts/protocol/socket/SocketUtils.sol +++ b/contracts/protocol/socket/SocketUtils.sol @@ -50,7 +50,7 @@ abstract contract SocketUtils is SocketConfig { address transmitter_, bytes32 payloadId_, bytes32 appGatewayId_, - ExecuteParams memory executeParams_ + ExecuteParams calldata executeParams_ ) internal view returns (bytes32) { return keccak256( @@ -78,7 +78,7 @@ abstract contract SocketUtils is SocketConfig { */ function _createPayloadId( address switchboard_, - ExecuteParams memory executeParams_ + ExecuteParams calldata executeParams_ ) internal view returns (bytes32) { return keccak256( @@ -86,8 +86,8 @@ abstract contract SocketUtils is SocketConfig { executeParams_.requestCount, executeParams_.batchCount, executeParams_.payloadCount, - switchboard_, - chainSlug + chainSlug, + switchboard_ ) ); } @@ -100,7 +100,7 @@ abstract contract SocketUtils is SocketConfig { */ function _recoverSigner( bytes32 digest_, - bytes memory signature_ + bytes calldata signature_ ) internal view returns (address signer) { bytes32 digest = keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", digest_)); // recovered signer is checked for the valid roles later diff --git a/contracts/protocol/utils/common/Structs.sol b/contracts/protocol/utils/common/Structs.sol index 2b9f3838..43fef165 100644 --- a/contracts/protocol/utils/common/Structs.sol +++ b/contracts/protocol/utils/common/Structs.sol @@ -118,8 +118,8 @@ struct ResolvedPromises { // AM struct Bid { - address transmitter; uint256 fee; + address transmitter; bytes extraData; } @@ -233,28 +233,28 @@ struct RequestParams { } struct RequestMetadata { + bool onlyReadRequests; + address consumeFrom; address appGateway; address auctionManager; uint256 maxFees; - Bid winningBid; - bytes onCompleteData; - bool onlyReadRequests; - address consumeFrom; uint256 queryCount; uint256 finalizeCount; + Bid winningBid; + bytes onCompleteData; } struct ExecuteParams { CallType callType; - uint256 deadline; - uint256 gasLimit; - uint256 value; - bytes payload; - address target; uint40 requestCount; uint40 batchCount; uint40 payloadCount; + uint256 deadline; + uint256 gasLimit; + uint256 value; bytes32 prevDigestsHash; + address target; + bytes payload; bytes extraData; } @@ -269,6 +269,6 @@ struct PayloadIdParams { uint40 requestCount; uint40 batchCount; uint40 payloadCount; - address switchboard; uint32 chainSlug; + address switchboard; } diff --git a/contracts/protocol/watcherPrecompile/core/WatcherIdUtils.sol b/contracts/protocol/watcherPrecompile/core/WatcherIdUtils.sol index 5121b3da..1d2ed762 100644 --- a/contracts/protocol/watcherPrecompile/core/WatcherIdUtils.sol +++ b/contracts/protocol/watcherPrecompile/core/WatcherIdUtils.sol @@ -26,7 +26,7 @@ library WatcherIdUtils { ) internal pure returns (bytes32) { return keccak256( - abi.encode(requestCount_, batchCount_, payloadCount_, switchboard_, chainSlug_) + abi.encode(requestCount_, batchCount_, payloadCount_, chainSlug_, switchboard_) ); } } diff --git a/test/SetupTest.t.sol b/test/SetupTest.t.sol index fbd05066..4735ab79 100644 --- a/test/SetupTest.t.sol +++ b/test/SetupTest.t.sol @@ -256,7 +256,7 @@ contract SetupTest is Test { isLastPayload ); } else { - (, , bytes memory returnData) = _uploadProofAndExecute(payloadParams); + (, bytes memory returnData) = _uploadProofAndExecute(payloadParams); _resolveAndExpectFinalizeRequested( payloadParams.payloadId, payloadParams, @@ -270,7 +270,7 @@ contract SetupTest is Test { function _uploadProofAndExecute( PayloadParams memory payloadParams - ) internal returns (bool, bool, bytes memory) { + ) internal returns (bool, bytes memory) { (bytes memory watcherProof, bytes32 digest) = _generateWatcherProof(payloadParams); _writeProof(payloadParams.payloadId, watcherProof); diff --git a/test/apps/Counter.t.sol b/test/apps/Counter.t.sol index b2ec64ad..f2c58c2c 100644 --- a/test/apps/Counter.t.sol +++ b/test/apps/Counter.t.sol @@ -34,7 +34,7 @@ contract CounterTest is DeliveryHelperTest { requestCount = _deploy(chainSlug, counterGateway, contractIds); } - function testCounterDeployment1() external { + function testCounterDeployment() external { deploySetup(); deployCounterApp(arbChainSlug); diff --git a/test/mock/MockSocket.sol b/test/mock/MockSocket.sol index b4e0f483..d74d1da1 100644 --- a/test/mock/MockSocket.sol +++ b/test/mock/MockSocket.sol @@ -107,9 +107,9 @@ contract MockSocket is ISocket { * @notice Executes a payload that has been delivered by transmitters and authenticated by switchboards */ function execute( - ExecuteParams memory executeParams_, - TransmissionParams memory transmissionParams_ - ) external payable override returns (bool, bool, bytes memory) { + ExecuteParams calldata executeParams_, + TransmissionParams calldata transmissionParams_ + ) external payable override returns (bool, bytes memory) { // execute payload // return // _execute(