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 Nov 21, 2024
1 parent 71f983c commit d2d1adc
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ARG VERSION=""
ARG BUILDNUM=""

# Build Geth in a stock Go builder container
FROM golang:1.23-alpine AS builder
FROM --platform=${BUILDPLATFORM} golang:1.23-alpine as builder

RUN apk add --no-cache gcc musl-dev linux-headers git

Expand Down
27 changes: 15 additions & 12 deletions consensus/moonchain/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/core/tracing"
"math/big"
"strings"
"time"
Expand Down Expand Up @@ -196,13 +197,17 @@ func (t *Moonchain) Prepare(chain consensus.ChainHeaderReader, header *types.Hea
//
// Note: The block header and state database might be updated to reflect any
// consensus rules that happen at finalization (e.g. block rewards).
func (t *Moonchain) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, withdrawals []*types.Withdrawal) {
func (t *Moonchain) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body) {
// no block rewards in l2
header.UncleHash = types.CalcUncleHash(nil)
header.Difficulty = common.Big0
// Withdrawals processing.
for _, w := range withdrawals {
state.AddBalance(w.Address, uint256.MustFromBig(new(big.Int).SetUint64(w.Amount)))
for _, w := range body.Withdrawals {
state.AddBalance(
w.Address,
uint256.MustFromBig(new(big.Int).SetUint64(w.Amount)),
tracing.BalanceIncreaseWithdrawal,
)
}
header.Root = state.IntermediateRoot(true)
}
Expand All @@ -212,14 +217,14 @@ func (t *Moonchain) Finalize(chain consensus.ChainHeaderReader, header *types.He
//
// Note: The block header and state database might be updated to reflect any
// consensus rules that happen at finalization (e.g. block rewards).
func (t *Moonchain) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt, withdrawals []*types.Withdrawal) (*types.Block, error) {
if withdrawals == nil {
withdrawals = make([]*types.Withdrawal, 0)
func (t *Moonchain) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, receipts []*types.Receipt) (*types.Block, error) {
if body.Withdrawals == nil {
body.Withdrawals = make([]*types.Withdrawal, 0)
}

// Verify anchor transaction
if len(txs) != 0 { // Transactions list might be empty when building empty payload.
isAnchor, err := t.ValidateAnchorTx(txs[0], header)
if len(body.Transactions) != 0 { // Transactions list might be empty when building empty payload.
isAnchor, err := t.ValidateAnchorTx(body.Transactions[0], header)
if err != nil {
return nil, err
}
Expand All @@ -229,10 +234,8 @@ func (t *Moonchain) FinalizeAndAssemble(chain consensus.ChainHeaderReader, heade
}

// Finalize block
t.Finalize(chain, header, state, txs, uncles, withdrawals)
return types.NewBlockWithWithdrawals(
header, txs, nil /* ignore uncles */, receipts, withdrawals, trie.NewStackTrie(nil),
), nil
t.Finalize(chain, header, state, body)
return types.NewBlock(header, body, receipts, trie.NewStackTrie(nil)), nil
}

// Seal generates a new sealing request for the given input block and pushes
Expand Down
29 changes: 10 additions & 19 deletions consensus/moonchain/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package moonchain_test

import (
"bytes"
"github.com/ethereum/go-ethereum/consensus/moonchain"
"math/big"
"strings"
"testing"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/taiko"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
Expand All @@ -30,25 +29,17 @@ var (

genesis *core.Genesis
txs []*types.Transaction
testEngine *taiko.Taiko
testEngine *moonchain.Moonchain
)

func init() {
config := params.TestChainConfig
config.GrayGlacierBlock = nil
config.ArrowGlacierBlock = nil
config.Ethash = nil
config.Taiko = true
testEngine = taiko.New(config)

taikoL2AddressPrefix := strings.TrimPrefix(config.ChainID.String(), "0")

taikoL2Address := common.HexToAddress(
"0x" +
taikoL2AddressPrefix +
strings.Repeat("0", common.AddressLength*2-len(taikoL2AddressPrefix)-len(taiko.TaikoL2AddressSuffix)) +
taiko.TaikoL2AddressSuffix,
)
config.Mxc = true
testEngine = moonchain.New(config)
moonchainL2Address := common.HexToAddress("0x1000777700000000000000000000000000000001")

genesis = &core.Genesis{
Config: config,
Expand All @@ -64,9 +55,9 @@ func init() {
Nonce: 0,
GasTipCap: common.Big0,
GasFeeCap: new(big.Int).SetUint64(875_000_000),
Data: taiko.AnchorSelector,
Gas: taiko.AnchorGasLimit,
To: &taikoL2Address,
Data: moonchain.AnchorSelector,
Gas: moonchain.AnchorGasLimit,
To: &moonchainL2Address,
}),
types.MustSignNewTx(testKey, types.LatestSigner(genesis.Config), &types.LegacyTx{
Nonce: 0,
Expand Down Expand Up @@ -122,7 +113,7 @@ func newTestBackend(t *testing.T) (*eth.Ethereum, []*types.Block) {
t.Fatalf("can't import test blocks: %v", err)
}

if _, ok := ethservice.Engine().(*taiko.Taiko); !ok {
if _, ok := ethservice.Engine().(*moonchain.Moonchain); !ok {
t.Fatalf("not use taiko engine")
}

Expand All @@ -134,7 +125,7 @@ func generateTestChain() []*types.Block {
generate := func(i int, g *core.BlockGen) {
g.OffsetTime(5)

g.SetExtra([]byte("test_taiko"))
g.SetExtra([]byte("test_moonchain"))
g.SetDifficulty(common.Big0)

for i, tx := range txs {
Expand Down
6 changes: 3 additions & 3 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,11 +497,11 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
)
feeTreasury := new(big.Int).Sub(totalFee, feeCoinbase)
if st.evm.ChainConfig().Mxc {
st.state.AddBalance(st.getMoonchainTreasuryAddress(), uint256.MustFromBig(feeTreasury))
st.state.AddBalance(st.getMoonchainTreasuryAddress(), uint256.MustFromBig(feeTreasury), tracing.BalanceIncreaseTreasury)
} else {
st.state.AddBalance(st.getTreasuryAddress(), uint256.MustFromBig(feeTreasury))
st.state.AddBalance(st.getTreasuryAddress(), uint256.MustFromBig(feeTreasury), tracing.BalanceIncreaseTreasury)
}
st.state.AddBalance(st.evm.Context.Coinbase, uint256.MustFromBig(feeCoinbase))
st.state.AddBalance(st.evm.Context.Coinbase, uint256.MustFromBig(feeCoinbase), tracing.BalanceIncreaseBaseFeeSharing)
}
// add the coinbase to the witness iff the fee is greater than 0
if rules.IsEIP4762 && fee.Sign() != 0 {
Expand Down
2 changes: 1 addition & 1 deletion miner/payload_building.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func (miner *Miner) buildPayload(args *BuildPayloadArgs, witness bool) (*Payload
select {
case <-timer.C:
// CHANGE(moonchain): do not update payload.
if w.chainConfig.Mxc {
if miner.chainConfig.Mxc {
continue
}
// CHANGE(taiko): do not update payload.
Expand Down
1 change: 0 additions & 1 deletion params/moonchain_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ var networkIDToChainConfigByMoonchain = map[*big.Int]*ChainConfig{
MoonchainGenevaNetworkID: MoonchainChainConfig,
MainnetChainConfig.ChainID: MainnetChainConfig,
SepoliaChainConfig.ChainID: SepoliaChainConfig,
GoerliChainConfig.ChainID: GoerliChainConfig,
TestChainConfig.ChainID: TestChainConfig,
NonActivatedConfig.ChainID: NonActivatedConfig,
}
Expand Down
5 changes: 0 additions & 5 deletions params/moonchain_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ func TestNetworkIDToChainConfigOrDefaultByMoonchain(t *testing.T) {
SepoliaChainConfig.ChainID,
SepoliaChainConfig,
},
{
"goerli",
GoerliChainConfig.ChainID,
GoerliChainConfig,
},
{
"doesntExist",
big.NewInt(89390218390),
Expand Down

0 comments on commit d2d1adc

Please sign in to comment.