gitlab.com/jokerrs1/Sia@v1.3.2/modules/miner.go (about)

     1  package modules
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/NebulousLabs/Sia/types"
     7  )
     8  
     9  const (
    10  	// MinerDir is the name of the directory that is used to store the miner's
    11  	// persistent data.
    12  	MinerDir = "miner"
    13  )
    14  
    15  // BlockManager contains functions that can interface with external miners,
    16  // providing and receiving blocks that have experienced nonce grinding.
    17  type BlockManager interface {
    18  	// HeaderForWork returns a block header that can be grinded on and
    19  	// resubmitted to the miner. HeaderForWork() will remember the block that
    20  	// corresponds to the header for 50 calls.
    21  	HeaderForWork() (types.BlockHeader, types.Target, error)
    22  
    23  	// SubmitHeader takes a block header that has been worked on and has a
    24  	// valid target.
    25  	SubmitHeader(types.BlockHeader) error
    26  
    27  	// BlocksMined returns the number of blocks and stale blocks that have been
    28  	// mined using this miner.
    29  	BlocksMined() (goodBlocks, staleBlocks int)
    30  }
    31  
    32  // CPUMiner provides access to a single-threaded cpu miner.
    33  type CPUMiner interface {
    34  	// CPUHashrate returns the hashrate of the cpu miner in hashes per second.
    35  	CPUHashrate() int
    36  
    37  	// Mining returns true if the cpu miner is enabled, and false otherwise.
    38  	CPUMining() bool
    39  
    40  	// StartMining turns on the miner, which will endlessly work for new
    41  	// blocks.
    42  	StartCPUMining()
    43  
    44  	// StopMining turns off the miner, but keeps the same number of threads.
    45  	StopCPUMining()
    46  }
    47  
    48  // TestMiner provides direct access to block fetching, solving, and
    49  // manipulation. The primary use of this interface is integration testing.
    50  type TestMiner interface {
    51  	// AddBlock is an extension of FindBlock - AddBlock will submit the block
    52  	// after finding it.
    53  	AddBlock() (types.Block, error)
    54  
    55  	// BlockForWork returns a block that is ready for nonce grinding. All
    56  	// blocks returned by BlockForWork have a unique Merkle root, meaning that
    57  	// each can safely start from nonce 0.
    58  	BlockForWork() (types.Block, types.Target, error)
    59  
    60  	// FindBlock will have the miner make 1 attempt to find a solved block that
    61  	// builds on the current consensus set. It will give up after a few
    62  	// seconds, returning the block and a bool indicating whether the block is
    63  	// solved.
    64  	FindBlock() (types.Block, error)
    65  
    66  	// SolveBlock will have the miner make 1 attempt to solve the input block,
    67  	// which amounts to trying a few thousand different nonces. SolveBlock is
    68  	// primarily used for testing.
    69  	SolveBlock(types.Block, types.Target) (types.Block, bool)
    70  
    71  	// Needs to have all other miner functions in addition to shortcuts for
    72  	// mining blocks.
    73  	Miner
    74  }
    75  
    76  // The Miner interface provides access to mining features.
    77  type Miner interface {
    78  	BlockManager
    79  	CPUMiner
    80  	io.Closer
    81  }