8 #if defined(HAVE_CONFIG_H)
25 int static FormatHashBlocks(
void* pbuffer,
unsigned int len)
27 unsigned char* pdata = (
unsigned char*)pbuffer;
28 unsigned int blocks = 1 + ((len + 8) / 64);
29 unsigned char* pend = pdata + 64 * blocks;
30 memset(pdata + len, 0, 64 * blocks - len);
32 unsigned int bits = len * 8;
33 pend[-1] = (bits >> 0) & 0xff;
34 pend[-2] = (bits >> 8) & 0xff;
35 pend[-3] = (bits >> 16) & 0xff;
36 pend[-4] = (bits >> 24) & 0xff;
40 static const unsigned int pSHA256InitState[8] =
41 {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
46 unsigned char data[64];
50 for (
int i = 0; i < 16; i++)
51 ((uint32_t*)data)[i] =
ByteReverse(((uint32_t*)pinput)[i]);
53 for (
int i = 0; i < 8; i++)
54 ctx.h[i] = ((uint32_t*)pinit)[i];
56 SHA256_Update(&ctx, data,
sizeof(data));
57 for (
int i = 0; i < 8; i++)
58 ((uint32_t*)pstate)[i] = ctx.h[i];
73 dPriority = dFeePerKb = 0;
78 LogPrintf(
"COrphan(hash=%s, dPriority=%.1f, dFeePerKb=%.1f)\n",
80 BOOST_FOREACH(
uint256 hash, setDependsOn)
90 typedef boost::tuple<double, double, const CTransaction*>
TxPriority;
100 if (a.get<1>() == b.get<1>())
101 return a.get<0>() < b.get<0>();
102 return a.get<1>() < b.get<1>();
106 if (a.get<0>() == b.get<0>())
107 return a.get<1>() < b.get<1>();
108 return a.get<0>() < b.get<0>();
117 if(!pblocktemplate.get())
119 CBlock *pblock = &pblocktemplate->block;
124 txNew.
vin[0].prevout.SetNull();
125 txNew.
vout.resize(1);
126 txNew.
vout[0].scriptPubKey = scriptPubKeyIn;
129 pblock->
vtx.push_back(txNew);
130 pblocktemplate->vTxFees.push_back(-1);
131 pblocktemplate->vTxSigOps.push_back(-1);
134 unsigned int nBlockMaxSize =
GetArg(
"-blockmaxsize", DEFAULT_BLOCK_MAX_SIZE);
136 nBlockMaxSize = std::max((
unsigned int)1000, std::min((
unsigned int)(MAX_BLOCK_SIZE-1000), nBlockMaxSize));
140 unsigned int nBlockPrioritySize =
GetArg(
"-blockprioritysize", DEFAULT_BLOCK_PRIORITY_SIZE);
141 nBlockPrioritySize = std::min(nBlockMaxSize, nBlockPrioritySize);
145 unsigned int nBlockMinSize =
GetArg(
"-blockminsize", DEFAULT_BLOCK_MIN_SIZE);
146 nBlockMinSize = std::min(nBlockMaxSize, nBlockMinSize);
156 list<COrphan> vOrphan;
157 map<uint256, vector<COrphan*> > mapDependers;
158 bool fPrintPriority =
GetBoolArg(
"-printpriority",
false);
161 vector<TxPriority> vecPriority;
163 for (map<uint256, CTxMemPoolEntry>::iterator mi =
mempool.
mapTx.begin();
171 double dPriority = 0;
172 int64_t nTotalIn = 0;
173 bool fMissingInputs =
false;
174 BOOST_FOREACH(
const CTxIn& txin, tx.
vin)
184 LogPrintf(
"ERROR: mempool transaction missing input\n");
185 if (
fDebug) assert(
"mempool transaction missing input" == 0);
186 fMissingInputs =
true;
196 vOrphan.push_back(
COrphan(&tx));
197 porphan = &vOrphan.back();
199 mapDependers[txin.
prevout.
hash].push_back(porphan);
207 nTotalIn += nValueIn;
211 dPriority += (double)nValueIn * nConf;
213 if (fMissingInputs)
continue;
222 double dFeePerKb = double(nTotalIn-tx.
GetValueOut()) / (
double(nTxSize)/1000.0);
230 vecPriority.push_back(
TxPriority(dPriority, dFeePerKb, &mi->second.GetTx()));
234 uint64_t nBlockSize = 1000;
235 uint64_t nBlockTx = 0;
236 int nBlockSigOps = 100;
237 bool fSortedByFee = (nBlockPrioritySize <= 0);
240 std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);
242 while (!vecPriority.empty())
245 double dPriority = vecPriority.front().get<0>();
246 double dFeePerKb = vecPriority.front().get<1>();
247 const CTransaction& tx = *(vecPriority.front().get<2>());
249 std::pop_heap(vecPriority.begin(), vecPriority.end(), comparer);
250 vecPriority.pop_back();
254 if (nBlockSize + nTxSize >= nBlockMaxSize)
259 if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
269 ((nBlockSize + nTxSize >= nBlockPrioritySize) || !
AllowFree(dPriority)))
273 std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);
282 if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
294 pblock->
vtx.push_back(tx);
295 pblocktemplate->vTxFees.push_back(nTxFees);
296 pblocktemplate->vTxSigOps.push_back(nTxSigOps);
297 nBlockSize += nTxSize;
299 nBlockSigOps += nTxSigOps;
304 LogPrintf(
"priority %.1f feeperkb %.1f txid %s\n",
309 if (mapDependers.count(hash))
311 BOOST_FOREACH(
COrphan* porphan, mapDependers[hash])
319 std::push_heap(vecPriority.begin(), vecPriority.end(), comparer);
328 LogPrintf(
"CreateNewBlock(): total size %u\n", nBlockSize);
331 pblocktemplate->vTxFees[0] = -nFees;
342 indexDummy.
pprev = pindexPrev;
346 if (!
ConnectBlock(*pblock, state, &indexDummy, viewNew,
true))
347 throw std::runtime_error(
"CreateNewBlock() : ConnectBlock failed");
350 return pblocktemplate.release();
363 unsigned int nHeight = pindexPrev->
nHeight+1;
365 assert(pblock->
vtx[0].vin[0].scriptSig.size() <= 100);
388 unsigned char pchPadding0[64];
390 unsigned char pchPadding1[64];
393 memset(&tmp, 0,
sizeof(tmp));
395 tmp.block.nVersion = pblock->
nVersion;
398 tmp.block.nTime = pblock->
nTime;
399 tmp.block.nBits = pblock->
nBits;
400 tmp.block.nNonce = pblock->
nNonce;
402 FormatHashBlocks(&tmp.block,
sizeof(tmp.block));
403 FormatHashBlocks(&tmp.hash1,
sizeof(tmp.hash1));
406 for (
unsigned int i = 0; i <
sizeof(tmp)/4; i++)
407 ((
unsigned int*)&tmp)[i] =
ByteReverse(((
unsigned int*)&tmp)[i]);
412 memcpy(pdata, &tmp.block, 128);
413 memcpy(phash1, &tmp.hash1, 64);
439 if (hash > hashTarget)
452 return error(
"AnoncoinMiner : generated block is stale");
466 return error(
"AnoncoinMiner : ProcessBlock, block not accepted");
472 void static AnoncoinMiner(
CWallet *pwallet)
480 unsigned int nExtraNonce = 0;
497 if (!pblocktemplate.get())
499 CBlock *pblock = &pblocktemplate->block;
502 LogPrintf(
"Running AnoncoinMiner with %u transactions in block (%u bytes)\n", pblock->
vtx.size(),
508 char pmidstatebuf[32+16];
char* pmidstate = alignup<16>(pmidstatebuf);
509 char pdatabuf[128+16];
char* pdata = alignup<16>(pdatabuf);
510 char phash1buf[64+16];
char* phash1 = alignup<16>(phash1buf);
514 unsigned int& nBlockTime = *(
unsigned int*)(pdata + 64 + 4);
515 unsigned int& nBlockBits = *(
unsigned int*)(pdata + 64 + 8);
524 unsigned int nHashesDone = 0;
526 char scratchpad[SCRYPT_SCRATCHPAD_SIZE];
530 if (thash <= hashTarget)
540 throw boost::thread_interrupted();
546 if ((pblock->
nNonce & 0xFF) == 0)
551 static int64_t nHashCounter;
558 nHashCounter += nHashesDone;
569 static int64_t nLogTime;
570 if (
GetTime() - nLogTime > 30 * 60)
580 boost::this_thread::interruption_point();
583 if (pblock->
nNonce >= 0xffff0000)
601 catch (boost::thread_interrupted)
610 static boost::thread_group* minerThreads = NULL;
616 nThreads = boost::thread::hardware_concurrency();
619 if (minerThreads != NULL)
621 minerThreads->interrupt_all();
626 if (nThreads == 0 || !fGenerate)
629 minerThreads =
new boost::thread_group();
630 for (
int i = 0; i < nThreads; i++)
631 minerThreads->create_thread(boost::bind(&AnoncoinMiner, pwallet));
bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
int64_t GetValueOut() const
CBlockTemplate * CreateNewBlockWithKey(CReserveKey &reservekey)
void IncrementExtraNonce(CBlock *pblock, CBlockIndex *pindexPrev, unsigned int &nExtraNonce)
Modify the extranonce in a block.
#define THREAD_PRIORITY_LOWEST
#define THREAD_PRIORITY_NORMAL
int64_t GetBlockValue(int nHeight, int64_t nFees)
double ComputePriority(double dPriorityInputs, unsigned int nTxSize=0) const
std::vector< CTxOut > vout
uint256 getuint256() const
CCriticalSection cs_wallet
Main wallet lock.
void MilliSleep(int64_t n)
CBlockTemplate * CreateNewBlock(const CScript &scriptPubKeyIn)
Generate a new block, without valid proof-of-work.
#define scrypt_1024_1_1_256_sp(input, output, scratchpad)
pruned version of CTransaction: only retains metadata and unspent transaction outputs ...
bool AllowFree(double dPriority)
string FormatMoney(int64_t n, bool fPlus)
void RenameThread(const char *name)
CChain chainActive
The currently-connected chain of blocks.
bool GetCoins(const uint256 &txid, CCoins &coins)
bool CheckWork(CBlock *pblock, CWallet &wallet, CReserveKey &reservekey)
Check mined block.
unsigned int GetTransactionsUpdated() const
uint256 BuildMerkleTree() const
void UpdateTime(CBlockHeader &block, const CBlockIndex *pindexPrev)
CBigNum & SetCompact(unsigned int nCompact)
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or NULL if none.
bool GetBoolArg(const std::string &strArg, bool fDefault)
Return boolean argument or default value.
unsigned int GetSerializeSize(char a, int, int=0)
bool HaveCoins(const uint256 &txid)
An input of a transaction.
bool HaveInputs(const CTransaction &tx)
std::vector< CTxOut > vout
void FormatHashBuffers(CBlock *pblock, char *pmidstate, char *pdata, char *phash1)
Do mining precalculation.
An encapsulated public key.
set< uint256 > setDependsOn
boost::tuple< double, double, const CTransaction * > TxPriority
C++ wrapper for BIGNUM (OpenSSL bignum)
std::map< uint256, CTxMemPoolEntry > mapTx
void UpdateCoins(const CTransaction &tx, CValidationState &state, CCoinsViewCache &inputs, CTxUndo &txundo, int nHeight, const uint256 &txhash)
bool GetReservedKey(CPubKey &pubkey)
bool ConnectBlock(CBlock &block, CValidationState &state, CBlockIndex *pindex, CCoinsViewCache &view, bool fJustCheck)
std::map< uint256, int > mapRequestCount
CCoinsViewCache * pcoinsTip
Global variable that points to the active CCoinsView (protected by cs_main)
std::string GetHex() const
void SetThreadPriority(int nPriority)
unsigned int GetP2SHSigOpCount(const CTransaction &tx, CCoinsViewCache &inputs)
Count ECDSA signature operations in pay-to-script-hash inputs.
Capture information about block/transaction validation.
bool ProcessBlock(CValidationState &state, CNode *pfrom, CBlock *pblock, CDiskBlockPos *dbp)
Process an incoming block.
A key allocated from the key pool.
uint32_t ByteReverse(uint32_t value)
unsigned int GetLegacySigOpCount(const CTransaction &tx)
Count ECDSA signature operations the old-fashioned (pre-0.6) way.
The block chain is a tree shaped structure starting with the genesis block at the root...
const CChainParams & Params()
Return the currently selected parameters.
bool CheckInputs(const CTransaction &tx, CValidationState &state, CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, std::vector< CScriptCheck > *pvChecks)
Serialized script, used inside transaction inputs and outputs.
Undo information for a CTransaction.
void * memcpy(void *a, const void *b, size_t c)
std::string ToString() const
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
void GenerateAnoncoins(bool fGenerate, CWallet *pwallet, int nThreads)
Run the miner threads.
COrphan(const CTransaction *ptxIn)
int64_t GetValueIn(const CTransaction &tx)
Amount of anoncoins coming in to a transaction Note that lightweight clients may not know anything be...
static int64_t nMinRelayTxFee
Fees smaller than this (in satoshi) are considered zero fee (for relaying and mining) ...
bool operator()(const TxPriority &a, const TxPriority &b)
std::vector< CTransaction > vtx
std::string GetArg(const std::string &strArg, const std::string &strDefault)
Return string argument or default value.
The basic transaction that is broadcasted on the network and contained in blocks. ...
CCoinsView that adds a memory cache for transactions to another CCoinsView.
void SHA256Transform(void *pstate, void *pinput, const void *pinit)
Base sha256 mining transform.
TxPriorityCompare(bool _byFee)
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock)
virtual Network NetworkID() const =0
uint256 GetBlockHash() const