Skip to content

Commit

Permalink
restrcut
Browse files Browse the repository at this point in the history
  • Loading branch information
frankcrypto committed Dec 7, 2023
1 parent 35dcd4a commit 49b08dc
Show file tree
Hide file tree
Showing 9 changed files with 331 additions and 256 deletions.
5 changes: 5 additions & 0 deletions consensus/model/block_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package model
import (
"github.com/Qitmeer/qng/common/hash"
"github.com/Qitmeer/qng/core/types"
"github.com/Qitmeer/qng/meerdag"
"github.com/Qitmeer/qng/params"
)

type BlockChain interface {
Expand All @@ -22,4 +24,7 @@ type BlockChain interface {
GetBlockById(id uint) Block
FetchBlockByHash(hash *hash.Hash) (*types.SerializedBlock, error)
GetBlockOrderByHash(hash *hash.Hash) (uint, error)
GetBlockHeader(ib meerdag.IBlock) *types.BlockHeader
BlockDAG() *meerdag.MeerDAG
ChainParams() *params.Params
}
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
}
8 changes: 5 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,7 @@ 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).CalcNextRequiredDifficulty(prevNode,
header.Timestamp, instance)
if err != nil {
return err
Expand Down
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.

40 changes: 40 additions & 0 deletions core/types/pow/difficultymanager/interface_difficultymanager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package difficultymanager

import (
"time"

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

const (
// MEER difficulty adjustment
DIFFICULTY_MODE_MEER = iota
// KASPAD difficulty adjustment
DIFFICULTY_MODE_KASPAD
)

// 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 meerdag.IBlock, newBlockTime time.Time, powInstance pow.IPow) (uint32, error)
}

func NewDiffManager(b model.BlockChain) DifficultyManager {
switch b.ChainParams().PowConfig.DifficultyMode {
case DIFFICULTY_MODE_KASPAD:
return &kaspadDiff{
b: b,
powMax: b.ChainParams().PowConfig.MeerXKeccakV1PowLimit,
difficultyAdjustmentWindowSize: int(b.ChainParams().WorkDiffWindowSize),
disableDifficultyAdjustment: false,
targetTimePerBlock: b.ChainParams().TargetTimePerBlock,
genesisBits: b.ChainParams().PowConfig.MeerXKeccakV1PowLimitBits,
}
}
return &meerDiff{
b: b,
}
}

This file was deleted.

Loading

0 comments on commit 49b08dc

Please sign in to comment.