github.com/johnathanhowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/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 acesss 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  	// sovled.
    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  
    72  // The Miner interface provides access to mining features.
    73  type Miner interface {
    74  	BlockManager
    75  	CPUMiner
    76  	io.Closer
    77  }