From 90619e1dffb44c9a1f77230ff0d4fca9c9d9219c Mon Sep 17 00:00:00 2001 From: luanxu-mxc Date: Sun, 13 Oct 2024 15:30:16 +0800 Subject: [PATCH] feat(all): update moonchain protocol --- cmd/utils/flags.go | 2 +- cmd/utils/moonchain_flags.go | 4 ++-- consensus/moonchain/consensus.go | 2 -- core/state_processor.go | 3 +++ core/state_transition.go | 20 ++++++-------------- eth/catalyst/api.go | 2 +- eth/ethconfig/config.go | 2 +- eth/state_accessor.go | 2 +- eth/tracers/api.go | 16 +++++----------- miner/worker.go | 9 ++------- 10 files changed, 22 insertions(+), 40 deletions(-) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 86ea8ee95737..b4fbc4709021 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -1740,7 +1740,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) { } // Override any default configs for hard coded networks. switch { - // CHANGE(moonchain): when --moonchain flag is set, use the Taiko genesis. + // CHANGE(moonchain): when --mxc flag is set, use the Moonchain genesis. case ctx.IsSet(MoonchainFlag.Name): cfg.Genesis = core.MoonchainGenesisBlock(cfg.NetworkId) // CHANGE(taiko): when --taiko flag is set, use the Taiko genesis. diff --git a/cmd/utils/moonchain_flags.go b/cmd/utils/moonchain_flags.go index d9ee05a3f614..e8b42a5dda26 100644 --- a/cmd/utils/moonchain_flags.go +++ b/cmd/utils/moonchain_flags.go @@ -18,12 +18,12 @@ var ( } ) -// RegisterMoonchainAPIs initializes and registers the Taiko RPC APIs. +// RegisterMoonchainAPIs initializes and registers the Moonchain RPC APIs. func RegisterMoonchainAPIs(stack *node.Node, cfg *ethconfig.Config, backend *eth.Ethereum) { if os.Getenv("MOONCHAIN_TEST") != "" { return } - // Add methods under "taiko_" RPC namespace to the available APIs list + // Add methods under "moonchain_" RPC namespace to the available APIs list stack.RegisterAPIs([]rpc.API{ { Namespace: "moonchain", diff --git a/consensus/moonchain/consensus.go b/consensus/moonchain/consensus.go index feec343204ce..6a0ee4984fc1 100644 --- a/consensus/moonchain/consensus.go +++ b/consensus/moonchain/consensus.go @@ -204,8 +204,6 @@ func (t *Moonchain) Finalize(chain consensus.ChainHeaderReader, header *types.He for _, w := range withdrawals { state.AddBalance(w.Address, uint256.MustFromBig(new(big.Int).SetUint64(w.Amount))) } - - state.AddBalance(t.moonchainL2Treasury, uint256.MustFromBig(new(big.Int).SetBytes(header.Extra))) header.Root = state.IntermediateRoot(true) } diff --git a/core/state_processor.go b/core/state_processor.go index 50c623c6b57d..07579277235a 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -92,6 +92,9 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg if err != nil { return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err) } + if p.config.IsOntake(block.Number()) { + msg.BasefeeSharingPctg = DecodeOntakeExtraData(header.Extra) + } statedb.SetTxContext(tx.Hash(), i) receipt, err := applyTransaction(msg, p.config, gp, statedb, blockNumber, blockHash, tx, usedGas, vmenv) if err != nil { diff --git a/core/state_transition.go b/core/state_transition.go index 7e4d979b26e9..b1e572b93077 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -470,27 +470,19 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) { fee := new(uint256.Int).SetUint64(st.gasUsed()) fee.Mul(fee, effectiveTipU256) st.state.AddBalance(st.evm.Context.Coinbase, fee) - // CHANGE(moonchain): basefee is not burnt, but sent to a treasury and block.coinbase instead. - // TODO: moonchain protocol update - if st.evm.ChainConfig().Mxc && st.evm.Context.BaseFee != nil && !st.msg.IsAnchor { - totalFee := new(big.Int).Mul(st.evm.Context.BaseFee, new(big.Int).SetUint64(st.gasUsed())) - feeCoinbase := new(big.Int).Div( - new(big.Int).Mul(totalFee, new(big.Int).SetUint64(uint64(st.msg.BasefeeSharingPctg))), - new(big.Int).SetUint64(100), - ) - feeTreasury := new(big.Int).Sub(totalFee, feeCoinbase) - st.state.AddBalance(st.getMoonchainTreasuryAddress(), uint256.MustFromBig(feeTreasury)) - st.state.AddBalance(st.evm.Context.Coinbase, uint256.MustFromBig(feeCoinbase)) - } // CHANGE(taiko): basefee is not burnt, but sent to a treasury and block.coinbase instead. - if st.evm.ChainConfig().Taiko && st.evm.Context.BaseFee != nil && !st.msg.IsAnchor { + if (st.evm.ChainConfig().Taiko || st.evm.ChainConfig().Mxc) && st.evm.Context.BaseFee != nil && !st.msg.IsAnchor { totalFee := new(big.Int).Mul(st.evm.Context.BaseFee, new(big.Int).SetUint64(st.gasUsed())) feeCoinbase := new(big.Int).Div( new(big.Int).Mul(totalFee, new(big.Int).SetUint64(uint64(st.msg.BasefeeSharingPctg))), new(big.Int).SetUint64(100), ) feeTreasury := new(big.Int).Sub(totalFee, feeCoinbase) - st.state.AddBalance(st.getTreasuryAddress(), uint256.MustFromBig(feeTreasury)) + if st.evm.ChainConfig().Mxc { + st.state.AddBalance(st.getMoonchainTreasuryAddress(), uint256.MustFromBig(feeTreasury)) + } else { + st.state.AddBalance(st.getTreasuryAddress(), uint256.MustFromBig(feeTreasury)) + } st.state.AddBalance(st.evm.Context.Coinbase, uint256.MustFromBig(feeCoinbase)) } } diff --git a/eth/catalyst/api.go b/eth/catalyst/api.go index c5e4b8b32899..792711c1c414 100644 --- a/eth/catalyst/api.go +++ b/eth/catalyst/api.go @@ -554,7 +554,7 @@ func (api *ConsensusAPI) NewPayloadV2(params engine.ExecutableData) (engine.Payl } if api.eth.BlockChain().Config().LatestFork(params.Timestamp) == forks.Shanghai { if params.Withdrawals == nil && - (api.eth.BlockChain().Config().Taiko && params.WithdrawalsHash == (common.Hash{})) { + (api.eth.BlockChain().Config().Taiko || api.eth.BlockChain().Config().Mxc) && params.WithdrawalsHash == (common.Hash{}) { return engine.PayloadStatusV1{Status: engine.INVALID}, engine.InvalidParams.With(errors.New("nil withdrawals post-shanghai")) } } else { diff --git a/eth/ethconfig/config.go b/eth/ethconfig/config.go index 0e62fd5f136e..b5af31ae4079 100644 --- a/eth/ethconfig/config.go +++ b/eth/ethconfig/config.go @@ -167,7 +167,7 @@ type Config struct { // Clique is allowed for now to live standalone, but ethash is forbidden and can // only exist on already merged networks. func CreateConsensusEngine(config *params.ChainConfig, db ethdb.Database) (consensus.Engine, error) { - // CHANGE(moonchain): use Mxc consesus engine when the --moonchain flag is set + // CHANGE(moonchain): use Mxc consesus engine when the --mxc flag is set if config.Mxc { return moonchain.New(config), nil } diff --git a/eth/state_accessor.go b/eth/state_accessor.go index 8b2bb6bb1ad9..64f3d0907eb3 100644 --- a/eth/state_accessor.go +++ b/eth/state_accessor.go @@ -239,7 +239,7 @@ func (eth *Ethereum) stateAtTransaction(ctx context.Context, block *types.Block, // Recompute transactions up to the target index. signer := types.MakeSigner(eth.blockchain.Config(), block.Number(), block.Time()) for idx, tx := range block.Transactions() { - if idx == 0 && eth.config.Genesis.Config.Taiko { + if idx == 0 && (eth.config.Genesis.Config.Taiko || eth.config.Genesis.Config.Mxc) { if err := tx.MarkAsAnchor(); err != nil { return nil, vm.BlockContext{}, nil, nil, err } diff --git a/eth/tracers/api.go b/eth/tracers/api.go index f6117cadcc48..6915a1b9e2db 100644 --- a/eth/tracers/api.go +++ b/eth/tracers/api.go @@ -270,7 +270,7 @@ func (api *API) traceChain(start, end *types.Block, config *TraceConfig, closed ) // Trace all the transactions contained within for i, tx := range task.block.Transactions() { - if i == 0 && api.backend.ChainConfig().Taiko { + if i == 0 && (api.backend.ChainConfig().Taiko || api.backend.ChainConfig().Mxc) { if err := tx.MarkAsAnchor(); err != nil { log.Warn("Mark anchor transaction error", "error", err) task.results[i] = &txTraceResult{TxHash: tx.Hash(), Error: err.Error()} @@ -534,7 +534,7 @@ func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config deleteEmptyObjects = chainConfig.IsEIP158(block.Number()) ) for i, tx := range block.Transactions() { - if i == 0 && chainConfig.Taiko { + if i == 0 && (chainConfig.Taiko || chainConfig.Mxc) { if err := tx.MarkAsAnchor(); err != nil { return nil, err } @@ -616,7 +616,7 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac results = make([]*txTraceResult, len(txs)) ) for i, tx := range txs { - if i == 0 && api.backend.ChainConfig().Taiko { + if i == 0 && (api.backend.ChainConfig().Taiko || api.backend.ChainConfig().Mxc) { if err := tx.MarkAsAnchor(); err != nil { return nil, err } @@ -660,14 +660,8 @@ func (api *API) traceBlockParallel(ctx context.Context, block *types.Block, stat } // CHANGE(moonchain): mark the first transaction as anchor transaction. - if len(txs) > 0 && api.backend.ChainConfig().Mxc { - if err := txs[0].MarkAsAnchor(); err != nil { - return nil, err - } - } - // CHANGE(taiko): mark the first transaction as anchor transaction. - if len(txs) > 0 && api.backend.ChainConfig().Taiko { + if len(txs) > 0 && (api.backend.ChainConfig().Taiko || api.backend.ChainConfig().Mxc) { if err := txs[0].MarkAsAnchor(); err != nil { return nil, err } @@ -789,7 +783,7 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block chainConfig, canon = overrideConfig(chainConfig, config.Overrides) } for i, tx := range block.Transactions() { - if i == 0 && chainConfig.Taiko { + if i == 0 && (chainConfig.Taiko || chainConfig.Mxc) { if err := tx.MarkAsAnchor(); err != nil { return nil, err } diff --git a/miner/worker.go b/miner/worker.go index 1629bae693d2..1b7cd511280f 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -959,12 +959,7 @@ func (w *worker) prepareWork(genParams *generateParams) (*environment, error) { if parent.Time >= timestamp { // CHANGE(taiko): block.timestamp == parent.timestamp is allowed in Taiko protocol. // CHANGE(moonchain): block.timestamp == parent.timestamp is allowed in Mxc protocol. - if !w.chainConfig.Mxc { - if genParams.forceTime { - return nil, fmt.Errorf("invalid timestamp, parent %d given %d", parent.Time, timestamp) - } - timestamp = parent.Time + 1 - } else if !w.chainConfig.Taiko { + if !w.chainConfig.Taiko && !w.chainConfig.Mxc { if genParams.forceTime { return nil, fmt.Errorf("invalid timestamp, parent %d given %d", parent.Time, timestamp) } @@ -993,7 +988,7 @@ func (w *worker) prepareWork(genParams *generateParams) (*environment, error) { } // Set baseFee and GasLimit if we are on an EIP-1559 chain if w.chainConfig.IsLondon(header.Number) { - if w.chainConfig.Taiko && genParams.baseFeePerGas != nil { + if (w.chainConfig.Taiko || w.chainConfig.Mxc) && genParams.baseFeePerGas != nil { header.BaseFee = genParams.baseFeePerGas } else { header.BaseFee = eip1559.CalcBaseFee(w.chainConfig, parent)