github.com/oskarth/go-ethereum@v1.6.8-0.20191013093314-dac24a9d3494/consensus/ethash/api.go (about) 1 // Copyright 2018 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 ethash 18 19 import ( 20 "errors" 21 22 "github.com/ethereum/go-ethereum/common" 23 "github.com/ethereum/go-ethereum/common/hexutil" 24 "github.com/ethereum/go-ethereum/core/types" 25 ) 26 27 var errEthashStopped = errors.New("ethash stopped") 28 29 // API exposes ethash related methods for the RPC interface. 30 type API struct { 31 ethash *Ethash // Make sure the mode of ethash is normal. 32 } 33 34 // GetWork returns a work package for external miner. 35 // 36 // The work package consists of 3 strings: 37 // result[0] - 32 bytes hex encoded current block header pow-hash 38 // result[1] - 32 bytes hex encoded seed hash used for DAG 39 // result[2] - 32 bytes hex encoded boundary condition ("target"), 2^256/difficulty 40 func (api *API) GetWork() ([3]string, error) { 41 if api.ethash.config.PowMode != ModeNormal && api.ethash.config.PowMode != ModeTest { 42 return [3]string{}, errors.New("not supported") 43 } 44 45 var ( 46 workCh = make(chan [3]string, 1) 47 errc = make(chan error, 1) 48 ) 49 50 select { 51 case api.ethash.fetchWorkCh <- &sealWork{errc: errc, res: workCh}: 52 case <-api.ethash.exitCh: 53 return [3]string{}, errEthashStopped 54 } 55 56 select { 57 case work := <-workCh: 58 return work, nil 59 case err := <-errc: 60 return [3]string{}, err 61 } 62 } 63 64 // SubmitWork can be used by external miner to submit their POW solution. 65 // It returns an indication if the work was accepted. 66 // Note either an invalid solution, a stale work a non-existent work will return false. 67 func (api *API) SubmitWork(nonce types.BlockNonce, hash, digest common.Hash) bool { 68 if api.ethash.config.PowMode != ModeNormal && api.ethash.config.PowMode != ModeTest { 69 return false 70 } 71 72 var errc = make(chan error, 1) 73 74 select { 75 case api.ethash.submitWorkCh <- &mineResult{ 76 nonce: nonce, 77 mixDigest: digest, 78 hash: hash, 79 errc: errc, 80 }: 81 case <-api.ethash.exitCh: 82 return false 83 } 84 85 err := <-errc 86 return err == nil 87 } 88 89 // SubmitHashrate can be used for remote miners to submit their hash rate. 90 // This enables the node to report the combined hash rate of all miners 91 // which submit work through this node. 92 // 93 // It accepts the miner hash rate and an identifier which must be unique 94 // between nodes. 95 func (api *API) SubmitHashRate(rate hexutil.Uint64, id common.Hash) bool { 96 if api.ethash.config.PowMode != ModeNormal && api.ethash.config.PowMode != ModeTest { 97 return false 98 } 99 100 var done = make(chan struct{}, 1) 101 102 select { 103 case api.ethash.submitRateCh <- &hashrate{done: done, rate: uint64(rate), id: id}: 104 case <-api.ethash.exitCh: 105 return false 106 } 107 108 // Block until hash rate submitted successfully. 109 <-done 110 111 return true 112 } 113 114 // GetHashrate returns the current hashrate for local CPU miner and remote miner. 115 func (api *API) GetHashrate() uint64 { 116 return uint64(api.ethash.Hashrate()) 117 }