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
57 int fGenerateBitcoins = false;
\r
58 int64 nTransactionFee = 0;
\r
59 CAddress addrIncoming;
\r
60 int fLimitProcessors = false;
\r
61 int nLimitProcessors = 1;
\r
62 int fMinimizeToTray = true;
\r
63 int fMinimizeOnClose = true;
\r
70 //////////////////////////////////////////////////////////////////////////////
\r
75 bool AddKey(const CKey& key)
\r
77 CRITICAL_BLOCK(cs_mapKeys)
\r
79 mapKeys[key.GetPubKey()] = key.GetPrivKey();
\r
80 mapPubKeys[Hash160(key.GetPubKey())] = key.GetPubKey();
\r
82 return CWalletDB().WriteKey(key.GetPubKey(), key.GetPrivKey());
\r
85 vector<unsigned char> GenerateNewKey()
\r
87 RandAddSeedPerfmon();
\r
91 throw runtime_error("GenerateNewKey() : AddKey failed\n");
\r
92 return key.GetPubKey();
\r
98 //////////////////////////////////////////////////////////////////////////////
\r
103 bool AddToWallet(const CWalletTx& wtxIn)
\r
105 uint256 hash = wtxIn.GetHash();
\r
106 CRITICAL_BLOCK(cs_mapWallet)
\r
108 // Inserts only if not already there, returns tx inserted or tx found
\r
109 pair<map<uint256, CWalletTx>::iterator, bool> ret = mapWallet.insert(make_pair(hash, wtxIn));
\r
110 CWalletTx& wtx = (*ret.first).second;
\r
111 bool fInsertedNew = ret.second;
\r
113 wtx.nTimeReceived = GetAdjustedTime();
\r
115 bool fUpdated = false;
\r
119 if (wtxIn.hashBlock != 0 && wtxIn.hashBlock != wtx.hashBlock)
\r
121 wtx.hashBlock = wtxIn.hashBlock;
\r
124 if (wtxIn.nIndex != -1 && (wtxIn.vMerkleBranch != wtx.vMerkleBranch || wtxIn.nIndex != wtx.nIndex))
\r
126 wtx.vMerkleBranch = wtxIn.vMerkleBranch;
\r
127 wtx.nIndex = wtxIn.nIndex;
\r
130 if (wtxIn.fFromMe && wtxIn.fFromMe != wtx.fFromMe)
\r
132 wtx.fFromMe = wtxIn.fFromMe;
\r
135 if (wtxIn.fSpent && wtxIn.fSpent != wtx.fSpent)
\r
137 wtx.fSpent = wtxIn.fSpent;
\r
143 printf("AddToWallet %s %s%s\n", wtxIn.GetHash().ToString().substr(0,6).c_str(), (fInsertedNew ? "new" : ""), (fUpdated ? "update" : ""));
\r
146 if (fInsertedNew || fUpdated)
\r
147 if (!wtx.WriteToDisk())
\r
150 // If default receiving address gets used, replace it with a new one
\r
151 CScript scriptDefaultKey;
\r
152 scriptDefaultKey.SetBitcoinAddress(vchDefaultKey);
\r
153 foreach(const CTxOut& txout, wtx.vout)
\r
155 if (txout.scriptPubKey == scriptDefaultKey)
\r
157 CWalletDB walletdb;
\r
158 walletdb.WriteDefaultKey(GenerateNewKey());
\r
159 walletdb.WriteName(PubKeyToAddress(vchDefaultKey), "");
\r
164 vWalletUpdated.push_back(hash);
\r
168 MainFrameRepaint();
\r
172 bool AddToWalletIfMine(const CTransaction& tx, const CBlock* pblock)
\r
174 if (tx.IsMine() || mapWallet.count(tx.GetHash()))
\r
177 // Get merkle branch if transaction was found in a block
\r
179 wtx.SetMerkleBranch(pblock);
\r
180 return AddToWallet(wtx);
\r
185 bool EraseFromWallet(uint256 hash)
\r
187 CRITICAL_BLOCK(cs_mapWallet)
\r
189 if (mapWallet.erase(hash))
\r
190 CWalletDB().EraseTx(hash);
\r
195 void WalletUpdateSpent(const COutPoint& prevout)
\r
197 // Anytime a signature is successfully verified, it's proof the outpoint is spent.
\r
198 // Update the wallet spent flag if it doesn't know due to wallet.dat being
\r
199 // restored from backup or the user making copies of wallet.dat.
\r
200 CRITICAL_BLOCK(cs_mapWallet)
\r
202 map<uint256, CWalletTx>::iterator mi = mapWallet.find(prevout.hash);
\r
203 if (mi != mapWallet.end())
\r
205 CWalletTx& wtx = (*mi).second;
\r
206 if (!wtx.fSpent && wtx.vout[prevout.n].IsMine())
\r
208 printf("WalletUpdateSpent found spent coin %sbc %s\n", FormatMoney(wtx.GetCredit()).c_str(), wtx.GetHash().ToString().c_str());
\r
211 vWalletUpdated.push_back(prevout.hash);
\r
224 //////////////////////////////////////////////////////////////////////////////
\r
226 // mapOrphanTransactions
\r
229 void AddOrphanTx(const CDataStream& vMsg)
\r
232 CDataStream(vMsg) >> tx;
\r
233 uint256 hash = tx.GetHash();
\r
234 if (mapOrphanTransactions.count(hash))
\r
236 CDataStream* pvMsg = mapOrphanTransactions[hash] = new CDataStream(vMsg);
\r
237 foreach(const CTxIn& txin, tx.vin)
\r
238 mapOrphanTransactionsByPrev.insert(make_pair(txin.prevout.hash, pvMsg));
\r
241 void EraseOrphanTx(uint256 hash)
\r
243 if (!mapOrphanTransactions.count(hash))
\r
245 const CDataStream* pvMsg = mapOrphanTransactions[hash];
\r
247 CDataStream(*pvMsg) >> tx;
\r
248 foreach(const CTxIn& txin, tx.vin)
\r
250 for (multimap<uint256, CDataStream*>::iterator mi = mapOrphanTransactionsByPrev.lower_bound(txin.prevout.hash);
\r
251 mi != mapOrphanTransactionsByPrev.upper_bound(txin.prevout.hash);)
\r
253 if ((*mi).second == pvMsg)
\r
254 mapOrphanTransactionsByPrev.erase(mi++);
\r
260 mapOrphanTransactions.erase(hash);
\r
270 //////////////////////////////////////////////////////////////////////////////
\r
275 bool CTxIn::IsMine() const
\r
277 CRITICAL_BLOCK(cs_mapWallet)
\r
279 map<uint256, CWalletTx>::iterator mi = mapWallet.find(prevout.hash);
\r
280 if (mi != mapWallet.end())
\r
282 const CWalletTx& prev = (*mi).second;
\r
283 if (prevout.n < prev.vout.size())
\r
284 if (prev.vout[prevout.n].IsMine())
\r
291 int64 CTxIn::GetDebit() const
\r
293 CRITICAL_BLOCK(cs_mapWallet)
\r
295 map<uint256, CWalletTx>::iterator mi = mapWallet.find(prevout.hash);
\r
296 if (mi != mapWallet.end())
\r
298 const CWalletTx& prev = (*mi).second;
\r
299 if (prevout.n < prev.vout.size())
\r
300 if (prev.vout[prevout.n].IsMine())
\r
301 return prev.vout[prevout.n].nValue;
\r
307 int64 CWalletTx::GetTxTime() const
\r
309 if (!fTimeReceivedIsTxTime && hashBlock != 0)
\r
311 // If we did not receive the transaction directly, we rely on the block's
\r
312 // time to figure out when it happened. We use the median over a range
\r
313 // of blocks to try to filter out inaccurate block times.
\r
314 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
\r
315 if (mi != mapBlockIndex.end())
\r
317 CBlockIndex* pindex = (*mi).second;
\r
319 return pindex->GetMedianTime();
\r
322 return nTimeReceived;
\r
325 int CWalletTx::GetRequestCount() const
\r
327 // Returns -1 if it wasn't being tracked
\r
328 int nRequests = -1;
\r
329 CRITICAL_BLOCK(cs_mapRequestCount)
\r
334 if (hashBlock != 0)
\r
336 map<uint256, int>::iterator mi = mapRequestCount.find(hashBlock);
\r
337 if (mi != mapRequestCount.end())
\r
338 nRequests = (*mi).second;
\r
343 // Did anyone request this transaction?
\r
344 map<uint256, int>::iterator mi = mapRequestCount.find(GetHash());
\r
345 if (mi != mapRequestCount.end())
\r
347 nRequests = (*mi).second;
\r
349 // How about the block it's in?
\r
350 if (nRequests == 0 && hashBlock != 0)
\r
352 map<uint256, int>::iterator mi = mapRequestCount.find(hashBlock);
\r
353 if (mi != mapRequestCount.end())
\r
354 nRequests = (*mi).second;
\r
356 nRequests = 1; // If it's in someone else's block it must have got out
\r
367 int CMerkleTx::SetMerkleBranch(const CBlock* pblock)
\r
371 if (hashBlock == 0)
\r
377 if (pblock == NULL)
\r
379 // Load the block this tx is in
\r
381 if (!CTxDB("r").ReadTxIndex(GetHash(), txindex))
\r
383 if (!blockTmp.ReadFromDisk(txindex.pos.nFile, txindex.pos.nBlockPos))
\r
385 pblock = &blockTmp;
\r
388 // Update the tx's hashBlock
\r
389 hashBlock = pblock->GetHash();
\r
391 // Locate the transaction
\r
392 for (nIndex = 0; nIndex < pblock->vtx.size(); nIndex++)
\r
393 if (pblock->vtx[nIndex] == *(CTransaction*)this)
\r
395 if (nIndex == pblock->vtx.size())
\r
397 vMerkleBranch.clear();
\r
399 printf("ERROR: SetMerkleBranch() : couldn't find tx in block\n");
\r
403 // Fill in merkle branch
\r
404 vMerkleBranch = pblock->GetMerkleBranch(nIndex);
\r
407 // Is the tx in a block that's in the main chain
\r
408 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
\r
409 if (mi == mapBlockIndex.end())
\r
411 CBlockIndex* pindex = (*mi).second;
\r
412 if (!pindex || !pindex->IsInMainChain())
\r
415 return pindexBest->nHeight - pindex->nHeight + 1;
\r
420 void CWalletTx::AddSupportingTransactions(CTxDB& txdb)
\r
424 const int COPY_DEPTH = 3;
\r
425 if (SetMerkleBranch() < COPY_DEPTH)
\r
427 vector<uint256> vWorkQueue;
\r
428 foreach(const CTxIn& txin, vin)
\r
429 vWorkQueue.push_back(txin.prevout.hash);
\r
431 // This critsect is OK because txdb is already open
\r
432 CRITICAL_BLOCK(cs_mapWallet)
\r
434 map<uint256, const CMerkleTx*> mapWalletPrev;
\r
435 set<uint256> setAlreadyDone;
\r
436 for (int i = 0; i < vWorkQueue.size(); i++)
\r
438 uint256 hash = vWorkQueue[i];
\r
439 if (setAlreadyDone.count(hash))
\r
441 setAlreadyDone.insert(hash);
\r
444 if (mapWallet.count(hash))
\r
446 tx = mapWallet[hash];
\r
447 foreach(const CMerkleTx& txWalletPrev, mapWallet[hash].vtxPrev)
\r
448 mapWalletPrev[txWalletPrev.GetHash()] = &txWalletPrev;
\r
450 else if (mapWalletPrev.count(hash))
\r
452 tx = *mapWalletPrev[hash];
\r
454 else if (!fClient && txdb.ReadDiskTx(hash, tx))
\r
460 printf("ERROR: AddSupportingTransactions() : unsupported transaction\n");
\r
464 int nDepth = tx.SetMerkleBranch();
\r
465 vtxPrev.push_back(tx);
\r
467 if (nDepth < COPY_DEPTH)
\r
468 foreach(const CTxIn& txin, tx.vin)
\r
469 vWorkQueue.push_back(txin.prevout.hash);
\r
474 reverse(vtxPrev.begin(), vtxPrev.end());
\r
487 bool CTransaction::AcceptTransaction(CTxDB& txdb, bool fCheckInputs, bool* pfMissingInputs)
\r
489 if (pfMissingInputs)
\r
490 *pfMissingInputs = false;
\r
492 // Coinbase is only valid in a block, not as a loose transaction
\r
494 return error("AcceptTransaction() : coinbase as individual tx");
\r
496 if (!CheckTransaction())
\r
497 return error("AcceptTransaction() : CheckTransaction failed");
\r
499 // To help v0.1.5 clients who would see it as a negative number
\r
500 if (nLockTime > INT_MAX)
\r
501 return error("AcceptTransaction() : not accepting nLockTime beyond 2038");
\r
503 // Do we already have it?
\r
504 uint256 hash = GetHash();
\r
505 CRITICAL_BLOCK(cs_mapTransactions)
\r
506 if (mapTransactions.count(hash))
\r
509 if (txdb.ContainsTx(hash))
\r
512 // Check for conflicts with in-memory transactions
\r
513 CTransaction* ptxOld = NULL;
\r
514 for (int i = 0; i < vin.size(); i++)
\r
516 COutPoint outpoint = vin[i].prevout;
\r
517 if (mapNextTx.count(outpoint))
\r
519 // Allow replacing with a newer version of the same transaction
\r
522 ptxOld = mapNextTx[outpoint].ptx;
\r
523 if (!IsNewerThan(*ptxOld))
\r
525 for (int i = 0; i < vin.size(); i++)
\r
527 COutPoint outpoint = vin[i].prevout;
\r
528 if (!mapNextTx.count(outpoint) || mapNextTx[outpoint].ptx != ptxOld)
\r
535 // Check against previous transactions
\r
536 map<uint256, CTxIndex> mapUnused;
\r
538 if (fCheckInputs && !ConnectInputs(txdb, mapUnused, CDiskTxPos(1,1,1), 0, nFees, false, false))
\r
540 if (pfMissingInputs)
\r
541 *pfMissingInputs = true;
\r
542 return error("AcceptTransaction() : ConnectInputs failed %s", hash.ToString().substr(0,6).c_str());
\r
545 // Store transaction in memory
\r
546 CRITICAL_BLOCK(cs_mapTransactions)
\r
550 printf("mapTransaction.erase(%s) replacing with new version\n", ptxOld->GetHash().ToString().c_str());
\r
551 mapTransactions.erase(ptxOld->GetHash());
\r
556 ///// are we sure this is ok when loading transactions or restoring block txes
\r
557 // If updated, erase old tx from wallet
\r
559 EraseFromWallet(ptxOld->GetHash());
\r
561 printf("AcceptTransaction(): accepted %s\n", hash.ToString().substr(0,6).c_str());
\r
566 bool CTransaction::AddToMemoryPool()
\r
568 // Add to memory pool without checking anything. Don't call this directly,
\r
569 // call AcceptTransaction to properly check the transaction first.
\r
570 CRITICAL_BLOCK(cs_mapTransactions)
\r
572 uint256 hash = GetHash();
\r
573 mapTransactions[hash] = *this;
\r
574 for (int i = 0; i < vin.size(); i++)
\r
575 mapNextTx[vin[i].prevout] = CInPoint(&mapTransactions[hash], i);
\r
576 nTransactionsUpdated++;
\r
582 bool CTransaction::RemoveFromMemoryPool()
\r
584 // Remove transaction from memory pool
\r
585 CRITICAL_BLOCK(cs_mapTransactions)
\r
587 foreach(const CTxIn& txin, vin)
\r
588 mapNextTx.erase(txin.prevout);
\r
589 mapTransactions.erase(GetHash());
\r
590 nTransactionsUpdated++;
\r
600 int CMerkleTx::GetDepthInMainChain(int& nHeightRet) const
\r
602 if (hashBlock == 0 || nIndex == -1)
\r
605 // Find the block it claims to be in
\r
606 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
\r
607 if (mi == mapBlockIndex.end())
\r
609 CBlockIndex* pindex = (*mi).second;
\r
610 if (!pindex || !pindex->IsInMainChain())
\r
613 // Make sure the merkle branch connects to this block
\r
614 if (!fMerkleVerified)
\r
616 if (CBlock::CheckMerkleBranch(GetHash(), vMerkleBranch, nIndex) != pindex->hashMerkleRoot)
\r
618 fMerkleVerified = true;
\r
621 nHeightRet = pindex->nHeight;
\r
622 return pindexBest->nHeight - pindex->nHeight + 1;
\r
626 int CMerkleTx::GetBlocksToMaturity() const
\r
630 return max(0, (COINBASE_MATURITY+20) - GetDepthInMainChain());
\r
634 bool CMerkleTx::AcceptTransaction(CTxDB& txdb, bool fCheckInputs)
\r
638 if (!IsInMainChain() && !ClientConnectInputs())
\r
640 return CTransaction::AcceptTransaction(txdb, false);
\r
644 return CTransaction::AcceptTransaction(txdb, fCheckInputs);
\r
650 bool CWalletTx::AcceptWalletTransaction(CTxDB& txdb, bool fCheckInputs)
\r
652 CRITICAL_BLOCK(cs_mapTransactions)
\r
654 foreach(CMerkleTx& tx, vtxPrev)
\r
656 if (!tx.IsCoinBase())
\r
658 uint256 hash = tx.GetHash();
\r
659 if (!mapTransactions.count(hash) && !txdb.ContainsTx(hash))
\r
660 tx.AcceptTransaction(txdb, fCheckInputs);
\r
664 return AcceptTransaction(txdb, fCheckInputs);
\r
669 void ReacceptWalletTransactions()
\r
672 CRITICAL_BLOCK(cs_mapWallet)
\r
674 foreach(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
\r
676 CWalletTx& wtx = item.second;
\r
677 if (wtx.fSpent && wtx.IsCoinBase())
\r
681 if (txdb.ReadTxIndex(wtx.GetHash(), txindex))
\r
683 // Update fSpent if a tx got spent somewhere else by a copy of wallet.dat
\r
686 if (txindex.vSpent.size() != wtx.vout.size())
\r
688 printf("ERROR: ReacceptWalletTransactions() : txindex.vSpent.size() %d != wtx.vout.size() %d\n", txindex.vSpent.size(), wtx.vout.size());
\r
691 for (int i = 0; i < txindex.vSpent.size(); i++)
\r
693 if (!txindex.vSpent[i].IsNull() && wtx.vout[i].IsMine())
\r
695 printf("ReacceptWalletTransactions found spent coin %sbc %s\n", FormatMoney(wtx.GetCredit()).c_str(), wtx.GetHash().ToString().c_str());
\r
705 // Reaccept any txes of ours that aren't already in a block
\r
706 if (!wtx.IsCoinBase())
\r
707 wtx.AcceptWalletTransaction(txdb, false);
\r
714 void CWalletTx::RelayWalletTransaction(CTxDB& txdb)
\r
716 foreach(const CMerkleTx& tx, vtxPrev)
\r
718 if (!tx.IsCoinBase())
\r
720 uint256 hash = tx.GetHash();
\r
721 if (!txdb.ContainsTx(hash))
\r
722 RelayMessage(CInv(MSG_TX, hash), (CTransaction)tx);
\r
727 uint256 hash = GetHash();
\r
728 if (!txdb.ContainsTx(hash))
\r
730 printf("Relaying wtx %s\n", hash.ToString().substr(0,6).c_str());
\r
731 RelayMessage(CInv(MSG_TX, hash), (CTransaction)*this);
\r
736 void ResendWalletTransactions()
\r
738 // Do this infrequently and randomly to avoid giving away
\r
739 // that these are our transactions.
\r
740 static int64 nNextTime;
\r
741 if (GetTime() < nNextTime)
\r
743 bool fFirst = (nNextTime == 0);
\r
744 nNextTime = GetTime() + GetRand(120 * 60);
\r
748 // Rebroadcast any of our txes that aren't in a block yet
\r
749 printf("ResendWalletTransactions()\n");
\r
751 CRITICAL_BLOCK(cs_mapWallet)
\r
753 // Sort them in chronological order
\r
754 multimap<unsigned int, CWalletTx*> mapSorted;
\r
755 foreach(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
\r
757 CWalletTx& wtx = item.second;
\r
758 // Don't rebroadcast until it's had plenty of time that
\r
759 // it should have gotten in already by now.
\r
760 if (nTimeBestReceived - wtx.nTimeReceived > 60 * 60)
\r
761 mapSorted.insert(make_pair(wtx.nTimeReceived, &wtx));
\r
763 foreach(PAIRTYPE(const unsigned int, CWalletTx*)& item, mapSorted)
\r
765 CWalletTx& wtx = *item.second;
\r
766 wtx.RelayWalletTransaction(txdb);
\r
780 //////////////////////////////////////////////////////////////////////////////
\r
782 // CBlock and CBlockIndex
\r
785 bool CBlock::ReadFromDisk(const CBlockIndex* pblockindex, bool fReadTransactions)
\r
787 return ReadFromDisk(pblockindex->nFile, pblockindex->nBlockPos, fReadTransactions);
\r
790 uint256 GetOrphanRoot(const CBlock* pblock)
\r
792 // Work back to the first block in the orphan chain
\r
793 while (mapOrphanBlocks.count(pblock->hashPrevBlock))
\r
794 pblock = mapOrphanBlocks[pblock->hashPrevBlock];
\r
795 return pblock->GetHash();
\r
798 int64 CBlock::GetBlockValue(int nHeight, int64 nFees) const
\r
800 int64 nSubsidy = 50 * COIN;
\r
802 // Subsidy is cut in half every 4 years
\r
803 nSubsidy >>= (nHeight / 210000);
\r
805 return nSubsidy + nFees;
\r
808 unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast)
\r
810 const int64 nTargetTimespan = 14 * 24 * 60 * 60; // two weeks
\r
811 const int64 nTargetSpacing = 10 * 60;
\r
812 const int64 nInterval = nTargetTimespan / nTargetSpacing;
\r
815 if (pindexLast == NULL)
\r
816 return bnProofOfWorkLimit.GetCompact();
\r
818 // Only change once per interval
\r
819 if ((pindexLast->nHeight+1) % nInterval != 0)
\r
820 return pindexLast->nBits;
\r
822 // Go back by what we want to be 14 days worth of blocks
\r
823 const CBlockIndex* pindexFirst = pindexLast;
\r
824 for (int i = 0; pindexFirst && i < nInterval-1; i++)
\r
825 pindexFirst = pindexFirst->pprev;
\r
826 assert(pindexFirst);
\r
828 // Limit adjustment step
\r
829 int64 nActualTimespan = (int64)pindexLast->nTime - (int64)pindexFirst->nTime;
\r
830 printf(" nActualTimespan = %"PRI64d" before bounds\n", nActualTimespan);
\r
831 if (nActualTimespan < nTargetTimespan/4)
\r
832 nActualTimespan = nTargetTimespan/4;
\r
833 if (nActualTimespan > nTargetTimespan*4)
\r
834 nActualTimespan = nTargetTimespan*4;
\r
838 bnNew.SetCompact(pindexLast->nBits);
\r
839 bnNew *= nActualTimespan;
\r
840 bnNew /= nTargetTimespan;
\r
842 if (bnNew > bnProofOfWorkLimit)
\r
843 bnNew = bnProofOfWorkLimit;
\r
846 printf("GetNextWorkRequired RETARGET\n");
\r
847 printf("nTargetTimespan = %"PRI64d" nActualTimespan = %"PRI64d"\n", nTargetTimespan, nActualTimespan);
\r
848 printf("Before: %08x %s\n", pindexLast->nBits, CBigNum().SetCompact(pindexLast->nBits).getuint256().ToString().c_str());
\r
849 printf("After: %08x %s\n", bnNew.GetCompact(), bnNew.getuint256().ToString().c_str());
\r
851 return bnNew.GetCompact();
\r
854 bool IsInitialBlockDownload()
\r
856 if (pindexBest == NULL)
\r
858 static int64 nLastUpdate;
\r
859 static CBlockIndex* pindexLastBest;
\r
860 if (pindexBest != pindexLastBest)
\r
862 pindexLastBest = pindexBest;
\r
863 nLastUpdate = GetTime();
\r
865 return (GetTime() - nLastUpdate < 10 &&
\r
866 pindexBest->nTime < GetTime() - 24 * 60 * 60);
\r
873 return (bnBestInvalidWork > bnBestChainWork + pindexBest->GetBlockWork() * 6);
\r
876 void Lockdown(CBlockIndex* pindexNew)
\r
878 if (pindexNew->bnChainWork > bnBestInvalidWork)
\r
880 bnBestInvalidWork = pindexNew->bnChainWork;
\r
881 CTxDB().WriteBestInvalidWork(bnBestInvalidWork);
\r
882 MainFrameRepaint();
\r
884 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
885 printf("Lockdown: current best=%s height=%d work=%s\n", hashBestChain.ToString().substr(0,22).c_str(), nBestHeight, bnBestChainWork.ToString().c_str());
\r
886 printf("Lockdown: IsLockdown()=%d\n", (IsLockdown() ? 1 : 0));
\r
888 printf("Lockdown: WARNING: Displayed transactions may not be correct! You may need to upgrade, or other nodes may need to upgrade.\n");
\r
898 bool CTransaction::DisconnectInputs(CTxDB& txdb)
\r
900 // Relinquish previous transactions' spent pointers
\r
903 foreach(const CTxIn& txin, vin)
\r
905 COutPoint prevout = txin.prevout;
\r
907 // Get prev txindex from disk
\r
909 if (!txdb.ReadTxIndex(prevout.hash, txindex))
\r
910 return error("DisconnectInputs() : ReadTxIndex failed");
\r
912 if (prevout.n >= txindex.vSpent.size())
\r
913 return error("DisconnectInputs() : prevout.n out of range");
\r
915 // Mark outpoint as not spent
\r
916 txindex.vSpent[prevout.n].SetNull();
\r
919 txdb.UpdateTxIndex(prevout.hash, txindex);
\r
923 // Remove transaction from index
\r
924 if (!txdb.EraseTxIndex(*this))
\r
925 return error("DisconnectInputs() : EraseTxPos failed");
\r
931 bool CTransaction::ConnectInputs(CTxDB& txdb, map<uint256, CTxIndex>& mapTestPool, CDiskTxPos posThisTx, int nHeight, int64& nFees, bool fBlock, bool fMiner, int64 nMinFee)
\r
933 // Take over previous transactions' spent pointers
\r
936 int64 nValueIn = 0;
\r
937 for (int i = 0; i < vin.size(); i++)
\r
939 COutPoint prevout = vin[i].prevout;
\r
943 bool fFound = true;
\r
944 if (fMiner && mapTestPool.count(prevout.hash))
\r
946 // Get txindex from current proposed changes
\r
947 txindex = mapTestPool[prevout.hash];
\r
951 // Read txindex from txdb
\r
952 fFound = txdb.ReadTxIndex(prevout.hash, txindex);
\r
954 if (!fFound && (fBlock || fMiner))
\r
955 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
958 CTransaction txPrev;
\r
959 if (!fFound || txindex.pos == CDiskTxPos(1,1,1))
\r
961 // Get prev tx from single transactions in memory
\r
962 CRITICAL_BLOCK(cs_mapTransactions)
\r
964 if (!mapTransactions.count(prevout.hash))
\r
965 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
966 txPrev = mapTransactions[prevout.hash];
\r
969 txindex.vSpent.resize(txPrev.vout.size());
\r
973 // Get prev tx from disk
\r
974 if (!txPrev.ReadFromDisk(txindex.pos))
\r
975 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
978 if (prevout.n >= txPrev.vout.size() || prevout.n >= txindex.vSpent.size())
\r
979 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
981 // If prev is coinbase, check that it's matured
\r
982 if (txPrev.IsCoinBase())
\r
983 for (CBlockIndex* pindex = pindexBest; pindex && nBestHeight - pindex->nHeight < COINBASE_MATURITY-1; pindex = pindex->pprev)
\r
984 if (pindex->nBlockPos == txindex.pos.nBlockPos && pindex->nFile == txindex.pos.nFile)
\r
985 return error("ConnectInputs() : tried to spend coinbase at depth %d", nBestHeight - pindex->nHeight);
\r
987 // Verify signature
\r
988 if (!VerifySignature(txPrev, *this, i))
\r
989 return error("ConnectInputs() : %s VerifySignature failed", GetHash().ToString().substr(0,6).c_str());
\r
991 // Check for conflicts
\r
992 if (!txindex.vSpent[prevout.n].IsNull())
\r
993 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
995 // Mark outpoints as spent
\r
996 txindex.vSpent[prevout.n] = posThisTx;
\r
1000 txdb.UpdateTxIndex(prevout.hash, txindex);
\r
1002 mapTestPool[prevout.hash] = txindex;
\r
1004 nValueIn += txPrev.vout[prevout.n].nValue;
\r
1007 // Tally transaction fees
\r
1008 int64 nTxFee = nValueIn - GetValueOut();
\r
1010 return error("ConnectInputs() : %s nTxFee < 0", GetHash().ToString().substr(0,6).c_str());
\r
1011 if (nTxFee < nMinFee)
\r
1018 // Add transaction to disk index
\r
1019 if (!txdb.AddTxIndex(*this, posThisTx, nHeight))
\r
1020 return error("ConnectInputs() : AddTxPos failed");
\r
1024 // Add transaction to test pool
\r
1025 mapTestPool[GetHash()] = CTxIndex(CDiskTxPos(1,1,1), vout.size());
\r
1032 bool CTransaction::ClientConnectInputs()
\r
1037 // Take over previous transactions' spent pointers
\r
1038 CRITICAL_BLOCK(cs_mapTransactions)
\r
1040 int64 nValueIn = 0;
\r
1041 for (int i = 0; i < vin.size(); i++)
\r
1043 // Get prev tx from single transactions in memory
\r
1044 COutPoint prevout = vin[i].prevout;
\r
1045 if (!mapTransactions.count(prevout.hash))
\r
1047 CTransaction& txPrev = mapTransactions[prevout.hash];
\r
1049 if (prevout.n >= txPrev.vout.size())
\r
1052 // Verify signature
\r
1053 if (!VerifySignature(txPrev, *this, i))
\r
1054 return error("ConnectInputs() : VerifySignature failed");
\r
1056 ///// this is redundant with the mapNextTx stuff, not sure which I want to get rid of
\r
1057 ///// this has to go away now that posNext is gone
\r
1058 // // Check for conflicts
\r
1059 // if (!txPrev.vout[prevout.n].posNext.IsNull())
\r
1060 // return error("ConnectInputs() : prev tx already used");
\r
1062 // // Flag outpoints as used
\r
1063 // txPrev.vout[prevout.n].posNext = posThisTx;
\r
1065 nValueIn += txPrev.vout[prevout.n].nValue;
\r
1067 if (GetValueOut() > nValueIn)
\r
1077 bool CBlock::DisconnectBlock(CTxDB& txdb, CBlockIndex* pindex)
\r
1079 // Disconnect in reverse order
\r
1080 for (int i = vtx.size()-1; i >= 0; i--)
\r
1081 if (!vtx[i].DisconnectInputs(txdb))
\r
1084 // Update block index on disk without changing it in memory.
\r
1085 // The memory index structure will be changed after the db commits.
\r
1086 if (pindex->pprev)
\r
1088 CDiskBlockIndex blockindexPrev(pindex->pprev);
\r
1089 blockindexPrev.hashNext = 0;
\r
1090 txdb.WriteBlockIndex(blockindexPrev);
\r
1096 bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex)
\r
1098 //// issue here: it doesn't know the version
\r
1099 unsigned int nTxPos = pindex->nBlockPos + ::GetSerializeSize(CBlock(), SER_DISK) - 1 + GetSizeOfCompactSize(vtx.size());
\r
1101 map<uint256, CTxIndex> mapUnused;
\r
1103 foreach(CTransaction& tx, vtx)
\r
1105 CDiskTxPos posThisTx(pindex->nFile, pindex->nBlockPos, nTxPos);
\r
1106 nTxPos += ::GetSerializeSize(tx, SER_DISK);
\r
1108 if (!tx.ConnectInputs(txdb, mapUnused, posThisTx, pindex->nHeight, nFees, true, false))
\r
1112 if (vtx[0].GetValueOut() > GetBlockValue(pindex->nHeight, nFees))
\r
1115 // Update block index on disk without changing it in memory.
\r
1116 // The memory index structure will be changed after the db commits.
\r
1117 if (pindex->pprev)
\r
1119 CDiskBlockIndex blockindexPrev(pindex->pprev);
\r
1120 blockindexPrev.hashNext = pindex->GetBlockHash();
\r
1121 txdb.WriteBlockIndex(blockindexPrev);
\r
1124 // Watch for transactions paying to me
\r
1125 foreach(CTransaction& tx, vtx)
\r
1126 AddToWalletIfMine(tx, this);
\r
1133 bool Reorganize(CTxDB& txdb, CBlockIndex* pindexNew)
\r
1135 printf("REORGANIZE\n");
\r
1138 CBlockIndex* pfork = pindexBest;
\r
1139 CBlockIndex* plonger = pindexNew;
\r
1140 while (pfork != plonger)
\r
1142 while (plonger->nHeight > pfork->nHeight)
\r
1143 if (!(plonger = plonger->pprev))
\r
1144 return error("Reorganize() : plonger->pprev is null");
\r
1145 if (pfork == plonger)
\r
1147 if (!(pfork = pfork->pprev))
\r
1148 return error("Reorganize() : pfork->pprev is null");
\r
1151 // List of what to disconnect
\r
1152 vector<CBlockIndex*> vDisconnect;
\r
1153 for (CBlockIndex* pindex = pindexBest; pindex != pfork; pindex = pindex->pprev)
\r
1154 vDisconnect.push_back(pindex);
\r
1156 // List of what to connect
\r
1157 vector<CBlockIndex*> vConnect;
\r
1158 for (CBlockIndex* pindex = pindexNew; pindex != pfork; pindex = pindex->pprev)
\r
1159 vConnect.push_back(pindex);
\r
1160 reverse(vConnect.begin(), vConnect.end());
\r
1162 // Disconnect shorter branch
\r
1163 vector<CTransaction> vResurrect;
\r
1164 foreach(CBlockIndex* pindex, vDisconnect)
\r
1167 if (!block.ReadFromDisk(pindex->nFile, pindex->nBlockPos))
\r
1168 return error("Reorganize() : ReadFromDisk for disconnect failed");
\r
1169 if (!block.DisconnectBlock(txdb, pindex))
\r
1170 return error("Reorganize() : DisconnectBlock failed");
\r
1172 // Queue memory transactions to resurrect
\r
1173 foreach(const CTransaction& tx, block.vtx)
\r
1174 if (!tx.IsCoinBase())
\r
1175 vResurrect.push_back(tx);
\r
1178 // Connect longer branch
\r
1179 vector<CTransaction> vDelete;
\r
1180 for (int i = 0; i < vConnect.size(); i++)
\r
1182 CBlockIndex* pindex = vConnect[i];
\r
1184 if (!block.ReadFromDisk(pindex->nFile, pindex->nBlockPos))
\r
1185 return error("Reorganize() : ReadFromDisk for connect failed");
\r
1186 if (!block.ConnectBlock(txdb, pindex))
\r
1190 return error("Reorganize() : ConnectBlock failed");
\r
1193 // Queue memory transactions to delete
\r
1194 foreach(const CTransaction& tx, block.vtx)
\r
1195 vDelete.push_back(tx);
\r
1197 if (!txdb.WriteHashBestChain(pindexNew->GetBlockHash()))
\r
1198 return error("Reorganize() : WriteHashBestChain failed");
\r
1200 // Commit now because resurrecting could take some time
\r
1203 // Disconnect shorter branch
\r
1204 foreach(CBlockIndex* pindex, vDisconnect)
\r
1205 if (pindex->pprev)
\r
1206 pindex->pprev->pnext = NULL;
\r
1208 // Connect longer branch
\r
1209 foreach(CBlockIndex* pindex, vConnect)
\r
1210 if (pindex->pprev)
\r
1211 pindex->pprev->pnext = pindex;
\r
1213 // Resurrect memory transactions that were in the disconnected branch
\r
1214 foreach(CTransaction& tx, vResurrect)
\r
1215 tx.AcceptTransaction(txdb, false);
\r
1217 // Delete redundant memory transactions that are in the connected branch
\r
1218 foreach(CTransaction& tx, vDelete)
\r
1219 tx.RemoveFromMemoryPool();
\r
1225 bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos)
\r
1227 // Check for duplicate
\r
1228 uint256 hash = GetHash();
\r
1229 if (mapBlockIndex.count(hash))
\r
1230 return error("AddToBlockIndex() : %s already exists", hash.ToString().substr(0,16).c_str());
\r
1232 // Construct new block index object
\r
1233 CBlockIndex* pindexNew = new CBlockIndex(nFile, nBlockPos, *this);
\r
1235 return error("AddToBlockIndex() : new CBlockIndex failed");
\r
1236 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
\r
1237 pindexNew->phashBlock = &((*mi).first);
\r
1238 map<uint256, CBlockIndex*>::iterator miPrev = mapBlockIndex.find(hashPrevBlock);
\r
1239 if (miPrev != mapBlockIndex.end())
\r
1241 pindexNew->pprev = (*miPrev).second;
\r
1242 pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
\r
1244 pindexNew->bnChainWork = (pindexNew->pprev ? pindexNew->pprev->bnChainWork : 0) + pindexNew->GetBlockWork();
\r
1247 txdb.WriteBlockIndex(CDiskBlockIndex(pindexNew));
\r
1250 if (pindexNew->bnChainWork > bnBestChainWork)
\r
1253 if (pindexGenesisBlock == NULL && hash == hashGenesisBlock)
\r
1255 pindexGenesisBlock = pindexNew;
\r
1256 txdb.WriteHashBestChain(hash);
\r
1258 else if (hashPrevBlock == hashBestChain)
\r
1260 // Adding to current best branch
\r
1261 if (!ConnectBlock(txdb, pindexNew) || !txdb.WriteHashBestChain(hash))
\r
1264 Lockdown(pindexNew);
\r
1265 return error("AddToBlockIndex() : ConnectBlock failed");
\r
1268 pindexNew->pprev->pnext = pindexNew;
\r
1270 // Delete redundant memory transactions
\r
1271 foreach(CTransaction& tx, vtx)
\r
1272 tx.RemoveFromMemoryPool();
\r
1276 // New best branch
\r
1277 if (!Reorganize(txdb, pindexNew))
\r
1280 Lockdown(pindexNew);
\r
1281 return error("AddToBlockIndex() : Reorganize failed");
\r
1287 hashBestChain = hash;
\r
1288 pindexBest = pindexNew;
\r
1289 nBestHeight = pindexBest->nHeight;
\r
1290 bnBestChainWork = pindexNew->bnChainWork;
\r
1291 nTimeBestReceived = GetTime();
\r
1292 nTransactionsUpdated++;
\r
1293 printf("AddToBlockIndex: new best=%s height=%d work=%s\n", hashBestChain.ToString().substr(0,22).c_str(), nBestHeight, bnBestChainWork.ToString().c_str());
\r
1298 if (pindexNew == pindexBest)
\r
1300 // Notify UI to display prev block's coinbase if it was ours
\r
1301 static uint256 hashPrevBestCoinBase;
\r
1302 CRITICAL_BLOCK(cs_mapWallet)
\r
1303 vWalletUpdated.push_back(hashPrevBestCoinBase);
\r
1304 hashPrevBestCoinBase = vtx[0].GetHash();
\r
1307 MainFrameRepaint();
\r
1314 bool CBlock::CheckBlock() const
\r
1316 // These are checks that are independent of context
\r
1317 // that can be verified before saving an orphan block.
\r
1320 if (vtx.empty() || vtx.size() > MAX_SIZE || ::GetSerializeSize(*this, SER_DISK) > MAX_SIZE)
\r
1321 return error("CheckBlock() : size limits failed");
\r
1323 // Check timestamp
\r
1324 if (nTime > GetAdjustedTime() + 2 * 60 * 60)
\r
1325 return error("CheckBlock() : block timestamp too far in the future");
\r
1327 // First transaction must be coinbase, the rest must not be
\r
1328 if (vtx.empty() || !vtx[0].IsCoinBase())
\r
1329 return error("CheckBlock() : first tx is not coinbase");
\r
1330 for (int i = 1; i < vtx.size(); i++)
\r
1331 if (vtx[i].IsCoinBase())
\r
1332 return error("CheckBlock() : more than one coinbase");
\r
1334 // Check transactions
\r
1335 foreach(const CTransaction& tx, vtx)
\r
1336 if (!tx.CheckTransaction())
\r
1337 return error("CheckBlock() : CheckTransaction failed");
\r
1339 // Check proof of work matches claimed amount
\r
1340 if (CBigNum().SetCompact(nBits) > bnProofOfWorkLimit)
\r
1341 return error("CheckBlock() : nBits below minimum work");
\r
1342 if (GetHash() > CBigNum().SetCompact(nBits).getuint256())
\r
1343 return error("CheckBlock() : hash doesn't match nBits");
\r
1345 // Check merkleroot
\r
1346 if (hashMerkleRoot != BuildMerkleTree())
\r
1347 return error("CheckBlock() : hashMerkleRoot mismatch");
\r
1352 bool CBlock::AcceptBlock()
\r
1354 // Check for duplicate
\r
1355 uint256 hash = GetHash();
\r
1356 if (mapBlockIndex.count(hash))
\r
1357 return error("AcceptBlock() : block already in mapBlockIndex");
\r
1359 // Get prev block index
\r
1360 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashPrevBlock);
\r
1361 if (mi == mapBlockIndex.end())
\r
1362 return error("AcceptBlock() : prev block not found");
\r
1363 CBlockIndex* pindexPrev = (*mi).second;
\r
1365 // Check timestamp against prev
\r
1366 if (nTime <= pindexPrev->GetMedianTimePast())
\r
1367 return error("AcceptBlock() : block's timestamp is too early");
\r
1369 // Check that all transactions are finalized
\r
1370 foreach(const CTransaction& tx, vtx)
\r
1371 if (!tx.IsFinal(pindexPrev->nHeight+1, nTime))
\r
1372 return error("AcceptBlock() : contains a non-final transaction");
\r
1374 // Check proof of work
\r
1375 if (nBits != GetNextWorkRequired(pindexPrev))
\r
1376 return error("AcceptBlock() : incorrect proof of work");
\r
1378 // Check that the block chain matches the known block chain up to a checkpoint
\r
1379 if (pindexPrev->nHeight+1 == 11111 && hash != uint256("0x0000000069e244f73d78e8fd29ba2fd2ed618bd6fa2ee92559f542fdb26e7c1d"))
\r
1380 return error("AcceptBlock() : rejected by checkpoint lockin at 11111");
\r
1381 if (pindexPrev->nHeight+1 == 33333 && hash != uint256("0x000000002dd5588a74784eaa7ab0507a18ad16a236e7b1ce69f00d7ddfb5d0a6"))
\r
1382 return error("AcceptBlock() : rejected by checkpoint lockin at 33333");
\r
1383 if (pindexPrev->nHeight+1 == 68555 && hash != uint256("0x00000000001e1b4903550a0b96e9a9405c8a95f387162e4944e8d9fbe501cd6a"))
\r
1384 return error("AcceptBlock() : rejected by checkpoint lockin at 68555");
\r
1385 if (pindexPrev->nHeight+1 == 70567 && hash != uint256("0x00000000006a49b14bcf27462068f1264c961f11fa2e0eddd2be0791e1d4124a"))
\r
1386 return error("AcceptBlock() : rejected by checkpoint lockin at 70567");
\r
1388 // Write block to history file
\r
1389 if (!CheckDiskSpace(::GetSerializeSize(*this, SER_DISK)))
\r
1390 return error("AcceptBlock() : out of disk space");
\r
1391 unsigned int nFile;
\r
1392 unsigned int nBlockPos;
\r
1393 if (!WriteToDisk(!fClient, nFile, nBlockPos))
\r
1394 return error("AcceptBlock() : WriteToDisk failed");
\r
1395 if (!AddToBlockIndex(nFile, nBlockPos))
\r
1396 return error("AcceptBlock() : AddToBlockIndex failed");
\r
1398 // Relay inventory, but don't relay old inventory during initial block download
\r
1399 if (hashBestChain == hash)
\r
1400 CRITICAL_BLOCK(cs_vNodes)
\r
1401 foreach(CNode* pnode, vNodes)
\r
1402 if (nBestHeight > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : 55000))
\r
1403 pnode->PushInventory(CInv(MSG_BLOCK, hash));
\r
1408 bool ProcessBlock(CNode* pfrom, CBlock* pblock)
\r
1410 // Check for duplicate
\r
1411 uint256 hash = pblock->GetHash();
\r
1412 if (mapBlockIndex.count(hash))
\r
1413 return error("ProcessBlock() : already have block %d %s", mapBlockIndex[hash]->nHeight, hash.ToString().substr(0,16).c_str());
\r
1414 if (mapOrphanBlocks.count(hash))
\r
1415 return error("ProcessBlock() : already have block (orphan) %s", hash.ToString().substr(0,16).c_str());
\r
1417 // Preliminary checks
\r
1418 if (!pblock->CheckBlock())
\r
1421 return error("ProcessBlock() : CheckBlock FAILED");
\r
1424 // If don't already have its previous block, shunt it off to holding area until we get it
\r
1425 if (!mapBlockIndex.count(pblock->hashPrevBlock))
\r
1427 printf("ProcessBlock: ORPHAN BLOCK, prev=%s\n", pblock->hashPrevBlock.ToString().substr(0,16).c_str());
\r
1428 mapOrphanBlocks.insert(make_pair(hash, pblock));
\r
1429 mapOrphanBlocksByPrev.insert(make_pair(pblock->hashPrevBlock, pblock));
\r
1431 // Ask this guy to fill in what we're missing
\r
1433 pfrom->PushGetBlocks(pindexBest, GetOrphanRoot(pblock));
\r
1438 if (!pblock->AcceptBlock())
\r
1441 return error("ProcessBlock() : AcceptBlock FAILED");
\r
1445 // Recursively process any orphan blocks that depended on this one
\r
1446 vector<uint256> vWorkQueue;
\r
1447 vWorkQueue.push_back(hash);
\r
1448 for (int i = 0; i < vWorkQueue.size(); i++)
\r
1450 uint256 hashPrev = vWorkQueue[i];
\r
1451 for (multimap<uint256, CBlock*>::iterator mi = mapOrphanBlocksByPrev.lower_bound(hashPrev);
\r
1452 mi != mapOrphanBlocksByPrev.upper_bound(hashPrev);
\r
1455 CBlock* pblockOrphan = (*mi).second;
\r
1456 if (pblockOrphan->AcceptBlock())
\r
1457 vWorkQueue.push_back(pblockOrphan->GetHash());
\r
1458 mapOrphanBlocks.erase(pblockOrphan->GetHash());
\r
1459 delete pblockOrphan;
\r
1461 mapOrphanBlocksByPrev.erase(hashPrev);
\r
1464 printf("ProcessBlock: ACCEPTED\n");
\r
1475 template<typename Stream>
\r
1476 bool ScanMessageStart(Stream& s)
\r
1478 // Scan ahead to the next pchMessageStart, which should normally be immediately
\r
1479 // at the file pointer. Leaves file pointer at end of pchMessageStart.
\r
1481 short prevmask = s.exceptions(0);
\r
1482 const char* p = BEGIN(pchMessageStart);
\r
1492 s.exceptions(prevmask);
\r
1496 p = BEGIN(pchMessageStart);
\r
1499 if (++p == END(pchMessageStart))
\r
1502 s.exceptions(prevmask);
\r
1511 s.exceptions(prevmask);
\r
1516 bool CheckDiskSpace(int64 nAdditionalBytes)
\r
1518 uint64 nFreeBytesAvailable = filesystem::space(GetDataDir()).available;
\r
1520 // Check for 15MB because database could create another 10MB log file at any time
\r
1521 if (nFreeBytesAvailable < (int64)15000000 + nAdditionalBytes)
\r
1524 printf("*** %s***\n", _("Warning: Disk space is low "));
\r
1526 ThreadSafeMessageBox(_("Warning: Disk space is low "), "Bitcoin", wxOK | wxICON_EXCLAMATION);
\r
1528 CreateThread(Shutdown, NULL);
\r
1534 FILE* OpenBlockFile(unsigned int nFile, unsigned int nBlockPos, const char* pszMode)
\r
1538 FILE* file = fopen(strprintf("%s/blk%04d.dat", GetDataDir().c_str(), nFile).c_str(), pszMode);
\r
1541 if (nBlockPos != 0 && !strchr(pszMode, 'a') && !strchr(pszMode, 'w'))
\r
1543 if (fseek(file, nBlockPos, SEEK_SET) != 0)
\r
1552 static unsigned int nCurrentBlockFile = 1;
\r
1554 FILE* AppendBlockFile(unsigned int& nFileRet)
\r
1559 FILE* file = OpenBlockFile(nCurrentBlockFile, 0, "ab");
\r
1562 if (fseek(file, 0, SEEK_END) != 0)
\r
1564 // FAT32 filesize max 4GB, fseek and ftell max 2GB, so we must stay under 2GB
\r
1565 if (ftell(file) < 0x7F000000 - MAX_SIZE)
\r
1567 nFileRet = nCurrentBlockFile;
\r
1571 nCurrentBlockFile++;
\r
1575 bool LoadBlockIndex(bool fAllowNew)
\r
1578 // Load block index
\r
1581 if (!txdb.LoadBlockIndex())
\r
1586 // Init with genesis block
\r
1588 if (mapBlockIndex.empty())
\r
1595 // GetHash() = 0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
\r
1596 // hashMerkleRoot = 0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
\r
1597 // txNew.vin[0].scriptSig = 486604799 4 0x736B6E616220726F662074756F6C69616220646E6F63657320666F206B6E697262206E6F20726F6C6C65636E61684320393030322F6E614A2F33302073656D695420656854
\r
1598 // txNew.vout[0].nValue = 5000000000
\r
1599 // txNew.vout[0].scriptPubKey = 0x5F1DF16B2B704C8A578D0BBAF74D385CDE12C11EE50455F3C438EF4C3FBCF649B6DE611FEAE06279A60939E028A8D65C10B73071A6F16719274855FEB0FD8A6704 OP_CHECKSIG
\r
1600 // block.nVersion = 1
\r
1601 // block.nTime = 1231006505
\r
1602 // block.nBits = 0x1d00ffff
\r
1603 // block.nNonce = 2083236893
\r
1604 // CBlock(hash=000000000019d6, ver=1, hashPrevBlock=00000000000000, hashMerkleRoot=4a5e1e, nTime=1231006505, nBits=1d00ffff, nNonce=2083236893, vtx=1)
\r
1605 // CTransaction(hash=4a5e1e, ver=1, vin.size=1, vout.size=1, nLockTime=0)
\r
1606 // CTxIn(COutPoint(000000, -1), coinbase 04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73)
\r
1607 // CTxOut(nValue=50.00000000, scriptPubKey=0x5F1DF16B2B704C8A578D0B)
\r
1608 // vMerkleTree: 4a5e1e
\r
1611 const char* pszTimestamp = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks";
\r
1612 CTransaction txNew;
\r
1613 txNew.vin.resize(1);
\r
1614 txNew.vout.resize(1);
\r
1615 txNew.vin[0].scriptSig = CScript() << 486604799 << CBigNum(4) << vector<unsigned char>((const unsigned char*)pszTimestamp, (const unsigned char*)pszTimestamp + strlen(pszTimestamp));
\r
1616 txNew.vout[0].nValue = 50 * COIN;
\r
1618 bnPubKey.SetHex("0x5F1DF16B2B704C8A578D0BBAF74D385CDE12C11EE50455F3C438EF4C3FBCF649B6DE611FEAE06279A60939E028A8D65C10B73071A6F16719274855FEB0FD8A6704");
\r
1619 txNew.vout[0].scriptPubKey = CScript() << bnPubKey << OP_CHECKSIG;
\r
1621 block.vtx.push_back(txNew);
\r
1622 block.hashPrevBlock = 0;
\r
1623 block.hashMerkleRoot = block.BuildMerkleTree();
\r
1624 block.nVersion = 1;
\r
1625 block.nTime = 1231006505;
\r
1626 block.nBits = 0x1d00ffff;
\r
1627 block.nNonce = 2083236893;
\r
1630 printf("%s\n", block.GetHash().ToString().c_str());
\r
1631 printf("%s\n", block.hashMerkleRoot.ToString().c_str());
\r
1632 printf("%s\n", hashGenesisBlock.ToString().c_str());
\r
1633 txNew.vout[0].scriptPubKey.print();
\r
1635 assert(block.hashMerkleRoot == uint256("0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"));
\r
1637 assert(block.GetHash() == hashGenesisBlock);
\r
1639 // Start new block file
\r
1640 unsigned int nFile;
\r
1641 unsigned int nBlockPos;
\r
1642 if (!block.WriteToDisk(!fClient, nFile, nBlockPos))
\r
1643 return error("LoadBlockIndex() : writing genesis block to disk failed");
\r
1644 if (!block.AddToBlockIndex(nFile, nBlockPos))
\r
1645 return error("LoadBlockIndex() : genesis block not accepted");
\r
1653 void PrintBlockTree()
\r
1655 // precompute tree structure
\r
1656 map<CBlockIndex*, vector<CBlockIndex*> > mapNext;
\r
1657 for (map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.begin(); mi != mapBlockIndex.end(); ++mi)
\r
1659 CBlockIndex* pindex = (*mi).second;
\r
1660 mapNext[pindex->pprev].push_back(pindex);
\r
1662 //while (rand() % 3 == 0)
\r
1663 // mapNext[pindex->pprev].push_back(pindex);
\r
1666 vector<pair<int, CBlockIndex*> > vStack;
\r
1667 vStack.push_back(make_pair(0, pindexGenesisBlock));
\r
1670 while (!vStack.empty())
\r
1672 int nCol = vStack.back().first;
\r
1673 CBlockIndex* pindex = vStack.back().second;
\r
1674 vStack.pop_back();
\r
1676 // print split or gap
\r
1677 if (nCol > nPrevCol)
\r
1679 for (int i = 0; i < nCol-1; i++)
\r
1683 else if (nCol < nPrevCol)
\r
1685 for (int i = 0; i < nCol; i++)
\r
1692 for (int i = 0; i < nCol; i++)
\r
1697 block.ReadFromDisk(pindex);
\r
1698 printf("%d (%u,%u) %s %s tx %d",
\r
1701 pindex->nBlockPos,
\r
1702 block.GetHash().ToString().substr(0,16).c_str(),
\r
1703 DateTimeStrFormat("%x %H:%M:%S", block.nTime).c_str(),
\r
1704 block.vtx.size());
\r
1706 CRITICAL_BLOCK(cs_mapWallet)
\r
1708 if (mapWallet.count(block.vtx[0].GetHash()))
\r
1710 CWalletTx& wtx = mapWallet[block.vtx[0].GetHash()];
\r
1711 printf(" mine: %d %d %d", wtx.GetDepthInMainChain(), wtx.GetBlocksToMaturity(), wtx.GetCredit());
\r
1717 // put the main timechain first
\r
1718 vector<CBlockIndex*>& vNext = mapNext[pindex];
\r
1719 for (int i = 0; i < vNext.size(); i++)
\r
1721 if (vNext[i]->pnext)
\r
1723 swap(vNext[0], vNext[i]);
\r
1728 // iterate children
\r
1729 for (int i = 0; i < vNext.size(); i++)
\r
1730 vStack.push_back(make_pair(nCol+i, vNext[i]));
\r
1743 //////////////////////////////////////////////////////////////////////////////
\r
1749 bool AlreadyHave(CTxDB& txdb, const CInv& inv)
\r
1753 case MSG_TX: return mapTransactions.count(inv.hash) || txdb.ContainsTx(inv.hash);
\r
1754 case MSG_BLOCK: return mapBlockIndex.count(inv.hash) || mapOrphanBlocks.count(inv.hash);
\r
1756 // Don't know what it is, just say we already got one
\r
1766 bool ProcessMessages(CNode* pfrom)
\r
1768 CDataStream& vRecv = pfrom->vRecv;
\r
1769 if (vRecv.empty())
\r
1772 // printf("ProcessMessages(%d bytes)\n", vRecv.size());
\r
1776 // (4) message start
\r
1785 // Scan for message start
\r
1786 CDataStream::iterator pstart = search(vRecv.begin(), vRecv.end(), BEGIN(pchMessageStart), END(pchMessageStart));
\r
1787 int nHeaderSize = vRecv.GetSerializeSize(CMessageHeader());
\r
1788 if (vRecv.end() - pstart < nHeaderSize)
\r
1790 if (vRecv.size() > nHeaderSize)
\r
1792 printf("\n\nPROCESSMESSAGE MESSAGESTART NOT FOUND\n\n");
\r
1793 vRecv.erase(vRecv.begin(), vRecv.end() - nHeaderSize);
\r
1797 if (pstart - vRecv.begin() > 0)
\r
1798 printf("\n\nPROCESSMESSAGE SKIPPED %d BYTES\n\n", pstart - vRecv.begin());
\r
1799 vRecv.erase(vRecv.begin(), pstart);
\r
1802 vector<char> vHeaderSave(vRecv.begin(), vRecv.begin() + nHeaderSize);
\r
1803 CMessageHeader hdr;
\r
1805 if (!hdr.IsValid())
\r
1807 printf("\n\nPROCESSMESSAGE: ERRORS IN HEADER %s\n\n\n", hdr.GetCommand().c_str());
\r
1810 string strCommand = hdr.GetCommand();
\r
1813 unsigned int nMessageSize = hdr.nMessageSize;
\r
1814 if (nMessageSize > vRecv.size())
\r
1816 // Rewind and wait for rest of message
\r
1817 ///// need a mechanism to give up waiting for overlong message size error
\r
1818 vRecv.insert(vRecv.begin(), vHeaderSave.begin(), vHeaderSave.end());
\r
1822 // Copy message to its own buffer
\r
1823 CDataStream vMsg(vRecv.begin(), vRecv.begin() + nMessageSize, vRecv.nType, vRecv.nVersion);
\r
1824 vRecv.ignore(nMessageSize);
\r
1827 if (vRecv.GetVersion() >= 209)
\r
1829 uint256 hash = Hash(vMsg.begin(), vMsg.end());
\r
1830 unsigned int nChecksum = 0;
\r
1831 memcpy(&nChecksum, &hash, sizeof(nChecksum));
\r
1832 if (nChecksum != hdr.nChecksum)
\r
1834 printf("ProcessMessage(%s, %d bytes) : CHECKSUM ERROR nChecksum=%08x hdr.nChecksum=%08x\n",
\r
1835 strCommand.c_str(), nMessageSize, nChecksum, hdr.nChecksum);
\r
1840 // Process message
\r
1841 bool fRet = false;
\r
1844 CRITICAL_BLOCK(cs_main)
\r
1845 fRet = ProcessMessage(pfrom, strCommand, vMsg);
\r
1849 catch (std::ios_base::failure& e)
\r
1851 if (strstr(e.what(), "CDataStream::read() : end of data"))
\r
1853 // Allow exceptions from underlength message on vRecv
\r
1854 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
1856 else if (strstr(e.what(), ": size too large"))
\r
1858 // Allow exceptions from overlong size
\r
1859 printf("ProcessMessage(%s, %d bytes) : Exception '%s' caught\n", strCommand.c_str(), nMessageSize, e.what());
\r
1863 PrintException(&e, "ProcessMessage()");
\r
1866 catch (std::exception& e) {
\r
1867 PrintException(&e, "ProcessMessage()");
\r
1869 PrintException(NULL, "ProcessMessage()");
\r
1873 printf("ProcessMessage(%s, %d bytes) FAILED\n", strCommand.c_str(), nMessageSize);
\r
1883 bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
\r
1885 static map<unsigned int, vector<unsigned char> > mapReuseKey;
\r
1886 RandAddSeedPerfmon();
\r
1888 printf("%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
\r
1889 printf("received: %s (%d bytes)\n", strCommand.c_str(), vRecv.size());
\r
1890 if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0)
\r
1892 printf("dropmessagestest DROPPING RECV MESSAGE\n");
\r
1900 if (strCommand == "version")
\r
1902 // Each connection can only send one version message
\r
1903 if (pfrom->nVersion != 0)
\r
1908 CAddress addrFrom;
\r
1909 uint64 nNonce = 1;
\r
1911 vRecv >> pfrom->nVersion >> pfrom->nServices >> nTime >> addrMe;
\r
1912 if (pfrom->nVersion == 10300)
\r
1913 pfrom->nVersion = 300;
\r
1914 if (pfrom->nVersion >= 106 && !vRecv.empty())
\r
1915 vRecv >> addrFrom >> nNonce;
\r
1916 if (pfrom->nVersion >= 106 && !vRecv.empty())
\r
1917 vRecv >> strSubVer;
\r
1918 if (pfrom->nVersion >= 209 && !vRecv.empty())
\r
1919 vRecv >> pfrom->nStartingHeight;
\r
1921 if (pfrom->nVersion == 0)
\r
1924 // Disconnect if we connected to ourself
\r
1925 if (nNonce == nLocalHostNonce && nNonce > 1)
\r
1927 pfrom->fDisconnect = true;
\r
1931 pfrom->fClient = !(pfrom->nServices & NODE_NETWORK);
\r
1932 if (pfrom->fClient)
\r
1934 pfrom->vSend.nType |= SER_BLOCKHEADERONLY;
\r
1935 pfrom->vRecv.nType |= SER_BLOCKHEADERONLY;
\r
1938 AddTimeData(pfrom->addr.ip, nTime);
\r
1941 if (pfrom->nVersion >= 209)
\r
1942 pfrom->PushMessage("verack");
\r
1943 pfrom->vSend.SetVersion(min(pfrom->nVersion, VERSION));
\r
1944 if (pfrom->nVersion < 209)
\r
1945 pfrom->vRecv.SetVersion(min(pfrom->nVersion, VERSION));
\r
1947 // Ask the first connected node for block updates
\r
1948 static int nAskedForBlocks;
\r
1949 if (!pfrom->fClient && (nAskedForBlocks < 1 || vNodes.size() <= 1))
\r
1951 nAskedForBlocks++;
\r
1952 pfrom->PushGetBlocks(pindexBest, uint256(0));
\r
1955 pfrom->fSuccessfullyConnected = true;
\r
1957 printf("version message: version %d, blocks=%d\n", pfrom->nVersion, pfrom->nStartingHeight);
\r
1961 else if (pfrom->nVersion == 0)
\r
1963 // Must have a version message before anything else
\r
1968 else if (strCommand == "verack")
\r
1970 pfrom->vRecv.SetVersion(min(pfrom->nVersion, VERSION));
\r
1974 else if (strCommand == "addr")
\r
1976 vector<CAddress> vAddr;
\r
1978 if (pfrom->nVersion < 200) // don't want addresses from 0.1.5
\r
1980 if (pfrom->nVersion < 209 && mapAddresses.size() > 1000) // don't want addr from 0.2.0 unless seeding
\r
1982 if (vAddr.size() > 1000)
\r
1983 return error("message addr size() = %d", vAddr.size());
\r
1985 // Store the new addresses
\r
1986 foreach(CAddress& addr, vAddr)
\r
1990 // ignore IPv6 for now, since it isn't implemented anyway
\r
1991 if (!addr.IsIPv4())
\r
1993 addr.nTime = GetAdjustedTime() - 2 * 60 * 60;
\r
1994 if (pfrom->fGetAddr || vAddr.size() > 10)
\r
1995 addr.nTime -= 5 * 24 * 60 * 60;
\r
1997 pfrom->AddAddressKnown(addr);
\r
1998 if (!pfrom->fGetAddr && addr.IsRoutable())
\r
2000 // Relay to a limited number of other nodes
\r
2001 CRITICAL_BLOCK(cs_vNodes)
\r
2003 // Use deterministic randomness to send to
\r
2004 // the same places for 12 hours at a time
\r
2005 static uint256 hashSalt;
\r
2006 if (hashSalt == 0)
\r
2007 RAND_bytes((unsigned char*)&hashSalt, sizeof(hashSalt));
\r
2008 uint256 hashRand = addr.ip ^ ((GetTime()+addr.ip)/(12*60*60)) ^ hashSalt;
\r
2009 multimap<uint256, CNode*> mapMix;
\r
2010 foreach(CNode* pnode, vNodes)
\r
2011 mapMix.insert(make_pair(hashRand = Hash(BEGIN(hashRand), END(hashRand)), pnode));
\r
2012 int nRelayNodes = 4;
\r
2013 for (multimap<uint256, CNode*>::iterator mi = mapMix.begin(); mi != mapMix.end() && nRelayNodes-- > 0; ++mi)
\r
2014 ((*mi).second)->PushAddress(addr);
\r
2018 if (vAddr.size() < 1000)
\r
2019 pfrom->fGetAddr = false;
\r
2023 else if (strCommand == "inv")
\r
2025 vector<CInv> vInv;
\r
2027 if (vInv.size() > 50000)
\r
2028 return error("message inv size() = %d", vInv.size());
\r
2031 foreach(const CInv& inv, vInv)
\r
2035 pfrom->AddInventoryKnown(inv);
\r
2037 bool fAlreadyHave = AlreadyHave(txdb, inv);
\r
2038 printf(" got inventory: %s %s\n", inv.ToString().c_str(), fAlreadyHave ? "have" : "new");
\r
2040 if (!fAlreadyHave)
\r
2041 pfrom->AskFor(inv);
\r
2042 else if (inv.type == MSG_BLOCK && mapOrphanBlocks.count(inv.hash))
\r
2043 pfrom->PushGetBlocks(pindexBest, GetOrphanRoot(mapOrphanBlocks[inv.hash]));
\r
2045 // Track requests for our stuff
\r
2046 CRITICAL_BLOCK(cs_mapRequestCount)
\r
2048 map<uint256, int>::iterator mi = mapRequestCount.find(inv.hash);
\r
2049 if (mi != mapRequestCount.end())
\r
2056 else if (strCommand == "getdata")
\r
2058 vector<CInv> vInv;
\r
2060 if (vInv.size() > 50000)
\r
2061 return error("message getdata size() = %d", vInv.size());
\r
2063 foreach(const CInv& inv, vInv)
\r
2067 printf("received getdata for: %s\n", inv.ToString().c_str());
\r
2069 if (inv.type == MSG_BLOCK)
\r
2071 // Send block from disk
\r
2072 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(inv.hash);
\r
2073 if (mi != mapBlockIndex.end())
\r
2075 //// could optimize this to send header straight from blockindex for client
\r
2077 block.ReadFromDisk((*mi).second, !pfrom->fClient);
\r
2078 pfrom->PushMessage("block", block);
\r
2080 // Trigger them to send a getblocks request for the next batch of inventory
\r
2081 if (inv.hash == pfrom->hashContinue)
\r
2083 // Bypass PushInventory, this must send even if redundant,
\r
2084 // and we want it right after the last block so they don't
\r
2085 // wait for other stuff first.
\r
2086 vector<CInv> vInv;
\r
2087 vInv.push_back(CInv(MSG_BLOCK, hashBestChain));
\r
2088 pfrom->PushMessage("inv", vInv);
\r
2089 pfrom->hashContinue = 0;
\r
2093 else if (inv.IsKnownType())
\r
2095 // Send stream from relay memory
\r
2096 CRITICAL_BLOCK(cs_mapRelay)
\r
2098 map<CInv, CDataStream>::iterator mi = mapRelay.find(inv);
\r
2099 if (mi != mapRelay.end())
\r
2100 pfrom->PushMessage(inv.GetCommand(), (*mi).second);
\r
2104 // Track requests for our stuff
\r
2105 CRITICAL_BLOCK(cs_mapRequestCount)
\r
2107 map<uint256, int>::iterator mi = mapRequestCount.find(inv.hash);
\r
2108 if (mi != mapRequestCount.end())
\r
2115 else if (strCommand == "getblocks")
\r
2117 CBlockLocator locator;
\r
2119 vRecv >> locator >> hashStop;
\r
2121 // Find the first block the caller has in the main chain
\r
2122 CBlockIndex* pindex = locator.GetBlockIndex();
\r
2124 // Send the rest of the chain
\r
2126 pindex = pindex->pnext;
\r
2127 int nLimit = 500 + locator.GetDistanceBack();
\r
2128 printf("getblocks %d to %s limit %d\n", (pindex ? pindex->nHeight : -1), hashStop.ToString().substr(0,16).c_str(), nLimit);
\r
2129 for (; pindex; pindex = pindex->pnext)
\r
2131 if (pindex->GetBlockHash() == hashStop)
\r
2133 printf(" getblocks stopping at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString().substr(0,16).c_str());
\r
2136 pfrom->PushInventory(CInv(MSG_BLOCK, pindex->GetBlockHash()));
\r
2137 if (--nLimit <= 0)
\r
2139 // When this block is requested, we'll send an inv that'll make them
\r
2140 // getblocks the next batch of inventory.
\r
2141 printf(" getblocks stopping at limit %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString().substr(0,16).c_str());
\r
2142 pfrom->hashContinue = pindex->GetBlockHash();
\r
2149 else if (strCommand == "tx")
\r
2151 vector<uint256> vWorkQueue;
\r
2152 CDataStream vMsg(vRecv);
\r
2156 CInv inv(MSG_TX, tx.GetHash());
\r
2157 pfrom->AddInventoryKnown(inv);
\r
2159 bool fMissingInputs = false;
\r
2160 if (tx.AcceptTransaction(true, &fMissingInputs))
\r
2162 AddToWalletIfMine(tx, NULL);
\r
2163 RelayMessage(inv, vMsg);
\r
2164 mapAlreadyAskedFor.erase(inv);
\r
2165 vWorkQueue.push_back(inv.hash);
\r
2167 // Recursively process any orphan transactions that depended on this one
\r
2168 for (int i = 0; i < vWorkQueue.size(); i++)
\r
2170 uint256 hashPrev = vWorkQueue[i];
\r
2171 for (multimap<uint256, CDataStream*>::iterator mi = mapOrphanTransactionsByPrev.lower_bound(hashPrev);
\r
2172 mi != mapOrphanTransactionsByPrev.upper_bound(hashPrev);
\r
2175 const CDataStream& vMsg = *((*mi).second);
\r
2177 CDataStream(vMsg) >> tx;
\r
2178 CInv inv(MSG_TX, tx.GetHash());
\r
2180 if (tx.AcceptTransaction(true))
\r
2182 printf(" accepted orphan tx %s\n", inv.hash.ToString().substr(0,6).c_str());
\r
2183 AddToWalletIfMine(tx, NULL);
\r
2184 RelayMessage(inv, vMsg);
\r
2185 mapAlreadyAskedFor.erase(inv);
\r
2186 vWorkQueue.push_back(inv.hash);
\r
2191 foreach(uint256 hash, vWorkQueue)
\r
2192 EraseOrphanTx(hash);
\r
2194 else if (fMissingInputs)
\r
2196 printf("storing orphan tx %s\n", inv.hash.ToString().substr(0,6).c_str());
\r
2197 AddOrphanTx(vMsg);
\r
2202 else if (strCommand == "block")
\r
2204 auto_ptr<CBlock> pblock(new CBlock);
\r
2208 printf("received block %s\n", pblock->GetHash().ToString().substr(0,16).c_str());
\r
2209 // pblock->print();
\r
2211 CInv inv(MSG_BLOCK, pblock->GetHash());
\r
2212 pfrom->AddInventoryKnown(inv);
\r
2214 if (ProcessBlock(pfrom, pblock.release()))
\r
2215 mapAlreadyAskedFor.erase(inv);
\r
2219 else if (strCommand == "getaddr")
\r
2221 // This includes all nodes that are currently online,
\r
2222 // since they rebroadcast an addr every 24 hours
\r
2223 pfrom->vAddrToSend.clear();
\r
2224 int64 nSince = GetAdjustedTime() - 24 * 60 * 60; // in the last 24 hours
\r
2225 CRITICAL_BLOCK(cs_mapAddresses)
\r
2227 unsigned int nSize = mapAddresses.size();
\r
2228 foreach(const PAIRTYPE(vector<unsigned char>, CAddress)& item, mapAddresses)
\r
2232 const CAddress& addr = item.second;
\r
2233 if (addr.nTime > nSince)
\r
2234 pfrom->PushAddress(addr);
\r
2240 else if (strCommand == "checkorder")
\r
2242 uint256 hashReply;
\r
2244 vRecv >> hashReply >> order;
\r
2246 /// we have a chance to check the order here
\r
2248 // Keep giving the same key to the same ip until they use it
\r
2249 if (!mapReuseKey.count(pfrom->addr.ip))
\r
2250 mapReuseKey[pfrom->addr.ip] = GenerateNewKey();
\r
2252 // Send back approval of order and pubkey to use
\r
2253 CScript scriptPubKey;
\r
2254 scriptPubKey << mapReuseKey[pfrom->addr.ip] << OP_CHECKSIG;
\r
2255 pfrom->PushMessage("reply", hashReply, (int)0, scriptPubKey);
\r
2259 else if (strCommand == "submitorder")
\r
2261 uint256 hashReply;
\r
2263 vRecv >> hashReply >> wtxNew;
\r
2264 wtxNew.fFromMe = false;
\r
2267 if (!wtxNew.AcceptWalletTransaction())
\r
2269 pfrom->PushMessage("reply", hashReply, (int)1);
\r
2270 return error("submitorder AcceptWalletTransaction() failed, returning error 1");
\r
2272 wtxNew.fTimeReceivedIsTxTime = true;
\r
2273 AddToWallet(wtxNew);
\r
2274 wtxNew.RelayWalletTransaction();
\r
2275 mapReuseKey.erase(pfrom->addr.ip);
\r
2277 // Send back confirmation
\r
2278 pfrom->PushMessage("reply", hashReply, (int)0);
\r
2282 else if (strCommand == "reply")
\r
2284 uint256 hashReply;
\r
2285 vRecv >> hashReply;
\r
2287 CRequestTracker tracker;
\r
2288 CRITICAL_BLOCK(pfrom->cs_mapRequests)
\r
2290 map<uint256, CRequestTracker>::iterator mi = pfrom->mapRequests.find(hashReply);
\r
2291 if (mi != pfrom->mapRequests.end())
\r
2293 tracker = (*mi).second;
\r
2294 pfrom->mapRequests.erase(mi);
\r
2297 if (!tracker.IsNull())
\r
2298 tracker.fn(tracker.param1, vRecv);
\r
2302 else if (strCommand == "ping")
\r
2309 // Ignore unknown commands for extensibility
\r
2313 // Update the last seen time for this node's address
\r
2314 if (pfrom->fNetworkNode)
\r
2315 if (strCommand == "version" || strCommand == "addr" || strCommand == "inv" || strCommand == "getdata" || strCommand == "ping")
\r
2316 AddressCurrentlyConnected(pfrom->addr);
\r
2330 bool SendMessages(CNode* pto, bool fSendTrickle)
\r
2332 CRITICAL_BLOCK(cs_main)
\r
2334 // Don't send anything until we get their version message
\r
2335 if (pto->nVersion == 0)
\r
2338 // Keep-alive ping
\r
2339 if (pto->nLastSend && GetTime() - pto->nLastSend > 30 * 60 && pto->vSend.empty())
\r
2340 pto->PushMessage("ping");
\r
2342 // Address refresh broadcast
\r
2343 static int64 nLastRebroadcast;
\r
2344 if (GetTime() - nLastRebroadcast > 24 * 60 * 60) // every 24 hours
\r
2346 nLastRebroadcast = GetTime();
\r
2347 CRITICAL_BLOCK(cs_vNodes)
\r
2349 foreach(CNode* pnode, vNodes)
\r
2351 // Periodically clear setAddrKnown to allow refresh broadcasts
\r
2352 pnode->setAddrKnown.clear();
\r
2354 // Rebroadcast our address
\r
2355 if (addrLocalHost.IsRoutable() && !fUseProxy)
\r
2356 pnode->PushAddress(addrLocalHost);
\r
2361 // Resend wallet transactions that haven't gotten in a block yet
\r
2362 ResendWalletTransactions();
\r
2370 vector<CAddress> vAddr;
\r
2371 vAddr.reserve(pto->vAddrToSend.size());
\r
2372 foreach(const CAddress& addr, pto->vAddrToSend)
\r
2374 // returns true if wasn't already contained in the set
\r
2375 if (pto->setAddrKnown.insert(addr).second)
\r
2377 vAddr.push_back(addr);
\r
2378 // receiver rejects addr messages larger than 1000
\r
2379 if (vAddr.size() >= 1000)
\r
2381 pto->PushMessage("addr", vAddr);
\r
2386 pto->vAddrToSend.clear();
\r
2387 if (!vAddr.empty())
\r
2388 pto->PushMessage("addr", vAddr);
\r
2393 // Message: inventory
\r
2395 vector<CInv> vInv;
\r
2396 vector<CInv> vInvWait;
\r
2397 CRITICAL_BLOCK(pto->cs_inventory)
\r
2399 vInv.reserve(pto->vInventoryToSend.size());
\r
2400 vInvWait.reserve(pto->vInventoryToSend.size());
\r
2401 foreach(const CInv& inv, pto->vInventoryToSend)
\r
2403 if (pto->setInventoryKnown.count(inv))
\r
2406 // trickle out tx inv to protect privacy
\r
2407 if (inv.type == MSG_TX && !fSendTrickle)
\r
2409 // 1/4 of tx invs blast to all immediately
\r
2410 static uint256 hashSalt;
\r
2411 if (hashSalt == 0)
\r
2412 RAND_bytes((unsigned char*)&hashSalt, sizeof(hashSalt));
\r
2413 uint256 hashRand = inv.hash ^ hashSalt;
\r
2414 hashRand = Hash(BEGIN(hashRand), END(hashRand));
\r
2415 bool fTrickleWait = ((hashRand & 3) != 0);
\r
2417 // always trickle our own transactions
\r
2418 if (!fTrickleWait)
\r
2420 TRY_CRITICAL_BLOCK(cs_mapWallet)
\r
2422 map<uint256, CWalletTx>::iterator mi = mapWallet.find(inv.hash);
\r
2423 if (mi != mapWallet.end())
\r
2425 CWalletTx& wtx = (*mi).second;
\r
2427 fTrickleWait = true;
\r
2434 vInvWait.push_back(inv);
\r
2439 // returns true if wasn't already contained in the set
\r
2440 if (pto->setInventoryKnown.insert(inv).second)
\r
2442 vInv.push_back(inv);
\r
2443 if (vInv.size() >= 1000)
\r
2445 pto->PushMessage("inv", vInv);
\r
2450 pto->vInventoryToSend = vInvWait;
\r
2452 if (!vInv.empty())
\r
2453 pto->PushMessage("inv", vInv);
\r
2457 // Message: getdata
\r
2459 vector<CInv> vGetData;
\r
2460 int64 nNow = GetTime() * 1000000;
\r
2462 while (!pto->mapAskFor.empty() && (*pto->mapAskFor.begin()).first <= nNow)
\r
2464 const CInv& inv = (*pto->mapAskFor.begin()).second;
\r
2465 if (!AlreadyHave(txdb, inv))
\r
2467 printf("sending getdata: %s\n", inv.ToString().c_str());
\r
2468 vGetData.push_back(inv);
\r
2469 if (vGetData.size() >= 1000)
\r
2471 pto->PushMessage("getdata", vGetData);
\r
2475 pto->mapAskFor.erase(pto->mapAskFor.begin());
\r
2477 if (!vGetData.empty())
\r
2478 pto->PushMessage("getdata", vGetData);
\r
2497 //////////////////////////////////////////////////////////////////////////////
\r
2502 void GenerateBitcoins(bool fGenerate)
\r
2504 if (fGenerateBitcoins != fGenerate)
\r
2506 fGenerateBitcoins = fGenerate;
\r
2507 CWalletDB().WriteSetting("fGenerateBitcoins", fGenerateBitcoins);
\r
2508 MainFrameRepaint();
\r
2510 if (fGenerateBitcoins)
\r
2512 int nProcessors = boost::thread::hardware_concurrency();
\r
2513 printf("%d processors\n", nProcessors);
\r
2514 if (nProcessors < 1)
\r
2516 if (fLimitProcessors && nProcessors > nLimitProcessors)
\r
2517 nProcessors = nLimitProcessors;
\r
2518 int nAddThreads = nProcessors - vnThreadsRunning[3];
\r
2519 printf("Starting %d BitcoinMiner threads\n", nAddThreads);
\r
2520 for (int i = 0; i < nAddThreads; i++)
\r
2522 if (!CreateThread(ThreadBitcoinMiner, NULL))
\r
2523 printf("Error: CreateThread(ThreadBitcoinMiner) failed\n");
\r
2529 void ThreadBitcoinMiner(void* parg)
\r
2533 vnThreadsRunning[3]++;
\r
2535 vnThreadsRunning[3]--;
\r
2537 catch (std::exception& e) {
\r
2538 vnThreadsRunning[3]--;
\r
2539 PrintException(&e, "ThreadBitcoinMiner()");
\r
2541 vnThreadsRunning[3]--;
\r
2542 PrintException(NULL, "ThreadBitcoinMiner()");
\r
2544 UIThreadCall(bind(CalledSetStatusBar, "", 0));
\r
2545 printf("ThreadBitcoinMiner exiting, %d threads remaining\n", vnThreadsRunning[3]);
\r
2548 int FormatHashBlocks(void* pbuffer, unsigned int len)
\r
2550 unsigned char* pdata = (unsigned char*)pbuffer;
\r
2551 unsigned int blocks = 1 + ((len + 8) / 64);
\r
2552 unsigned char* pend = pdata + 64 * blocks;
\r
2553 memset(pdata + len, 0, 64 * blocks - len);
\r
2554 pdata[len] = 0x80;
\r
2555 unsigned int bits = len * 8;
\r
2556 pend[-1] = (bits >> 0) & 0xff;
\r
2557 pend[-2] = (bits >> 8) & 0xff;
\r
2558 pend[-3] = (bits >> 16) & 0xff;
\r
2559 pend[-4] = (bits >> 24) & 0xff;
\r
2563 using CryptoPP::ByteReverse;
\r
2565 static const unsigned int pSHA256InitState[8] =
\r
2566 {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
\r
2568 inline void SHA256Transform(void* pstate, void* pinput, const void* pinit)
\r
2570 memcpy(pstate, pinit, 32);
\r
2571 CryptoPP::SHA256::Transform((CryptoPP::word32*)pstate, (CryptoPP::word32*)pinput);
\r
2578 void BitcoinMiner()
\r
2580 printf("BitcoinMiner started\n");
\r
2581 SetThreadPriority(THREAD_PRIORITY_LOWEST);
\r
2585 CBigNum bnExtraNonce = 0;
\r
2586 while (fGenerateBitcoins)
\r
2591 while (vNodes.empty() || IsInitialBlockDownload())
\r
2596 if (!fGenerateBitcoins)
\r
2600 unsigned int nTransactionsUpdatedLast = nTransactionsUpdated;
\r
2601 CBlockIndex* pindexPrev = pindexBest;
\r
2602 unsigned int nBits = GetNextWorkRequired(pindexPrev);
\r
2606 // Create coinbase tx
\r
2608 CTransaction txNew;
\r
2609 txNew.vin.resize(1);
\r
2610 txNew.vin[0].prevout.SetNull();
\r
2611 txNew.vin[0].scriptSig << nBits << ++bnExtraNonce;
\r
2612 txNew.vout.resize(1);
\r
2613 txNew.vout[0].scriptPubKey << key.GetPubKey() << OP_CHECKSIG;
\r
2617 // Create new block
\r
2619 auto_ptr<CBlock> pblock(new CBlock());
\r
2620 if (!pblock.get())
\r
2623 // Add our coinbase tx as first transaction
\r
2624 pblock->vtx.push_back(txNew);
\r
2626 // Collect the latest transactions into the block
\r
2628 CRITICAL_BLOCK(cs_main)
\r
2629 CRITICAL_BLOCK(cs_mapTransactions)
\r
2632 map<uint256, CTxIndex> mapTestPool;
\r
2633 vector<char> vfAlreadyAdded(mapTransactions.size());
\r
2634 bool fFoundSomething = true;
\r
2635 unsigned int nBlockSize = 0;
\r
2636 while (fFoundSomething && nBlockSize < MAX_SIZE/2)
\r
2638 fFoundSomething = false;
\r
2639 unsigned int n = 0;
\r
2640 for (map<uint256, CTransaction>::iterator mi = mapTransactions.begin(); mi != mapTransactions.end(); ++mi, ++n)
\r
2642 if (vfAlreadyAdded[n])
\r
2644 CTransaction& tx = (*mi).second;
\r
2645 if (tx.IsCoinBase() || !tx.IsFinal())
\r
2647 unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK);
\r
2648 if (nBlockSize + nTxSize >= MAX_BLOCK_SIZE - 10000)
\r
2651 // Transaction fee based on block size
\r
2652 int64 nMinFee = tx.GetMinFee(nBlockSize);
\r
2654 map<uint256, CTxIndex> mapTestPoolTmp(mapTestPool);
\r
2655 if (!tx.ConnectInputs(txdb, mapTestPoolTmp, CDiskTxPos(1,1,1), 0, nFees, false, true, nMinFee))
\r
2657 swap(mapTestPool, mapTestPoolTmp);
\r
2659 pblock->vtx.push_back(tx);
\r
2660 nBlockSize += nTxSize;
\r
2661 vfAlreadyAdded[n] = true;
\r
2662 fFoundSomething = true;
\r
2666 pblock->nBits = nBits;
\r
2667 pblock->vtx[0].vout[0].nValue = pblock->GetBlockValue(pindexPrev->nHeight+1, nFees);
\r
2668 printf("Running BitcoinMiner with %d transactions in block\n", pblock->vtx.size());
\r
2672 // Prebuild hash buffer
\r
2674 struct tmpworkspace
\r
2679 uint256 hashPrevBlock;
\r
2680 uint256 hashMerkleRoot;
\r
2681 unsigned int nTime;
\r
2682 unsigned int nBits;
\r
2683 unsigned int nNonce;
\r
2686 unsigned char pchPadding0[64];
\r
2688 unsigned char pchPadding1[64];
\r
2690 char tmpbuf[sizeof(tmpworkspace)+16];
\r
2691 tmpworkspace& tmp = *(tmpworkspace*)alignup<16>(tmpbuf);
\r
2693 tmp.block.nVersion = pblock->nVersion;
\r
2694 tmp.block.hashPrevBlock = pblock->hashPrevBlock = (pindexPrev ? pindexPrev->GetBlockHash() : 0);
\r
2695 tmp.block.hashMerkleRoot = pblock->hashMerkleRoot = pblock->BuildMerkleTree();
\r
2696 tmp.block.nTime = pblock->nTime = max((pindexPrev ? pindexPrev->GetMedianTimePast()+1 : 0), GetAdjustedTime());
\r
2697 tmp.block.nBits = pblock->nBits = nBits;
\r
2698 tmp.block.nNonce = pblock->nNonce = 0;
\r
2700 unsigned int nBlocks0 = FormatHashBlocks(&tmp.block, sizeof(tmp.block));
\r
2701 unsigned int nBlocks1 = FormatHashBlocks(&tmp.hash1, sizeof(tmp.hash1));
\r
2703 // Byte swap all the input buffer
\r
2704 for (int i = 0; i < sizeof(tmp)/4; i++)
\r
2705 ((unsigned int*)&tmp)[i] = ByteReverse(((unsigned int*)&tmp)[i]);
\r
2707 // Precalc the first half of the first hash, which stays constant
\r
2708 uint256 midstatebuf[2];
\r
2709 uint256& midstate = *alignup<16>(midstatebuf);
\r
2710 SHA256Transform(&midstate, &tmp.block, pSHA256InitState);
\r
2716 int64 nStart = GetTime();
\r
2717 uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
\r
2718 uint256 hashbuf[2];
\r
2719 uint256& hash = *alignup<16>(hashbuf);
\r
2722 SHA256Transform(&tmp.hash1, (char*)&tmp.block + 64, &midstate);
\r
2723 SHA256Transform(&hash, &tmp.hash1, pSHA256InitState);
\r
2725 if (((unsigned short*)&hash)[14] == 0)
\r
2727 // Byte swap the result after preliminary check
\r
2728 for (int i = 0; i < sizeof(hash)/4; i++)
\r
2729 ((unsigned int*)&hash)[i] = ByteReverse(((unsigned int*)&hash)[i]);
\r
2731 if (hash <= hashTarget)
\r
2733 pblock->nNonce = ByteReverse(tmp.block.nNonce);
\r
2734 assert(hash == pblock->GetHash());
\r
2737 printf("BitcoinMiner:\n");
\r
2738 printf("proof-of-work found \n hash: %s \ntarget: %s\n", hash.GetHex().c_str(), hashTarget.GetHex().c_str());
\r
2740 printf("%s ", DateTimeStrFormat("%x %H:%M", GetTime()).c_str());
\r
2741 printf("generated %s\n", FormatMoney(pblock->vtx[0].vout[0].nValue).c_str());
\r
2743 SetThreadPriority(THREAD_PRIORITY_NORMAL);
\r
2744 CRITICAL_BLOCK(cs_main)
\r
2746 if (pindexPrev == pindexBest)
\r
2753 // Track how many getdata requests this block gets
\r
2754 CRITICAL_BLOCK(cs_mapRequestCount)
\r
2755 mapRequestCount[pblock->GetHash()] = 0;
\r
2757 // Process this block the same as if we had received it from another node
\r
2758 if (!ProcessBlock(NULL, pblock.release()))
\r
2759 printf("ERROR in BitcoinMiner, ProcessBlock, block not accepted\n");
\r
2762 SetThreadPriority(THREAD_PRIORITY_LOWEST);
\r
2769 // Update nTime every few seconds
\r
2770 const unsigned int nMask = 0xffff;
\r
2771 if ((++tmp.block.nNonce & nMask) == 0)
\r
2773 // Meter hashes/sec
\r
2774 static int64 nTimerStart;
\r
2775 static int nHashCounter;
\r
2776 if (nTimerStart == 0)
\r
2777 nTimerStart = GetTimeMillis();
\r
2780 if (GetTimeMillis() - nTimerStart > 4000)
\r
2782 static CCriticalSection cs;
\r
2783 CRITICAL_BLOCK(cs)
\r
2785 if (GetTimeMillis() - nTimerStart > 4000)
\r
2787 double dHashesPerSec = 1000.0 * (nMask+1) * nHashCounter / (GetTimeMillis() - nTimerStart);
\r
2788 nTimerStart = GetTimeMillis();
\r
2790 string strStatus = strprintf(" %.0f khash/s", dHashesPerSec/1000.0);
\r
2791 UIThreadCall(bind(CalledSetStatusBar, strStatus, 0));
\r
2792 static int64 nLogTime;
\r
2793 if (GetTime() - nLogTime > 30 * 60)
\r
2795 nLogTime = GetTime();
\r
2796 printf("%s ", DateTimeStrFormat("%x %H:%M", GetTime()).c_str());
\r
2797 printf("hashmeter %3d CPUs %6.0f khash/s\n", vnThreadsRunning[3], dHashesPerSec/1000.0);
\r
2803 // Check for stop or if block needs to be rebuilt
\r
2806 if (!fGenerateBitcoins)
\r
2808 if (fLimitProcessors && vnThreadsRunning[3] > nLimitProcessors)
\r
2810 if (vNodes.empty())
\r
2812 if (tmp.block.nNonce == 0)
\r
2814 if (nTransactionsUpdated != nTransactionsUpdatedLast && GetTime() - nStart > 60)
\r
2816 if (pindexPrev != pindexBest)
\r
2819 pblock->nTime = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
\r
2820 tmp.block.nTime = ByteReverse(pblock->nTime);
\r
2843 //////////////////////////////////////////////////////////////////////////////
\r
2849 int64 GetBalance()
\r
2851 int64 nStart = GetTimeMillis();
\r
2854 CRITICAL_BLOCK(cs_mapWallet)
\r
2856 for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
\r
2858 CWalletTx* pcoin = &(*it).second;
\r
2859 if (!pcoin->IsFinal() || pcoin->fSpent)
\r
2861 nTotal += pcoin->GetCredit(true);
\r
2865 //printf("GetBalance() %"PRI64d"ms\n", GetTimeMillis() - nStart);
\r
2870 int GetRandInt(int nMax)
\r
2872 return GetRand(nMax);
\r
2875 bool SelectCoins(int64 nTargetValue, set<CWalletTx*>& setCoinsRet)
\r
2877 setCoinsRet.clear();
\r
2879 // List of values less than target
\r
2880 int64 nLowestLarger = INT64_MAX;
\r
2881 CWalletTx* pcoinLowestLarger = NULL;
\r
2882 vector<pair<int64, CWalletTx*> > vValue;
\r
2883 int64 nTotalLower = 0;
\r
2885 CRITICAL_BLOCK(cs_mapWallet)
\r
2887 vector<CWalletTx*> vCoins;
\r
2888 vCoins.reserve(mapWallet.size());
\r
2889 for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
\r
2890 vCoins.push_back(&(*it).second);
\r
2891 random_shuffle(vCoins.begin(), vCoins.end(), GetRandInt);
\r
2893 foreach(CWalletTx* pcoin, vCoins)
\r
2895 if (!pcoin->IsFinal() || pcoin->fSpent)
\r
2897 int64 n = pcoin->GetCredit();
\r
2900 if (n < nTargetValue)
\r
2902 vValue.push_back(make_pair(n, pcoin));
\r
2905 else if (n == nTargetValue)
\r
2907 setCoinsRet.insert(pcoin);
\r
2910 else if (n < nLowestLarger)
\r
2912 nLowestLarger = n;
\r
2913 pcoinLowestLarger = pcoin;
\r
2918 if (nTotalLower < nTargetValue)
\r
2920 if (pcoinLowestLarger == NULL)
\r
2922 setCoinsRet.insert(pcoinLowestLarger);
\r
2926 // Solve subset sum by stochastic approximation
\r
2927 sort(vValue.rbegin(), vValue.rend());
\r
2928 vector<char> vfIncluded;
\r
2929 vector<char> vfBest(vValue.size(), true);
\r
2930 int64 nBest = nTotalLower;
\r
2932 for (int nRep = 0; nRep < 1000 && nBest != nTargetValue; nRep++)
\r
2934 vfIncluded.assign(vValue.size(), false);
\r
2936 bool fReachedTarget = false;
\r
2937 for (int nPass = 0; nPass < 2 && !fReachedTarget; nPass++)
\r
2939 for (int i = 0; i < vValue.size(); i++)
\r
2941 if (nPass == 0 ? rand() % 2 : !vfIncluded[i])
\r
2943 nTotal += vValue[i].first;
\r
2944 vfIncluded[i] = true;
\r
2945 if (nTotal >= nTargetValue)
\r
2947 fReachedTarget = true;
\r
2948 if (nTotal < nBest)
\r
2951 vfBest = vfIncluded;
\r
2953 nTotal -= vValue[i].first;
\r
2954 vfIncluded[i] = false;
\r
2961 // If the next larger is still closer, return it
\r
2962 if (pcoinLowestLarger && nLowestLarger - nTargetValue <= nBest - nTargetValue)
\r
2963 setCoinsRet.insert(pcoinLowestLarger);
\r
2966 for (int i = 0; i < vValue.size(); i++)
\r
2968 setCoinsRet.insert(vValue[i].second);
\r
2971 printf("SelectCoins() best subset: ");
\r
2972 for (int i = 0; i < vValue.size(); i++)
\r
2974 printf("%s ", FormatMoney(vValue[i].first).c_str());
\r
2975 printf("total %s\n", FormatMoney(nBest).c_str());
\r
2984 bool CreateTransaction(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, CKey& keyRet, int64& nFeeRequiredRet)
\r
2986 nFeeRequiredRet = 0;
\r
2987 CRITICAL_BLOCK(cs_main)
\r
2989 // txdb must be opened before the mapWallet lock
\r
2991 CRITICAL_BLOCK(cs_mapWallet)
\r
2993 int64 nFee = nTransactionFee;
\r
2996 wtxNew.vin.clear();
\r
2997 wtxNew.vout.clear();
\r
2998 wtxNew.fFromMe = true;
\r
3001 int64 nValueOut = nValue;
\r
3002 int64 nTotalValue = nValue + nFee;
\r
3004 // Choose coins to use
\r
3005 set<CWalletTx*> setCoins;
\r
3006 if (!SelectCoins(nTotalValue, setCoins))
\r
3008 int64 nValueIn = 0;
\r
3009 foreach(CWalletTx* pcoin, setCoins)
\r
3010 nValueIn += pcoin->GetCredit();
\r
3012 // Fill a vout to the payee
\r
3013 bool fChangeFirst = GetRand(2);
\r
3014 if (!fChangeFirst)
\r
3015 wtxNew.vout.push_back(CTxOut(nValueOut, scriptPubKey));
\r
3017 // Fill a vout back to self with any change
\r
3018 if (nValueIn > nTotalValue)
\r
3020 // Note: We use a new key here to keep it from being obvious which side is the change.
\r
3021 // The drawback is that by not reusing a previous key, the change may be lost if a
\r
3022 // backup is restored, if the backup doesn't have the new private key for the change.
\r
3023 // If we reused the old key, it would be possible to add code to look for and
\r
3024 // rediscover unknown transactions that were written with keys of ours to recover
\r
3025 // post-backup change.
\r
3027 // New private key
\r
3028 if (keyRet.IsNull())
\r
3029 keyRet.MakeNewKey();
\r
3031 // Fill a vout to ourself, using same address type as the payment
\r
3032 CScript scriptChange;
\r
3033 if (scriptPubKey.GetBitcoinAddressHash160() != 0)
\r
3034 scriptChange.SetBitcoinAddress(keyRet.GetPubKey());
\r
3036 scriptChange << keyRet.GetPubKey() << OP_CHECKSIG;
\r
3037 wtxNew.vout.push_back(CTxOut(nValueIn - nTotalValue, scriptChange));
\r
3040 // Fill a vout to the payee
\r
3042 wtxNew.vout.push_back(CTxOut(nValueOut, scriptPubKey));
\r
3045 foreach(CWalletTx* pcoin, setCoins)
\r
3046 for (int nOut = 0; nOut < pcoin->vout.size(); nOut++)
\r
3047 if (pcoin->vout[nOut].IsMine())
\r
3048 wtxNew.vin.push_back(CTxIn(pcoin->GetHash(), nOut));
\r
3052 foreach(CWalletTx* pcoin, setCoins)
\r
3053 for (int nOut = 0; nOut < pcoin->vout.size(); nOut++)
\r
3054 if (pcoin->vout[nOut].IsMine())
\r
3055 if (!SignSignature(*pcoin, wtxNew, nIn++))
\r
3058 // Check that enough fee is included
\r
3059 if (nFee < wtxNew.GetMinFee())
\r
3061 nFee = nFeeRequiredRet = wtxNew.GetMinFee();
\r
3065 // Fill vtxPrev by copying from previous transactions vtxPrev
\r
3066 wtxNew.AddSupportingTransactions(txdb);
\r
3067 wtxNew.fTimeReceivedIsTxTime = true;
\r
3076 // Call after CreateTransaction unless you want to abort
\r
3077 bool CommitTransaction(CWalletTx& wtxNew, const CKey& key)
\r
3079 CRITICAL_BLOCK(cs_main)
\r
3081 printf("CommitTransaction:\n%s", wtxNew.ToString().c_str());
\r
3082 CRITICAL_BLOCK(cs_mapWallet)
\r
3084 // This is only to keep the database open to defeat the auto-flush for the
\r
3085 // duration of this scope. This is the only place where this optimization
\r
3086 // maybe makes sense; please don't do it anywhere else.
\r
3087 CWalletDB walletdb("r");
\r
3089 // Add the change's private key to wallet
\r
3090 if (!key.IsNull() && !AddKey(key))
\r
3091 throw runtime_error("CommitTransaction() : AddKey failed\n");
\r
3093 // Add tx to wallet, because if it has change it's also ours,
\r
3094 // otherwise just for transaction history.
\r
3095 AddToWallet(wtxNew);
\r
3097 // Mark old coins as spent
\r
3098 set<CWalletTx*> setCoins;
\r
3099 foreach(const CTxIn& txin, wtxNew.vin)
\r
3100 setCoins.insert(&mapWallet[txin.prevout.hash]);
\r
3101 foreach(CWalletTx* pcoin, setCoins)
\r
3103 pcoin->fSpent = true;
\r
3104 pcoin->WriteToDisk();
\r
3105 vWalletUpdated.push_back(pcoin->GetHash());
\r
3109 // Track how many getdata requests our transaction gets
\r
3110 CRITICAL_BLOCK(cs_mapRequestCount)
\r
3111 mapRequestCount[wtxNew.GetHash()] = 0;
\r
3114 if (!wtxNew.AcceptTransaction())
\r
3116 // This must not fail. The transaction has already been signed and recorded.
\r
3117 printf("CommitTransaction() : Error: Transaction not valid");
\r
3120 wtxNew.RelayWalletTransaction();
\r
3122 MainFrameRepaint();
\r
3129 string SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, bool fAskFee)
\r
3131 CRITICAL_BLOCK(cs_main)
\r
3134 int64 nFeeRequired;
\r
3135 if (!CreateTransaction(scriptPubKey, nValue, wtxNew, key, nFeeRequired))
\r
3138 if (nValue + nFeeRequired > GetBalance())
\r
3139 strError = strprintf(_("Error: This is an oversized transaction that requires a transaction fee of %s "), FormatMoney(nFeeRequired).c_str());
\r
3141 strError = _("Error: Transaction creation failed ");
\r
3142 printf("SendMoney() : %s", strError.c_str());
\r
3146 if (fAskFee && !ThreadSafeAskFee(nFeeRequired, _("Sending..."), NULL))
\r
3149 if (!CommitTransaction(wtxNew, key))
\r
3150 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
3152 MainFrameRepaint();
\r
3158 string SendMoneyToBitcoinAddress(string strAddress, int64 nValue, CWalletTx& wtxNew, bool fAskFee)
\r
3162 return _("Invalid amount");
\r
3163 if (nValue + nTransactionFee > GetBalance())
\r
3164 return _("Insufficient funds");
\r
3166 // Parse bitcoin address
\r
3167 CScript scriptPubKey;
\r
3168 if (!scriptPubKey.SetBitcoinAddress(strAddress))
\r
3169 return _("Invalid bitcoin address");
\r
3171 return SendMoney(scriptPubKey, nValue, wtxNew, fAskFee);
\r