@@ -43,18 +43,7 @@ library GasUtils {
4343 /**
4444 * @dev Base cost of the `IExecutor.execute` method.
4545 */
46- uint256 internal constant EXECUTION_BASE_COST = EXECUTION_SELECTOR_OVERHEAD + 46960 + 144 + 22 ;
47-
48- /**
49- * @dev Base cost of the `IGateway.submitMessage` method.
50- */
51- uint256 internal constant SUBMIT_BASE_COST = 24064 - 66 + 133 + 44 ;
52-
53- /**
54- * @dev Extra gas cost that any account `Contract or EOA` must pay when calling `IGateway.submitMessage` method.
55- * This cost is necessary for initialize the account's `nonce` storage slot.
56- */
57- uint256 internal constant FIRST_MESSAGE_EXTRA_COST = 17100 + 6000 ;
46+ uint256 internal constant EXECUTION_BASE_COST = EXECUTION_SELECTOR_OVERHEAD + 46960 + 144 + 22 - 28 + 43 ;
5847
5948 /**
6049 * @dev Solidity's reserved location for the free memory pointer.
@@ -87,7 +76,7 @@ library GasUtils {
8776 * @dev Compute the gas cost of memory expansion.
8877 * @param words number of words, where a word is 32 bytes
8978 */
90- function memoryExpansionGasCost (uint256 words ) internal pure returns (uint256 ) {
79+ function memoryExpansionGasCost (uint256 words ) private pure returns (uint256 ) {
9180 unchecked {
9281 return (words.saturatingMul (words) >> 9 ).saturatingAdd (words.saturatingMul (3 ));
9382 }
@@ -121,67 +110,6 @@ library GasUtils {
121110 }
122111 }
123112
124- /**
125- * @dev Compute the gas cost of the `IGateway.submitMessage` method, without the `GatewayProxy` overhead.
126- * @param messageSize The size of the message in bytes. This is the `gmp.data.length`.
127- */
128- function _submitMessageGasCost (uint16 messageSize ) private pure returns (uint256 gasCost ) {
129- unchecked {
130- gasCost = SUBMIT_BASE_COST;
131-
132- // `countNonZeros` gas cost
133- uint256 words = (messageSize + 31 ) >> 5 ;
134- gasCost += (words * 106 ) + (((words + 254 ) / 255 ) * 214 );
135-
136- // CALLDATACOPY
137- gasCost += words * 3 ;
138-
139- // emit GmpCreated() gas cost (8 gas per byte)
140- gasCost += words << 8 ;
141-
142- // Memory expansion cost
143- words += 17 - 1 + 2 ;
144- gasCost += ((words * words) >> 9 ) + (words * 3 );
145-
146- return gasCost;
147- }
148- }
149-
150- /**
151- * @dev Compute the gas cost of the `IGateway.submitMessage` method including the `GatewayProxy` overhead.
152- * @param messageSize The size of the message in bytes. This is the `gmp.data.length`.
153- */
154- function submitMessageGasCost (uint16 messageSize ) internal pure returns (uint256 gasCost ) {
155- unchecked {
156- // Compute the gas cost of the `IGateway.submitMessage` method.
157- gasCost = _submitMessageGasCost (messageSize);
158-
159- // Convert `gmp.data.length` to `abi.encodeCall(IGateway.submitMessage, (...)).length`.
160- uint256 calldataSize = ((messageSize + 31 ) & 0xffe0 ) + 164 ;
161-
162- // Compute the `GatewayProxy` gas overhead.
163- gasCost += proxyOverheadGasCost (uint16 (calldataSize), 32 );
164-
165- return gasCost;
166- }
167- }
168-
169- /**
170- * @dev Compute the minimal gas needed to execute the `IGateway.submitMessage` method.
171- * @param messageSize The size of the message in bytes. This is the `gmp.data.length`.
172- */
173- function submitMessageGasNeeded (uint16 messageSize ) internal pure returns (uint256 gasNeeded ) {
174- unchecked {
175- // Compute the gas cost of the `IGateway.submitMessage` method.
176- gasNeeded = _submitMessageGasCost (messageSize);
177- // gasNeeded = gasNeeded.saturatingSub(2114);
178- // gasNeeded = _inverseOfAllButOne64th();
179- uint256 calldataSize = ((messageSize + 31 ) & 0xffe0 ) + 164 ;
180- gasNeeded = gasNeeded.saturatingAdd (proxyOverheadGasCost (calldataSize, 32 ));
181- gasNeeded = gasNeeded.saturatingSub (36 );
182- }
183- }
184-
185113 /**
186114 * @dev Estimate the price in wei for send an GMP message.
187115 * @param gasPrice The gas price in UFloat9x56 format.
@@ -213,7 +141,7 @@ library GasUtils {
213141 unchecked {
214142 // add execution cost
215143 uint256 gasCost =
216- computeExecutionRefund (uint16 (BranchlessMath.min (messageSize, type (uint16 ).max)), gasLimit);
144+ executionGasUsed (uint16 (BranchlessMath.min (messageSize, type (uint16 ).max)), gasLimit);
217145 // add base cost
218146 gasCost = gasCost.saturatingAdd (21000 );
219147
@@ -274,28 +202,12 @@ library GasUtils {
274202 }
275203 }
276204
277- /**
278- * @dev Compute the gas needed for the transaction, this is different from the gas used.
279- * `gas needed > gas used` because of EIP-150
280- */
281- function executionGasNeeded (uint256 messageSize , uint256 gasLimit ) internal pure returns (uint256 gasNeeded ) {
282- unchecked {
283- gasNeeded = _executionGasCost (messageSize, gasLimit);
284- gasNeeded = gasNeeded.saturatingAdd (2300 - 184 );
285- gasNeeded = inverseOfAllButOne64th (gasNeeded);
286- messageSize = messageSize.align32 ().saturatingAdd (388 );
287- gasNeeded = gasNeeded.saturatingAdd (proxyOverheadGasCost (messageSize, 64 ));
288- // Remove the proxy final overhead, once the message requires (2300 - 184) extra gas.
289- gasNeeded = gasNeeded.saturatingSub (38 );
290- }
291- }
292-
293205 /**
294206 * @dev Compute the gas that should be refunded to the executor for the execution.
295207 * @param messageSize The size of the message.
296208 * @param gasUsed The gas used by the gmp message.
297209 */
298- function computeExecutionRefund (uint16 messageSize , uint256 gasUsed )
210+ function executionGasUsed (uint16 messageSize , uint256 gasUsed )
299211 internal
300212 pure
301213 returns (uint256 executionCost )
@@ -311,6 +223,17 @@ library GasUtils {
311223 }
312224 }
313225
226+ /**
227+ * @dev Compute the transaction base cost.
228+ */
229+ function txBaseCost () internal pure returns (uint256 ) {
230+ unchecked {
231+ uint256 nonZeros = countNonZerosCalldata (msg .data );
232+ uint256 zeros = msg .data .length - nonZeros;
233+ return 21000 + (nonZeros * 16 ) + (zeros * 4 );
234+ }
235+ }
236+
314237 /**
315238 * @dev Count the number of non-zero bytes in a byte sequence from memory.
316239 * gas cost = 217 + (words * 112) + ((words - 1) * 193)
@@ -398,15 +321,4 @@ library GasUtils {
398321 }
399322 }
400323 }
401-
402- /**
403- * @dev Compute the transaction base cost.
404- */
405- function txBaseCost () internal pure returns (uint256 ) {
406- unchecked {
407- uint256 nonZeros = countNonZerosCalldata (msg .data );
408- uint256 zeros = msg .data .length - nonZeros;
409- return 21000 + (nonZeros * 16 ) + (zeros * 4 );
410- }
411- }
412324}
0 commit comments