github.com/theQRL/go-zond@v0.1.1/zond/api_miner.go (about) 1 // Copyright 2023 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package zond 18 19 import ( 20 "math/big" 21 "time" 22 23 "github.com/theQRL/go-zond/common" 24 "github.com/theQRL/go-zond/common/hexutil" 25 ) 26 27 // MinerAPI provides an API to control the miner. 28 type MinerAPI struct { 29 e *Ethereum 30 } 31 32 // NewMinerAPI create a new MinerAPI instance. 33 func NewMinerAPI(e *Ethereum) *MinerAPI { 34 return &MinerAPI{e} 35 } 36 37 // Start starts the miner with the given number of threads. If threads is nil, 38 // the number of workers started is equal to the number of logical CPUs that are 39 // usable by this process. If mining is already running, this method adjust the 40 // number of threads allowed to use and updates the minimum price required by the 41 // transaction pool. 42 func (api *MinerAPI) Start() error { 43 return api.e.StartMining() 44 } 45 46 // Stop terminates the miner, both at the consensus engine level as well as at 47 // the block creation level. 48 func (api *MinerAPI) Stop() { 49 api.e.StopMining() 50 } 51 52 // SetExtra sets the extra data string that is included when this miner mines a block. 53 func (api *MinerAPI) SetExtra(extra string) (bool, error) { 54 if err := api.e.Miner().SetExtra([]byte(extra)); err != nil { 55 return false, err 56 } 57 return true, nil 58 } 59 60 // SetGasPrice sets the minimum accepted gas price for the miner. 61 func (api *MinerAPI) SetGasPrice(gasPrice hexutil.Big) bool { 62 api.e.lock.Lock() 63 api.e.gasPrice = (*big.Int)(&gasPrice) 64 api.e.lock.Unlock() 65 66 api.e.txPool.SetGasTip((*big.Int)(&gasPrice)) 67 return true 68 } 69 70 // SetGasLimit sets the gaslimit to target towards during mining. 71 func (api *MinerAPI) SetGasLimit(gasLimit hexutil.Uint64) bool { 72 api.e.Miner().SetGasCeil(uint64(gasLimit)) 73 return true 74 } 75 76 // SetEtherbase sets the etherbase of the miner. 77 func (api *MinerAPI) SetEtherbase(etherbase common.Address) bool { 78 api.e.SetEtherbase(etherbase) 79 return true 80 } 81 82 // SetRecommitInterval updates the interval for miner sealing work recommitting. 83 func (api *MinerAPI) SetRecommitInterval(interval int) { 84 api.e.Miner().SetRecommitInterval(time.Duration(interval) * time.Millisecond) 85 }