Skip to content

Commit

Permalink
Add helpers to keep sender adapters sorted in test
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominator008 committed Sep 23, 2023
1 parent 39bf2ad commit 5c1dc22
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 65 deletions.
40 changes: 37 additions & 3 deletions test/Setup.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,10 @@ abstract contract Setup is Test {
vm.selectFork(fork[SRC_CHAIN_ID]);
vm.startPrank(owner);

address[] memory _senderAdapters = new address[](2);
_senderAdapters[0] = contractAddress[SRC_CHAIN_ID][bytes("AXELAR_SENDER_ADAPTER")];
_senderAdapters[1] = contractAddress[SRC_CHAIN_ID][bytes("WORMHOLE_SENDER_ADAPTER")];
address[] memory _senderAdapters = _sortTwoAdapters(
contractAddress[SRC_CHAIN_ID][bytes("AXELAR_SENDER_ADAPTER")],
contractAddress[SRC_CHAIN_ID][bytes("WORMHOLE_SENDER_ADAPTER")]
);

MultiBridgeMessageSender(contractAddress[SRC_CHAIN_ID][bytes("MMA_SENDER")]).addSenderAdapters(_senderAdapters);

Expand Down Expand Up @@ -455,4 +456,37 @@ abstract contract Setup is Test {
}
}
}

// @dev sorts two adapters
function _sortTwoAdapters(address adapterA, address adapterB) internal pure returns (address[] memory adapters) {
adapters = new address[](2);
if (adapterA < adapterB) {
adapters[0] = adapterA;
adapters[1] = adapterB;
} else {
adapters[0] = adapterB;
adapters[1] = adapterA;
}
}

// @dev sorts two adapters and fees
function _sortTwoAdaptersWithFees(address adapterA, address adapterB, uint256 feeA, uint256 feeB)
internal
pure
returns (address[] memory adapters, uint256[] memory fees)
{
adapters = new address[](2);
fees = new uint256[](2);
if (adapterA < adapterB) {
adapters[0] = adapterA;
adapters[1] = adapterB;
fees[0] = feeA;
fees[1] = feeB;
} else {
adapters[0] = adapterB;
adapters[1] = adapterA;
fees[0] = feeB;
fees[1] = feeA;
}
}
}
9 changes: 6 additions & 3 deletions test/integration-tests/GracePeriodExpiry.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ contract GracePeriodExpiryTest is Setup {

/// send cross-chain message using MMA infra
vm.recordLogs();
uint256[] memory fees = new uint256[](2);
(uint256 wormholeFee,) =
IWormholeRelayer(POLYGON_RELAYER).quoteEVMDeliveryPrice(_wormholeChainId(DST_CHAIN_ID), 0, 0);
fees[0] = 0.01 ether;
fees[1] = wormholeFee;
(, uint256[] memory fees) = _sortTwoAdaptersWithFees(
contractAddress[SRC_CHAIN_ID][bytes("AXELAR_SENDER_ADAPTER")],
contractAddress[SRC_CHAIN_ID][bytes("WORMHOLE_SENDER_ADAPTER")],
0.01 ether,
wormholeFee
);
MultiBridgeMessageSender(contractAddress[SRC_CHAIN_ID][bytes("MMA_SENDER")]).remoteCall{value: 2 ether}(
DST_CHAIN_ID,
address(target),
Expand Down
9 changes: 6 additions & 3 deletions test/integration-tests/MultiMessageAggregation.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ contract MultiBridgeMessageAggregationTest is Setup {

/// send cross-chain message using MMA infra
vm.recordLogs();
uint256[] memory fees = new uint256[](2);
(uint256 wormholeFee,) =
IWormholeRelayer(POLYGON_RELAYER).quoteEVMDeliveryPrice(_wormholeChainId(DST_CHAIN_ID), 0, 0);
fees[0] = 0.01 ether;
fees[1] = wormholeFee;
(, uint256[] memory fees) = _sortTwoAdaptersWithFees(
contractAddress[SRC_CHAIN_ID][bytes("AXELAR_SENDER_ADAPTER")],
contractAddress[SRC_CHAIN_ID][bytes("WORMHOLE_SENDER_ADAPTER")],
0.01 ether,
wormholeFee
);
MultiBridgeMessageSender(contractAddress[SRC_CHAIN_ID][bytes("MMA_SENDER")]).remoteCall{value: 2 ether}(
DST_CHAIN_ID,
address(target),
Expand Down
9 changes: 6 additions & 3 deletions test/integration-tests/RemoteAdapterAdd.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,14 @@ contract RemoteAdapterAdd is Setup {

/// send cross-chain message using MMA infra
vm.recordLogs();
uint256[] memory fees = new uint256[](2);
(uint256 wormholeFee,) =
IWormholeRelayer(POLYGON_RELAYER).quoteEVMDeliveryPrice(_wormholeChainId(DST_CHAIN_ID), 0, 0);
fees[0] = 0.01 ether;
fees[1] = wormholeFee;
(, uint256[] memory fees) = _sortTwoAdaptersWithFees(
contractAddress[SRC_CHAIN_ID][bytes("AXELAR_SENDER_ADAPTER")],
contractAddress[SRC_CHAIN_ID][bytes("WORMHOLE_SENDER_ADAPTER")],
0.01 ether,
wormholeFee
);
MultiBridgeMessageSender(contractAddress[SRC_CHAIN_ID][bytes("MMA_SENDER")]).remoteCall{value: 2 ether}(
DST_CHAIN_ID,
address(contractAddress[DST_CHAIN_ID][bytes("MMA_RECEIVER")]),
Expand Down
9 changes: 6 additions & 3 deletions test/integration-tests/RemoteAdapterRemove.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,14 @@ contract RemoteAdapterRemove is Setup {

/// send cross-chain message using MMA infra
vm.recordLogs();
uint256[] memory fees = new uint256[](2);
(uint256 wormholeFee,) =
IWormholeRelayer(POLYGON_RELAYER).quoteEVMDeliveryPrice(_wormholeChainId(DST_CHAIN_ID), 0, 0);
fees[0] = 0.01 ether;
fees[1] = wormholeFee;
(, uint256[] memory fees) = _sortTwoAdaptersWithFees(
contractAddress[SRC_CHAIN_ID][bytes("AXELAR_SENDER_ADAPTER")],
contractAddress[SRC_CHAIN_ID][bytes("WORMHOLE_SENDER_ADAPTER")],
0.01 ether,
wormholeFee
);
MultiBridgeMessageSender(contractAddress[SRC_CHAIN_ID][bytes("MMA_SENDER")]).remoteCall{value: 2 ether}(
DST_CHAIN_ID,
address(contractAddress[DST_CHAIN_ID][bytes("MMA_RECEIVER")]),
Expand Down
9 changes: 6 additions & 3 deletions test/integration-tests/RemoteSetQuorum.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ contract RemoteQuorumUpdate is Setup {

/// send cross-chain message using MMA infra
vm.recordLogs();
uint256[] memory fees = new uint256[](2);
(uint256 wormholeFee,) =
IWormholeRelayer(POLYGON_RELAYER).quoteEVMDeliveryPrice(_wormholeChainId(DST_CHAIN_ID), 0, 0);
fees[1] = wormholeFee;
fees[0] = 0.01 ether;
(, uint256[] memory fees) = _sortTwoAdaptersWithFees(
contractAddress[SRC_CHAIN_ID][bytes("AXELAR_SENDER_ADAPTER")],
contractAddress[SRC_CHAIN_ID][bytes("WORMHOLE_SENDER_ADAPTER")],
0.01 ether,
wormholeFee
);
MultiBridgeMessageSender(contractAddress[SRC_CHAIN_ID][bytes("MMA_SENDER")]).remoteCall{value: 2 ether}(
DST_CHAIN_ID,
address(contractAddress[DST_CHAIN_ID][bytes("MMA_RECEIVER")]),
Expand Down
9 changes: 6 additions & 3 deletions test/integration-tests/RemoteTimelockUpdate.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ contract RemoteTimelockUpdate is Setup {

/// send cross-chain message using MMA infra
vm.recordLogs();
uint256[] memory fees = new uint256[](2);
(uint256 wormholeFee,) =
IWormholeRelayer(POLYGON_RELAYER).quoteEVMDeliveryPrice(_wormholeChainId(DST_CHAIN_ID), 0, 0);
fees[0] = 0.01 ether;
fees[1] = wormholeFee;
(, uint256[] memory fees) = _sortTwoAdaptersWithFees(
contractAddress[SRC_CHAIN_ID][bytes("AXELAR_SENDER_ADAPTER")],
contractAddress[SRC_CHAIN_ID][bytes("WORMHOLE_SENDER_ADAPTER")],
0.01 ether,
wormholeFee
);
MultiBridgeMessageSender(contractAddress[SRC_CHAIN_ID][bytes("MMA_SENDER")]).remoteCall{value: 2 ether}(
POLYGON_CHAIN_ID,
address(contractAddress[POLYGON_CHAIN_ID][bytes("TIMELOCK")]),
Expand Down
9 changes: 6 additions & 3 deletions test/integration-tests/TimelockCheck.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ contract TimelockCheckTest is Setup {

/// send cross-chain message using MMA infra
vm.recordLogs();
uint256[] memory fees = new uint256[](2);
(uint256 wormholeFee,) =
IWormholeRelayer(POLYGON_RELAYER).quoteEVMDeliveryPrice(_wormholeChainId(DST_CHAIN_ID), 0, 0);
fees[0] = 0.01 ether;
fees[1] = wormholeFee;
(, uint256[] memory fees) = _sortTwoAdaptersWithFees(
contractAddress[SRC_CHAIN_ID][bytes("AXELAR_SENDER_ADAPTER")],
contractAddress[SRC_CHAIN_ID][bytes("WORMHOLE_SENDER_ADAPTER")],
0.01 ether,
wormholeFee
);
MultiBridgeMessageSender(contractAddress[SRC_CHAIN_ID][bytes("MMA_SENDER")]).remoteCall{value: 2 ether}(
DST_CHAIN_ID,
address(target),
Expand Down
125 changes: 84 additions & 41 deletions test/unit-tests/MultiBridgeMessageSender.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ contract MultiBridgeMessageSenderTest is Setup {
function test_remote_call() public {
vm.startPrank(caller);

address[] memory senderAdapters = new address[](2);
senderAdapters[0] = axelarAdapterAddr;
senderAdapters[1] = wormholeAdapterAddr;

// Wormhole requires exact fees to be passed in
(uint256 wormholeFee,) = IWormholeRelayer(POLYGON_RELAYER).quoteEVMDeliveryPrice(
_wormholeChainId(DST_CHAIN_ID), 0, senderGAC.getGlobalMsgDeliveryGasLimit()
);
(address[] memory senderAdapters, uint256[] memory fees) =
_sortTwoAdaptersWithFees(axelarAdapterAddr, wormholeAdapterAddr, 0.01 ether, wormholeFee);
bool[] memory adapterSuccess = new bool[](2);
adapterSuccess[0] = true;
adapterSuccess[1] = true;
Expand All @@ -86,14 +88,6 @@ contract MultiBridgeMessageSenderTest is Setup {
});
bytes32 msgId = MessageLibrary.computeMsgId(message);

uint256[] memory fees = new uint256[](2);
// Wormhole requires exact fees to be passed in
(uint256 wormholeFee,) = IWormholeRelayer(POLYGON_RELAYER).quoteEVMDeliveryPrice(
_wormholeChainId(DST_CHAIN_ID), 0, senderGAC.getGlobalMsgDeliveryGasLimit()
);
fees[0] = 0.01 ether;
fees[1] = wormholeFee;

vm.expectEmit(true, true, true, true, address(sender));
emit MultiBridgeMessageSent(
msgId, 1, DST_CHAIN_ID, address(42), bytes("42"), 0, expiration, senderAdapters, adapterSuccess
Expand All @@ -116,12 +110,11 @@ contract MultiBridgeMessageSenderTest is Setup {

uint256 balanceBefore = refundAddress.balance;

uint256[] memory fees = new uint256[](2);
(uint256 wormholeFee,) = IWormholeRelayer(POLYGON_RELAYER).quoteEVMDeliveryPrice(
_wormholeChainId(DST_CHAIN_ID), 0, senderGAC.getGlobalMsgDeliveryGasLimit()
);
fees[0] = 0.01 ether;
fees[1] = wormholeFee;
(, uint256[] memory fees) =
_sortTwoAdaptersWithFees(axelarAdapterAddr, wormholeAdapterAddr, 0.01 ether, wormholeFee);
sender.remoteCall{value: nativeValue}(
DST_CHAIN_ID, address(42), bytes("42"), 0, expiration, refundAddress, fees
);
Expand Down Expand Up @@ -341,9 +334,7 @@ contract MultiBridgeMessageSenderTest is Setup {
fees[1] = 0.01 ether;

// Remove both adapters
address[] memory senderAdapters = new address[](2);
senderAdapters[0] = axelarAdapterAddr;
senderAdapters[1] = wormholeAdapterAddr;
address[] memory senderAdapters = _sortTwoAdapters(axelarAdapterAddr, wormholeAdapterAddr);

sender.removeSenderAdapters(senderAdapters);

Expand All @@ -366,14 +357,77 @@ contract MultiBridgeMessageSenderTest is Setup {
vm.startPrank(caller);

address[] memory senderAdapters = new address[](3);
senderAdapters[0] = failingAdapterAddr;
senderAdapters[1] = axelarAdapterAddr;
senderAdapters[2] = wormholeAdapterAddr;

uint256[] memory fees = new uint256[](3);
bool[] memory adapterSuccess = new bool[](3);
adapterSuccess[0] = false;
adapterSuccess[1] = true;
adapterSuccess[2] = true;
(uint256 wormholeFee,) = IWormholeRelayer(POLYGON_RELAYER).quoteEVMDeliveryPrice(
_wormholeChainId(DST_CHAIN_ID), 0, senderGAC.getGlobalMsgDeliveryGasLimit()
);

if (axelarAdapterAddr < wormholeAdapterAddr) {
if (failingAdapterAddr < axelarAdapterAddr) {
senderAdapters[0] = failingAdapterAddr;
senderAdapters[1] = axelarAdapterAddr;
senderAdapters[2] = wormholeAdapterAddr;
fees[0] = 0.01 ether;
fees[1] = 0.01 ether;
fees[2] = wormholeFee;
adapterSuccess[0] = false;
adapterSuccess[1] = true;
adapterSuccess[2] = true;
} else if (failingAdapterAddr > wormholeAdapterAddr) {
senderAdapters[0] = axelarAdapterAddr;
senderAdapters[1] = wormholeAdapterAddr;
senderAdapters[2] = failingAdapterAddr;
fees[0] = 0.01 ether;
fees[1] = wormholeFee;
fees[2] = 0.01 ether;
adapterSuccess[0] = true;
adapterSuccess[1] = true;
adapterSuccess[2] = false;
} else {
senderAdapters[0] = axelarAdapterAddr;
senderAdapters[1] = failingAdapterAddr;
senderAdapters[2] = wormholeAdapterAddr;
fees[0] = 0.01 ether;
fees[1] = 0.01 ether;
fees[2] = wormholeFee;
adapterSuccess[0] = true;
adapterSuccess[1] = false;
adapterSuccess[2] = true;
}
} else {
if (failingAdapterAddr < wormholeAdapterAddr) {
senderAdapters[0] = failingAdapterAddr;
senderAdapters[1] = wormholeAdapterAddr;
senderAdapters[2] = axelarAdapterAddr;
fees[0] = 0.01 ether;
fees[1] = wormholeFee;
fees[2] = 0.01 ether;
adapterSuccess[0] = false;
adapterSuccess[1] = true;
adapterSuccess[2] = true;
} else if (failingAdapterAddr > axelarAdapterAddr) {
senderAdapters[0] = wormholeAdapterAddr;
senderAdapters[1] = axelarAdapterAddr;
senderAdapters[2] = failingAdapterAddr;
fees[0] = wormholeFee;
fees[1] = 0.01 ether;
fees[2] = 0.01 ether;
adapterSuccess[0] = true;
adapterSuccess[1] = true;
adapterSuccess[2] = false;
} else {
senderAdapters[0] = wormholeAdapterAddr;
senderAdapters[1] = failingAdapterAddr;
senderAdapters[2] = axelarAdapterAddr;
fees[0] = wormholeFee;
fees[1] = 0.01 ether;
fees[2] = 0.01 ether;
adapterSuccess[0] = true;
adapterSuccess[1] = false;
adapterSuccess[2] = true;
}
}

uint256 expiration = EXPIRATION_CONSTANT;

Expand All @@ -388,14 +442,6 @@ contract MultiBridgeMessageSenderTest is Setup {
});
bytes32 msgId = MessageLibrary.computeMsgId(message);

uint256[] memory fees = new uint256[](3);
(uint256 wormholeFee,) = IWormholeRelayer(POLYGON_RELAYER).quoteEVMDeliveryPrice(
_wormholeChainId(DST_CHAIN_ID), 0, senderGAC.getGlobalMsgDeliveryGasLimit()
);
fees[0] = 0.01 ether;
fees[1] = 0.01 ether;
fees[2] = wormholeFee;

vm.expectEmit(true, true, true, true, address(sender));
emit MessageSendFailed(failingAdapterAddr, message);

Expand Down Expand Up @@ -443,19 +489,18 @@ contract MultiBridgeMessageSenderTest is Setup {

sender.addSenderAdapters(adapters);

(address[] memory origAdapters) = _sortTwoAdapters(axelarAdapterAddr, wormholeAdapterAddr);
assertEq(sender.senderAdapters(0), address(42));
assertEq(sender.senderAdapters(1), address(43));
assertEq(sender.senderAdapters(2), axelarAdapterAddr);
assertEq(sender.senderAdapters(3), wormholeAdapterAddr);
assertEq(sender.senderAdapters(2), origAdapters[0]);
assertEq(sender.senderAdapters(3), origAdapters[1]);
}

/// @dev add to empty sender adapters
function test_add_sender_adapters_to_empty() public {
vm.startPrank(owner);

address[] memory removals = new address[](2);
removals[0] = axelarAdapterAddr;
removals[1] = wormholeAdapterAddr;
address[] memory removals = _sortTwoAdapters(axelarAdapterAddr, wormholeAdapterAddr);
sender.removeSenderAdapters(removals);

address[] memory additions = new address[](2);
Expand Down Expand Up @@ -549,9 +594,7 @@ contract MultiBridgeMessageSenderTest is Setup {
function test_remove_sender_adapters() public {
vm.startPrank(owner);

address[] memory adapters = new address[](2);
adapters[0] = axelarAdapterAddr;
adapters[1] = wormholeAdapterAddr;
address[] memory adapters = _sortTwoAdapters(axelarAdapterAddr, wormholeAdapterAddr);

vm.expectEmit(true, true, true, true, address(sender));
emit SenderAdaptersUpdated(adapters, false);
Expand Down

0 comments on commit 5c1dc22

Please sign in to comment.