Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify difficulty adjustment #580

Merged
merged 15 commits into from
Dec 11, 2023
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
}
16 changes: 16 additions & 0 deletions consensus/model/difficulty_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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 {
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
}
13 changes: 9 additions & 4 deletions core/blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ package blockchain
import (
"container/list"
"fmt"
"sort"
"sync"
"time"

"github.com/Qitmeer/qng/common/hash"
"github.com/Qitmeer/qng/common/roughtime"
"github.com/Qitmeer/qng/common/system"
Expand All @@ -18,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 All @@ -28,9 +33,6 @@ import (
"github.com/Qitmeer/qng/params"
"github.com/Qitmeer/qng/services/progresslog"
"github.com/schollz/progressbar/v3"
"sort"
"sync"
"time"
)

const (
Expand Down Expand Up @@ -135,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 @@ -169,6 +172,8 @@ func (b *BlockChain) Init() error {
for _, v := range tips {
log.Info(fmt.Sprintf("hash=%s,order=%s,height=%d", v.GetHash(), meerdag.GetOrderLogStr(v.GetOrder()), v.GetHeight()))
}

b.difficultyManager = difficultymanager.NewDiffManager(b.Consensus().BlockChain(), b.params)
return nil
}

Expand Down
Loading
Loading