diff --git a/accept.go b/accept.go index f6b288fcb..e3087fff5 100644 --- a/accept.go +++ b/accept.go @@ -39,7 +39,8 @@ func (b *BlockChain) maybeAcceptBlock(block *btcutil.Block, fastAdd bool) error // Ensure the difficulty specified in the block header matches // the calculated difficulty based on the previous block and // difficulty retarget rules. - expectedDifficulty, err := b.calcNextRequiredDifficulty(prevNode, block) + expectedDifficulty, err := b.calcNextRequiredDifficulty(prevNode, + block.MsgBlock().Header.Timestamp) if err != nil { return err } diff --git a/difficulty.go b/difficulty.go index 77b9558cf..99168aea0 100644 --- a/difficulty.go +++ b/difficulty.go @@ -6,7 +6,6 @@ package btcchain import ( "fmt" - "github.com/conformal/btcutil" "github.com/conformal/btcwire" "math/big" "time" @@ -260,7 +259,10 @@ func (b *BlockChain) findPrevTestNetDifficulty(startNode *blockNode) (uint32, er // calcNextRequiredDifficulty calculates the required difficulty for the block // after the passed previous block node based on the difficulty retarget rules. -func (b *BlockChain) calcNextRequiredDifficulty(lastNode *blockNode, block *btcutil.Block) (uint32, error) { +// This function differs from the exported CalcNextRequiredDifficulty in that +// the exported version uses the current best chain as the previous block node +// while this function accepts any block node. +func (b *BlockChain) calcNextRequiredDifficulty(lastNode *blockNode, newBlockTime time.Time) (uint32, error) { // Choose the correct proof of work limit for the active network. powLimit := b.chainParams().PowLimit powLimitBits := b.chainParams().PowLimitBits @@ -284,7 +286,6 @@ func (b *BlockChain) calcNextRequiredDifficulty(lastNode *blockNode, block *btcu // Return minimum difficulty when more than twice the // desired amount of time needed to generate a block has // elapsed. - newBlockTime := block.MsgBlock().Header.Timestamp allowMinTime := lastNode.timestamp.Add(targetSpacing * 2) if newBlockTime.After(allowMinTime) { return powLimitBits, nil @@ -367,3 +368,12 @@ func (b *BlockChain) calcNextRequiredDifficulty(lastNode *blockNode, block *btcu return newTargetBits, nil } + +// 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 NOT safe for concurrent access. +func (b *BlockChain) CalcNextRequiredDifficulty(timestamp time.Time) (uint32, error) { + return b.calcNextRequiredDifficulty(b.bestChain, timestamp) +}