Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas optimizations when importing events #67

Draft
wants to merge 26 commits into
base: main
Choose a base branch
from
Draft

Conversation

iFrostizz
Copy link
Contributor

@iFrostizz iFrostizz commented Aug 29, 2024

Breakdown of the changes: https://docs.google.com/document/d/12MuoEeECCWr3jNgYAZJrSCXv3tkYoFKO_DLHPCLU2EE/edit?usp=sharing

In order to import an event, it should be proven that this one is part of the final tip.
The first step is to get a block hash from the source chain and check that the provided block hash (which is calculated from the keccak256 hash of the rlp header) equals the (last?) queried one. By the way, if getVerifiedWarpBlockHash(0) really returns the last block hash, that means that it would not be possible to keep up with fast blocks if the relayer didn't sent the transaction that imports the log fast enough (need to investigate).

Then the block header is RLP-decoded (only the top level so it should be pretty cheap) and the receipt root is taken to verify the receipts from the MPT and get the receipt in order to RLP-decode the log.

By only RLP-decoding the needed information (specific log from index), 30k gas are shaved from the importEvent function from the testImportEvent test.

Ran 1 test for test/PriceFeedImporterTests.sol:PriceFeedImporterTest
[PASS] testImportEvent() (gas: 538371)
Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 4.10ms (1.76ms CPU time)
| src/PriceFeedImporter.sol:PriceFeedImporter contract |                 |        |        |        |         |
|------------------------------------------------------|-----------------|--------|--------|--------|---------|
| Deployment Cost                                      | Deployment Size |        |        |        |         |
| 2551523                                              | 11748           |        |        |        |         |
| Function Name                                        | min             | avg    | median | max    | # calls |
| importEvent                                          | 504217          | 504217 | 504217 | 504217 | 1       |
| latestRoundData                                      | 2505            | 2716   | 2716   | 2927   | 2       |
| latestSourceBlockNumber                              | 328             | 328    | 328    | 328    | 1       |
| latestSourceLogIndex                                 | 307             | 307    | 307    | 307    | 1       |
| latestSourceTxIndex                                  | 395             | 395    | 395    | 395    | 1       |
Ran 1 test for test/PriceFeedImporterTests.sol:PriceFeedImporterTest
[PASS] testImportEvent() (gas: 502375)
Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 3.17ms (1.30ms CPU time)
| src/PriceFeedImporter.sol:PriceFeedImporter contract |                 |        |        |        |         |
|------------------------------------------------------|-----------------|--------|--------|--------|---------|
| Deployment Cost                                      | Deployment Size |        |        |        |         |
| 3121595                                              | 14209           |        |        |        |         |
| Function Name                                        | min             | avg    | median | max    | # calls |
| importEvent                                          | 473034          | 473034 | 473034 | 473034 | 1       |
| latestRoundData                                      | 2527            | 2738   | 2738   | 2949   | 2       |
| latestSourceBlockNumber                              | 373             | 373    | 373    | 373    | 1       |
| latestSourceLogIndex                                 | 330             | 330    | 330    | 330    | 1       |
| latestSourceTxIndex                                  | 395             | 395    | 395    | 395    | 1       |

Please find the following flamegraph generated by an experimental branch adding the --flamegraph flag to forge test

flamegraph_PriceFeedImporterTest_testImportEvent

edit:

| src/PriceFeedImporter.sol:PriceFeedImporter contract |                 |        |        |        |         |
|------------------------------------------------------|-----------------|--------|--------|--------|---------|
| Deployment Cost                                      | Deployment Size |        |        |        |         |
| 3092441                                              | 14074           |        |        |        |         |
| Function Name                                        | min             | avg    | median | max    | # calls |
| importEvent                                          | 79349           | 249454 | 249454 | 419560 | 2       |
| latestRoundData                                      | 2527            | 2738   | 2738   | 2949   | 2       |
| latestSourceBlockNumber                              | 373             | 373    | 373    | 373    | 1       |
| latestSourceLogIndex                                 | 330             | 330    | 330    | 330    | 1       |
| latestSourceTxIndex                                  | 395             | 395    | 395    | 395    | 1       |

almost saved 100k from the call to importEvent

@iFrostizz iFrostizz self-assigned this Aug 29, 2024
@iFrostizz iFrostizz changed the base branch from main to add_round_data September 3, 2024 09:15
@iFrostizz iFrostizz changed the base branch from add_round_data to main September 12, 2024 13:32
@iFrostizz
Copy link
Contributor Author

This branch creates a deep copy of solidity-merkle-trees to a new lib2 directory. Before merging this, we should probably add changes upstream to the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant