github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/consensus/ethash/api.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2018 Go Ethereum作者
    10  //此文件是Go以太坊库的一部分。
    11  //
    12  //Go-Ethereum库是免费软件:您可以重新分发它和/或修改
    13  //根据GNU发布的较低通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊图书馆的发行目的是希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU较低的通用公共许可证,了解更多详细信息。
    21  //
    22  //你应该收到一份GNU较低级别的公共许可证副本
    23  //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  
    25  package ethash
    26  
    27  import (
    28  	"errors"
    29  
    30  	"github.com/ethereum/go-ethereum/common"
    31  	"github.com/ethereum/go-ethereum/common/hexutil"
    32  	"github.com/ethereum/go-ethereum/core/types"
    33  )
    34  
    35  var errEthashStopped = errors.New("ethash stopped")
    36  
    37  //API为RPC接口公开与ethash相关的方法。
    38  type API struct {
    39  ethash *Ethash //确保ethash模式正常。
    40  }
    41  
    42  //
    43  //
    44  //
    45  //结果[0]-32字节十六进制编码的当前块头POW哈希
    46  //结果[1]-用于DAG的32字节十六进制编码种子哈希
    47  //结果[2]-32字节十六进制编码边界条件(“目标”),2^256/难度
    48  func (api *API) GetWork() ([3]string, error) {
    49  	if api.ethash.config.PowMode != ModeNormal && api.ethash.config.PowMode != ModeTest {
    50  		return [3]string{}, errors.New("not supported")
    51  	}
    52  
    53  	var (
    54  		workCh = make(chan [3]string, 1)
    55  		errc   = make(chan error, 1)
    56  	)
    57  
    58  	select {
    59  	case api.ethash.fetchWorkCh <- &sealWork{errc: errc, res: workCh}:
    60  	case <-api.ethash.exitCh:
    61  		return [3]string{}, errEthashStopped
    62  	}
    63  
    64  	select {
    65  	case work := <-workCh:
    66  		return work, nil
    67  	case err := <-errc:
    68  		return [3]string{}, err
    69  	}
    70  }
    71  
    72  //
    73  //
    74  //注意:如果解决方案无效,则过时的工作不存在的工作将返回false。
    75  func (api *API) SubmitWork(nonce types.BlockNonce, hash, digest common.Hash) bool {
    76  	if api.ethash.config.PowMode != ModeNormal && api.ethash.config.PowMode != ModeTest {
    77  		return false
    78  	}
    79  
    80  	var errc = make(chan error, 1)
    81  
    82  	select {
    83  	case api.ethash.submitWorkCh <- &mineResult{
    84  		nonce:     nonce,
    85  		mixDigest: digest,
    86  		hash:      hash,
    87  		errc:      errc,
    88  	}:
    89  	case <-api.ethash.exitCh:
    90  		return false
    91  	}
    92  
    93  	err := <-errc
    94  	return err == nil
    95  }
    96  
    97  //submit hash rate可用于远程矿工提交哈希率。
    98  //这使节点能够报告所有矿工的组合哈希率
    99  //通过这个节点提交工作。
   100  //
   101  //它接受矿工哈希率和标识符,该标识符必须是唯一的
   102  //节点之间。
   103  func (api *API) SubmitHashRate(rate hexutil.Uint64, id common.Hash) bool {
   104  	if api.ethash.config.PowMode != ModeNormal && api.ethash.config.PowMode != ModeTest {
   105  		return false
   106  	}
   107  
   108  	var done = make(chan struct{}, 1)
   109  
   110  	select {
   111  	case api.ethash.submitRateCh <- &hashrate{done: done, rate: uint64(rate), id: id}:
   112  	case <-api.ethash.exitCh:
   113  		return false
   114  	}
   115  
   116  //
   117  	<-done
   118  
   119  	return true
   120  }
   121  
   122  //GetHashrate返回本地CPU矿工和远程矿工的当前哈希率。
   123  func (api *API) GetHashrate() uint64 {
   124  	return uint64(api.ethash.Hashrate())
   125  }