github.com/hhwill/poc-eth@v0.0.0-20240218063348-3bb107c90dbf/consensus/ethpoc/api.go (about)

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