Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit 467c3a9

Browse files
authored
Approximate gas calc. (#57)
1 parent d9e425d commit 467c3a9

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

test/GasUtils.t.sol

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {GasSpender} from "./GasSpender.sol";
1111
import {Gateway} from "../src/Gateway.sol";
1212
import {GasUtils} from "../src/GasUtils.sol";
1313
import {IGmpReceiver} from "../src/interfaces/IGmpReceiver.sol";
14-
import {GmpMessage, Signature, TssKey, GmpStatus, PrimitiveUtils, Batch} from "../src/Primitives.sol";
14+
import {GmpMessage, Signature, TssKey, GmpStatus, PrimitiveUtils, Batch, MAX_PAYLOAD_SIZE} from "../src/Primitives.sol";
1515

1616
uint256 constant secret = 0x42;
1717
uint256 constant nonce = 0x69;
@@ -49,7 +49,7 @@ contract GasUtilsTest is Test {
4949
/**
5050
* @dev Compare the estimated gas cost VS the actual gas cost of the `execute` method.
5151
*/
52-
function test_baseExecutionCost(uint16 messageSize, uint16 gasLimit) external {
52+
function test_gas_calc_and_refund(uint16 messageSize, uint16 gasLimit) external {
5353
vm.txGasPrice(1);
5454
vm.assume(gasLimit >= 5000);
5555
vm.assume(messageSize <= (0x6000 - 32));
@@ -99,4 +99,14 @@ contract GasUtilsTest is Test {
9999
console.log("proxyOverheadGas", proxyOverheadGas);
100100
assertEq(balanceAfter - balanceBefore - baseGas - mGasUsed, 0, "Balance should not change");
101101
}
102+
103+
function test_lin_approx(uint16 messageSize) external pure {
104+
vm.assume(messageSize <= MAX_PAYLOAD_SIZE);
105+
uint256 calcGas = TestUtils.calcGas(messageSize);
106+
uint256 approxGas = TestUtils.linApproxGas(messageSize);
107+
assertGe(approxGas + 650, calcGas);
108+
int256 error = int256(approxGas) - int256(calcGas);
109+
uint256 absError = error >= 0 ? uint256(error) : uint256(-error);
110+
assertLe(absError, 750);
111+
}
102112
}

test/TestUtils.sol

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import {
1818
Batch,
1919
GatewayOp,
2020
Command,
21-
GMP_VERSION
21+
GMP_VERSION,
22+
MAX_PAYLOAD_SIZE
2223
} from "../src/Primitives.sol";
2324
import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
2425

@@ -110,8 +111,8 @@ library TestUtils {
110111
gateway: gateway.toSender(),
111112
relativeGasPriceNumerator: 1,
112113
relativeGasPriceDenominator: 1,
113-
gasCoef0: 0,
114-
gasCoef1: 1
114+
gasCoef0: gasCoef0(),
115+
gasCoef1: gasCoef1()
115116
})
116117
);
117118
vm.stopPrank();
@@ -148,14 +149,6 @@ library TestUtils {
148149
return TestUtils.sign(shard, hash, nonce);
149150
}
150151

151-
function countNonZeros(bytes memory data) internal pure returns (uint256 count) {
152-
for (uint256 i = 0; i < data.length; i++) {
153-
if (data[i] != 0x0) {
154-
count += 1;
155-
}
156-
}
157-
}
158-
159152
function estimateBaseGas(uint256 messageSize) internal pure returns (uint256) {
160153
uint256 calldataSize = messageSize.align32() + 676; // selector + Signature + Batch
161154
return 21000 + calldataSize * 16; // assume every byte is a 1
@@ -188,4 +181,20 @@ library TestUtils {
188181
return gas;
189182
}
190183
}
184+
185+
function calcGas(uint16 messageSize) internal pure returns (uint256) {
186+
return estimateGas(messageSize, 0) + estimateBaseGas(uint256(messageSize));
187+
}
188+
189+
function gasCoef0() internal pure returns (uint256) {
190+
return calcGas(0);
191+
}
192+
193+
function gasCoef1() internal pure returns (uint256) {
194+
return (calcGas(uint16(MAX_PAYLOAD_SIZE)) - calcGas(0)) / MAX_PAYLOAD_SIZE;
195+
}
196+
197+
function linApproxGas(uint16 messageSize) internal pure returns (uint256) {
198+
return gasCoef1() * messageSize + gasCoef0();
199+
}
191200
}

0 commit comments

Comments
 (0)