首页 > 编程知识 正文

以太坊矿机挖矿,以太坊挖矿软件源代码

时间:2023-05-04 17:31:45 阅读:188423 作者:4037

先看一下流程图

首先在你打开console时,系统会帮你做好一些准备,比如说new好miner,worker等

 

入口

可以看到,miner有6个方法,那么对应的源码中就会有6个api。

在eth包中api.go中,可以看到有以下6个api。

那么接下来,就让我们去仔细看一下这6个api的具体功能实现。

miner.start()//有一个参数,即挖矿所需协程数量func (api *PrivateMinerAPI) Start(threads *int) error {// Set the number of threads if the seal engine supports it//如果协程个数指针为空那么,那么就new一个if threads == nil {threads = new(int)//如果协程个数为0,那么就让就指定其为-1,接下来挖矿时就无法开启协程} else if *threads == 0 {*threads = -1 // Disable the miner from within}type threaded interface {SetThreads(threads int)}//断言if th, ok := api.e.engine.(threaded); ok {log.Info("Updated mining threads", "threads", *threads)th.SetThreads(*threads)}// Start the miner and return//开始挖矿并返回//如果没有在Miningif !api.e.IsMining() {// Propagate the initial price point to the transaction pool//上锁api.e.lock.RLock()//获取gaspriceprice := api.e.gasPrice//解锁api.e.lock.RUnlock()//设置阈值//SetGasPrice()通过交易池所需的最低价格,更新新的交易和降低所有交易到这个阈值及以下api.e.txPool.SetGasPrice(price)//开始挖矿,有错就返回,无错就进行。return api.e.StartMining(true)}return nil}

通过设置协程数来挖矿,并做一些所需工作,然后开始进行mining。

startmining(true)

func (s *Ethereum) StartMining(local bool) error {//获取挖矿账户地址eb, err := s.Etherbase()if err != nil {log.Error("Cannot start mining without etherbase", "err", err)return fmt.Errorf("etherbase missing: %v", err)}//断言engine是否PoA共识引擎if clique, ok := s.engine.(*clique.Clique); ok {//获取存有该账户的walletwallet, err := s.accountManager.Find(accounts.Account{Address: eb})if wallet == nil || err != nil {log.Error("Etherbase account unavailable locally", "err", err)return fmt.Errorf("signer missing: %v", err)}//向引擎中注入私钥,以形成新的块,其就是PoA共识做的前期准备clique.Authorize(eb, wallet.SignHash)}if local {// If local (CPU) mining is started, we can disable the transaction rejection//如果启动了本地(CPU)挖掘,我们可以禁用事务拒绝// mechanism introduced to speed sync times. CPU mining on mainnet is ludicrous//同步时间加快机制,cpu挖矿是荒谬的// so no one will ever hit this path, whereas marking sync done on CPU mining// will ensure that private networks work in single miner mode too.//所以没有人会走这条路,而在CPU挖掘上标记同步将确保专用网络也在单矿工模式下工作。atomic.StoreUint32(&s.protocolManager.acceptTxs, 1)}//开一条协程去挖矿//eb为矿工地址go s.miner.Start(eb)return nil}

miner.Start(eb)

func (self *Miner) Start(coinbase common.Address) {//赋值给变量新值,无论以前是什么值atomic.StoreInt32(&self.shouldStart, 1)//设置worker的coinbase属性self.worker.setEtherbase(coinbase)//设置miner的coinbase属性self.coinbase = coinbase//加载canStart指令,这个决定是否可以启动挖掘操作if atomic.LoadInt32(&self.canStart) == 0 {log.Info("Network syncing, will start miner afterwards")return}//赋值给变量新值,无论以前是什么值atomic.StoreInt32(&self.mining, 1)log.Info("Starting mining operation")//worker.startself.worker.start()self.worker.commitNewWork()}

worker.start() 

func (self *worker) start() {self.mu.Lock()defer self.mu.Unlock()atomic.StoreInt32(&self.mining, 1)// spin up agentsfor agent := range self.agents {agent.Start()}}

worker.commitnetwork()

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。