Skip to content

Commit

Permalink
gas optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
ezynda3 committed Mar 5, 2025
1 parent 915e560 commit fe2691c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
31 changes: 14 additions & 17 deletions src/Facets/ChainflipFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -131,23 +131,6 @@ contract ChainflipFacet is ILiFi, ReentrancyGuard, SwapperV2, Validatable {
_bridgeData.sendingAssetId
);

// Initialize message variable at function scope level
bytes memory message;

// Validate destination call flag matches message presence first
if (_bridgeData.hasDestinationCall) {
if (_chainflipData.dstCallSwapData.length == 0) {
revert InformationMismatch();
}
message = abi.encode(
_bridgeData.transactionId,
_chainflipData.dstCallSwapData,
_bridgeData.receiver
);
} else if (_chainflipData.dstCallSwapData.length > 0) {
revert InformationMismatch();
}

// Handle address encoding based on destination chain type
bytes memory encodedDstAddress;
if (_bridgeData.receiver == LibAsset.NON_EVM_ADDRESS) {
Expand Down Expand Up @@ -185,6 +168,16 @@ contract ChainflipFacet is ILiFi, ReentrancyGuard, SwapperV2, Validatable {

// Handle destination calls
if (_bridgeData.hasDestinationCall) {
if (_chainflipData.dstCallSwapData.length == 0) {
revert InformationMismatch();
}

bytes memory message = abi.encode(
_bridgeData.transactionId,
_chainflipData.dstCallSwapData,
_bridgeData.receiver
);

if (isNativeAsset) {
chainflipVault.xCallNative{ value: _bridgeData.minAmount }(
dstChain,
Expand All @@ -207,6 +200,10 @@ contract ChainflipFacet is ILiFi, ReentrancyGuard, SwapperV2, Validatable {
);
}
} else {
if (_chainflipData.dstCallSwapData.length > 0) {
revert InformationMismatch();
}

if (isNativeAsset) {
chainflipVault.xSwapNative{ value: _bridgeData.minAmount }(
dstChain,
Expand Down
27 changes: 27 additions & 0 deletions test/solidity/Facets/ChainflipFacet.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,33 @@ contract ChainflipFacetTest is TestBaseFacet {
vm.stopPrank();
}

function testBase_Revert_BridgeWithInvalidDestinationCallFlag()
public
override
{
vm.startPrank(USER_SENDER);

// approval
usdc.approve(_facetTestContractAddress, bridgeData.minAmount);

// prepare bridgeData
bridgeData.hasDestinationCall = true;

// In the ChainflipFacet, when hasDestinationCall is true, it uses dstCallReceiver
// which is zero by default, so it will revert with InvalidReceiver before checking
// for the destination call data
//
// NOTE: We override this test from TestBaseFacet because the base test expects
// InformationMismatch error, but our implementation checks for zero receiver address
// first (which throws InvalidReceiver) before checking for empty destination call data.
// The order of validation in _startBridge() means we hit the InvalidReceiver check
// before we would hit the InformationMismatch check.
vm.expectRevert(InvalidReceiver.selector);

initiateBridgeTxWithFacet(false);
vm.stopPrank();
}

function testRevert_WhenDestinationCallFlagMismatchesMessage() public {
// Case 1: hasDestinationCall is true but message is empty
bridgeData.hasDestinationCall = true;
Expand Down

0 comments on commit fe2691c

Please sign in to comment.