Skip to content

Commit

Permalink
multi: add BgBlkTmplGenerator.
Browse files Browse the repository at this point in the history
BgBlkTmplGenerator represents the background process that
generates block templates and notifies all subscribed clients
on template regeneration. It generates new templates based
on mempool activity for vote and non-vote transactions and
the time elapsed since last template regeneration.

This also adds a template pool to the background block
generator for recreating submitted blocks.
  • Loading branch information
dnldd committed Nov 19, 2018
1 parent e875495 commit b2d2a8e
Show file tree
Hide file tree
Showing 8 changed files with 471 additions and 48 deletions.
6 changes: 6 additions & 0 deletions blockchain/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ func (b *BlockChain) processOrphans(hash *chainhash.Hash, flags BehaviorFlags) e
return nil
}

// WaitForChain blocks if the chain is processing a block or handling a reorg.
func (b *BlockChain) WaitForChain() {
b.chainLock.RLock()
b.chainLock.RUnlock()
}

// ProcessBlock is the main workhorse for handling insertion of new blocks into
// the block chain. It includes functionality such as rejecting duplicate
// blocks, ensuring blocks follow all rules, orphan handling, and insertion into
Expand Down
25 changes: 25 additions & 0 deletions blockmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ type headerNode struct {
// incoming blocks.
type blockManager struct {
server *server
g *BgBlkTmplGenerator
started int32
shutdown int32
chain *blockchain.BlockChain
Expand Down Expand Up @@ -1896,6 +1897,10 @@ func (b *blockManager) handleNotifyMsg(notification *blockchain.Notification) {
r.ntfnMgr.NotifyBlockConnected(block)
}

if b.server.bg != nil {
b.server.bg.handleConnectedBlock(block.Height())
}

// Stake tickets are spent or missed from the most recently connected block.
case blockchain.NTSpentAndMissedTickets:
tnd, ok := notification.Data.(*blockchain.TicketNotificationsData)
Expand Down Expand Up @@ -1987,6 +1992,14 @@ func (b *blockManager) handleNotifyMsg(notification *blockchain.Notification) {
handleDisconnectedBlockTxns(block.Transactions()[1:])
handleDisconnectedBlockTxns(block.STransactions())

if b.server.bg != nil {
b.server.bg.handleDisconnectedBlock(block.Height())
}

// Filter and update the rebroadcast inventory.
b.server.PruneRebroadcastInventory()

// Notify registered websocket clients.
if r := b.server.rpcServer; r != nil {
// Filter and update the rebroadcast inventory.
b.server.PruneRebroadcastInventory()
Expand All @@ -1995,6 +2008,18 @@ func (b *blockManager) handleNotifyMsg(notification *blockchain.Notification) {
r.ntfnMgr.NotifyBlockDisconnected(block)
}

// Chain reorganization has commenced.
case blockchain.NTChainReorgStarted:
if b.server.bg != nil {
b.server.bg.handleChainReorgStarted()
}

// Chain reorganization has concluded.
case blockchain.NTChainReorgDone:
if b.server.bg != nil {
b.server.bg.handleChainReorgDone()
}

// The blockchain is reorganizing.
case blockchain.NTReorganization:
rd, ok := notification.Data.(*blockchain.ReorganizationNtfnsData)
Expand Down
13 changes: 12 additions & 1 deletion mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ type Config struct {
// to use for indexing the unconfirmed transactions in the memory pool.
// This can be nil if the address index is not enabled.
ExistsAddrIndex *indexers.ExistsAddrIndex

// OnVoteReceived defines the function used to signal receiving a new
// vote in the mempool.
OnVoteReceived func(voteTx *wire.MsgTx)
}

// Policy houses the policy (configuration parameters) which is used to
Expand Down Expand Up @@ -645,6 +649,9 @@ func (mp *TxPool) RemoveDoubleSpends(tx *dcrutil.Tx) {
// This function MUST be called with the mempool lock held (for writes).
func (mp *TxPool) addTransaction(utxoView *blockchain.UtxoViewpoint,
tx *dcrutil.Tx, txType stake.TxType, height int64, fee int64) {
if txType == stake.TxTypeSSGen {
mp.cfg.OnVoteReceived(tx.MsgTx())
}

// Add the transaction to the pool and mark the referenced outpoints
// as spent by the pool.
Expand Down Expand Up @@ -1597,8 +1604,12 @@ func (mp *TxPool) LastUpdated() time.Time {
// New returns a new memory pool for validating and storing standalone
// transactions until they are mined into a block.
func New(cfg *Config) *TxPool {
cfgCopy := *cfg
if cfgCopy.OnVoteReceived == nil {
cfgCopy.OnVoteReceived = func(tx *wire.MsgTx) {}
}
return &TxPool{
cfg: *cfg,
cfg: cfgCopy,
pool: make(map[chainhash.Hash]*TxDesc),
orphans: make(map[chainhash.Hash]*dcrutil.Tx),
orphansByPrev: make(map[wire.OutPoint]map[chainhash.Hash]*dcrutil.Tx),
Expand Down
1 change: 1 addition & 0 deletions mempool/mempool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ func newPoolHarness(chainParams *chaincfg.Params) (*poolHarness, []spendableOutp
SigCache: nil,
AddrIndex: nil,
ExistsAddrIndex: nil,
OnVoteReceived: nil,
}),
}

Expand Down
Loading

0 comments on commit b2d2a8e

Please sign in to comment.