Skip to content

Commit

Permalink
feat(all): update moonchain protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
luanxu-mxc committed Oct 13, 2024
1 parent 8ec1bd5 commit 90619e1
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 40 deletions.
2 changes: 1 addition & 1 deletion cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions cmd/utils/moonchain_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 0 additions & 2 deletions consensus/moonchain/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
3 changes: 3 additions & 0 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 6 additions & 14 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
Expand Down
2 changes: 1 addition & 1 deletion eth/catalyst/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion eth/ethconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion eth/state_accessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 5 additions & 11 deletions eth/tracers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 2 additions & 7 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 90619e1

Please sign in to comment.