Skip to content

Commit

Permalink
multi: remove cached templates from block manager.
Browse files Browse the repository at this point in the history
this removes the old cached template fields and functions from the block manager and updates checkBlockForHiddenVotes to use generated
templates from the background template block generator.
  • Loading branch information
dnldd committed Nov 19, 2018
1 parent edf7161 commit 2bde01d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 67 deletions.
77 changes: 13 additions & 64 deletions blockmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,7 @@ type blockManager struct {
lotteryDataBroadcast map[chainhash.Hash]struct{}
lotteryDataBroadcastMutex sync.RWMutex

cachedCurrentTemplate *BlockTemplate
cachedParentTemplate *BlockTemplate
AggressiveMining bool
AggressiveMining bool

// The following fields are used to filter duplicate block announcements.
announcedBlockMtx sync.Mutex
Expand Down Expand Up @@ -700,17 +698,16 @@ func (b *blockManager) checkBlockForHiddenVotes(block *dcrutil.Block) {
// the parent template hasn't yet been updated, so we may
// need to use the current template.
var template *BlockTemplate
if b.cachedCurrentTemplate != nil {
if b.cachedCurrentTemplate.Height ==
block.Height() {
template = b.cachedCurrentTemplate
if b.server.bg != nil {
if b.server.bg.currentTemplate != nil {
if b.server.bg.currentTemplate.Height == block.Height() {
template = b.server.bg.currentTemplate
}
}
}
if template == nil &&
b.cachedParentTemplate != nil {
if b.cachedParentTemplate.Height ==
block.Height() {
template = b.cachedParentTemplate
if template == nil && b.server.bg.parentTemplate != nil {
if b.server.bg.parentTemplate.Height == block.Height() {
template = b.server.bg.parentTemplate
}
}
}

Expand Down Expand Up @@ -1616,26 +1613,6 @@ out:
case isCurrentMsg:
msg.reply <- b.current()

case getCurrentTemplateMsg:
cur := deepCopyBlockTemplate(b.cachedCurrentTemplate)
msg.reply <- getCurrentTemplateResponse{
Template: cur,
}

case setCurrentTemplateMsg:
b.cachedCurrentTemplate = deepCopyBlockTemplate(msg.Template)
msg.reply <- setCurrentTemplateResponse{}

case getParentTemplateMsg:
par := deepCopyBlockTemplate(b.cachedParentTemplate)
msg.reply <- getParentTemplateResponse{
Template: par,
}

case setParentTemplateMsg:
b.cachedParentTemplate = deepCopyBlockTemplate(msg.Template)
msg.reply <- setParentTemplateResponse{}

default:
bmgrLog.Warnf("Invalid message type in block "+
"handler: %T", msg)
Expand Down Expand Up @@ -2035,7 +2012,9 @@ func (b *blockManager) handleNotifyMsg(notification *blockchain.Notification) {

// Drop the associated mining template from the old chain, since it
// will be no longer valid.
b.cachedCurrentTemplate = nil
b.server.bg.templateMtx.Lock()
b.server.bg.currentTemplate = nil
b.server.bg.templateMtx.Unlock()
}
}

Expand Down Expand Up @@ -2329,36 +2308,6 @@ func (b *blockManager) TicketPoolValue() (dcrutil.Amount, error) {
return b.chain.TicketPoolValue()
}

// GetCurrentTemplate gets the current block template for mining.
func (b *blockManager) GetCurrentTemplate() *BlockTemplate {
reply := make(chan getCurrentTemplateResponse)
b.msgChan <- getCurrentTemplateMsg{reply: reply}
response := <-reply
return response.Template
}

// SetCurrentTemplate sets the current block template for mining.
func (b *blockManager) SetCurrentTemplate(bt *BlockTemplate) {
reply := make(chan setCurrentTemplateResponse)
b.msgChan <- setCurrentTemplateMsg{Template: bt, reply: reply}
<-reply
}

// GetParentTemplate gets the current parent block template for mining.
func (b *blockManager) GetParentTemplate() *BlockTemplate {
reply := make(chan getParentTemplateResponse)
b.msgChan <- getParentTemplateMsg{reply: reply}
response := <-reply
return response.Template
}

// SetParentTemplate sets the current parent block template for mining.
func (b *blockManager) SetParentTemplate(bt *BlockTemplate) {
reply := make(chan setParentTemplateResponse)
b.msgChan <- setParentTemplateMsg{Template: bt, reply: reply}
<-reply
}

// newBlockManager returns a new Decred block manager.
// Use Start to begin processing asynchronous block and inv updates.
func newBlockManager(s *server, indexManager blockchain.IndexManager, interrupt <-chan struct{}) (*blockManager, error) {
Expand Down
6 changes: 3 additions & 3 deletions mining.go
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ func deepCopyBlockTemplate(blockTemplate *BlockTemplate) *BlockTemplate {
func handleTooFewVoters(subsidyCache *blockchain.SubsidyCache, nextHeight int64, miningAddress dcrutil.Address, bm *blockManager) (*BlockTemplate, error) {
timeSource := bm.server.timeSource
stakeValidationHeight := bm.server.chainParams.StakeValidationHeight
curTemplate := bm.GetCurrentTemplate()
curTemplate := bm.server.bg.currentTemplate

// Check to see if we've fallen off the chain, for example if a
// reorganization had recently occurred. If this is the case,
Expand All @@ -783,8 +783,8 @@ func handleTooFewVoters(subsidyCache *blockchain.SubsidyCache, nextHeight int64,
&curTemplate.Block.Header.PrevBlock) {
minrLog.Debugf("Cached mining templates are no longer current, " +
"resetting.")
bm.SetCurrentTemplate(nil)
bm.SetParentTemplate(nil)
bm.server.bg.currentTemplate = nil
bm.server.bg.parentTemplate = nil
}
}

Expand Down

0 comments on commit 2bde01d

Please sign in to comment.