github.com/core-coin/go-core/v2@v2.1.9/consensus/cryptore/api.go (about)

     1  // Copyright 2023 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package cryptore
    18  
    19  import (
    20  	"errors"
    21  
    22  	"github.com/core-coin/go-core/v2/common"
    23  	"github.com/core-coin/go-core/v2/common/hexutil"
    24  	"github.com/core-coin/go-core/v2/core/types"
    25  )
    26  
    27  var errCryptoreStopped = errors.New("cryptore stopped")
    28  
    29  // API exposes cryptore related methods for the RPC interface.
    30  type API struct {
    31  	cryptore *Cryptore
    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[2] - 32 bytes hex encoded boundary condition ("target"), 2^256/difficulty
    40  //	result[3] - hex encoded block number
    41  func (api *API) GetWork() ([4]string, error) {
    42  	if api.cryptore.remote == nil {
    43  		return [4]string{}, errors.New("not supported")
    44  	}
    45  
    46  	var (
    47  		workCh = make(chan [4]string, 1)
    48  		errc   = make(chan error, 1)
    49  	)
    50  	select {
    51  	case api.cryptore.remote.fetchWorkCh <- &sealWork{errc: errc, res: workCh}:
    52  	case <-api.cryptore.remote.exitCh:
    53  		return [4]string{}, errCryptoreStopped
    54  	}
    55  	select {
    56  	case work := <-workCh:
    57  		return work, nil
    58  	case err := <-errc:
    59  		return [4]string{}, err
    60  	}
    61  }
    62  
    63  // SubmitWork can be used by external miner to submit their POW solution.
    64  // It returns an indication if the work was accepted.
    65  // Note either an invalid solution, a stale work a non-existent work will return false.
    66  func (api *API) SubmitWork(nonce types.BlockNonce, hash common.Hash) bool {
    67  	if api.cryptore.remote == nil {
    68  		return false
    69  	}
    70  
    71  	var errc = make(chan error, 1)
    72  	select {
    73  	case api.cryptore.remote.submitWorkCh <- &mineResult{
    74  		nonce: nonce,
    75  		hash:  hash,
    76  		errc:  errc,
    77  	}:
    78  	case <-api.cryptore.remote.exitCh:
    79  		return false
    80  	}
    81  	err := <-errc
    82  	return err == nil
    83  }
    84  
    85  // SubmitHashrate can be used for remote miners to submit their hash rate.
    86  // This enables the node to report the combined hash rate of all miners
    87  // which submit work through this node.
    88  //
    89  // It accepts the miner hash rate and an identifier which must be unique
    90  // between nodes.
    91  func (api *API) SubmitHashRate(rate hexutil.Uint64, id common.Hash) bool {
    92  	if api.cryptore.remote == nil {
    93  		return false
    94  	}
    95  
    96  	var done = make(chan struct{}, 1)
    97  	select {
    98  	case api.cryptore.remote.submitRateCh <- &hashrate{done: done, rate: uint64(rate), id: id}:
    99  	case <-api.cryptore.remote.exitCh:
   100  		return false
   101  	}
   102  
   103  	// Block until hash rate submitted successfully.
   104  	<-done
   105  	return true
   106  }
   107  
   108  // GetHashrate returns the current hashrate for local CPU miner and remote miner.
   109  func (api *API) GetHashrate() uint64 {
   110  	return uint64(api.cryptore.Hashrate())
   111  }