Skip to content

Commit

Permalink
cleanup: adapter add invariants
Browse files Browse the repository at this point in the history
  • Loading branch information
sujithsomraaj committed Oct 18, 2023
1 parent 9e33842 commit 1fcd91a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
20 changes: 13 additions & 7 deletions test/invariant-tests/AdapterList.Invariant.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import "test/Setup.t.sol";
import "test/contracts-mock/MockUniswapReceiver.sol";

/// handler import
import {SenderAdapterAddHandler} from "test/invariant-tests/handlers/SenderAdapterAdd.handler.sol";
import {AdapterListHandler} from "test/invariant-tests/handlers/AdapterList.handler.sol";

/// @notice invariants for adding adapters to multi bridge sender
/// @notice invariants for maintaining adapter list on `MultiBridgeMessageSender`
contract AdapterListInvariant is Setup {
SenderAdapterAddHandler public handler;
AdapterListHandler public handler;

/// @notice initializes the setup
function setUp() public override {
Expand All @@ -22,32 +22,38 @@ contract AdapterListInvariant is Setup {

/// @dev selects fork and deploy the handlers
vm.selectFork(fork[SRC_CHAIN_ID]);
handler = new SenderAdapterAddHandler(
handler = new AdapterListHandler(
contractAddress[SRC_CHAIN_ID]["GAC"],
contractAddress[SRC_CHAIN_ID]["MMA_SENDER"]
);

/// @dev bind the handler as target for invariant
targetContract(address(handler));
}

/// @notice invariant-1: adding an adapter should always increase the length of the adapter list
/// @notice invariant-2: once a trusted executor is added, its entry should exist in the adapter list
/// @notice invariant-3: the adapter list should never contain duplicates
/// @notice invariant-4: the adapter list should always be in orde
/// @notice invariant-4: the adapter list should always be in order

/// @notice invariant-5: removing an adapter should always decrease the length of the adapter list
/// @notice invariant-6: once a trusted executor is removed, it should not persist in the adapter list
function invariant_test_adapter_additions() public {
MultiBridgeMessageSender targetContract = MultiBridgeMessageSender(contractAddress[SRC_CHAIN_ID]["MMA_SENDER"]);

if (handler.success()) {
uint256 currAdds = handler.currAdds();

address newAddition = targetContract.senderAdapters(currAdds - 1);
/// @dev if this asset passes then invariant-2 holds
assertTrue(newAddition != address(0));

/// @dev if this revert then the final index is currAdds - 1 and the invariant-1 holds
/// @dev if this revert invariant-1 and invariant-5 holds
try targetContract.senderAdapters(currAdds) {
assertFalse(1 == 2);
} catch {}

/// @dev assertions for invariant-3 and invariant-4
/// @dev assertions for invariant-3, invariant-4, invariant-5, invariant-6
if (currAdds > 1) {
for (uint256 i = currAdds; i > 0; i--) {
assertTrue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@ import "forge-std/Test.sol";
import {MessageSenderGAC} from "src/controllers/MessageSenderGAC.sol";
import {MultiBridgeMessageSender} from "src/MultiBridgeMessageSender.sol";

/// @notice handler for testing adding adapters to multi message sender
/// @dev simulates the ideal criteria for a successful adapter addition
contract SenderAdapterAddHandler is Test {
/// @notice handler for testing maintaining adapter list
contract AdapterListHandler is Test {
/// @notice local state
MultiBridgeMessageSender public multiBridgeMessageSender;
MessageSenderGAC public gac;

bool public success;
uint256 public currAdds;

/// @notice modifier to prank the add sender adapter call
/// @notice modifier to prank caller
modifier prank(address _prankster) {
vm.startPrank(_prankster);
_;
Expand All @@ -34,6 +33,7 @@ contract SenderAdapterAddHandler is Test {
/// @notice helper for adding new adapters
function addSenderAdapters(address _newSenderAdapter) external prank(gac.getGlobalOwner()) {
success = false;

vm.assume(_newSenderAdapter != address(0));

address[] memory _additions = new address[](1);
Expand All @@ -44,4 +44,18 @@ contract SenderAdapterAddHandler is Test {
currAdds++;
} catch {}
}

/// @notice helper for removing existing adapters
function removeSenderAdapters() external prank(gac.getGlobalOwner()) {
vm.assume(currAdds > 0);
success = false;

address[] memory _removals = new address[](1);
_removals[0] = multiBridgeMessageSender.senderAdapters(currAdds - 1);

try multiBridgeMessageSender.removeSenderAdapters(_removals) {
success = true;
currAdds--;
} catch {}
}
}

0 comments on commit 1fcd91a

Please sign in to comment.