-
Notifications
You must be signed in to change notification settings - Fork 954
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
optimize
getTransactionHash
by implementing it in assembly (#847)
This pull request includes significant changes to the `Safe` contract and its associated test suite. The changes focus on optimizing the encoding of transaction data and enhancing the test coverage for transaction hash calculations. ### Optimizations in `Safe` contract: * [`contracts/Safe.sol`](diffhunk://#diff-587b494ea631bb6b7adf4fc3e1a2e6a277a385ff16e1163b26e39de24e9483deL414-R467): Rewrote the transaction data encoding logic in assembly to avoid multiple memory allocations, improving gas efficiency. ### Enhancements in test suite: * [`test/core/Safe.Signatures.spec.ts`](diffhunk://#diff-d7bc3771858069f85022d38344b6cb5302146da4bafde1ac18910e3d7bfac43bL49-R56): Enhanced the test case for calculating EIP-712 hash by introducing a loop to generate and test multiple random transactions. [[1]](diffhunk://#diff-d7bc3771858069f85022d38344b6cb5302146da4bafde1ac18910e3d7bfac43bL49-R56) [[2]](diffhunk://#diff-d7bc3771858069f85022d38344b6cb5302146da4bafde1ac18910e3d7bfac43bR72). The previous test case was inefficient as it contained empty safe transaction data. The test would still pass if you forgot to include it in hashing. * I also added a FV rule to verify hash computation correctness. ### Benchmarks #### Before ``` ERC20 - transfer Used 51800n gas for >transfer< ✔ with an EOA (137ms) Used 82980n gas for >transfer< ✔ with a single owner Safe Used 88874n gas for >transfer< ✔ with a single owner and guard Safe Used 90024n gas for >transfer< ✔ with a 2 out of 2 Safe Used 97094n gas for >transfer< ✔ with a 3 out of 3 Safe Used 97094n gas for >transfer< ✔ with a 3 out of 5 Safe ``` #### After ``` ERC20 - transfer Used 51800n gas for >transfer< ✔ with an EOA (71ms) Used 82494n gas for >transfer< ✔ with a single owner Safe Used 88375n gas for >transfer< ✔ with a single owner and guard Safe Used 89547n gas for >transfer< ✔ with a 2 out of 2 Safe Used 96577n gas for >transfer< ✔ with a 3 out of 3 Safe Used 96589n gas for >transfer< ✔ with a 3 out of 5 Safe ``` On average, it saves ~485 gas, not much, but considering this is the hottest path, it should result in significant accumulated savings. (After 44 Safe transactions, a user would save 21k gas - enough for broadcasting a native token transfer) ### Codesize It saves 273 bytes in code size. #### Before SafeL2 22582 bytes (limit is 24576) #### After SafeL2 22309 bytes (limit is 24576)
- Loading branch information
Showing
4 changed files
with
158 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters