Skip to content

Commit

Permalink
restruct
Browse files Browse the repository at this point in the history
  • Loading branch information
frankcrypto committed Dec 11, 2023
1 parent 149a9a7 commit e209def
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 24 deletions.
17 changes: 17 additions & 0 deletions consensus/model/difficulty_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package model

import (
"math/big"
"time"

"github.com/Qitmeer/qng/core/types/pow"
)

// DifficultyManager provides a method to resolve the
// difficulty value of a block
type DifficultyManager interface {
CalcNextRequiredDifficulty(timestamp time.Time, powType pow.PowType) (uint32, error)
RequiredDifficulty(block Block, newBlockTime time.Time, powInstance pow.IPow) (uint32, error)
CalcEasiestDifficulty(bits uint32, duration time.Duration, powInstance pow.IPow) uint32
GetCurrentPowDiff(ib Block, powType pow.PowType) *big.Int
}
5 changes: 4 additions & 1 deletion core/blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/Qitmeer/qng/core/state"
"github.com/Qitmeer/qng/core/types"
"github.com/Qitmeer/qng/core/types/pow"
"github.com/Qitmeer/qng/core/types/pow/difficultymanager"
"github.com/Qitmeer/qng/database/common"
"github.com/Qitmeer/qng/engine/txscript"
l "github.com/Qitmeer/qng/log"
Expand Down Expand Up @@ -136,7 +137,8 @@ type BlockChain struct {
wg sync.WaitGroup
quit chan struct{}

meerChain *meer.MeerChain
meerChain *meer.MeerChain
difficultyManager model.DifficultyManager
}

func (b *BlockChain) Init() error {
Expand Down Expand Up @@ -1070,6 +1072,7 @@ func New(consensus model.Consensus) (*BlockChain, error) {
}
b.meerChain = mchain
b.Services().RegisterService(b.meerChain)
b.difficultyManager = difficultymanager.NewDiffManager(b.Consensus().BlockChain(), par)
return &b, nil
}

Expand Down
48 changes: 48 additions & 0 deletions core/blockchain/difficulty.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2017-2018 The qitmeer developers
// Copyright (c) 2013-2016 The btcsuite developers
// Copyright (c) 2015-2018 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package blockchain

import (
"math/big"
"time"

"github.com/Qitmeer/qng/consensus/model"
"github.com/Qitmeer/qng/core/types/pow"
)

// CalcEasiestDifficulty calculates the easiest possible difficulty that a block
// can have given starting difficulty bits and a duration. It is mainly used to
// verify that claimed proof of work by a block is sane as compared to a
// known good checkpoint.
func (m *BlockChain) calcEasiestDifficulty(bits uint32, duration time.Duration, powInstance pow.IPow) uint32 {
return m.difficultyManager.CalcEasiestDifficulty(bits, duration, powInstance)
}

func (m *BlockChain) calcNextRequiredDifficulty(block model.Block, newBlockTime time.Time, powInstance pow.IPow) (uint32, error) {
return m.difficultyManager.RequiredDifficulty(block, newBlockTime, powInstance)
}

// CalcNextRequiredDifficulty calculates the required difficulty for the block
// after the end of the current best chain based on the difficulty retarget
// rules.
//
// This function is safe for concurrent access.
func (b *BlockChain) CalcNextRequiredDifficulty(timestamp time.Time, powType pow.PowType) (uint32, error) {
b.ChainRLock()
block := b.GetMainChainTip()
instance := pow.GetInstance(powType, 0, []byte{})
instance.SetParams(b.params.PowConfig)
instance.SetMainHeight(pow.MainHeight(block.GetHeight() + 1))
difficulty, err := b.difficultyManager.RequiredDifficulty(block, timestamp, instance)
b.ChainRUnlock()
return difficulty, err
}

// find block node by pow type
func (b *BlockChain) GetCurrentPowDiff(ib model.Block, powType pow.PowType) *big.Int {
return b.GetCurrentPowDiff(ib, powType)
}
3 changes: 1 addition & 2 deletions core/blockchain/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/Qitmeer/qng/core/state"
"github.com/Qitmeer/qng/core/types"
"github.com/Qitmeer/qng/core/types/pow"
"github.com/Qitmeer/qng/core/types/pow/difficultymanager"
"github.com/Qitmeer/qng/engine/txscript"
l "github.com/Qitmeer/qng/log"
"github.com/Qitmeer/qng/meerdag"
Expand Down Expand Up @@ -155,7 +154,7 @@ func (b *BlockChain) preProcessBlock(block *types.SerializedBlock, flags Behavio
// expected based on elapsed time since the last checkpoint and
// maximum adjustment allowed by the retarget rules.
duration := blockHeader.Timestamp.Sub(checkpointTime)
requiredTarget := pow.CompactToBig(difficultymanager.NewDiffManager(b.consensus.BlockChain(), b.params).CalcEasiestDifficulty(
requiredTarget := pow.CompactToBig(b.calcEasiestDifficulty(
checkpointNode.Difficulty, duration, block.Block().Header.Pow))
currentTarget := pow.CompactToBig(blockHeader.Difficulty)
if !block.Block().Header.Pow.CompareDiff(currentTarget, requiredTarget) {
Expand Down
3 changes: 1 addition & 2 deletions core/blockchain/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/Qitmeer/qng/core/state"
"github.com/Qitmeer/qng/core/types"
"github.com/Qitmeer/qng/core/types/pow"
"github.com/Qitmeer/qng/core/types/pow/difficultymanager"
"github.com/Qitmeer/qng/engine/txscript"
"github.com/Qitmeer/qng/meerdag"
"github.com/Qitmeer/qng/params"
Expand Down Expand Up @@ -761,7 +760,7 @@ func (b *BlockChain) checkBlockHeaderContext(block *types.SerializedBlock, prevN
// the calculated difficulty based on the previous block and
// difficulty retarget rules.

expDiff, err := difficultymanager.NewDiffManager(b.consensus.BlockChain(), b.params).RequiredDifficulty(prevNode,
expDiff, err := b.calcNextRequiredDifficulty(prevNode,
header.Timestamp, instance)
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
package difficultymanager

import (
"math/big"
"time"

"github.com/Qitmeer/qng/consensus/model"
"github.com/Qitmeer/qng/core/types"
"github.com/Qitmeer/qng/core/types/pow"
"github.com/Qitmeer/qng/params"
)

// DifficultyManager provides a method to resolve the
// difficulty value of a block
type DifficultyManager interface {
CalcNextRequiredDifficulty(timestamp time.Time, powType pow.PowType) (uint32, error)
RequiredDifficulty(block model.Block, newBlockTime time.Time, powInstance pow.IPow) (uint32, error)
CalcEasiestDifficulty(bits uint32, duration time.Duration, powInstance pow.IPow) uint32
GetCurrentPowDiff(ib model.Block, powType pow.PowType) *big.Int
}

func NewDiffManager(b model.BlockChain, cfg *params.Params) DifficultyManager {
func NewDiffManager(b model.BlockChain, cfg *params.Params) model.DifficultyManager {
switch cfg.PowConfig.DifficultyMode {
case types.DIFFICULTY_MODE_KASPAD:
return &kaspadDiff{
Expand Down
3 changes: 1 addition & 2 deletions node/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/Qitmeer/qng/core/json"
"github.com/Qitmeer/qng/core/protocol"
"github.com/Qitmeer/qng/core/types/pow"
"github.com/Qitmeer/qng/core/types/pow/difficultymanager"
"github.com/Qitmeer/qng/meerdag"
"github.com/Qitmeer/qng/meerevm/eth"
"github.com/Qitmeer/qng/params"
Expand Down Expand Up @@ -60,7 +59,7 @@ func NewPublicBlockChainAPI(node *QitmeerFull) *PublicBlockChainAPI {
func (api *PublicBlockChainAPI) GetNodeInfo() (interface{}, error) {
best := api.node.GetBlockChain().BestSnapshot()
node := api.node.GetBlockChain().BlockDAG().GetBlock(&best.Hash)
powNodes := difficultymanager.NewDiffManager(api.node.GetBlockChain().Consensus().BlockChain(), api.node.GetBlockChain().ChainParams()).GetCurrentPowDiff(node, pow.MEERXKECCAKV1)
powNodes := api.node.GetBlockChain().GetCurrentPowDiff(node, pow.MEERXKECCAKV1)
ret := &json.InfoNodeResult{
ID: api.node.GetPeerServer().PeerID().String(),
Version: int32(1000000*version.Major + 10000*version.Minor + 100*version.Patch),
Expand Down
5 changes: 2 additions & 3 deletions services/mining/newblocktemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
s "github.com/Qitmeer/qng/core/serialization"
"github.com/Qitmeer/qng/core/types"
"github.com/Qitmeer/qng/core/types/pow"
"github.com/Qitmeer/qng/core/types/pow/difficultymanager"
"github.com/Qitmeer/qng/engine/txscript"
"github.com/Qitmeer/qng/log"
"github.com/Qitmeer/qng/meerdag"
Expand Down Expand Up @@ -506,7 +505,7 @@ mempool:
ts := MedianAdjustedTime(bc, timeSource)

//
reqCompactDifficulty, err := difficultymanager.NewDiffManager(bc.Consensus().BlockChain(), bc.ChainParams()).CalcNextRequiredDifficulty(ts, powType)
reqCompactDifficulty, err := bc.CalcNextRequiredDifficulty(ts, powType)
if err != nil {
return nil, miningRuleError(ErrGettingDifficulty, err.Error())
}
Expand Down Expand Up @@ -599,7 +598,7 @@ func UpdateBlockTime(msgBlock *types.Block, chain *blockchain.BlockChain, timeSo
// If running on a network that requires recalculating the difficulty,
// do so now.
if activeNetParams.ReduceMinDifficulty {
difficulty, err := difficultymanager.NewDiffManager(chain.Consensus().BlockChain(), chain.ChainParams()).CalcNextRequiredDifficulty(
difficulty, err := chain.CalcNextRequiredDifficulty(
newTimestamp, msgBlock.Header.Pow.GetPowType())
if err != nil {
return miningRuleError(ErrGettingDifficulty, err.Error())
Expand Down

0 comments on commit e209def

Please sign in to comment.