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

Code Jar Improvements with Stub #137

Merged
merged 3 commits into from
Dec 21, 2023
Merged

Conversation

hayesgm
Copy link
Contributor

@hayesgm hayesgm commented Dec 14, 2023

This patch improves Code Jar to use CodeJarStub, which is a stub that's semi-verifiable on Etherscan for deployments, and significantly reduces the complexity/noise of how CodeJar works. This is also the basis for CodeJar deployments, which will be included in a different branch.

@fluffywaffles fluffywaffles self-requested a review December 14, 2023 21:24
Copy link
Collaborator

@fluffywaffles fluffywaffles left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm, some of the gas overhead might be worth looking into.

are we only seeing overhead for tests that use ScriptSource and so do a deploy? 🤔

object "CodeJarStub_9" {
code {
/// @src 0:66:211 "contract CodeJarStub {..."
mstore(64, 128)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's pretty lol, but this is all that ends up executing, I believe. since constructor_CodeJarStub_9 is our code that returns. I believe the optimizer is smart enough to optimize the rest out when generating bytecode.

@kevincheng96
Copy link
Collaborator

kevincheng96 commented Dec 15, 2023

hmmm, some of the gas overhead might be worth looking into.

are we only seeing overhead for tests that use ScriptSource and so do a deploy? 🤔

Looks like the overhead is only affecting tests using ScriptSource. However, we don't meter the gas cost of deploying the scripts. My guess is that the creation code has increased in size by a decent amount, increasing the gas cost of getCodeAddress in saveCode.

Agreed, would be good to confirm where the extra overhead comes from.

assembly {
codeCreateAddress := create2(0, add(initCode, 32), initCodeLen, 0)
}
CodeJarStub codeCreateAddress = new CodeJarStub{salt: 0}(code);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: not technically an address, think a name like createdCode is more fitting

Comment on lines 50 to 49
* @dev Returns the create2 address based on the given initCode
* @return The create2 address based on running the initCode constructor
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outdated comments

@hayesgm hayesgm force-pushed the hayesgm/code-jar-improvements branch 4 times, most recently from 86eeaba to f89eec4 Compare December 19, 2023 02:37
Copy link
Collaborator

@kevincheng96 kevincheng96 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Reminder to remove the yul code and re-generate the gas diffs.

In the meantime, I'll try to investigate why there is a sizable gas overhead for some of the tests.

@kevincheng96
Copy link
Collaborator

In the meantime, I'll try to investigate why there is a sizable gas overhead for some of the tests.

Looks like the gas overhead is coming from the initcode for CodeJarStub being much larger than the raw bytecode initcode we were using prior. From my testing, the initcode size for CodeJarStub is around 300 bytes, while the raw bytecode size is around 10 bytes. This difference in size is not only be increasing the cost of deployment, but also the cost of retrieving the deterministic address of an already deployed contract. The extra gas overhead for already deployed contracts is ~3k gas.

Though, I guess there's nothing we can do other than try to play around with compiler settings to optimize CodeJarStub. This is the price we pay for Etherscan verification.

This patch improves Code Jar to use CodeJarStub, which is a stub that's semi-verifiable on Etherscan for deployments, and significantly reduces the complexity/noise of how CodeJar works. This is also the basis for CodeJar deployments, which will be included in a different branch.
@hayesgm hayesgm force-pushed the hayesgm/code-jar-improvements branch from 0499de8 to ca843bb Compare December 20, 2023 23:54
This patch just hacks in `datasize("CodeJarStub")` as a magic constant (derived emperically) into the contract itself, which allows us to get an optimal version of CodeJarStub that saves ~2-3K gas from using Solidity's ABI-encoding which is particularly expensive mostly due to dynamic allocations that break the optimizer. This is a weird system, but read the comments as to why it's probably an okay decision.
@cwang25 cwang25 merged commit 0209a8d into main Dec 21, 2023
3 of 4 checks passed
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.

4 participants