-
Notifications
You must be signed in to change notification settings - Fork 20.4k
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
core: implement EIP-7623 increase calldata cost #30946
base: master
Are you sure you want to change the base?
Conversation
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.
LGTM
I was a bit stumped by the changes to the test cases in internal/ethapi/testdata
, but I figured out that these tests use the MergedTestChainConfig
which already enables Prague, so the gas costs do not match anymore after this, which is expected.
Refer to ethereum/go-ethereum#30946. That PR is ongoing so we may need further updates.
75b4157
to
49d2543
Compare
I rebased this into a single commit. |
func IntrinsicGas(data []byte, accessList types.AccessList, authList []types.SetCodeAuthorization, isContractCreation, isHomestead, isEIP2028, isEIP3860 bool) (uint64, error) { | ||
// IntrinsicGas computes the 'intrinsic gas' and the number of tokens for EIP-7623 | ||
// for a message with the given data. | ||
func IntrinsicGas(data []byte, accessList types.AccessList, authList []types.SetCodeAuthorization, isContractCreation, isHomestead, isEIP2028, isEIP3860 bool) (uint64, uint64, error) { |
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 think this is overloading IntrinsicGas(..)
. The intrinsic gas of the tx is the amount paid upfront before execution starts. A large portion of this is the number of zero / non-zero bytes, however, tokens just end up as an unrelated byproduct of the calculation.
Iterating the data byte-by-byte twice is annoying, but I think if that is a perf bottleneck we could just calculate the total number of zero bytes ahead of time and feed that to a new intrinsic gas / data floor gas function that operate on zero bytes and data len instead of entire data field.
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.
So I was a bit confused as here you are talking about calculating the total number of zero bytes ahead of time and passing that in. Whereas later you suggested a different approach.
However I had a look and figured out I can instead of tokens return the floor gas itself from IntrinsicGas
. Given that it is a sort of second type of "intrinsic gas" it seems satisfactory to me. It also made the code generally cleaner (as gas floor computed once).
Please let me know what you think.
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 kinda prefer the way @s1na wrote it as well. It forces the user of IntrinsicGas to think about it, while the diff that you posted will not. This way users will make mistakes (like forgetting to do the calculation in the txpool or in t8n) as they need to have the knowledge that after the IntrinsicGas calculation, the DataGas calculation has to follow
Thoughts on something like this? diff --git a/core/state_transition.go b/core/state_transition.go
index c65739ac30..60fb000b05 100644
--- a/core/state_transition.go
+++ b/core/state_transition.go
@@ -66,14 +66,20 @@ func (result *ExecutionResult) Revert() []byte {
return common.CopyBytes(result.ReturnData)
}
+func ZeroBytes(data []byte) (z uint64) {
+ for _, byt := range data {
+ if byt == 0 {
+ z++
+ }
+ }
+ return
+}
+
// IntrinsicGas computes the 'intrinsic gas' and the number of tokens for EIP-7623
// for a message with the given data.
-func IntrinsicGas(data []byte, accessList types.AccessList, authList []types.SetCodeAuthorization, isContractCreation, isHomestead, isEIP2028, isEIP3860 bool) (uint64, uint64, error) {
+func IntrinsicGas(data []byte, accessList types.AccessList, authList []types.SetCodeAuthorization, isContractCreation, isHomestead, isEIP2028, isEIP3860 bool) (uint64, error) {
// Set the starting gas for the raw transaction
- var (
- gas uint64
- tokens uint64
- )
+ var gas uint64
if isContractCreation && isHomestead {
gas = params.TxGasContractCreation
} else {
@@ -83,14 +89,8 @@ func IntrinsicGas(data []byte, accessList types.AccessList, authList []types.Set
// Bump the required gas by the amount of transactional data
if dataLen > 0 {
// Zero and non-zero bytes are priced differently
- var nz uint64
- for _, byt := range data {
- if byt != 0 {
- nz++
- }
- }
- z := dataLen - nz
- tokens = nz*params.TokenPerNonZeroByte7623 + z
+ z := ZeroBytes(data)
+ nz := dataLen - z
// Make sure we don't exceed uint64 for all data combinations
nonZeroGas := params.TxDataNonZeroGasFrontier
@@ -98,19 +98,19 @@ func IntrinsicGas(data []byte, accessList types.AccessList, authList []types.Set
nonZeroGas = params.TxDataNonZeroGasEIP2028
}
if (math.MaxUint64-gas)/nonZeroGas < nz {
- return 0, tokens, ErrGasUintOverflow
+ return 0, ErrGasUintOverflow
}
gas += nz * nonZeroGas
if (math.MaxUint64-gas)/params.TxDataZeroGas < z {
- return 0, tokens, ErrGasUintOverflow
+ return 0, ErrGasUintOverflow
}
gas += z * params.TxDataZeroGas
if isContractCreation && isEIP3860 {
lenWords := toWordSize(dataLen)
if (math.MaxUint64-gas)/params.InitCodeWordGas < lenWords {
- return 0, tokens, ErrGasUintOverflow
+ return 0, ErrGasUintOverflow
}
gas += lenWords * params.InitCodeWordGas
}
@@ -122,7 +122,7 @@ func IntrinsicGas(data []byte, accessList types.AccessList, authList []types.Set
if authList != nil {
gas += uint64(len(authList)) * params.CallNewAccountGas
}
- return gas, tokens, nil
+ return gas, nil
}
// toWordSize returns the ceiled word size required for init code payment calculation.
@@ -423,22 +423,24 @@ func (st *stateTransition) execute() (*ExecutionResult, error) {
)
// Check clauses 4-5, subtract intrinsic gas if everything is correct
- gas, dataTokens, err := IntrinsicGas(msg.Data, msg.AccessList, msg.SetCodeAuthorizations, contractCreation, rules.IsHomestead, rules.IsIstanbul, rules.IsShanghai)
+ gas, err := IntrinsicGas(msg.Data, msg.AccessList, msg.SetCodeAuthorizations, contractCreation, rules.IsHomestead, rules.IsIstanbul, rules.IsShanghai)
if err != nil {
return nil, err
}
if st.gasRemaining < gas {
return nil, fmt.Errorf("%w: have %d, want %d", ErrIntrinsicGas, st.gasRemaining, gas)
}
- // Gas limit suffices for the floor data cost (EIP-7623)
+ // Ensure gas limit suffices for the floor data cost (EIP-7623)
+ var floorGas uint64
if rules.IsPrague {
- floorGas, err := FloorDataGas(dataTokens)
+ floor, err := DataGas(msg.Data)
if err != nil {
return nil, err
}
if st.gasRemaining < floorGas {
return nil, fmt.Errorf("%w: have %d, want %d", ErrDataFloorGas, st.gasRemaining, floorGas)
}
+ floorGas = floor
}
if t := st.evm.Config.Tracer; t != nil && t.OnGasChange != nil {
t.OnGasChange(st.gasRemaining, st.gasRemaining-gas, tracing.GasChangeTxIntrinsicGas)
@@ -503,9 +505,7 @@ func (st *stateTransition) execute() (*ExecutionResult, error) {
ret, st.gasRemaining, vmerr = st.evm.Call(sender, st.to(), msg.Data, st.gasRemaining, value)
}
if rules.IsPrague {
- // After EIP-7623: Data-heavy transactions pay the floor gas.
- // Overflow error has already been checked and can be ignored here.
- floorGas, _ := FloorDataGas(dataTokens)
+ // Data-heavy transactions pay the floor data gas.
if st.gasUsed() < floorGas {
st.gasRemaining = st.initialGas - floorGas
}
@@ -648,9 +648,14 @@ func (st *stateTransition) blobGasUsed() uint64 {
return uint64(len(st.msg.BlobHashes) * params.BlobTxBlobGasPerBlob)
}
-// FloorDataGas calculates the minimum gas required for a transaction
+// DataGas calculates the minimum gas required for a transaction
// based on its data tokens (EIP-7623).
-func FloorDataGas(tokens uint64) (uint64, error) {
+func DataGas(data []byte) (uint64, error) {
+ var (
+ z = ZeroBytes(data)
+ nz = uint64(len(data)) - z
+ tokens = nz*params.TokenPerNonZeroByte7623 + z
+ )
// Check for overflow
if (math.MaxUint64-params.TxGas)/params.CostFloorPerToken7623 < tokens {
return 0, ErrGasUintOverflow |
Just noting your |
I updated the PR based on ethereum/EIPs#9227. |
Co-authored-by: Marius van der Wijden <[email protected]> Co-authored-by: Sina Mahmoodi <[email protected]> return floor gas instead of tokens apply floor after refunds fix tests fix gas return amount fix tests
4a6dd6e
to
81baa03
Compare
Unrelated to this PR, but we should consider switching to
|
@@ -37,7 +36,7 @@ func (p *precompileContract) Run(input []byte) ([]byte, error) { return nil, nil | |||
|
|||
func TestStateOverrideMovePrecompile(t *testing.T) { | |||
db := state.NewDatabase(triedb.NewDatabase(rawdb.NewMemoryDatabase(), nil), nil) | |||
statedb, err := state.New(types.EmptyRootHash, db) | |||
statedb, err := state.New(common.Hash{}, db) |
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.
Looks like this change should be its standalone PR?
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.
Yeah I don't know why that was there :)
This PR builds on #29040 and updates it to the new version of the spec. I filled the EEST tests and they pass.
Link to spec: https://eips.ethereum.org/EIPS/eip-7623