Refactor gas constants
See original GitHub issueWhat is wrong?
I noticed that our gas constants are a bit messy and should be reorganized.
Let’s take GAS_BALANCE
as an example which is defined as such:
For someone new to the code base this may seem as if retrieving the balance from some account has a fixed gas cost of 20
when in reality it is 400
as specified with the tangerine whistle fork.
How can it be fixed
My gut feeling says that only true constants™ belong into eth.constants
and that all fork specific constants should live in evm.vm.forks.<fork_name>.constants
.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:12 (11 by maintainers)
Top Results From Across the Web
Gas constant - Wikipedia
The gas constant is the constant of proportionality that relates the energy scale in physics to the temperature scale and the scale used...
Read more >Specific gas constant for two gases - Chemistry Stack Exchange
I'm trying to find the specific gas constant of the mixture. Rsp=RM. I thought it might be M=0.1703418+0.03478.
Read more >Universal and Individual Gas Constants
The Individual Gas Constant depends on the particular gas and is related to the molecular weight of the gas. The value is independent...
Read more >"constants" expressions are expressions, not constants. #9232
Constant expressions are left as expressions, not constants. ... Use of constant keccak variables results in extra hashing (and so gas).
Read more >Refactor compound density logic - Theory - Thrive Development ...
Hello everyone, While working on my global compound diffusion model, I realised that the gaseous compounds in the patches (nitrogen/dioxygen/carbon dioxyde) ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
When porting py-evm to Nim, we first used the same gas costs structure of Py-EVM but then refactored to isolate all gas costs and computation in a single file. See https://github.com/status-im/nimbus/pull/49 and the final implementation.
In the end, we currently have on array per fork that maps the “name” to the gas cost from Yellow Paper Appendix G:
@mratsim thanks for chiming in here and sharing your code from the nimbus client 👍 I had written up a big comment right before the internet had an outage (traveling) but I think it’s well aligned with what you wrote. Here it goes:
As promised to @glaksmono in chat, here’s me elaborating on the issue. Let’s look at how the constants are used to day and the things that may be wrong with that.
Let’s consider
GAS_BALANCE
as an example. This constant was introduced to be used as a gas price for the opcode that reads the balance from an account. It was set to20
. We can see it being used here to set the gas price of that opcode for the frontier code.https://github.com/ethereum/py-evm/blob/931e91531389c1a5f26f8f0690d04a7bca65e24a/eth/constants.py#L44
However, retrieving the balance doesn’t actually cost
20
gas anymore. It costs400
as specified in the tangerine whistle fork. However, there was a new constantGAS_BALANCE_EIP150
being introduced for that which lives in the tangerine whistle specific constants.https://github.com/ethereum/py-evm/blob/931e91531389c1a5f26f8f0690d04a7bca65e24a/eth/vm/forks/tangerine_whistle/constants.py#L13
My initial reaction was: Let’s clean up
eth.constants
and move all things that are only used within a specific fork into the constants of that specific fork. But I think there’s a problem with that approach as well.There are more generic constants such as
GAS_VERYLOW
which are used by many different forks. But thenGAS_LOW
which falls into the same category is only used in the frontier fork. That’s more or less random which means, just looking at the usage side doesn’t give us a strong indicator of how to organize these constants.And then there’s another category. A constant such as
GAS_SSET
isn’t used by any fork but insideeth.vm.logic
instead.With that in mind, let’s try something else.
Let’s create a new class
BaseConstants
ineth.vm.forks.core
and define it as such:And then instead of the forks maintaining plain old constants in a separate file, let them have their inherited versions of the
BaseConstants
.Instead of introducing a new, different constant to adjust the gas price of the balance opcode, the fork would just redefine
GAS_BALANCE
with a different value.With that in mind, let’s derive some rules on how to use organize constants under that model.
Every constant that has been used in the very first version (Frontier) already, goes into
BaseConstants
Constants that just redefine existing constants get dropped (e.g.
GAS_BALANCE_EIP150
) and instead, in the inheritedForknameConstants
, the value of the original constant is overwritten.New constants may be introduced in
ForknameConstants
if they describe things that would not make sense inBaseConstants
as they aren’t generic and rather describe fork specific things.The
ForknameConstants
family of classes must never be imported outside ofeth.vm.forks
. E.g.eth.vm.logic
can only importBaseConstants
but notTangerineWhistleConstants
If a function inside
eth.vm.logic
wants to import from a fork specific constant, then that is a clear signal that the function either needs to be moved into a fork specific place (which is what @pipermerriam was suggesting) or that it needs to be refactored to accept the right constants to get injected.@pipermerriam does that feel alright to you?