Skip to content
Open
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
19 changes: 19 additions & 0 deletions contracts/interfaces/ISocket.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 25 additions & 11 deletions contracts/socket/SocketDst.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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_,
Expand All @@ -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);
}
}

/**
Expand Down
231 changes: 231 additions & 0 deletions test/socket/SocketBatcher.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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_
)
);
}
}
Loading