From 1cba5c8fc0e39f9e65c5d0bb6f4750dfd1e3f02a Mon Sep 17 00:00:00 2001 From: Dave Collins <davec@conformal.com> Date: Tue, 23 Aug 2016 01:41:49 -0500 Subject: [PATCH] blockchain: Remove exported CalcPastTimeMedian func. This removes the exported CalcPastTimeMedian function from the blockchain package as it is no longer needed since the information is now available via the BestState snapshot. Also, update the only known caller of this, which is the chain state in block manager, to use the snapshot instead. In reality, now that everything the block manager chain state provides is available via the blockchain BestState snapshot, the entire thing can be removed, however that will be done in a separate to commit to keep the changes targeted. --- blockchain/chain.go | 14 +------------- blockmanager.go | 8 ++------ 2 files changed, 3 insertions(+), 19 deletions(-) diff --git a/blockchain/chain.go b/blockchain/chain.go index f838dd107..56717aaf4 100644 --- a/blockchain/chain.go +++ b/blockchain/chain.go @@ -134,7 +134,7 @@ type BestState struct { BlockSize uint64 // The size of the block. NumTxns uint64 // The number of txns in the block. TotalTxns uint64 // The total number of txns in the chain. - MedianTime time.Time // Median time as per CalcPastMedianTime. + MedianTime time.Time // Median time as per calcPastMedianTime. } // newBestState returns a new best stats instance for the given parameters. @@ -682,18 +682,6 @@ func (b *BlockChain) calcPastMedianTime(startNode *blockNode) (time.Time, error) return medianTimestamp, nil } -// CalcPastMedianTime calculates the median time of the previous few blocks -// prior to, and including, the end of the current best chain. It is primarily -// used to ensure new blocks have sane timestamps. -// -// This function is safe for concurrent access. -func (b *BlockChain) CalcPastMedianTime() (time.Time, error) { - b.chainLock.Lock() - defer b.chainLock.Unlock() - - return b.calcPastMedianTime(b.bestNode) -} - // getReorganizeNodes finds the fork point between the main chain and the passed // node and returns a list of block nodes that would need to be detached from // the main chain and a list of block nodes that would need to be attached to diff --git a/blockmanager.go b/blockmanager.go index 37967a621..16c57f309 100644 --- a/blockmanager.go +++ b/blockmanager.go @@ -213,12 +213,8 @@ func (b *blockManager) updateChainState(newestHash *chainhash.Hash, newestHeight b.chainState.newestHash = newestHash b.chainState.newestHeight = newestHeight - medianTime, err := b.chain.CalcPastMedianTime() - if err != nil { - b.chainState.pastMedianTimeErr = err - } else { - b.chainState.pastMedianTime = medianTime - } + b.chainState.pastMedianTime = b.chain.BestSnapshot().MedianTime + b.chainState.pastMedianTimeErr = nil } // findNextHeaderCheckpoint returns the next checkpoint after the passed height.