-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
Clean up and document gas tiers #14772
Conversation
ExtCode, // 700, Extcode | ||
Balance, // 400, Balance |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm removing the ExtCode
and Balance
tiers because they seem obsolete. The instructions using them no longer have a static cost independent of the EVM version so I think they should be using the Special
tier.
Balance, // 400, Balance | ||
Special, // multiparam or otherwise special | ||
Invalid // Invalid. | ||
// NOTE: Tiers should be ordered by cost, since we sometimes perform comparisons between them. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do that in
solidity/libyul/optimiser/Metrics.cpp
Lines 185 to 194 in c78f965
void CodeCost::addInstructionCost(evmasm::Instruction _instruction) | |
{ | |
evmasm::Tier gasPriceTier = evmasm::instructionInfo(_instruction, evmVersionFromDialect(m_dialect)).gasPriceTier; | |
if (gasPriceTier < evmasm::Tier::VeryLow) | |
m_cost -= 1; | |
else if (gasPriceTier < evmasm::Tier::High) | |
m_cost += 1; | |
else | |
m_cost += 49; | |
} |
It's also the only place other than GasMeter::runGas()
where we refer to these tiers directly. All other uses are through GasMeter
so the removal of the obsolete tiers will have no real effect on compiler behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, I didn't have time to dig into this, but I wonder if that logic in Metrics.cpp
is even correct. Is Tier::BlockHash
(formerly called Tier::Ext
) really supposed add 49 to to cost even though BLOCKHASH
costs 20 gas? Should TLOAD
/TSTORE
also get 49? And isn't this also overpricing some instructions that are in Special
not due to dynamic cost but rather becuse their cost is EVM-version dependent?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe, when Tier::Ext
was introduced, the logic was not updated accordingly and it went unnoticed...
I have no idea where the value 49
comes from, but it would make sense that Tier::BlockHash
should get a value of 20
and then the upcoming Tier::WarmAccess
for transient storage opcodes should get 100
.
However, I also noticed that SSTORE
is in Tier::Special
, which would make it underpriced ?
try | ||
{ | ||
// Run optimiser and compile the contract. | ||
compiler->compileContract(_contract, _otherCompilers, cborEncodedMetadata); | ||
} | ||
catch(evmasm::OptimizerException const&) | ||
{ | ||
solAssert(false, "Optimizer exception during compilation"); | ||
} | ||
// Run optimiser and compile the contract. | ||
compiler->compileContract(_contract, _otherCompilers, cborEncodedMetadata); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ran into this when I forgot to update GasMeter::runGas()
and got an ICE without the original message and pointing here rather than at the original assert in GasMeter::runGas()
that failed. Since OptimizerException
inherits from util::Exception
and we have top-level handlers for that, I think it's better to just let it through.
@@ -43,32 +43,34 @@ class KnownState; | |||
|
|||
namespace GasCosts | |||
{ | |||
/// NOTE: The GAS_... constants referenced by comments are defined for each EVM version in the Execution Specs: | |||
/// https://ethereum.github.io/execution-specs/autoapi/ethereum/<evm version>/vm/gas/index.html |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cancun spec is still in a branch: https://github.com/ethereum/execution-specs/blob/forks/cancun/src/ethereum/cancun/vm/gas.py.
Also, the links for earlier versions (e.g. gas docs for shanghai) are broken temporarily. There was a PR merged an hour ago and something must have gone wrong. Fortunately it can also be viewed in the repo. E.g. gas docs for shanghai on master
.
Low, // 5, Fast | ||
Mid, // 8, Mid | ||
High, // 10, Slow | ||
Ext, // 20, Ext |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure where Ext
even comes from. It's for BLOCKHASH
, which in Execution Specs is simply GAS_BLOCK_HASH
, and there's no GAS_EXT
there. From the name I at first assumed it could be for EXTCODEHASH
and EXTCODESIZE
, which in Yellow Paper is in a group called W_extaccount
(along with BALANCE
), but that's not it. And even in the Yellow Paper BLOCKHASH
is in a group of its own.
Since all of this is unnecessarily confusing, I decided to rename this tier to BlockHash
to match the Execution Specs.
443fb95
to
0f5d940
Compare
This pull request is stale because it has been open for 14 days with no activity. |
06495c3
to
1355f52
Compare
1355f52
to
ade8096
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've only superficially looked through this so far: but just to confirm: there is no actual behavioural change involved in any of this here or did I miss something? If so, I'm fine with just merging.
Yeah, this is just a refactor+docs. No non-trivial behavior changes. Like, the removed |
- By catching and asserting it we hide the line number and message. - OptimizerException inherits from util::Exception so just letting it through will have the same effect as asserting.
- They're no longer used because the cost depends on the EVM version.
ade8096
to
a973b4c
Compare
Related to #14739 (in particular #14737 (comment) and my upcoming
MCOPY
PR).I needed to understand how those gas tiers work to add a new one for
MCOPY
. In the end it turned out that I don't even need to do it (tiers only represent the static cost and for the dynamic part I just need to addMCOPY
next to other...COPY
opcodes inGasMeter::estimateMax()
). Still, this was not obvious at first and I had to spend some effort to dig up that info, so I decided to add some comments explaining how to use it and how those tiers relate to constants from Execution Specs and opcode groups from Yellow Paper.The PR also removes some obsolete tiers and an unnecessary exception handler (see review comments).