github.com/bigzoro/my_simplechain@v0.0.0-20240315012955-8ad0a2a29bb9/consensus/scrypt/api.go (about)

     1  // Copyright (c) 2019 Simplechain
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Lesser General Public License as published by
     5  // the Free Software Foundation, either version 3 of the License, or
     6  // (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    11  // GNU Lesser General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Lesser General Public License
    14  // along with this program. If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package scrypt
    17  
    18  import (
    19  	"errors"
    20  
    21  	"github.com/bigzoro/my_simplechain/common"
    22  	"github.com/bigzoro/my_simplechain/common/hexutil"
    23  	"github.com/bigzoro/my_simplechain/core/types"
    24  )
    25  
    26  var errScryptStopped = errors.New("scrypt stopped")
    27  
    28  // API exposes scrypt related methods for the RPC interface.
    29  type API struct {
    30  	powScrypt *PowScrypt // Make sure the mode of scrypt is normal.
    31  }
    32  
    33  // TODO: Scrypt work define
    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 boundary condition ("target"), 2^256/difficulty
    40  //	result[2] - hex encoded block number
    41  func (api *API) GetWork() ([3]string, error) {
    42  	if api.powScrypt.config.PowMode != ModeNormal && api.powScrypt.config.PowMode != ModeTest {
    43  		return [3]string{}, errors.New("not supported")
    44  	}
    45  
    46  	var (
    47  		workCh = make(chan [3]string, 1)
    48  		errc   = make(chan error, 1)
    49  	)
    50  
    51  	select {
    52  	case api.powScrypt.fetchWorkCh <- &sealWork{errc: errc, res: workCh}:
    53  	case <-api.powScrypt.exitCh:
    54  		return [3]string{}, errScryptStopped
    55  	}
    56  
    57  	select {
    58  	case work := <-workCh:
    59  		return work, nil
    60  	case err := <-errc:
    61  		return [3]string{}, err
    62  	}
    63  }
    64  
    65  // SubmitWork can be used by external miner to submit their POW solution.
    66  // It returns an indication if the work was accepted.
    67  // Note either an invalid solution, a stale work a non-existent work will return false.
    68  func (api *API) SubmitWork(nonce types.BlockNonce, hash, digest common.Hash) bool {
    69  	if api.powScrypt.config.PowMode != ModeNormal && api.powScrypt.config.PowMode != ModeTest {
    70  		return false
    71  	}
    72  
    73  	var errc = make(chan error, 1)
    74  
    75  	select {
    76  	case api.powScrypt.submitWorkCh <- &mineResult{
    77  		nonce:     nonce,
    78  		mixDigest: digest,
    79  		hash:      hash,
    80  		errc:      errc,
    81  	}:
    82  	case <-api.powScrypt.exitCh:
    83  		return false
    84  	}
    85  
    86  	err := <-errc
    87  	return err == nil
    88  }
    89  
    90  // SubmitHashrate can be used for remote miners to submit their hash rate.
    91  // This enables the node to report the combined hash rate of all miners
    92  // which submit work through this node.
    93  //
    94  // It accepts the miner hash rate and an identifier which must be unique
    95  // between nodes.
    96  func (api *API) SubmitHashRate(rate hexutil.Uint64, id common.Hash) bool {
    97  	if api.powScrypt.config.PowMode != ModeNormal && api.powScrypt.config.PowMode != ModeTest {
    98  		return false
    99  	}
   100  
   101  	var done = make(chan struct{}, 1)
   102  
   103  	select {
   104  	case api.powScrypt.submitRateCh <- &hashrate{done: done, rate: uint64(rate), id: id}:
   105  	case <-api.powScrypt.exitCh:
   106  		return false
   107  	}
   108  
   109  	// Block until hash rate submitted successfully.
   110  	<-done
   111  
   112  	return true
   113  }
   114  
   115  // GetHashrate returns the current hashrate for local CPU miner and remote miner.
   116  func (api *API) GetHashrate() uint64 {
   117  	return uint64(api.powScrypt.Hashrate())
   118  }