Skip to content

Commit

Permalink
multi: Move update blk time to blk templ generator.
Browse files Browse the repository at this point in the history
This moves the UpdateBlockTime function to the recently introduced
BlkTmplGenerator type in order to break its dependence on the block
manager.
  • Loading branch information
davecgh committed Sep 14, 2018
1 parent 79da9e6 commit 2dcfb0a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cpuminer.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func (m *CPUMiner) solveBlock(msgBlock *wire.MsgBlock, ticker *time.Ticker, quit
return false
}

err = UpdateBlockTime(msgBlock, m.g.blockManager)
err = m.g.UpdateBlockTime(header)
if err != nil {
minrLog.Warnf("CPU miner unable to update block template "+
"time: %v", err)
Expand Down
12 changes: 5 additions & 7 deletions mining.go
Original file line number Diff line number Diff line change
Expand Up @@ -2036,23 +2036,21 @@ mempoolLoop:
// consensus rules. Finally, it will update the target difficulty if needed
// based on the new time for the test networks since their target difficulty can
// change based upon time.
func UpdateBlockTime(msgBlock *wire.MsgBlock, bManager *blockManager) error {
func (g *BlkTmplGenerator) UpdateBlockTime(header *wire.BlockHeader) error {
// The new timestamp is potentially adjusted to ensure it comes after
// the median time of the last several blocks per the chain consensus
// rules.
newTimestamp := medianAdjustedTime(bManager.chain.BestSnapshot(),
bManager.server.timeSource)
msgBlock.Header.Timestamp = newTimestamp
newTimestamp := medianAdjustedTime(g.chain.BestSnapshot(), g.timeSource)
header.Timestamp = newTimestamp

// If running on a network that requires recalculating the difficulty,
// do so now.
if activeNetParams.ReduceMinDifficulty {
difficulty, err := bManager.chain.CalcNextRequiredDifficulty(
newTimestamp)
difficulty, err := g.chain.CalcNextRequiredDifficulty(newTimestamp)
if err != nil {
return miningRuleError(ErrGettingDifficulty, err.Error())
}
msgBlock.Header.Bits = difficulty
header.Bits = difficulty
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2300,7 +2300,7 @@ func (state *gbtWorkState) updateBlockTemplate(s *rpcServer, useCoinbaseValue bo
// Update the time of the block template to the current time
// while accounting for the median time of the past several
// blocks per the chain consensus rules.
err := UpdateBlockTime(msgBlock, s.server.blockManager)
err := s.generator.UpdateBlockTime(&msgBlock.Header)
if err != nil {
context := "Failed to update timestamp"
return rpcInternalError(err.Error(), context)
Expand Down Expand Up @@ -3970,7 +3970,7 @@ func handleGetWorkRequest(s *rpcServer) (interface{}, error) {
// Update the time of the block template to the current time
// while accounting for the median time of the past several
// blocks per the chain consensus rules.
err := UpdateBlockTime(msgBlock, s.server.blockManager)
err := s.generator.UpdateBlockTime(&msgBlock.Header)
if err != nil {
return nil, rpcInternalError(err.Error(),
"Failed to update block time")
Expand Down

0 comments on commit 2dcfb0a

Please sign in to comment.