refactor: Move contract code-checking logic to SwapExecutionDispatcher

I was inspired to do this because, when disabling the slither check for the delegatecall when calling the swap executor, I realized it's not clear from the same contract that we have already checked for contract code existence when setting the executor. This made me feel uneasy, as this contract can then not stand alone and must rely on the higher level contract to safely check for code existence, otherwise the delegatecall is unsafe. Keeping this logic in a separate contract seems error-prone to me, as we may remove the check for code existence without immediately realizing the implications of doing so.

For this reason I have organized it as follows:
- Logic/tests relating to proper roles/access control in the main TychoRouter.
- Lower-level logic/tests that checks contract validity before setting the executor in the SwapExecutionDispatcher
This commit is contained in:
TAMARA LIPOWSKI
2025-01-23 15:40:24 -05:00
parent b616e11354
commit fb9f340cb7
6 changed files with 86 additions and 59 deletions

View File

@@ -4,6 +4,7 @@ pragma solidity ^0.8.28;
import "@interfaces/ISwapExecutor.sol";
error SwapExecutionDispatcher__UnapprovedExecutor();
error SwapExecutionDispatcher__NonContractExecutor();
/**
* @title SwapExecutionDispatcher - Dispatch swap execution to external contracts
@@ -20,6 +21,29 @@ error SwapExecutionDispatcher__UnapprovedExecutor();
contract SwapExecutionDispatcher {
mapping(address => bool) public swapExecutors;
event ExecutorSet(address indexed executor);
/**
* @dev Adds or replace an approved swap executor contract address if it is a
* contract.
* @param target address of the swap executor contract
*/
function _setSwapExecutor(address target) internal {
if (target.code.length == 0) {
revert SwapExecutionDispatcher__NonContractExecutor();
}
swapExecutors[target] = true;
emit ExecutorSet(target);
}
/**
* @dev Remove an approved swap executor contract address
* @param target address of the swap executor contract
*/
function _removeSwapExecutor(address target) internal {
delete swapExecutors[target];
}
/**
* @dev Calls an executor, assumes swap.protocolData contains
* token addresses if required by the executor.

View File

@@ -10,7 +10,6 @@ import "./CallbackVerificationDispatcher.sol";
error TychoRouter__WithdrawalFailed();
error TychoRouter__AddressZero();
error TychoRouter__NonContractExecutor();
error TychoRouter__NonContractVerifier();
contract TychoRouter is
@@ -45,7 +44,6 @@ contract TychoRouter is
address indexed oldFeeReceiver, address indexed newFeeReceiver
);
event FeeSet(uint256 indexed oldFee, uint256 indexed newFee);
event ExecutorSet(address indexed executor);
event CallbackVerifierSet(address indexed callbackVerifier);
constructor(address _permit2) {
@@ -95,26 +93,24 @@ contract TychoRouter is
/**
* @dev Entrypoint to add or replace an approved swap executor contract address
* @param target address of the swap method contract
* @param target address of the swap executor contract
*/
function setSwapExecutor(address target)
external
onlyRole(EXECUTOR_SETTER_ROLE)
{
if (target.code.length == 0) revert TychoRouter__NonContractExecutor();
swapExecutors[target] = true;
emit ExecutorSet(target);
_setSwapExecutor(target);
}
/**
* @dev Entrypoint to remove an approved swap executor contract address
* @param target address of the swap method contract
* @param target address of the swap executor contract
*/
function removeSwapExecutor(address target)
external
onlyRole(EXECUTOR_SETTER_ROLE)
{
delete swapExecutors[target];
_removeSwapExecutor(target);
}
/**