Skip to content

Commit

Permalink
Detect fork state based on the current block number #1772
Browse files Browse the repository at this point in the history
  • Loading branch information
begmaroman authored Aug 2, 2023
1 parent 2b9430c commit 992d6b9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func NewServer(config *Config) (*Server, error) {
// start transaction pool
m.txpool, err = txpool.NewTxPool(
logger,
m.chain.Params.Forks.At(0),
m.chain.Params.Forks,
hub,
m.grpcServer,
m.network,
Expand Down
21 changes: 13 additions & 8 deletions txpool/txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ type promoteRequest struct {
type TxPool struct {
logger hclog.Logger
signer signer
forks chain.ForksInTime
forks *chain.Forks
store store

// map of all accounts registered by the pool
Expand Down Expand Up @@ -199,7 +199,7 @@ type TxPool struct {
// NewTxPool returns a new pool for processing incoming transactions.
func NewTxPool(
logger hclog.Logger,
forks chain.ForksInTime,
forks *chain.Forks,
store store,
grpcServer *grpc.Server,
network *network.Server,
Expand Down Expand Up @@ -592,23 +592,28 @@ func (p *TxPool) validateTx(tx *types.Transaction) error {
tx.From = from
}

// Grab current block number
currentHeader := p.store.Header()
currentBlockNumber := currentHeader.Number

// Get forks state for the current block
forks := p.forks.At(currentBlockNumber)

// Check if transaction can deploy smart contract
if tx.IsContractCreation() && p.forks.EIP158 && len(tx.Input) > state.TxPoolMaxInitCodeSize {
if tx.IsContractCreation() && forks.EIP158 && len(tx.Input) > state.TxPoolMaxInitCodeSize {
metrics.IncrCounter([]string{txPoolMetrics, "contract_deploy_too_large_txs"}, 1)

return runtime.ErrMaxCodeSizeExceeded
}

// Grab the state root, current block number and block gas limit for the latest block
currentHeader := p.store.Header()
// Grab the state root, and block gas limit for the latest block
stateRoot := currentHeader.StateRoot
currentBlockNumber := currentHeader.Number
latestBlockGasLimit := currentHeader.GasLimit
baseFee := p.GetBaseFee() // base fee is calculated for the next block

if tx.Type == types.DynamicFeeTx {
// Reject dynamic fee tx if london hardfork is not enabled
if !p.forks.London {
if !forks.London {
metrics.IncrCounter([]string{txPoolMetrics, "invalid_tx_type"}, 1)

return ErrInvalidTxType
Expand Down Expand Up @@ -684,7 +689,7 @@ func (p *TxPool) validateTx(tx *types.Transaction) error {
}

// Make sure the transaction has more gas than the basic transaction fee
intrinsicGas, err := state.TransactionGasCost(tx, p.forks.Homestead, p.forks.Istanbul)
intrinsicGas, err := state.TransactionGasCost(tx, forks.Homestead, forks.Istanbul)
if err != nil {
metrics.IncrCounter([]string{txPoolMetrics, "invalid_intrinsic_gas_tx"}, 1)

Expand Down
9 changes: 5 additions & 4 deletions txpool/txpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func newTestPoolWithSlots(maxSlots uint64, mockStore ...store) (*TxPool, error)

return NewTxPool(
hclog.NewNullLogger(),
forks.At(0),
forks,
storeToUse,
nil,
nil,
Expand Down Expand Up @@ -2053,7 +2053,7 @@ func Test_TxPool_validateTx(t *testing.T) {
t.Run("tx input larger than the TxPoolMaxInitCodeSize", func(t *testing.T) {
t.Parallel()
pool := setupPool()
pool.forks.EIP158 = true
pool.forks = chain.AllForksEnabled

input := make([]byte, state.TxPoolMaxInitCodeSize+1)
_, err := rand.Read(input)
Expand All @@ -2072,7 +2072,7 @@ func Test_TxPool_validateTx(t *testing.T) {
t.Run("tx input the same as TxPoolMaxInitCodeSize", func(t *testing.T) {
t.Parallel()
pool := setupPool()
pool.forks.EIP158 = true
pool.forks = chain.AllForksEnabled

input := make([]byte, state.TxPoolMaxInitCodeSize)
_, err := rand.Read(input)
Expand Down Expand Up @@ -2195,7 +2195,8 @@ func Test_TxPool_validateTx(t *testing.T) {
t.Run("eip-1559 tx placed without eip-1559 fork enabled", func(t *testing.T) {
t.Parallel()
pool := setupPool()
pool.forks.London = false
pool.forks = chain.AllForksEnabled
pool.forks.RemoveFork(chain.London)

tx := newTx(defaultAddr, 0, 1)
tx.Type = types.DynamicFeeTx
Expand Down

0 comments on commit 992d6b9

Please sign in to comment.