Skip to content

Commit

Permalink
Implement calldata cost increase per EIP-7623
Browse files Browse the repository at this point in the history
  • Loading branch information
somnathb1 committed Dec 11, 2024
1 parent f22317e commit dde8198
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
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
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.isCancun()

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

0 comments on commit dde8198

Please sign in to comment.