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

[Do-Not-Merge] Implement calldata cost increase per EIP-7623 #13080

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ type Message interface {

// IntrinsicGas computes the 'intrinsic gas' for a message with the given data.
// TODO: convert the input to a struct
func IntrinsicGas(data []byte, accessList types.AccessList, isContractCreation bool, isHomestead, isEIP2028, isEIP3860 bool, authorizationsLen uint64) (uint64, error) {
func IntrinsicGas(data []byte, accessList types.AccessList, isContractCreation bool, isHomestead, isEIP2028, isEIP3860, isPrague bool, authorizationsLen uint64) (uint64, error) {
// Zero and non-zero bytes are priced differently
dataLen := uint64(len(data))
dataNonZeroLen := uint64(0)
Expand All @@ -118,7 +118,7 @@ func IntrinsicGas(data []byte, accessList types.AccessList, isContractCreation b
}
}

gas, status := txpoolcfg.CalcIntrinsicGas(dataLen, dataNonZeroLen, authorizationsLen, accessList, isContractCreation, isHomestead, isEIP2028, isEIP3860)
gas, status := txpoolcfg.CalcIntrinsicGas(dataLen, dataNonZeroLen, authorizationsLen, accessList, isContractCreation, isHomestead, isEIP2028, isEIP3860, isPrague)
if status != txpoolcfg.Success {
return 0, ErrGasUintOverflow
}
Expand Down Expand Up @@ -460,7 +460,7 @@ func (st *StateTransition) TransitionDb(refunds bool, gasBailout bool) (*evmtype
}

// Check clauses 4-5, subtract intrinsic gas if everything is correct
gas, err := IntrinsicGas(st.data, accessTuples, contractCreation, rules.IsHomestead, rules.IsIstanbul, isEIP3860, uint64(len(auths)))
gas, err := IntrinsicGas(st.data, accessTuples, contractCreation, rules.IsHomestead, rules.IsIstanbul, isEIP3860, rules.IsPrague, uint64(len(auths)))
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions erigon-lib/common/fixedgas/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
TxDataNonZeroGasEIP2028 uint64 = 16 // Per byte of non zero data attached to a transaction after EIP 2028 (part in Istanbul)
TxAccessListAddressGas uint64 = 2400 // Per address specified in EIP 2930 access list
TxAccessListStorageKeyGas uint64 = 1900 // Per storage key specified in EIP 2930 access list
TxTotalCostFloorPerToken uint64 = 10 // Per token of calldata in a transaction, as a minimum the txn must pay (EIP-7623)

MaxCodeSize = 24576 // Maximum bytecode to permit for a contract

Expand Down
2 changes: 1 addition & 1 deletion tests/transaction_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (tt *TransactionTest) Run(chainID *big.Int) error {
if stx, ok := tx.(*types.SetCodeTransaction); ok {
authorizationsLen = uint64(len(stx.GetAuthorizations()))
}
requiredGas, err := core.IntrinsicGas(msg.Data(), msg.AccessList(), msg.To() == nil, rules.IsHomestead, rules.IsIstanbul, rules.IsShanghai, authorizationsLen)
requiredGas, err := core.IntrinsicGas(msg.Data(), msg.AccessList(), msg.To() == nil, rules.IsHomestead, rules.IsIstanbul, rules.IsShanghai, rules.IsPrague, authorizationsLen)
if err != nil {
return nil, nil, 0, err
}
Expand Down
5 changes: 3 additions & 2 deletions txnprovider/txpool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ func (p *TxPool) best(ctx context.Context, n int, txns *TxnsRlp, onTopOf, availa
best := p.pending.best

isShanghai := p.isShanghai() || p.isAgra()
isPrague := p.isPrague()

txns.Resize(uint(min(n, len(best.ms))))
var toRemove []*metaTxn
Expand Down Expand Up @@ -707,7 +708,7 @@ func (p *TxPool) best(ctx context.Context, n int, txns *TxnsRlp, onTopOf, availa
// not an exact science using intrinsic gas but as close as we could hope for at
// this stage
authorizationLen := uint64(len(mt.TxnSlot.Authorizations))
intrinsicGas, _ := txpoolcfg.CalcIntrinsicGas(uint64(mt.TxnSlot.DataLen), uint64(mt.TxnSlot.DataNonZeroLen), authorizationLen, nil, mt.TxnSlot.Creation, true, true, isShanghai)
intrinsicGas, _ := txpoolcfg.CalcIntrinsicGas(uint64(mt.TxnSlot.DataLen), uint64(mt.TxnSlot.DataNonZeroLen), authorizationLen, nil, mt.TxnSlot.Creation, true, true, isShanghai, isPrague)
if intrinsicGas > availableGas {
// we might find another txn with a low enough intrinsic gas to include so carry on
continue
Expand Down Expand Up @@ -882,7 +883,7 @@ func (p *TxPool) validateTx(txn *TxnSlot, isLocal bool, stateCache kvcache.Cache
}
return txpoolcfg.UnderPriced
}
gas, reason := txpoolcfg.CalcIntrinsicGas(uint64(txn.DataLen), uint64(txn.DataNonZeroLen), uint64(authorizationLen), nil, txn.Creation, true, true, isShanghai)
gas, reason := txpoolcfg.CalcIntrinsicGas(uint64(txn.DataLen), uint64(txn.DataNonZeroLen), uint64(authorizationLen), nil, txn.Creation, true, true, isShanghai, p.isPrague())
if txn.Traced {
p.logger.Info(fmt.Sprintf("TX TRACING: validateTx intrinsic gas idHash=%x gas=%d", txn.IDHash, gas))
}
Expand Down
2 changes: 1 addition & 1 deletion txnprovider/txpool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ func TestShanghaiIntrinsicGas(t *testing.T) {

for name, c := range cases {
t.Run(name, func(t *testing.T) {
gas, reason := txpoolcfg.CalcIntrinsicGas(c.dataLen, c.dataNonZeroLen, c.authorizationsLen, nil, c.creation, true, true, c.isShanghai)
gas, reason := txpoolcfg.CalcIntrinsicGas(c.dataLen, c.dataNonZeroLen, c.authorizationsLen, nil, c.creation, true, true, c.isShanghai, false)
if reason != txpoolcfg.Success {
t.Errorf("expected success but got reason %v", reason)
}
Expand Down
18 changes: 17 additions & 1 deletion txnprovider/txpool/txpoolcfg/txpoolcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (r DiscardReason) String() string {

// CalcIntrinsicGas computes the 'intrinsic gas' for a message with the given data.
// TODO: move input data to a struct
func CalcIntrinsicGas(dataLen, dataNonZeroLen, authorizationsLen uint64, accessList types.AccessList, isContractCreation, isHomestead, isEIP2028, isShanghai bool) (uint64, DiscardReason) {
func CalcIntrinsicGas(dataLen, dataNonZeroLen, authorizationsLen uint64, accessList types.AccessList, isContractCreation, isHomestead, isEIP2028, isShanghai, isPrague bool) (uint64, DiscardReason) {
// Set the starting gas for the raw transaction
var gas uint64
if isContractCreation && isHomestead {
Expand Down Expand Up @@ -239,6 +239,22 @@ func CalcIntrinsicGas(dataLen, dataNonZeroLen, authorizationsLen uint64, accessL
return 0, GasUintOverflow
}
}

// EIP-7623
if isPrague {
tokenLen := dataLen + 3*nz
floorCost, overflow := emath.SafeMul(tokenLen, fixedgas.TxTotalCostFloorPerToken)
if overflow {
return 0, GasUintOverflow
}
floorCost, overflow = emath.SafeAdd(floorCost, fixedgas.TxGas)
if overflow {
return 0, GasUintOverflow
}
if floorCost > gas {
gas = floorCost
}
}
}
if accessList != nil {
product, overflow := emath.SafeMul(uint64(len(accessList)), fixedgas.TxAccessListAddressGas)
Expand Down
Loading