Skip to content

Commit

Permalink
Fix incorrect intrinsic gas calculation (#13632)
Browse files Browse the repository at this point in the history
  • Loading branch information
shohamc1 authored Jan 31, 2025
1 parent 4563dbb commit 5e5c1a1
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
5 changes: 3 additions & 2 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import (
"fmt"
"slices"

"github.com/erigontech/erigon-lib/log/v3"
"github.com/holiman/uint256"

"github.com/erigontech/erigon-lib/log/v3"

libcommon "github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon-lib/common/fixedgas"
"github.com/erigontech/erigon-lib/txpool/txpoolcfg"
Expand Down Expand Up @@ -113,7 +114,7 @@ func IntrinsicGas(data []byte, accessList types2.AccessList, isContractCreation
}
}

gas, floorGas7623, status := txpoolcfg.CalcIntrinsicGas(dataLen, dataNonZeroLen, authorizationsLen, accessList, isContractCreation, isHomestead, isEIP2028, isEIP3860, isPrague)
gas, floorGas7623, status := txpoolcfg.CalcIntrinsicGas(dataLen, dataNonZeroLen, authorizationsLen, uint64(len(accessList)), uint64(accessList.StorageKeys()), isContractCreation, isHomestead, isEIP2028, isEIP3860, isPrague)
if status != txpoolcfg.Success {
return 0, 0, ErrGasUintOverflow
}
Expand Down
9 changes: 5 additions & 4 deletions erigon-lib/txpool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ import (

gokzg4844 "github.com/crate-crypto/go-kzg-4844"
mapset "github.com/deckarep/golang-set/v2"
"github.com/erigontech/erigon-lib/common/hexutility"
"github.com/erigontech/erigon-lib/log/v3"
"github.com/go-stack/stack"
"github.com/google/btree"
"github.com/hashicorp/golang-lru/v2/simplelru"
"github.com/holiman/uint256"

"github.com/erigontech/erigon-lib/common/hexutility"
"github.com/erigontech/erigon-lib/log/v3"

"github.com/erigontech/erigon-lib/chain"
"github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon-lib/common/assert"
Expand Down Expand Up @@ -777,7 +778,7 @@ func (p *TxPool) best(n uint16, txs *types.TxsRlp, tx kv.Tx, onTopOf, availableG
// not an exact science using intrinsic gas but as close as we could hope for at
// this stage
authorizationLen := uint64(len(mt.Tx.Authorizations))
intrinsicGas, floorGas, _ := txpoolcfg.CalcIntrinsicGas(uint64(mt.Tx.DataLen), uint64(mt.Tx.DataNonZeroLen), authorizationLen, nil, mt.Tx.Creation, true, true, isShanghai, isPrague)
intrinsicGas, floorGas, _ := txpoolcfg.CalcIntrinsicGas(uint64(mt.Tx.DataLen), uint64(mt.Tx.DataNonZeroLen), authorizationLen, uint64(mt.Tx.AlAddrCount), uint64(mt.Tx.AlStorCount), mt.Tx.Creation, true, true, isShanghai, isPrague)
if isPrague && floorGas > intrinsicGas {
intrinsicGas = floorGas
}
Expand Down Expand Up @@ -924,7 +925,7 @@ func (p *TxPool) validateTx(txn *types.TxSlot, isLocal bool, stateCache kvcache.
}
return txpoolcfg.UnderPriced
}
gas, floorGas, reason := txpoolcfg.CalcIntrinsicGas(uint64(txn.DataLen), uint64(txn.DataNonZeroLen), uint64(authorizationLen), nil, txn.Creation, true, true, isShanghai, p.isPrague())
gas, floorGas, reason := txpoolcfg.CalcIntrinsicGas(uint64(txn.DataLen), uint64(txn.DataNonZeroLen), uint64(authorizationLen), uint64(txn.AlAddrCount), uint64(txn.AlStorCount), txn.Creation, true, true, isShanghai, p.isPrague())
if p.isPrague() && floorGas > gas {
gas = floorGas
}
Expand Down
3 changes: 1 addition & 2 deletions erigon-lib/txpool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package txpool
import (
"bytes"
"context"

// "crypto/rand"
"fmt"
"math"
Expand Down Expand Up @@ -635,7 +634,7 @@ func TestShanghaiIntrinsicGas(t *testing.T) {
for name, c := range cases {
t.Run(name, func(t *testing.T) {
// Todo (@somnathb1) - Factor in EIP-7623
gas, _, reason := txpoolcfg.CalcIntrinsicGas(c.dataLen, c.dataNonZeroLen, c.authorizationsLen, nil, c.creation, true, true, c.isShanghai, false)
gas, _, reason := txpoolcfg.CalcIntrinsicGas(c.dataLen, c.dataNonZeroLen, c.authorizationsLen, 0, 0, c.creation, true, true, c.isShanghai, false)
if reason != txpoolcfg.Success {
t.Errorf("expected success but got reason %v", reason)
}
Expand Down
9 changes: 4 additions & 5 deletions erigon-lib/txpool/txpoolcfg/txpoolcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon-lib/common/fixedgas"
emath "github.com/erigontech/erigon-lib/common/math"
"github.com/erigontech/erigon-lib/types"
)

// BorDefaultTxPoolPriceLimit defines the minimum gas price limit for bor to enforce txs acceptance into the pool.
Expand Down Expand Up @@ -188,7 +187,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, isPrague bool) (gas uint64, floorGas7623 uint64, d DiscardReason) {
func CalcIntrinsicGas(dataLen, dataNonZeroLen, authorizationsLen, accessListLen, storageKeysLen uint64, isContractCreation, isHomestead, isEIP2028, isShanghai, isPrague bool) (gas uint64, floorGas7623 uint64, d DiscardReason) {
// Set the starting gas for the raw transaction
if isContractCreation && isHomestead {
gas = fixedgas.TxGasContractCreation
Expand Down Expand Up @@ -251,8 +250,8 @@ func CalcIntrinsicGas(dataLen, dataNonZeroLen, authorizationsLen uint64, accessL
}
}
}
if accessList != nil {
product, overflow := emath.SafeMul(uint64(len(accessList)), fixedgas.TxAccessListAddressGas)
if accessListLen > 0 {
product, overflow := emath.SafeMul(accessListLen, fixedgas.TxAccessListAddressGas)
if overflow {
return 0, 0, GasUintOverflow
}
Expand All @@ -261,7 +260,7 @@ func CalcIntrinsicGas(dataLen, dataNonZeroLen, authorizationsLen uint64, accessL
return 0, 0, GasUintOverflow
}

product, overflow = emath.SafeMul(uint64(accessList.StorageKeys()), fixedgas.TxAccessListStorageKeyGas)
product, overflow = emath.SafeMul(storageKeysLen, fixedgas.TxAccessListStorageKeyGas)
if overflow {
return 0, 0, GasUintOverflow
}
Expand Down
5 changes: 3 additions & 2 deletions erigon-lib/txpool/txpoolcfg/txpoolcfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ package txpoolcfg
import (
"testing"

"github.com/erigontech/erigon-lib/common/fixedgas"
"github.com/stretchr/testify/assert"

"github.com/erigontech/erigon-lib/common/fixedgas"
)

func TestZeroDataIntrinsicGas(t *testing.T) {
assert := assert.New(t)
gas, floorGas7623, discardReason := CalcIntrinsicGas(0, 0, 0, nil, false, true, true, true, true)
gas, floorGas7623, discardReason := CalcIntrinsicGas(0, 0, 0, 0, 0, false, true, true, true, true)
assert.Equal(discardReason, Success)
assert.Equal(gas, fixedgas.TxGas)
assert.Equal(floorGas7623, fixedgas.TxGas)
Expand Down

0 comments on commit 5e5c1a1

Please sign in to comment.