Skip to content

Commit f777318

Browse files
sync l1datafee change for curie (#888)
* update rollup/rcfg/config.go * update consensus/misc/curie.go * update rollup/fees/rollup_fee_test.go * update rollup/fees/rollup_fee.go * update rollup/tracing/tracing.go * update tests/state_test_util.go * update light/txpool.go * fix some * update eth/tracers/internal/tracetest/prestate_test.go * update eth/tracers/api.go * update core/chain_makers.go * update core/state_processor.go * update cmd/evm/internal/t8ntool/execution.go * update txpool * update ethclient/ethclient_test.go * update ethclient/gethclient/gethclient_test.go * update core/blockchain_test.go * update miner/worker.go * update txpool 2
1 parent 5e255cf commit f777318

File tree

31 files changed

+367
-103
lines changed

31 files changed

+367
-103
lines changed

accounts/abi/bind/backends/simulated.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallM
715715
vmEnv := vm.NewEVM(evmContext, txContext, stateDB, b.config, vm.Config{NoBaseFee: true})
716716
gasPool := new(core.GasPool).AddGas(math.MaxUint64)
717717
signer := types.MakeSigner(b.blockchain.Config(), header.Number, header.Time)
718-
l1DataFee, err := fees.EstimateL1DataFeeForMessage(msg, header.BaseFee, b.blockchain.Config().ChainID, signer, stateDB)
718+
l1DataFee, err := fees.EstimateL1DataFeeForMessage(msg, header.BaseFee, b.blockchain.Config(), signer, stateDB, header.Number)
719719
if err != nil {
720720
return nil, err
721721
}

cmd/evm/internal/t8ntool/execution.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
186186
chainConfig.DAOForkBlock.Cmp(new(big.Int).SetUint64(pre.Env.Number)) == 0 {
187187
misc.ApplyDAOHardFork(statedb)
188188
}
189+
// Apply Curie hard fork
190+
if chainConfig.CurieBlock != nil && chainConfig.CurieBlock.Cmp(new(big.Int).SetUint64(pre.Env.Number)) == 0 {
191+
misc.ApplyCurieHardFork(statedb)
192+
}
189193
if beaconRoot := pre.Env.ParentBeaconBlockRoot; beaconRoot != nil {
190194
evm := vm.NewEVM(vmContext, vm.TxContext{}, statedb, chainConfig, vmConfig)
191195
core.ProcessBeaconBlockRoot(*beaconRoot, evm, statedb)
@@ -221,7 +225,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
221225
)
222226
evm := vm.NewEVM(vmContext, txContext, statedb, chainConfig, vmConfig)
223227

224-
l1DataFee, err := fees.CalculateL1DataFee(tx, statedb)
228+
l1DataFee, err := fees.CalculateL1DataFee(tx, statedb, chainConfig, new(big.Int).SetUint64(pre.Env.Number))
225229
if err != nil {
226230
log.Info("rejected tx due to fees.CalculateL1DataFee", "index", i, "hash", tx.Hash(), "from", msg.From, "error", err)
227231
rejectedTxs = append(rejectedTxs, &rejectedTx{i, err.Error()})

consensus/misc/curie.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package misc
2+
3+
import (
4+
"github.com/ethereum/go-ethereum/common"
5+
"github.com/ethereum/go-ethereum/core/state"
6+
"github.com/ethereum/go-ethereum/log"
7+
"github.com/ethereum/go-ethereum/rollup/rcfg"
8+
)
9+
10+
// ApplyCurieHardFork modifies the state database according to the Curie hard-fork rules,
11+
// updating the bytecode and storage of the L1GasPriceOracle contract.
12+
func ApplyCurieHardFork(statedb *state.StateDB) {
13+
log.Info("Applying Curie hard fork")
14+
15+
// update contract byte code
16+
statedb.SetCode(rcfg.L1GasPriceOracleAddress, rcfg.CurieL1GasPriceOracleBytecode)
17+
18+
// initialize new storage slots
19+
statedb.SetState(rcfg.L1GasPriceOracleAddress, rcfg.IsCurieSlot, common.BytesToHash([]byte{1}))
20+
statedb.SetState(rcfg.L1GasPriceOracleAddress, rcfg.L1BlobBaseFeeSlot, common.BytesToHash([]byte{1}))
21+
statedb.SetState(rcfg.L1GasPriceOracleAddress, rcfg.CommitScalarSlot, common.BigToHash(rcfg.InitialCommitScalar))
22+
statedb.SetState(rcfg.L1GasPriceOracleAddress, rcfg.BlobScalarSlot, common.BigToHash(rcfg.InitialBlobScalar))
23+
}

core/blockchain_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package core
1818

1919
import (
20+
"encoding/json"
2021
"errors"
2122
"fmt"
2223
"math/big"
@@ -39,7 +40,9 @@ import (
3940
"github.com/ethereum/go-ethereum/eth/tracers/logger"
4041
"github.com/ethereum/go-ethereum/ethdb"
4142
"github.com/ethereum/go-ethereum/params"
43+
"github.com/ethereum/go-ethereum/rollup/rcfg"
4244
"github.com/ethereum/go-ethereum/trie"
45+
"github.com/stretchr/testify/assert"
4346
)
4447

4548
// So we can deterministically seed different blockchains
@@ -4716,3 +4719,78 @@ func TestEIP3651(t *testing.T) {
47164719
t.Fatalf("sender balance incorrect: expected %d, got %d", expected, actual)
47174720
}
47184721
}
4722+
4723+
func TestCurieTransition(t *testing.T) {
4724+
// Set fork blocks in config
4725+
// (we make a deep copy to avoid interference with other tests)
4726+
var config *params.ChainConfig
4727+
b, _ := json.Marshal(params.AllEthashProtocolChanges)
4728+
json.Unmarshal(b, &config)
4729+
config.CurieBlock = big.NewInt(2)
4730+
config.DescartesBlock = nil
4731+
4732+
var (
4733+
db = rawdb.NewMemoryDatabase()
4734+
gspec = &Genesis{Config: config}
4735+
genesis = gspec.MustCommit(db, trie.NewDatabase(db, trie.HashDefaults))
4736+
)
4737+
4738+
blockchain, _ := NewBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil)
4739+
defer blockchain.Stop()
4740+
blocks, _ := GenerateChain(gspec.Config, genesis, ethash.NewFaker(), db, 4, nil)
4741+
4742+
if _, err := blockchain.InsertChain(blocks); err != nil {
4743+
t.Fatal(err)
4744+
}
4745+
4746+
latestBlock := uint64(4)
4747+
assert.Equal(t, latestBlock, blockchain.CurrentHeader().Number.Uint64())
4748+
4749+
for ii := uint64(0); ii <= latestBlock; ii++ {
4750+
block := blockchain.GetBlockByNumber(ii)
4751+
4752+
number := block.Number().Uint64()
4753+
baseFee := block.BaseFee()
4754+
4755+
statedb, _ := state.New(block.Root(), state.NewDatabase(db), nil)
4756+
4757+
code := statedb.GetCode(rcfg.L1GasPriceOracleAddress)
4758+
codeSize := statedb.GetCodeSize(rcfg.L1GasPriceOracleAddress)
4759+
keccakCodeHash := statedb.GetKeccakCodeHash(rcfg.L1GasPriceOracleAddress)
4760+
poseidonCodeHash := statedb.GetPoseidonCodeHash(rcfg.L1GasPriceOracleAddress)
4761+
4762+
l1BlobBaseFee := statedb.GetState(rcfg.L1GasPriceOracleAddress, rcfg.L1BlobBaseFeeSlot)
4763+
commitScalar := statedb.GetState(rcfg.L1GasPriceOracleAddress, rcfg.CommitScalarSlot)
4764+
blobScalar := statedb.GetState(rcfg.L1GasPriceOracleAddress, rcfg.BlobScalarSlot)
4765+
isCurie := statedb.GetState(rcfg.L1GasPriceOracleAddress, rcfg.IsCurieSlot)
4766+
4767+
if number < config.CurieBlock.Uint64() {
4768+
assert.Nil(t, baseFee, "Expected zero base fee before Curie")
4769+
4770+
// we don't have predeploys configured in this test,
4771+
// so there is no gas oracle deployed before Curie
4772+
assert.Nil(t, code)
4773+
assert.Equal(t, uint64(0), codeSize)
4774+
assert.Equal(t, common.Hash{}, keccakCodeHash)
4775+
assert.Equal(t, common.Hash{}, poseidonCodeHash)
4776+
4777+
assert.Equal(t, common.Hash{}, l1BlobBaseFee)
4778+
assert.Equal(t, common.Hash{}, commitScalar)
4779+
assert.Equal(t, common.Hash{}, blobScalar)
4780+
assert.Equal(t, common.Hash{}, isCurie)
4781+
} else {
4782+
assert.NotNil(t, baseFee, "Expected nonzero base fee after Curie")
4783+
4784+
// all gas oracle entries updated
4785+
assert.NotNil(t, code)
4786+
assert.NotEqual(t, uint64(0), codeSize)
4787+
assert.NotEqual(t, common.Hash{}, keccakCodeHash)
4788+
assert.NotEqual(t, common.Hash{}, poseidonCodeHash)
4789+
4790+
assert.NotEqual(t, common.Hash{}, l1BlobBaseFee)
4791+
assert.NotEqual(t, common.Hash{}, commitScalar)
4792+
assert.NotEqual(t, common.Hash{}, blobScalar)
4793+
assert.NotEqual(t, common.Hash{}, isCurie)
4794+
}
4795+
}
4796+
}

core/chain_makers.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,9 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
328328
if config.DAOForkSupport && config.DAOForkBlock != nil && config.DAOForkBlock.Cmp(b.header.Number) == 0 {
329329
misc.ApplyDAOHardFork(statedb)
330330
}
331+
if config.CurieBlock != nil && config.CurieBlock.Cmp(b.header.Number) == 0 {
332+
misc.ApplyCurieHardFork(statedb)
333+
}
331334
// Execute any user modifications to the block
332335
if gen != nil {
333336
gen(i, b)

core/state_prefetcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (p *statePrefetcher) Prefetch(block *types.Block, statedb *state.StateDB, c
7171
}
7272
statedb.SetTxContext(tx.Hash(), i)
7373

74-
l1DataFee, err := fees.CalculateL1DataFee(tx, statedb)
74+
l1DataFee, err := fees.CalculateL1DataFee(tx, statedb, p.config, block.Number())
7575
if err != nil {
7676
return
7777
}

core/state_processor.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
7272
if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 {
7373
misc.ApplyDAOHardFork(statedb)
7474
}
75+
// Apply Curie hard fork
76+
if p.config.CurieBlock != nil && p.config.CurieBlock.Cmp(block.Number()) == 0 {
77+
misc.ApplyCurieHardFork(statedb)
78+
}
7579
var (
7680
context = NewEVMBlockContext(header, p.bc, p.config, nil)
7781
vmenv = vm.NewEVM(context, vm.TxContext{}, statedb, p.config, cfg)
@@ -110,7 +114,7 @@ func applyTransaction(msg *Message, config *params.ChainConfig, gp *GasPool, sta
110114
txContext := NewEVMTxContext(msg)
111115
evm.Reset(txContext, statedb)
112116

113-
l1DataFee, err := fees.CalculateL1DataFee(tx, statedb)
117+
l1DataFee, err := fees.CalculateL1DataFee(tx, statedb, config, blockNumber)
114118
if err != nil {
115119
return nil, err
116120
}

core/txpool/blobpool/blobpool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,7 @@ func (p *BlobPool) validateTx(tx *types.Transaction) error {
10801080
return nil
10811081
},
10821082
}
1083-
if err := txpool.ValidateTransactionWithState(tx, p.signer, stateOpts); err != nil {
1083+
if err := txpool.ValidateTransactionWithState(tx, p.signer, stateOpts, p.chain.Config(), p.head.Number); err != nil {
10841084
return err
10851085
}
10861086
// If the transaction replaces an existing one, ensure that price bumps are

core/txpool/legacypool/legacypool.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ func (pool *LegacyPool) validateTx(tx *types.Transaction, local bool) error {
638638
return nil
639639
},
640640
}
641-
if err := txpool.ValidateTransactionWithState(tx, pool.signer, opts); err != nil {
641+
if err := txpool.ValidateTransactionWithState(tx, pool.signer, opts, pool.chainconfig, pool.currentHead.Load().Number); err != nil {
642642
return err
643643
}
644644
return nil
@@ -759,7 +759,7 @@ func (pool *LegacyPool) add(tx *types.Transaction, local bool) (replaced bool, e
759759
// Try to replace an existing transaction in the pending pool
760760
if list := pool.pending[from]; list != nil && list.Contains(tx.Nonce()) {
761761
// Nonce already pending, check if required price bump is met
762-
inserted, old := list.Add(tx, pool.config.PriceBump)
762+
inserted, old := list.Add(tx, pool.currentState, pool.config.PriceBump, pool.chainconfig, pool.currentHead.Load().Number)
763763
if !inserted {
764764
pendingDiscardMeter.Mark(1)
765765
return false, txpool.ErrReplaceUnderpriced
@@ -833,7 +833,7 @@ func (pool *LegacyPool) enqueueTx(hash common.Hash, tx *types.Transaction, local
833833
if pool.queue[from] == nil {
834834
pool.queue[from] = newList(false)
835835
}
836-
inserted, old := pool.queue[from].Add(tx, pool.config.PriceBump)
836+
inserted, old := pool.queue[from].Add(tx, pool.currentState, pool.config.PriceBump, pool.chainconfig, pool.currentHead.Load().Number)
837837
if !inserted {
838838
// An older transaction was better, discard this
839839
queuedDiscardMeter.Mark(1)
@@ -887,7 +887,7 @@ func (pool *LegacyPool) promoteTx(addr common.Address, hash common.Hash, tx *typ
887887
}
888888
list := pool.pending[addr]
889889

890-
inserted, old := list.Add(tx, pool.config.PriceBump)
890+
inserted, old := list.Add(tx, pool.currentState, pool.config.PriceBump, pool.chainconfig, pool.currentHead.Load().Number)
891891
if !inserted {
892892
// An older transaction was better, discard this
893893
pool.all.Remove(hash)

core/txpool/legacypool/list.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ import (
2626
"time"
2727

2828
"github.com/ethereum/go-ethereum/common"
29+
"github.com/ethereum/go-ethereum/core/state"
2930
"github.com/ethereum/go-ethereum/core/types"
31+
"github.com/ethereum/go-ethereum/log"
32+
"github.com/ethereum/go-ethereum/params"
33+
"github.com/ethereum/go-ethereum/rollup/fees"
3034
)
3135

3236
// nonceHeap is a heap.Interface implementation over 64bit unsigned integers for
@@ -298,7 +302,7 @@ func (l *list) Contains(nonce uint64) bool {
298302
//
299303
// If the new transaction is accepted into the list, the lists' cost and gas
300304
// thresholds are also potentially updated.
301-
func (l *list) Add(tx *types.Transaction, priceBump uint64) (bool, *types.Transaction) {
305+
func (l *list) Add(tx *types.Transaction, state *state.StateDB, priceBump uint64, chainconfig *params.ChainConfig, blockNumber *big.Int) (bool, *types.Transaction) {
302306
// If there's an older better transaction, abort
303307
old := l.txs.Get(tx.Nonce())
304308
if old != nil {
@@ -322,13 +326,24 @@ func (l *list) Add(tx *types.Transaction, priceBump uint64) (bool, *types.Transa
322326
return false, nil
323327
}
324328
// Old is being replaced, subtract old cost
329+
// TODO: fix for L1DataFee
325330
l.subTotalCost([]*types.Transaction{old})
326331
}
332+
l1DataFee := big.NewInt(0)
333+
if state != nil && chainconfig != nil {
334+
var err error
335+
l1DataFee, err = fees.CalculateL1DataFee(tx, state, chainconfig, blockNumber)
336+
if err != nil {
337+
log.Error("Failed to calculate L1 data fee", "err", err, "tx", tx)
338+
return false, nil
339+
}
340+
}
327341
// Add new tx cost to totalcost
342+
// TODO: fix totalcost for L1DataFee for both sub and add
328343
l.totalcost.Add(l.totalcost, tx.Cost())
329344
// Otherwise overwrite the old transaction with the current one
330345
l.txs.Put(tx)
331-
if cost := tx.Cost(); l.costcap.Cmp(cost) < 0 {
346+
if cost := new(big.Int).Add(tx.Cost(), l1DataFee); l.costcap.Cmp(cost) < 0 {
332347
l.costcap = cost
333348
}
334349
if gas := tx.Gas(); l.gascap < gas {
@@ -454,6 +469,7 @@ func (l *list) LastElement() *types.Transaction {
454469

455470
// subTotalCost subtracts the cost of the given transactions from the
456471
// total cost of all transactions.
472+
// TODO: fix for L1DataFee
457473
func (l *list) subTotalCost(txs []*types.Transaction) {
458474
for _, tx := range txs {
459475
l.totalcost.Sub(l.totalcost, tx.Cost())

0 commit comments

Comments
 (0)