Skip to content

Commit

Permalink
feat:Support cancun fork switch
Browse files Browse the repository at this point in the history
  • Loading branch information
lochjin authored and dindinw committed Aug 28, 2024
1 parent b76b9a1 commit b846c1a
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 10 deletions.
29 changes: 29 additions & 0 deletions consensus/forks/cancunfork.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package forks

import (
"github.com/Qitmeer/qng/core/protocol"
"github.com/Qitmeer/qng/params"
"github.com/ethereum/go-ethereum/common"
"math"
"math/big"
)

const (
// TODO:Future decision on whether to start
// Support cancun height
CancunForkEvmHeight = math.MaxInt64
)

func IsCancunForkHeight(height int64) bool {
if params.ActiveNetParams.Net == protocol.PrivNet {
return true
}
return height >= CancunForkEvmHeight
}

func GetCancunForkDifficulty(height int64) *big.Int {
if IsCancunForkHeight(height) {
return common.Big0
}
return common.Big1
}
8 changes: 4 additions & 4 deletions consensus/forks/meerchangefork.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (

const (
// TODO:Future decision on whether to start
// Support MeerChange system contract at height
MeerChangeForkHeight = math.MaxInt64
// Support MeerChange system contract at evm height
MeerChangeForkEvmHeight = math.MaxInt64
)

func IsMeerChangeForkHeight(mainHeight int64) bool {
func IsMeerChangeForkHeight(height int64) bool {
if params.ActiveNetParams.Net != protocol.MainNet {
return true
}
return mainHeight >= MeerChangeForkHeight
return height >= MeerChangeForkEvmHeight
}
8 changes: 6 additions & 2 deletions meerevm/meer/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,15 @@ func (me *MeerEngine) verifyHeader(chain consensus.ChainHeaderReader, header, pa
}

func (me *MeerEngine) CalcDifficulty(chain consensus.ChainHeaderReader, time uint64, parent *types.Header) *big.Int {
return big.NewInt(1)
return forks.GetCancunForkDifficulty(parent.Number.Int64())
}

func (me *MeerEngine) Prepare(chain consensus.ChainHeaderReader, header *types.Header) error {
header.Difficulty = big.NewInt(1)
number := header.Number.Int64()
if number > 0 {
number--
}
header.Difficulty = forks.GetCancunForkDifficulty(number)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion meerevm/meer/meerchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (b *MeerChain) buildBlock(parent *types.Header, qtxs []model.Tx, timestamp
gaslimit = 0x10000000000000
}

header := makeHeader(&b.chain.Config().Eth, parentBlock, statedb, timestamp, gaslimit)
header := makeHeader(&b.chain.Config().Eth, parentBlock, statedb, timestamp, gaslimit, forks.GetCancunForkDifficulty(parent.Number.Int64()))

if config.DAOForkSupport && config.DAOForkBlock != nil && config.DAOForkBlock.Cmp(header.Number) == 0 {
misc.ApplyDAOHardFork(statedb)
Expand Down
2 changes: 1 addition & 1 deletion meerevm/meer/meerpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (m *MeerPool) handler() {
}

func (m *MeerPool) prepareMeerChangeTxs(txs []*types.Transaction) bool {
if !forks.IsMeerChangeForkHeight(int64(m.consensus.BlockChain().GetMainChainTip().GetHeight())) {
if !forks.IsMeerChangeForkHeight(m.eth.BlockChain().CurrentBlock().Number.Int64()) {
return false
}
for _, tx := range txs {
Expand Down
4 changes: 2 additions & 2 deletions meerevm/meer/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"math/big"
)

func makeHeader(cfg *ethconfig.Config, parent *types.Block, state *state.StateDB, timestamp int64, gaslimit uint64) *types.Header {
func makeHeader(cfg *ethconfig.Config, parent *types.Block, state *state.StateDB, timestamp int64, gaslimit uint64, difficulty *big.Int) *types.Header {
ptt := int64(parent.Time())
if timestamp <= ptt {
timestamp = ptt + 1
Expand All @@ -25,7 +25,7 @@ func makeHeader(cfg *ethconfig.Config, parent *types.Block, state *state.StateDB
Root: state.IntermediateRoot(cfg.Genesis.Config.IsEIP158(parent.Number())),
ParentHash: parent.Hash(),
Coinbase: parent.Coinbase(),
Difficulty: common.Big1,
Difficulty: difficulty,
GasLimit: gaslimit,
Number: new(big.Int).Add(parent.Number(), common.Big1),
Time: uint64(timestamp),
Expand Down

0 comments on commit b846c1a

Please sign in to comment.