github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/consensus/ethash/api.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:34</date> 10 //</624450074918785024> 11 12 13 package ethash 14 15 import ( 16 "errors" 17 18 "github.com/ethereum/go-ethereum/common" 19 "github.com/ethereum/go-ethereum/common/hexutil" 20 "github.com/ethereum/go-ethereum/core/types" 21 ) 22 23 var errEthashStopped = errors.New("ethash stopped") 24 25 //API为RPC接口公开与ethash相关的方法。 26 type API struct { 27 ethash *Ethash //确保ethash模式正常。 28 } 29 30 //GetWork返回外部矿工的工作包。 31 // 32 //工作包由3个字符串组成: 33 //结果[0]-32字节十六进制编码的当前块头POW哈希 34 //结果[1]-用于DAG的32字节十六进制编码种子哈希 35 //结果[2]-32字节十六进制编码边界条件(“目标”),2^256/难度 36 //结果[3]-十六进制编码的块号 37 func (api *API) GetWork() ([4]string, error) { 38 if api.ethash.config.PowMode != ModeNormal && api.ethash.config.PowMode != ModeTest { 39 return [4]string{}, errors.New("not supported") 40 } 41 42 var ( 43 workCh = make(chan [4]string, 1) 44 errc = make(chan error, 1) 45 ) 46 47 select { 48 case api.ethash.fetchWorkCh <- &sealWork{errc: errc, res: workCh}: 49 case <-api.ethash.exitCh: 50 return [4]string{}, errEthashStopped 51 } 52 53 select { 54 case work := <-workCh: 55 return work, nil 56 case err := <-errc: 57 return [4]string{}, err 58 } 59 } 60 61 //外部矿工可使用SubNetwork提交其POW解决方案。 62 //如果工作被接受,它会返回一个指示。 63 //注意:如果解决方案无效,则过时的工作不存在的工作将返回false。 64 func (api *API) SubmitWork(nonce types.BlockNonce, hash, digest common.Hash) bool { 65 if api.ethash.config.PowMode != ModeNormal && api.ethash.config.PowMode != ModeTest { 66 return false 67 } 68 69 var errc = make(chan error, 1) 70 71 select { 72 case api.ethash.submitWorkCh <- &mineResult{ 73 nonce: nonce, 74 mixDigest: digest, 75 hash: hash, 76 errc: errc, 77 }: 78 case <-api.ethash.exitCh: 79 return false 80 } 81 82 err := <-errc 83 return err == nil 84 } 85 86 //submit hash rate可用于远程矿工提交哈希率。 87 //这使节点能够报告所有矿工的组合哈希率 88 //通过这个节点提交工作。 89 // 90 //它接受矿工哈希率和标识符,该标识符必须是唯一的 91 //节点之间。 92 func (api *API) SubmitHashRate(rate hexutil.Uint64, id common.Hash) bool { 93 if api.ethash.config.PowMode != ModeNormal && api.ethash.config.PowMode != ModeTest { 94 return false 95 } 96 97 var done = make(chan struct{}, 1) 98 99 select { 100 case api.ethash.submitRateCh <- &hashrate{done: done, rate: uint64(rate), id: id}: 101 case <-api.ethash.exitCh: 102 return false 103 } 104 105 //阻止,直到哈希率成功提交。 106 <-done 107 108 return true 109 } 110 111 //GetHashrate返回本地CPU矿工和远程矿工的当前哈希率。 112 func (api *API) GetHashrate() uint64 { 113 return uint64(api.ethash.Hashrate()) 114 } 115