diff --git a/cpuminer.go b/cpuminer.go index f1150c51..116b0d01 100644 --- a/cpuminer.go +++ b/cpuminer.go @@ -15,6 +15,7 @@ import ( "github.com/decred/dcrd/blockchain" "github.com/decred/dcrd/chaincfg" "github.com/decred/dcrd/chaincfg/chainhash" + "github.com/decred/dcrd/mining" "github.com/decred/dcrd/wire" "github.com/decred/dcrutil" ) @@ -59,8 +60,8 @@ var ( // system which is typically sufficient. type CPUMiner struct { sync.Mutex - policy *miningPolicy - txSource TxSource + policy *mining.Policy + txSource mining.TxSource server *server numWorkers uint32 started bool @@ -649,7 +650,7 @@ func (m *CPUMiner) GenerateNBlocks(n uint32) ([]*chainhash.Hash, error) { // newCPUMiner returns a new instance of a CPU miner for the provided server. // Use Start to begin the mining process. See the documentation for CPUMiner // type for more details. -func newCPUMiner(policy *miningPolicy, s *server) *CPUMiner { +func newCPUMiner(policy *mining.Policy, s *server) *CPUMiner { return &CPUMiner{ policy: policy, txSource: s.txMemPool, diff --git a/mempool.go b/mempool.go index 9b45adda..5432198d 100644 --- a/mempool.go +++ b/mempool.go @@ -21,6 +21,7 @@ import ( "github.com/decred/dcrd/chaincfg" "github.com/decred/dcrd/chaincfg/chainhash" "github.com/decred/dcrd/database" + "github.com/decred/dcrd/mining" "github.com/decred/dcrd/txscript" "github.com/decred/dcrd/wire" "github.com/decred/dcrutil" @@ -85,7 +86,7 @@ type VoteTx struct { // mempoolTxDesc is a descriptor containing a transaction in the mempool along // with additional metadata. type mempoolTxDesc struct { - miningTxDesc + mining.TxDesc // StartingPriority is the priority of the transaction when it was added // to the pool. @@ -399,7 +400,7 @@ func (mp *txMemPool) SortParentsByVotes(currentTopBlock chainhash.Hash, } // Ensure the txMemPool type implements the mining.TxSource interface. -var _ TxSource = (*txMemPool)(nil) +var _ mining.TxSource = (*txMemPool)(nil) // removeOrphan is the internal function which implements the public // RemoveOrphan. See the comment for RemoveOrphan for more details. @@ -708,7 +709,7 @@ func (mp *txMemPool) addTransaction(txStore blockchain.TxStore, tx *dcrutil.Tx, // Add the transaction to the pool and mark the referenced outpoints // as spent by the pool. mp.pool[*tx.Sha()] = &mempoolTxDesc{ - miningTxDesc: miningTxDesc{ + TxDesc: mining.TxDesc{ Tx: tx, Type: txType, Added: time.Now(), @@ -1735,16 +1736,16 @@ func (mp *txMemPool) TxDescs() []*mempoolTxDesc { // MiningDescs returns a slice of mining descriptors for all the transactions // in the pool. // -// This is part of the TxSource interface implementation and is safe for +// This is part of the mining.TxSource interface implementation and is safe for // concurrent access as required by the interface contract. -func (mp *txMemPool) MiningDescs() []*miningTxDesc { +func (mp *txMemPool) MiningDescs() []*mining.TxDesc { mp.RLock() defer mp.RUnlock() - descs := make([]*miningTxDesc, len(mp.pool)) + descs := make([]*mining.TxDesc, len(mp.pool)) i := 0 for _, desc := range mp.pool { - descs[i] = &desc.miningTxDesc + descs[i] = &desc.TxDesc i++ } diff --git a/mining.go b/mining.go index 01ab6464..4bd7f3bf 100644 --- a/mining.go +++ b/mining.go @@ -18,6 +18,7 @@ import ( "github.com/decred/dcrd/chaincfg" "github.com/decred/dcrd/chaincfg/chainhash" "github.com/decred/dcrd/database" + "github.com/decred/dcrd/mining" "github.com/decred/dcrd/txscript" "github.com/decred/dcrd/wire" "github.com/decred/dcrutil" @@ -40,67 +41,6 @@ const ( kilobyte = 1000 ) -// miningTxDesc is a descriptor about a transaction in a transaction source -// along with additional metadata. -type miningTxDesc struct { - // Tx is the transaction associated with the entry. - Tx *dcrutil.Tx - - // Type is the type of the transaction associated with the entry. - Type stake.TxType - - // Added is the time when the entry was added to the source pool. - Added time.Time - - // Height is the block height when the entry was added to the the source - // pool. - Height int64 - - // Fee is the total fee the transaction associated with the entry pays. - Fee int64 -} - -// TxSource represents a source of transactions to consider for inclusion in -// new blocks. -// -// The interface contract requires that all of these methods are safe for -// concurrent access with respect to the source. -type TxSource interface { - // LastUpdated returns the last time a transaction was added to or - // removed from the source pool. - LastUpdated() time.Time - - // MiningDescs returns a slice of mining descriptors for all the - // transactions in the source pool. - MiningDescs() []*miningTxDesc - - // HaveTransaction returns whether or not the passed transaction hash - // exists in the source pool. - HaveTransaction(hash *chainhash.Hash) bool -} - -// miningPolicy houses the policy (configuration parameters) which is used to -// control the generation of block templates. See the documentation for -// NewBlockTemplate for more details on each of these parameters are used. -type miningPolicy struct { - // BlockMinSize is the minimum block size in bytes to be used when - // generating a block template. - BlockMinSize uint32 - - // BlockMaxSize is the maximum block size in bytes to be used when - // generating a block template. - BlockMaxSize uint32 - - // BlockPrioritySize is the size in bytes for high-priority / low-fee - // transactions to be used when generating a block template. - BlockPrioritySize uint32 - - // TxMinFreeFee is the minimum fee in Atoms/kB that is required for a - // transaction to be treated as free for mining purposes (block template - // generation). This value is in Atoms/1000 bytes. - TxMinFreeFee dcrutil.Amount -} - // txPrioItem houses a transaction along with extra information that allows the // transaction to be prioritized and track dependencies on other transactions // which have not been mined into a block yet. @@ -1167,14 +1107,14 @@ func handleCreatedBlockTemplate(blockTemplate *BlockTemplate, // // This function returns nil, nil if there are not enough voters on any of // the current top blocks to create a new block template. -func NewBlockTemplate(policy *miningPolicy, server *server, +func NewBlockTemplate(policy *mining.Policy, server *server, payToAddress dcrutil.Address) (*BlockTemplate, error) { // TODO: The mempool should be completely separated via the TxSource // interface so this function is fully decoupled. mempool := server.txMemPool - var txSource TxSource = server.txMemPool + var txSource mining.TxSource = server.txMemPool blockManager := server.blockManager timeSource := server.timeSource chainState := &blockManager.chainState diff --git a/mining/README.md b/mining/README.md new file mode 100644 index 00000000..b938d14c --- /dev/null +++ b/mining/README.md @@ -0,0 +1,23 @@ +mining +====== + +[![Build Status](http://img.shields.io/travis/decred/dcrd.svg)] +(https://travis-ci.org/decred/dcrd) [![ISC License] +(http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org) +[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg)] +(http://godoc.org/github.com/decred/dcrd/mining) + +## Overview + +This package is currently a work in progress. + +## Installation and Updating + +```bash +$ go get -u github.com/decred/dcrd/mining +``` + +## License + +Package mining is licensed under the [copyfree](http://copyfree.org) ISC +License. diff --git a/mining/mining.go b/mining/mining.go new file mode 100644 index 00000000..fcc395b8 --- /dev/null +++ b/mining/mining.go @@ -0,0 +1,53 @@ +// Copyright (c) 2014-2015 The btcsuite developers +// Copyright (c) 2016 The Decred developers +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. + +package mining + +import ( + "time" + + "github.com/decred/dcrd/blockchain/stake" + "github.com/decred/dcrd/chaincfg/chainhash" + "github.com/decred/dcrutil" +) + +// TxDesc is a descriptor about a transaction in a transaction source along with +// additional metadata. +type TxDesc struct { + // Tx is the transaction associated with the entry. + Tx *dcrutil.Tx + + // Type is the type of the transaction associated with the entry. + Type stake.TxType + + // Added is the time when the entry was added to the source pool. + Added time.Time + + // Height is the block height when the entry was added to the the source + // pool. + Height int64 + + // Fee is the total fee the transaction associated with the entry pays. + Fee int64 +} + +// TxSource represents a source of transactions to consider for inclusion in +// new blocks. +// +// The interface contract requires that all of these methods are safe for +// concurrent access with respect to the source. +type TxSource interface { + // LastUpdated returns the last time a transaction was added to or + // removed from the source pool. + LastUpdated() time.Time + + // MiningDescs returns a slice of mining descriptors for all the + // transactions in the source pool. + MiningDescs() []*TxDesc + + // HaveTransaction returns whether or not the passed transaction hash + // exists in the source pool. + HaveTransaction(hash *chainhash.Hash) bool +} diff --git a/mining/policy.go b/mining/policy.go new file mode 100644 index 00000000..7b86f11a --- /dev/null +++ b/mining/policy.go @@ -0,0 +1,30 @@ +// Copyright (c) 2014-2015 The btcsuite developers +// Copyright (c) 2016 The Decred developers +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. + +package mining + +import "github.com/decred/dcrutil" + +// Policy houses the policy (configuration parameters) which is used to control +// the generation of block templates. See the documentation for +// NewBlockTemplate for more details on each of these parameters are used. +type Policy struct { + // BlockMinSize is the minimum block size in bytes to be used when + // generating a block template. + BlockMinSize uint32 + + // BlockMaxSize is the maximum block size in bytes to be used when + // generating a block template. + BlockMaxSize uint32 + + // BlockPrioritySize is the size in bytes for high-priority / low-fee + // transactions to be used when generating a block template. + BlockPrioritySize uint32 + + // TxMinFreeFee is the minimum fee in Atoms/1000 bytes that is + // required for a transaction to be treated as free for mining purposes + // (block template generation). + TxMinFreeFee dcrutil.Amount +} diff --git a/rpcserver.go b/rpcserver.go index 5d552b67..f6d51d35 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -41,6 +41,7 @@ import ( "github.com/decred/dcrd/chaincfg/chainhash" "github.com/decred/dcrd/database" "github.com/decred/dcrd/dcrjson" + "github.com/decred/dcrd/mining" "github.com/decred/dcrd/txscript" "github.com/decred/dcrd/wire" @@ -5684,7 +5685,7 @@ func handleVerifyMessage(s *rpcServer, cmd interface{}, closeChan <-chan struct{ type rpcServer struct { started int32 shutdown int32 - policy *miningPolicy + policy *mining.Policy server *server authsha [fastsha256.Size]byte limitauthsha [fastsha256.Size]byte @@ -6168,7 +6169,7 @@ func genCertPair(certFile, keyFile string) error { } // newRPCServer returns a new instance of the rpcServer struct. -func newRPCServer(listenAddrs []string, policy *miningPolicy, s *server) (*rpcServer, error) { +func newRPCServer(listenAddrs []string, policy *mining.Policy, s *server) (*rpcServer, error) { rpc := rpcServer{ policy: policy, server: s, diff --git a/server.go b/server.go index 4431ef49..9f6757ae 100644 --- a/server.go +++ b/server.go @@ -26,6 +26,7 @@ import ( "github.com/decred/dcrd/chaincfg" "github.com/decred/dcrd/chaincfg/chainhash" "github.com/decred/dcrd/database" + "github.com/decred/dcrd/mining" "github.com/decred/dcrd/peer" "github.com/decred/dcrd/txscript" "github.com/decred/dcrd/wire" @@ -2522,7 +2523,7 @@ func newServer(listenAddrs []string, database database.Db, tmdb *stake.TicketDB, s.txMemPool = newTxMemPool(&txC) // Create the mining policy based on the configuration options. - policy := miningPolicy{ + policy := mining.Policy{ BlockMinSize: cfg.BlockMinSize, BlockMaxSize: cfg.BlockMaxSize, BlockPrioritySize: cfg.BlockPrioritySize,