diff options
Diffstat (limited to 'src/miner.cpp')
| -rw-r--r-- | src/miner.cpp | 60 |
1 files changed, 39 insertions, 21 deletions
diff --git a/src/miner.cpp b/src/miner.cpp index bfe382966..397c95c62 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -4,10 +4,16 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "miner.h" + +#include "core.h" #include "main.h" +#include "net.h" +#include "wallet.h" + +#include <stdint.h> double dHashesPerSec = 0.0; -int64 nHPSTimerStart = 0; +int64_t nHPSTimerStart = 0; ////////////////////////////////////////////////////////////////////////////// // @@ -110,8 +116,8 @@ public: }; -uint64 nLastBlockTx = 0; -uint64 nLastBlockSize = 0; +uint64_t nLastBlockTx = 0; +uint64_t nLastBlockSize = 0; // We want to sort transactions by priority and fee, so: typedef boost::tuple<double, double, CTransaction*> TxPriority; @@ -173,10 +179,10 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn) nBlockMinSize = std::min(nBlockMaxSize, nBlockMinSize); // Collect memory pool transactions into the block - int64 nFees = 0; + int64_t nFees = 0; { LOCK2(cs_main, mempool.cs); - CBlockIndex* pindexPrev = pindexBest; + CBlockIndex* pindexPrev = chainActive.Tip(); CCoinsViewCache view(*pcoinsTip, true); // Priority order to process transactions @@ -195,7 +201,7 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn) COrphan* porphan = NULL; double dPriority = 0; - int64 nTotalIn = 0; + int64_t nTotalIn = 0; bool fMissingInputs = false; BOOST_FOREACH(const CTxIn& txin, tx.vin) { @@ -229,7 +235,7 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn) } const CCoins &coins = view.GetCoins(txin.prevout.hash); - int64 nValueIn = coins.vout[txin.prevout.n].nValue; + int64_t nValueIn = coins.vout[txin.prevout.n].nValue; nTotalIn += nValueIn; int nConf = pindexPrev->nHeight - coins.nHeight + 1; @@ -238,9 +244,21 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn) } if (fMissingInputs) continue; - // Priority is sum(valuein * age) / txsize + // Priority is sum(valuein * age) / modified_txsize unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION); - dPriority /= nTxSize; + unsigned int nTxSizeMod = nTxSize; + // In order to avoid disincentivizing cleaning up the UTXO set we don't count + // the constant overhead for each txin and up to 110 bytes of scriptSig (which + // is enough to cover a compressed pubkey p2sh redemption) for priority. + // Providing any more cleanup incentive than making additional inputs free would + // risk encouraging people to create junk outputs to redeem later. + BOOST_FOREACH(const CTxIn& txin, tx.vin) + { + unsigned int offset = 41U + min(110U, (unsigned int)txin.scriptSig.size()); + if (nTxSizeMod > offset) + nTxSizeMod -= offset; + } + dPriority /= nTxSizeMod; // This is a more accurate fee-per-kilobyte than is used by the client code, because the // client code rounds up the size to the nearest 1K. That's good, because it gives an @@ -257,8 +275,8 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn) } // Collect transactions into block - uint64 nBlockSize = 1000; - uint64 nBlockTx = 0; + uint64_t nBlockSize = 1000; + uint64_t nBlockTx = 0; int nBlockSigOps = 100; bool fSortedByFee = (nBlockPrioritySize <= 0); @@ -302,7 +320,7 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn) if (!view.HaveInputs(tx)) continue; - int64 nTxFees = view.GetValueIn(tx)-GetValueOut(tx); + int64_t nTxFees = view.GetValueIn(tx)-GetValueOut(tx); nTxSigOps += GetP2SHSigOpCount(tx, view); if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS) @@ -351,7 +369,7 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn) nLastBlockTx = nBlockTx; nLastBlockSize = nBlockSize; - LogPrintf("CreateNewBlock(): total size %"PRI64u"\n", nBlockSize); + LogPrintf("CreateNewBlock(): total size %"PRIu64"\n", nBlockSize); pblock->vtx[0].vout[0].nValue = GetBlockValue(pindexPrev->nHeight+1, nFees); pblocktemplate->vTxFees[0] = -nFees; @@ -467,7 +485,7 @@ bool CheckWork(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey) // Found a solution { LOCK(cs_main); - if (pblock->hashPrevBlock != hashBestChain) + if (pblock->hashPrevBlock != chainActive.Tip()->GetBlockHash()) return error("BitcoinMiner : generated block is stale"); // Remove key from key pool @@ -509,8 +527,8 @@ void static BitcoinMiner(CWallet *pwallet) // // Create new block // - unsigned int nTransactionsUpdatedLast = nTransactionsUpdated; - CBlockIndex* pindexPrev = pindexBest; + unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated(); + CBlockIndex* pindexPrev = chainActive.Tip(); auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlockWithKey(reservekey)); if (!pblocktemplate.get()) @@ -538,7 +556,7 @@ void static BitcoinMiner(CWallet *pwallet) // // Search // - int64 nStart = GetTime(); + int64_t nStart = GetTime(); uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256(); uint256 hashbuf[2]; uint256& hash = *alignup<16>(hashbuf); @@ -577,7 +595,7 @@ void static BitcoinMiner(CWallet *pwallet) } // Meter hashes/sec - static int64 nHashCounter; + static int64_t nHashCounter; if (nHPSTimerStart == 0) { nHPSTimerStart = GetTimeMillis(); @@ -595,7 +613,7 @@ void static BitcoinMiner(CWallet *pwallet) dHashesPerSec = 1000.0 * nHashCounter / (GetTimeMillis() - nHPSTimerStart); nHPSTimerStart = GetTimeMillis(); nHashCounter = 0; - static int64 nLogTime; + static int64_t nLogTime; if (GetTime() - nLogTime > 30 * 60) { nLogTime = GetTime(); @@ -611,9 +629,9 @@ void static BitcoinMiner(CWallet *pwallet) break; if (nBlockNonce >= 0xffff0000) break; - if (nTransactionsUpdated != nTransactionsUpdatedLast && GetTime() - nStart > 60) + if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 60) break; - if (pindexPrev != pindexBest) + if (pindexPrev != chainActive.Tip()) break; // Update nTime every few seconds |