diff --git a/contracts/interfaces/ISocket.sol b/contracts/interfaces/ISocket.sol index 57c13185..f4aafab2 100644 --- a/contracts/interfaces/ISocket.sol +++ b/contracts/interfaces/ISocket.sol @@ -65,6 +65,25 @@ interface ISocket { */ event ExecutionSuccess(bytes32 msgId); + /** + * @notice emits when the destination plug reverts during message execution + * @dev the message is still consumed (messageExecuted stays true) so a + * failing plug cannot block the message lifecycle or be retried forever + * @param msgId msg id which failed execution + * @param result revert reason string returned by the plug + */ + event ExecutionFailed(bytes32 msgId, string result); + + /** + * @notice emits when the destination plug reverts during message execution + * with non-string revert data (e.g. a custom error) + * @dev the message is still consumed (messageExecuted stays true) so a + * failing plug cannot block the message lifecycle or be retried forever + * @param msgId msg id which failed execution + * @param result raw revert data returned by the plug + */ + event ExecutionFailedBytes(bytes32 msgId, bytes result); + /** * @notice emits the config set by a plug for a remoteChainSlug * @param plug address of plug on current chain diff --git a/contracts/socket/SocketDst.sol b/contracts/socket/SocketDst.sol index 58517e76..a18e1218 100644 --- a/contracts/socket/SocketDst.sol +++ b/contracts/socket/SocketDst.sol @@ -273,6 +273,12 @@ abstract contract SocketDst is SocketBase { * This function assumes localPlug_ will have code while executing. As the message * execution failure is not blocking the system, it is not necessary to check if * code exists in the given address. + * + * The plug call is wrapped in try/catch so a destination-side revert does not + * roll back the whole execute() call: the message stays consumed + * (messageExecuted was set before verification) and the failure is recorded + * via ExecutionFailed / ExecutionFailedBytes instead of leaving the message + * retryable forever with no terminal state. */ function _execute( address executor_, @@ -282,17 +288,25 @@ abstract contract SocketDst is SocketBase { ISocket.MessageDetails memory messageDetails_ ) internal { // NOTE: external un-trusted call - IPlug(localPlug_).inbound{gas: executionGasLimit_, value: msg.value}( - remoteChainSlug_, - messageDetails_.payload - ); - - executionManager__.updateExecutionFees( - executor_, - uint128(messageDetails_.executionFee), - messageDetails_.msgId - ); - emit ExecutionSuccess(messageDetails_.msgId); + try + IPlug(localPlug_).inbound{ + gas: executionGasLimit_, + value: msg.value + }(remoteChainSlug_, messageDetails_.payload) + { + executionManager__.updateExecutionFees( + executor_, + uint128(messageDetails_.executionFee), + messageDetails_.msgId + ); + emit ExecutionSuccess(messageDetails_.msgId); + } catch Error(string memory reason) { + // catch failing revert() and require() + emit ExecutionFailed(messageDetails_.msgId, reason); + } catch (bytes memory reason) { + // catch failing assert() and custom errors + emit ExecutionFailedBytes(messageDetails_.msgId, reason); + } } /** diff --git a/test/socket/SocketBatcher.t.sol b/test/socket/SocketBatcher.t.sol index a7851a10..6ead1e44 100644 --- a/test/socket/SocketBatcher.t.sol +++ b/test/socket/SocketBatcher.t.sol @@ -63,4 +63,235 @@ contract SocketBatcherTest is Setup { address(_b.configs__[socketConfigIndex].switchboard__) ); } + + /** + * @notice Regression test for #437: one failing destination plug must not + * revert the whole batch. The good message executes, the failing one is + * recorded via ExecutionFailedBytes, and neither is rolled back. + */ + function testExecuteBatchContinuesAfterFailingPlug() external { + uint256 amount = 100; + bytes memory addPayload = abi.encode( + keccak256("OP_ADD"), + amount, + _plugOwner + ); + bytes memory subPayload = abi.encode( + keccak256("OP_SUB"), + 150, + _plugOwner + ); + bytes memory proof = abi.encode(0); + address capacitor = address(_a.configs__[index].capacitor__); + + // sub amount (150) exceeds the counter after the add (100), so the + // destination plug reverts on the second message + uint256 executionFee = _sendAddAndSub(amount, 150); + + bytes32 msgIdGood = _packMessageId( + _a.chainSlug, + address(dstCounter__), + 0 + ); + bytes32 msgIdBad = _packMessageId( + _a.chainSlug, + address(dstCounter__), + 1 + ); + + (bytes32 packetIdGood, bytes32 rootGood) = sealAndPropose( + capacitor, + DEFAULT_BATCH_LENGTH + ); + (bytes32 packetIdBad, bytes32 rootBad) = sealAndPropose( + capacitor, + DEFAULT_BATCH_LENGTH + ); + _attestOnDst( + address(_b.configs__[index].switchboard__), + _b.chainSlug, + packetIdGood, + 0, + rootGood, + _watcherPrivateKey + ); + _attestOnDst( + address(_b.configs__[index].switchboard__), + _b.chainSlug, + packetIdBad, + 0, + rootBad, + _watcherPrivateKey + ); + + SocketBatcher.ExecuteRequest[] + memory requests = new SocketBatcher.ExecuteRequest[](2); + requests[0] = _buildRequest( + packetIdGood, + rootGood, + msgIdGood, + addPayload, + executionFee, + proof + ); + requests[1] = _buildRequest( + packetIdBad, + rootBad, + msgIdBad, + subPayload, + executionFee, + proof + ); + + // good message succeeds, failing one is recorded — in that order + vm.recordLogs(); + batcher__.executeBatch(address(_b.socket__), requests); + _assertBatchEvents(msgIdGood, msgIdBad); + + // good message executed, bad one consumed without rolling it back + assertEq(dstCounter__.counter(), amount); + assertTrue(_b.socket__.messageExecuted(msgIdGood)); + assertTrue(_b.socket__.messageExecuted(msgIdBad)); + + // retry of the failed message is blocked by the replay guard + vm.expectRevert(SocketDst.MessageAlreadyExecuted.selector); + _executePayloadOnDst( + _b, + ExecutePayloadOnDstParams( + packetIdBad, + 0, + msgIdBad, + _minMsgGasLimit, + bytes32(0), + executionFee, + rootBad, + subPayload, + proof + ) + ); + } + + function _assertBatchEvents( + bytes32 msgIdGood_, + bytes32 msgIdBad_ + ) internal { + Vm.Log[] memory logs = vm.getRecordedLogs(); + + bytes32 successTopic = keccak256("ExecutionSuccess(bytes32)"); + bytes32 failedTopic = keccak256( + "ExecutionFailedBytes(bytes32,bytes)" + ); + + bool sawSuccess; + bool sawFailure; + uint256 successIndex; + uint256 failureIndex; + for (uint256 i = 0; i < logs.length; i++) { + if (logs[i].topics[0] == successTopic) { + (bytes32 loggedMsgId) = abi.decode(logs[i].data, (bytes32)); + if (loggedMsgId == msgIdGood_) { + sawSuccess = true; + successIndex = i; + } + } + if (logs[i].topics[0] == failedTopic) { + (bytes32 loggedMsgId, bytes memory reason) = abi.decode( + logs[i].data, + (bytes32, bytes) + ); + if ( + loggedMsgId == msgIdBad_ && + keccak256(reason) == + keccak256( + abi.encodeWithSelector( + Counter.InvalidAmount.selector + ) + ) + ) { + sawFailure = true; + failureIndex = i; + } + } + } + + assertTrue(sawSuccess); + assertTrue(sawFailure); + assertLt(successIndex, failureIndex); + } + + function _sendAddAndSub( + uint256 addAmount_, + uint256 subAmount_ + ) internal returns (uint256 executionFee_) { + (uint256 switchboardFees, uint256 verificationFee) = _a + .configs__[index] + .switchboard__ + .getMinFees(_b.chainSlug); + + uint256 socketFees; + (executionFee_, socketFees) = _a + .executionManager__ + .getExecutionTransmissionMinFees( + _minMsgGasLimit, + 100, + bytes32(0), + _transmissionParams, + _b.chainSlug, + address(_a.transmitManager__) + ); + + uint256 value = switchboardFees + + socketFees + + verificationFee + + executionFee_; + + // executionFees to be recomputed which is totalValue - (socketFees + switchboardFees) + // verificationOverheadFees also should go to Executor, hence we do the additional computation below + executionFee_ = verificationFee + executionFee_; + + hoax(_plugOwner); + srcCounter__.remoteAddOperation{value: value}( + _b.chainSlug, + addAmount_, + _minMsgGasLimit, + bytes32(0), + bytes32(0) + ); + + hoax(_plugOwner); + srcCounter__.remoteSubOperation{value: value}( + _b.chainSlug, + subAmount_, + _minMsgGasLimit, + bytes32(0), + bytes32(0) + ); + } + + function _buildRequest( + bytes32 packetId_, + bytes32 root_, + bytes32 msgId_, + bytes memory payload_, + uint256 executionFee_, + bytes memory proof_ + ) internal view returns (SocketBatcher.ExecuteRequest memory) { + return + SocketBatcher.ExecuteRequest( + ISocket.ExecutionDetails( + packetId_, + 0, + _minMsgGasLimit, + proof_, + _createSignature(root_, _executorPrivateKey) + ), + ISocket.MessageDetails( + msgId_, + executionFee_, + _minMsgGasLimit, + bytes32(0), + payload_ + ) + ); + } } diff --git a/test/socket/SocketDst.t.sol b/test/socket/SocketDst.t.sol index 97cb7109..3ce59547 100644 --- a/test/socket/SocketDst.t.sol +++ b/test/socket/SocketDst.t.sol @@ -453,6 +453,119 @@ contract SocketDstTest is Setup { ); } + /** + * @notice Regression test for #437: a destination plug revert must not + * revert the whole execute() call. The message lands in a failed + * terminal state (messageExecuted stays true, ExecutionFailedBytes + * emitted) and cannot be retried. + */ + function testExecuteMessageWithFailingPlug() external { + uint256 amount = 100; + bytes memory payload = abi.encode( + keccak256("OP_SUB"), + amount, + _plugOwner + ); + bytes memory proof = abi.encode(0); + address capacitor = address(_a.configs__[index].capacitor__); + + uint256 executionFee; + { + (uint256 switchboardFees, uint256 verificationFee) = _a + .configs__[index] + .switchboard__ + .getMinFees(_b.chainSlug); + + uint256 socketFees; + (executionFee, socketFees) = _a + .executionManager__ + .getExecutionTransmissionMinFees( + _minMsgGasLimit, + 100, + bytes32(0), + _transmissionParams, + _b.chainSlug, + address(_a.transmitManager__) + ); + + uint256 value = switchboardFees + + socketFees + + verificationFee + + executionFee; + + // executionFees to be recomputed which is totalValue - (socketFees + switchboardFees) + // verificationOverheadFees also should go to Executor, hence we do the additional computation below + executionFee = verificationFee + executionFee; + + hoax(_plugOwner); + srcCounter__.remoteSubOperation{value: value}( + _b.chainSlug, + amount, + _minMsgGasLimit, + bytes32(0), + bytes32(0) + ); + } + + bytes32 msgId = _packMessageId(_a.chainSlug, address(dstCounter__), 0); + (bytes32 packetId, bytes32 root) = sealAndPropose( + capacitor, + DEFAULT_BATCH_LENGTH + ); + _attestOnDst( + address(_b.configs__[index].switchboard__), + _b.chainSlug, + packetId, + 0, + root, + _watcherPrivateKey + ); + + // dst counter is 0, so the SUB reverts with InvalidAmount() on inbound. + // execute() itself must NOT revert: the failure is recorded instead. + vm.expectEmit(false, false, false, true); + emit ExecutionFailedBytes( + msgId, + abi.encodeWithSelector(Counter.InvalidAmount.selector) + ); + + _executePayloadOnDst( + _b, + ExecutePayloadOnDstParams( + packetId, + 0, + msgId, + _minMsgGasLimit, + bytes32(0), + executionFee, + root, + payload, + proof + ) + ); + + // destination state untouched, message consumed + assertEq(dstCounter__.counter(), 0); + assertTrue(_b.socket__.messageExecuted(msgId)); + + // retry is blocked by the replay guard + vm.expectRevert(SocketDst.MessageAlreadyExecuted.selector); + _executePayloadOnDst( + _b, + ExecutePayloadOnDstParams( + packetId, + 0, + msgId, + _minMsgGasLimit, + bytes32(0), + executionFee, + root, + payload, + proof + ) + ); + } + function testExecuteMessageWithValue() external { uint256 amount = 100; uint256 msgValue = 100;