github.com/ebakus/go-ebakus@v1.0.5-0.20200520105415-dbccef9ec421/consensus/ethash/api.go (about)

     1  // Copyright 2019 The ebakus/go-ebakus Authors
     2  // This file is part of the ebakus/go-ebakus library.
     3  //
     4  // The ebakus/go-ebakus 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 ebakus/go-ebakus 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 ebakus/go-ebakus library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package ethash
    18  
    19  import (
    20  	"errors"
    21  
    22  	"github.com/ebakus/go-ebakus/common"
    23  	"github.com/ebakus/go-ebakus/common/hexutil"
    24  )
    25  
    26  var errEthashStopped = errors.New("ethash stopped")
    27  
    28  // API exposes ethash related methods for the RPC interface.
    29  type API struct {
    30  	ethash *Ethash // Make sure the mode of ethash is normal.
    31  }
    32  
    33  // GetWork returns a work package for external miner.
    34  //
    35  // The work package consists of 3 strings:
    36  //   result[0] - 32 bytes hex encoded current block header pow-hash
    37  //   result[1] - 32 bytes hex encoded seed hash used for DAG
    38  //   result[2] - 32 bytes hex encoded boundary condition ("target"), 2^256/difficulty
    39  //   result[3] - hex encoded block number
    40  func (api *API) GetWork() ([4]string, error) {
    41  	if api.ethash.config.PowMode != ModeNormal && api.ethash.config.PowMode != ModeTest {
    42  		return [4]string{}, errors.New("not supported")
    43  	}
    44  
    45  	var (
    46  		workCh = make(chan [4]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 [4]string{}, errEthashStopped
    54  	}
    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(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  		mixDigest: digest,
    77  		hash:      hash,
    78  		errc:      errc,
    79  	}:
    80  	case <-api.ethash.exitCh:
    81  		return false
    82  	}
    83  
    84  	err := <-errc
    85  	return err == nil
    86  }
    87  
    88  // SubmitHashrate can be used for remote miners to submit their hash rate.
    89  // This enables the node to report the combined hash rate of all miners
    90  // which submit work through this node.
    91  //
    92  // It accepts the miner hash rate and an identifier which must be unique
    93  // between nodes.
    94  func (api *API) SubmitHashRate(rate hexutil.Uint64, id common.Hash) bool {
    95  	if api.ethash.config.PowMode != ModeNormal && api.ethash.config.PowMode != ModeTest {
    96  		return false
    97  	}
    98  
    99  	var done = make(chan struct{}, 1)
   100  
   101  	select {
   102  	case api.ethash.submitRateCh <- &hashrate{done: done, rate: uint64(rate), id: id}:
   103  	case <-api.ethash.exitCh:
   104  		return false
   105  	}
   106  
   107  	// Block until hash rate submitted successfully.
   108  	<-done
   109  
   110  	return true
   111  }
   112  
   113  // GetHashrate returns the current hashrate for local CPU miner and remote miner.
   114  func (api *API) GetHashrate() uint64 {
   115  	return uint64(api.ethash.Hashrate())
   116  }