8 log "github.com/sirupsen/logrus"
10 "github.com/vapor/account"
11 "github.com/vapor/config"
12 "github.com/vapor/consensus"
13 "github.com/vapor/event"
14 "github.com/vapor/proposal"
15 "github.com/vapor/protocol"
19 logModule = "blockproposer"
22 // BlockProposer propose several block in specified time range
23 type BlockProposer struct {
26 accountManager *account.Manager
27 txPool *protocol.TxPool
30 eventDispatcher *event.Dispatcher
33 // generateBlocks is a worker that is controlled by the proposeWorkerController.
34 // It is self contained in that it creates block templates and attempts to solve
35 // them while detecting when it is performing stale work and reacting
36 // accordingly by generating a new block template. When a block is verified, it
39 // It must be run as a goroutine.
40 func (b *BlockProposer) generateBlocks() {
41 xpub := config.CommonConfig.PrivateKey().XPub()
42 xpubStr := hex.EncodeToString(xpub[:])
43 ticker := time.NewTicker(consensus.BlockTimeInterval * time.Millisecond)
53 bestBlockHeader := b.chain.BestBlockHeader()
54 bestBlockHash := bestBlockHeader.Hash()
56 now := uint64(time.Now().UnixNano() / 1e6)
58 if now < bestBlockHeader.Timestamp {
59 base = bestBlockHeader.Timestamp
61 minTimeToNextBlock := consensus.BlockTimeInterval - base%consensus.BlockTimeInterval
62 nextBlockTime := base + minTimeToNextBlock
63 if (nextBlockTime - now) < consensus.BlockTimeInterval/10 {
64 nextBlockTime += consensus.BlockTimeInterval
67 isBlocker, err := b.chain.IsBlocker(&bestBlockHash, xpubStr, nextBlockTime)
69 log.WithFields(log.Fields{"module": logModule, "error": err, "pubKey": xpubStr}).Error("fail on check is next blocker")
77 block, err := proposal.NewBlockTemplate(b.chain, b.txPool, b.accountManager, nextBlockTime)
79 log.WithFields(log.Fields{"module": logModule, "error": err}).Error("failed on create NewBlockTemplate")
83 isOrphan, err := b.chain.ProcessBlock(block)
85 log.WithFields(log.Fields{"module": logModule, "height": block.BlockHeader.Height, "error": err}).Error("proposer fail on ProcessBlock")
89 log.WithFields(log.Fields{"module": logModule, "height": block.BlockHeader.Height, "isOrphan": isOrphan, "tx": len(block.Transactions)}).Info("proposer processed block")
90 // Broadcast the block and announce chain insertion event
91 if err = b.eventDispatcher.Post(event.NewProposedBlockEvent{Block: *block}); err != nil {
92 log.WithFields(log.Fields{"module": logModule, "height": block.BlockHeader.Height, "error": err}).Error("proposer fail on post block")
97 // Start begins the block propose process as well as the speed monitor used to
98 // track hashing metrics. Calling this function when the block proposer has
99 // already been started will have no effect.
101 // This function is safe for concurrent access.
102 func (b *BlockProposer) Start() {
106 // Nothing to do if the miner is already running
111 b.quit = make(chan struct{})
112 go b.generateBlocks()
115 log.Infof("block proposer started")
118 // Stop gracefully stops the proposal process by signalling all workers, and the
119 // speed monitor to quit. Calling this function when the block proposer has not
120 // already been started will have no effect.
122 // This function is safe for concurrent access.
123 func (b *BlockProposer) Stop() {
127 // Nothing to do if the miner is not currently running
134 log.Info("block proposer stopped")
137 // IsProposing returns whether the block proposer has been started.
139 // This function is safe for concurrent access.
140 func (b *BlockProposer) IsProposing() bool {
147 // NewBlockProposer returns a new instance of a block proposer for the provided configuration.
148 // Use Start to begin the proposal process. See the documentation for BlockProposer
149 // type for more details.
150 func NewBlockProposer(c *protocol.Chain, accountManager *account.Manager, txPool *protocol.TxPool, dispatcher *event.Dispatcher) *BlockProposer {
151 return &BlockProposer{
153 accountManager: accountManager,
155 eventDispatcher: dispatcher,