feat(pulse): Gas optimizations #2492
Open
+35
−367
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Two changes here to bring down the gas usage of the basic pulse flow.
I also twiddled some of the struct field orders to bring down the number of storage slots used.
I suggest starting with PulseState to see the essence of the changes. The rest of the stuff is pretty mechanical fallout from there.
Rationale
These two features exist because we were concerned about the reliability of listening to the events on the chain. However, @tejasbadadare and I thought about this a bit and came up with a good solution to ensure we don't miss anything: you track the latest N block hashes and the events in each block, then on new blocks, you can make sure you're building on the same chain tip, or roll back and rebuild the chain as needed. The set of open requests is constructed by playing forward the events (both the create request & fulfill request events) in those blocks.
Given that, we can save a lot of gas by removing information from the chain state and moving it to the events.
How has this been tested?
Gas usage benchmark before this PR (cold storage case):
After this PR:
Hot storage case
Before
After