github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/api/proposer.go (about)

     1  package api
     2  
     3  import (
     4  	"errors"
     5  )
     6  
     7  func (a *API) setMining(in struct {
     8  	IsMining bool `json:"is_mining"`
     9  }) Response {
    10  	if in.IsMining {
    11  		if _, err := a.wallet.AccountMgr.GetMiningAddress(); err != nil {
    12  			return NewErrorResponse(errors.New("Mining address does not exist"))
    13  		}
    14  		return a.startMining()
    15  	}
    16  	return a.stopMining()
    17  }
    18  
    19  func (a *API) startMining() Response {
    20  	a.blockProposer.Start()
    21  	if !a.IsMining() {
    22  		return NewErrorResponse(errors.New("Failed to start mining"))
    23  	}
    24  	return NewSuccessResponse("")
    25  }
    26  
    27  func (a *API) stopMining() Response {
    28  	a.blockProposer.Stop()
    29  	if a.IsMining() {
    30  		return NewErrorResponse(errors.New("Failed to stop mining"))
    31  	}
    32  	return NewSuccessResponse("")
    33  }