8 "github.com/vapor/config"
10 log "github.com/sirupsen/logrus"
12 "github.com/vapor/account"
13 "github.com/vapor/common"
14 "github.com/vapor/consensus"
15 engine "github.com/vapor/consensus/consensus"
16 "github.com/vapor/consensus/consensus/dpos"
17 "github.com/vapor/crypto"
18 "github.com/vapor/crypto/ed25519/chainkd"
19 "github.com/vapor/mining"
20 "github.com/vapor/protocol"
21 "github.com/vapor/protocol/bc"
22 "github.com/vapor/protocol/bc/types"
23 "github.com/vapor/protocol/vm/vmutil"
27 maxNonce = ^uint64(0) // 2^64 - 1
33 var ConsensusEngine engine.Engine
35 // Miner creates blocks and searches for proof-of-work values.
39 accountManager *account.Manager
40 txPool *protocol.TxPool
44 workerWg sync.WaitGroup
45 updateNumWorkers chan struct{}
47 newBlockCh chan *bc.Hash
48 Authoritys map[string]string
53 func NewMiner(c *protocol.Chain, accountManager *account.Manager, txPool *protocol.TxPool, newBlockCh chan *bc.Hash, engine engine.Engine) *Miner {
54 authoritys := make(map[string]string)
56 dpos, ok := engine.(*dpos.Dpos)
58 log.Error("Only the dpos engine was allowed")
61 dpos.Authorize(config.CommonConfig.Consensus.Dpos.Coinbase)
62 c.SetConsensusEngine(dpos)
63 ConsensusEngine = dpos
66 accountManager: accountManager,
68 numWorkers: defaultNumWorkers,
69 updateNumWorkers: make(chan struct{}),
70 newBlockCh: newBlockCh,
71 Authoritys: authoritys,
77 func (m *Miner) generateProof(block types.Block) (types.Proof, error) {
79 if consensus.ActiveNetParams.Signer == "" {
80 return types.Proof{}, errors.New("Signer is empty")
82 xPrv.UnmarshalText([]byte(consensus.ActiveNetParams.Signer))
83 sign := xPrv.Sign(block.BlockCommitment.TransactionsMerkleRoot.Bytes())
84 pubHash := crypto.Ripemd160(xPrv.XPub().PublicKey())
86 address, _ := common.NewPeginAddressWitnessScriptHash(pubHash, &consensus.ActiveNetParams)
87 control, err := vmutil.P2WPKHProgram([]byte(pubHash))
89 return types.Proof{}, err
91 return types.Proof{Sign: sign, ControlProgram: control, Address: address.ScriptAddress()}, nil
94 // generateBlocks is a worker that is controlled by the miningWorkerController.
95 // It is self contained in that it creates block templates and attempts to solve
96 // them while detecting when it is performing stale work and reacting
97 // accordingly by generating a new block template. When a block is solved, it
100 // It must be run as a goroutine.
101 func (m *Miner) generateBlocks(quit chan struct{}) {
102 ticker := time.NewTicker(time.Second * hashUpdateSecs)
113 engine, ok := m.engine.(*dpos.Dpos)
115 log.Error("Only the dpos engine was allowed")
119 header := m.chain.BestBlockHeader()
120 isSeal, err := engine.IsSealer(m.chain, header.Hash(), header, uint64(time.Now().Unix()))
122 log.WithFields(log.Fields{"module": module, "error": err}).Error("Determine whether seal is wrong")
128 block, err := mining.NewBlockTemplate1(m.chain, m.txPool, m.accountManager, m.engine)
130 log.Errorf("Mining: failed on create NewBlockTemplate: %v", err)
131 time.Sleep(3 * time.Second)
135 time.Sleep(3 * time.Second)
138 block, err = m.engine.Seal(m.chain, block)
140 log.Errorf("Seal, %v", err)
143 m.chain.SetConsensusEngine(m.engine)
144 if isOrphan, err := m.chain.ProcessBlock(block); err == nil {
145 log.WithFields(log.Fields{
146 "height": block.BlockHeader.Height,
147 "isOrphan": isOrphan,
148 "tx": len(block.Transactions),
149 }).Info("Miner processed block")
151 blockHash := block.Hash()
152 m.newBlockCh <- &blockHash
154 log.WithField("height", block.BlockHeader.Height).Errorf("Miner fail on ProcessBlock, %v", err)
157 time.Sleep(3 * time.Second)
163 // miningWorkerController launches the worker goroutines that are used to
164 // generate block templates and solve them. It also provides the ability to
165 // dynamically adjust the number of running worker goroutines.
167 // It must be run as a goroutine.
168 func (m *Miner) miningWorkerController() {
169 // launchWorkers groups common code to launch a specified number of
170 // workers for generating blocks.
171 var runningWorkers []chan struct{}
172 launchWorkers := func(numWorkers uint64) {
173 for i := uint64(0); i < numWorkers; i++ {
174 quit := make(chan struct{})
175 runningWorkers = append(runningWorkers, quit)
178 go m.generateBlocks(quit)
182 // Launch the current number of workers by default.
183 runningWorkers = make([]chan struct{}, 0, m.numWorkers)
184 launchWorkers(m.numWorkers)
189 // Update the number of running workers.
190 case <-m.updateNumWorkers:
192 numRunning := uint64(len(runningWorkers))
193 if m.numWorkers == numRunning {
198 if m.numWorkers > numRunning {
199 launchWorkers(m.numWorkers - numRunning)
203 // Signal the most recently created goroutines to exit.
204 for i := numRunning - 1; i >= m.numWorkers; i-- {
205 close(runningWorkers[i])
206 runningWorkers[i] = nil
207 runningWorkers = runningWorkers[:i]
211 for _, quit := range runningWorkers {
221 // Start begins the CPU mining process as well as the speed monitor used to
222 // track hashing metrics. Calling this function when the CPU miner has
223 // already been started will have no effect.
225 // This function is safe for concurrent access.
226 func (m *Miner) Start() {
230 // Nothing to do if the miner is already running
235 m.quit = make(chan struct{})
236 go m.miningWorkerController()
239 log.Infof("CPU miner started")
242 // Stop gracefully stops the mining process by signalling all workers, and the
243 // speed monitor to quit. Calling this function when the CPU miner has not
244 // already been started will have no effect.
246 // This function is safe for concurrent access.
247 func (m *Miner) Stop() {
251 // Nothing to do if the miner is not currently running
258 log.Info("CPU miner stopped")
261 // IsMining returns whether or not the CPU miner has been started and is
262 // therefore currenting mining.
264 // This function is safe for concurrent access.
265 func (m *Miner) IsMining() bool {
272 // SetNumWorkers sets the number of workers to create which solve blocks. Any
273 // negative values will cause a default number of workers to be used which is
274 // based on the number of processor cores in the system. A value of 0 will
275 // cause all CPU mining to be stopped.
277 // This function is safe for concurrent access.
278 func (m *Miner) SetNumWorkers(numWorkers int32) {
283 // Don't lock until after the first check since Stop does its own
288 // Use default if provided value is negative.
290 m.numWorkers = defaultNumWorkers
292 m.numWorkers = uint64(numWorkers)
295 // When the miner is already running, notify the controller about the
298 m.updateNumWorkers <- struct{}{}
302 // NumWorkers returns the number of workers which are running to solve blocks.
304 // This function is safe for concurrent access.
305 func (m *Miner) NumWorkers() int32 {
309 return int32(m.numWorkers)