github.com/dominant-strategies/go-quai@v0.28.2/consensus/blake3pow/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 blake3pow 18 19 import ( 20 "errors" 21 22 "github.com/dominant-strategies/go-quai/common" 23 "github.com/dominant-strategies/go-quai/common/hexutil" 24 "github.com/dominant-strategies/go-quai/core/types" 25 ) 26 27 var errBlake3powStopped = errors.New("blake3pow stopped") 28 29 // API exposes blake3pow related methods for the RPC interface. 30 type API struct { 31 blake3pow *Blake3pow 32 } 33 34 // GetWork returns a work package for external miner. 35 // 36 // The work package consists of 3 strings: 37 // 38 // result[0] - 32 bytes hex encoded current block header pow-hash 39 // result[1] - 32 bytes hex encoded seed hash used for DAG 40 // result[2] - 32 bytes hex encoded boundary condition ("target"), 2^256/difficulty 41 // result[3] - hex encoded block number 42 func (api *API) GetWork() ([4]string, error) { 43 if api.blake3pow.remote == nil { 44 return [4]string{}, errors.New("not supported") 45 } 46 47 var ( 48 workCh = make(chan [4]string, 1) 49 errc = make(chan error, 1) 50 ) 51 select { 52 case api.blake3pow.remote.fetchWorkCh <- &sealWork{errc: errc, res: workCh}: 53 case <-api.blake3pow.remote.exitCh: 54 return [4]string{}, errBlake3powStopped 55 } 56 select { 57 case work := <-workCh: 58 return work, nil 59 case err := <-errc: 60 return [4]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.blake3pow.remote == nil { 69 return false 70 } 71 72 var errc = make(chan error, 1) 73 select { 74 case api.blake3pow.remote.submitWorkCh <- &mineResult{ 75 nonce: nonce, 76 hash: hash, 77 errc: errc, 78 }: 79 case <-api.blake3pow.remote.exitCh: 80 return false 81 } 82 err := <-errc 83 return err == nil 84 } 85 86 // SubmitHashrate can be used for remote miners to submit their hash rate. 87 // This enables the node to report the combined hash rate of all miners 88 // which submit work through this node. 89 // 90 // It accepts the miner hash rate and an identifier which must be unique 91 // between nodes. 92 func (api *API) SubmitHashrate(rate hexutil.Uint64, id common.Hash) bool { 93 if api.blake3pow.remote == nil { 94 return false 95 } 96 97 var done = make(chan struct{}, 1) 98 select { 99 case api.blake3pow.remote.submitRateCh <- &hashrate{done: done, rate: uint64(rate), id: id}: 100 case <-api.blake3pow.remote.exitCh: 101 return false 102 } 103 104 // Block until hash rate submitted successfully. 105 <-done 106 return true 107 } 108 109 // Gblake3powrate returns the current hashrate for local CPU miner and remote miner. 110 func (api *API) Gblake3powrate() uint64 { 111 return uint64(api.blake3pow.Hashrate()) 112 }