1 // Copyright (c) 2009-2010 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
6 #include "cryptopp/sha.h"
\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 CBigNum bnBestChainWork = 0;
\r
28 CBigNum bnBestInvalidWork = 0;
\r
29 uint256 hashBestChain = 0;
\r
30 CBlockIndex* pindexBest = NULL;
\r
31 int64 nTimeBestReceived = 0;
\r
33 map<uint256, CBlock*> mapOrphanBlocks;
\r
34 multimap<uint256, CBlock*> mapOrphanBlocksByPrev;
\r
36 map<uint256, CDataStream*> mapOrphanTransactions;
\r
37 multimap<uint256, CDataStream*> mapOrphanTransactionsByPrev;
\r
39 map<uint256, CWalletTx> mapWallet;
\r
40 vector<uint256> vWalletUpdated;
\r
41 CCriticalSection cs_mapWallet;
\r
43 map<vector<unsigned char>, CPrivKey> mapKeys;
\r
44 map<uint160, vector<unsigned char> > mapPubKeys;
\r
45 CCriticalSection cs_mapKeys;
\r
48 map<uint256, int> mapRequestCount;
\r
49 CCriticalSection cs_mapRequestCount;
\r
51 map<string, string> mapAddressBook;
\r
52 CCriticalSection cs_mapAddressBook;
\r
54 vector<unsigned char> vchDefaultKey;
\r
56 double dHashesPerSec;
\r
57 int64 nHPSTimerStart;
\r
60 int fGenerateBitcoins = false;
\r
61 int64 nTransactionFee = 0;
\r
62 CAddress addrIncoming;
\r
63 int fLimitProcessors = false;
\r
64 int nLimitProcessors = 1;
\r
65 int fMinimizeToTray = true;
\r
66 int fMinimizeOnClose = true;
\r
73 //////////////////////////////////////////////////////////////////////////////
\r
78 bool AddKey(const CKey& key)
\r
80 CRITICAL_BLOCK(cs_mapKeys)
\r
82 mapKeys[key.GetPubKey()] = key.GetPrivKey();
\r
83 mapPubKeys[Hash160(key.GetPubKey())] = key.GetPubKey();
\r
85 return CWalletDB().WriteKey(key.GetPubKey(), key.GetPrivKey());
\r
88 vector<unsigned char> GenerateNewKey()
\r
90 RandAddSeedPerfmon();
\r
94 throw runtime_error("GenerateNewKey() : AddKey failed\n");
\r
95 return key.GetPubKey();
\r
101 //////////////////////////////////////////////////////////////////////////////
\r
106 bool AddToWallet(const CWalletTx& wtxIn)
\r
108 uint256 hash = wtxIn.GetHash();
\r
109 CRITICAL_BLOCK(cs_mapWallet)
\r
111 // Inserts only if not already there, returns tx inserted or tx found
\r
112 pair<map<uint256, CWalletTx>::iterator, bool> ret = mapWallet.insert(make_pair(hash, wtxIn));
\r
113 CWalletTx& wtx = (*ret.first).second;
\r
114 bool fInsertedNew = ret.second;
\r
116 wtx.nTimeReceived = GetAdjustedTime();
\r
118 bool fUpdated = false;
\r
122 if (wtxIn.hashBlock != 0 && wtxIn.hashBlock != wtx.hashBlock)
\r
124 wtx.hashBlock = wtxIn.hashBlock;
\r
127 if (wtxIn.nIndex != -1 && (wtxIn.vMerkleBranch != wtx.vMerkleBranch || wtxIn.nIndex != wtx.nIndex))
\r
129 wtx.vMerkleBranch = wtxIn.vMerkleBranch;
\r
130 wtx.nIndex = wtxIn.nIndex;
\r
133 if (wtxIn.fFromMe && wtxIn.fFromMe != wtx.fFromMe)
\r
135 wtx.fFromMe = wtxIn.fFromMe;
\r
138 if (wtxIn.fSpent && wtxIn.fSpent != wtx.fSpent)
\r
140 wtx.fSpent = wtxIn.fSpent;
\r
146 printf("AddToWallet %s %s%s\n", wtxIn.GetHash().ToString().substr(0,6).c_str(), (fInsertedNew ? "new" : ""), (fUpdated ? "update" : ""));
\r
149 if (fInsertedNew || fUpdated)
\r
150 if (!wtx.WriteToDisk())
\r
153 // If default receiving address gets used, replace it with a new one
\r
154 CScript scriptDefaultKey;
\r
155 scriptDefaultKey.SetBitcoinAddress(vchDefaultKey);
\r
156 foreach(const CTxOut& txout, wtx.vout)
\r
158 if (txout.scriptPubKey == scriptDefaultKey)
\r
160 CWalletDB walletdb;
\r
161 walletdb.WriteDefaultKey(GenerateNewKey());
\r
162 walletdb.WriteName(PubKeyToAddress(vchDefaultKey), "");
\r
167 vWalletUpdated.push_back(hash);
\r
171 MainFrameRepaint();
\r
175 bool AddToWalletIfMine(const CTransaction& tx, const CBlock* pblock)
\r
177 if (tx.IsMine() || mapWallet.count(tx.GetHash()))
\r
180 // Get merkle branch if transaction was found in a block
\r
182 wtx.SetMerkleBranch(pblock);
\r
183 return AddToWallet(wtx);
\r
188 bool EraseFromWallet(uint256 hash)
\r
190 CRITICAL_BLOCK(cs_mapWallet)
\r
192 if (mapWallet.erase(hash))
\r
193 CWalletDB().EraseTx(hash);
\r
198 void WalletUpdateSpent(const COutPoint& prevout)
\r
200 // Anytime a signature is successfully verified, it's proof the outpoint is spent.
\r
201 // Update the wallet spent flag if it doesn't know due to wallet.dat being
\r
202 // restored from backup or the user making copies of wallet.dat.
\r
203 CRITICAL_BLOCK(cs_mapWallet)
\r
205 map<uint256, CWalletTx>::iterator mi = mapWallet.find(prevout.hash);
\r
206 if (mi != mapWallet.end())
\r
208 CWalletTx& wtx = (*mi).second;
\r
209 if (!wtx.fSpent && wtx.vout[prevout.n].IsMine())
\r
211 printf("WalletUpdateSpent found spent coin %sbc %s\n", FormatMoney(wtx.GetCredit()).c_str(), wtx.GetHash().ToString().c_str());
\r
214 vWalletUpdated.push_back(prevout.hash);
\r
227 //////////////////////////////////////////////////////////////////////////////
\r
229 // mapOrphanTransactions
\r
232 void AddOrphanTx(const CDataStream& vMsg)
\r
235 CDataStream(vMsg) >> tx;
\r
236 uint256 hash = tx.GetHash();
\r
237 if (mapOrphanTransactions.count(hash))
\r
239 CDataStream* pvMsg = mapOrphanTransactions[hash] = new CDataStream(vMsg);
\r
240 foreach(const CTxIn& txin, tx.vin)
\r
241 mapOrphanTransactionsByPrev.insert(make_pair(txin.prevout.hash, pvMsg));
\r
244 void EraseOrphanTx(uint256 hash)
\r
246 if (!mapOrphanTransactions.count(hash))
\r
248 const CDataStream* pvMsg = mapOrphanTransactions[hash];
\r
250 CDataStream(*pvMsg) >> tx;
\r
251 foreach(const CTxIn& txin, tx.vin)
\r
253 for (multimap<uint256, CDataStream*>::iterator mi = mapOrphanTransactionsByPrev.lower_bound(txin.prevout.hash);
\r
254 mi != mapOrphanTransactionsByPrev.upper_bound(txin.prevout.hash);)
\r
256 if ((*mi).second == pvMsg)
\r
257 mapOrphanTransactionsByPrev.erase(mi++);
\r
263 mapOrphanTransactions.erase(hash);
\r
273 //////////////////////////////////////////////////////////////////////////////
\r
278 bool CTxIn::IsMine() const
\r
280 CRITICAL_BLOCK(cs_mapWallet)
\r
282 map<uint256, CWalletTx>::iterator mi = mapWallet.find(prevout.hash);
\r
283 if (mi != mapWallet.end())
\r
285 const CWalletTx& prev = (*mi).second;
\r
286 if (prevout.n < prev.vout.size())
\r
287 if (prev.vout[prevout.n].IsMine())
\r
294 int64 CTxIn::GetDebit() const
\r
296 CRITICAL_BLOCK(cs_mapWallet)
\r
298 map<uint256, CWalletTx>::iterator mi = mapWallet.find(prevout.hash);
\r
299 if (mi != mapWallet.end())
\r
301 const CWalletTx& prev = (*mi).second;
\r
302 if (prevout.n < prev.vout.size())
\r
303 if (prev.vout[prevout.n].IsMine())
\r
304 return prev.vout[prevout.n].nValue;
\r
310 int64 CWalletTx::GetTxTime() const
\r
312 if (!fTimeReceivedIsTxTime && hashBlock != 0)
\r
314 // If we did not receive the transaction directly, we rely on the block's
\r
315 // time to figure out when it happened. We use the median over a range
\r
316 // of blocks to try to filter out inaccurate block times.
\r
317 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
\r
318 if (mi != mapBlockIndex.end())
\r
320 CBlockIndex* pindex = (*mi).second;
\r
322 return pindex->GetMedianTime();
\r
325 return nTimeReceived;
\r
328 int CWalletTx::GetRequestCount() const
\r
330 // Returns -1 if it wasn't being tracked
\r
331 int nRequests = -1;
\r
332 CRITICAL_BLOCK(cs_mapRequestCount)
\r
337 if (hashBlock != 0)
\r
339 map<uint256, int>::iterator mi = mapRequestCount.find(hashBlock);
\r
340 if (mi != mapRequestCount.end())
\r
341 nRequests = (*mi).second;
\r
346 // Did anyone request this transaction?
\r
347 map<uint256, int>::iterator mi = mapRequestCount.find(GetHash());
\r
348 if (mi != mapRequestCount.end())
\r
350 nRequests = (*mi).second;
\r
352 // How about the block it's in?
\r
353 if (nRequests == 0 && hashBlock != 0)
\r
355 map<uint256, int>::iterator mi = mapRequestCount.find(hashBlock);
\r
356 if (mi != mapRequestCount.end())
\r
357 nRequests = (*mi).second;
\r
359 nRequests = 1; // If it's in someone else's block it must have got out
\r
370 int CMerkleTx::SetMerkleBranch(const CBlock* pblock)
\r
374 if (hashBlock == 0)
\r
380 if (pblock == NULL)
\r
382 // Load the block this tx is in
\r
384 if (!CTxDB("r").ReadTxIndex(GetHash(), txindex))
\r
386 if (!blockTmp.ReadFromDisk(txindex.pos.nFile, txindex.pos.nBlockPos))
\r
388 pblock = &blockTmp;
\r
391 // Update the tx's hashBlock
\r
392 hashBlock = pblock->GetHash();
\r
394 // Locate the transaction
\r
395 for (nIndex = 0; nIndex < pblock->vtx.size(); nIndex++)
\r
396 if (pblock->vtx[nIndex] == *(CTransaction*)this)
\r
398 if (nIndex == pblock->vtx.size())
\r
400 vMerkleBranch.clear();
\r
402 printf("ERROR: SetMerkleBranch() : couldn't find tx in block\n");
\r
406 // Fill in merkle branch
\r
407 vMerkleBranch = pblock->GetMerkleBranch(nIndex);
\r
410 // Is the tx in a block that's in the main chain
\r
411 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
\r
412 if (mi == mapBlockIndex.end())
\r
414 CBlockIndex* pindex = (*mi).second;
\r
415 if (!pindex || !pindex->IsInMainChain())
\r
418 return pindexBest->nHeight - pindex->nHeight + 1;
\r
423 void CWalletTx::AddSupportingTransactions(CTxDB& txdb)
\r
427 const int COPY_DEPTH = 3;
\r
428 if (SetMerkleBranch() < COPY_DEPTH)
\r
430 vector<uint256> vWorkQueue;
\r
431 foreach(const CTxIn& txin, vin)
\r
432 vWorkQueue.push_back(txin.prevout.hash);
\r
434 // This critsect is OK because txdb is already open
\r
435 CRITICAL_BLOCK(cs_mapWallet)
\r
437 map<uint256, const CMerkleTx*> mapWalletPrev;
\r
438 set<uint256> setAlreadyDone;
\r
439 for (int i = 0; i < vWorkQueue.size(); i++)
\r
441 uint256 hash = vWorkQueue[i];
\r
442 if (setAlreadyDone.count(hash))
\r
444 setAlreadyDone.insert(hash);
\r
447 if (mapWallet.count(hash))
\r
449 tx = mapWallet[hash];
\r
450 foreach(const CMerkleTx& txWalletPrev, mapWallet[hash].vtxPrev)
\r
451 mapWalletPrev[txWalletPrev.GetHash()] = &txWalletPrev;
\r
453 else if (mapWalletPrev.count(hash))
\r
455 tx = *mapWalletPrev[hash];
\r
457 else if (!fClient && txdb.ReadDiskTx(hash, tx))
\r
463 printf("ERROR: AddSupportingTransactions() : unsupported transaction\n");
\r
467 int nDepth = tx.SetMerkleBranch();
\r
468 vtxPrev.push_back(tx);
\r
470 if (nDepth < COPY_DEPTH)
\r
471 foreach(const CTxIn& txin, tx.vin)
\r
472 vWorkQueue.push_back(txin.prevout.hash);
\r
477 reverse(vtxPrev.begin(), vtxPrev.end());
\r
490 bool CTransaction::AcceptTransaction(CTxDB& txdb, bool fCheckInputs, bool* pfMissingInputs)
\r
492 if (pfMissingInputs)
\r
493 *pfMissingInputs = false;
\r
495 // Coinbase is only valid in a block, not as a loose transaction
\r
497 return error("AcceptTransaction() : coinbase as individual tx");
\r
499 if (!CheckTransaction())
\r
500 return error("AcceptTransaction() : CheckTransaction failed");
\r
502 // To help v0.1.5 clients who would see it as a negative number
\r
503 if (nLockTime > INT_MAX)
\r
504 return error("AcceptTransaction() : not accepting nLockTime beyond 2038");
\r
506 // Do we already have it?
\r
507 uint256 hash = GetHash();
\r
508 CRITICAL_BLOCK(cs_mapTransactions)
\r
509 if (mapTransactions.count(hash))
\r
512 if (txdb.ContainsTx(hash))
\r
515 // Check for conflicts with in-memory transactions
\r
516 CTransaction* ptxOld = NULL;
\r
517 for (int i = 0; i < vin.size(); i++)
\r
519 COutPoint outpoint = vin[i].prevout;
\r
520 if (mapNextTx.count(outpoint))
\r
522 // Allow replacing with a newer version of the same transaction
\r
525 ptxOld = mapNextTx[outpoint].ptx;
\r
526 if (!IsNewerThan(*ptxOld))
\r
528 for (int i = 0; i < vin.size(); i++)
\r
530 COutPoint outpoint = vin[i].prevout;
\r
531 if (!mapNextTx.count(outpoint) || mapNextTx[outpoint].ptx != ptxOld)
\r
538 // Check against previous transactions
\r
539 map<uint256, CTxIndex> mapUnused;
\r
541 if (fCheckInputs && !ConnectInputs(txdb, mapUnused, CDiskTxPos(1,1,1), 0, nFees, false, false))
\r
543 if (pfMissingInputs)
\r
544 *pfMissingInputs = true;
\r
545 return error("AcceptTransaction() : ConnectInputs failed %s", hash.ToString().substr(0,6).c_str());
\r
548 // Store transaction in memory
\r
549 CRITICAL_BLOCK(cs_mapTransactions)
\r
553 printf("mapTransaction.erase(%s) replacing with new version\n", ptxOld->GetHash().ToString().c_str());
\r
554 mapTransactions.erase(ptxOld->GetHash());
\r
559 ///// are we sure this is ok when loading transactions or restoring block txes
\r
560 // If updated, erase old tx from wallet
\r
562 EraseFromWallet(ptxOld->GetHash());
\r
564 printf("AcceptTransaction(): accepted %s\n", hash.ToString().substr(0,6).c_str());
\r
569 bool CTransaction::AddToMemoryPool()
\r
571 // Add to memory pool without checking anything. Don't call this directly,
\r
572 // call AcceptTransaction to properly check the transaction first.
\r
573 CRITICAL_BLOCK(cs_mapTransactions)
\r
575 uint256 hash = GetHash();
\r
576 mapTransactions[hash] = *this;
\r
577 for (int i = 0; i < vin.size(); i++)
\r
578 mapNextTx[vin[i].prevout] = CInPoint(&mapTransactions[hash], i);
\r
579 nTransactionsUpdated++;
\r
585 bool CTransaction::RemoveFromMemoryPool()
\r
587 // Remove transaction from memory pool
\r
588 CRITICAL_BLOCK(cs_mapTransactions)
\r
590 foreach(const CTxIn& txin, vin)
\r
591 mapNextTx.erase(txin.prevout);
\r
592 mapTransactions.erase(GetHash());
\r
593 nTransactionsUpdated++;
\r
603 int CMerkleTx::GetDepthInMainChain(int& nHeightRet) const
\r
605 if (hashBlock == 0 || nIndex == -1)
\r
608 // Find the block it claims to be in
\r
609 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
\r
610 if (mi == mapBlockIndex.end())
\r
612 CBlockIndex* pindex = (*mi).second;
\r
613 if (!pindex || !pindex->IsInMainChain())
\r
616 // Make sure the merkle branch connects to this block
\r
617 if (!fMerkleVerified)
\r
619 if (CBlock::CheckMerkleBranch(GetHash(), vMerkleBranch, nIndex) != pindex->hashMerkleRoot)
\r
621 fMerkleVerified = true;
\r
624 nHeightRet = pindex->nHeight;
\r
625 return pindexBest->nHeight - pindex->nHeight + 1;
\r
629 int CMerkleTx::GetBlocksToMaturity() const
\r
633 return max(0, (COINBASE_MATURITY+20) - GetDepthInMainChain());
\r
637 bool CMerkleTx::AcceptTransaction(CTxDB& txdb, bool fCheckInputs)
\r
641 if (!IsInMainChain() && !ClientConnectInputs())
\r
643 return CTransaction::AcceptTransaction(txdb, false);
\r
647 return CTransaction::AcceptTransaction(txdb, fCheckInputs);
\r
653 bool CWalletTx::AcceptWalletTransaction(CTxDB& txdb, bool fCheckInputs)
\r
655 CRITICAL_BLOCK(cs_mapTransactions)
\r
657 foreach(CMerkleTx& tx, vtxPrev)
\r
659 if (!tx.IsCoinBase())
\r
661 uint256 hash = tx.GetHash();
\r
662 if (!mapTransactions.count(hash) && !txdb.ContainsTx(hash))
\r
663 tx.AcceptTransaction(txdb, fCheckInputs);
\r
667 return AcceptTransaction(txdb, fCheckInputs);
\r
672 void ReacceptWalletTransactions()
\r
675 CRITICAL_BLOCK(cs_mapWallet)
\r
677 foreach(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
\r
679 CWalletTx& wtx = item.second;
\r
680 if (wtx.fSpent && wtx.IsCoinBase())
\r
684 if (txdb.ReadTxIndex(wtx.GetHash(), txindex))
\r
686 // Update fSpent if a tx got spent somewhere else by a copy of wallet.dat
\r
689 if (txindex.vSpent.size() != wtx.vout.size())
\r
691 printf("ERROR: ReacceptWalletTransactions() : txindex.vSpent.size() %d != wtx.vout.size() %d\n", txindex.vSpent.size(), wtx.vout.size());
\r
694 for (int i = 0; i < txindex.vSpent.size(); i++)
\r
696 if (!txindex.vSpent[i].IsNull() && wtx.vout[i].IsMine())
\r
698 printf("ReacceptWalletTransactions found spent coin %sbc %s\n", FormatMoney(wtx.GetCredit()).c_str(), wtx.GetHash().ToString().c_str());
\r
708 // Reaccept any txes of ours that aren't already in a block
\r
709 if (!wtx.IsCoinBase())
\r
710 wtx.AcceptWalletTransaction(txdb, false);
\r
717 void CWalletTx::RelayWalletTransaction(CTxDB& txdb)
\r
719 foreach(const CMerkleTx& tx, vtxPrev)
\r
721 if (!tx.IsCoinBase())
\r
723 uint256 hash = tx.GetHash();
\r
724 if (!txdb.ContainsTx(hash))
\r
725 RelayMessage(CInv(MSG_TX, hash), (CTransaction)tx);
\r
730 uint256 hash = GetHash();
\r
731 if (!txdb.ContainsTx(hash))
\r
733 printf("Relaying wtx %s\n", hash.ToString().substr(0,6).c_str());
\r
734 RelayMessage(CInv(MSG_TX, hash), (CTransaction)*this);
\r
739 void ResendWalletTransactions()
\r
741 // Do this infrequently and randomly to avoid giving away
\r
742 // that these are our transactions.
\r
743 static int64 nNextTime;
\r
744 if (GetTime() < nNextTime)
\r
746 bool fFirst = (nNextTime == 0);
\r
747 nNextTime = GetTime() + GetRand(120 * 60);
\r
751 // Rebroadcast any of our txes that aren't in a block yet
\r
752 printf("ResendWalletTransactions()\n");
\r
754 CRITICAL_BLOCK(cs_mapWallet)
\r
756 // Sort them in chronological order
\r
757 multimap<unsigned int, CWalletTx*> mapSorted;
\r
758 foreach(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
\r
760 CWalletTx& wtx = item.second;
\r
761 // Don't rebroadcast until it's had plenty of time that
\r
762 // it should have gotten in already by now.
\r
763 if (nTimeBestReceived - wtx.nTimeReceived > 60 * 60)
\r
764 mapSorted.insert(make_pair(wtx.nTimeReceived, &wtx));
\r
766 foreach(PAIRTYPE(const unsigned int, CWalletTx*)& item, mapSorted)
\r
768 CWalletTx& wtx = *item.second;
\r
769 wtx.RelayWalletTransaction(txdb);
\r
783 //////////////////////////////////////////////////////////////////////////////
\r
785 // CBlock and CBlockIndex
\r
788 bool CBlock::ReadFromDisk(const CBlockIndex* pblockindex, bool fReadTransactions)
\r
790 return ReadFromDisk(pblockindex->nFile, pblockindex->nBlockPos, fReadTransactions);
\r
793 uint256 GetOrphanRoot(const CBlock* pblock)
\r
795 // Work back to the first block in the orphan chain
\r
796 while (mapOrphanBlocks.count(pblock->hashPrevBlock))
\r
797 pblock = mapOrphanBlocks[pblock->hashPrevBlock];
\r
798 return pblock->GetHash();
\r
801 int64 CBlock::GetBlockValue(int nHeight, int64 nFees) const
\r
803 int64 nSubsidy = 50 * COIN;
\r
805 // Subsidy is cut in half every 4 years
\r
806 nSubsidy >>= (nHeight / 210000);
\r
808 return nSubsidy + nFees;
\r
811 unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast)
\r
813 const int64 nTargetTimespan = 14 * 24 * 60 * 60; // two weeks
\r
814 const int64 nTargetSpacing = 10 * 60;
\r
815 const int64 nInterval = nTargetTimespan / nTargetSpacing;
\r
818 if (pindexLast == NULL)
\r
819 return bnProofOfWorkLimit.GetCompact();
\r
821 // Only change once per interval
\r
822 if ((pindexLast->nHeight+1) % nInterval != 0)
\r
823 return pindexLast->nBits;
\r
825 // Go back by what we want to be 14 days worth of blocks
\r
826 const CBlockIndex* pindexFirst = pindexLast;
\r
827 for (int i = 0; pindexFirst && i < nInterval-1; i++)
\r
828 pindexFirst = pindexFirst->pprev;
\r
829 assert(pindexFirst);
\r
831 // Limit adjustment step
\r
832 int64 nActualTimespan = (int64)pindexLast->nTime - (int64)pindexFirst->nTime;
\r
833 printf(" nActualTimespan = %"PRI64d" before bounds\n", nActualTimespan);
\r
834 if (nActualTimespan < nTargetTimespan/4)
\r
835 nActualTimespan = nTargetTimespan/4;
\r
836 if (nActualTimespan > nTargetTimespan*4)
\r
837 nActualTimespan = nTargetTimespan*4;
\r
841 bnNew.SetCompact(pindexLast->nBits);
\r
842 bnNew *= nActualTimespan;
\r
843 bnNew /= nTargetTimespan;
\r
845 if (bnNew > bnProofOfWorkLimit)
\r
846 bnNew = bnProofOfWorkLimit;
\r
849 printf("GetNextWorkRequired RETARGET\n");
\r
850 printf("nTargetTimespan = %"PRI64d" nActualTimespan = %"PRI64d"\n", nTargetTimespan, nActualTimespan);
\r
851 printf("Before: %08x %s\n", pindexLast->nBits, CBigNum().SetCompact(pindexLast->nBits).getuint256().ToString().c_str());
\r
852 printf("After: %08x %s\n", bnNew.GetCompact(), bnNew.getuint256().ToString().c_str());
\r
854 return bnNew.GetCompact();
\r
857 bool IsInitialBlockDownload()
\r
859 if (pindexBest == NULL)
\r
861 static int64 nLastUpdate;
\r
862 static CBlockIndex* pindexLastBest;
\r
863 if (pindexBest != pindexLastBest)
\r
865 pindexLastBest = pindexBest;
\r
866 nLastUpdate = GetTime();
\r
868 return (GetTime() - nLastUpdate < 10 &&
\r
869 pindexBest->nTime < GetTime() - 24 * 60 * 60);
\r
876 return (bnBestInvalidWork > bnBestChainWork + pindexBest->GetBlockWork() * 6);
\r
879 void Lockdown(CBlockIndex* pindexNew)
\r
881 if (pindexNew->bnChainWork > bnBestInvalidWork)
\r
883 bnBestInvalidWork = pindexNew->bnChainWork;
\r
884 CTxDB().WriteBestInvalidWork(bnBestInvalidWork);
\r
885 MainFrameRepaint();
\r
887 printf("Lockdown: invalid block=%s height=%d work=%s\n", pindexNew->GetBlockHash().ToString().substr(0,22).c_str(), pindexNew->nHeight, pindexNew->bnChainWork.ToString().c_str());
\r
888 printf("Lockdown: current best=%s height=%d work=%s\n", hashBestChain.ToString().substr(0,22).c_str(), nBestHeight, bnBestChainWork.ToString().c_str());
\r
889 printf("Lockdown: IsLockdown()=%d\n", (IsLockdown() ? 1 : 0));
\r
891 printf("Lockdown: WARNING: Displayed transactions may not be correct! You may need to upgrade, or other nodes may need to upgrade.\n");
\r
901 bool CTransaction::DisconnectInputs(CTxDB& txdb)
\r
903 // Relinquish previous transactions' spent pointers
\r
906 foreach(const CTxIn& txin, vin)
\r
908 COutPoint prevout = txin.prevout;
\r
910 // Get prev txindex from disk
\r
912 if (!txdb.ReadTxIndex(prevout.hash, txindex))
\r
913 return error("DisconnectInputs() : ReadTxIndex failed");
\r
915 if (prevout.n >= txindex.vSpent.size())
\r
916 return error("DisconnectInputs() : prevout.n out of range");
\r
918 // Mark outpoint as not spent
\r
919 txindex.vSpent[prevout.n].SetNull();
\r
922 txdb.UpdateTxIndex(prevout.hash, txindex);
\r
926 // Remove transaction from index
\r
927 if (!txdb.EraseTxIndex(*this))
\r
928 return error("DisconnectInputs() : EraseTxPos failed");
\r
934 bool CTransaction::ConnectInputs(CTxDB& txdb, map<uint256, CTxIndex>& mapTestPool, CDiskTxPos posThisTx, int nHeight, int64& nFees, bool fBlock, bool fMiner, int64 nMinFee)
\r
936 // Take over previous transactions' spent pointers
\r
939 int64 nValueIn = 0;
\r
940 for (int i = 0; i < vin.size(); i++)
\r
942 COutPoint prevout = vin[i].prevout;
\r
946 bool fFound = true;
\r
947 if (fMiner && mapTestPool.count(prevout.hash))
\r
949 // Get txindex from current proposed changes
\r
950 txindex = mapTestPool[prevout.hash];
\r
954 // Read txindex from txdb
\r
955 fFound = txdb.ReadTxIndex(prevout.hash, txindex);
\r
957 if (!fFound && (fBlock || fMiner))
\r
958 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
961 CTransaction txPrev;
\r
962 if (!fFound || txindex.pos == CDiskTxPos(1,1,1))
\r
964 // Get prev tx from single transactions in memory
\r
965 CRITICAL_BLOCK(cs_mapTransactions)
\r
967 if (!mapTransactions.count(prevout.hash))
\r
968 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
969 txPrev = mapTransactions[prevout.hash];
\r
972 txindex.vSpent.resize(txPrev.vout.size());
\r
976 // Get prev tx from disk
\r
977 if (!txPrev.ReadFromDisk(txindex.pos))
\r
978 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
981 if (prevout.n >= txPrev.vout.size() || prevout.n >= txindex.vSpent.size())
\r
982 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
984 // If prev is coinbase, check that it's matured
\r
985 if (txPrev.IsCoinBase())
\r
986 for (CBlockIndex* pindex = pindexBest; pindex && nBestHeight - pindex->nHeight < COINBASE_MATURITY-1; pindex = pindex->pprev)
\r
987 if (pindex->nBlockPos == txindex.pos.nBlockPos && pindex->nFile == txindex.pos.nFile)
\r
988 return error("ConnectInputs() : tried to spend coinbase at depth %d", nBestHeight - pindex->nHeight);
\r
990 // Verify signature
\r
991 if (!VerifySignature(txPrev, *this, i))
\r
992 return error("ConnectInputs() : %s VerifySignature failed", GetHash().ToString().substr(0,6).c_str());
\r
994 // Check for conflicts
\r
995 if (!txindex.vSpent[prevout.n].IsNull())
\r
996 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
998 // Mark outpoints as spent
\r
999 txindex.vSpent[prevout.n] = posThisTx;
\r
1003 txdb.UpdateTxIndex(prevout.hash, txindex);
\r
1005 mapTestPool[prevout.hash] = txindex;
\r
1007 nValueIn += txPrev.vout[prevout.n].nValue;
\r
1010 // Tally transaction fees
\r
1011 int64 nTxFee = nValueIn - GetValueOut();
\r
1013 return error("ConnectInputs() : %s nTxFee < 0", GetHash().ToString().substr(0,6).c_str());
\r
1014 if (nTxFee < nMinFee)
\r
1021 // Add transaction to disk index
\r
1022 if (!txdb.AddTxIndex(*this, posThisTx, nHeight))
\r
1023 return error("ConnectInputs() : AddTxPos failed");
\r
1027 // Add transaction to test pool
\r
1028 mapTestPool[GetHash()] = CTxIndex(CDiskTxPos(1,1,1), vout.size());
\r
1035 bool CTransaction::ClientConnectInputs()
\r
1040 // Take over previous transactions' spent pointers
\r
1041 CRITICAL_BLOCK(cs_mapTransactions)
\r
1043 int64 nValueIn = 0;
\r
1044 for (int i = 0; i < vin.size(); i++)
\r
1046 // Get prev tx from single transactions in memory
\r
1047 COutPoint prevout = vin[i].prevout;
\r
1048 if (!mapTransactions.count(prevout.hash))
\r
1050 CTransaction& txPrev = mapTransactions[prevout.hash];
\r
1052 if (prevout.n >= txPrev.vout.size())
\r
1055 // Verify signature
\r
1056 if (!VerifySignature(txPrev, *this, i))
\r
1057 return error("ConnectInputs() : VerifySignature failed");
\r
1059 ///// this is redundant with the mapNextTx stuff, not sure which I want to get rid of
\r
1060 ///// this has to go away now that posNext is gone
\r
1061 // // Check for conflicts
\r
1062 // if (!txPrev.vout[prevout.n].posNext.IsNull())
\r
1063 // return error("ConnectInputs() : prev tx already used");
\r
1065 // // Flag outpoints as used
\r
1066 // txPrev.vout[prevout.n].posNext = posThisTx;
\r
1068 nValueIn += txPrev.vout[prevout.n].nValue;
\r
1070 if (GetValueOut() > nValueIn)
\r
1080 bool CBlock::DisconnectBlock(CTxDB& txdb, CBlockIndex* pindex)
\r
1082 // Disconnect in reverse order
\r
1083 for (int i = vtx.size()-1; i >= 0; i--)
\r
1084 if (!vtx[i].DisconnectInputs(txdb))
\r
1087 // Update block index on disk without changing it in memory.
\r
1088 // The memory index structure will be changed after the db commits.
\r
1089 if (pindex->pprev)
\r
1091 CDiskBlockIndex blockindexPrev(pindex->pprev);
\r
1092 blockindexPrev.hashNext = 0;
\r
1093 txdb.WriteBlockIndex(blockindexPrev);
\r
1099 bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex)
\r
1101 //// issue here: it doesn't know the version
\r
1102 unsigned int nTxPos = pindex->nBlockPos + ::GetSerializeSize(CBlock(), SER_DISK) - 1 + GetSizeOfCompactSize(vtx.size());
\r
1104 map<uint256, CTxIndex> mapUnused;
\r
1106 foreach(CTransaction& tx, vtx)
\r
1108 CDiskTxPos posThisTx(pindex->nFile, pindex->nBlockPos, nTxPos);
\r
1109 nTxPos += ::GetSerializeSize(tx, SER_DISK);
\r
1111 if (!tx.ConnectInputs(txdb, mapUnused, posThisTx, pindex->nHeight, nFees, true, false))
\r
1115 if (vtx[0].GetValueOut() > GetBlockValue(pindex->nHeight, nFees))
\r
1118 // Update block index on disk without changing it in memory.
\r
1119 // The memory index structure will be changed after the db commits.
\r
1120 if (pindex->pprev)
\r
1122 CDiskBlockIndex blockindexPrev(pindex->pprev);
\r
1123 blockindexPrev.hashNext = pindex->GetBlockHash();
\r
1124 txdb.WriteBlockIndex(blockindexPrev);
\r
1127 // Watch for transactions paying to me
\r
1128 foreach(CTransaction& tx, vtx)
\r
1129 AddToWalletIfMine(tx, this);
\r
1136 bool Reorganize(CTxDB& txdb, CBlockIndex* pindexNew)
\r
1138 printf("REORGANIZE\n");
\r
1141 CBlockIndex* pfork = pindexBest;
\r
1142 CBlockIndex* plonger = pindexNew;
\r
1143 while (pfork != plonger)
\r
1145 while (plonger->nHeight > pfork->nHeight)
\r
1146 if (!(plonger = plonger->pprev))
\r
1147 return error("Reorganize() : plonger->pprev is null");
\r
1148 if (pfork == plonger)
\r
1150 if (!(pfork = pfork->pprev))
\r
1151 return error("Reorganize() : pfork->pprev is null");
\r
1154 // List of what to disconnect
\r
1155 vector<CBlockIndex*> vDisconnect;
\r
1156 for (CBlockIndex* pindex = pindexBest; pindex != pfork; pindex = pindex->pprev)
\r
1157 vDisconnect.push_back(pindex);
\r
1159 // List of what to connect
\r
1160 vector<CBlockIndex*> vConnect;
\r
1161 for (CBlockIndex* pindex = pindexNew; pindex != pfork; pindex = pindex->pprev)
\r
1162 vConnect.push_back(pindex);
\r
1163 reverse(vConnect.begin(), vConnect.end());
\r
1165 // Disconnect shorter branch
\r
1166 vector<CTransaction> vResurrect;
\r
1167 foreach(CBlockIndex* pindex, vDisconnect)
\r
1170 if (!block.ReadFromDisk(pindex->nFile, pindex->nBlockPos))
\r
1171 return error("Reorganize() : ReadFromDisk for disconnect failed");
\r
1172 if (!block.DisconnectBlock(txdb, pindex))
\r
1173 return error("Reorganize() : DisconnectBlock failed");
\r
1175 // Queue memory transactions to resurrect
\r
1176 foreach(const CTransaction& tx, block.vtx)
\r
1177 if (!tx.IsCoinBase())
\r
1178 vResurrect.push_back(tx);
\r
1181 // Connect longer branch
\r
1182 vector<CTransaction> vDelete;
\r
1183 for (int i = 0; i < vConnect.size(); i++)
\r
1185 CBlockIndex* pindex = vConnect[i];
\r
1187 if (!block.ReadFromDisk(pindex->nFile, pindex->nBlockPos))
\r
1188 return error("Reorganize() : ReadFromDisk for connect failed");
\r
1189 if (!block.ConnectBlock(txdb, pindex))
\r
1193 return error("Reorganize() : ConnectBlock failed");
\r
1196 // Queue memory transactions to delete
\r
1197 foreach(const CTransaction& tx, block.vtx)
\r
1198 vDelete.push_back(tx);
\r
1200 if (!txdb.WriteHashBestChain(pindexNew->GetBlockHash()))
\r
1201 return error("Reorganize() : WriteHashBestChain failed");
\r
1203 // Commit now because resurrecting could take some time
\r
1206 // Disconnect shorter branch
\r
1207 foreach(CBlockIndex* pindex, vDisconnect)
\r
1208 if (pindex->pprev)
\r
1209 pindex->pprev->pnext = NULL;
\r
1211 // Connect longer branch
\r
1212 foreach(CBlockIndex* pindex, vConnect)
\r
1213 if (pindex->pprev)
\r
1214 pindex->pprev->pnext = pindex;
\r
1216 // Resurrect memory transactions that were in the disconnected branch
\r
1217 foreach(CTransaction& tx, vResurrect)
\r
1218 tx.AcceptTransaction(txdb, false);
\r
1220 // Delete redundant memory transactions that are in the connected branch
\r
1221 foreach(CTransaction& tx, vDelete)
\r
1222 tx.RemoveFromMemoryPool();
\r
1228 bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos)
\r
1230 // Check for duplicate
\r
1231 uint256 hash = GetHash();
\r
1232 if (mapBlockIndex.count(hash))
\r
1233 return error("AddToBlockIndex() : %s already exists", hash.ToString().substr(0,16).c_str());
\r
1235 // Construct new block index object
\r
1236 CBlockIndex* pindexNew = new CBlockIndex(nFile, nBlockPos, *this);
\r
1238 return error("AddToBlockIndex() : new CBlockIndex failed");
\r
1239 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
\r
1240 pindexNew->phashBlock = &((*mi).first);
\r
1241 map<uint256, CBlockIndex*>::iterator miPrev = mapBlockIndex.find(hashPrevBlock);
\r
1242 if (miPrev != mapBlockIndex.end())
\r
1244 pindexNew->pprev = (*miPrev).second;
\r
1245 pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
\r
1247 pindexNew->bnChainWork = (pindexNew->pprev ? pindexNew->pprev->bnChainWork : 0) + pindexNew->GetBlockWork();
\r
1250 txdb.WriteBlockIndex(CDiskBlockIndex(pindexNew));
\r
1253 if (pindexNew->bnChainWork > bnBestChainWork)
\r
1256 if (pindexGenesisBlock == NULL && hash == hashGenesisBlock)
\r
1258 pindexGenesisBlock = pindexNew;
\r
1259 txdb.WriteHashBestChain(hash);
\r
1261 else if (hashPrevBlock == hashBestChain)
\r
1263 // Adding to current best branch
\r
1264 if (!ConnectBlock(txdb, pindexNew) || !txdb.WriteHashBestChain(hash))
\r
1267 Lockdown(pindexNew);
\r
1268 return error("AddToBlockIndex() : ConnectBlock failed");
\r
1271 pindexNew->pprev->pnext = pindexNew;
\r
1273 // Delete redundant memory transactions
\r
1274 foreach(CTransaction& tx, vtx)
\r
1275 tx.RemoveFromMemoryPool();
\r
1279 // New best branch
\r
1280 if (!Reorganize(txdb, pindexNew))
\r
1283 Lockdown(pindexNew);
\r
1284 return error("AddToBlockIndex() : Reorganize failed");
\r
1290 hashBestChain = hash;
\r
1291 pindexBest = pindexNew;
\r
1292 nBestHeight = pindexBest->nHeight;
\r
1293 bnBestChainWork = pindexNew->bnChainWork;
\r
1294 nTimeBestReceived = GetTime();
\r
1295 nTransactionsUpdated++;
\r
1296 printf("AddToBlockIndex: new best=%s height=%d work=%s\n", hashBestChain.ToString().substr(0,22).c_str(), nBestHeight, bnBestChainWork.ToString().c_str());
\r
1301 if (pindexNew == pindexBest)
\r
1303 // Notify UI to display prev block's coinbase if it was ours
\r
1304 static uint256 hashPrevBestCoinBase;
\r
1305 CRITICAL_BLOCK(cs_mapWallet)
\r
1306 vWalletUpdated.push_back(hashPrevBestCoinBase);
\r
1307 hashPrevBestCoinBase = vtx[0].GetHash();
\r
1310 MainFrameRepaint();
\r
1317 bool CBlock::CheckBlock() const
\r
1319 // These are checks that are independent of context
\r
1320 // that can be verified before saving an orphan block.
\r
1323 if (vtx.empty() || vtx.size() > MAX_SIZE || ::GetSerializeSize(*this, SER_DISK) > MAX_SIZE)
\r
1324 return error("CheckBlock() : size limits failed");
\r
1326 // Check timestamp
\r
1327 if (nTime > GetAdjustedTime() + 2 * 60 * 60)
\r
1328 return error("CheckBlock() : block timestamp too far in the future");
\r
1330 // First transaction must be coinbase, the rest must not be
\r
1331 if (vtx.empty() || !vtx[0].IsCoinBase())
\r
1332 return error("CheckBlock() : first tx is not coinbase");
\r
1333 for (int i = 1; i < vtx.size(); i++)
\r
1334 if (vtx[i].IsCoinBase())
\r
1335 return error("CheckBlock() : more than one coinbase");
\r
1337 // Check transactions
\r
1338 foreach(const CTransaction& tx, vtx)
\r
1339 if (!tx.CheckTransaction())
\r
1340 return error("CheckBlock() : CheckTransaction failed");
\r
1342 // Check proof of work matches claimed amount
\r
1343 if (CBigNum().SetCompact(nBits) > bnProofOfWorkLimit)
\r
1344 return error("CheckBlock() : nBits below minimum work");
\r
1345 if (GetHash() > CBigNum().SetCompact(nBits).getuint256())
\r
1346 return error("CheckBlock() : hash doesn't match nBits");
\r
1348 // Check merkleroot
\r
1349 if (hashMerkleRoot != BuildMerkleTree())
\r
1350 return error("CheckBlock() : hashMerkleRoot mismatch");
\r
1355 bool CBlock::AcceptBlock()
\r
1357 // Check for duplicate
\r
1358 uint256 hash = GetHash();
\r
1359 if (mapBlockIndex.count(hash))
\r
1360 return error("AcceptBlock() : block already in mapBlockIndex");
\r
1362 // Get prev block index
\r
1363 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashPrevBlock);
\r
1364 if (mi == mapBlockIndex.end())
\r
1365 return error("AcceptBlock() : prev block not found");
\r
1366 CBlockIndex* pindexPrev = (*mi).second;
\r
1368 // Check timestamp against prev
\r
1369 if (nTime <= pindexPrev->GetMedianTimePast())
\r
1370 return error("AcceptBlock() : block's timestamp is too early");
\r
1372 // Check that all transactions are finalized
\r
1373 foreach(const CTransaction& tx, vtx)
\r
1374 if (!tx.IsFinal(pindexPrev->nHeight+1, nTime))
\r
1375 return error("AcceptBlock() : contains a non-final transaction");
\r
1377 // Check proof of work
\r
1378 if (nBits != GetNextWorkRequired(pindexPrev))
\r
1379 return error("AcceptBlock() : incorrect proof of work");
\r
1381 // Check that the block chain matches the known block chain up to a checkpoint
\r
1382 if (pindexPrev->nHeight+1 == 11111 && hash != uint256("0x0000000069e244f73d78e8fd29ba2fd2ed618bd6fa2ee92559f542fdb26e7c1d"))
\r
1383 return error("AcceptBlock() : rejected by checkpoint lockin at 11111");
\r
1384 if (pindexPrev->nHeight+1 == 33333 && hash != uint256("0x000000002dd5588a74784eaa7ab0507a18ad16a236e7b1ce69f00d7ddfb5d0a6"))
\r
1385 return error("AcceptBlock() : rejected by checkpoint lockin at 33333");
\r
1386 if (pindexPrev->nHeight+1 == 68555 && hash != uint256("0x00000000001e1b4903550a0b96e9a9405c8a95f387162e4944e8d9fbe501cd6a"))
\r
1387 return error("AcceptBlock() : rejected by checkpoint lockin at 68555");
\r
1388 if (pindexPrev->nHeight+1 == 70567 && hash != uint256("0x00000000006a49b14bcf27462068f1264c961f11fa2e0eddd2be0791e1d4124a"))
\r
1389 return error("AcceptBlock() : rejected by checkpoint lockin at 70567");
\r
1391 // Write block to history file
\r
1392 if (!CheckDiskSpace(::GetSerializeSize(*this, SER_DISK)))
\r
1393 return error("AcceptBlock() : out of disk space");
\r
1394 unsigned int nFile;
\r
1395 unsigned int nBlockPos;
\r
1396 if (!WriteToDisk(!fClient, nFile, nBlockPos))
\r
1397 return error("AcceptBlock() : WriteToDisk failed");
\r
1398 if (!AddToBlockIndex(nFile, nBlockPos))
\r
1399 return error("AcceptBlock() : AddToBlockIndex failed");
\r
1401 // Relay inventory, but don't relay old inventory during initial block download
\r
1402 if (hashBestChain == hash)
\r
1403 CRITICAL_BLOCK(cs_vNodes)
\r
1404 foreach(CNode* pnode, vNodes)
\r
1405 if (nBestHeight > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : 55000))
\r
1406 pnode->PushInventory(CInv(MSG_BLOCK, hash));
\r
1411 bool ProcessBlock(CNode* pfrom, CBlock* pblock)
\r
1413 // Check for duplicate
\r
1414 uint256 hash = pblock->GetHash();
\r
1415 if (mapBlockIndex.count(hash))
\r
1416 return error("ProcessBlock() : already have block %d %s", mapBlockIndex[hash]->nHeight, hash.ToString().substr(0,16).c_str());
\r
1417 if (mapOrphanBlocks.count(hash))
\r
1418 return error("ProcessBlock() : already have block (orphan) %s", hash.ToString().substr(0,16).c_str());
\r
1420 // Preliminary checks
\r
1421 if (!pblock->CheckBlock())
\r
1424 return error("ProcessBlock() : CheckBlock FAILED");
\r
1427 // If don't already have its previous block, shunt it off to holding area until we get it
\r
1428 if (!mapBlockIndex.count(pblock->hashPrevBlock))
\r
1430 printf("ProcessBlock: ORPHAN BLOCK, prev=%s\n", pblock->hashPrevBlock.ToString().substr(0,16).c_str());
\r
1431 mapOrphanBlocks.insert(make_pair(hash, pblock));
\r
1432 mapOrphanBlocksByPrev.insert(make_pair(pblock->hashPrevBlock, pblock));
\r
1434 // Ask this guy to fill in what we're missing
\r
1436 pfrom->PushGetBlocks(pindexBest, GetOrphanRoot(pblock));
\r
1441 if (!pblock->AcceptBlock())
\r
1444 return error("ProcessBlock() : AcceptBlock FAILED");
\r
1448 // Recursively process any orphan blocks that depended on this one
\r
1449 vector<uint256> vWorkQueue;
\r
1450 vWorkQueue.push_back(hash);
\r
1451 for (int i = 0; i < vWorkQueue.size(); i++)
\r
1453 uint256 hashPrev = vWorkQueue[i];
\r
1454 for (multimap<uint256, CBlock*>::iterator mi = mapOrphanBlocksByPrev.lower_bound(hashPrev);
\r
1455 mi != mapOrphanBlocksByPrev.upper_bound(hashPrev);
\r
1458 CBlock* pblockOrphan = (*mi).second;
\r
1459 if (pblockOrphan->AcceptBlock())
\r
1460 vWorkQueue.push_back(pblockOrphan->GetHash());
\r
1461 mapOrphanBlocks.erase(pblockOrphan->GetHash());
\r
1462 delete pblockOrphan;
\r
1464 mapOrphanBlocksByPrev.erase(hashPrev);
\r
1467 printf("ProcessBlock: ACCEPTED\n");
\r
1478 template<typename Stream>
\r
1479 bool ScanMessageStart(Stream& s)
\r
1481 // Scan ahead to the next pchMessageStart, which should normally be immediately
\r
1482 // at the file pointer. Leaves file pointer at end of pchMessageStart.
\r
1484 short prevmask = s.exceptions(0);
\r
1485 const char* p = BEGIN(pchMessageStart);
\r
1495 s.exceptions(prevmask);
\r
1499 p = BEGIN(pchMessageStart);
\r
1502 if (++p == END(pchMessageStart))
\r
1505 s.exceptions(prevmask);
\r
1514 s.exceptions(prevmask);
\r
1519 bool CheckDiskSpace(int64 nAdditionalBytes)
\r
1521 uint64 nFreeBytesAvailable = filesystem::space(GetDataDir()).available;
\r
1523 // Check for 15MB because database could create another 10MB log file at any time
\r
1524 if (nFreeBytesAvailable < (int64)15000000 + nAdditionalBytes)
\r
1527 printf("*** %s***\n", _("Warning: Disk space is low "));
\r
1529 ThreadSafeMessageBox(_("Warning: Disk space is low "), "Bitcoin", wxOK | wxICON_EXCLAMATION);
\r
1531 CreateThread(Shutdown, NULL);
\r
1537 FILE* OpenBlockFile(unsigned int nFile, unsigned int nBlockPos, const char* pszMode)
\r
1541 FILE* file = fopen(strprintf("%s/blk%04d.dat", GetDataDir().c_str(), nFile).c_str(), pszMode);
\r
1544 if (nBlockPos != 0 && !strchr(pszMode, 'a') && !strchr(pszMode, 'w'))
\r
1546 if (fseek(file, nBlockPos, SEEK_SET) != 0)
\r
1555 static unsigned int nCurrentBlockFile = 1;
\r
1557 FILE* AppendBlockFile(unsigned int& nFileRet)
\r
1562 FILE* file = OpenBlockFile(nCurrentBlockFile, 0, "ab");
\r
1565 if (fseek(file, 0, SEEK_END) != 0)
\r
1567 // FAT32 filesize max 4GB, fseek and ftell max 2GB, so we must stay under 2GB
\r
1568 if (ftell(file) < 0x7F000000 - MAX_SIZE)
\r
1570 nFileRet = nCurrentBlockFile;
\r
1574 nCurrentBlockFile++;
\r
1578 bool LoadBlockIndex(bool fAllowNew)
\r
1581 // Load block index
\r
1584 if (!txdb.LoadBlockIndex())
\r
1589 // Init with genesis block
\r
1591 if (mapBlockIndex.empty())
\r
1598 // GetHash() = 0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
\r
1599 // hashMerkleRoot = 0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
\r
1600 // txNew.vin[0].scriptSig = 486604799 4 0x736B6E616220726F662074756F6C69616220646E6F63657320666F206B6E697262206E6F20726F6C6C65636E61684320393030322F6E614A2F33302073656D695420656854
\r
1601 // txNew.vout[0].nValue = 5000000000
\r
1602 // txNew.vout[0].scriptPubKey = 0x5F1DF16B2B704C8A578D0BBAF74D385CDE12C11EE50455F3C438EF4C3FBCF649B6DE611FEAE06279A60939E028A8D65C10B73071A6F16719274855FEB0FD8A6704 OP_CHECKSIG
\r
1603 // block.nVersion = 1
\r
1604 // block.nTime = 1231006505
\r
1605 // block.nBits = 0x1d00ffff
\r
1606 // block.nNonce = 2083236893
\r
1607 // CBlock(hash=000000000019d6, ver=1, hashPrevBlock=00000000000000, hashMerkleRoot=4a5e1e, nTime=1231006505, nBits=1d00ffff, nNonce=2083236893, vtx=1)
\r
1608 // CTransaction(hash=4a5e1e, ver=1, vin.size=1, vout.size=1, nLockTime=0)
\r
1609 // CTxIn(COutPoint(000000, -1), coinbase 04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73)
\r
1610 // CTxOut(nValue=50.00000000, scriptPubKey=0x5F1DF16B2B704C8A578D0B)
\r
1611 // vMerkleTree: 4a5e1e
\r
1614 const char* pszTimestamp = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks";
\r
1615 CTransaction txNew;
\r
1616 txNew.vin.resize(1);
\r
1617 txNew.vout.resize(1);
\r
1618 txNew.vin[0].scriptSig = CScript() << 486604799 << CBigNum(4) << vector<unsigned char>((const unsigned char*)pszTimestamp, (const unsigned char*)pszTimestamp + strlen(pszTimestamp));
\r
1619 txNew.vout[0].nValue = 50 * COIN;
\r
1621 bnPubKey.SetHex("0x5F1DF16B2B704C8A578D0BBAF74D385CDE12C11EE50455F3C438EF4C3FBCF649B6DE611FEAE06279A60939E028A8D65C10B73071A6F16719274855FEB0FD8A6704");
\r
1622 txNew.vout[0].scriptPubKey = CScript() << bnPubKey << OP_CHECKSIG;
\r
1624 block.vtx.push_back(txNew);
\r
1625 block.hashPrevBlock = 0;
\r
1626 block.hashMerkleRoot = block.BuildMerkleTree();
\r
1627 block.nVersion = 1;
\r
1628 block.nTime = 1231006505;
\r
1629 block.nBits = 0x1d00ffff;
\r
1630 block.nNonce = 2083236893;
\r
1633 printf("%s\n", block.GetHash().ToString().c_str());
\r
1634 printf("%s\n", block.hashMerkleRoot.ToString().c_str());
\r
1635 printf("%s\n", hashGenesisBlock.ToString().c_str());
\r
1636 txNew.vout[0].scriptPubKey.print();
\r
1638 assert(block.hashMerkleRoot == uint256("0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"));
\r
1640 assert(block.GetHash() == hashGenesisBlock);
\r
1642 // Start new block file
\r
1643 unsigned int nFile;
\r
1644 unsigned int nBlockPos;
\r
1645 if (!block.WriteToDisk(!fClient, nFile, nBlockPos))
\r
1646 return error("LoadBlockIndex() : writing genesis block to disk failed");
\r
1647 if (!block.AddToBlockIndex(nFile, nBlockPos))
\r
1648 return error("LoadBlockIndex() : genesis block not accepted");
\r
1656 void PrintBlockTree()
\r
1658 // precompute tree structure
\r
1659 map<CBlockIndex*, vector<CBlockIndex*> > mapNext;
\r
1660 for (map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.begin(); mi != mapBlockIndex.end(); ++mi)
\r
1662 CBlockIndex* pindex = (*mi).second;
\r
1663 mapNext[pindex->pprev].push_back(pindex);
\r
1665 //while (rand() % 3 == 0)
\r
1666 // mapNext[pindex->pprev].push_back(pindex);
\r
1669 vector<pair<int, CBlockIndex*> > vStack;
\r
1670 vStack.push_back(make_pair(0, pindexGenesisBlock));
\r
1673 while (!vStack.empty())
\r
1675 int nCol = vStack.back().first;
\r
1676 CBlockIndex* pindex = vStack.back().second;
\r
1677 vStack.pop_back();
\r
1679 // print split or gap
\r
1680 if (nCol > nPrevCol)
\r
1682 for (int i = 0; i < nCol-1; i++)
\r
1686 else if (nCol < nPrevCol)
\r
1688 for (int i = 0; i < nCol; i++)
\r
1695 for (int i = 0; i < nCol; i++)
\r
1700 block.ReadFromDisk(pindex);
\r
1701 printf("%d (%u,%u) %s %s tx %d",
\r
1704 pindex->nBlockPos,
\r
1705 block.GetHash().ToString().substr(0,16).c_str(),
\r
1706 DateTimeStrFormat("%x %H:%M:%S", block.nTime).c_str(),
\r
1707 block.vtx.size());
\r
1709 CRITICAL_BLOCK(cs_mapWallet)
\r
1711 if (mapWallet.count(block.vtx[0].GetHash()))
\r
1713 CWalletTx& wtx = mapWallet[block.vtx[0].GetHash()];
\r
1714 printf(" mine: %d %d %d", wtx.GetDepthInMainChain(), wtx.GetBlocksToMaturity(), wtx.GetCredit());
\r
1720 // put the main timechain first
\r
1721 vector<CBlockIndex*>& vNext = mapNext[pindex];
\r
1722 for (int i = 0; i < vNext.size(); i++)
\r
1724 if (vNext[i]->pnext)
\r
1726 swap(vNext[0], vNext[i]);
\r
1731 // iterate children
\r
1732 for (int i = 0; i < vNext.size(); i++)
\r
1733 vStack.push_back(make_pair(nCol+i, vNext[i]));
\r
1746 //////////////////////////////////////////////////////////////////////////////
\r
1752 bool AlreadyHave(CTxDB& txdb, const CInv& inv)
\r
1756 case MSG_TX: return mapTransactions.count(inv.hash) || txdb.ContainsTx(inv.hash);
\r
1757 case MSG_BLOCK: return mapBlockIndex.count(inv.hash) || mapOrphanBlocks.count(inv.hash);
\r
1759 // Don't know what it is, just say we already got one
\r
1769 bool ProcessMessages(CNode* pfrom)
\r
1771 CDataStream& vRecv = pfrom->vRecv;
\r
1772 if (vRecv.empty())
\r
1775 // printf("ProcessMessages(%d bytes)\n", vRecv.size());
\r
1779 // (4) message start
\r
1788 // Scan for message start
\r
1789 CDataStream::iterator pstart = search(vRecv.begin(), vRecv.end(), BEGIN(pchMessageStart), END(pchMessageStart));
\r
1790 int nHeaderSize = vRecv.GetSerializeSize(CMessageHeader());
\r
1791 if (vRecv.end() - pstart < nHeaderSize)
\r
1793 if (vRecv.size() > nHeaderSize)
\r
1795 printf("\n\nPROCESSMESSAGE MESSAGESTART NOT FOUND\n\n");
\r
1796 vRecv.erase(vRecv.begin(), vRecv.end() - nHeaderSize);
\r
1800 if (pstart - vRecv.begin() > 0)
\r
1801 printf("\n\nPROCESSMESSAGE SKIPPED %d BYTES\n\n", pstart - vRecv.begin());
\r
1802 vRecv.erase(vRecv.begin(), pstart);
\r
1805 vector<char> vHeaderSave(vRecv.begin(), vRecv.begin() + nHeaderSize);
\r
1806 CMessageHeader hdr;
\r
1808 if (!hdr.IsValid())
\r
1810 printf("\n\nPROCESSMESSAGE: ERRORS IN HEADER %s\n\n\n", hdr.GetCommand().c_str());
\r
1813 string strCommand = hdr.GetCommand();
\r
1816 unsigned int nMessageSize = hdr.nMessageSize;
\r
1817 if (nMessageSize > vRecv.size())
\r
1819 // Rewind and wait for rest of message
\r
1820 ///// need a mechanism to give up waiting for overlong message size error
\r
1821 vRecv.insert(vRecv.begin(), vHeaderSave.begin(), vHeaderSave.end());
\r
1825 // Copy message to its own buffer
\r
1826 CDataStream vMsg(vRecv.begin(), vRecv.begin() + nMessageSize, vRecv.nType, vRecv.nVersion);
\r
1827 vRecv.ignore(nMessageSize);
\r
1830 if (vRecv.GetVersion() >= 209)
\r
1832 uint256 hash = Hash(vMsg.begin(), vMsg.end());
\r
1833 unsigned int nChecksum = 0;
\r
1834 memcpy(&nChecksum, &hash, sizeof(nChecksum));
\r
1835 if (nChecksum != hdr.nChecksum)
\r
1837 printf("ProcessMessage(%s, %d bytes) : CHECKSUM ERROR nChecksum=%08x hdr.nChecksum=%08x\n",
\r
1838 strCommand.c_str(), nMessageSize, nChecksum, hdr.nChecksum);
\r
1843 // Process message
\r
1844 bool fRet = false;
\r
1847 CRITICAL_BLOCK(cs_main)
\r
1848 fRet = ProcessMessage(pfrom, strCommand, vMsg);
\r
1852 catch (std::ios_base::failure& e)
\r
1854 if (strstr(e.what(), "CDataStream::read() : end of data"))
\r
1856 // Allow exceptions from underlength message on vRecv
\r
1857 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
1859 else if (strstr(e.what(), ": size too large"))
\r
1861 // Allow exceptions from overlong size
\r
1862 printf("ProcessMessage(%s, %d bytes) : Exception '%s' caught\n", strCommand.c_str(), nMessageSize, e.what());
\r
1866 PrintException(&e, "ProcessMessage()");
\r
1869 catch (std::exception& e) {
\r
1870 PrintException(&e, "ProcessMessage()");
\r
1872 PrintException(NULL, "ProcessMessage()");
\r
1876 printf("ProcessMessage(%s, %d bytes) FAILED\n", strCommand.c_str(), nMessageSize);
\r
1886 bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
\r
1888 static map<unsigned int, vector<unsigned char> > mapReuseKey;
\r
1889 RandAddSeedPerfmon();
\r
1891 printf("%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
\r
1892 printf("received: %s (%d bytes)\n", strCommand.c_str(), vRecv.size());
\r
1893 if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0)
\r
1895 printf("dropmessagestest DROPPING RECV MESSAGE\n");
\r
1903 if (strCommand == "version")
\r
1905 // Each connection can only send one version message
\r
1906 if (pfrom->nVersion != 0)
\r
1911 CAddress addrFrom;
\r
1912 uint64 nNonce = 1;
\r
1914 vRecv >> pfrom->nVersion >> pfrom->nServices >> nTime >> addrMe;
\r
1915 if (pfrom->nVersion == 10300)
\r
1916 pfrom->nVersion = 300;
\r
1917 if (pfrom->nVersion >= 106 && !vRecv.empty())
\r
1918 vRecv >> addrFrom >> nNonce;
\r
1919 if (pfrom->nVersion >= 106 && !vRecv.empty())
\r
1920 vRecv >> strSubVer;
\r
1921 if (pfrom->nVersion >= 209 && !vRecv.empty())
\r
1922 vRecv >> pfrom->nStartingHeight;
\r
1924 if (pfrom->nVersion == 0)
\r
1927 // Disconnect if we connected to ourself
\r
1928 if (nNonce == nLocalHostNonce && nNonce > 1)
\r
1930 pfrom->fDisconnect = true;
\r
1934 pfrom->fClient = !(pfrom->nServices & NODE_NETWORK);
\r
1935 if (pfrom->fClient)
\r
1937 pfrom->vSend.nType |= SER_BLOCKHEADERONLY;
\r
1938 pfrom->vRecv.nType |= SER_BLOCKHEADERONLY;
\r
1941 AddTimeData(pfrom->addr.ip, nTime);
\r
1944 if (pfrom->nVersion >= 209)
\r
1945 pfrom->PushMessage("verack");
\r
1946 pfrom->vSend.SetVersion(min(pfrom->nVersion, VERSION));
\r
1947 if (pfrom->nVersion < 209)
\r
1948 pfrom->vRecv.SetVersion(min(pfrom->nVersion, VERSION));
\r
1950 // Ask the first connected node for block updates
\r
1951 static int nAskedForBlocks;
\r
1952 if (!pfrom->fClient && (nAskedForBlocks < 1 || vNodes.size() <= 1))
\r
1954 nAskedForBlocks++;
\r
1955 pfrom->PushGetBlocks(pindexBest, uint256(0));
\r
1958 pfrom->fSuccessfullyConnected = true;
\r
1960 printf("version message: version %d, blocks=%d\n", pfrom->nVersion, pfrom->nStartingHeight);
\r
1964 else if (pfrom->nVersion == 0)
\r
1966 // Must have a version message before anything else
\r
1971 else if (strCommand == "verack")
\r
1973 pfrom->vRecv.SetVersion(min(pfrom->nVersion, VERSION));
\r
1977 else if (strCommand == "addr")
\r
1979 vector<CAddress> vAddr;
\r
1981 if (pfrom->nVersion < 200) // don't want addresses from 0.1.5
\r
1983 if (pfrom->nVersion < 209 && mapAddresses.size() > 1000) // don't want addr from 0.2.0 unless seeding
\r
1985 if (vAddr.size() > 1000)
\r
1986 return error("message addr size() = %d", vAddr.size());
\r
1988 // Store the new addresses
\r
1989 foreach(CAddress& addr, vAddr)
\r
1993 // ignore IPv6 for now, since it isn't implemented anyway
\r
1994 if (!addr.IsIPv4())
\r
1996 addr.nTime = GetAdjustedTime() - 2 * 60 * 60;
\r
1997 if (pfrom->fGetAddr || vAddr.size() > 10)
\r
1998 addr.nTime -= 5 * 24 * 60 * 60;
\r
2000 pfrom->AddAddressKnown(addr);
\r
2001 if (!pfrom->fGetAddr && addr.IsRoutable())
\r
2003 // Relay to a limited number of other nodes
\r
2004 CRITICAL_BLOCK(cs_vNodes)
\r
2006 // Use deterministic randomness to send to
\r
2007 // the same places for 12 hours at a time
\r
2008 static uint256 hashSalt;
\r
2009 if (hashSalt == 0)
\r
2010 RAND_bytes((unsigned char*)&hashSalt, sizeof(hashSalt));
\r
2011 uint256 hashRand = addr.ip ^ ((GetTime()+addr.ip)/(12*60*60)) ^ hashSalt;
\r
2012 multimap<uint256, CNode*> mapMix;
\r
2013 foreach(CNode* pnode, vNodes)
\r
2014 mapMix.insert(make_pair(hashRand = Hash(BEGIN(hashRand), END(hashRand)), pnode));
\r
2015 int nRelayNodes = 4;
\r
2016 for (multimap<uint256, CNode*>::iterator mi = mapMix.begin(); mi != mapMix.end() && nRelayNodes-- > 0; ++mi)
\r
2017 ((*mi).second)->PushAddress(addr);
\r
2021 if (vAddr.size() < 1000)
\r
2022 pfrom->fGetAddr = false;
\r
2026 else if (strCommand == "inv")
\r
2028 vector<CInv> vInv;
\r
2030 if (vInv.size() > 50000)
\r
2031 return error("message inv size() = %d", vInv.size());
\r
2034 foreach(const CInv& inv, vInv)
\r
2038 pfrom->AddInventoryKnown(inv);
\r
2040 bool fAlreadyHave = AlreadyHave(txdb, inv);
\r
2041 printf(" got inventory: %s %s\n", inv.ToString().c_str(), fAlreadyHave ? "have" : "new");
\r
2043 if (!fAlreadyHave)
\r
2044 pfrom->AskFor(inv);
\r
2045 else if (inv.type == MSG_BLOCK && mapOrphanBlocks.count(inv.hash))
\r
2046 pfrom->PushGetBlocks(pindexBest, GetOrphanRoot(mapOrphanBlocks[inv.hash]));
\r
2048 // Track requests for our stuff
\r
2049 CRITICAL_BLOCK(cs_mapRequestCount)
\r
2051 map<uint256, int>::iterator mi = mapRequestCount.find(inv.hash);
\r
2052 if (mi != mapRequestCount.end())
\r
2059 else if (strCommand == "getdata")
\r
2061 vector<CInv> vInv;
\r
2063 if (vInv.size() > 50000)
\r
2064 return error("message getdata size() = %d", vInv.size());
\r
2066 foreach(const CInv& inv, vInv)
\r
2070 printf("received getdata for: %s\n", inv.ToString().c_str());
\r
2072 if (inv.type == MSG_BLOCK)
\r
2074 // Send block from disk
\r
2075 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(inv.hash);
\r
2076 if (mi != mapBlockIndex.end())
\r
2078 //// could optimize this to send header straight from blockindex for client
\r
2080 block.ReadFromDisk((*mi).second, !pfrom->fClient);
\r
2081 pfrom->PushMessage("block", block);
\r
2083 // Trigger them to send a getblocks request for the next batch of inventory
\r
2084 if (inv.hash == pfrom->hashContinue)
\r
2086 // Bypass PushInventory, this must send even if redundant,
\r
2087 // and we want it right after the last block so they don't
\r
2088 // wait for other stuff first.
\r
2089 vector<CInv> vInv;
\r
2090 vInv.push_back(CInv(MSG_BLOCK, hashBestChain));
\r
2091 pfrom->PushMessage("inv", vInv);
\r
2092 pfrom->hashContinue = 0;
\r
2096 else if (inv.IsKnownType())
\r
2098 // Send stream from relay memory
\r
2099 CRITICAL_BLOCK(cs_mapRelay)
\r
2101 map<CInv, CDataStream>::iterator mi = mapRelay.find(inv);
\r
2102 if (mi != mapRelay.end())
\r
2103 pfrom->PushMessage(inv.GetCommand(), (*mi).second);
\r
2107 // Track requests for our stuff
\r
2108 CRITICAL_BLOCK(cs_mapRequestCount)
\r
2110 map<uint256, int>::iterator mi = mapRequestCount.find(inv.hash);
\r
2111 if (mi != mapRequestCount.end())
\r
2118 else if (strCommand == "getblocks")
\r
2120 CBlockLocator locator;
\r
2122 vRecv >> locator >> hashStop;
\r
2124 // Find the first block the caller has in the main chain
\r
2125 CBlockIndex* pindex = locator.GetBlockIndex();
\r
2127 // Send the rest of the chain
\r
2129 pindex = pindex->pnext;
\r
2130 int nLimit = 500 + locator.GetDistanceBack();
\r
2131 printf("getblocks %d to %s limit %d\n", (pindex ? pindex->nHeight : -1), hashStop.ToString().substr(0,16).c_str(), nLimit);
\r
2132 for (; pindex; pindex = pindex->pnext)
\r
2134 if (pindex->GetBlockHash() == hashStop)
\r
2136 printf(" getblocks stopping at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString().substr(0,16).c_str());
\r
2139 pfrom->PushInventory(CInv(MSG_BLOCK, pindex->GetBlockHash()));
\r
2140 if (--nLimit <= 0)
\r
2142 // When this block is requested, we'll send an inv that'll make them
\r
2143 // getblocks the next batch of inventory.
\r
2144 printf(" getblocks stopping at limit %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString().substr(0,16).c_str());
\r
2145 pfrom->hashContinue = pindex->GetBlockHash();
\r
2152 else if (strCommand == "tx")
\r
2154 vector<uint256> vWorkQueue;
\r
2155 CDataStream vMsg(vRecv);
\r
2159 CInv inv(MSG_TX, tx.GetHash());
\r
2160 pfrom->AddInventoryKnown(inv);
\r
2162 bool fMissingInputs = false;
\r
2163 if (tx.AcceptTransaction(true, &fMissingInputs))
\r
2165 AddToWalletIfMine(tx, NULL);
\r
2166 RelayMessage(inv, vMsg);
\r
2167 mapAlreadyAskedFor.erase(inv);
\r
2168 vWorkQueue.push_back(inv.hash);
\r
2170 // Recursively process any orphan transactions that depended on this one
\r
2171 for (int i = 0; i < vWorkQueue.size(); i++)
\r
2173 uint256 hashPrev = vWorkQueue[i];
\r
2174 for (multimap<uint256, CDataStream*>::iterator mi = mapOrphanTransactionsByPrev.lower_bound(hashPrev);
\r
2175 mi != mapOrphanTransactionsByPrev.upper_bound(hashPrev);
\r
2178 const CDataStream& vMsg = *((*mi).second);
\r
2180 CDataStream(vMsg) >> tx;
\r
2181 CInv inv(MSG_TX, tx.GetHash());
\r
2183 if (tx.AcceptTransaction(true))
\r
2185 printf(" accepted orphan tx %s\n", inv.hash.ToString().substr(0,6).c_str());
\r
2186 AddToWalletIfMine(tx, NULL);
\r
2187 RelayMessage(inv, vMsg);
\r
2188 mapAlreadyAskedFor.erase(inv);
\r
2189 vWorkQueue.push_back(inv.hash);
\r
2194 foreach(uint256 hash, vWorkQueue)
\r
2195 EraseOrphanTx(hash);
\r
2197 else if (fMissingInputs)
\r
2199 printf("storing orphan tx %s\n", inv.hash.ToString().substr(0,6).c_str());
\r
2200 AddOrphanTx(vMsg);
\r
2205 else if (strCommand == "block")
\r
2207 auto_ptr<CBlock> pblock(new CBlock);
\r
2211 printf("received block %s\n", pblock->GetHash().ToString().substr(0,16).c_str());
\r
2212 // pblock->print();
\r
2214 CInv inv(MSG_BLOCK, pblock->GetHash());
\r
2215 pfrom->AddInventoryKnown(inv);
\r
2217 if (ProcessBlock(pfrom, pblock.release()))
\r
2218 mapAlreadyAskedFor.erase(inv);
\r
2222 else if (strCommand == "getaddr")
\r
2224 // This includes all nodes that are currently online,
\r
2225 // since they rebroadcast an addr every 24 hours
\r
2226 pfrom->vAddrToSend.clear();
\r
2227 int64 nSince = GetAdjustedTime() - 12 * 60 * 60; // in the last 12 hours
\r
2228 CRITICAL_BLOCK(cs_mapAddresses)
\r
2230 unsigned int nSize = mapAddresses.size();
\r
2231 foreach(const PAIRTYPE(vector<unsigned char>, CAddress)& item, mapAddresses)
\r
2235 const CAddress& addr = item.second;
\r
2236 if (addr.nTime > nSince)
\r
2237 pfrom->PushAddress(addr);
\r
2243 else if (strCommand == "checkorder")
\r
2245 uint256 hashReply;
\r
2247 vRecv >> hashReply >> order;
\r
2249 /// we have a chance to check the order here
\r
2251 // Keep giving the same key to the same ip until they use it
\r
2252 if (!mapReuseKey.count(pfrom->addr.ip))
\r
2253 mapReuseKey[pfrom->addr.ip] = GenerateNewKey();
\r
2255 // Send back approval of order and pubkey to use
\r
2256 CScript scriptPubKey;
\r
2257 scriptPubKey << mapReuseKey[pfrom->addr.ip] << OP_CHECKSIG;
\r
2258 pfrom->PushMessage("reply", hashReply, (int)0, scriptPubKey);
\r
2262 else if (strCommand == "submitorder")
\r
2264 uint256 hashReply;
\r
2266 vRecv >> hashReply >> wtxNew;
\r
2267 wtxNew.fFromMe = false;
\r
2270 if (!wtxNew.AcceptWalletTransaction())
\r
2272 pfrom->PushMessage("reply", hashReply, (int)1);
\r
2273 return error("submitorder AcceptWalletTransaction() failed, returning error 1");
\r
2275 wtxNew.fTimeReceivedIsTxTime = true;
\r
2276 AddToWallet(wtxNew);
\r
2277 wtxNew.RelayWalletTransaction();
\r
2278 mapReuseKey.erase(pfrom->addr.ip);
\r
2280 // Send back confirmation
\r
2281 pfrom->PushMessage("reply", hashReply, (int)0);
\r
2285 else if (strCommand == "reply")
\r
2287 uint256 hashReply;
\r
2288 vRecv >> hashReply;
\r
2290 CRequestTracker tracker;
\r
2291 CRITICAL_BLOCK(pfrom->cs_mapRequests)
\r
2293 map<uint256, CRequestTracker>::iterator mi = pfrom->mapRequests.find(hashReply);
\r
2294 if (mi != pfrom->mapRequests.end())
\r
2296 tracker = (*mi).second;
\r
2297 pfrom->mapRequests.erase(mi);
\r
2300 if (!tracker.IsNull())
\r
2301 tracker.fn(tracker.param1, vRecv);
\r
2305 else if (strCommand == "ping")
\r
2312 // Ignore unknown commands for extensibility
\r
2316 // Update the last seen time for this node's address
\r
2317 if (pfrom->fNetworkNode)
\r
2318 if (strCommand == "version" || strCommand == "addr" || strCommand == "inv" || strCommand == "getdata" || strCommand == "ping")
\r
2319 AddressCurrentlyConnected(pfrom->addr);
\r
2333 bool SendMessages(CNode* pto, bool fSendTrickle)
\r
2335 CRITICAL_BLOCK(cs_main)
\r
2337 // Don't send anything until we get their version message
\r
2338 if (pto->nVersion == 0)
\r
2341 // Keep-alive ping
\r
2342 if (pto->nLastSend && GetTime() - pto->nLastSend > 30 * 60 && pto->vSend.empty())
\r
2343 pto->PushMessage("ping");
\r
2345 // Address refresh broadcast
\r
2346 static int64 nLastRebroadcast;
\r
2347 if (GetTime() - nLastRebroadcast > 24 * 60 * 60) // every 24 hours
\r
2349 nLastRebroadcast = GetTime();
\r
2350 CRITICAL_BLOCK(cs_vNodes)
\r
2352 foreach(CNode* pnode, vNodes)
\r
2354 // Periodically clear setAddrKnown to allow refresh broadcasts
\r
2355 pnode->setAddrKnown.clear();
\r
2357 // Rebroadcast our address
\r
2358 if (addrLocalHost.IsRoutable() && !fUseProxy)
\r
2359 pnode->PushAddress(addrLocalHost);
\r
2364 // Resend wallet transactions that haven't gotten in a block yet
\r
2365 ResendWalletTransactions();
\r
2373 vector<CAddress> vAddr;
\r
2374 vAddr.reserve(pto->vAddrToSend.size());
\r
2375 foreach(const CAddress& addr, pto->vAddrToSend)
\r
2377 // returns true if wasn't already contained in the set
\r
2378 if (pto->setAddrKnown.insert(addr).second)
\r
2380 vAddr.push_back(addr);
\r
2381 // receiver rejects addr messages larger than 1000
\r
2382 if (vAddr.size() >= 1000)
\r
2384 pto->PushMessage("addr", vAddr);
\r
2389 pto->vAddrToSend.clear();
\r
2390 if (!vAddr.empty())
\r
2391 pto->PushMessage("addr", vAddr);
\r
2396 // Message: inventory
\r
2398 vector<CInv> vInv;
\r
2399 vector<CInv> vInvWait;
\r
2400 CRITICAL_BLOCK(pto->cs_inventory)
\r
2402 vInv.reserve(pto->vInventoryToSend.size());
\r
2403 vInvWait.reserve(pto->vInventoryToSend.size());
\r
2404 foreach(const CInv& inv, pto->vInventoryToSend)
\r
2406 if (pto->setInventoryKnown.count(inv))
\r
2409 // trickle out tx inv to protect privacy
\r
2410 if (inv.type == MSG_TX && !fSendTrickle)
\r
2412 // 1/4 of tx invs blast to all immediately
\r
2413 static uint256 hashSalt;
\r
2414 if (hashSalt == 0)
\r
2415 RAND_bytes((unsigned char*)&hashSalt, sizeof(hashSalt));
\r
2416 uint256 hashRand = inv.hash ^ hashSalt;
\r
2417 hashRand = Hash(BEGIN(hashRand), END(hashRand));
\r
2418 bool fTrickleWait = ((hashRand & 3) != 0);
\r
2420 // always trickle our own transactions
\r
2421 if (!fTrickleWait)
\r
2423 TRY_CRITICAL_BLOCK(cs_mapWallet)
\r
2425 map<uint256, CWalletTx>::iterator mi = mapWallet.find(inv.hash);
\r
2426 if (mi != mapWallet.end())
\r
2428 CWalletTx& wtx = (*mi).second;
\r
2430 fTrickleWait = true;
\r
2437 vInvWait.push_back(inv);
\r
2442 // returns true if wasn't already contained in the set
\r
2443 if (pto->setInventoryKnown.insert(inv).second)
\r
2445 vInv.push_back(inv);
\r
2446 if (vInv.size() >= 1000)
\r
2448 pto->PushMessage("inv", vInv);
\r
2453 pto->vInventoryToSend = vInvWait;
\r
2455 if (!vInv.empty())
\r
2456 pto->PushMessage("inv", vInv);
\r
2460 // Message: getdata
\r
2462 vector<CInv> vGetData;
\r
2463 int64 nNow = GetTime() * 1000000;
\r
2465 while (!pto->mapAskFor.empty() && (*pto->mapAskFor.begin()).first <= nNow)
\r
2467 const CInv& inv = (*pto->mapAskFor.begin()).second;
\r
2468 if (!AlreadyHave(txdb, inv))
\r
2470 printf("sending getdata: %s\n", inv.ToString().c_str());
\r
2471 vGetData.push_back(inv);
\r
2472 if (vGetData.size() >= 1000)
\r
2474 pto->PushMessage("getdata", vGetData);
\r
2478 pto->mapAskFor.erase(pto->mapAskFor.begin());
\r
2480 if (!vGetData.empty())
\r
2481 pto->PushMessage("getdata", vGetData);
\r
2500 //////////////////////////////////////////////////////////////////////////////
\r
2505 void GenerateBitcoins(bool fGenerate)
\r
2507 if (fGenerateBitcoins != fGenerate)
\r
2509 fGenerateBitcoins = fGenerate;
\r
2510 CWalletDB().WriteSetting("fGenerateBitcoins", fGenerateBitcoins);
\r
2511 MainFrameRepaint();
\r
2513 if (fGenerateBitcoins)
\r
2515 int nProcessors = boost::thread::hardware_concurrency();
\r
2516 printf("%d processors\n", nProcessors);
\r
2517 if (nProcessors < 1)
\r
2519 if (fLimitProcessors && nProcessors > nLimitProcessors)
\r
2520 nProcessors = nLimitProcessors;
\r
2521 int nAddThreads = nProcessors - vnThreadsRunning[3];
\r
2522 printf("Starting %d BitcoinMiner threads\n", nAddThreads);
\r
2523 for (int i = 0; i < nAddThreads; i++)
\r
2525 if (!CreateThread(ThreadBitcoinMiner, NULL))
\r
2526 printf("Error: CreateThread(ThreadBitcoinMiner) failed\n");
\r
2532 void ThreadBitcoinMiner(void* parg)
\r
2536 vnThreadsRunning[3]++;
\r
2538 vnThreadsRunning[3]--;
\r
2540 catch (std::exception& e) {
\r
2541 vnThreadsRunning[3]--;
\r
2542 PrintException(&e, "ThreadBitcoinMiner()");
\r
2544 vnThreadsRunning[3]--;
\r
2545 PrintException(NULL, "ThreadBitcoinMiner()");
\r
2547 UIThreadCall(bind(CalledSetStatusBar, "", 0));
\r
2548 nHPSTimerStart = 0;
\r
2549 if (vnThreadsRunning[3] == 0)
\r
2550 dHashesPerSec = 0;
\r
2551 printf("ThreadBitcoinMiner exiting, %d threads remaining\n", vnThreadsRunning[3]);
\r
2554 int FormatHashBlocks(void* pbuffer, unsigned int len)
\r
2556 unsigned char* pdata = (unsigned char*)pbuffer;
\r
2557 unsigned int blocks = 1 + ((len + 8) / 64);
\r
2558 unsigned char* pend = pdata + 64 * blocks;
\r
2559 memset(pdata + len, 0, 64 * blocks - len);
\r
2560 pdata[len] = 0x80;
\r
2561 unsigned int bits = len * 8;
\r
2562 pend[-1] = (bits >> 0) & 0xff;
\r
2563 pend[-2] = (bits >> 8) & 0xff;
\r
2564 pend[-3] = (bits >> 16) & 0xff;
\r
2565 pend[-4] = (bits >> 24) & 0xff;
\r
2569 using CryptoPP::ByteReverse;
\r
2571 static const unsigned int pSHA256InitState[8] =
\r
2572 {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
\r
2574 inline void SHA256Transform(void* pstate, void* pinput, const void* pinit)
\r
2576 memcpy(pstate, pinit, 32);
\r
2577 CryptoPP::SHA256::Transform((CryptoPP::word32*)pstate, (CryptoPP::word32*)pinput);
\r
2584 void BitcoinMiner()
\r
2586 printf("BitcoinMiner started\n");
\r
2587 SetThreadPriority(THREAD_PRIORITY_LOWEST);
\r
2591 CBigNum bnExtraNonce = 0;
\r
2592 while (fGenerateBitcoins)
\r
2597 while (vNodes.empty() || IsInitialBlockDownload())
\r
2602 if (!fGenerateBitcoins)
\r
2606 unsigned int nTransactionsUpdatedLast = nTransactionsUpdated;
\r
2607 CBlockIndex* pindexPrev = pindexBest;
\r
2608 unsigned int nBits = GetNextWorkRequired(pindexPrev);
\r
2612 // Create coinbase tx
\r
2614 CTransaction txNew;
\r
2615 txNew.vin.resize(1);
\r
2616 txNew.vin[0].prevout.SetNull();
\r
2617 txNew.vin[0].scriptSig << nBits << ++bnExtraNonce;
\r
2618 txNew.vout.resize(1);
\r
2619 txNew.vout[0].scriptPubKey << key.GetPubKey() << OP_CHECKSIG;
\r
2623 // Create new block
\r
2625 auto_ptr<CBlock> pblock(new CBlock());
\r
2626 if (!pblock.get())
\r
2629 // Add our coinbase tx as first transaction
\r
2630 pblock->vtx.push_back(txNew);
\r
2632 // Collect the latest transactions into the block
\r
2634 CRITICAL_BLOCK(cs_main)
\r
2635 CRITICAL_BLOCK(cs_mapTransactions)
\r
2638 map<uint256, CTxIndex> mapTestPool;
\r
2639 vector<char> vfAlreadyAdded(mapTransactions.size());
\r
2640 bool fFoundSomething = true;
\r
2641 unsigned int nBlockSize = 0;
\r
2642 while (fFoundSomething && nBlockSize < MAX_SIZE/2)
\r
2644 fFoundSomething = false;
\r
2645 unsigned int n = 0;
\r
2646 for (map<uint256, CTransaction>::iterator mi = mapTransactions.begin(); mi != mapTransactions.end(); ++mi, ++n)
\r
2648 if (vfAlreadyAdded[n])
\r
2650 CTransaction& tx = (*mi).second;
\r
2651 if (tx.IsCoinBase() || !tx.IsFinal())
\r
2653 unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK);
\r
2654 if (nBlockSize + nTxSize >= MAX_BLOCK_SIZE - 10000)
\r
2657 // Transaction fee based on block size
\r
2658 int64 nMinFee = tx.GetMinFee(nBlockSize);
\r
2660 map<uint256, CTxIndex> mapTestPoolTmp(mapTestPool);
\r
2661 if (!tx.ConnectInputs(txdb, mapTestPoolTmp, CDiskTxPos(1,1,1), 0, nFees, false, true, nMinFee))
\r
2663 swap(mapTestPool, mapTestPoolTmp);
\r
2665 pblock->vtx.push_back(tx);
\r
2666 nBlockSize += nTxSize;
\r
2667 vfAlreadyAdded[n] = true;
\r
2668 fFoundSomething = true;
\r
2672 pblock->nBits = nBits;
\r
2673 pblock->vtx[0].vout[0].nValue = pblock->GetBlockValue(pindexPrev->nHeight+1, nFees);
\r
2674 printf("Running BitcoinMiner with %d transactions in block\n", pblock->vtx.size());
\r
2678 // Prebuild hash buffer
\r
2680 struct tmpworkspace
\r
2685 uint256 hashPrevBlock;
\r
2686 uint256 hashMerkleRoot;
\r
2687 unsigned int nTime;
\r
2688 unsigned int nBits;
\r
2689 unsigned int nNonce;
\r
2692 unsigned char pchPadding0[64];
\r
2694 unsigned char pchPadding1[64];
\r
2696 char tmpbuf[sizeof(tmpworkspace)+16];
\r
2697 tmpworkspace& tmp = *(tmpworkspace*)alignup<16>(tmpbuf);
\r
2699 tmp.block.nVersion = pblock->nVersion;
\r
2700 tmp.block.hashPrevBlock = pblock->hashPrevBlock = (pindexPrev ? pindexPrev->GetBlockHash() : 0);
\r
2701 tmp.block.hashMerkleRoot = pblock->hashMerkleRoot = pblock->BuildMerkleTree();
\r
2702 tmp.block.nTime = pblock->nTime = max((pindexPrev ? pindexPrev->GetMedianTimePast()+1 : 0), GetAdjustedTime());
\r
2703 tmp.block.nBits = pblock->nBits = nBits;
\r
2704 tmp.block.nNonce = pblock->nNonce = 0;
\r
2706 unsigned int nBlocks0 = FormatHashBlocks(&tmp.block, sizeof(tmp.block));
\r
2707 unsigned int nBlocks1 = FormatHashBlocks(&tmp.hash1, sizeof(tmp.hash1));
\r
2709 // Byte swap all the input buffer
\r
2710 for (int i = 0; i < sizeof(tmp)/4; i++)
\r
2711 ((unsigned int*)&tmp)[i] = ByteReverse(((unsigned int*)&tmp)[i]);
\r
2713 // Precalc the first half of the first hash, which stays constant
\r
2714 uint256 midstatebuf[2];
\r
2715 uint256& midstate = *alignup<16>(midstatebuf);
\r
2716 SHA256Transform(&midstate, &tmp.block, pSHA256InitState);
\r
2722 int64 nStart = GetTime();
\r
2723 uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
\r
2724 uint256 hashbuf[2];
\r
2725 uint256& hash = *alignup<16>(hashbuf);
\r
2728 SHA256Transform(&tmp.hash1, (char*)&tmp.block + 64, &midstate);
\r
2729 SHA256Transform(&hash, &tmp.hash1, pSHA256InitState);
\r
2731 if (((unsigned short*)&hash)[14] == 0)
\r
2733 // Byte swap the result after preliminary check
\r
2734 for (int i = 0; i < sizeof(hash)/4; i++)
\r
2735 ((unsigned int*)&hash)[i] = ByteReverse(((unsigned int*)&hash)[i]);
\r
2737 if (hash <= hashTarget)
\r
2739 pblock->nNonce = ByteReverse(tmp.block.nNonce);
\r
2740 assert(hash == pblock->GetHash());
\r
2743 printf("BitcoinMiner:\n");
\r
2744 printf("proof-of-work found \n hash: %s \ntarget: %s\n", hash.GetHex().c_str(), hashTarget.GetHex().c_str());
\r
2746 printf("%s ", DateTimeStrFormat("%x %H:%M", GetTime()).c_str());
\r
2747 printf("generated %s\n", FormatMoney(pblock->vtx[0].vout[0].nValue).c_str());
\r
2749 SetThreadPriority(THREAD_PRIORITY_NORMAL);
\r
2750 CRITICAL_BLOCK(cs_main)
\r
2752 if (pindexPrev == pindexBest)
\r
2759 // Track how many getdata requests this block gets
\r
2760 CRITICAL_BLOCK(cs_mapRequestCount)
\r
2761 mapRequestCount[pblock->GetHash()] = 0;
\r
2763 // Process this block the same as if we had received it from another node
\r
2764 if (!ProcessBlock(NULL, pblock.release()))
\r
2765 printf("ERROR in BitcoinMiner, ProcessBlock, block not accepted\n");
\r
2768 SetThreadPriority(THREAD_PRIORITY_LOWEST);
\r
2775 // Update nTime every few seconds
\r
2776 const unsigned int nMask = 0xffff;
\r
2777 const int nHashesPerCycle = (nMask+1);
\r
2778 if ((++tmp.block.nNonce & nMask) == 0)
\r
2780 // Meter hashes/sec
\r
2781 static int nCycleCounter;
\r
2782 if (nHPSTimerStart == 0)
\r
2784 nHPSTimerStart = GetTimeMillis();
\r
2785 nCycleCounter = 0;
\r
2789 if (GetTimeMillis() - nHPSTimerStart > 4000)
\r
2791 static CCriticalSection cs;
\r
2792 CRITICAL_BLOCK(cs)
\r
2794 if (GetTimeMillis() - nHPSTimerStart > 4000)
\r
2796 dHashesPerSec = 1000.0 * nHashesPerCycle * nCycleCounter / (GetTimeMillis() - nHPSTimerStart);
\r
2797 nHPSTimerStart = GetTimeMillis();
\r
2798 nCycleCounter = 0;
\r
2799 string strStatus = strprintf(" %.0f khash/s", dHashesPerSec/1000.0);
\r
2800 UIThreadCall(bind(CalledSetStatusBar, strStatus, 0));
\r
2801 static int64 nLogTime;
\r
2802 if (GetTime() - nLogTime > 30 * 60)
\r
2804 nLogTime = GetTime();
\r
2805 printf("%s ", DateTimeStrFormat("%x %H:%M", GetTime()).c_str());
\r
2806 printf("hashmeter %3d CPUs %6.0f khash/s\n", vnThreadsRunning[3], dHashesPerSec/1000.0);
\r
2812 // Check for stop or if block needs to be rebuilt
\r
2815 if (!fGenerateBitcoins)
\r
2817 if (fLimitProcessors && vnThreadsRunning[3] > nLimitProcessors)
\r
2819 if (vNodes.empty())
\r
2821 if (tmp.block.nNonce == 0)
\r
2823 if (nTransactionsUpdated != nTransactionsUpdatedLast && GetTime() - nStart > 60)
\r
2825 if (pindexPrev != pindexBest)
\r
2828 pblock->nTime = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
\r
2829 tmp.block.nTime = ByteReverse(pblock->nTime);
\r
2852 //////////////////////////////////////////////////////////////////////////////
\r
2858 int64 GetBalance()
\r
2860 int64 nStart = GetTimeMillis();
\r
2863 CRITICAL_BLOCK(cs_mapWallet)
\r
2865 for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
\r
2867 CWalletTx* pcoin = &(*it).second;
\r
2868 if (!pcoin->IsFinal() || pcoin->fSpent)
\r
2870 nTotal += pcoin->GetCredit(true);
\r
2874 //printf("GetBalance() %"PRI64d"ms\n", GetTimeMillis() - nStart);
\r
2879 int GetRandInt(int nMax)
\r
2881 return GetRand(nMax);
\r
2884 bool SelectCoins(int64 nTargetValue, set<CWalletTx*>& setCoinsRet)
\r
2886 setCoinsRet.clear();
\r
2888 // List of values less than target
\r
2889 int64 nLowestLarger = INT64_MAX;
\r
2890 CWalletTx* pcoinLowestLarger = NULL;
\r
2891 vector<pair<int64, CWalletTx*> > vValue;
\r
2892 int64 nTotalLower = 0;
\r
2894 CRITICAL_BLOCK(cs_mapWallet)
\r
2896 vector<CWalletTx*> vCoins;
\r
2897 vCoins.reserve(mapWallet.size());
\r
2898 for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
\r
2899 vCoins.push_back(&(*it).second);
\r
2900 random_shuffle(vCoins.begin(), vCoins.end(), GetRandInt);
\r
2902 foreach(CWalletTx* pcoin, vCoins)
\r
2904 if (!pcoin->IsFinal() || pcoin->fSpent)
\r
2906 int64 n = pcoin->GetCredit();
\r
2909 if (n < nTargetValue)
\r
2911 vValue.push_back(make_pair(n, pcoin));
\r
2914 else if (n == nTargetValue)
\r
2916 setCoinsRet.insert(pcoin);
\r
2919 else if (n < nLowestLarger)
\r
2921 nLowestLarger = n;
\r
2922 pcoinLowestLarger = pcoin;
\r
2927 if (nTotalLower < nTargetValue)
\r
2929 if (pcoinLowestLarger == NULL)
\r
2931 setCoinsRet.insert(pcoinLowestLarger);
\r
2935 // Solve subset sum by stochastic approximation
\r
2936 sort(vValue.rbegin(), vValue.rend());
\r
2937 vector<char> vfIncluded;
\r
2938 vector<char> vfBest(vValue.size(), true);
\r
2939 int64 nBest = nTotalLower;
\r
2941 for (int nRep = 0; nRep < 1000 && nBest != nTargetValue; nRep++)
\r
2943 vfIncluded.assign(vValue.size(), false);
\r
2945 bool fReachedTarget = false;
\r
2946 for (int nPass = 0; nPass < 2 && !fReachedTarget; nPass++)
\r
2948 for (int i = 0; i < vValue.size(); i++)
\r
2950 if (nPass == 0 ? rand() % 2 : !vfIncluded[i])
\r
2952 nTotal += vValue[i].first;
\r
2953 vfIncluded[i] = true;
\r
2954 if (nTotal >= nTargetValue)
\r
2956 fReachedTarget = true;
\r
2957 if (nTotal < nBest)
\r
2960 vfBest = vfIncluded;
\r
2962 nTotal -= vValue[i].first;
\r
2963 vfIncluded[i] = false;
\r
2970 // If the next larger is still closer, return it
\r
2971 if (pcoinLowestLarger && nLowestLarger - nTargetValue <= nBest - nTargetValue)
\r
2972 setCoinsRet.insert(pcoinLowestLarger);
\r
2975 for (int i = 0; i < vValue.size(); i++)
\r
2977 setCoinsRet.insert(vValue[i].second);
\r
2980 printf("SelectCoins() best subset: ");
\r
2981 for (int i = 0; i < vValue.size(); i++)
\r
2983 printf("%s ", FormatMoney(vValue[i].first).c_str());
\r
2984 printf("total %s\n", FormatMoney(nBest).c_str());
\r
2993 bool CreateTransaction(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, CKey& keyRet, int64& nFeeRequiredRet)
\r
2995 nFeeRequiredRet = 0;
\r
2996 CRITICAL_BLOCK(cs_main)
\r
2998 // txdb must be opened before the mapWallet lock
\r
3000 CRITICAL_BLOCK(cs_mapWallet)
\r
3002 int64 nFee = nTransactionFee;
\r
3005 wtxNew.vin.clear();
\r
3006 wtxNew.vout.clear();
\r
3007 wtxNew.fFromMe = true;
\r
3010 int64 nValueOut = nValue;
\r
3011 int64 nTotalValue = nValue + nFee;
\r
3013 // Choose coins to use
\r
3014 set<CWalletTx*> setCoins;
\r
3015 if (!SelectCoins(nTotalValue, setCoins))
\r
3017 int64 nValueIn = 0;
\r
3018 foreach(CWalletTx* pcoin, setCoins)
\r
3019 nValueIn += pcoin->GetCredit();
\r
3021 // Fill a vout to the payee
\r
3022 bool fChangeFirst = GetRand(2);
\r
3023 if (!fChangeFirst)
\r
3024 wtxNew.vout.push_back(CTxOut(nValueOut, scriptPubKey));
\r
3026 // Fill a vout back to self with any change
\r
3027 if (nValueIn > nTotalValue)
\r
3029 // Note: We use a new key here to keep it from being obvious which side is the change.
\r
3030 // The drawback is that by not reusing a previous key, the change may be lost if a
\r
3031 // backup is restored, if the backup doesn't have the new private key for the change.
\r
3032 // If we reused the old key, it would be possible to add code to look for and
\r
3033 // rediscover unknown transactions that were written with keys of ours to recover
\r
3034 // post-backup change.
\r
3036 // New private key
\r
3037 if (keyRet.IsNull())
\r
3038 keyRet.MakeNewKey();
\r
3040 // Fill a vout to ourself, using same address type as the payment
\r
3041 CScript scriptChange;
\r
3042 if (scriptPubKey.GetBitcoinAddressHash160() != 0)
\r
3043 scriptChange.SetBitcoinAddress(keyRet.GetPubKey());
\r
3045 scriptChange << keyRet.GetPubKey() << OP_CHECKSIG;
\r
3046 wtxNew.vout.push_back(CTxOut(nValueIn - nTotalValue, scriptChange));
\r
3049 // Fill a vout to the payee
\r
3051 wtxNew.vout.push_back(CTxOut(nValueOut, scriptPubKey));
\r
3054 foreach(CWalletTx* pcoin, setCoins)
\r
3055 for (int nOut = 0; nOut < pcoin->vout.size(); nOut++)
\r
3056 if (pcoin->vout[nOut].IsMine())
\r
3057 wtxNew.vin.push_back(CTxIn(pcoin->GetHash(), nOut));
\r
3061 foreach(CWalletTx* pcoin, setCoins)
\r
3062 for (int nOut = 0; nOut < pcoin->vout.size(); nOut++)
\r
3063 if (pcoin->vout[nOut].IsMine())
\r
3064 if (!SignSignature(*pcoin, wtxNew, nIn++))
\r
3067 // Check that enough fee is included
\r
3068 if (nFee < wtxNew.GetMinFee())
\r
3070 nFee = nFeeRequiredRet = wtxNew.GetMinFee();
\r
3074 // Fill vtxPrev by copying from previous transactions vtxPrev
\r
3075 wtxNew.AddSupportingTransactions(txdb);
\r
3076 wtxNew.fTimeReceivedIsTxTime = true;
\r
3085 // Call after CreateTransaction unless you want to abort
\r
3086 bool CommitTransaction(CWalletTx& wtxNew, const CKey& key)
\r
3088 CRITICAL_BLOCK(cs_main)
\r
3090 printf("CommitTransaction:\n%s", wtxNew.ToString().c_str());
\r
3091 CRITICAL_BLOCK(cs_mapWallet)
\r
3093 // This is only to keep the database open to defeat the auto-flush for the
\r
3094 // duration of this scope. This is the only place where this optimization
\r
3095 // maybe makes sense; please don't do it anywhere else.
\r
3096 CWalletDB walletdb("r");
\r
3098 // Add the change's private key to wallet
\r
3099 if (!key.IsNull() && !AddKey(key))
\r
3100 throw runtime_error("CommitTransaction() : AddKey failed\n");
\r
3102 // Add tx to wallet, because if it has change it's also ours,
\r
3103 // otherwise just for transaction history.
\r
3104 AddToWallet(wtxNew);
\r
3106 // Mark old coins as spent
\r
3107 set<CWalletTx*> setCoins;
\r
3108 foreach(const CTxIn& txin, wtxNew.vin)
\r
3109 setCoins.insert(&mapWallet[txin.prevout.hash]);
\r
3110 foreach(CWalletTx* pcoin, setCoins)
\r
3112 pcoin->fSpent = true;
\r
3113 pcoin->WriteToDisk();
\r
3114 vWalletUpdated.push_back(pcoin->GetHash());
\r
3118 // Track how many getdata requests our transaction gets
\r
3119 CRITICAL_BLOCK(cs_mapRequestCount)
\r
3120 mapRequestCount[wtxNew.GetHash()] = 0;
\r
3123 if (!wtxNew.AcceptTransaction())
\r
3125 // This must not fail. The transaction has already been signed and recorded.
\r
3126 printf("CommitTransaction() : Error: Transaction not valid");
\r
3129 wtxNew.RelayWalletTransaction();
\r
3131 MainFrameRepaint();
\r
3138 string SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, bool fAskFee)
\r
3140 CRITICAL_BLOCK(cs_main)
\r
3143 int64 nFeeRequired;
\r
3144 if (!CreateTransaction(scriptPubKey, nValue, wtxNew, key, nFeeRequired))
\r
3147 if (nValue + nFeeRequired > GetBalance())
\r
3148 strError = strprintf(_("Error: This is an oversized transaction that requires a transaction fee of %s "), FormatMoney(nFeeRequired).c_str());
\r
3150 strError = _("Error: Transaction creation failed ");
\r
3151 printf("SendMoney() : %s", strError.c_str());
\r
3155 if (fAskFee && !ThreadSafeAskFee(nFeeRequired, _("Sending..."), NULL))
\r
3158 if (!CommitTransaction(wtxNew, key))
\r
3159 return _("Error: The transaction was rejected. This might happen if some of the coins in your wallet were already spent, such as if you used a copy of wallet.dat and coins were spent in the copy but not marked as spent here.");
\r
3161 MainFrameRepaint();
\r
3167 string SendMoneyToBitcoinAddress(string strAddress, int64 nValue, CWalletTx& wtxNew, bool fAskFee)
\r
3171 return _("Invalid amount");
\r
3172 if (nValue + nTransactionFee > GetBalance())
\r
3173 return _("Insufficient funds");
\r
3175 // Parse bitcoin address
\r
3176 CScript scriptPubKey;
\r
3177 if (!scriptPubKey.SetBitcoinAddress(strAddress))
\r
3178 return _("Invalid bitcoin address");
\r
3180 return SendMoney(scriptPubKey, nValue, wtxNew, fAskFee);
\r