1 // Copyright (c) 2009 Satoshi Nakamoto
\r
2 // Distributed under the MIT/X11 software license, see the accompanying
\r
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
\r
16 CCriticalSection cs_main;
\r
18 map<uint256, CTransaction> mapTransactions;
\r
19 CCriticalSection cs_mapTransactions;
\r
20 unsigned int nTransactionsUpdated = 0;
\r
21 map<COutPoint, CInPoint> mapNextTx;
\r
23 map<uint256, CBlockIndex*> mapBlockIndex;
\r
24 const uint256 hashGenesisBlock("0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f");
\r
25 CBlockIndex* pindexGenesisBlock = NULL;
\r
26 int nBestHeight = -1;
\r
27 uint256 hashBestChain = 0;
\r
28 CBlockIndex* pindexBest = NULL;
\r
30 map<uint256, CBlock*> mapOrphanBlocks;
\r
31 multimap<uint256, CBlock*> mapOrphanBlocksByPrev;
\r
33 map<uint256, CDataStream*> mapOrphanTransactions;
\r
34 multimap<uint256, CDataStream*> mapOrphanTransactionsByPrev;
\r
36 map<uint256, CWalletTx> mapWallet;
\r
37 vector<uint256> vWalletUpdated;
\r
38 CCriticalSection cs_mapWallet;
\r
40 map<vector<unsigned char>, CPrivKey> mapKeys;
\r
41 map<uint160, vector<unsigned char> > mapPubKeys;
\r
42 CCriticalSection cs_mapKeys;
\r
45 int nDropMessagesTest = 0;
\r
48 int fGenerateBitcoins = false;
\r
49 int64 nTransactionFee = 0;
\r
50 CAddress addrIncoming;
\r
51 int fLimitProcessors = false;
\r
52 int nLimitProcessors = 1;
\r
59 //////////////////////////////////////////////////////////////////////////////
\r
64 bool AddKey(const CKey& key)
\r
66 CRITICAL_BLOCK(cs_mapKeys)
\r
68 mapKeys[key.GetPubKey()] = key.GetPrivKey();
\r
69 mapPubKeys[Hash160(key.GetPubKey())] = key.GetPubKey();
\r
71 return CWalletDB().WriteKey(key.GetPubKey(), key.GetPrivKey());
\r
74 vector<unsigned char> GenerateNewKey()
\r
79 throw runtime_error("GenerateNewKey() : AddKey failed\n");
\r
80 return key.GetPubKey();
\r
86 //////////////////////////////////////////////////////////////////////////////
\r
91 bool AddToWallet(const CWalletTx& wtxIn)
\r
93 uint256 hash = wtxIn.GetHash();
\r
94 CRITICAL_BLOCK(cs_mapWallet)
\r
96 // Inserts only if not already there, returns tx inserted or tx found
\r
97 pair<map<uint256, CWalletTx>::iterator, bool> ret = mapWallet.insert(make_pair(hash, wtxIn));
\r
98 CWalletTx& wtx = (*ret.first).second;
\r
99 bool fInsertedNew = ret.second;
\r
101 wtx.nTimeReceived = GetAdjustedTime();
\r
103 bool fUpdated = false;
\r
107 if (wtxIn.hashBlock != 0 && wtxIn.hashBlock != wtx.hashBlock)
\r
109 wtx.hashBlock = wtxIn.hashBlock;
\r
112 if (wtxIn.nIndex != -1 && (wtxIn.vMerkleBranch != wtx.vMerkleBranch || wtxIn.nIndex != wtx.nIndex))
\r
114 wtx.vMerkleBranch = wtxIn.vMerkleBranch;
\r
115 wtx.nIndex = wtxIn.nIndex;
\r
118 if (wtxIn.fFromMe && wtxIn.fFromMe != wtx.fFromMe)
\r
120 wtx.fFromMe = wtxIn.fFromMe;
\r
123 if (wtxIn.fSpent && wtxIn.fSpent != wtx.fSpent)
\r
125 wtx.fSpent = wtxIn.fSpent;
\r
131 printf("AddToWallet %s %s%s\n", wtxIn.GetHash().ToString().substr(0,6).c_str(), (fInsertedNew ? "new" : ""), (fUpdated ? "update" : ""));
\r
134 if (fInsertedNew || fUpdated)
\r
135 if (!wtx.WriteToDisk())
\r
139 vWalletUpdated.push_back(hash);
\r
143 MainFrameRepaint();
\r
147 bool AddToWalletIfMine(const CTransaction& tx, const CBlock* pblock)
\r
149 if (tx.IsMine() || mapWallet.count(tx.GetHash()))
\r
152 // Get merkle branch if transaction was found in a block
\r
154 wtx.SetMerkleBranch(pblock);
\r
155 return AddToWallet(wtx);
\r
160 bool EraseFromWallet(uint256 hash)
\r
162 CRITICAL_BLOCK(cs_mapWallet)
\r
164 if (mapWallet.erase(hash))
\r
165 CWalletDB().EraseTx(hash);
\r
178 //////////////////////////////////////////////////////////////////////////////
\r
180 // mapOrphanTransactions
\r
183 void AddOrphanTx(const CDataStream& vMsg)
\r
186 CDataStream(vMsg) >> tx;
\r
187 uint256 hash = tx.GetHash();
\r
188 if (mapOrphanTransactions.count(hash))
\r
190 CDataStream* pvMsg = mapOrphanTransactions[hash] = new CDataStream(vMsg);
\r
191 foreach(const CTxIn& txin, tx.vin)
\r
192 mapOrphanTransactionsByPrev.insert(make_pair(txin.prevout.hash, pvMsg));
\r
195 void EraseOrphanTx(uint256 hash)
\r
197 if (!mapOrphanTransactions.count(hash))
\r
199 const CDataStream* pvMsg = mapOrphanTransactions[hash];
\r
201 CDataStream(*pvMsg) >> tx;
\r
202 foreach(const CTxIn& txin, tx.vin)
\r
204 for (multimap<uint256, CDataStream*>::iterator mi = mapOrphanTransactionsByPrev.lower_bound(txin.prevout.hash);
\r
205 mi != mapOrphanTransactionsByPrev.upper_bound(txin.prevout.hash);)
\r
207 if ((*mi).second == pvMsg)
\r
208 mapOrphanTransactionsByPrev.erase(mi++);
\r
214 mapOrphanTransactions.erase(hash);
\r
224 //////////////////////////////////////////////////////////////////////////////
\r
229 bool CTxIn::IsMine() const
\r
231 CRITICAL_BLOCK(cs_mapWallet)
\r
233 map<uint256, CWalletTx>::iterator mi = mapWallet.find(prevout.hash);
\r
234 if (mi != mapWallet.end())
\r
236 const CWalletTx& prev = (*mi).second;
\r
237 if (prevout.n < prev.vout.size())
\r
238 if (prev.vout[prevout.n].IsMine())
\r
245 int64 CTxIn::GetDebit() const
\r
247 CRITICAL_BLOCK(cs_mapWallet)
\r
249 map<uint256, CWalletTx>::iterator mi = mapWallet.find(prevout.hash);
\r
250 if (mi != mapWallet.end())
\r
252 const CWalletTx& prev = (*mi).second;
\r
253 if (prevout.n < prev.vout.size())
\r
254 if (prev.vout[prevout.n].IsMine())
\r
255 return prev.vout[prevout.n].nValue;
\r
261 int64 CWalletTx::GetTxTime() const
\r
263 if (!fTimeReceivedIsTxTime && hashBlock != 0)
\r
265 // If we did not receive the transaction directly, we rely on the block's
\r
266 // time to figure out when it happened. We use the median over a range
\r
267 // of blocks to try to filter out inaccurate block times.
\r
268 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
\r
269 if (mi != mapBlockIndex.end())
\r
271 CBlockIndex* pindex = (*mi).second;
\r
273 return pindex->GetMedianTime();
\r
276 return nTimeReceived;
\r
284 int CMerkleTx::SetMerkleBranch(const CBlock* pblock)
\r
288 if (hashBlock == 0)
\r
294 if (pblock == NULL)
\r
296 // Load the block this tx is in
\r
298 if (!CTxDB("r").ReadTxIndex(GetHash(), txindex))
\r
300 if (!blockTmp.ReadFromDisk(txindex.pos.nFile, txindex.pos.nBlockPos, true))
\r
302 pblock = &blockTmp;
\r
305 // Update the tx's hashBlock
\r
306 hashBlock = pblock->GetHash();
\r
308 // Locate the transaction
\r
309 for (nIndex = 0; nIndex < pblock->vtx.size(); nIndex++)
\r
310 if (pblock->vtx[nIndex] == *(CTransaction*)this)
\r
312 if (nIndex == pblock->vtx.size())
\r
314 vMerkleBranch.clear();
\r
316 printf("ERROR: SetMerkleBranch() : couldn't find tx in block\n");
\r
320 // Fill in merkle branch
\r
321 vMerkleBranch = pblock->GetMerkleBranch(nIndex);
\r
324 // Is the tx in a block that's in the main chain
\r
325 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
\r
326 if (mi == mapBlockIndex.end())
\r
328 CBlockIndex* pindex = (*mi).second;
\r
329 if (!pindex || !pindex->IsInMainChain())
\r
332 return pindexBest->nHeight - pindex->nHeight + 1;
\r
337 void CWalletTx::AddSupportingTransactions(CTxDB& txdb)
\r
341 const int COPY_DEPTH = 3;
\r
342 if (SetMerkleBranch() < COPY_DEPTH)
\r
344 vector<uint256> vWorkQueue;
\r
345 foreach(const CTxIn& txin, vin)
\r
346 vWorkQueue.push_back(txin.prevout.hash);
\r
348 // This critsect is OK because txdb is already open
\r
349 CRITICAL_BLOCK(cs_mapWallet)
\r
351 map<uint256, const CMerkleTx*> mapWalletPrev;
\r
352 set<uint256> setAlreadyDone;
\r
353 for (int i = 0; i < vWorkQueue.size(); i++)
\r
355 uint256 hash = vWorkQueue[i];
\r
356 if (setAlreadyDone.count(hash))
\r
358 setAlreadyDone.insert(hash);
\r
361 if (mapWallet.count(hash))
\r
363 tx = mapWallet[hash];
\r
364 foreach(const CMerkleTx& txWalletPrev, mapWallet[hash].vtxPrev)
\r
365 mapWalletPrev[txWalletPrev.GetHash()] = &txWalletPrev;
\r
367 else if (mapWalletPrev.count(hash))
\r
369 tx = *mapWalletPrev[hash];
\r
371 else if (!fClient && txdb.ReadDiskTx(hash, tx))
\r
377 printf("ERROR: AddSupportingTransactions() : unsupported transaction\n");
\r
381 int nDepth = tx.SetMerkleBranch();
\r
382 vtxPrev.push_back(tx);
\r
384 if (nDepth < COPY_DEPTH)
\r
385 foreach(const CTxIn& txin, tx.vin)
\r
386 vWorkQueue.push_back(txin.prevout.hash);
\r
391 reverse(vtxPrev.begin(), vtxPrev.end());
\r
404 bool CTransaction::AcceptTransaction(CTxDB& txdb, bool fCheckInputs, bool* pfMissingInputs)
\r
406 if (pfMissingInputs)
\r
407 *pfMissingInputs = false;
\r
409 // Coinbase is only valid in a block, not as a loose transaction
\r
411 return error("AcceptTransaction() : coinbase as individual tx");
\r
413 if (!CheckTransaction())
\r
414 return error("AcceptTransaction() : CheckTransaction failed");
\r
416 // To help v0.1.5 clients who would see it as negative number. please delete this later.
\r
417 if (nLockTime > INT_MAX)
\r
418 return error("AcceptTransaction() : not accepting nLockTime beyond 2038");
\r
420 // Do we already have it?
\r
421 uint256 hash = GetHash();
\r
422 CRITICAL_BLOCK(cs_mapTransactions)
\r
423 if (mapTransactions.count(hash))
\r
426 if (txdb.ContainsTx(hash))
\r
429 // Check for conflicts with in-memory transactions
\r
430 CTransaction* ptxOld = NULL;
\r
431 for (int i = 0; i < vin.size(); i++)
\r
433 COutPoint outpoint = vin[i].prevout;
\r
434 if (mapNextTx.count(outpoint))
\r
436 // Allow replacing with a newer version of the same transaction
\r
439 ptxOld = mapNextTx[outpoint].ptx;
\r
440 if (!IsNewerThan(*ptxOld))
\r
442 for (int i = 0; i < vin.size(); i++)
\r
444 COutPoint outpoint = vin[i].prevout;
\r
445 if (!mapNextTx.count(outpoint) || mapNextTx[outpoint].ptx != ptxOld)
\r
452 // Check against previous transactions
\r
453 map<uint256, CTxIndex> mapUnused;
\r
455 if (fCheckInputs && !ConnectInputs(txdb, mapUnused, CDiskTxPos(1,1,1), 0, nFees, false, false))
\r
457 if (pfMissingInputs)
\r
458 *pfMissingInputs = true;
\r
459 return error("AcceptTransaction() : ConnectInputs failed %s", hash.ToString().substr(0,6).c_str());
\r
462 // Store transaction in memory
\r
463 CRITICAL_BLOCK(cs_mapTransactions)
\r
467 printf("mapTransaction.erase(%s) replacing with new version\n", ptxOld->GetHash().ToString().c_str());
\r
468 mapTransactions.erase(ptxOld->GetHash());
\r
473 ///// are we sure this is ok when loading transactions or restoring block txes
\r
474 // If updated, erase old tx from wallet
\r
476 EraseFromWallet(ptxOld->GetHash());
\r
478 printf("AcceptTransaction(): accepted %s\n", hash.ToString().substr(0,6).c_str());
\r
483 bool CTransaction::AddToMemoryPool()
\r
485 // Add to memory pool without checking anything. Don't call this directly,
\r
486 // call AcceptTransaction to properly check the transaction first.
\r
487 CRITICAL_BLOCK(cs_mapTransactions)
\r
489 uint256 hash = GetHash();
\r
490 mapTransactions[hash] = *this;
\r
491 for (int i = 0; i < vin.size(); i++)
\r
492 mapNextTx[vin[i].prevout] = CInPoint(&mapTransactions[hash], i);
\r
493 nTransactionsUpdated++;
\r
499 bool CTransaction::RemoveFromMemoryPool()
\r
501 // Remove transaction from memory pool
\r
502 CRITICAL_BLOCK(cs_mapTransactions)
\r
504 foreach(const CTxIn& txin, vin)
\r
505 mapNextTx.erase(txin.prevout);
\r
506 mapTransactions.erase(GetHash());
\r
507 nTransactionsUpdated++;
\r
517 int CMerkleTx::GetDepthInMainChain() const
\r
519 if (hashBlock == 0 || nIndex == -1)
\r
522 // Find the block it claims to be in
\r
523 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
\r
524 if (mi == mapBlockIndex.end())
\r
526 CBlockIndex* pindex = (*mi).second;
\r
527 if (!pindex || !pindex->IsInMainChain())
\r
530 // Make sure the merkle branch connects to this block
\r
531 if (!fMerkleVerified)
\r
533 if (CBlock::CheckMerkleBranch(GetHash(), vMerkleBranch, nIndex) != pindex->hashMerkleRoot)
\r
535 fMerkleVerified = true;
\r
538 return pindexBest->nHeight - pindex->nHeight + 1;
\r
542 int CMerkleTx::GetBlocksToMaturity() const
\r
546 return max(0, (COINBASE_MATURITY+20) - GetDepthInMainChain());
\r
550 bool CMerkleTx::AcceptTransaction(CTxDB& txdb, bool fCheckInputs)
\r
554 if (!IsInMainChain() && !ClientConnectInputs())
\r
556 return CTransaction::AcceptTransaction(txdb, false);
\r
560 return CTransaction::AcceptTransaction(txdb, fCheckInputs);
\r
566 bool CWalletTx::AcceptWalletTransaction(CTxDB& txdb, bool fCheckInputs)
\r
568 CRITICAL_BLOCK(cs_mapTransactions)
\r
570 foreach(CMerkleTx& tx, vtxPrev)
\r
572 if (!tx.IsCoinBase())
\r
574 uint256 hash = tx.GetHash();
\r
575 if (!mapTransactions.count(hash) && !txdb.ContainsTx(hash))
\r
576 tx.AcceptTransaction(txdb, fCheckInputs);
\r
580 return AcceptTransaction(txdb, fCheckInputs);
\r
585 void ReacceptWalletTransactions()
\r
587 // Reaccept any txes of ours that aren't already in a block
\r
589 CRITICAL_BLOCK(cs_mapWallet)
\r
591 foreach(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
\r
593 CWalletTx& wtx = item.second;
\r
594 if (!wtx.IsCoinBase() && !txdb.ContainsTx(wtx.GetHash()))
\r
595 wtx.AcceptWalletTransaction(txdb, false);
\r
601 void CWalletTx::RelayWalletTransaction(CTxDB& txdb)
\r
603 foreach(const CMerkleTx& tx, vtxPrev)
\r
605 if (!tx.IsCoinBase())
\r
607 uint256 hash = tx.GetHash();
\r
608 if (!txdb.ContainsTx(hash))
\r
609 RelayMessage(CInv(MSG_TX, hash), (CTransaction)tx);
\r
614 uint256 hash = GetHash();
\r
615 if (!txdb.ContainsTx(hash))
\r
617 printf("Relaying wtx %s\n", hash.ToString().substr(0,6).c_str());
\r
618 RelayMessage(CInv(MSG_TX, hash), (CTransaction)*this);
\r
623 void RelayWalletTransactions()
\r
625 static int64 nLastTime;
\r
626 if (GetTime() - nLastTime < 10 * 60)
\r
628 nLastTime = GetTime();
\r
630 // Rebroadcast any of our txes that aren't in a block yet
\r
631 printf("RelayWalletTransactions()\n");
\r
633 CRITICAL_BLOCK(cs_mapWallet)
\r
635 // Sort them in chronological order
\r
636 multimap<unsigned int, CWalletTx*> mapSorted;
\r
637 foreach(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
\r
639 CWalletTx& wtx = item.second;
\r
640 mapSorted.insert(make_pair(wtx.nTimeReceived, &wtx));
\r
642 foreach(PAIRTYPE(const unsigned int, CWalletTx*)& item, mapSorted)
\r
644 CWalletTx& wtx = *item.second;
\r
645 wtx.RelayWalletTransaction(txdb);
\r
659 //////////////////////////////////////////////////////////////////////////////
\r
661 // CBlock and CBlockIndex
\r
664 bool CBlock::ReadFromDisk(const CBlockIndex* pblockindex, bool fReadTransactions)
\r
666 return ReadFromDisk(pblockindex->nFile, pblockindex->nBlockPos, fReadTransactions);
\r
669 uint256 GetOrphanRoot(const CBlock* pblock)
\r
671 // Work back to the first block in the orphan chain
\r
672 while (mapOrphanBlocks.count(pblock->hashPrevBlock))
\r
673 pblock = mapOrphanBlocks[pblock->hashPrevBlock];
\r
674 return pblock->GetHash();
\r
677 int64 CBlock::GetBlockValue(int64 nFees) const
\r
679 int64 nSubsidy = 50 * COIN;
\r
681 // Subsidy is cut in half every 4 years
\r
682 nSubsidy >>= (nBestHeight / 210000);
\r
684 return nSubsidy + nFees;
\r
687 unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast)
\r
689 const unsigned int nTargetTimespan = 14 * 24 * 60 * 60; // two weeks
\r
690 const unsigned int nTargetSpacing = 10 * 60;
\r
691 const unsigned int nInterval = nTargetTimespan / nTargetSpacing;
\r
694 if (pindexLast == NULL)
\r
695 return bnProofOfWorkLimit.GetCompact();
\r
697 // Only change once per interval
\r
698 if ((pindexLast->nHeight+1) % nInterval != 0)
\r
699 return pindexLast->nBits;
\r
701 // Go back by what we want to be 14 days worth of blocks
\r
702 const CBlockIndex* pindexFirst = pindexLast;
\r
703 for (int i = 0; pindexFirst && i < nInterval-1; i++)
\r
704 pindexFirst = pindexFirst->pprev;
\r
705 assert(pindexFirst);
\r
707 // Limit adjustment step
\r
708 unsigned int nActualTimespan = pindexLast->nTime - pindexFirst->nTime;
\r
709 printf(" nActualTimespan = %d before bounds\n", nActualTimespan);
\r
710 if (nActualTimespan < nTargetTimespan/4)
\r
711 nActualTimespan = nTargetTimespan/4;
\r
712 if (nActualTimespan > nTargetTimespan*4)
\r
713 nActualTimespan = nTargetTimespan*4;
\r
717 bnNew.SetCompact(pindexLast->nBits);
\r
718 bnNew *= nActualTimespan;
\r
719 bnNew /= nTargetTimespan;
\r
721 if (bnNew > bnProofOfWorkLimit)
\r
722 bnNew = bnProofOfWorkLimit;
\r
725 printf("\n\n\nGetNextWorkRequired RETARGET *****\n");
\r
726 printf("nTargetTimespan = %d nActualTimespan = %d\n", nTargetTimespan, nActualTimespan);
\r
727 printf("Before: %08x %s\n", pindexLast->nBits, CBigNum().SetCompact(pindexLast->nBits).getuint256().ToString().c_str());
\r
728 printf("After: %08x %s\n", bnNew.GetCompact(), bnNew.getuint256().ToString().c_str());
\r
730 return bnNew.GetCompact();
\r
741 bool CTransaction::DisconnectInputs(CTxDB& txdb)
\r
743 // Relinquish previous transactions' spent pointers
\r
746 foreach(const CTxIn& txin, vin)
\r
748 COutPoint prevout = txin.prevout;
\r
750 // Get prev txindex from disk
\r
752 if (!txdb.ReadTxIndex(prevout.hash, txindex))
\r
753 return error("DisconnectInputs() : ReadTxIndex failed");
\r
755 if (prevout.n >= txindex.vSpent.size())
\r
756 return error("DisconnectInputs() : prevout.n out of range");
\r
758 // Mark outpoint as not spent
\r
759 txindex.vSpent[prevout.n].SetNull();
\r
762 txdb.UpdateTxIndex(prevout.hash, txindex);
\r
766 // Remove transaction from index
\r
767 if (!txdb.EraseTxIndex(*this))
\r
768 return error("DisconnectInputs() : EraseTxPos failed");
\r
774 bool CTransaction::ConnectInputs(CTxDB& txdb, map<uint256, CTxIndex>& mapTestPool, CDiskTxPos posThisTx, int nHeight, int64& nFees, bool fBlock, bool fMiner, int64 nMinFee)
\r
776 // Take over previous transactions' spent pointers
\r
779 int64 nValueIn = 0;
\r
780 for (int i = 0; i < vin.size(); i++)
\r
782 COutPoint prevout = vin[i].prevout;
\r
786 bool fFound = true;
\r
787 if (fMiner && mapTestPool.count(prevout.hash))
\r
789 // Get txindex from current proposed changes
\r
790 txindex = mapTestPool[prevout.hash];
\r
794 // Read txindex from txdb
\r
795 fFound = txdb.ReadTxIndex(prevout.hash, txindex);
\r
797 if (!fFound && (fBlock || fMiner))
\r
798 return fMiner ? false : error("ConnectInputs() : %s prev tx %s index entry not found", GetHash().ToString().substr(0,6).c_str(), prevout.hash.ToString().substr(0,6).c_str());
\r
801 CTransaction txPrev;
\r
802 if (!fFound || txindex.pos == CDiskTxPos(1,1,1))
\r
804 // Get prev tx from single transactions in memory
\r
805 CRITICAL_BLOCK(cs_mapTransactions)
\r
807 if (!mapTransactions.count(prevout.hash))
\r
808 return error("ConnectInputs() : %s mapTransactions prev not found %s", GetHash().ToString().substr(0,6).c_str(), prevout.hash.ToString().substr(0,6).c_str());
\r
809 txPrev = mapTransactions[prevout.hash];
\r
812 txindex.vSpent.resize(txPrev.vout.size());
\r
816 // Get prev tx from disk
\r
817 if (!txPrev.ReadFromDisk(txindex.pos))
\r
818 return error("ConnectInputs() : %s ReadFromDisk prev tx %s failed", GetHash().ToString().substr(0,6).c_str(), prevout.hash.ToString().substr(0,6).c_str());
\r
821 if (prevout.n >= txPrev.vout.size() || prevout.n >= txindex.vSpent.size())
\r
822 return error("ConnectInputs() : %s prevout.n out of range %d %d %d prev tx %s\n%s", GetHash().ToString().substr(0,6).c_str(), prevout.n, txPrev.vout.size(), txindex.vSpent.size(), prevout.hash.ToString().substr(0,6).c_str(), txPrev.ToString().c_str());
\r
824 // If prev is coinbase, check that it's matured
\r
825 if (txPrev.IsCoinBase())
\r
826 for (CBlockIndex* pindex = pindexBest; pindex && nBestHeight - pindex->nHeight < COINBASE_MATURITY-1; pindex = pindex->pprev)
\r
827 if (pindex->nBlockPos == txindex.pos.nBlockPos && pindex->nFile == txindex.pos.nFile)
\r
828 return error("ConnectInputs() : tried to spend coinbase at depth %d", nBestHeight - pindex->nHeight);
\r
830 // Verify signature
\r
831 if (!VerifySignature(txPrev, *this, i))
\r
832 return error("ConnectInputs() : %s VerifySignature failed", GetHash().ToString().substr(0,6).c_str());
\r
834 // Check for conflicts
\r
835 if (!txindex.vSpent[prevout.n].IsNull())
\r
836 return fMiner ? false : error("ConnectInputs() : %s prev tx already used at %s", GetHash().ToString().substr(0,6).c_str(), txindex.vSpent[prevout.n].ToString().c_str());
\r
838 // Mark outpoints as spent
\r
839 txindex.vSpent[prevout.n] = posThisTx;
\r
843 txdb.UpdateTxIndex(prevout.hash, txindex);
\r
845 mapTestPool[prevout.hash] = txindex;
\r
847 nValueIn += txPrev.vout[prevout.n].nValue;
\r
850 // Tally transaction fees
\r
851 int64 nTxFee = nValueIn - GetValueOut();
\r
853 return error("ConnectInputs() : %s nTxFee < 0", GetHash().ToString().substr(0,6).c_str());
\r
854 if (nTxFee < nMinFee)
\r
861 // Add transaction to disk index
\r
862 if (!txdb.AddTxIndex(*this, posThisTx, nHeight))
\r
863 return error("ConnectInputs() : AddTxPos failed");
\r
867 // Add transaction to test pool
\r
868 mapTestPool[GetHash()] = CTxIndex(CDiskTxPos(1,1,1), vout.size());
\r
875 bool CTransaction::ClientConnectInputs()
\r
880 // Take over previous transactions' spent pointers
\r
881 CRITICAL_BLOCK(cs_mapTransactions)
\r
883 int64 nValueIn = 0;
\r
884 for (int i = 0; i < vin.size(); i++)
\r
886 // Get prev tx from single transactions in memory
\r
887 COutPoint prevout = vin[i].prevout;
\r
888 if (!mapTransactions.count(prevout.hash))
\r
890 CTransaction& txPrev = mapTransactions[prevout.hash];
\r
892 if (prevout.n >= txPrev.vout.size())
\r
895 // Verify signature
\r
896 if (!VerifySignature(txPrev, *this, i))
\r
897 return error("ConnectInputs() : VerifySignature failed");
\r
899 ///// this is redundant with the mapNextTx stuff, not sure which I want to get rid of
\r
900 ///// this has to go away now that posNext is gone
\r
901 // // Check for conflicts
\r
902 // if (!txPrev.vout[prevout.n].posNext.IsNull())
\r
903 // return error("ConnectInputs() : prev tx already used");
\r
905 // // Flag outpoints as used
\r
906 // txPrev.vout[prevout.n].posNext = posThisTx;
\r
908 nValueIn += txPrev.vout[prevout.n].nValue;
\r
910 if (GetValueOut() > nValueIn)
\r
920 bool CBlock::DisconnectBlock(CTxDB& txdb, CBlockIndex* pindex)
\r
922 // Disconnect in reverse order
\r
923 for (int i = vtx.size()-1; i >= 0; i--)
\r
924 if (!vtx[i].DisconnectInputs(txdb))
\r
927 // Update block index on disk without changing it in memory.
\r
928 // The memory index structure will be changed after the db commits.
\r
931 CDiskBlockIndex blockindexPrev(pindex->pprev);
\r
932 blockindexPrev.hashNext = 0;
\r
933 txdb.WriteBlockIndex(blockindexPrev);
\r
939 bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex)
\r
941 //// issue here: it doesn't know the version
\r
942 unsigned int nTxPos = pindex->nBlockPos + ::GetSerializeSize(CBlock(), SER_DISK) - 1 + GetSizeOfCompactSize(vtx.size());
\r
944 map<uint256, CTxIndex> mapUnused;
\r
946 foreach(CTransaction& tx, vtx)
\r
948 CDiskTxPos posThisTx(pindex->nFile, pindex->nBlockPos, nTxPos);
\r
949 nTxPos += ::GetSerializeSize(tx, SER_DISK);
\r
951 if (!tx.ConnectInputs(txdb, mapUnused, posThisTx, pindex->nHeight, nFees, true, false))
\r
955 if (vtx[0].GetValueOut() > GetBlockValue(nFees))
\r
958 // Update block index on disk without changing it in memory.
\r
959 // The memory index structure will be changed after the db commits.
\r
962 CDiskBlockIndex blockindexPrev(pindex->pprev);
\r
963 blockindexPrev.hashNext = pindex->GetBlockHash();
\r
964 txdb.WriteBlockIndex(blockindexPrev);
\r
967 // Watch for transactions paying to me
\r
968 foreach(CTransaction& tx, vtx)
\r
969 AddToWalletIfMine(tx, this);
\r
976 bool Reorganize(CTxDB& txdb, CBlockIndex* pindexNew)
\r
978 printf("*** REORGANIZE ***\n");
\r
981 CBlockIndex* pfork = pindexBest;
\r
982 CBlockIndex* plonger = pindexNew;
\r
983 while (pfork != plonger)
\r
985 if (!(pfork = pfork->pprev))
\r
986 return error("Reorganize() : pfork->pprev is null");
\r
987 while (plonger->nHeight > pfork->nHeight)
\r
988 if (!(plonger = plonger->pprev))
\r
989 return error("Reorganize() : plonger->pprev is null");
\r
992 // List of what to disconnect
\r
993 vector<CBlockIndex*> vDisconnect;
\r
994 for (CBlockIndex* pindex = pindexBest; pindex != pfork; pindex = pindex->pprev)
\r
995 vDisconnect.push_back(pindex);
\r
997 // List of what to connect
\r
998 vector<CBlockIndex*> vConnect;
\r
999 for (CBlockIndex* pindex = pindexNew; pindex != pfork; pindex = pindex->pprev)
\r
1000 vConnect.push_back(pindex);
\r
1001 reverse(vConnect.begin(), vConnect.end());
\r
1003 // Disconnect shorter branch
\r
1004 vector<CTransaction> vResurrect;
\r
1005 foreach(CBlockIndex* pindex, vDisconnect)
\r
1008 if (!block.ReadFromDisk(pindex->nFile, pindex->nBlockPos, true))
\r
1009 return error("Reorganize() : ReadFromDisk for disconnect failed");
\r
1010 if (!block.DisconnectBlock(txdb, pindex))
\r
1011 return error("Reorganize() : DisconnectBlock failed");
\r
1013 // Queue memory transactions to resurrect
\r
1014 foreach(const CTransaction& tx, block.vtx)
\r
1015 if (!tx.IsCoinBase())
\r
1016 vResurrect.push_back(tx);
\r
1019 // Connect longer branch
\r
1020 vector<CTransaction> vDelete;
\r
1021 for (int i = 0; i < vConnect.size(); i++)
\r
1023 CBlockIndex* pindex = vConnect[i];
\r
1025 if (!block.ReadFromDisk(pindex->nFile, pindex->nBlockPos, true))
\r
1026 return error("Reorganize() : ReadFromDisk for connect failed");
\r
1027 if (!block.ConnectBlock(txdb, pindex))
\r
1029 // Invalid block, delete the rest of this branch
\r
1031 for (int j = i; j < vConnect.size(); j++)
\r
1033 CBlockIndex* pindex = vConnect[j];
\r
1034 pindex->EraseBlockFromDisk();
\r
1035 txdb.EraseBlockIndex(pindex->GetBlockHash());
\r
1036 mapBlockIndex.erase(pindex->GetBlockHash());
\r
1039 return error("Reorganize() : ConnectBlock failed");
\r
1042 // Queue memory transactions to delete
\r
1043 foreach(const CTransaction& tx, block.vtx)
\r
1044 vDelete.push_back(tx);
\r
1046 if (!txdb.WriteHashBestChain(pindexNew->GetBlockHash()))
\r
1047 return error("Reorganize() : WriteHashBestChain failed");
\r
1049 // Commit now because resurrecting could take some time
\r
1052 // Disconnect shorter branch
\r
1053 foreach(CBlockIndex* pindex, vDisconnect)
\r
1054 if (pindex->pprev)
\r
1055 pindex->pprev->pnext = NULL;
\r
1057 // Connect longer branch
\r
1058 foreach(CBlockIndex* pindex, vConnect)
\r
1059 if (pindex->pprev)
\r
1060 pindex->pprev->pnext = pindex;
\r
1062 // Resurrect memory transactions that were in the disconnected branch
\r
1063 foreach(CTransaction& tx, vResurrect)
\r
1064 tx.AcceptTransaction(txdb, false);
\r
1066 // Delete redundant memory transactions that are in the connected branch
\r
1067 foreach(CTransaction& tx, vDelete)
\r
1068 tx.RemoveFromMemoryPool();
\r
1074 bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos)
\r
1076 // Check for duplicate
\r
1077 uint256 hash = GetHash();
\r
1078 if (mapBlockIndex.count(hash))
\r
1079 return error("AddToBlockIndex() : %s already exists", hash.ToString().substr(0,14).c_str());
\r
1081 // Construct new block index object
\r
1082 CBlockIndex* pindexNew = new CBlockIndex(nFile, nBlockPos, *this);
\r
1084 return error("AddToBlockIndex() : new CBlockIndex failed");
\r
1085 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
\r
1086 pindexNew->phashBlock = &((*mi).first);
\r
1087 map<uint256, CBlockIndex*>::iterator miPrev = mapBlockIndex.find(hashPrevBlock);
\r
1088 if (miPrev != mapBlockIndex.end())
\r
1090 pindexNew->pprev = (*miPrev).second;
\r
1091 pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
\r
1096 txdb.WriteBlockIndex(CDiskBlockIndex(pindexNew));
\r
1099 if (pindexNew->nHeight > nBestHeight)
\r
1101 if (pindexGenesisBlock == NULL && hash == hashGenesisBlock)
\r
1103 pindexGenesisBlock = pindexNew;
\r
1104 txdb.WriteHashBestChain(hash);
\r
1106 else if (hashPrevBlock == hashBestChain)
\r
1108 // Adding to current best branch
\r
1109 if (!ConnectBlock(txdb, pindexNew) || !txdb.WriteHashBestChain(hash))
\r
1112 pindexNew->EraseBlockFromDisk();
\r
1113 mapBlockIndex.erase(pindexNew->GetBlockHash());
\r
1115 return error("AddToBlockIndex() : ConnectBlock failed");
\r
1118 pindexNew->pprev->pnext = pindexNew;
\r
1120 // Delete redundant memory transactions
\r
1121 foreach(CTransaction& tx, vtx)
\r
1122 tx.RemoveFromMemoryPool();
\r
1126 // New best branch
\r
1127 if (!Reorganize(txdb, pindexNew))
\r
1130 return error("AddToBlockIndex() : Reorganize failed");
\r
1135 hashBestChain = hash;
\r
1136 pindexBest = pindexNew;
\r
1137 nBestHeight = pindexBest->nHeight;
\r
1138 nTransactionsUpdated++;
\r
1139 printf("AddToBlockIndex: new best=%s height=%d\n", hashBestChain.ToString().substr(0,14).c_str(), nBestHeight);
\r
1145 if (pindexNew == pindexBest)
\r
1147 // Relay wallet transactions that haven't gotten in yet
\r
1148 RelayWalletTransactions();
\r
1150 // Notify UI to display prev block's coinbase if it was ours
\r
1151 static uint256 hashPrevBestCoinBase;
\r
1152 CRITICAL_BLOCK(cs_mapWallet)
\r
1153 vWalletUpdated.push_back(hashPrevBestCoinBase);
\r
1154 hashPrevBestCoinBase = vtx[0].GetHash();
\r
1157 MainFrameRepaint();
\r
1164 bool CBlock::CheckBlock() const
\r
1166 // These are checks that are independent of context
\r
1167 // that can be verified before saving an orphan block.
\r
1170 if (vtx.empty() || vtx.size() > MAX_SIZE || ::GetSerializeSize(*this, SER_DISK) > MAX_SIZE)
\r
1171 return error("CheckBlock() : size limits failed");
\r
1173 // Check timestamp
\r
1174 if (nTime > GetAdjustedTime() + 2 * 60 * 60)
\r
1175 return error("CheckBlock() : block timestamp too far in the future");
\r
1177 // First transaction must be coinbase, the rest must not be
\r
1178 if (vtx.empty() || !vtx[0].IsCoinBase())
\r
1179 return error("CheckBlock() : first tx is not coinbase");
\r
1180 for (int i = 1; i < vtx.size(); i++)
\r
1181 if (vtx[i].IsCoinBase())
\r
1182 return error("CheckBlock() : more than one coinbase");
\r
1184 // Check transactions
\r
1185 foreach(const CTransaction& tx, vtx)
\r
1186 if (!tx.CheckTransaction())
\r
1187 return error("CheckBlock() : CheckTransaction failed");
\r
1189 // Check proof of work matches claimed amount
\r
1190 if (CBigNum().SetCompact(nBits) > bnProofOfWorkLimit)
\r
1191 return error("CheckBlock() : nBits below minimum work");
\r
1192 if (GetHash() > CBigNum().SetCompact(nBits).getuint256())
\r
1193 return error("CheckBlock() : hash doesn't match nBits");
\r
1195 // Check merkleroot
\r
1196 if (hashMerkleRoot != BuildMerkleTree())
\r
1197 return error("CheckBlock() : hashMerkleRoot mismatch");
\r
1202 bool CBlock::AcceptBlock()
\r
1204 // Check for duplicate
\r
1205 uint256 hash = GetHash();
\r
1206 if (mapBlockIndex.count(hash))
\r
1207 return error("AcceptBlock() : block already in mapBlockIndex");
\r
1209 // Get prev block index
\r
1210 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashPrevBlock);
\r
1211 if (mi == mapBlockIndex.end())
\r
1212 return error("AcceptBlock() : prev block not found");
\r
1213 CBlockIndex* pindexPrev = (*mi).second;
\r
1215 // Check timestamp against prev
\r
1216 if (nTime <= pindexPrev->GetMedianTimePast())
\r
1217 return error("AcceptBlock() : block's timestamp is too early");
\r
1219 // Check that all transactions are finalized (starting around Dec 2009)
\r
1220 if (nBestHeight > 31000) // 25620 + 5320
\r
1221 foreach(const CTransaction& tx, vtx)
\r
1222 if (!tx.IsFinal(nTime))
\r
1223 return error("AcceptBlock() : contains a non-final transaction");
\r
1225 // Check proof of work
\r
1226 if (nBits != GetNextWorkRequired(pindexPrev))
\r
1227 return error("AcceptBlock() : incorrect proof of work");
\r
1229 // Write block to history file
\r
1230 if (!CheckDiskSpace(::GetSerializeSize(*this, SER_DISK)))
\r
1231 return error("AcceptBlock() : out of disk space");
\r
1232 unsigned int nFile;
\r
1233 unsigned int nBlockPos;
\r
1234 if (!WriteToDisk(!fClient, nFile, nBlockPos))
\r
1235 return error("AcceptBlock() : WriteToDisk failed");
\r
1236 if (!AddToBlockIndex(nFile, nBlockPos))
\r
1237 return error("AcceptBlock() : AddToBlockIndex failed");
\r
1239 if (hashBestChain == hash)
\r
1240 RelayInventory(CInv(MSG_BLOCK, hash));
\r
1242 // // Add atoms to user reviews for coins created
\r
1243 // vector<unsigned char> vchPubKey;
\r
1244 // if (ExtractPubKey(vtx[0].vout[0].scriptPubKey, false, vchPubKey))
\r
1246 // unsigned short nAtom = GetRand(USHRT_MAX - 100) + 100;
\r
1247 // vector<unsigned short> vAtoms(1, nAtom);
\r
1248 // AddAtomsAndPropagate(Hash(vchPubKey.begin(), vchPubKey.end()), vAtoms, true);
\r
1254 bool ProcessBlock(CNode* pfrom, CBlock* pblock)
\r
1256 // Check for duplicate
\r
1257 uint256 hash = pblock->GetHash();
\r
1258 if (mapBlockIndex.count(hash))
\r
1259 return error("ProcessBlock() : already have block %d %s", mapBlockIndex[hash]->nHeight, hash.ToString().substr(0,14).c_str());
\r
1260 if (mapOrphanBlocks.count(hash))
\r
1261 return error("ProcessBlock() : already have block (orphan) %s", hash.ToString().substr(0,14).c_str());
\r
1263 // Preliminary checks
\r
1264 if (!pblock->CheckBlock())
\r
1267 return error("ProcessBlock() : CheckBlock FAILED");
\r
1270 // If don't already have its previous block, shunt it off to holding area until we get it
\r
1271 if (!mapBlockIndex.count(pblock->hashPrevBlock))
\r
1273 printf("ProcessBlock: ORPHAN BLOCK, prev=%s\n", pblock->hashPrevBlock.ToString().substr(0,14).c_str());
\r
1274 mapOrphanBlocks.insert(make_pair(hash, pblock));
\r
1275 mapOrphanBlocksByPrev.insert(make_pair(pblock->hashPrevBlock, pblock));
\r
1277 // Ask this guy to fill in what we're missing
\r
1279 pfrom->PushMessage("getblocks", CBlockLocator(pindexBest), GetOrphanRoot(pblock));
\r
1284 if (!pblock->AcceptBlock())
\r
1287 return error("ProcessBlock() : AcceptBlock FAILED");
\r
1291 // Recursively process any orphan blocks that depended on this one
\r
1292 vector<uint256> vWorkQueue;
\r
1293 vWorkQueue.push_back(hash);
\r
1294 for (int i = 0; i < vWorkQueue.size(); i++)
\r
1296 uint256 hashPrev = vWorkQueue[i];
\r
1297 for (multimap<uint256, CBlock*>::iterator mi = mapOrphanBlocksByPrev.lower_bound(hashPrev);
\r
1298 mi != mapOrphanBlocksByPrev.upper_bound(hashPrev);
\r
1301 CBlock* pblockOrphan = (*mi).second;
\r
1302 if (pblockOrphan->AcceptBlock())
\r
1303 vWorkQueue.push_back(pblockOrphan->GetHash());
\r
1304 mapOrphanBlocks.erase(pblockOrphan->GetHash());
\r
1305 delete pblockOrphan;
\r
1307 mapOrphanBlocksByPrev.erase(hashPrev);
\r
1310 printf("ProcessBlock: ACCEPTED\n");
\r
1321 template<typename Stream>
\r
1322 bool ScanMessageStart(Stream& s)
\r
1324 // Scan ahead to the next pchMessageStart, which should normally be immediately
\r
1325 // at the file pointer. Leaves file pointer at end of pchMessageStart.
\r
1327 short prevmask = s.exceptions(0);
\r
1328 const char* p = BEGIN(pchMessageStart);
\r
1338 s.exceptions(prevmask);
\r
1342 p = BEGIN(pchMessageStart);
\r
1345 if (++p == END(pchMessageStart))
\r
1348 s.exceptions(prevmask);
\r
1357 s.exceptions(prevmask);
\r
1362 bool CheckDiskSpace(int64 nAdditionalBytes)
\r
1365 uint64 nFreeBytesAvailable = 0; // bytes available to caller
\r
1366 uint64 nTotalNumberOfBytes = 0; // bytes on disk
\r
1367 uint64 nTotalNumberOfFreeBytes = 0; // free bytes on disk
\r
1368 if (!GetDiskFreeSpaceEx(GetDataDir().c_str(),
\r
1369 (PULARGE_INTEGER)&nFreeBytesAvailable,
\r
1370 (PULARGE_INTEGER)&nTotalNumberOfBytes,
\r
1371 (PULARGE_INTEGER)&nTotalNumberOfFreeBytes))
\r
1373 printf("ERROR: GetDiskFreeSpaceEx() failed\n");
\r
1377 uint64 nFreeBytesAvailable = filesystem::space(GetDataDir()).available;
\r
1380 // Check for 15MB because database could create another 10MB log file at any time
\r
1381 if (nFreeBytesAvailable < (int64)15000000 + nAdditionalBytes)
\r
1384 ThreadSafeMessageBox("Warning: Your disk space is low ", "Bitcoin", wxOK | wxICON_EXCLAMATION);
\r
1385 _beginthread(Shutdown, 0, NULL);
\r
1391 FILE* OpenBlockFile(unsigned int nFile, unsigned int nBlockPos, const char* pszMode)
\r
1395 FILE* file = fopen(strprintf("%s/blk%04d.dat", GetDataDir().c_str(), nFile).c_str(), pszMode);
\r
1398 if (nBlockPos != 0 && !strchr(pszMode, 'a') && !strchr(pszMode, 'w'))
\r
1400 if (fseek(file, nBlockPos, SEEK_SET) != 0)
\r
1409 static unsigned int nCurrentBlockFile = 1;
\r
1411 FILE* AppendBlockFile(unsigned int& nFileRet)
\r
1416 FILE* file = OpenBlockFile(nCurrentBlockFile, 0, "ab");
\r
1419 if (fseek(file, 0, SEEK_END) != 0)
\r
1421 // FAT32 filesize max 4GB, fseek and ftell max 2GB, so we must stay under 2GB
\r
1422 if (ftell(file) < 0x7F000000 - MAX_SIZE)
\r
1424 nFileRet = nCurrentBlockFile;
\r
1428 nCurrentBlockFile++;
\r
1432 bool LoadBlockIndex(bool fAllowNew)
\r
1435 // Load block index
\r
1438 if (!txdb.LoadBlockIndex())
\r
1443 // Init with genesis block
\r
1445 if (mapBlockIndex.empty())
\r
1452 // GetHash() = 0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
\r
1453 // hashMerkleRoot = 0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
\r
1454 // txNew.vin[0].scriptSig = 486604799 4 0x736B6E616220726F662074756F6C69616220646E6F63657320666F206B6E697262206E6F20726F6C6C65636E61684320393030322F6E614A2F33302073656D695420656854
\r
1455 // txNew.vout[0].nValue = 5000000000
\r
1456 // txNew.vout[0].scriptPubKey = 0x5F1DF16B2B704C8A578D0BBAF74D385CDE12C11EE50455F3C438EF4C3FBCF649B6DE611FEAE06279A60939E028A8D65C10B73071A6F16719274855FEB0FD8A6704 OP_CHECKSIG
\r
1457 // block.nVersion = 1
\r
1458 // block.nTime = 1231006505
\r
1459 // block.nBits = 0x1d00ffff
\r
1460 // block.nNonce = 2083236893
\r
1461 // CBlock(hash=000000000019d6, ver=1, hashPrevBlock=00000000000000, hashMerkleRoot=4a5e1e, nTime=1231006505, nBits=1d00ffff, nNonce=2083236893, vtx=1)
\r
1462 // CTransaction(hash=4a5e1e, ver=1, vin.size=1, vout.size=1, nLockTime=0)
\r
1463 // CTxIn(COutPoint(000000, -1), coinbase 04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73)
\r
1464 // CTxOut(nValue=50.00000000, scriptPubKey=0x5F1DF16B2B704C8A578D0B)
\r
1465 // vMerkleTree: 4a5e1e
\r
1468 char* pszTimestamp = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks";
\r
1469 CTransaction txNew;
\r
1470 txNew.vin.resize(1);
\r
1471 txNew.vout.resize(1);
\r
1472 txNew.vin[0].scriptSig = CScript() << 486604799 << CBigNum(4) << vector<unsigned char>((unsigned char*)pszTimestamp, (unsigned char*)pszTimestamp + strlen(pszTimestamp));
\r
1473 txNew.vout[0].nValue = 50 * COIN;
\r
1474 txNew.vout[0].scriptPubKey = CScript() << CBigNum("0x5F1DF16B2B704C8A578D0BBAF74D385CDE12C11EE50455F3C438EF4C3FBCF649B6DE611FEAE06279A60939E028A8D65C10B73071A6F16719274855FEB0FD8A6704") << OP_CHECKSIG;
\r
1476 block.vtx.push_back(txNew);
\r
1477 block.hashPrevBlock = 0;
\r
1478 block.hashMerkleRoot = block.BuildMerkleTree();
\r
1479 block.nVersion = 1;
\r
1480 block.nTime = 1231006505;
\r
1481 block.nBits = 0x1d00ffff;
\r
1482 block.nNonce = 2083236893;
\r
1484 //// debug print, delete this later
\r
1485 printf("%s\n", block.GetHash().ToString().c_str());
\r
1486 printf("%s\n", block.hashMerkleRoot.ToString().c_str());
\r
1487 printf("%s\n", hashGenesisBlock.ToString().c_str());
\r
1488 txNew.vout[0].scriptPubKey.print();
\r
1490 assert(block.hashMerkleRoot == uint256("0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"));
\r
1492 assert(block.GetHash() == hashGenesisBlock);
\r
1494 // Start new block file
\r
1495 unsigned int nFile;
\r
1496 unsigned int nBlockPos;
\r
1497 if (!block.WriteToDisk(!fClient, nFile, nBlockPos))
\r
1498 return error("LoadBlockIndex() : writing genesis block to disk failed");
\r
1499 if (!block.AddToBlockIndex(nFile, nBlockPos))
\r
1500 return error("LoadBlockIndex() : genesis block not accepted");
\r
1508 void PrintBlockTree()
\r
1510 // precompute tree structure
\r
1511 map<CBlockIndex*, vector<CBlockIndex*> > mapNext;
\r
1512 for (map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.begin(); mi != mapBlockIndex.end(); ++mi)
\r
1514 CBlockIndex* pindex = (*mi).second;
\r
1515 mapNext[pindex->pprev].push_back(pindex);
\r
1517 //while (rand() % 3 == 0)
\r
1518 // mapNext[pindex->pprev].push_back(pindex);
\r
1521 vector<pair<int, CBlockIndex*> > vStack;
\r
1522 vStack.push_back(make_pair(0, pindexGenesisBlock));
\r
1525 while (!vStack.empty())
\r
1527 int nCol = vStack.back().first;
\r
1528 CBlockIndex* pindex = vStack.back().second;
\r
1529 vStack.pop_back();
\r
1531 // print split or gap
\r
1532 if (nCol > nPrevCol)
\r
1534 for (int i = 0; i < nCol-1; i++)
\r
1538 else if (nCol < nPrevCol)
\r
1540 for (int i = 0; i < nCol; i++)
\r
1547 for (int i = 0; i < nCol; i++)
\r
1552 block.ReadFromDisk(pindex, true);
\r
1553 printf("%d (%u,%u) %s %s tx %d",
\r
1556 pindex->nBlockPos,
\r
1557 block.GetHash().ToString().substr(0,14).c_str(),
\r
1558 DateTimeStrFormat("%x %H:%M:%S", block.nTime).c_str(),
\r
1559 block.vtx.size());
\r
1561 CRITICAL_BLOCK(cs_mapWallet)
\r
1563 if (mapWallet.count(block.vtx[0].GetHash()))
\r
1565 CWalletTx& wtx = mapWallet[block.vtx[0].GetHash()];
\r
1566 printf(" mine: %d %d %d", wtx.GetDepthInMainChain(), wtx.GetBlocksToMaturity(), wtx.GetCredit());
\r
1572 // put the main timechain first
\r
1573 vector<CBlockIndex*>& vNext = mapNext[pindex];
\r
1574 for (int i = 0; i < vNext.size(); i++)
\r
1576 if (vNext[i]->pnext)
\r
1578 swap(vNext[0], vNext[i]);
\r
1583 // iterate children
\r
1584 for (int i = 0; i < vNext.size(); i++)
\r
1585 vStack.push_back(make_pair(nCol+i, vNext[i]));
\r
1598 //////////////////////////////////////////////////////////////////////////////
\r
1604 bool AlreadyHave(CTxDB& txdb, const CInv& inv)
\r
1608 case MSG_TX: return mapTransactions.count(inv.hash) || txdb.ContainsTx(inv.hash);
\r
1609 case MSG_BLOCK: return mapBlockIndex.count(inv.hash) || mapOrphanBlocks.count(inv.hash);
\r
1610 case MSG_REVIEW: return true;
\r
1611 case MSG_PRODUCT: return mapProducts.count(inv.hash);
\r
1613 // Don't know what it is, just say we already got one
\r
1623 bool ProcessMessages(CNode* pfrom)
\r
1625 CDataStream& vRecv = pfrom->vRecv;
\r
1626 if (vRecv.empty())
\r
1628 //printf("ProcessMessages(%d bytes)\n", vRecv.size());
\r
1632 // (4) message start
\r
1640 // Scan for message start
\r
1641 CDataStream::iterator pstart = search(vRecv.begin(), vRecv.end(), BEGIN(pchMessageStart), END(pchMessageStart));
\r
1642 if (vRecv.end() - pstart < sizeof(CMessageHeader))
\r
1644 if (vRecv.size() > sizeof(CMessageHeader))
\r
1646 printf("\n\nPROCESSMESSAGE MESSAGESTART NOT FOUND\n\n");
\r
1647 vRecv.erase(vRecv.begin(), vRecv.end() - sizeof(CMessageHeader));
\r
1651 if (pstart - vRecv.begin() > 0)
\r
1652 printf("\n\nPROCESSMESSAGE SKIPPED %d BYTES\n\n", pstart - vRecv.begin());
\r
1653 vRecv.erase(vRecv.begin(), pstart);
\r
1656 CMessageHeader hdr;
\r
1658 if (!hdr.IsValid())
\r
1660 printf("\n\nPROCESSMESSAGE: ERRORS IN HEADER %s\n\n\n", hdr.GetCommand().c_str());
\r
1663 string strCommand = hdr.GetCommand();
\r
1666 unsigned int nMessageSize = hdr.nMessageSize;
\r
1667 if (nMessageSize > vRecv.size())
\r
1669 // Rewind and wait for rest of message
\r
1670 ///// need a mechanism to give up waiting for overlong message size error
\r
1671 //printf("message-break\n");
\r
1672 vRecv.insert(vRecv.begin(), BEGIN(hdr), END(hdr));
\r
1677 // Copy message to its own buffer
\r
1678 CDataStream vMsg(vRecv.begin(), vRecv.begin() + nMessageSize, vRecv.nType, vRecv.nVersion);
\r
1679 vRecv.ignore(nMessageSize);
\r
1681 // Process message
\r
1682 bool fRet = false;
\r
1685 CRITICAL_BLOCK(cs_main)
\r
1686 fRet = ProcessMessage(pfrom, strCommand, vMsg);
\r
1690 catch (std::ios_base::failure& e)
\r
1692 if (strstr(e.what(), "CDataStream::read() : end of data"))
\r
1694 // Allow exceptions from underlength message on vRecv
\r
1695 printf("ProcessMessage(%s, %d bytes) : Exception '%s' caught, normally caused by a message being shorter than its stated length\n", strCommand.c_str(), nMessageSize, e.what());
\r
1699 PrintException(&e, "ProcessMessage()");
\r
1702 catch (std::exception& e) {
\r
1703 PrintException(&e, "ProcessMessage()");
\r
1705 PrintException(NULL, "ProcessMessage()");
\r
1709 printf("ProcessMessage(%s, %d bytes) FAILED\n", strCommand.c_str(), nMessageSize);
\r
1719 bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
\r
1721 static map<unsigned int, vector<unsigned char> > mapReuseKey;
\r
1722 RandAddSeedPerfmon();
\r
1723 printf("received: %s (%d bytes)\n", strCommand.c_str(), vRecv.size());
\r
1724 if (nDropMessagesTest > 0 && GetRand(nDropMessagesTest) == 0)
\r
1726 printf("dropmessages DROPPING RECV MESSAGE\n");
\r
1734 if (strCommand == "version")
\r
1736 // Each connection can only send one version message
\r
1737 if (pfrom->nVersion != 0)
\r
1742 CAddress addrFrom;
\r
1743 uint64 nNonce = 1;
\r
1744 vRecv >> pfrom->nVersion >> pfrom->nServices >> nTime >> addrMe;
\r
1745 if (pfrom->nVersion >= 106 && !vRecv.empty())
\r
1746 vRecv >> addrFrom >> nNonce;
\r
1747 if (pfrom->nVersion == 0)
\r
1750 // Disconnect if we connected to ourself
\r
1751 if (nNonce == nLocalHostNonce)
\r
1753 pfrom->fDisconnect = true;
\r
1754 pfrom->vRecv.clear();
\r
1755 pfrom->vSend.clear();
\r
1759 pfrom->vSend.SetVersion(min(pfrom->nVersion, VERSION));
\r
1760 pfrom->vRecv.SetVersion(min(pfrom->nVersion, VERSION));
\r
1762 pfrom->fClient = !(pfrom->nServices & NODE_NETWORK);
\r
1763 if (pfrom->fClient)
\r
1765 pfrom->vSend.nType |= SER_BLOCKHEADERONLY;
\r
1766 pfrom->vRecv.nType |= SER_BLOCKHEADERONLY;
\r
1769 AddTimeData(pfrom->addr.ip, nTime);
\r
1771 // Ask the first connected node for block updates
\r
1772 static bool fAskedForBlocks;
\r
1773 if (!fAskedForBlocks && !pfrom->fClient)
\r
1775 fAskedForBlocks = true;
\r
1776 pfrom->PushMessage("getblocks", CBlockLocator(pindexBest), uint256(0));
\r
1779 pfrom->fSuccessfullyConnected = true;
\r
1781 // Update the last seen time
\r
1782 if (pfrom->fNetworkNode)
\r
1783 AddressCurrentlyConnected(pfrom->addr);
\r
1785 printf("version message: version %d\n", pfrom->nVersion);
\r
1789 else if (pfrom->nVersion == 0)
\r
1791 // Must have a version message before anything else
\r
1796 else if (strCommand == "addr")
\r
1798 vector<CAddress> vAddr;
\r
1801 // Store the new addresses
\r
1803 foreach(CAddress& addr, vAddr)
\r
1807 addr.nTime = GetAdjustedTime();
\r
1808 if (pfrom->fGetAddr)
\r
1809 addr.nTime -= 5 * 24 * 60 * 60;
\r
1810 AddAddress(addrdb, addr, false);
\r
1811 pfrom->AddAddressKnown(addr);
\r
1812 if (!pfrom->fGetAddr && addr.IsRoutable())
\r
1814 // Put on lists to send to other nodes
\r
1815 CRITICAL_BLOCK(cs_vNodes)
\r
1816 foreach(CNode* pnode, vNodes)
\r
1817 pnode->PushAddress(addr);
\r
1820 pfrom->fGetAddr = false;
\r
1824 else if (strCommand == "inv")
\r
1826 vector<CInv> vInv;
\r
1829 // Update the last seen time for this node's address
\r
1830 if (pfrom->fNetworkNode)
\r
1831 AddressCurrentlyConnected(pfrom->addr);
\r
1834 foreach(const CInv& inv, vInv)
\r
1838 pfrom->AddInventoryKnown(inv);
\r
1840 bool fAlreadyHave = AlreadyHave(txdb, inv);
\r
1841 printf(" got inventory: %s %s\n", inv.ToString().c_str(), fAlreadyHave ? "have" : "new");
\r
1843 if (!fAlreadyHave)
\r
1844 pfrom->AskFor(inv);
\r
1845 else if (inv.type == MSG_BLOCK && mapOrphanBlocks.count(inv.hash))
\r
1846 pfrom->PushMessage("getblocks", CBlockLocator(pindexBest), GetOrphanRoot(mapOrphanBlocks[inv.hash]));
\r
1851 else if (strCommand == "getdata")
\r
1853 vector<CInv> vInv;
\r
1856 foreach(const CInv& inv, vInv)
\r
1860 printf("received getdata for: %s\n", inv.ToString().c_str());
\r
1862 if (inv.type == MSG_BLOCK)
\r
1864 // Send block from disk
\r
1865 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(inv.hash);
\r
1866 if (mi != mapBlockIndex.end())
\r
1868 //// could optimize this to send header straight from blockindex for client
\r
1870 block.ReadFromDisk((*mi).second, !pfrom->fClient);
\r
1871 pfrom->PushMessage("block", block);
\r
1874 else if (inv.IsKnownType())
\r
1876 // Send stream from relay memory
\r
1877 CRITICAL_BLOCK(cs_mapRelay)
\r
1879 map<CInv, CDataStream>::iterator mi = mapRelay.find(inv);
\r
1880 if (mi != mapRelay.end())
\r
1881 pfrom->PushMessage(inv.GetCommand(), (*mi).second);
\r
1888 else if (strCommand == "getblocks")
\r
1890 CBlockLocator locator;
\r
1892 vRecv >> locator >> hashStop;
\r
1894 // Find the first block the caller has in the main chain
\r
1895 CBlockIndex* pindex = locator.GetBlockIndex();
\r
1897 // Send the rest of the chain
\r
1899 pindex = pindex->pnext;
\r
1900 printf("getblocks %d to %s\n", (pindex ? pindex->nHeight : -1), hashStop.ToString().substr(0,14).c_str());
\r
1901 for (; pindex; pindex = pindex->pnext)
\r
1903 if (pindex->GetBlockHash() == hashStop)
\r
1905 printf(" getblocks stopping at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString().substr(0,14).c_str());
\r
1909 // Bypass setInventoryKnown in case an inventory message got lost
\r
1910 CRITICAL_BLOCK(pfrom->cs_inventory)
\r
1912 CInv inv(MSG_BLOCK, pindex->GetBlockHash());
\r
1913 // returns true if wasn't already contained in the set
\r
1914 if (pfrom->setInventoryKnown2.insert(inv).second)
\r
1916 pfrom->setInventoryKnown.erase(inv);
\r
1917 pfrom->vInventoryToSend.push_back(inv);
\r
1924 else if (strCommand == "tx")
\r
1926 vector<uint256> vWorkQueue;
\r
1927 CDataStream vMsg(vRecv);
\r
1931 CInv inv(MSG_TX, tx.GetHash());
\r
1932 pfrom->AddInventoryKnown(inv);
\r
1934 bool fMissingInputs = false;
\r
1935 if (tx.AcceptTransaction(true, &fMissingInputs))
\r
1937 AddToWalletIfMine(tx, NULL);
\r
1938 RelayMessage(inv, vMsg);
\r
1939 mapAlreadyAskedFor.erase(inv);
\r
1940 vWorkQueue.push_back(inv.hash);
\r
1942 // Recursively process any orphan transactions that depended on this one
\r
1943 for (int i = 0; i < vWorkQueue.size(); i++)
\r
1945 uint256 hashPrev = vWorkQueue[i];
\r
1946 for (multimap<uint256, CDataStream*>::iterator mi = mapOrphanTransactionsByPrev.lower_bound(hashPrev);
\r
1947 mi != mapOrphanTransactionsByPrev.upper_bound(hashPrev);
\r
1950 const CDataStream& vMsg = *((*mi).second);
\r
1952 CDataStream(vMsg) >> tx;
\r
1953 CInv inv(MSG_TX, tx.GetHash());
\r
1955 if (tx.AcceptTransaction(true))
\r
1957 printf(" accepted orphan tx %s\n", inv.hash.ToString().substr(0,6).c_str());
\r
1958 AddToWalletIfMine(tx, NULL);
\r
1959 RelayMessage(inv, vMsg);
\r
1960 mapAlreadyAskedFor.erase(inv);
\r
1961 vWorkQueue.push_back(inv.hash);
\r
1966 foreach(uint256 hash, vWorkQueue)
\r
1967 EraseOrphanTx(hash);
\r
1969 else if (fMissingInputs)
\r
1971 printf("storing orphan tx %s\n", inv.hash.ToString().substr(0,6).c_str());
\r
1972 AddOrphanTx(vMsg);
\r
1977 else if (strCommand == "review")
\r
1979 CDataStream vMsg(vRecv);
\r
1983 CInv inv(MSG_REVIEW, review.GetHash());
\r
1984 pfrom->AddInventoryKnown(inv);
\r
1986 if (review.AcceptReview())
\r
1988 // Relay the original message as-is in case it's a higher version than we know how to parse
\r
1989 RelayMessage(inv, vMsg);
\r
1990 mapAlreadyAskedFor.erase(inv);
\r
1995 else if (strCommand == "block")
\r
1997 auto_ptr<CBlock> pblock(new CBlock);
\r
2001 printf("received block:\n"); pblock->print();
\r
2003 CInv inv(MSG_BLOCK, pblock->GetHash());
\r
2004 pfrom->AddInventoryKnown(inv);
\r
2006 if (ProcessBlock(pfrom, pblock.release()))
\r
2007 mapAlreadyAskedFor.erase(inv);
\r
2011 else if (strCommand == "getaddr")
\r
2013 pfrom->vAddrToSend.clear();
\r
2014 int64 nSince = GetAdjustedTime() - 5 * 24 * 60 * 60; // in the last 5 days
\r
2015 CRITICAL_BLOCK(cs_mapAddresses)
\r
2017 unsigned int nSize = mapAddresses.size();
\r
2018 foreach(const PAIRTYPE(vector<unsigned char>, CAddress)& item, mapAddresses)
\r
2022 const CAddress& addr = item.second;
\r
2023 if (addr.nTime > nSince)
\r
2024 pfrom->PushAddress(addr);
\r
2030 else if (strCommand == "checkorder")
\r
2032 uint256 hashReply;
\r
2034 vRecv >> hashReply >> order;
\r
2036 /// we have a chance to check the order here
\r
2038 // Keep giving the same key to the same ip until they use it
\r
2039 if (!mapReuseKey.count(pfrom->addr.ip))
\r
2040 mapReuseKey[pfrom->addr.ip] = GenerateNewKey();
\r
2042 // Send back approval of order and pubkey to use
\r
2043 CScript scriptPubKey;
\r
2044 scriptPubKey << mapReuseKey[pfrom->addr.ip] << OP_CHECKSIG;
\r
2045 pfrom->PushMessage("reply", hashReply, (int)0, scriptPubKey);
\r
2049 else if (strCommand == "submitorder")
\r
2051 uint256 hashReply;
\r
2053 vRecv >> hashReply >> wtxNew;
\r
2056 if (!wtxNew.AcceptWalletTransaction())
\r
2058 pfrom->PushMessage("reply", hashReply, (int)1);
\r
2059 return error("submitorder AcceptWalletTransaction() failed, returning error 1");
\r
2061 wtxNew.fTimeReceivedIsTxTime = true;
\r
2062 AddToWallet(wtxNew);
\r
2063 wtxNew.RelayWalletTransaction();
\r
2064 mapReuseKey.erase(pfrom->addr.ip);
\r
2066 // Send back confirmation
\r
2067 pfrom->PushMessage("reply", hashReply, (int)0);
\r
2071 else if (strCommand == "reply")
\r
2073 uint256 hashReply;
\r
2074 vRecv >> hashReply;
\r
2076 CRequestTracker tracker;
\r
2077 CRITICAL_BLOCK(pfrom->cs_mapRequests)
\r
2079 map<uint256, CRequestTracker>::iterator mi = pfrom->mapRequests.find(hashReply);
\r
2080 if (mi != pfrom->mapRequests.end())
\r
2082 tracker = (*mi).second;
\r
2083 pfrom->mapRequests.erase(mi);
\r
2086 if (!tracker.IsNull())
\r
2087 tracker.fn(tracker.param1, vRecv);
\r
2093 // Ignore unknown commands for extensibility
\r
2107 bool SendMessages(CNode* pto)
\r
2109 CRITICAL_BLOCK(cs_main)
\r
2111 // Don't send anything until we get their version message
\r
2112 if (pto->nVersion == 0)
\r
2115 // Address refresh broadcast
\r
2116 static int64 nLastRebroadcast;
\r
2117 if (nLastRebroadcast < GetTime() - 24 * 60 * 60) // every 24 hours
\r
2119 nLastRebroadcast = GetTime();
\r
2120 CRITICAL_BLOCK(cs_vNodes)
\r
2122 foreach(CNode* pnode, vNodes)
\r
2124 // Periodically clear setAddrKnown to allow refresh broadcasts
\r
2125 pnode->setAddrKnown.clear();
\r
2127 // Rebroadcast our address
\r
2128 if (addrLocalHost.IsRoutable() && !fUseProxy)
\r
2129 pnode->PushAddress(addrLocalHost);
\r
2138 vector<CAddress> vAddrToSend;
\r
2139 vAddrToSend.reserve(pto->vAddrToSend.size());
\r
2140 foreach(const CAddress& addr, pto->vAddrToSend)
\r
2142 // returns true if wasn't already contained in the set
\r
2143 if (pto->setAddrKnown.insert(addr).second)
\r
2144 vAddrToSend.push_back(addr);
\r
2146 pto->vAddrToSend.clear();
\r
2147 if (!vAddrToSend.empty())
\r
2148 pto->PushMessage("addr", vAddrToSend);
\r
2152 // Message: inventory
\r
2154 vector<CInv> vInventoryToSend;
\r
2155 CRITICAL_BLOCK(pto->cs_inventory)
\r
2157 vInventoryToSend.reserve(pto->vInventoryToSend.size());
\r
2158 foreach(const CInv& inv, pto->vInventoryToSend)
\r
2160 // returns true if wasn't already contained in the set
\r
2161 if (pto->setInventoryKnown.insert(inv).second)
\r
2162 vInventoryToSend.push_back(inv);
\r
2164 pto->vInventoryToSend.clear();
\r
2165 pto->setInventoryKnown2.clear();
\r
2167 if (!vInventoryToSend.empty())
\r
2168 pto->PushMessage("inv", vInventoryToSend);
\r
2172 // Message: getdata
\r
2174 vector<CInv> vAskFor;
\r
2175 int64 nNow = GetTime() * 1000000;
\r
2177 while (!pto->mapAskFor.empty() && (*pto->mapAskFor.begin()).first <= nNow)
\r
2179 const CInv& inv = (*pto->mapAskFor.begin()).second;
\r
2180 if (!AlreadyHave(txdb, inv))
\r
2182 printf("sending getdata: %s\n", inv.ToString().c_str());
\r
2183 vAskFor.push_back(inv);
\r
2185 pto->mapAskFor.erase(pto->mapAskFor.begin());
\r
2187 if (!vAskFor.empty())
\r
2188 pto->PushMessage("getdata", vAskFor);
\r
2207 //////////////////////////////////////////////////////////////////////////////
\r
2212 void GenerateBitcoins(bool fGenerate)
\r
2214 if (fGenerateBitcoins != fGenerate)
\r
2216 fGenerateBitcoins = fGenerate;
\r
2217 CWalletDB().WriteSetting("fGenerateBitcoins", fGenerateBitcoins);
\r
2218 MainFrameRepaint();
\r
2220 if (fGenerateBitcoins)
\r
2222 int nProcessors = wxThread::GetCPUCount();
\r
2223 printf("%d processors\n", nProcessors);
\r
2224 if (nProcessors < 1)
\r
2226 if (fLimitProcessors && nProcessors > nLimitProcessors)
\r
2227 nProcessors = nLimitProcessors;
\r
2228 int nAddThreads = nProcessors - vnThreadsRunning[3];
\r
2229 printf("Starting %d BitcoinMiner threads\n", nAddThreads);
\r
2230 for (int i = 0; i < nAddThreads; i++)
\r
2231 if (_beginthread(ThreadBitcoinMiner, 0, NULL) == -1)
\r
2232 printf("Error: _beginthread(ThreadBitcoinMiner) failed\n");
\r
2236 void ThreadBitcoinMiner(void* parg)
\r
2240 vnThreadsRunning[3]++;
\r
2242 vnThreadsRunning[3]--;
\r
2244 catch (std::exception& e) {
\r
2245 vnThreadsRunning[3]--;
\r
2246 PrintException(&e, "ThreadBitcoinMiner()");
\r
2248 vnThreadsRunning[3]--;
\r
2249 PrintException(NULL, "ThreadBitcoinMiner()");
\r
2252 printf("ThreadBitcoinMiner exiting, %d threads remaining\n", vnThreadsRunning[3]);
\r
2255 int FormatHashBlocks(void* pbuffer, unsigned int len)
\r
2257 unsigned char* pdata = (unsigned char*)pbuffer;
\r
2258 unsigned int blocks = 1 + ((len + 8) / 64);
\r
2259 unsigned char* pend = pdata + 64 * blocks;
\r
2260 memset(pdata + len, 0, 64 * blocks - len);
\r
2261 pdata[len] = 0x80;
\r
2262 unsigned int bits = len * 8;
\r
2263 pend[-1] = (bits >> 0) & 0xff;
\r
2264 pend[-2] = (bits >> 8) & 0xff;
\r
2265 pend[-3] = (bits >> 16) & 0xff;
\r
2266 pend[-4] = (bits >> 24) & 0xff;
\r
2270 using CryptoPP::ByteReverse;
\r
2271 static int detectlittleendian = 1;
\r
2273 void BlockSHA256(const void* pin, unsigned int nBlocks, void* pout)
\r
2275 unsigned int* pinput = (unsigned int*)pin;
\r
2276 unsigned int* pstate = (unsigned int*)pout;
\r
2278 CryptoPP::SHA256::InitState(pstate);
\r
2280 if (*(char*)&detectlittleendian != 0)
\r
2282 for (int n = 0; n < nBlocks; n++)
\r
2284 unsigned int pbuf[16];
\r
2285 for (int i = 0; i < 16; i++)
\r
2286 pbuf[i] = ByteReverse(pinput[n * 16 + i]);
\r
2287 CryptoPP::SHA256::Transform(pstate, pbuf);
\r
2289 for (int i = 0; i < 8; i++)
\r
2290 pstate[i] = ByteReverse(pstate[i]);
\r
2294 for (int n = 0; n < nBlocks; n++)
\r
2295 CryptoPP::SHA256::Transform(pstate, pinput + n * 16);
\r
2300 void BitcoinMiner()
\r
2302 printf("BitcoinMiner started\n");
\r
2306 CBigNum bnExtraNonce = 0;
\r
2307 while (fGenerateBitcoins)
\r
2309 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_LOWEST);
\r
2313 while (vNodes.empty())
\r
2320 unsigned int nTransactionsUpdatedLast = nTransactionsUpdated;
\r
2321 CBlockIndex* pindexPrev = pindexBest;
\r
2322 unsigned int nBits = GetNextWorkRequired(pindexPrev);
\r
2326 // Create coinbase tx
\r
2328 CTransaction txNew;
\r
2329 txNew.vin.resize(1);
\r
2330 txNew.vin[0].prevout.SetNull();
\r
2331 txNew.vin[0].scriptSig << nBits << ++bnExtraNonce;
\r
2332 txNew.vout.resize(1);
\r
2333 txNew.vout[0].scriptPubKey << key.GetPubKey() << OP_CHECKSIG;
\r
2337 // Create new block
\r
2339 auto_ptr<CBlock> pblock(new CBlock());
\r
2340 if (!pblock.get())
\r
2343 // Add our coinbase tx as first transaction
\r
2344 pblock->vtx.push_back(txNew);
\r
2346 // Collect the latest transactions into the block
\r
2348 CRITICAL_BLOCK(cs_main)
\r
2349 CRITICAL_BLOCK(cs_mapTransactions)
\r
2352 map<uint256, CTxIndex> mapTestPool;
\r
2353 vector<char> vfAlreadyAdded(mapTransactions.size());
\r
2354 bool fFoundSomething = true;
\r
2355 unsigned int nBlockSize = 0;
\r
2356 while (fFoundSomething && nBlockSize < MAX_SIZE/2)
\r
2358 fFoundSomething = false;
\r
2359 unsigned int n = 0;
\r
2360 for (map<uint256, CTransaction>::iterator mi = mapTransactions.begin(); mi != mapTransactions.end(); ++mi, ++n)
\r
2362 if (vfAlreadyAdded[n])
\r
2364 CTransaction& tx = (*mi).second;
\r
2365 if (tx.IsCoinBase() || !tx.IsFinal())
\r
2368 // Transaction fee requirements, mainly only needed for flood control
\r
2369 // Under 10K (about 80 inputs) is free for first 100 transactions
\r
2370 // Base rate is 0.01 per KB
\r
2371 int64 nMinFee = tx.GetMinFee(pblock->vtx.size() < 100);
\r
2373 map<uint256, CTxIndex> mapTestPoolTmp(mapTestPool);
\r
2374 if (!tx.ConnectInputs(txdb, mapTestPoolTmp, CDiskTxPos(1,1,1), 0, nFees, false, true, nMinFee))
\r
2376 swap(mapTestPool, mapTestPoolTmp);
\r
2378 pblock->vtx.push_back(tx);
\r
2379 nBlockSize += ::GetSerializeSize(tx, SER_NETWORK);
\r
2380 vfAlreadyAdded[n] = true;
\r
2381 fFoundSomething = true;
\r
2385 pblock->nBits = nBits;
\r
2386 pblock->vtx[0].vout[0].nValue = pblock->GetBlockValue(nFees);
\r
2387 printf("Running BitcoinMiner with %d transactions in block\n", pblock->vtx.size());
\r
2391 // Prebuild hash buffer
\r
2398 uint256 hashPrevBlock;
\r
2399 uint256 hashMerkleRoot;
\r
2400 unsigned int nTime;
\r
2401 unsigned int nBits;
\r
2402 unsigned int nNonce;
\r
2405 unsigned char pchPadding0[64];
\r
2407 unsigned char pchPadding1[64];
\r
2411 tmp.block.nVersion = pblock->nVersion;
\r
2412 tmp.block.hashPrevBlock = pblock->hashPrevBlock = (pindexPrev ? pindexPrev->GetBlockHash() : 0);
\r
2413 tmp.block.hashMerkleRoot = pblock->hashMerkleRoot = pblock->BuildMerkleTree();
\r
2414 tmp.block.nTime = pblock->nTime = max((pindexPrev ? pindexPrev->GetMedianTimePast()+1 : 0), GetAdjustedTime());
\r
2415 tmp.block.nBits = pblock->nBits = nBits;
\r
2416 tmp.block.nNonce = pblock->nNonce = 1;
\r
2418 unsigned int nBlocks0 = FormatHashBlocks(&tmp.block, sizeof(tmp.block));
\r
2419 unsigned int nBlocks1 = FormatHashBlocks(&tmp.hash1, sizeof(tmp.hash1));
\r
2425 unsigned int nStart = GetTime();
\r
2426 uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
\r
2430 BlockSHA256(&tmp.block, nBlocks0, &tmp.hash1);
\r
2431 BlockSHA256(&tmp.hash1, nBlocks1, &hash);
\r
2433 if (hash <= hashTarget)
\r
2435 pblock->nNonce = tmp.block.nNonce;
\r
2436 assert(hash == pblock->GetHash());
\r
2439 printf("BitcoinMiner:\n");
\r
2440 printf("proof-of-work found \n hash: %s \ntarget: %s\n", hash.GetHex().c_str(), hashTarget.GetHex().c_str());
\r
2443 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
\r
2444 CRITICAL_BLOCK(cs_main)
\r
2446 if (pindexPrev == pindexBest)
\r
2453 // Process this block the same as if we had received it from another node
\r
2454 if (!ProcessBlock(NULL, pblock.release()))
\r
2455 printf("ERROR in BitcoinMiner, ProcessBlock, block not accepted\n");
\r
2458 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_LOWEST);
\r
2464 // Update nTime every few seconds
\r
2465 if ((++tmp.block.nNonce & 0xffff) == 0)
\r
2469 if (!fGenerateBitcoins)
\r
2471 if (fLimitProcessors && vnThreadsRunning[3] > nLimitProcessors)
\r
2473 if (tmp.block.nNonce == 0)
\r
2475 if (pindexPrev != pindexBest)
\r
2477 if (nTransactionsUpdated != nTransactionsUpdatedLast && GetTime() - nStart > 60)
\r
2479 if (vNodes.empty())
\r
2481 tmp.block.nTime = pblock->nTime = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
\r
2504 //////////////////////////////////////////////////////////////////////////////
\r
2510 int64 GetBalance()
\r
2512 int64 nStart = GetTimeMillis();
\r
2515 CRITICAL_BLOCK(cs_mapWallet)
\r
2517 for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
\r
2519 CWalletTx* pcoin = &(*it).second;
\r
2520 if (!pcoin->IsFinal() || pcoin->fSpent)
\r
2522 nTotal += pcoin->GetCredit(true);
\r
2526 //printf("GetBalance() %"PRI64d"ms\n", GetTimeMillis() - nStart);
\r
2532 bool SelectCoins(int64 nTargetValue, set<CWalletTx*>& setCoinsRet)
\r
2534 setCoinsRet.clear();
\r
2536 // List of values less than target
\r
2537 int64 nLowestLarger = _I64_MAX;
\r
2538 CWalletTx* pcoinLowestLarger = NULL;
\r
2539 vector<pair<int64, CWalletTx*> > vValue;
\r
2540 int64 nTotalLower = 0;
\r
2542 CRITICAL_BLOCK(cs_mapWallet)
\r
2544 for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
\r
2546 CWalletTx* pcoin = &(*it).second;
\r
2547 if (!pcoin->IsFinal() || pcoin->fSpent)
\r
2549 int64 n = pcoin->GetCredit();
\r
2552 if (n < nTargetValue)
\r
2554 vValue.push_back(make_pair(n, pcoin));
\r
2557 else if (n == nTargetValue)
\r
2559 setCoinsRet.insert(pcoin);
\r
2562 else if (n < nLowestLarger)
\r
2564 nLowestLarger = n;
\r
2565 pcoinLowestLarger = pcoin;
\r
2570 if (nTotalLower < nTargetValue)
\r
2572 if (pcoinLowestLarger == NULL)
\r
2574 setCoinsRet.insert(pcoinLowestLarger);
\r
2578 // Solve subset sum by stochastic approximation
\r
2579 sort(vValue.rbegin(), vValue.rend());
\r
2580 vector<char> vfIncluded;
\r
2581 vector<char> vfBest(vValue.size(), true);
\r
2582 int64 nBest = nTotalLower;
\r
2584 for (int nRep = 0; nRep < 1000 && nBest != nTargetValue; nRep++)
\r
2586 vfIncluded.assign(vValue.size(), false);
\r
2588 bool fReachedTarget = false;
\r
2589 for (int nPass = 0; nPass < 2 && !fReachedTarget; nPass++)
\r
2591 for (int i = 0; i < vValue.size(); i++)
\r
2593 if (nPass == 0 ? rand() % 2 : !vfIncluded[i])
\r
2595 nTotal += vValue[i].first;
\r
2596 vfIncluded[i] = true;
\r
2597 if (nTotal >= nTargetValue)
\r
2599 fReachedTarget = true;
\r
2600 if (nTotal < nBest)
\r
2603 vfBest = vfIncluded;
\r
2605 nTotal -= vValue[i].first;
\r
2606 vfIncluded[i] = false;
\r
2613 // If the next larger is still closer, return it
\r
2614 if (pcoinLowestLarger && nLowestLarger - nTargetValue <= nBest - nTargetValue)
\r
2615 setCoinsRet.insert(pcoinLowestLarger);
\r
2618 for (int i = 0; i < vValue.size(); i++)
\r
2620 setCoinsRet.insert(vValue[i].second);
\r
2623 printf("SelectCoins() best subset: ");
\r
2624 for (int i = 0; i < vValue.size(); i++)
\r
2626 printf("%s ", FormatMoney(vValue[i].first).c_str());
\r
2627 printf("total %s\n", FormatMoney(nBest).c_str());
\r
2636 bool CreateTransaction(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, CKey& keyRet, int64& nFeeRequiredRet)
\r
2638 nFeeRequiredRet = 0;
\r
2639 CRITICAL_BLOCK(cs_main)
\r
2641 // txdb must be opened before the mapWallet lock
\r
2643 CRITICAL_BLOCK(cs_mapWallet)
\r
2645 int64 nFee = nTransactionFee;
\r
2648 wtxNew.vin.clear();
\r
2649 wtxNew.vout.clear();
\r
2652 int64 nValueOut = nValue;
\r
2655 // Choose coins to use
\r
2656 set<CWalletTx*> setCoins;
\r
2657 if (!SelectCoins(nValue, setCoins))
\r
2659 int64 nValueIn = 0;
\r
2660 foreach(CWalletTx* pcoin, setCoins)
\r
2661 nValueIn += pcoin->GetCredit();
\r
2663 // Fill a vout to the payee
\r
2664 bool fChangeFirst = GetRand(2);
\r
2665 if (!fChangeFirst)
\r
2666 wtxNew.vout.push_back(CTxOut(nValueOut, scriptPubKey));
\r
2668 // Fill a vout back to self with any change
\r
2669 if (nValueIn > nValue)
\r
2671 // New private key
\r
2672 if (keyRet.IsNull())
\r
2673 keyRet.MakeNewKey();
\r
2675 // Fill a vout to ourself
\r
2676 CScript scriptPubKey;
\r
2677 scriptPubKey << keyRet.GetPubKey() << OP_CHECKSIG;
\r
2678 wtxNew.vout.push_back(CTxOut(nValueIn - nValue, scriptPubKey));
\r
2681 // Fill a vout to the payee
\r
2683 wtxNew.vout.push_back(CTxOut(nValueOut, scriptPubKey));
\r
2686 foreach(CWalletTx* pcoin, setCoins)
\r
2687 for (int nOut = 0; nOut < pcoin->vout.size(); nOut++)
\r
2688 if (pcoin->vout[nOut].IsMine())
\r
2689 wtxNew.vin.push_back(CTxIn(pcoin->GetHash(), nOut));
\r
2693 foreach(CWalletTx* pcoin, setCoins)
\r
2694 for (int nOut = 0; nOut < pcoin->vout.size(); nOut++)
\r
2695 if (pcoin->vout[nOut].IsMine())
\r
2696 SignSignature(*pcoin, wtxNew, nIn++);
\r
2698 // Check that enough fee is included
\r
2699 if (nFee < wtxNew.GetMinFee(true))
\r
2701 nFee = nFeeRequiredRet = wtxNew.GetMinFee(true);
\r
2705 // Fill vtxPrev by copying from previous transactions vtxPrev
\r
2706 wtxNew.AddSupportingTransactions(txdb);
\r
2707 wtxNew.fTimeReceivedIsTxTime = true;
\r
2716 // Call after CreateTransaction unless you want to abort
\r
2717 bool CommitTransactionSpent(const CWalletTx& wtxNew, const CKey& key)
\r
2719 CRITICAL_BLOCK(cs_main)
\r
2720 CRITICAL_BLOCK(cs_mapWallet)
\r
2722 //// todo: eventually should make this transactional, never want to add a
\r
2723 //// transaction without marking spent transactions, although the risk of
\r
2724 //// interruption during this step is remote.
\r
2726 // This is only to keep the database open to defeat the auto-flush for the
\r
2727 // duration of this scope. This is the only place where this optimization
\r
2728 // maybe makes sense; please don't do it anywhere else. Keeping databases
\r
2729 // open longer than necessary can create deadlocks.
\r
2730 CWalletDB walletdb("r");
\r
2732 // Add the change's private key to wallet
\r
2733 if (!key.IsNull() && !AddKey(key))
\r
2734 throw runtime_error("CommitTransactionSpent() : AddKey failed\n");
\r
2736 // Add tx to wallet, because if it has change it's also ours,
\r
2737 // otherwise just for transaction history.
\r
2738 AddToWallet(wtxNew);
\r
2740 // Mark old coins as spent
\r
2741 set<CWalletTx*> setCoins;
\r
2742 foreach(const CTxIn& txin, wtxNew.vin)
\r
2743 setCoins.insert(&mapWallet[txin.prevout.hash]);
\r
2744 foreach(CWalletTx* pcoin, setCoins)
\r
2746 pcoin->fSpent = true;
\r
2747 pcoin->WriteToDisk();
\r
2748 vWalletUpdated.push_back(pcoin->GetHash());
\r
2751 MainFrameRepaint();
\r
2758 bool SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew)
\r
2760 CRITICAL_BLOCK(cs_main)
\r
2763 int64 nFeeRequired;
\r
2764 if (!CreateTransaction(scriptPubKey, nValue, wtxNew, key, nFeeRequired))
\r
2767 if (nValue + nFeeRequired > GetBalance())
\r
2768 strError = strprintf("Error: This is an oversized transaction that requires a transaction fee of %s ", FormatMoney(nFeeRequired).c_str());
\r
2770 strError = "Error: Transaction creation failed ";
\r
2771 wxMessageBox(strError, "Sending...");
\r
2772 return error("SendMoney() : %s", strError.c_str());
\r
2774 if (!CommitTransactionSpent(wtxNew, key))
\r
2776 wxMessageBox("Error finalizing transaction ", "Sending...");
\r
2777 return error("SendMoney() : Error finalizing transaction");
\r
2780 printf("SendMoney: %s\n", wtxNew.GetHash().ToString().substr(0,6).c_str());
\r
2783 if (!wtxNew.AcceptTransaction())
\r
2785 // This must not fail. The transaction has already been signed and recorded.
\r
2786 throw runtime_error("SendMoney() : wtxNew.AcceptTransaction() failed\n");
\r
2787 wxMessageBox("Error: Transaction not valid ", "Sending...");
\r
2788 return error("SendMoney() : Error: Transaction not valid");
\r
2790 wtxNew.RelayWalletTransaction();
\r
2792 MainFrameRepaint();
\r