Skip to content

Commit

Permalink
feat: optimise EVM2EVMMultiOffRamp.executeSingleMessage (#1235)
Browse files Browse the repository at this point in the history
## Motivation
`EVM2EVMMultiOffRamp.executeSingleMessage` is optimised by replacing
memory to calldata, internal functions
`_releaseOrMintSingleTokens` and `_releaseOrMintSingleToken` can be
optimised
```
function executeSingleMessage(Internal.Any2EVMRampMessage memory message, bytes[] memory offchainTokenData) external
```
Tradeoff is increased contract size, which is exceeding the limit and
thus we have to decrease the optimiser runs

## Findings
After replacing memory with call data we have the following findings for
gas and contract size

| Test               | Before  | after | delta |
|
-------------------------------------------------------------------------------------------
| ------ | ------ | ------ |
|
EVM2EVMMultiOffRamp_executeSingleMessage:test_NonContractWithTokens_Success()
| 249368 | 247671 | \-1697 |
| EVM2EVMMultiOffRamp_executeSingleMessage:test_NonContract_Success() |
20672 | 19245 | \-1427 |
|
EVM2EVMMultiOffRamp_executeSingleMessage:test_executeSingleMessage_NoTokens_Success()
| 48381 | 47265 | \-1116 |
|
EVM2EVMMultiOffRamp_executeSingleMessage:test_executeSingleMessage_WithTokens_Success()
| 278146 | 276759 | \-1387 |
|
EVM2EVMMultiOffRamp_executeSingleMessage:test_executeSingleMessage_WithValidation_Success()
| 93615 | 92499 | \-1116 |
|
EVM2EVMMultiOffRamp__releaseOrMintSingleToken:test__releaseOrMintSingleToken_Success()
| 108343 | 107939 | \-404 |

| **Optimser Run** | **Size** | **Margin(kB)** | **With call data
optimisation** |
| ---------------- | --------------------------------- | --------------
| ------------------------------- |
| 2500 | 24.113 | 0.463 | No |
| 2500 | 24.857 (size increase of 0.744kB) | \-0.281 | Yes |
| 2400 | 24.635 | \-0.059 | Yes |
| 2200 | 24.635 | \-0.059 | |
| 2000 | 24.572 | 0.004 | Yes |

---------

Signed-off-by: 0xsuryansh <suryansh.shrivastava@smartcontract.com>
Co-authored-by: app-token-issuer-infra-releng[bot] <120227048+app-token-issuer-infra-releng[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and asoliman92 committed Aug 28, 2024
1 parent ce8d50e commit 54b8aac
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 105 deletions.
184 changes: 92 additions & 92 deletions contracts/gas-snapshots/ccip.gas-snapshot

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion contracts/scripts/native_solc_compile_all_ccip
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ SOLC_VERSION="0.8.24"
OPTIMIZE_RUNS=26000
OPTIMIZE_RUNS_OFFRAMP=18000
OPTIMIZE_RUNS_ONRAMP=4100
OPTIMIZE_RUNS_MULTI_OFFRAMP=2500
OPTIMIZE_RUNS_MULTI_OFFRAMP=2000


SCRIPTPATH="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
Expand Down
17 changes: 10 additions & 7 deletions contracts/src/v0.8/ccip/offRamp/EVM2EVMMultiOffRamp.sol
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,10 @@ contract EVM2EVMMultiOffRamp is ITypeAndVersion, MultiOCR3Base {
/// its execution and enforce atomicity among successful message processing and token transfer.
/// @dev We use ERC-165 to check for the ccipReceive interface to permit sending tokens to contracts
/// (for example smart contract wallets) without an associated message.
function executeSingleMessage(Internal.Any2EVMRampMessage memory message, bytes[] memory offchainTokenData) external {
function executeSingleMessage(
Internal.Any2EVMRampMessage calldata message,
bytes[] calldata offchainTokenData
) external {
if (msg.sender != address(this)) revert CanOnlySelfCall();
Client.EVMTokenAmount[] memory destTokenAmounts = new Client.EVMTokenAmount[](0);
if (message.tokenAmounts.length > 0) {
Expand Down Expand Up @@ -804,11 +807,11 @@ contract EVM2EVMMultiOffRamp is ITypeAndVersion, MultiOCR3Base {
/// @param offchainTokenData Data fetched offchain by the DON.
/// @return destTokenAmount local token address with amount
function _releaseOrMintSingleToken(
Internal.RampTokenAmount memory sourceTokenAmount,
bytes memory originalSender,
Internal.RampTokenAmount calldata sourceTokenAmount,
bytes calldata originalSender,
address receiver,
uint64 sourceChainSelector,
bytes memory offchainTokenData
bytes calldata offchainTokenData
) internal returns (Client.EVMTokenAmount memory destTokenAmount) {
// We need to safely decode the token address from the sourceTokenData, as it could be wrong,
// in which case it doesn't have to be a valid EVM address.
Expand Down Expand Up @@ -883,11 +886,11 @@ contract EVM2EVMMultiOffRamp is ITypeAndVersion, MultiOCR3Base {
/// any non-rate limiting errors that may occur. If we encounter a rate limiting related error
/// we bubble it up. If we encounter a non-rate limiting error we wrap it in a TokenHandlingError.
function _releaseOrMintTokens(
Internal.RampTokenAmount[] memory sourceTokenAmounts,
bytes memory originalSender,
Internal.RampTokenAmount[] calldata sourceTokenAmounts,
bytes calldata originalSender,
address receiver,
uint64 sourceChainSelector,
bytes[] memory offchainTokenData
bytes[] calldata offchainTokenData
) internal returns (Client.EVMTokenAmount[] memory destTokenAmounts) {
destTokenAmounts = new Client.EVMTokenAmount[](sourceTokenAmounts.length);
for (uint256 i = 0; i < sourceTokenAmounts.length; ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ contract EVM2EVMMultiOffRampHelper is EVM2EVMMultiOffRamp, IgnoreContractSize {
}

function releaseOrMintSingleToken(
Internal.RampTokenAmount memory sourceTokenAmount,
Internal.RampTokenAmount calldata sourceTokenAmount,
bytes calldata originalSender,
address receiver,
uint64 sourceChainSelector,
Expand All @@ -39,8 +39,8 @@ contract EVM2EVMMultiOffRampHelper is EVM2EVMMultiOffRamp, IgnoreContractSize {
}

function releaseOrMintTokens(
Internal.RampTokenAmount[] memory sourceTokenAmounts,
bytes memory originalSender,
Internal.RampTokenAmount[] calldata sourceTokenAmounts,
bytes calldata originalSender,
address receiver,
uint64 sourceChainSelector,
bytes[] calldata offchainTokenData
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ccip_reader_tester: ../../../contracts/solc/v0.8.24/CCIPReaderTester/CCIPReaderT
commit_store: ../../../contracts/solc/v0.8.24/CommitStore/CommitStore.abi ../../../contracts/solc/v0.8.24/CommitStore/CommitStore.bin ddc26c10c2a52b59624faae9005827b09b98db4566887a736005e8cc37cf8a51
commit_store_helper: ../../../contracts/solc/v0.8.24/CommitStoreHelper/CommitStoreHelper.abi ../../../contracts/solc/v0.8.24/CommitStoreHelper/CommitStoreHelper.bin ebd8aac686fa28a71d4212bcd25a28f8f640d50dce5e50498b2f6b8534890b69
ether_sender_receiver: ../../../contracts/solc/v0.8.24/EtherSenderReceiver/EtherSenderReceiver.abi ../../../contracts/solc/v0.8.24/EtherSenderReceiver/EtherSenderReceiver.bin 09510a3f773f108a3c231e8d202835c845ded862d071ec54c4f89c12d868b8de
evm_2_evm_multi_offramp: ../../../contracts/solc/v0.8.24/EVM2EVMMultiOffRamp/EVM2EVMMultiOffRamp.abi ../../../contracts/solc/v0.8.24/EVM2EVMMultiOffRamp/EVM2EVMMultiOffRamp.bin cb1c4d1bd8460181f1545524bc2537a58f6839ee0acad6a068f5e24216a9cee9
evm_2_evm_multi_offramp: ../../../contracts/solc/v0.8.24/EVM2EVMMultiOffRamp/EVM2EVMMultiOffRamp.abi ../../../contracts/solc/v0.8.24/EVM2EVMMultiOffRamp/EVM2EVMMultiOffRamp.bin 2b3284f25be1c46ad1c7896fef47d812133e0bdb78cf5e48618c4f3aba5fe3d4
evm_2_evm_multi_onramp: ../../../contracts/solc/v0.8.24/EVM2EVMMultiOnRamp/EVM2EVMMultiOnRamp.abi ../../../contracts/solc/v0.8.24/EVM2EVMMultiOnRamp/EVM2EVMMultiOnRamp.bin 15cd5695049ab4be1f396ec1d7b609738b2bcefa3740a7a48316e1f72506a34a
evm_2_evm_offramp: ../../../contracts/solc/v0.8.24/EVM2EVMOffRamp/EVM2EVMOffRamp.abi ../../../contracts/solc/v0.8.24/EVM2EVMOffRamp/EVM2EVMOffRamp.bin ea2a9622ace941075ea62cd348e9c69c5aa3bf8a7daf298fea51984f80a1d27d
evm_2_evm_onramp: ../../../contracts/solc/v0.8.24/EVM2EVMOnRamp/EVM2EVMOnRamp.abi ../../../contracts/solc/v0.8.24/EVM2EVMOnRamp/EVM2EVMOnRamp.bin bf1a3090a2f8d1632b82a6d45200cf6f3833a1e27303745405c1ed13ffc0cf83
Expand Down

0 comments on commit 54b8aac

Please sign in to comment.