-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
frankcrypto
committed
Dec 11, 2023
1 parent
149a9a7
commit e209def
Showing
8 changed files
with
75 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 1 addition & 14 deletions
15
...ltymanager/interface_difficultymanager.go → ...ow/difficultymanager/difficultymanager.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters