From b041971ca88d3d61106f6b5f6006201b89cda93a Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Sun, 2 Mar 2014 12:17:36 -0600 Subject: [PATCH] Export CalcNextRequiredDifficutly function. This commit exports a new variant of the existing internal calcNextRequiredDifficulty function which calculates and returns the next required difficulty for a block after the end of the current best chain based on the difficulty retarget rules. In order to support the exported function the internal one was slightly modified to accept the block timestamp instead of an entire block since the timestamp is all that is needed and the caller of the exported function might next have a full block yet. --- accept.go | 3 ++- difficulty.go | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) 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) +}