Skip to content

Commit

Permalink
restruct difficulty modules
Browse files Browse the repository at this point in the history
  • Loading branch information
frankcrypto committed Dec 10, 2023
1 parent 34c8148 commit 248ddd6
Show file tree
Hide file tree
Showing 16 changed files with 371 additions and 283 deletions.
3 changes: 2 additions & 1 deletion consensus/consensus.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package consensus

import (
"sync"

"github.com/Qitmeer/qng/common/hash"
"github.com/Qitmeer/qng/config"
"github.com/Qitmeer/qng/consensus/model"
Expand All @@ -13,7 +15,6 @@ import (
"github.com/Qitmeer/qng/node/service"
"github.com/Qitmeer/qng/params"
"github.com/Qitmeer/qng/services/index"
"sync"
)

type consensus struct {
Expand Down
1 change: 1 addition & 0 deletions consensus/model/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ type Block interface {
GetOrder() uint
HasParents() bool
GetMainParent() uint
GetHeight() uint
}
2 changes: 2 additions & 0 deletions consensus/model/block_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ type BlockChain interface {
GetBlockOrderByHash(hash *hash.Hash) (uint, error)
GetBlockHeader(block Block) *types.BlockHeader
ForeachBlueBlocks(start Block, depth uint, powType pow.PowType, fn func(block Block, header *types.BlockHeader) error) error
ChainRLock()
ChainRUnlock()
}
11 changes: 11 additions & 0 deletions consensus/model/meer_dag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package model

import (
"github.com/Qitmeer/qng/common/hash"
"github.com/Qitmeer/qng/rpc/api"
)

type MeerDag interface {
RegisterAPIs(apis []api.API)
GetBlockIDByTxHash(txhash *hash.Hash) uint64
}
6 changes: 4 additions & 2 deletions core/blockchain/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ package blockchain
import (
"container/list"
"fmt"
"time"

"github.com/Qitmeer/qng/common/hash"
"github.com/Qitmeer/qng/consensus/model"
"github.com/Qitmeer/qng/core/blockchain/utxo"
"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"
"time"
)

// ProcessBlock is the main workhorse for handling insertion of new blocks into
Expand Down Expand Up @@ -153,7 +155,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(b.calcEasiestDifficulty(
requiredTarget := pow.CompactToBig(difficultymanager.NewDiffManager(b.consensus.BlockChain(), b.params).CalcEasiestDifficulty(
checkpointNode.Difficulty, duration, block.Block().Header.Pow))
currentTarget := pow.CompactToBig(blockHeader.Difficulty)
if !block.Block().Header.Pow.CompareDiff(currentTarget, requiredTarget) {
Expand Down
9 changes: 6 additions & 3 deletions core/blockchain/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
"encoding/binary"
"encoding/hex"
"fmt"
"math"
"time"

"github.com/Qitmeer/qng/common/hash"
"github.com/Qitmeer/qng/consensus/forks"
"github.com/Qitmeer/qng/consensus/model"
Expand All @@ -22,11 +25,10 @@ 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"
"math"
"time"
)

const (
Expand Down Expand Up @@ -758,7 +760,8 @@ func (b *BlockChain) checkBlockHeaderContext(block *types.SerializedBlock, prevN
// Ensure the difficulty specified in the block header matches
// the calculated difficulty based on the previous block and
// difficulty retarget rules.
expDiff, err := b.calcNextRequiredDifficulty(prevNode,

expDiff, err := difficultymanager.NewDiffManager(b.consensus.BlockChain(), b.params).RequiredDifficulty(prevNode,
header.Timestamp, instance)
if err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions core/types/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ const (
// MaxAmount is the maximum transaction amount allowed in atoms.
// TODO, relocate the coin related item to chain's params
MaxAmount = 21e6 * AtomsPerCoin

// MEER difficulty adjustment
DIFFICULTY_MODE_MEER = 0
// KASPAD difficulty adjustment
DIFFICULTY_MODE_KASPAD
)
4 changes: 3 additions & 1 deletion core/types/pow/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ type PowConfig struct {

//is init
init bool

DifficultyMode int
}

//global cache
// global cache
func GetPowConfig() *PowConfig {
if PowConfigInstance != nil {
return PowConfigInstance
Expand Down
77 changes: 0 additions & 77 deletions core/types/pow/difficultymanager/difficultymanager.go

This file was deleted.

39 changes: 39 additions & 0 deletions core/types/pow/difficultymanager/interface_difficultymanager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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 {
switch cfg.PowConfig.DifficultyMode {
case types.DIFFICULTY_MODE_KASPAD:
return &kaspadDiff{
b: b,
powMax: cfg.PowConfig.MeerXKeccakV1PowLimit,
difficultyAdjustmentWindowSize: int(cfg.WorkDiffWindowSize),
disableDifficultyAdjustment: false,
targetTimePerBlock: cfg.TargetTimePerBlock,
genesisBits: cfg.PowConfig.MeerXKeccakV1PowLimitBits,
cfg: cfg,
}
}
return &meerDiff{
b: b,
cfg: cfg,
}
}

This file was deleted.

Loading

0 comments on commit 248ddd6

Please sign in to comment.