You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.cpp 148KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2013 The Bitcoin developers
  3. // Distributed under the MIT/X11 software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #include "main.h"
  6. #include "addrman.h"
  7. #include "alert.h"
  8. #include "chainparams.h"
  9. #include "checkpoints.h"
  10. #include "checkqueue.h"
  11. #include "init.h"
  12. #include "net.h"
  13. #include "txdb.h"
  14. #include "txmempool.h"
  15. #include "ui_interface.h"
  16. #include "util.h"
  17. #include <inttypes.h>
  18. #include <stdint.h>
  19. #include <boost/algorithm/string/replace.hpp>
  20. #include <boost/filesystem.hpp>
  21. #include <boost/filesystem/fstream.hpp>
  22. using namespace std;
  23. using namespace boost;
  24. //
  25. // Global state
  26. //
  27. CCriticalSection cs_main;
  28. CTxMemPool mempool;
  29. map<uint256, CBlockIndex*> mapBlockIndex;
  30. CChain chainActive;
  31. int64_t nTimeBestReceived = 0;
  32. int nScriptCheckThreads = 0;
  33. bool fImporting = false;
  34. bool fReindex = false;
  35. bool fBenchmark = false;
  36. bool fTxIndex = false;
  37. unsigned int nCoinCacheSize = 5000;
  38. /** Fees smaller than this (in satoshi) are considered zero fee (for transaction creation) */
  39. int64_t CTransaction::nMinTxFee = 10000; // Override with -mintxfee
  40. /** Fees smaller than this (in satoshi) are considered zero fee (for relaying) */
  41. int64_t CTransaction::nMinRelayTxFee = 10000;
  42. static CMedianFilter<int> cPeerBlockCounts(8, 0); // Amount of blocks that other nodes claim to have
  43. map<uint256, CBlock*> mapOrphanBlocks;
  44. multimap<uint256, CBlock*> mapOrphanBlocksByPrev;
  45. map<uint256, CTransaction> mapOrphanTransactions;
  46. map<uint256, set<uint256> > mapOrphanTransactionsByPrev;
  47. // Constant stuff for coinbase transactions we create:
  48. CScript COINBASE_FLAGS;
  49. const string strMessageMagic = "Bitcoin Signed Message:\n";
  50. // Settings
  51. int64_t nTransactionFee = 0;
  52. // Internal stuff
  53. namespace {
  54. struct CBlockIndexWorkComparator
  55. {
  56. bool operator()(CBlockIndex *pa, CBlockIndex *pb) {
  57. if (pa->nChainWork > pb->nChainWork) return false;
  58. if (pa->nChainWork < pb->nChainWork) return true;
  59. if (pa->GetBlockHash() < pb->GetBlockHash()) return false;
  60. if (pa->GetBlockHash() > pb->GetBlockHash()) return true;
  61. return false; // identical blocks
  62. }
  63. };
  64. CBlockIndex *pindexBestInvalid;
  65. set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexValid; // may contain all CBlockIndex*'s that have validness >=BLOCK_VALID_TRANSACTIONS, and must contain those who aren't failed
  66. CCriticalSection cs_LastBlockFile;
  67. CBlockFileInfo infoLastBlockFile;
  68. int nLastBlockFile = 0;
  69. }
  70. //////////////////////////////////////////////////////////////////////////////
  71. //
  72. // dispatching functions
  73. //
  74. // These functions dispatch to one or all registered wallets
  75. namespace {
  76. struct CMainSignals {
  77. // Notifies listeners of updated transaction data (passing hash, transaction, and optionally the block it is found in.
  78. boost::signals2::signal<void (const uint256 &, const CTransaction &, const CBlock *)> SyncTransaction;
  79. // Notifies listeners of an erased transaction (currently disabled, requires transaction replacement).
  80. boost::signals2::signal<void (const uint256 &)> EraseTransaction;
  81. // Notifies listeners of an updated transaction without new data (for now: a coinbase potentially becoming visible).
  82. boost::signals2::signal<void (const uint256 &)> UpdatedTransaction;
  83. // Notifies listeners of a new active block chain.
  84. boost::signals2::signal<void (const CBlockLocator &)> SetBestChain;
  85. // Notifies listeners about an inventory item being seen on the network.
  86. boost::signals2::signal<void (const uint256 &)> Inventory;
  87. // Tells listeners to broadcast their data.
  88. boost::signals2::signal<void ()> Broadcast;
  89. } g_signals;
  90. }
  91. void RegisterWallet(CWalletInterface* pwalletIn) {
  92. g_signals.SyncTransaction.connect(boost::bind(&CWalletInterface::SyncTransaction, pwalletIn, _1, _2, _3));
  93. g_signals.EraseTransaction.connect(boost::bind(&CWalletInterface::EraseFromWallet, pwalletIn, _1));
  94. g_signals.UpdatedTransaction.connect(boost::bind(&CWalletInterface::UpdatedTransaction, pwalletIn, _1));
  95. g_signals.SetBestChain.connect(boost::bind(&CWalletInterface::SetBestChain, pwalletIn, _1));
  96. g_signals.Inventory.connect(boost::bind(&CWalletInterface::Inventory, pwalletIn, _1));
  97. g_signals.Broadcast.connect(boost::bind(&CWalletInterface::ResendWalletTransactions, pwalletIn));
  98. }
  99. void UnregisterWallet(CWalletInterface* pwalletIn) {
  100. g_signals.Broadcast.disconnect(boost::bind(&CWalletInterface::ResendWalletTransactions, pwalletIn));
  101. g_signals.Inventory.disconnect(boost::bind(&CWalletInterface::Inventory, pwalletIn, _1));
  102. g_signals.SetBestChain.disconnect(boost::bind(&CWalletInterface::SetBestChain, pwalletIn, _1));
  103. g_signals.UpdatedTransaction.disconnect(boost::bind(&CWalletInterface::UpdatedTransaction, pwalletIn, _1));
  104. g_signals.EraseTransaction.disconnect(boost::bind(&CWalletInterface::EraseFromWallet, pwalletIn, _1));
  105. g_signals.SyncTransaction.disconnect(boost::bind(&CWalletInterface::SyncTransaction, pwalletIn, _1, _2, _3));
  106. }
  107. void UnregisterAllWallets() {
  108. g_signals.Broadcast.disconnect_all_slots();
  109. g_signals.Inventory.disconnect_all_slots();
  110. g_signals.SetBestChain.disconnect_all_slots();
  111. g_signals.UpdatedTransaction.disconnect_all_slots();
  112. g_signals.EraseTransaction.disconnect_all_slots();
  113. g_signals.SyncTransaction.disconnect_all_slots();
  114. }
  115. void SyncWithWallets(const uint256 &hash, const CTransaction &tx, const CBlock *pblock) {
  116. g_signals.SyncTransaction(hash, tx, pblock);
  117. }
  118. //////////////////////////////////////////////////////////////////////////////
  119. //
  120. // Registration of network node signals.
  121. //
  122. int static GetHeight()
  123. {
  124. LOCK(cs_main);
  125. return chainActive.Height();
  126. }
  127. void RegisterNodeSignals(CNodeSignals& nodeSignals)
  128. {
  129. nodeSignals.GetHeight.connect(&GetHeight);
  130. nodeSignals.ProcessMessages.connect(&ProcessMessages);
  131. nodeSignals.SendMessages.connect(&SendMessages);
  132. }
  133. void UnregisterNodeSignals(CNodeSignals& nodeSignals)
  134. {
  135. nodeSignals.GetHeight.disconnect(&GetHeight);
  136. nodeSignals.ProcessMessages.disconnect(&ProcessMessages);
  137. nodeSignals.SendMessages.disconnect(&SendMessages);
  138. }
  139. //////////////////////////////////////////////////////////////////////////////
  140. //
  141. // CChain implementation
  142. //
  143. CBlockIndex *CChain::SetTip(CBlockIndex *pindex) {
  144. if (pindex == NULL) {
  145. vChain.clear();
  146. return NULL;
  147. }
  148. vChain.resize(pindex->nHeight + 1);
  149. while (pindex && vChain[pindex->nHeight] != pindex) {
  150. vChain[pindex->nHeight] = pindex;
  151. pindex = pindex->pprev;
  152. }
  153. return pindex;
  154. }
  155. CBlockLocator CChain::GetLocator(const CBlockIndex *pindex) const {
  156. int nStep = 1;
  157. std::vector<uint256> vHave;
  158. vHave.reserve(32);
  159. if (!pindex)
  160. pindex = Tip();
  161. while (pindex) {
  162. vHave.push_back(pindex->GetBlockHash());
  163. // Stop when we have added the genesis block.
  164. if (pindex->nHeight == 0)
  165. break;
  166. // Exponentially larger steps back, plus the genesis block.
  167. int nHeight = std::max(pindex->nHeight - nStep, 0);
  168. // In case pindex is not in this chain, iterate pindex->pprev to find blocks.
  169. while (pindex->nHeight > nHeight && !Contains(pindex))
  170. pindex = pindex->pprev;
  171. // If pindex is in this chain, use direct height-based access.
  172. if (pindex->nHeight > nHeight)
  173. pindex = (*this)[nHeight];
  174. if (vHave.size() > 10)
  175. nStep *= 2;
  176. }
  177. return CBlockLocator(vHave);
  178. }
  179. CBlockIndex *CChain::FindFork(const CBlockLocator &locator) const {
  180. // Find the first block the caller has in the main chain
  181. BOOST_FOREACH(const uint256& hash, locator.vHave) {
  182. std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
  183. if (mi != mapBlockIndex.end())
  184. {
  185. CBlockIndex* pindex = (*mi).second;
  186. if (Contains(pindex))
  187. return pindex;
  188. }
  189. }
  190. return Genesis();
  191. }
  192. //////////////////////////////////////////////////////////////////////////////
  193. //
  194. // CCoinsView implementations
  195. //
  196. bool CCoinsView::GetCoins(const uint256 &txid, CCoins &coins) { return false; }
  197. bool CCoinsView::SetCoins(const uint256 &txid, const CCoins &coins) { return false; }
  198. bool CCoinsView::HaveCoins(const uint256 &txid) { return false; }
  199. CBlockIndex *CCoinsView::GetBestBlock() { return NULL; }
  200. bool CCoinsView::SetBestBlock(CBlockIndex *pindex) { return false; }
  201. bool CCoinsView::BatchWrite(const std::map<uint256, CCoins> &mapCoins, CBlockIndex *pindex) { return false; }
  202. bool CCoinsView::GetStats(CCoinsStats &stats) { return false; }
  203. CCoinsViewBacked::CCoinsViewBacked(CCoinsView &viewIn) : base(&viewIn) { }
  204. bool CCoinsViewBacked::GetCoins(const uint256 &txid, CCoins &coins) { return base->GetCoins(txid, coins); }
  205. bool CCoinsViewBacked::SetCoins(const uint256 &txid, const CCoins &coins) { return base->SetCoins(txid, coins); }
  206. bool CCoinsViewBacked::HaveCoins(const uint256 &txid) { return base->HaveCoins(txid); }
  207. CBlockIndex *CCoinsViewBacked::GetBestBlock() { return base->GetBestBlock(); }
  208. bool CCoinsViewBacked::SetBestBlock(CBlockIndex *pindex) { return base->SetBestBlock(pindex); }
  209. void CCoinsViewBacked::SetBackend(CCoinsView &viewIn) { base = &viewIn; }
  210. bool CCoinsViewBacked::BatchWrite(const std::map<uint256, CCoins> &mapCoins, CBlockIndex *pindex) { return base->BatchWrite(mapCoins, pindex); }
  211. bool CCoinsViewBacked::GetStats(CCoinsStats &stats) { return base->GetStats(stats); }
  212. CCoinsViewCache::CCoinsViewCache(CCoinsView &baseIn, bool fDummy) : CCoinsViewBacked(baseIn), pindexTip(NULL) { }
  213. bool CCoinsViewCache::GetCoins(const uint256 &txid, CCoins &coins) {
  214. if (cacheCoins.count(txid)) {
  215. coins = cacheCoins[txid];
  216. return true;
  217. }
  218. if (base->GetCoins(txid, coins)) {
  219. cacheCoins[txid] = coins;
  220. return true;
  221. }
  222. return false;
  223. }
  224. std::map<uint256,CCoins>::iterator CCoinsViewCache::FetchCoins(const uint256 &txid) {
  225. std::map<uint256,CCoins>::iterator it = cacheCoins.lower_bound(txid);
  226. if (it != cacheCoins.end() && it->first == txid)
  227. return it;
  228. CCoins tmp;
  229. if (!base->GetCoins(txid,tmp))
  230. return cacheCoins.end();
  231. std::map<uint256,CCoins>::iterator ret = cacheCoins.insert(it, std::make_pair(txid, CCoins()));
  232. tmp.swap(ret->second);
  233. return ret;
  234. }
  235. CCoins &CCoinsViewCache::GetCoins(const uint256 &txid) {
  236. std::map<uint256,CCoins>::iterator it = FetchCoins(txid);
  237. assert(it != cacheCoins.end());
  238. return it->second;
  239. }
  240. bool CCoinsViewCache::SetCoins(const uint256 &txid, const CCoins &coins) {
  241. cacheCoins[txid] = coins;
  242. return true;
  243. }
  244. bool CCoinsViewCache::HaveCoins(const uint256 &txid) {
  245. return FetchCoins(txid) != cacheCoins.end();
  246. }
  247. CBlockIndex *CCoinsViewCache::GetBestBlock() {
  248. if (pindexTip == NULL)
  249. pindexTip = base->GetBestBlock();
  250. return pindexTip;
  251. }
  252. bool CCoinsViewCache::SetBestBlock(CBlockIndex *pindex) {
  253. pindexTip = pindex;
  254. return true;
  255. }
  256. bool CCoinsViewCache::BatchWrite(const std::map<uint256, CCoins> &mapCoins, CBlockIndex *pindex) {
  257. for (std::map<uint256, CCoins>::const_iterator it = mapCoins.begin(); it != mapCoins.end(); it++)
  258. cacheCoins[it->first] = it->second;
  259. pindexTip = pindex;
  260. return true;
  261. }
  262. bool CCoinsViewCache::Flush() {
  263. bool fOk = base->BatchWrite(cacheCoins, pindexTip);
  264. if (fOk)
  265. cacheCoins.clear();
  266. return fOk;
  267. }
  268. unsigned int CCoinsViewCache::GetCacheSize() {
  269. return cacheCoins.size();
  270. }
  271. /** Helper; lookup from tip (used calling mempool.check()
  272. NOTE: code calling this MUST hold the cs_main lock so
  273. another thread doesn't modify pcoinsTip. When we switch
  274. to C++11 this should be replaced by lambda expressions...
  275. **/
  276. static CCoins &LookupFromTip(const uint256& hash) {
  277. return pcoinsTip->GetCoins(hash);
  278. }
  279. /** CCoinsView that brings transactions from a memorypool into view.
  280. It does not check for spendings by memory pool transactions. */
  281. CCoinsViewMemPool::CCoinsViewMemPool(CCoinsView &baseIn, CTxMemPool &mempoolIn) : CCoinsViewBacked(baseIn), mempool(mempoolIn) { }
  282. bool CCoinsViewMemPool::GetCoins(const uint256 &txid, CCoins &coins) {
  283. if (base->GetCoins(txid, coins))
  284. return true;
  285. CTransaction tx;
  286. if (mempool.lookup(txid, tx)) {
  287. coins = CCoins(tx, MEMPOOL_HEIGHT);
  288. return true;
  289. }
  290. return false;
  291. }
  292. bool CCoinsViewMemPool::HaveCoins(const uint256 &txid) {
  293. return mempool.exists(txid) || base->HaveCoins(txid);
  294. }
  295. CCoinsViewCache *pcoinsTip = NULL;
  296. CBlockTreeDB *pblocktree = NULL;
  297. //////////////////////////////////////////////////////////////////////////////
  298. //
  299. // mapOrphanTransactions
  300. //
  301. bool AddOrphanTx(const CTransaction& tx)
  302. {
  303. uint256 hash = tx.GetHash();
  304. if (mapOrphanTransactions.count(hash))
  305. return false;
  306. // Ignore big transactions, to avoid a
  307. // send-big-orphans memory exhaustion attack. If a peer has a legitimate
  308. // large transaction with a missing parent then we assume
  309. // it will rebroadcast it later, after the parent transaction(s)
  310. // have been mined or received.
  311. // 10,000 orphans, each of which is at most 5,000 bytes big is
  312. // at most 500 megabytes of orphans:
  313. unsigned int sz = tx.GetSerializeSize(SER_NETWORK, CTransaction::CURRENT_VERSION);
  314. if (sz > 5000)
  315. {
  316. LogPrint("mempool", "ignoring large orphan tx (size: %u, hash: %s)\n", sz, hash.ToString().c_str());
  317. return false;
  318. }
  319. mapOrphanTransactions[hash] = tx;
  320. BOOST_FOREACH(const CTxIn& txin, tx.vin)
  321. mapOrphanTransactionsByPrev[txin.prevout.hash].insert(hash);
  322. LogPrint("mempool", "stored orphan tx %s (mapsz %"PRIszu")\n", hash.ToString().c_str(),
  323. mapOrphanTransactions.size());
  324. return true;
  325. }
  326. void static EraseOrphanTx(uint256 hash)
  327. {
  328. if (!mapOrphanTransactions.count(hash))
  329. return;
  330. const CTransaction& tx = mapOrphanTransactions[hash];
  331. BOOST_FOREACH(const CTxIn& txin, tx.vin)
  332. {
  333. mapOrphanTransactionsByPrev[txin.prevout.hash].erase(hash);
  334. if (mapOrphanTransactionsByPrev[txin.prevout.hash].empty())
  335. mapOrphanTransactionsByPrev.erase(txin.prevout.hash);
  336. }
  337. mapOrphanTransactions.erase(hash);
  338. }
  339. unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans)
  340. {
  341. unsigned int nEvicted = 0;
  342. while (mapOrphanTransactions.size() > nMaxOrphans)
  343. {
  344. // Evict a random orphan:
  345. uint256 randomhash = GetRandHash();
  346. map<uint256, CTransaction>::iterator it = mapOrphanTransactions.lower_bound(randomhash);
  347. if (it == mapOrphanTransactions.end())
  348. it = mapOrphanTransactions.begin();
  349. EraseOrphanTx(it->first);
  350. ++nEvicted;
  351. }
  352. return nEvicted;
  353. }
  354. bool IsStandardTx(const CTransaction& tx, string& reason)
  355. {
  356. if (tx.nVersion > CTransaction::CURRENT_VERSION || tx.nVersion < 1) {
  357. reason = "version";
  358. return false;
  359. }
  360. if (!IsFinalTx(tx)) {
  361. reason = "non-final";
  362. return false;
  363. }
  364. // Extremely large transactions with lots of inputs can cost the network
  365. // almost as much to process as they cost the sender in fees, because
  366. // computing signature hashes is O(ninputs*txsize). Limiting transactions
  367. // to MAX_STANDARD_TX_SIZE mitigates CPU exhaustion attacks.
  368. unsigned int sz = tx.GetSerializeSize(SER_NETWORK, CTransaction::CURRENT_VERSION);
  369. if (sz >= MAX_STANDARD_TX_SIZE) {
  370. reason = "tx-size";
  371. return false;
  372. }
  373. BOOST_FOREACH(const CTxIn& txin, tx.vin)
  374. {
  375. // Biggest 'standard' txin is a 3-signature 3-of-3 CHECKMULTISIG
  376. // pay-to-script-hash, which is 3 ~80-byte signatures, 3
  377. // ~65-byte public keys, plus a few script ops.
  378. if (txin.scriptSig.size() > 500) {
  379. reason = "scriptsig-size";
  380. return false;
  381. }
  382. if (!txin.scriptSig.IsPushOnly()) {
  383. reason = "scriptsig-not-pushonly";
  384. return false;
  385. }
  386. }
  387. unsigned int nDataOut = 0;
  388. txnouttype whichType;
  389. BOOST_FOREACH(const CTxOut& txout, tx.vout) {
  390. if (!::IsStandard(txout.scriptPubKey, whichType)) {
  391. reason = "scriptpubkey";
  392. return false;
  393. }
  394. if (whichType == TX_NULL_DATA)
  395. nDataOut++;
  396. else if (txout.IsDust(CTransaction::nMinRelayTxFee)) {
  397. reason = "dust";
  398. return false;
  399. }
  400. }
  401. // only one OP_RETURN txout is permitted
  402. if (nDataOut > 1) {
  403. reason = "mucho-data";
  404. return false;
  405. }
  406. return true;
  407. }
  408. bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
  409. {
  410. // Time based nLockTime implemented in 0.1.6
  411. if (tx.nLockTime == 0)
  412. return true;
  413. if (nBlockHeight == 0)
  414. nBlockHeight = chainActive.Height();
  415. if (nBlockTime == 0)
  416. nBlockTime = GetAdjustedTime();
  417. if ((int64_t)tx.nLockTime < ((int64_t)tx.nLockTime < LOCKTIME_THRESHOLD ? (int64_t)nBlockHeight : nBlockTime))
  418. return true;
  419. BOOST_FOREACH(const CTxIn& txin, tx.vin)
  420. if (!txin.IsFinal())
  421. return false;
  422. return true;
  423. }
  424. /** Amount of bitcoins spent by the transaction.
  425. @return sum of all outputs (note: does not include fees)
  426. */
  427. int64_t GetValueOut(const CTransaction& tx)
  428. {
  429. int64_t nValueOut = 0;
  430. BOOST_FOREACH(const CTxOut& txout, tx.vout)
  431. {
  432. nValueOut += txout.nValue;
  433. if (!MoneyRange(txout.nValue) || !MoneyRange(nValueOut))
  434. throw std::runtime_error("GetValueOut() : value out of range");
  435. }
  436. return nValueOut;
  437. }
  438. //
  439. // Check transaction inputs, and make sure any
  440. // pay-to-script-hash transactions are evaluating IsStandard scripts
  441. //
  442. // Why bother? To avoid denial-of-service attacks; an attacker
  443. // can submit a standard HASH... OP_EQUAL transaction,
  444. // which will get accepted into blocks. The redemption
  445. // script can be anything; an attacker could use a very
  446. // expensive-to-check-upon-redemption script like:
  447. // DUP CHECKSIG DROP ... repeated 100 times... OP_1
  448. //
  449. bool AreInputsStandard(const CTransaction& tx, CCoinsViewCache& mapInputs)
  450. {
  451. if (tx.IsCoinBase())
  452. return true; // Coinbases don't use vin normally
  453. for (unsigned int i = 0; i < tx.vin.size(); i++)
  454. {
  455. const CTxOut& prev = mapInputs.GetOutputFor(tx.vin[i]);
  456. vector<vector<unsigned char> > vSolutions;
  457. txnouttype whichType;
  458. // get the scriptPubKey corresponding to this input:
  459. const CScript& prevScript = prev.scriptPubKey;
  460. if (!Solver(prevScript, whichType, vSolutions))
  461. return false;
  462. int nArgsExpected = ScriptSigArgsExpected(whichType, vSolutions);
  463. if (nArgsExpected < 0)
  464. return false;
  465. // Transactions with extra stuff in their scriptSigs are
  466. // non-standard. Note that this EvalScript() call will
  467. // be quick, because if there are any operations
  468. // beside "push data" in the scriptSig the
  469. // IsStandard() call returns false
  470. vector<vector<unsigned char> > stack;
  471. if (!EvalScript(stack, tx.vin[i].scriptSig, tx, i, false, 0))
  472. return false;
  473. if (whichType == TX_SCRIPTHASH)
  474. {
  475. if (stack.empty())
  476. return false;
  477. CScript subscript(stack.back().begin(), stack.back().end());
  478. vector<vector<unsigned char> > vSolutions2;
  479. txnouttype whichType2;
  480. if (!Solver(subscript, whichType2, vSolutions2))
  481. return false;
  482. if (whichType2 == TX_SCRIPTHASH)
  483. return false;
  484. int tmpExpected;
  485. tmpExpected = ScriptSigArgsExpected(whichType2, vSolutions2);
  486. if (tmpExpected < 0)
  487. return false;
  488. nArgsExpected += tmpExpected;
  489. }
  490. if (stack.size() != (unsigned int)nArgsExpected)
  491. return false;
  492. }
  493. return true;
  494. }
  495. unsigned int GetLegacySigOpCount(const CTransaction& tx)
  496. {
  497. unsigned int nSigOps = 0;
  498. BOOST_FOREACH(const CTxIn& txin, tx.vin)
  499. {
  500. nSigOps += txin.scriptSig.GetSigOpCount(false);
  501. }
  502. BOOST_FOREACH(const CTxOut& txout, tx.vout)
  503. {
  504. nSigOps += txout.scriptPubKey.GetSigOpCount(false);
  505. }
  506. return nSigOps;
  507. }
  508. unsigned int GetP2SHSigOpCount(const CTransaction& tx, CCoinsViewCache& inputs)
  509. {
  510. if (tx.IsCoinBase())
  511. return 0;
  512. unsigned int nSigOps = 0;
  513. for (unsigned int i = 0; i < tx.vin.size(); i++)
  514. {
  515. const CTxOut &prevout = inputs.GetOutputFor(tx.vin[i]);
  516. if (prevout.scriptPubKey.IsPayToScriptHash())
  517. nSigOps += prevout.scriptPubKey.GetSigOpCount(tx.vin[i].scriptSig);
  518. }
  519. return nSigOps;
  520. }
  521. int CMerkleTx::SetMerkleBranch(const CBlock* pblock)
  522. {
  523. CBlock blockTmp;
  524. if (pblock == NULL) {
  525. CCoins coins;
  526. if (pcoinsTip->GetCoins(GetHash(), coins)) {
  527. CBlockIndex *pindex = chainActive[coins.nHeight];
  528. if (pindex) {
  529. if (!ReadBlockFromDisk(blockTmp, pindex))
  530. return 0;
  531. pblock = &blockTmp;
  532. }
  533. }
  534. }
  535. if (pblock) {
  536. // Update the tx's hashBlock
  537. hashBlock = pblock->GetHash();
  538. // Locate the transaction
  539. for (nIndex = 0; nIndex < (int)pblock->vtx.size(); nIndex++)
  540. if (pblock->vtx[nIndex] == *(CTransaction*)this)
  541. break;
  542. if (nIndex == (int)pblock->vtx.size())
  543. {
  544. vMerkleBranch.clear();
  545. nIndex = -1;
  546. LogPrintf("ERROR: SetMerkleBranch() : couldn't find tx in block\n");
  547. return 0;
  548. }
  549. // Fill in merkle branch
  550. vMerkleBranch = pblock->GetMerkleBranch(nIndex);
  551. }
  552. // Is the tx in a block that's in the main chain
  553. map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
  554. if (mi == mapBlockIndex.end())
  555. return 0;
  556. CBlockIndex* pindex = (*mi).second;
  557. if (!pindex || !chainActive.Contains(pindex))
  558. return 0;
  559. return chainActive.Height() - pindex->nHeight + 1;
  560. }
  561. bool CheckTransaction(const CTransaction& tx, CValidationState &state)
  562. {
  563. // Basic checks that don't depend on any context
  564. if (tx.vin.empty())
  565. return state.DoS(10, error("CheckTransaction() : vin empty"));
  566. if (tx.vout.empty())
  567. return state.DoS(10, error("CheckTransaction() : vout empty"));
  568. // Size limits
  569. if (::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) > MAX_BLOCK_SIZE)
  570. return state.DoS(100, error("CTransaction::CheckTransaction() : size limits failed"));
  571. // Check for negative or overflow output values
  572. int64_t nValueOut = 0;
  573. BOOST_FOREACH(const CTxOut& txout, tx.vout)
  574. {
  575. if (txout.nValue < 0)
  576. return state.DoS(100, error("CheckTransaction() : txout.nValue negative"));
  577. if (txout.nValue > MAX_MONEY)
  578. return state.DoS(100, error("CheckTransaction() : txout.nValue too high"));
  579. nValueOut += txout.nValue;
  580. if (!MoneyRange(nValueOut))
  581. return state.DoS(100, error("CTransaction::CheckTransaction() : txout total out of range"));
  582. }
  583. // Check for duplicate inputs
  584. set<COutPoint> vInOutPoints;
  585. BOOST_FOREACH(const CTxIn& txin, tx.vin)
  586. {
  587. if (vInOutPoints.count(txin.prevout))
  588. return state.DoS(100, error("CTransaction::CheckTransaction() : duplicate inputs"));
  589. vInOutPoints.insert(txin.prevout);
  590. }
  591. if (tx.IsCoinBase())
  592. {
  593. if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100)
  594. return state.DoS(100, error("CheckTransaction() : coinbase script size"));
  595. }
  596. else
  597. {
  598. BOOST_FOREACH(const CTxIn& txin, tx.vin)
  599. if (txin.prevout.IsNull())
  600. return state.DoS(10, error("CheckTransaction() : prevout is null"));
  601. }
  602. return true;
  603. }
  604. int64_t GetMinFee(const CTransaction& tx, bool fAllowFree, enum GetMinFee_mode mode)
  605. {
  606. // Base fee is either nMinTxFee or nMinRelayTxFee
  607. int64_t nBaseFee = (mode == GMF_RELAY) ? tx.nMinRelayTxFee : tx.nMinTxFee;
  608. unsigned int nBytes = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
  609. int64_t nMinFee = (1 + (int64_t)nBytes / 1000) * nBaseFee;
  610. if (fAllowFree)
  611. {
  612. // There is a free transaction area in blocks created by most miners,
  613. // * If we are relaying we allow transactions up to DEFAULT_BLOCK_PRIORITY_SIZE - 1000
  614. // to be considered to fall into this category. We don't want to encourage sending
  615. // multiple transactions instead of one big transaction to avoid fees.
  616. // * If we are creating a transaction we allow transactions up to 1,000 bytes
  617. // to be considered safe and assume they can likely make it into this section.
  618. if (nBytes < (mode == GMF_SEND ? 1000 : (DEFAULT_BLOCK_PRIORITY_SIZE - 1000)))
  619. nMinFee = 0;
  620. }
  621. // This code can be removed after enough miners have upgraded to version 0.9.
  622. // Until then, be safe when sending and require a fee if any output
  623. // is less than CENT:
  624. if (nMinFee < nBaseFee && mode == GMF_SEND)
  625. {
  626. BOOST_FOREACH(const CTxOut& txout, tx.vout)
  627. if (txout.nValue < CENT)
  628. nMinFee = nBaseFee;
  629. }
  630. if (!MoneyRange(nMinFee))
  631. nMinFee = MAX_MONEY;
  632. return nMinFee;
  633. }
  634. bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransaction &tx, bool fLimitFree,
  635. bool* pfMissingInputs, bool fRejectInsaneFee)
  636. {
  637. if (pfMissingInputs)
  638. *pfMissingInputs = false;
  639. if (!CheckTransaction(tx, state))
  640. return error("AcceptToMemoryPool: : CheckTransaction failed");
  641. // Coinbase is only valid in a block, not as a loose transaction
  642. if (tx.IsCoinBase())
  643. return state.DoS(100, error("AcceptToMemoryPool: : coinbase as individual tx"));
  644. // Rather not work on nonstandard transactions (unless -testnet/-regtest)
  645. string reason;
  646. if (Params().NetworkID() == CChainParams::MAIN && !IsStandardTx(tx, reason))
  647. return error("AcceptToMemoryPool: : nonstandard transaction: %s",
  648. reason.c_str());
  649. // is it already in the memory pool?
  650. uint256 hash = tx.GetHash();
  651. if (pool.exists(hash))
  652. return false;
  653. // Check for conflicts with in-memory transactions
  654. CTransaction* ptxOld = NULL;
  655. {
  656. LOCK(pool.cs); // protect pool.mapNextTx
  657. for (unsigned int i = 0; i < tx.vin.size(); i++)
  658. {
  659. COutPoint outpoint = tx.vin[i].prevout;
  660. if (pool.mapNextTx.count(outpoint))
  661. {
  662. // Disable replacement feature for now
  663. return false;
  664. // Allow replacing with a newer version of the same transaction
  665. if (i != 0)
  666. return false;
  667. ptxOld = pool.mapNextTx[outpoint].ptx;
  668. if (IsFinalTx(*ptxOld))
  669. return false;
  670. if (!tx.IsNewerThan(*ptxOld))
  671. return false;
  672. for (unsigned int i = 0; i < tx.vin.size(); i++)
  673. {
  674. COutPoint outpoint = tx.vin[i].prevout;
  675. if (!pool.mapNextTx.count(outpoint) || pool.mapNextTx[outpoint].ptx != ptxOld)
  676. return false;
  677. }
  678. break;
  679. }
  680. }
  681. }
  682. {
  683. CCoinsView dummy;
  684. CCoinsViewCache view(dummy);
  685. {
  686. LOCK(pool.cs);
  687. CCoinsViewMemPool viewMemPool(*pcoinsTip, pool);
  688. view.SetBackend(viewMemPool);
  689. // do we already have it?
  690. if (view.HaveCoins(hash))
  691. return false;
  692. // do all inputs exist?
  693. // Note that this does not check for the presence of actual outputs (see the next check for that),
  694. // only helps filling in pfMissingInputs (to determine missing vs spent).
  695. BOOST_FOREACH(const CTxIn txin, tx.vin) {
  696. if (!view.HaveCoins(txin.prevout.hash)) {
  697. if (pfMissingInputs)
  698. *pfMissingInputs = true;
  699. return false;
  700. }
  701. }
  702. // are the actual inputs available?
  703. if (!view.HaveInputs(tx))
  704. return state.Invalid(error("AcceptToMemoryPool: : inputs already spent"));
  705. // Bring the best block into scope
  706. view.GetBestBlock();
  707. // we have all inputs cached now, so switch back to dummy, so we don't need to keep lock on mempool
  708. view.SetBackend(dummy);
  709. }
  710. // Check for non-standard pay-to-script-hash in inputs
  711. if (Params().NetworkID() == CChainParams::MAIN && !AreInputsStandard(tx, view))
  712. return error("AcceptToMemoryPool: : nonstandard transaction input");
  713. // Note: if you modify this code to accept non-standard transactions, then
  714. // you should add code here to check that the transaction does a
  715. // reasonable number of ECDSA signature verifications.
  716. int64_t nFees = view.GetValueIn(tx)-GetValueOut(tx);
  717. unsigned int nSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
  718. // Don't accept it if it can't get into a block
  719. int64_t txMinFee = GetMinFee(tx, true, GMF_RELAY);
  720. if (fLimitFree && nFees < txMinFee)
  721. return error("AcceptToMemoryPool: : not enough fees %s, %"PRId64" < %"PRId64,
  722. hash.ToString().c_str(),
  723. nFees, txMinFee);
  724. // Continuously rate-limit free transactions
  725. // This mitigates 'penny-flooding' -- sending thousands of free transactions just to
  726. // be annoying or make others' transactions take longer to confirm.
  727. if (fLimitFree && nFees < CTransaction::nMinRelayTxFee)
  728. {
  729. static CCriticalSection csFreeLimiter;
  730. static double dFreeCount;
  731. static int64_t nLastTime;
  732. int64_t nNow = GetTime();
  733. LOCK(csFreeLimiter);
  734. // Use an exponentially decaying ~10-minute window:
  735. dFreeCount *= pow(1.0 - 1.0/600.0, (double)(nNow - nLastTime));
  736. nLastTime = nNow;
  737. // -limitfreerelay unit is thousand-bytes-per-minute
  738. // At default rate it would take over a month to fill 1GB
  739. if (dFreeCount >= GetArg("-limitfreerelay", 15)*10*1000)
  740. return error("AcceptToMemoryPool: : free transaction rejected by rate limiter");
  741. LogPrint("mempool", "Rate limit dFreeCount: %g => %g\n", dFreeCount, dFreeCount+nSize);
  742. dFreeCount += nSize;
  743. }
  744. if (fRejectInsaneFee && nFees > CTransaction::nMinRelayTxFee * 10000)
  745. return error("AcceptToMemoryPool: : insane fees %s, %"PRId64" > %"PRId64,
  746. hash.ToString().c_str(),
  747. nFees, CTransaction::nMinRelayTxFee * 10000);
  748. // Check against previous transactions
  749. // This is done last to help prevent CPU exhaustion denial-of-service attacks.
  750. if (!CheckInputs(tx, state, view, true, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC))
  751. {
  752. return error("AcceptToMemoryPool: : ConnectInputs failed %s", hash.ToString().c_str());
  753. }
  754. }
  755. // Store transaction in memory
  756. {
  757. if (ptxOld)
  758. {
  759. LogPrint("mempool", "AcceptToMemoryPool: : replacing tx %s with new version\n", ptxOld->GetHash().ToString().c_str());
  760. pool.remove(*ptxOld);
  761. }
  762. pool.addUnchecked(hash, tx);
  763. }
  764. ///// are we sure this is ok when loading transactions or restoring block txes
  765. // If updated, erase old tx from wallet
  766. if (ptxOld)
  767. g_signals.EraseTransaction(ptxOld->GetHash());
  768. g_signals.SyncTransaction(hash, tx, NULL);
  769. LogPrint("mempool", "AcceptToMemoryPool: : accepted %s (poolsz %"PRIszu")\n",
  770. hash.ToString().c_str(),
  771. pool.mapTx.size());
  772. return true;
  773. }
  774. int CMerkleTx::GetDepthInMainChain(CBlockIndex* &pindexRet) const
  775. {
  776. if (hashBlock == 0 || nIndex == -1)
  777. return 0;
  778. // Find the block it claims to be in
  779. map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
  780. if (mi == mapBlockIndex.end())
  781. return 0;
  782. CBlockIndex* pindex = (*mi).second;
  783. if (!pindex || !chainActive.Contains(pindex))
  784. return 0;
  785. // Make sure the merkle branch connects to this block
  786. if (!fMerkleVerified)
  787. {
  788. if (CBlock::CheckMerkleBranch(GetHash(), vMerkleBranch, nIndex) != pindex->hashMerkleRoot)
  789. return 0;
  790. fMerkleVerified = true;
  791. }
  792. pindexRet = pindex;
  793. return chainActive.Height() - pindex->nHeight + 1;
  794. }
  795. int CMerkleTx::GetBlocksToMaturity() const
  796. {
  797. if (!IsCoinBase())
  798. return 0;
  799. return max(0, (COINBASE_MATURITY+1) - GetDepthInMainChain());
  800. }
  801. bool CMerkleTx::AcceptToMemoryPool(bool fLimitFree)
  802. {
  803. CValidationState state;
  804. return ::AcceptToMemoryPool(mempool, state, *this, fLimitFree, NULL);
  805. }
  806. // Return transaction in tx, and if it was found inside a block, its hash is placed in hashBlock
  807. bool GetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock, bool fAllowSlow)
  808. {
  809. CBlockIndex *pindexSlow = NULL;
  810. {
  811. LOCK(cs_main);
  812. {
  813. if (mempool.lookup(hash, txOut))
  814. {
  815. return true;
  816. }
  817. }
  818. if (fTxIndex) {
  819. CDiskTxPos postx;
  820. if (pblocktree->ReadTxIndex(hash, postx)) {
  821. CAutoFile file(OpenBlockFile(postx, true), SER_DISK, CLIENT_VERSION);
  822. CBlockHeader header;
  823. try {
  824. file >> header;
  825. fseek(file, postx.nTxOffset, SEEK_CUR);
  826. file >> txOut;
  827. } catch (std::exception &e) {
  828. return error("%s() : deserialize or I/O error", __PRETTY_FUNCTION__);
  829. }
  830. hashBlock = header.GetHash();
  831. if (txOut.GetHash() != hash)
  832. return error("%s() : txid mismatch", __PRETTY_FUNCTION__);
  833. return true;
  834. }
  835. }
  836. if (fAllowSlow) { // use coin database to locate block that contains transaction, and scan it
  837. int nHeight = -1;
  838. {
  839. CCoinsViewCache &view = *pcoinsTip;
  840. CCoins coins;
  841. if (view.GetCoins(hash, coins))
  842. nHeight = coins.nHeight;
  843. }
  844. if (nHeight > 0)
  845. pindexSlow = chainActive[nHeight];
  846. }
  847. }
  848. if (pindexSlow) {
  849. CBlock block;
  850. if (ReadBlockFromDisk(block, pindexSlow)) {
  851. BOOST_FOREACH(const CTransaction &tx, block.vtx) {
  852. if (tx.GetHash() == hash) {
  853. txOut = tx;
  854. hashBlock = pindexSlow->GetBlockHash();
  855. return true;
  856. }
  857. }
  858. }
  859. }
  860. return false;
  861. }
  862. //////////////////////////////////////////////////////////////////////////////
  863. //
  864. // CBlock and CBlockIndex
  865. //
  866. bool WriteBlockToDisk(CBlock& block, CDiskBlockPos& pos)
  867. {
  868. // Open history file to append
  869. CAutoFile fileout = CAutoFile(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION);
  870. if (!fileout)
  871. return error("WriteBlockToDisk() : OpenBlockFile failed");
  872. // Write index header
  873. unsigned int nSize = fileout.GetSerializeSize(block);
  874. fileout << FLATDATA(Params().MessageStart()) << nSize;
  875. // Write block
  876. long fileOutPos = ftell(fileout);
  877. if (fileOutPos < 0)
  878. return error("WriteBlockToDisk() : ftell failed");
  879. pos.nPos = (unsigned int)fileOutPos;
  880. fileout << block;
  881. // Flush stdio buffers and commit to disk before returning
  882. fflush(fileout);
  883. if (!IsInitialBlockDownload())
  884. FileCommit(fileout);
  885. return true;
  886. }
  887. bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos)
  888. {
  889. block.SetNull();
  890. // Open history file to read
  891. CAutoFile filein = CAutoFile(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);
  892. if (!filein)
  893. return error("ReadBlockFromDisk(CBlock&, CDiskBlockPos&) : OpenBlockFile failed");
  894. // Read block
  895. try {
  896. filein >> block;
  897. }
  898. catch (std::exception &e) {
  899. return error("%s() : deserialize or I/O error", __PRETTY_FUNCTION__);
  900. }
  901. // Check the header
  902. if (!CheckProofOfWork(block.GetHash(), block.nBits))
  903. return error("ReadBlockFromDisk(CBlock&, CDiskBlockPos&) : errors in block header");
  904. return true;
  905. }
  906. bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex)
  907. {
  908. if (!ReadBlockFromDisk(block, pindex->GetBlockPos()))
  909. return false;
  910. if (block.GetHash() != pindex->GetBlockHash())
  911. return error("ReadBlockFromDisk(CBlock&, CBlockIndex*) : GetHash() doesn't match index");
  912. return true;
  913. }
  914. uint256 static GetOrphanRoot(const CBlockHeader* pblock)
  915. {
  916. // Work back to the first block in the orphan chain
  917. while (mapOrphanBlocks.count(pblock->hashPrevBlock))
  918. pblock = mapOrphanBlocks[pblock->hashPrevBlock];
  919. return pblock->GetHash();
  920. }
  921. int64_t GetBlockValue(int nHeight, int64_t nFees)
  922. {
  923. int64_t nSubsidy = 50 * COIN;
  924. // Subsidy is cut in half every 210,000 blocks which will occur approximately every 4 years.
  925. nSubsidy >>= (nHeight / Params().SubsidyHalvingInterval());
  926. return nSubsidy + nFees;
  927. }
  928. static const int64_t nTargetTimespan = 14 * 24 * 60 * 60; // two weeks
  929. static const int64_t nTargetSpacing = 10 * 60;
  930. static const int64_t nInterval = nTargetTimespan / nTargetSpacing;
  931. //
  932. // minimum amount of work that could possibly be required nTime after
  933. // minimum work required was nBase
  934. //
  935. unsigned int ComputeMinWork(unsigned int nBase, int64_t nTime)
  936. {
  937. const CBigNum &bnLimit = Params().ProofOfWorkLimit();
  938. // Testnet has min-difficulty blocks
  939. // after nTargetSpacing*2 time between blocks:
  940. if (TestNet() && nTime > nTargetSpacing*2)
  941. return bnLimit.GetCompact();
  942. CBigNum bnResult;
  943. bnResult.SetCompact(nBase);
  944. while (nTime > 0 && bnResult < bnLimit)
  945. {
  946. // Maximum 400% adjustment...
  947. bnResult *= 4;
  948. // ... in best-case exactly 4-times-normal target time
  949. nTime -= nTargetTimespan*4;
  950. }
  951. if (bnResult > bnLimit)
  952. bnResult = bnLimit;
  953. return bnResult.GetCompact();
  954. }
  955. unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock)
  956. {
  957. unsigned int nProofOfWorkLimit = Params().ProofOfWorkLimit().GetCompact();
  958. // Genesis block
  959. if (pindexLast == NULL)
  960. return nProofOfWorkLimit;
  961. // Only change once per interval
  962. if ((pindexLast->nHeight+1) % nInterval != 0)
  963. {
  964. if (TestNet())
  965. {
  966. // Special difficulty rule for testnet:
  967. // If the new block's timestamp is more than 2* 10 minutes
  968. // then allow mining of a min-difficulty block.
  969. if (pblock->nTime > pindexLast->nTime + nTargetSpacing*2)
  970. return nProofOfWorkLimit;
  971. else
  972. {
  973. // Return the last non-special-min-difficulty-rules-block
  974. const CBlockIndex* pindex = pindexLast;
  975. while (pindex->pprev && pindex->nHeight % nInterval != 0 && pindex->nBits == nProofOfWorkLimit)
  976. pindex = pindex->pprev;
  977. return pindex->nBits;
  978. }
  979. }
  980. return pindexLast->nBits;
  981. }
  982. // Go back by what we want to be 14 days worth of blocks
  983. const CBlockIndex* pindexFirst = pindexLast;
  984. for (int i = 0; pindexFirst && i < nInterval-1; i++)
  985. pindexFirst = pindexFirst->pprev;
  986. assert(pindexFirst);
  987. // Limit adjustment step
  988. int64_t nActualTimespan = pindexLast->GetBlockTime() - pindexFirst->GetBlockTime();
  989. LogPrintf(" nActualTimespan = %"PRId64" before bounds\n", nActualTimespan);
  990. if (nActualTimespan < nTargetTimespan/4)
  991. nActualTimespan = nTargetTimespan/4;
  992. if (nActualTimespan > nTargetTimespan*4)
  993. nActualTimespan = nTargetTimespan*4;
  994. // Retarget
  995. CBigNum bnNew;
  996. bnNew.SetCompact(pindexLast->nBits);
  997. bnNew *= nActualTimespan;
  998. bnNew /= nTargetTimespan;
  999. if (bnNew > Params().ProofOfWorkLimit())
  1000. bnNew = Params().ProofOfWorkLimit();
  1001. /// debug print
  1002. LogPrintf("GetNextWorkRequired RETARGET\n");
  1003. LogPrintf("nTargetTimespan = %"PRId64" nActualTimespan = %"PRId64"\n", nTargetTimespan, nActualTimespan);
  1004. LogPrintf("Before: %08x %s\n", pindexLast->nBits, CBigNum().SetCompact(pindexLast->nBits).getuint256().ToString().c_str());
  1005. LogPrintf("After: %08x %s\n", bnNew.GetCompact(), bnNew.getuint256().ToString().c_str());
  1006. return bnNew.GetCompact();
  1007. }
  1008. bool CheckProofOfWork(uint256 hash, unsigned int nBits)
  1009. {
  1010. CBigNum bnTarget;
  1011. bnTarget.SetCompact(nBits);
  1012. // Check range
  1013. if (bnTarget <= 0 || bnTarget > Params().ProofOfWorkLimit())
  1014. return error("CheckProofOfWork() : nBits below minimum work");
  1015. // Check proof of work matches claimed amount
  1016. if (hash > bnTarget.getuint256())
  1017. return error("CheckProofOfWork() : hash doesn't match nBits");
  1018. return true;
  1019. }
  1020. // Return maximum amount of blocks that other nodes claim to have
  1021. int GetNumBlocksOfPeers()
  1022. {
  1023. return std::max(cPeerBlockCounts.median(), Checkpoints::GetTotalBlocksEstimate());
  1024. }
  1025. bool IsInitialBlockDownload()
  1026. {
  1027. if (fImporting || fReindex || chainActive.Height() < Checkpoints::GetTotalBlocksEstimate())
  1028. return true;
  1029. static int64_t nLastUpdate;
  1030. static CBlockIndex* pindexLastBest;
  1031. if (chainActive.Tip() != pindexLastBest)
  1032. {
  1033. pindexLastBest = chainActive.Tip();
  1034. nLastUpdate = GetTime();
  1035. }
  1036. return (GetTime() - nLastUpdate < 10 &&
  1037. chainActive.Tip()->GetBlockTime() < GetTime() - 24 * 60 * 60);
  1038. }
  1039. bool fLargeWorkForkFound = false;
  1040. bool fLargeWorkInvalidChainFound = false;
  1041. CBlockIndex *pindexBestForkTip = NULL, *pindexBestForkBase = NULL;
  1042. void CheckForkWarningConditions()
  1043. {
  1044. // Before we get past initial download, we cannot reliably alert about forks
  1045. // (we assume we don't get stuck on a fork before the last checkpoint)
  1046. if (IsInitialBlockDownload())
  1047. return;
  1048. // If our best fork is no longer within 72 blocks (+/- 12 hours if no one mines it)
  1049. // of our head, drop it
  1050. if (pindexBestForkTip && chainActive.Height() - pindexBestForkTip->nHeight >= 72)
  1051. pindexBestForkTip = NULL;
  1052. if (pindexBestForkTip || (pindexBestInvalid && pindexBestInvalid->nChainWork > chainActive.Tip()->nChainWork + (chainActive.Tip()->GetBlockWork() * 6).getuint256()))
  1053. {
  1054. if (!fLargeWorkForkFound)
  1055. {
  1056. std::string strCmd = GetArg("-alertnotify", "");
  1057. if (!strCmd.empty())
  1058. {
  1059. std::string warning = std::string("'Warning: Large-work fork detected, forking after block ") +
  1060. pindexBestForkBase->phashBlock->ToString() + std::string("'");
  1061. boost::replace_all(strCmd, "%s", warning);
  1062. boost::thread t(runCommand, strCmd); // thread runs free
  1063. }
  1064. }
  1065. if (pindexBestForkTip)
  1066. {
  1067. LogPrintf("CheckForkWarningConditions: Warning: Large valid fork found\n forking the chain at height %d (%s)\n lasting to height %d (%s).\nChain state database corruption likely.\n",
  1068. pindexBestForkBase->nHeight, pindexBestForkBase->phashBlock->ToString().c_str(),
  1069. pindexBestForkTip->nHeight, pindexBestForkTip->phashBlock->ToString().c_str());
  1070. fLargeWorkForkFound = true;
  1071. }
  1072. else
  1073. {
  1074. LogPrintf("CheckForkWarningConditions: Warning: Found invalid chain at least ~6 blocks longer than our best chain.\nChain state database corruption likely.\n");
  1075. fLargeWorkInvalidChainFound = true;
  1076. }
  1077. }
  1078. else
  1079. {
  1080. fLargeWorkForkFound = false;
  1081. fLargeWorkInvalidChainFound = false;
  1082. }
  1083. }
  1084. void CheckForkWarningConditionsOnNewFork(CBlockIndex* pindexNewForkTip)
  1085. {
  1086. // If we are on a fork that is sufficiently large, set a warning flag
  1087. CBlockIndex* pfork = pindexNewForkTip;
  1088. CBlockIndex* plonger = chainActive.Tip();
  1089. while (pfork && pfork != plonger)
  1090. {
  1091. while (plonger && plonger->nHeight > pfork->nHeight)
  1092. plonger = plonger->pprev;
  1093. if (pfork == plonger)
  1094. break;
  1095. pfork = pfork->pprev;
  1096. }
  1097. // We define a condition which we should warn the user about as a fork of at least 7 blocks
  1098. // who's tip is within 72 blocks (+/- 12 hours if no one mines it) of ours
  1099. // We use 7 blocks rather arbitrarily as it represents just under 10% of sustained network
  1100. // hash rate operating on the fork.
  1101. // or a chain that is entirely longer than ours and invalid (note that this should be detected by both)
  1102. // We define it this way because it allows us to only store the highest fork tip (+ base) which meets
  1103. // the 7-block condition and from this always have the most-likely-to-cause-warning fork
  1104. if (pfork && (!pindexBestForkTip || (pindexBestForkTip && pindexNewForkTip->nHeight > pindexBestForkTip->nHeight)) &&
  1105. pindexNewForkTip->nChainWork - pfork->nChainWork > (pfork->GetBlockWork() * 7).getuint256() &&
  1106. chainActive.Height() - pindexNewForkTip->nHeight < 72)
  1107. {
  1108. pindexBestForkTip = pindexNewForkTip;
  1109. pindexBestForkBase = pfork;
  1110. }
  1111. CheckForkWarningConditions();
  1112. }
  1113. void static InvalidChainFound(CBlockIndex* pindexNew)
  1114. {
  1115. if (!pindexBestInvalid || pindexNew->nChainWork > pindexBestInvalid->nChainWork)
  1116. {
  1117. pindexBestInvalid = pindexNew;
  1118. // The current code doesn't actually read the BestInvalidWork entry in
  1119. // the block database anymore, as it is derived from the flags in block
  1120. // index entry. We only write it for backward compatibility.
  1121. pblocktree->WriteBestInvalidWork(CBigNum(pindexBestInvalid->nChainWork));
  1122. uiInterface.NotifyBlocksChanged();
  1123. }
  1124. LogPrintf("InvalidChainFound: invalid block=%s height=%d log2_work=%.8g date=%s\n",
  1125. pindexNew->GetBlockHash().ToString().c_str(), pindexNew->nHeight,
  1126. log(pindexNew->nChainWork.getdouble())/log(2.0), DateTimeStrFormat("%Y-%m-%d %H:%M:%S",
  1127. pindexNew->GetBlockTime()).c_str());
  1128. LogPrintf("InvalidChainFound: current best=%s height=%d log2_work=%.8g date=%s\n",
  1129. chainActive.Tip()->GetBlockHash().ToString().c_str(), chainActive.Height(), log(chainActive.Tip()->nChainWork.getdouble())/log(2.0),
  1130. DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()).c_str());
  1131. CheckForkWarningConditions();
  1132. }
  1133. void static InvalidBlockFound(CBlockIndex *pindex) {
  1134. pindex->nStatus |= BLOCK_FAILED_VALID;
  1135. pblocktree->WriteBlockIndex(CDiskBlockIndex(pindex));
  1136. setBlockIndexValid.erase(pindex);
  1137. InvalidChainFound(pindex);
  1138. if (chainActive.Next(pindex)) {
  1139. CValidationState stateDummy;
  1140. ConnectBestBlock(stateDummy); // reorganise away from the failed block
  1141. }
  1142. }
  1143. bool ConnectBestBlock(CValidationState &state) {
  1144. do {
  1145. CBlockIndex *pindexNewBest;
  1146. {
  1147. std::set<CBlockIndex*,CBlockIndexWorkComparator>::reverse_iterator it = setBlockIndexValid.rbegin();
  1148. if (it == setBlockIndexValid.rend())
  1149. return true;
  1150. pindexNewBest = *it;
  1151. }
  1152. if (pindexNewBest == chainActive.Tip() || (chainActive.Tip() && pindexNewBest->nChainWork == chainActive.Tip()->nChainWork))
  1153. return true; // nothing to do
  1154. // check ancestry
  1155. CBlockIndex *pindexTest = pindexNewBest;
  1156. std::vector<CBlockIndex*> vAttach;
  1157. do {
  1158. if (pindexTest->nStatus & BLOCK_FAILED_MASK) {
  1159. // mark descendants failed
  1160. CBlockIndex *pindexFailed = pindexNewBest;
  1161. while (pindexTest != pindexFailed) {
  1162. pindexFailed->nStatus |= BLOCK_FAILED_CHILD;
  1163. setBlockIndexValid.erase(pindexFailed);
  1164. pblocktree->WriteBlockIndex(CDiskBlockIndex(pindexFailed));
  1165. pindexFailed = pindexFailed->pprev;
  1166. }
  1167. InvalidChainFound(pindexNewBest);
  1168. break;
  1169. }
  1170. if (chainActive.Tip() == NULL || pindexTest->nChainWork > chainActive.Tip()->nChainWork)
  1171. vAttach.push_back(pindexTest);
  1172. if (pindexTest->pprev == NULL || chainActive.Next(pindexTest)) {
  1173. reverse(vAttach.begin(), vAttach.end());
  1174. BOOST_FOREACH(CBlockIndex *pindexSwitch, vAttach) {
  1175. boost::this_thread::interruption_point();
  1176. try {
  1177. if (!SetBestChain(state, pindexSwitch))
  1178. return false;
  1179. } catch(std::runtime_error &e) {
  1180. return state.Abort(_("System error: ") + e.what());
  1181. }
  1182. }
  1183. return true;
  1184. }
  1185. pindexTest = pindexTest->pprev;
  1186. } while(true);
  1187. } while(true);
  1188. }
  1189. void UpdateTime(CBlockHeader& block, const CBlockIndex* pindexPrev)
  1190. {
  1191. block.nTime = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
  1192. // Updating time can change work required on testnet:
  1193. if (TestNet())
  1194. block.nBits = GetNextWorkRequired(pindexPrev, &block);
  1195. }
  1196. const CTxOut &CCoinsViewCache::GetOutputFor(const CTxIn& input)
  1197. {
  1198. const CCoins &coins = GetCoins(input.prevout.hash);
  1199. assert(coins.IsAvailable(input.prevout.n));
  1200. return coins.vout[input.prevout.n];
  1201. }
  1202. int64_t CCoinsViewCache::GetValueIn(const CTransaction& tx)
  1203. {
  1204. if (tx.IsCoinBase())
  1205. return 0;
  1206. int64_t nResult = 0;
  1207. for (unsigned int i = 0; i < tx.vin.size(); i++)
  1208. nResult += GetOutputFor(tx.vin[i]).nValue;
  1209. return nResult;
  1210. }
  1211. void UpdateCoins(const CTransaction& tx, CValidationState &state, CCoinsViewCache &inputs, CTxUndo &txundo, int nHeight, const uint256 &txhash)
  1212. {
  1213. // mark inputs spent
  1214. if (!tx.IsCoinBase()) {
  1215. BOOST_FOREACH(const CTxIn &txin, tx.vin) {
  1216. CCoins &coins = inputs.GetCoins(txin.prevout.hash);
  1217. CTxInUndo undo;
  1218. assert(coins.Spend(txin.prevout, undo));
  1219. txundo.vprevout.push_back(undo);
  1220. }
  1221. }
  1222. // add outputs
  1223. assert(inputs.SetCoins(txhash, CCoins(tx, nHeight)));
  1224. }
  1225. bool CCoinsViewCache::HaveInputs(const CTransaction& tx)
  1226. {
  1227. if (!tx.IsCoinBase()) {
  1228. // first check whether information about the prevout hash is available
  1229. for (unsigned int i = 0; i < tx.vin.size(); i++) {
  1230. const COutPoint &prevout = tx.vin[i].prevout;
  1231. if (!HaveCoins(prevout.hash))
  1232. return false;
  1233. }
  1234. // then check whether the actual outputs are available
  1235. for (unsigned int i = 0; i < tx.vin.size(); i++) {
  1236. const COutPoint &prevout = tx.vin[i].prevout;
  1237. const CCoins &coins = GetCoins(prevout.hash);
  1238. if (!coins.IsAvailable(prevout.n))
  1239. return false;
  1240. }
  1241. }
  1242. return true;
  1243. }
  1244. bool CScriptCheck::operator()() const {
  1245. const CScript &scriptSig = ptxTo->vin[nIn].scriptSig;
  1246. if (!VerifyScript(scriptSig, scriptPubKey, *ptxTo, nIn, nFlags, nHashType))
  1247. return error("CScriptCheck() : %s VerifySignature failed", ptxTo->GetHash().ToString().c_str());
  1248. return true;
  1249. }
  1250. bool VerifySignature(const CCoins& txFrom, const CTransaction& txTo, unsigned int nIn, unsigned int flags, int nHashType)
  1251. {
  1252. return CScriptCheck(txFrom, txTo, nIn, flags, nHashType)();
  1253. }
  1254. bool CheckInputs(const CTransaction& tx, CValidationState &state, CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, std::vector<CScriptCheck> *pvChecks)
  1255. {
  1256. if (!tx.IsCoinBase())
  1257. {
  1258. if (pvChecks)
  1259. pvChecks->reserve(tx.vin.size());
  1260. // This doesn't trigger the DoS code on purpose; if it did, it would make it easier
  1261. // for an attacker to attempt to split the network.
  1262. if (!inputs.HaveInputs(tx))
  1263. return state.Invalid(error("CheckInputs() : %s inputs unavailable", tx.GetHash().ToString().c_str()));
  1264. // While checking, GetBestBlock() refers to the parent block.
  1265. // This is also true for mempool checks.
  1266. int nSpendHeight = inputs.GetBestBlock()->nHeight + 1;
  1267. int64_t nValueIn = 0;
  1268. int64_t nFees = 0;
  1269. for (unsigned int i = 0; i < tx.vin.size(); i++)
  1270. {
  1271. const COutPoint &prevout = tx.vin[i].prevout;
  1272. const CCoins &coins = inputs.GetCoins(prevout.hash);
  1273. // If prev is coinbase, check that it's matured
  1274. if (coins.IsCoinBase()) {
  1275. if (nSpendHeight - coins.nHeight < COINBASE_MATURITY)
  1276. return state.Invalid(error("CheckInputs() : tried to spend coinbase at depth %d", nSpendHeight - coins.nHeight));
  1277. }
  1278. // Check for negative or overflow input values
  1279. nValueIn += coins.vout[prevout.n].nValue;
  1280. if (!MoneyRange(coins.vout[prevout.n].nValue) || !MoneyRange(nValueIn))
  1281. return state.DoS(100, error("CheckInputs() : txin values out of range"));
  1282. }
  1283. if (nValueIn < GetValueOut(tx))
  1284. return state.DoS(100, error("CheckInputs() : %s value in < value out", tx.GetHash().ToString().c_str()));
  1285. // Tally transaction fees
  1286. int64_t nTxFee = nValueIn - GetValueOut(tx);
  1287. if (nTxFee < 0)
  1288. return state.DoS(100, error("CheckInputs() : %s nTxFee < 0", tx.GetHash().ToString().c_str()));
  1289. nFees += nTxFee;
  1290. if (!MoneyRange(nFees))
  1291. return state.DoS(100, error("CheckInputs() : nFees out of range"));
  1292. // The first loop above does all the inexpensive checks.
  1293. // Only if ALL inputs pass do we perform expensive ECDSA signature checks.
  1294. // Helps prevent CPU exhaustion attacks.
  1295. // Skip ECDSA signature verification when connecting blocks
  1296. // before the last block chain checkpoint. This is safe because block merkle hashes are
  1297. // still computed and checked, and any change will be caught at the next checkpoint.
  1298. if (fScriptChecks) {
  1299. for (unsigned int i = 0; i < tx.vin.size(); i++) {
  1300. const COutPoint &prevout = tx.vin[i].prevout;
  1301. const CCoins &coins = inputs.GetCoins(prevout.hash);
  1302. // Verify signature
  1303. CScriptCheck check(coins, tx, i, flags, 0);
  1304. if (pvChecks) {
  1305. pvChecks->push_back(CScriptCheck());
  1306. check.swap(pvChecks->back());
  1307. } else if (!check()) {
  1308. if (flags & SCRIPT_VERIFY_STRICTENC) {
  1309. // For now, check whether the failure was caused by non-canonical
  1310. // encodings or not; if so, don't trigger DoS protection.
  1311. CScriptCheck check(coins, tx, i, flags & (~SCRIPT_VERIFY_STRICTENC), 0);
  1312. if (check())
  1313. return state.Invalid();
  1314. }
  1315. return state.DoS(100,false);
  1316. }
  1317. }
  1318. }
  1319. }
  1320. return true;
  1321. }
  1322. bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, CCoinsViewCache& view, bool* pfClean)
  1323. {
  1324. assert(pindex == view.GetBestBlock());
  1325. if (pfClean)
  1326. *pfClean = false;
  1327. bool fClean = true;
  1328. CBlockUndo blockUndo;
  1329. CDiskBlockPos pos = pindex->GetUndoPos();
  1330. if (pos.IsNull())
  1331. return error("DisconnectBlock() : no undo data available");
  1332. if (!blockUndo.ReadFromDisk(pos, pindex->pprev->GetBlockHash()))
  1333. return error("DisconnectBlock() : failure reading undo data");
  1334. if (blockUndo.vtxundo.size() + 1 != block.vtx.size())
  1335. return error("DisconnectBlock() : block and undo data inconsistent");
  1336. // undo transactions in reverse order
  1337. for (int i = block.vtx.size() - 1; i >= 0; i--) {
  1338. const CTransaction &tx = block.vtx[i];
  1339. uint256 hash = tx.GetHash();
  1340. // Check that all outputs are available and match the outputs in the block itself
  1341. // exactly. Note that transactions with only provably unspendable outputs won't
  1342. // have outputs available even in the block itself, so we handle that case
  1343. // specially with outsEmpty.
  1344. CCoins outsEmpty;
  1345. CCoins &outs = view.HaveCoins(hash) ? view.GetCoins(hash) : outsEmpty;
  1346. outs.ClearUnspendable();
  1347. CCoins outsBlock = CCoins(tx, pindex->nHeight);
  1348. // The CCoins serialization does not serialize negative numbers.
  1349. // No network rules currently depend on the version here, so an inconsistency is harmless
  1350. // but it must be corrected before txout nversion ever influences a network rule.
  1351. if (outsBlock.nVersion < 0)
  1352. outs.nVersion = outsBlock.nVersion;
  1353. if (outs != outsBlock)
  1354. fClean = fClean && error("DisconnectBlock() : added transaction mismatch? database corrupted");
  1355. // remove outputs
  1356. outs = CCoins();
  1357. // restore inputs
  1358. if (i > 0) { // not coinbases
  1359. const CTxUndo &txundo = blockUndo.vtxundo[i-1];
  1360. if (txundo.vprevout.size() != tx.vin.size())
  1361. return error("DisconnectBlock() : transaction and undo data inconsistent");
  1362. for (unsigned int j = tx.vin.size(); j-- > 0;) {
  1363. const COutPoint &out = tx.vin[j].prevout;
  1364. const CTxInUndo &undo = txundo.vprevout[j];
  1365. CCoins coins;
  1366. view.GetCoins(out.hash, coins); // this can fail if the prevout was already entirely spent
  1367. if (undo.nHeight != 0) {
  1368. // undo data contains height: this is the last output of the prevout tx being spent
  1369. if (!coins.IsPruned())
  1370. fClean = fClean && error("DisconnectBlock() : undo data overwriting existing transaction");
  1371. coins = CCoins();
  1372. coins.fCoinBase = undo.fCoinBase;
  1373. coins.nHeight = undo.nHeight;
  1374. coins.nVersion = undo.nVersion;
  1375. } else {
  1376. if (coins.IsPruned())
  1377. fClean = fClean && error("DisconnectBlock() : undo data adding output to missing transaction");
  1378. }
  1379. if (coins.IsAvailable(out.n))
  1380. fClean = fClean && error("DisconnectBlock() : undo data overwriting existing output");
  1381. if (coins.vout.size() < out.n+1)
  1382. coins.vout.resize(out.n+1);
  1383. coins.vout[out.n] = undo.txout;
  1384. if (!view.SetCoins(out.hash, coins))
  1385. return error("DisconnectBlock() : cannot restore coin inputs");
  1386. }
  1387. }
  1388. }
  1389. // move best block pointer to prevout block
  1390. view.SetBestBlock(pindex->pprev);
  1391. if (pfClean) {
  1392. *pfClean = fClean;
  1393. return true;
  1394. } else {
  1395. return fClean;
  1396. }
  1397. }
  1398. void static FlushBlockFile(bool fFinalize = false)
  1399. {
  1400. LOCK(cs_LastBlockFile);
  1401. CDiskBlockPos posOld(nLastBlockFile, 0);
  1402. FILE *fileOld = OpenBlockFile(posOld);
  1403. if (fileOld) {
  1404. if (fFinalize)
  1405. TruncateFile(fileOld, infoLastBlockFile.nSize);
  1406. FileCommit(fileOld);
  1407. fclose(fileOld);
  1408. }
  1409. fileOld = OpenUndoFile(posOld);
  1410. if (fileOld) {
  1411. if (fFinalize)
  1412. TruncateFile(fileOld, infoLastBlockFile.nUndoSize);
  1413. FileCommit(fileOld);
  1414. fclose(fileOld);
  1415. }
  1416. }
  1417. bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize);
  1418. static CCheckQueue<CScriptCheck> scriptcheckqueue(128);
  1419. void ThreadScriptCheck() {
  1420. RenameThread("bitcoin-scriptch");
  1421. scriptcheckqueue.Thread();
  1422. }
  1423. bool ConnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, CCoinsViewCache& view, bool fJustCheck)
  1424. {
  1425. // Check it again in case a previous version let a bad block in
  1426. if (!CheckBlock(block, state, !fJustCheck, !fJustCheck))
  1427. return false;
  1428. // verify that the view's current state corresponds to the previous block
  1429. assert(pindex->pprev == view.GetBestBlock());
  1430. // Special case for the genesis block, skipping connection of its transactions
  1431. // (its coinbase is unspendable)
  1432. if (block.GetHash() == Params().HashGenesisBlock()) {
  1433. view.SetBestBlock(pindex);
  1434. return true;
  1435. }
  1436. bool fScriptChecks = pindex->nHeight >= Checkpoints::GetTotalBlocksEstimate();
  1437. // Do not allow blocks that contain transactions which 'overwrite' older transactions,
  1438. // unless those are already completely spent.
  1439. // If such overwrites are allowed, coinbases and transactions depending upon those
  1440. // can be duplicated to remove the ability to spend the first instance -- even after
  1441. // being sent to another address.
  1442. // See BIP30 and http://r6.ca/blog/20120206T005236Z.html for more information.
  1443. // This logic is not necessary for memory pool transactions, as AcceptToMemoryPool
  1444. // already refuses previously-known transaction ids entirely.
  1445. // This rule was originally applied all blocks whose timestamp was after March 15, 2012, 0:00 UTC.
  1446. // Now that the whole chain is irreversibly beyond that time it is applied to all blocks except the
  1447. // two in the chain that violate it. This prevents exploiting the issue against nodes in their
  1448. // initial block download.
  1449. bool fEnforceBIP30 = (!pindex->phashBlock) || // Enforce on CreateNewBlock invocations which don't have a hash.
  1450. !((pindex->nHeight==91842 && pindex->GetBlockHash() == uint256("0x00000000000a4d0a398161ffc163c503763b1f4360639393e0e4c8e300e0caec")) ||
  1451. (pindex->nHeight==91880 && pindex->GetBlockHash() == uint256("0x00000000000743f190a18c5577a3c2d2a1f610ae9601ac046a38084ccb7cd721")));
  1452. if (fEnforceBIP30) {
  1453. for (unsigned int i = 0; i < block.vtx.size(); i++) {
  1454. uint256 hash = block.GetTxHash(i);
  1455. if (view.HaveCoins(hash) && !view.GetCoins(hash).IsPruned())
  1456. return state.DoS(100, error("ConnectBlock() : tried to overwrite transaction"));
  1457. }
  1458. }
  1459. // BIP16 didn't become active until Apr 1 2012
  1460. int64_t nBIP16SwitchTime = 1333238400;
  1461. bool fStrictPayToScriptHash = (pindex->nTime >= nBIP16SwitchTime);
  1462. unsigned int flags = SCRIPT_VERIFY_NOCACHE |
  1463. (fStrictPayToScriptHash ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE);
  1464. CBlockUndo blockundo;
  1465. CCheckQueueControl<CScriptCheck> control(fScriptChecks && nScriptCheckThreads ? &scriptcheckqueue : NULL);
  1466. int64_t nStart = GetTimeMicros();
  1467. int64_t nFees = 0;
  1468. int nInputs = 0;
  1469. unsigned int nSigOps = 0;
  1470. CDiskTxPos pos(pindex->GetBlockPos(), GetSizeOfCompactSize(block.vtx.size()));
  1471. std::vector<std::pair<uint256, CDiskTxPos> > vPos;
  1472. vPos.reserve(block.vtx.size());
  1473. for (unsigned int i = 0; i < block.vtx.size(); i++)
  1474. {
  1475. const CTransaction &tx = block.vtx[i];
  1476. nInputs += tx.vin.size();
  1477. nSigOps += GetLegacySigOpCount(tx);
  1478. if (nSigOps > MAX_BLOCK_SIGOPS)
  1479. return state.DoS(100, error("ConnectBlock() : too many sigops"));
  1480. if (!tx.IsCoinBase())
  1481. {
  1482. if (!view.HaveInputs(tx))
  1483. return state.DoS(100, error("ConnectBlock() : inputs missing/spent"));
  1484. if (fStrictPayToScriptHash)
  1485. {
  1486. // Add in sigops done by pay-to-script-hash inputs;
  1487. // this is to prevent a "rogue miner" from creating
  1488. // an incredibly-expensive-to-validate block.
  1489. nSigOps += GetP2SHSigOpCount(tx, view);
  1490. if (nSigOps > MAX_BLOCK_SIGOPS)
  1491. return state.DoS(100, error("ConnectBlock() : too many sigops"));
  1492. }
  1493. nFees += view.GetValueIn(tx)-GetValueOut(tx);
  1494. std::vector<CScriptCheck> vChecks;
  1495. if (!CheckInputs(tx, state, view, fScriptChecks, flags, nScriptCheckThreads ? &vChecks : NULL))
  1496. return false;
  1497. control.Add(vChecks);
  1498. }
  1499. CTxUndo txundo;
  1500. UpdateCoins(tx, state, view, txundo, pindex->nHeight, block.GetTxHash(i));
  1501. if (!tx.IsCoinBase())
  1502. blockundo.vtxundo.push_back(txundo);
  1503. vPos.push_back(std::make_pair(block.GetTxHash(i), pos));
  1504. pos.nTxOffset += ::GetSerializeSize(tx, SER_DISK, CLIENT_VERSION);
  1505. }
  1506. int64_t nTime = GetTimeMicros() - nStart;
  1507. if (fBenchmark)
  1508. LogPrintf("- Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin)\n", (unsigned)block.vtx.size(), 0.001 * nTime, 0.001 * nTime / block.vtx.size(), nInputs <= 1 ? 0 : 0.001 * nTime / (nInputs-1));
  1509. if (GetValueOut(block.vtx[0]) > GetBlockValue(pindex->nHeight, nFees))
  1510. return state.DoS(100, error("ConnectBlock() : coinbase pays too much (actual=%"PRId64" vs limit=%"PRId64")", GetValueOut(block.vtx[0]), GetBlockValue(pindex->nHeight, nFees)));
  1511. if (!control.Wait())
  1512. return state.DoS(100, false);
  1513. int64_t nTime2 = GetTimeMicros() - nStart;
  1514. if (fBenchmark)
  1515. LogPrintf("- Verify %u txins: %.2fms (%.3fms/txin)\n", nInputs - 1, 0.001 * nTime2, nInputs <= 1 ? 0 : 0.001 * nTime2 / (nInputs-1));
  1516. if (fJustCheck)
  1517. return true;
  1518. // Write undo information to disk
  1519. if (pindex->GetUndoPos().IsNull() || (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_SCRIPTS)
  1520. {
  1521. if (pindex->GetUndoPos().IsNull()) {
  1522. CDiskBlockPos pos;
  1523. if (!FindUndoPos(state, pindex->nFile, pos, ::GetSerializeSize(blockundo, SER_DISK, CLIENT_VERSION) + 40))
  1524. return error("ConnectBlock() : FindUndoPos failed");
  1525. if (!blockundo.WriteToDisk(pos, pindex->pprev->GetBlockHash()))
  1526. return state.Abort(_("Failed to write undo data"));
  1527. // update nUndoPos in block index
  1528. pindex->nUndoPos = pos.nPos;
  1529. pindex->nStatus |= BLOCK_HAVE_UNDO;
  1530. }
  1531. pindex->nStatus = (pindex->nStatus & ~BLOCK_VALID_MASK) | BLOCK_VALID_SCRIPTS;
  1532. CDiskBlockIndex blockindex(pindex);
  1533. if (!pblocktree->WriteBlockIndex(blockindex))
  1534. return state.Abort(_("Failed to write block index"));
  1535. }
  1536. if (fTxIndex)
  1537. if (!pblocktree->WriteTxIndex(vPos))
  1538. return state.Abort(_("Failed to write transaction index"));
  1539. // add this block to the view's block chain
  1540. assert(view.SetBestBlock(pindex));
  1541. // Watch for transactions paying to me
  1542. for (unsigned int i = 0; i < block.vtx.size(); i++)
  1543. g_signals.SyncTransaction(block.GetTxHash(i), block.vtx[i], &block);
  1544. return true;
  1545. }
  1546. bool SetBestChain(CValidationState &state, CBlockIndex* pindexNew)
  1547. {
  1548. mempool.check(&LookupFromTip);
  1549. // All modifications to the coin state will be done in this cache.
  1550. // Only when all have succeeded, we push it to pcoinsTip.
  1551. CCoinsViewCache view(*pcoinsTip, true);
  1552. // Find the fork (typically, there is none)
  1553. CBlockIndex* pfork = view.GetBestBlock();
  1554. CBlockIndex* plonger = pindexNew;
  1555. while (pfork && pfork != plonger)
  1556. {
  1557. while (plonger->nHeight > pfork->nHeight) {
  1558. plonger = plonger->pprev;
  1559. assert(plonger != NULL);
  1560. }
  1561. if (pfork == plonger)
  1562. break;
  1563. pfork = pfork->pprev;
  1564. assert(pfork != NULL);
  1565. }
  1566. // List of what to disconnect (typically nothing)
  1567. vector<CBlockIndex*> vDisconnect;
  1568. for (CBlockIndex* pindex = view.GetBestBlock(); pindex != pfork; pindex = pindex->pprev)
  1569. vDisconnect.push_back(pindex);
  1570. // List of what to connect (typically only pindexNew)
  1571. vector<CBlockIndex*> vConnect;
  1572. for (CBlockIndex* pindex = pindexNew; pindex != pfork; pindex = pindex->pprev)
  1573. vConnect.push_back(pindex);
  1574. reverse(vConnect.begin(), vConnect.end());
  1575. if (vDisconnect.size() > 0) {
  1576. LogPrintf("REORGANIZE: Disconnect %"PRIszu" blocks; %s...\n", vDisconnect.size(), pfork->GetBlockHash().ToString().c_str());
  1577. LogPrintf("REORGANIZE: Connect %"PRIszu" blocks; ...%s\n", vConnect.size(), pindexNew->GetBlockHash().ToString().c_str());
  1578. }
  1579. // Disconnect shorter branch
  1580. list<CTransaction> vResurrect;
  1581. BOOST_FOREACH(CBlockIndex* pindex, vDisconnect) {
  1582. CBlock block;
  1583. if (!ReadBlockFromDisk(block, pindex))
  1584. return state.Abort(_("Failed to read block"));
  1585. int64_t nStart = GetTimeMicros();
  1586. if (!DisconnectBlock(block, state, pindex, view))
  1587. return error("SetBestBlock() : DisconnectBlock %s failed", pindex->GetBlockHash().ToString().c_str());
  1588. if (fBenchmark)
  1589. LogPrintf("- Disconnect: %.2fms\n", (GetTimeMicros() - nStart) * 0.001);
  1590. // Queue memory transactions to resurrect.
  1591. // We only do this for blocks after the last checkpoint (reorganisation before that
  1592. // point should only happen with -reindex/-loadblock, or a misbehaving peer.
  1593. BOOST_REVERSE_FOREACH(const CTransaction& tx, block.vtx)
  1594. if (!tx.IsCoinBase() && pindex->nHeight > Checkpoints::GetTotalBlocksEstimate())
  1595. vResurrect.push_front(tx);
  1596. }
  1597. // Connect longer branch
  1598. vector<CTransaction> vDelete;
  1599. BOOST_FOREACH(CBlockIndex *pindex, vConnect) {
  1600. CBlock block;
  1601. if (!ReadBlockFromDisk(block, pindex))
  1602. return state.Abort(_("Failed to read block"));
  1603. int64_t nStart = GetTimeMicros();
  1604. if (!ConnectBlock(block, state, pindex, view)) {
  1605. if (state.IsInvalid()) {
  1606. InvalidChainFound(pindexNew);
  1607. InvalidBlockFound(pindex);
  1608. }
  1609. return error("SetBestBlock() : ConnectBlock %s failed", pindex->GetBlockHash().ToString().c_str());
  1610. }
  1611. if (fBenchmark)
  1612. LogPrintf("- Connect: %.2fms\n", (GetTimeMicros() - nStart) * 0.001);
  1613. // Queue memory transactions to delete
  1614. BOOST_FOREACH(const CTransaction& tx, block.vtx)
  1615. vDelete.push_back(tx);
  1616. }
  1617. // Flush changes to global coin state
  1618. int64_t nStart = GetTimeMicros();
  1619. int nModified = view.GetCacheSize();
  1620. assert(view.Flush());
  1621. int64_t nTime = GetTimeMicros() - nStart;
  1622. if (fBenchmark)
  1623. LogPrintf("- Flush %i transactions: %.2fms (%.4fms/tx)\n", nModified, 0.001 * nTime, 0.001 * nTime / nModified);
  1624. // Make sure it's successfully written to disk before changing memory structure
  1625. bool fIsInitialDownload = IsInitialBlockDownload();
  1626. if (!fIsInitialDownload || pcoinsTip->GetCacheSize() > nCoinCacheSize) {
  1627. // Typical CCoins structures on disk are around 100 bytes in size.
  1628. // Pushing a new one to the database can cause it to be written
  1629. // twice (once in the log, and once in the tables). This is already
  1630. // an overestimation, as most will delete an existing entry or
  1631. // overwrite one. Still, use a conservative safety factor of 2.
  1632. if (!CheckDiskSpace(100 * 2 * 2 * pcoinsTip->GetCacheSize()))
  1633. return state.Error();
  1634. FlushBlockFile();
  1635. pblocktree->Sync();
  1636. if (!pcoinsTip->Flush())
  1637. return state.Abort(_("Failed to write to coin database"));
  1638. }
  1639. // At this point, all changes have been done to the database.
  1640. // Proceed by updating the memory structures.
  1641. // Register new best chain
  1642. chainActive.SetTip(pindexNew);
  1643. // Resurrect memory transactions that were in the disconnected branch
  1644. BOOST_FOREACH(CTransaction& tx, vResurrect) {
  1645. // ignore validation errors in resurrected transactions
  1646. CValidationState stateDummy;
  1647. if (!AcceptToMemoryPool(mempool,stateDummy, tx, false, NULL))
  1648. mempool.remove(tx, true);
  1649. }
  1650. // Delete redundant memory transactions that are in the connected branch
  1651. BOOST_FOREACH(CTransaction& tx, vDelete) {
  1652. mempool.remove(tx);
  1653. mempool.removeConflicts(tx);
  1654. }
  1655. mempool.check(&LookupFromTip);
  1656. // Update best block in wallet (so we can detect restored wallets)
  1657. if ((pindexNew->nHeight % 20160) == 0 || (!fIsInitialDownload && (pindexNew->nHeight % 144) == 0))
  1658. g_signals.SetBestChain(chainActive.GetLocator(pindexNew));
  1659. // New best block
  1660. nTimeBestReceived = GetTime();
  1661. mempool.AddTransactionsUpdated(1);
  1662. LogPrintf("SetBestChain: new best=%s height=%d log2_work=%.8g tx=%lu date=%s progress=%f\n",
  1663. chainActive.Tip()->GetBlockHash().ToString().c_str(), chainActive.Height(), log(chainActive.Tip()->nChainWork.getdouble())/log(2.0), (unsigned long)pindexNew->nChainTx,
  1664. DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()).c_str(),
  1665. Checkpoints::GuessVerificationProgress(chainActive.Tip()));
  1666. // Check the version of the last 100 blocks to see if we need to upgrade:
  1667. if (!fIsInitialDownload)
  1668. {
  1669. int nUpgraded = 0;
  1670. const CBlockIndex* pindex = chainActive.Tip();
  1671. for (int i = 0; i < 100 && pindex != NULL; i++)
  1672. {
  1673. if (pindex->nVersion > CBlock::CURRENT_VERSION)
  1674. ++nUpgraded;
  1675. pindex = pindex->pprev;
  1676. }
  1677. if (nUpgraded > 0)
  1678. LogPrintf("SetBestChain: %d of last 100 blocks above version %d\n", nUpgraded, CBlock::CURRENT_VERSION);
  1679. if (nUpgraded > 100/2)
  1680. // strMiscWarning is read by GetWarnings(), called by Qt and the JSON-RPC code to warn the user:
  1681. strMiscWarning = _("Warning: This version is obsolete, upgrade required!");
  1682. }
  1683. std::string strCmd = GetArg("-blocknotify", "");
  1684. if (!fIsInitialDownload && !strCmd.empty())
  1685. {
  1686. boost::replace_all(strCmd, "%s", chainActive.Tip()->GetBlockHash().GetHex());
  1687. boost::thread t(runCommand, strCmd); // thread runs free
  1688. }
  1689. return true;
  1690. }
  1691. bool AddToBlockIndex(CBlock& block, CValidationState& state, const CDiskBlockPos& pos)
  1692. {
  1693. // Check for duplicate
  1694. uint256 hash = block.GetHash();
  1695. if (mapBlockIndex.count(hash))
  1696. return state.Invalid(error("AddToBlockIndex() : %s already exists", hash.ToString().c_str()));
  1697. // Construct new block index object
  1698. CBlockIndex* pindexNew = new CBlockIndex(block);
  1699. assert(pindexNew);
  1700. map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
  1701. pindexNew->phashBlock = &((*mi).first);
  1702. map<uint256, CBlockIndex*>::iterator miPrev = mapBlockIndex.find(block.hashPrevBlock);
  1703. if (miPrev != mapBlockIndex.end())
  1704. {
  1705. pindexNew->pprev = (*miPrev).second;
  1706. pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
  1707. }
  1708. pindexNew->nTx = block.vtx.size();
  1709. pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + pindexNew->GetBlockWork().getuint256();
  1710. pindexNew->nChainTx = (pindexNew->pprev ? pindexNew->pprev->nChainTx : 0) + pindexNew->nTx;
  1711. pindexNew->nFile = pos.nFile;
  1712. pindexNew->nDataPos = pos.nPos;
  1713. pindexNew->nUndoPos = 0;
  1714. pindexNew->nStatus = BLOCK_VALID_TRANSACTIONS | BLOCK_HAVE_DATA;
  1715. setBlockIndexValid.insert(pindexNew);
  1716. if (!pblocktree->WriteBlockIndex(CDiskBlockIndex(pindexNew)))
  1717. return state.Abort(_("Failed to write block index"));
  1718. // New best?
  1719. if (!ConnectBestBlock(state))
  1720. return false;
  1721. if (pindexNew == chainActive.Tip())
  1722. {
  1723. // Clear fork warning if its no longer applicable
  1724. CheckForkWarningConditions();
  1725. // Notify UI to display prev block's coinbase if it was ours
  1726. static uint256 hashPrevBestCoinBase;
  1727. g_signals.UpdatedTransaction(hashPrevBestCoinBase);
  1728. hashPrevBestCoinBase = block.GetTxHash(0);
  1729. } else
  1730. CheckForkWarningConditionsOnNewFork(pindexNew);
  1731. if (!pblocktree->Flush())
  1732. return state.Abort(_("Failed to sync block index"));
  1733. uiInterface.NotifyBlocksChanged();
  1734. return true;
  1735. }
  1736. bool FindBlockPos(CValidationState &state, CDiskBlockPos &pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime, bool fKnown = false)
  1737. {
  1738. bool fUpdatedLast = false;
  1739. LOCK(cs_LastBlockFile);
  1740. if (fKnown) {
  1741. if (nLastBlockFile != pos.nFile) {
  1742. nLastBlockFile = pos.nFile;
  1743. infoLastBlockFile.SetNull();
  1744. pblocktree->ReadBlockFileInfo(nLastBlockFile, infoLastBlockFile);
  1745. fUpdatedLast = true;
  1746. }
  1747. } else {
  1748. while (infoLastBlockFile.nSize + nAddSize >= MAX_BLOCKFILE_SIZE) {
  1749. LogPrintf("Leaving block file %i: %s\n", nLastBlockFile, infoLastBlockFile.ToString().c_str());
  1750. FlushBlockFile(true);
  1751. nLastBlockFile++;
  1752. infoLastBlockFile.SetNull();
  1753. pblocktree->ReadBlockFileInfo(nLastBlockFile, infoLastBlockFile); // check whether data for the new file somehow already exist; can fail just fine
  1754. fUpdatedLast = true;
  1755. }
  1756. pos.nFile = nLastBlockFile;
  1757. pos.nPos = infoLastBlockFile.nSize;
  1758. }
  1759. infoLastBlockFile.nSize += nAddSize;
  1760. infoLastBlockFile.AddBlock(nHeight, nTime);
  1761. if (!fKnown) {
  1762. unsigned int nOldChunks = (pos.nPos + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
  1763. unsigned int nNewChunks = (infoLastBlockFile.nSize + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
  1764. if (nNewChunks > nOldChunks) {
  1765. if (CheckDiskSpace(nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos)) {
  1766. FILE *file = OpenBlockFile(pos);
  1767. if (file) {
  1768. LogPrintf("Pre-allocating up to position 0x%x in blk%05u.dat\n", nNewChunks * BLOCKFILE_CHUNK_SIZE, pos.nFile);
  1769. AllocateFileRange(file, pos.nPos, nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos);
  1770. fclose(file);
  1771. }
  1772. }
  1773. else
  1774. return state.Error();
  1775. }
  1776. }
  1777. if (!pblocktree->WriteBlockFileInfo(nLastBlockFile, infoLastBlockFile))
  1778. return state.Abort(_("Failed to write file info"));
  1779. if (fUpdatedLast)
  1780. pblocktree->WriteLastBlockFile(nLastBlockFile);
  1781. return true;
  1782. }
  1783. bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize)
  1784. {
  1785. pos.nFile = nFile;
  1786. LOCK(cs_LastBlockFile);
  1787. unsigned int nNewSize;
  1788. if (nFile == nLastBlockFile) {
  1789. pos.nPos = infoLastBlockFile.nUndoSize;
  1790. nNewSize = (infoLastBlockFile.nUndoSize += nAddSize);
  1791. if (!pblocktree->WriteBlockFileInfo(nLastBlockFile, infoLastBlockFile))
  1792. return state.Abort(_("Failed to write block info"));
  1793. } else {
  1794. CBlockFileInfo info;
  1795. if (!pblocktree->ReadBlockFileInfo(nFile, info))
  1796. return state.Abort(_("Failed to read block info"));
  1797. pos.nPos = info.nUndoSize;
  1798. nNewSize = (info.nUndoSize += nAddSize);
  1799. if (!pblocktree->WriteBlockFileInfo(nFile, info))
  1800. return state.Abort(_("Failed to write block info"));
  1801. }
  1802. unsigned int nOldChunks = (pos.nPos + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
  1803. unsigned int nNewChunks = (nNewSize + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
  1804. if (nNewChunks > nOldChunks) {
  1805. if (CheckDiskSpace(nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos)) {
  1806. FILE *file = OpenUndoFile(pos);
  1807. if (file) {
  1808. LogPrintf("Pre-allocating up to position 0x%x in rev%05u.dat\n", nNewChunks * UNDOFILE_CHUNK_SIZE, pos.nFile);
  1809. AllocateFileRange(file, pos.nPos, nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos);
  1810. fclose(file);
  1811. }
  1812. }
  1813. else
  1814. return state.Error();
  1815. }
  1816. return true;
  1817. }
  1818. bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bool fCheckMerkleRoot)
  1819. {
  1820. // These are checks that are independent of context
  1821. // that can be verified before saving an orphan block.
  1822. // Size limits
  1823. if (block.vtx.empty() || block.vtx.size() > MAX_BLOCK_SIZE || ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION) > MAX_BLOCK_SIZE)
  1824. return state.DoS(100, error("CheckBlock() : size limits failed"));
  1825. // Check proof of work matches claimed amount
  1826. if (fCheckPOW && !CheckProofOfWork(block.GetHash(), block.nBits))
  1827. return state.DoS(50, error("CheckBlock() : proof of work failed"));
  1828. // Check timestamp
  1829. if (block.GetBlockTime() > GetAdjustedTime() + 2 * 60 * 60)
  1830. return state.Invalid(error("CheckBlock() : block timestamp too far in the future"));
  1831. // First transaction must be coinbase, the rest must not be
  1832. if (block.vtx.empty() || !block.vtx[0].IsCoinBase())
  1833. return state.DoS(100, error("CheckBlock() : first tx is not coinbase"));
  1834. for (unsigned int i = 1; i < block.vtx.size(); i++)
  1835. if (block.vtx[i].IsCoinBase())
  1836. return state.DoS(100, error("CheckBlock() : more than one coinbase"));
  1837. // Check transactions
  1838. BOOST_FOREACH(const CTransaction& tx, block.vtx)
  1839. if (!CheckTransaction(tx, state))
  1840. return error("CheckBlock() : CheckTransaction failed");
  1841. // Build the merkle tree already. We need it anyway later, and it makes the
  1842. // block cache the transaction hashes, which means they don't need to be
  1843. // recalculated many times during this block's validation.
  1844. block.BuildMerkleTree();
  1845. // Check for duplicate txids. This is caught by ConnectInputs(),
  1846. // but catching it earlier avoids a potential DoS attack:
  1847. set<uint256> uniqueTx;
  1848. for (unsigned int i = 0; i < block.vtx.size(); i++) {
  1849. uniqueTx.insert(block.GetTxHash(i));
  1850. }
  1851. if (uniqueTx.size() != block.vtx.size())
  1852. return state.DoS(100, error("CheckBlock() : duplicate transaction"), true);
  1853. unsigned int nSigOps = 0;
  1854. BOOST_FOREACH(const CTransaction& tx, block.vtx)
  1855. {
  1856. nSigOps += GetLegacySigOpCount(tx);
  1857. }
  1858. if (nSigOps > MAX_BLOCK_SIGOPS)
  1859. return state.DoS(100, error("CheckBlock() : out-of-bounds SigOpCount"));
  1860. // Check merkle root
  1861. if (fCheckMerkleRoot && block.hashMerkleRoot != block.vMerkleTree.back())
  1862. return state.DoS(100, error("CheckBlock() : hashMerkleRoot mismatch"));
  1863. return true;
  1864. }
  1865. bool AcceptBlock(CBlock& block, CValidationState& state, CDiskBlockPos* dbp)
  1866. {
  1867. // Check for duplicate
  1868. uint256 hash = block.GetHash();
  1869. if (mapBlockIndex.count(hash))
  1870. return state.Invalid(error("AcceptBlock() : block already in mapBlockIndex"));
  1871. // Get prev block index
  1872. CBlockIndex* pindexPrev = NULL;
  1873. int nHeight = 0;
  1874. if (hash != Params().HashGenesisBlock()) {
  1875. map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(block.hashPrevBlock);
  1876. if (mi == mapBlockIndex.end())
  1877. return state.DoS(10, error("AcceptBlock() : prev block not found"));
  1878. pindexPrev = (*mi).second;
  1879. nHeight = pindexPrev->nHeight+1;
  1880. // Check proof of work
  1881. if (block.nBits != GetNextWorkRequired(pindexPrev, &block))
  1882. return state.DoS(100, error("AcceptBlock() : incorrect proof of work"));
  1883. // Check timestamp against prev
  1884. if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast())
  1885. return state.Invalid(error("AcceptBlock() : block's timestamp is too early"));
  1886. // Check that all transactions are finalized
  1887. BOOST_FOREACH(const CTransaction& tx, block.vtx)
  1888. if (!IsFinalTx(tx, nHeight, block.GetBlockTime()))
  1889. return state.DoS(10, error("AcceptBlock() : contains a non-final transaction"));
  1890. // Check that the block chain matches the known block chain up to a checkpoint
  1891. if (!Checkpoints::CheckBlock(nHeight, hash))
  1892. return state.DoS(100, error("AcceptBlock() : rejected by checkpoint lock-in at %d", nHeight));
  1893. // Reject block.nVersion=1 blocks when 95% (75% on testnet) of the network has upgraded:
  1894. if (block.nVersion < 2)
  1895. {
  1896. if ((!TestNet() && CBlockIndex::IsSuperMajority(2, pindexPrev, 950, 1000)) ||
  1897. (TestNet() && CBlockIndex::IsSuperMajority(2, pindexPrev, 75, 100)))
  1898. {
  1899. return state.Invalid(error("AcceptBlock() : rejected nVersion=1 block"));
  1900. }
  1901. }
  1902. // Enforce block.nVersion=2 rule that the coinbase starts with serialized block height
  1903. if (block.nVersion >= 2)
  1904. {
  1905. // if 750 of the last 1,000 blocks are version 2 or greater (51/100 if testnet):
  1906. if ((!TestNet() && CBlockIndex::IsSuperMajority(2, pindexPrev, 750, 1000)) ||
  1907. (TestNet() && CBlockIndex::IsSuperMajority(2, pindexPrev, 51, 100)))
  1908. {
  1909. CScript expect = CScript() << nHeight;
  1910. if (block.vtx[0].vin[0].scriptSig.size() < expect.size() ||
  1911. !std::equal(expect.begin(), expect.end(), block.vtx[0].vin[0].scriptSig.begin()))
  1912. return state.DoS(100, error("AcceptBlock() : block height mismatch in coinbase"));
  1913. }
  1914. }
  1915. }
  1916. // Write block to history file
  1917. try {
  1918. unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
  1919. CDiskBlockPos blockPos;
  1920. if (dbp != NULL)
  1921. blockPos = *dbp;
  1922. if (!FindBlockPos(state, blockPos, nBlockSize+8, nHeight, block.nTime, dbp != NULL))
  1923. return error("AcceptBlock() : FindBlockPos failed");
  1924. if (dbp == NULL)
  1925. if (!WriteBlockToDisk(block, blockPos))
  1926. return state.Abort(_("Failed to write block"));
  1927. if (!AddToBlockIndex(block, state, blockPos))
  1928. return error("AcceptBlock() : AddToBlockIndex failed");
  1929. } catch(std::runtime_error &e) {
  1930. return state.Abort(_("System error: ") + e.what());
  1931. }
  1932. // Relay inventory, but don't relay old inventory during initial block download
  1933. int nBlockEstimate = Checkpoints::GetTotalBlocksEstimate();
  1934. if (chainActive.Tip()->GetBlockHash() == hash)
  1935. {
  1936. LOCK(cs_vNodes);
  1937. BOOST_FOREACH(CNode* pnode, vNodes)
  1938. if (chainActive.Height() > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : nBlockEstimate))
  1939. pnode->PushInventory(CInv(MSG_BLOCK, hash));
  1940. }
  1941. return true;
  1942. }
  1943. bool CBlockIndex::IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned int nRequired, unsigned int nToCheck)
  1944. {
  1945. unsigned int nFound = 0;
  1946. for (unsigned int i = 0; i < nToCheck && nFound < nRequired && pstart != NULL; i++)
  1947. {
  1948. if (pstart->nVersion >= minVersion)
  1949. ++nFound;
  1950. pstart = pstart->pprev;
  1951. }
  1952. return (nFound >= nRequired);
  1953. }
  1954. int64_t CBlockIndex::GetMedianTime() const
  1955. {
  1956. const CBlockIndex* pindex = this;
  1957. for (int i = 0; i < nMedianTimeSpan/2; i++)
  1958. {
  1959. if (!chainActive.Next(pindex))
  1960. return GetBlockTime();
  1961. pindex = chainActive.Next(pindex);
  1962. }
  1963. return pindex->GetMedianTimePast();
  1964. }
  1965. void PushGetBlocks(CNode* pnode, CBlockIndex* pindexBegin, uint256 hashEnd)
  1966. {
  1967. // Filter out duplicate requests
  1968. if (pindexBegin == pnode->pindexLastGetBlocksBegin && hashEnd == pnode->hashLastGetBlocksEnd)
  1969. return;
  1970. pnode->pindexLastGetBlocksBegin = pindexBegin;
  1971. pnode->hashLastGetBlocksEnd = hashEnd;
  1972. pnode->PushMessage("getblocks", chainActive.GetLocator(pindexBegin), hashEnd);
  1973. }
  1974. bool ProcessBlock(CValidationState &state, CNode* pfrom, CBlock* pblock, CDiskBlockPos *dbp)
  1975. {
  1976. // Check for duplicate
  1977. uint256 hash = pblock->GetHash();
  1978. if (mapBlockIndex.count(hash))
  1979. return state.Invalid(error("ProcessBlock() : already have block %d %s", mapBlockIndex[hash]->nHeight, hash.ToString().c_str()));
  1980. if (mapOrphanBlocks.count(hash))
  1981. return state.Invalid(error("ProcessBlock() : already have block (orphan) %s", hash.ToString().c_str()));
  1982. // Preliminary checks
  1983. if (!CheckBlock(*pblock, state))
  1984. return error("ProcessBlock() : CheckBlock FAILED");
  1985. CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(mapBlockIndex);
  1986. if (pcheckpoint && pblock->hashPrevBlock != (chainActive.Tip() ? chainActive.Tip()->GetBlockHash() : uint256(0)))
  1987. {
  1988. // Extra checks to prevent "fill up memory by spamming with bogus blocks"
  1989. int64_t deltaTime = pblock->GetBlockTime() - pcheckpoint->nTime;
  1990. if (deltaTime < 0)
  1991. {
  1992. return state.DoS(100, error("ProcessBlock() : block with timestamp before last checkpoint"));
  1993. }
  1994. CBigNum bnNewBlock;
  1995. bnNewBlock.SetCompact(pblock->nBits);
  1996. CBigNum bnRequired;
  1997. bnRequired.SetCompact(ComputeMinWork(pcheckpoint->nBits, deltaTime));
  1998. if (bnNewBlock > bnRequired)
  1999. {
  2000. return state.DoS(100, error("ProcessBlock() : block with too little proof-of-work"));
  2001. }
  2002. }
  2003. // If we don't already have its previous block, shunt it off to holding area until we get it
  2004. if (pblock->hashPrevBlock != 0 && !mapBlockIndex.count(pblock->hashPrevBlock))
  2005. {
  2006. LogPrintf("ProcessBlock: ORPHAN BLOCK, prev=%s\n", pblock->hashPrevBlock.ToString().c_str());
  2007. // Accept orphans as long as there is a node to request its parents from
  2008. if (pfrom) {
  2009. CBlock* pblock2 = new CBlock(*pblock);
  2010. mapOrphanBlocks.insert(make_pair(hash, pblock2));
  2011. mapOrphanBlocksByPrev.insert(make_pair(pblock2->hashPrevBlock, pblock2));
  2012. // Ask this guy to fill in what we're missing
  2013. PushGetBlocks(pfrom, chainActive.Tip(), GetOrphanRoot(pblock2));
  2014. }
  2015. return true;
  2016. }
  2017. // Store to disk
  2018. if (!AcceptBlock(*pblock, state, dbp))
  2019. return error("ProcessBlock() : AcceptBlock FAILED");
  2020. // Recursively process any orphan blocks that depended on this one
  2021. vector<uint256> vWorkQueue;
  2022. vWorkQueue.push_back(hash);
  2023. for (unsigned int i = 0; i < vWorkQueue.size(); i++)
  2024. {
  2025. uint256 hashPrev = vWorkQueue[i];
  2026. for (multimap<uint256, CBlock*>::iterator mi = mapOrphanBlocksByPrev.lower_bound(hashPrev);
  2027. mi != mapOrphanBlocksByPrev.upper_bound(hashPrev);
  2028. ++mi)
  2029. {
  2030. CBlock* pblockOrphan = (*mi).second;
  2031. // Use a dummy CValidationState so someone can't setup nodes to counter-DoS based on orphan resolution (that is, feeding people an invalid block based on LegitBlockX in order to get anyone relaying LegitBlockX banned)
  2032. CValidationState stateDummy;
  2033. if (AcceptBlock(*pblockOrphan, stateDummy))
  2034. vWorkQueue.push_back(pblockOrphan->GetHash());
  2035. mapOrphanBlocks.erase(pblockOrphan->GetHash());
  2036. delete pblockOrphan;
  2037. }
  2038. mapOrphanBlocksByPrev.erase(hashPrev);
  2039. }
  2040. LogPrintf("ProcessBlock: ACCEPTED\n");
  2041. return true;
  2042. }
  2043. CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter& filter)
  2044. {
  2045. header = block.GetBlockHeader();
  2046. vector<bool> vMatch;
  2047. vector<uint256> vHashes;
  2048. vMatch.reserve(block.vtx.size());
  2049. vHashes.reserve(block.vtx.size());
  2050. for (unsigned int i = 0; i < block.vtx.size(); i++)
  2051. {
  2052. uint256 hash = block.vtx[i].GetHash();
  2053. if (filter.IsRelevantAndUpdate(block.vtx[i], hash))
  2054. {
  2055. vMatch.push_back(true);
  2056. vMatchedTxn.push_back(make_pair(i, hash));
  2057. }
  2058. else
  2059. vMatch.push_back(false);
  2060. vHashes.push_back(hash);
  2061. }
  2062. txn = CPartialMerkleTree(vHashes, vMatch);
  2063. }
  2064. uint256 CPartialMerkleTree::CalcHash(int height, unsigned int pos, const std::vector<uint256> &vTxid) {
  2065. if (height == 0) {
  2066. // hash at height 0 is the txids themself
  2067. return vTxid[pos];
  2068. } else {
  2069. // calculate left hash
  2070. uint256 left = CalcHash(height-1, pos*2, vTxid), right;
  2071. // calculate right hash if not beyong the end of the array - copy left hash otherwise1
  2072. if (pos*2+1 < CalcTreeWidth(height-1))
  2073. right = CalcHash(height-1, pos*2+1, vTxid);
  2074. else
  2075. right = left;
  2076. // combine subhashes
  2077. return Hash(BEGIN(left), END(left), BEGIN(right), END(right));
  2078. }
  2079. }
  2080. void CPartialMerkleTree::TraverseAndBuild(int height, unsigned int pos, const std::vector<uint256> &vTxid, const std::vector<bool> &vMatch) {
  2081. // determine whether this node is the parent of at least one matched txid
  2082. bool fParentOfMatch = false;
  2083. for (unsigned int p = pos << height; p < (pos+1) << height && p < nTransactions; p++)
  2084. fParentOfMatch |= vMatch[p];
  2085. // store as flag bit
  2086. vBits.push_back(fParentOfMatch);
  2087. if (height==0 || !fParentOfMatch) {
  2088. // if at height 0, or nothing interesting below, store hash and stop
  2089. vHash.push_back(CalcHash(height, pos, vTxid));
  2090. } else {
  2091. // otherwise, don't store any hash, but descend into the subtrees
  2092. TraverseAndBuild(height-1, pos*2, vTxid, vMatch);
  2093. if (pos*2+1 < CalcTreeWidth(height-1))
  2094. TraverseAndBuild(height-1, pos*2+1, vTxid, vMatch);
  2095. }
  2096. }
  2097. uint256 CPartialMerkleTree::TraverseAndExtract(int height, unsigned int pos, unsigned int &nBitsUsed, unsigned int &nHashUsed, std::vector<uint256> &vMatch) {
  2098. if (nBitsUsed >= vBits.size()) {
  2099. // overflowed the bits array - failure
  2100. fBad = true;
  2101. return 0;
  2102. }
  2103. bool fParentOfMatch = vBits[nBitsUsed++];
  2104. if (height==0 || !fParentOfMatch) {
  2105. // if at height 0, or nothing interesting below, use stored hash and do not descend
  2106. if (nHashUsed >= vHash.size()) {
  2107. // overflowed the hash array - failure
  2108. fBad = true;
  2109. return 0;
  2110. }
  2111. const uint256 &hash = vHash[nHashUsed++];
  2112. if (height==0 && fParentOfMatch) // in case of height 0, we have a matched txid
  2113. vMatch.push_back(hash);
  2114. return hash;
  2115. } else {
  2116. // otherwise, descend into the subtrees to extract matched txids and hashes
  2117. uint256 left = TraverseAndExtract(height-1, pos*2, nBitsUsed, nHashUsed, vMatch), right;
  2118. if (pos*2+1 < CalcTreeWidth(height-1))
  2119. right = TraverseAndExtract(height-1, pos*2+1, nBitsUsed, nHashUsed, vMatch);
  2120. else
  2121. right = left;
  2122. // and combine them before returning
  2123. return Hash(BEGIN(left), END(left), BEGIN(right), END(right));
  2124. }
  2125. }
  2126. CPartialMerkleTree::CPartialMerkleTree(const std::vector<uint256> &vTxid, const std::vector<bool> &vMatch) : nTransactions(vTxid.size()), fBad(false) {
  2127. // reset state
  2128. vBits.clear();
  2129. vHash.clear();
  2130. // calculate height of tree
  2131. int nHeight = 0;
  2132. while (CalcTreeWidth(nHeight) > 1)
  2133. nHeight++;
  2134. // traverse the partial tree
  2135. TraverseAndBuild(nHeight, 0, vTxid, vMatch);
  2136. }
  2137. CPartialMerkleTree::CPartialMerkleTree() : nTransactions(0), fBad(true) {}
  2138. uint256 CPartialMerkleTree::ExtractMatches(std::vector<uint256> &vMatch) {
  2139. vMatch.clear();
  2140. // An empty set will not work
  2141. if (nTransactions == 0)
  2142. return 0;
  2143. // check for excessively high numbers of transactions
  2144. if (nTransactions > MAX_BLOCK_SIZE / 60) // 60 is the lower bound for the size of a serialized CTransaction
  2145. return 0;
  2146. // there can never be more hashes provided than one for every txid
  2147. if (vHash.size() > nTransactions)
  2148. return 0;
  2149. // there must be at least one bit per node in the partial tree, and at least one node per hash
  2150. if (vBits.size() < vHash.size())
  2151. return 0;
  2152. // calculate height of tree
  2153. int nHeight = 0;
  2154. while (CalcTreeWidth(nHeight) > 1)
  2155. nHeight++;
  2156. // traverse the partial tree
  2157. unsigned int nBitsUsed = 0, nHashUsed = 0;
  2158. uint256 hashMerkleRoot = TraverseAndExtract(nHeight, 0, nBitsUsed, nHashUsed, vMatch);
  2159. // verify that no problems occured during the tree traversal
  2160. if (fBad)
  2161. return 0;
  2162. // verify that all bits were consumed (except for the padding caused by serializing it as a byte sequence)
  2163. if ((nBitsUsed+7)/8 != (vBits.size()+7)/8)
  2164. return 0;
  2165. // verify that all hashes were consumed
  2166. if (nHashUsed != vHash.size())
  2167. return 0;
  2168. return hashMerkleRoot;
  2169. }
  2170. bool AbortNode(const std::string &strMessage) {
  2171. strMiscWarning = strMessage;
  2172. LogPrintf("*** %s\n", strMessage.c_str());
  2173. uiInterface.ThreadSafeMessageBox(strMessage, "", CClientUIInterface::MSG_ERROR);
  2174. StartShutdown();
  2175. return false;
  2176. }
  2177. bool CheckDiskSpace(uint64_t nAdditionalBytes)
  2178. {
  2179. uint64_t nFreeBytesAvailable = filesystem::space(GetDataDir()).available;
  2180. // Check for nMinDiskSpace bytes (currently 50MB)
  2181. if (nFreeBytesAvailable < nMinDiskSpace + nAdditionalBytes)
  2182. return AbortNode(_("Error: Disk space is low!"));
  2183. return true;
  2184. }
  2185. FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly)
  2186. {
  2187. if (pos.IsNull())
  2188. return NULL;
  2189. boost::filesystem::path path = GetDataDir() / "blocks" / strprintf("%s%05u.dat", prefix, pos.nFile);
  2190. boost::filesystem::create_directories(path.parent_path());
  2191. FILE* file = fopen(path.string().c_str(), "rb+");
  2192. if (!file && !fReadOnly)
  2193. file = fopen(path.string().c_str(), "wb+");
  2194. if (!file) {
  2195. LogPrintf("Unable to open file %s\n", path.string().c_str());
  2196. return NULL;
  2197. }
  2198. if (pos.nPos) {
  2199. if (fseek(file, pos.nPos, SEEK_SET)) {
  2200. LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, path.string().c_str());
  2201. fclose(file);
  2202. return NULL;
  2203. }
  2204. }
  2205. return file;
  2206. }
  2207. FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly) {
  2208. return OpenDiskFile(pos, "blk", fReadOnly);
  2209. }
  2210. FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly) {
  2211. return OpenDiskFile(pos, "rev", fReadOnly);
  2212. }
  2213. CBlockIndex * InsertBlockIndex(uint256 hash)
  2214. {
  2215. if (hash == 0)
  2216. return NULL;
  2217. // Return existing
  2218. map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
  2219. if (mi != mapBlockIndex.end())
  2220. return (*mi).second;
  2221. // Create new
  2222. CBlockIndex* pindexNew = new CBlockIndex();
  2223. if (!pindexNew)
  2224. throw runtime_error("LoadBlockIndex() : new CBlockIndex failed");
  2225. mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
  2226. pindexNew->phashBlock = &((*mi).first);
  2227. return pindexNew;
  2228. }
  2229. bool static LoadBlockIndexDB()
  2230. {
  2231. if (!pblocktree->LoadBlockIndexGuts())
  2232. return false;
  2233. boost::this_thread::interruption_point();
  2234. // Calculate nChainWork
  2235. vector<pair<int, CBlockIndex*> > vSortedByHeight;
  2236. vSortedByHeight.reserve(mapBlockIndex.size());
  2237. BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
  2238. {
  2239. CBlockIndex* pindex = item.second;
  2240. vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex));
  2241. }
  2242. sort(vSortedByHeight.begin(), vSortedByHeight.end());
  2243. BOOST_FOREACH(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
  2244. {
  2245. CBlockIndex* pindex = item.second;
  2246. pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + pindex->GetBlockWork().getuint256();
  2247. pindex->nChainTx = (pindex->pprev ? pindex->pprev->nChainTx : 0) + pindex->nTx;
  2248. if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TRANSACTIONS && !(pindex->nStatus & BLOCK_FAILED_MASK))
  2249. setBlockIndexValid.insert(pindex);
  2250. if (pindex->nStatus & BLOCK_FAILED_MASK && (!pindexBestInvalid || pindex->nChainWork > pindexBestInvalid->nChainWork))
  2251. pindexBestInvalid = pindex;
  2252. }
  2253. // Load block file info
  2254. pblocktree->ReadLastBlockFile(nLastBlockFile);
  2255. LogPrintf("LoadBlockIndexDB(): last block file = %i\n", nLastBlockFile);
  2256. if (pblocktree->ReadBlockFileInfo(nLastBlockFile, infoLastBlockFile))
  2257. LogPrintf("LoadBlockIndexDB(): last block file info: %s\n", infoLastBlockFile.ToString().c_str());
  2258. // Check whether we need to continue reindexing
  2259. bool fReindexing = false;
  2260. pblocktree->ReadReindexing(fReindexing);
  2261. fReindex |= fReindexing;
  2262. // Check whether we have a transaction index
  2263. pblocktree->ReadFlag("txindex", fTxIndex);
  2264. LogPrintf("LoadBlockIndexDB(): transaction index %s\n", fTxIndex ? "enabled" : "disabled");
  2265. // Load pointer to end of best chain
  2266. chainActive.SetTip(pcoinsTip->GetBestBlock());
  2267. if (chainActive.Tip() == NULL)
  2268. return true;
  2269. LogPrintf("LoadBlockIndexDB(): hashBestChain=%s height=%d date=%s\n",
  2270. chainActive.Tip()->GetBlockHash().ToString().c_str(), chainActive.Height(),
  2271. DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()).c_str());
  2272. return true;
  2273. }
  2274. bool VerifyDB(int nCheckLevel, int nCheckDepth)
  2275. {
  2276. if (chainActive.Tip() == NULL || chainActive.Tip()->pprev == NULL)
  2277. return true;
  2278. // Verify blocks in the best chain
  2279. if (nCheckDepth <= 0)
  2280. nCheckDepth = 1000000000; // suffices until the year 19000
  2281. if (nCheckDepth > chainActive.Height())
  2282. nCheckDepth = chainActive.Height();
  2283. nCheckLevel = std::max(0, std::min(4, nCheckLevel));
  2284. LogPrintf("Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel);
  2285. CCoinsViewCache coins(*pcoinsTip, true);
  2286. CBlockIndex* pindexState = chainActive.Tip();
  2287. CBlockIndex* pindexFailure = NULL;
  2288. int nGoodTransactions = 0;
  2289. CValidationState state;
  2290. for (CBlockIndex* pindex = chainActive.Tip(); pindex && pindex->pprev; pindex = pindex->pprev)
  2291. {
  2292. boost::this_thread::interruption_point();
  2293. if (pindex->nHeight < chainActive.Height()-nCheckDepth)
  2294. break;
  2295. CBlock block;
  2296. // check level 0: read from disk
  2297. if (!ReadBlockFromDisk(block, pindex))
  2298. return error("VerifyDB() : *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString().c_str());
  2299. // check level 1: verify block validity
  2300. if (nCheckLevel >= 1 && !CheckBlock(block, state))
  2301. return error("VerifyDB() : *** found bad block at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString().c_str());
  2302. // check level 2: verify undo validity
  2303. if (nCheckLevel >= 2 && pindex) {
  2304. CBlockUndo undo;
  2305. CDiskBlockPos pos = pindex->GetUndoPos();
  2306. if (!pos.IsNull()) {
  2307. if (!undo.ReadFromDisk(pos, pindex->pprev->GetBlockHash()))
  2308. return error("VerifyDB() : *** found bad undo data at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString().c_str());
  2309. }
  2310. }
  2311. // check level 3: check for inconsistencies during memory-only disconnect of tip blocks
  2312. if (nCheckLevel >= 3 && pindex == pindexState && (coins.GetCacheSize() + pcoinsTip->GetCacheSize()) <= 2*nCoinCacheSize + 32000) {
  2313. bool fClean = true;
  2314. if (!DisconnectBlock(block, state, pindex, coins, &fClean))
  2315. return error("VerifyDB() : *** irrecoverable inconsistency in block data at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString().c_str());
  2316. pindexState = pindex->pprev;
  2317. if (!fClean) {
  2318. nGoodTransactions = 0;
  2319. pindexFailure = pindex;
  2320. } else
  2321. nGoodTransactions += block.vtx.size();
  2322. }
  2323. }
  2324. if (pindexFailure)
  2325. return error("VerifyDB() : *** coin database inconsistencies found (last %i blocks, %i good transactions before that)\n", chainActive.Height() - pindexFailure->nHeight + 1, nGoodTransactions);
  2326. // check level 4: try reconnecting blocks
  2327. if (nCheckLevel >= 4) {
  2328. CBlockIndex *pindex = pindexState;
  2329. while (pindex != chainActive.Tip()) {
  2330. boost::this_thread::interruption_point();
  2331. pindex = chainActive.Next(pindex);
  2332. CBlock block;
  2333. if (!ReadBlockFromDisk(block, pindex))
  2334. return error("VerifyDB() : *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString().c_str());
  2335. if (!ConnectBlock(block, state, pindex, coins))
  2336. return error("VerifyDB() : *** found unconnectable block at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString().c_str());
  2337. }
  2338. }
  2339. LogPrintf("No coin database inconsistencies in last %i blocks (%i transactions)\n", chainActive.Height() - pindexState->nHeight, nGoodTransactions);
  2340. return true;
  2341. }
  2342. void UnloadBlockIndex()
  2343. {
  2344. mapBlockIndex.clear();
  2345. setBlockIndexValid.clear();
  2346. chainActive.SetTip(NULL);
  2347. pindexBestInvalid = NULL;
  2348. }
  2349. bool LoadBlockIndex()
  2350. {
  2351. // Load block index from databases
  2352. if (!fReindex && !LoadBlockIndexDB())
  2353. return false;
  2354. return true;
  2355. }
  2356. bool InitBlockIndex() {
  2357. // Check whether we're already initialized
  2358. if (chainActive.Genesis() != NULL)
  2359. return true;
  2360. // Use the provided setting for -txindex in the new database
  2361. fTxIndex = GetBoolArg("-txindex", false);
  2362. pblocktree->WriteFlag("txindex", fTxIndex);
  2363. LogPrintf("Initializing databases...\n");
  2364. // Only add the genesis block if not reindexing (in which case we reuse the one already on disk)
  2365. if (!fReindex) {
  2366. try {
  2367. CBlock &block = const_cast<CBlock&>(Params().GenesisBlock());
  2368. // Start new block file
  2369. unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
  2370. CDiskBlockPos blockPos;
  2371. CValidationState state;
  2372. if (!FindBlockPos(state, blockPos, nBlockSize+8, 0, block.nTime))
  2373. return error("LoadBlockIndex() : FindBlockPos failed");
  2374. if (!WriteBlockToDisk(block, blockPos))
  2375. return error("LoadBlockIndex() : writing genesis block to disk failed");
  2376. if (!AddToBlockIndex(block, state, blockPos))
  2377. return error("LoadBlockIndex() : genesis block not accepted");
  2378. } catch(std::runtime_error &e) {
  2379. return error("LoadBlockIndex() : failed to initialize block database: %s", e.what());
  2380. }
  2381. }
  2382. return true;
  2383. }
  2384. void PrintBlockTree()
  2385. {
  2386. // pre-compute tree structure
  2387. map<CBlockIndex*, vector<CBlockIndex*> > mapNext;
  2388. for (map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.begin(); mi != mapBlockIndex.end(); ++mi)
  2389. {
  2390. CBlockIndex* pindex = (*mi).second;
  2391. mapNext[pindex->pprev].push_back(pindex);
  2392. // test
  2393. //while (rand() % 3 == 0)
  2394. // mapNext[pindex->pprev].push_back(pindex);
  2395. }
  2396. vector<pair<int, CBlockIndex*> > vStack;
  2397. vStack.push_back(make_pair(0, chainActive.Genesis()));
  2398. int nPrevCol = 0;
  2399. while (!vStack.empty())
  2400. {
  2401. int nCol = vStack.back().first;
  2402. CBlockIndex* pindex = vStack.back().second;
  2403. vStack.pop_back();
  2404. // print split or gap
  2405. if (nCol > nPrevCol)
  2406. {
  2407. for (int i = 0; i < nCol-1; i++)
  2408. LogPrintf("| ");
  2409. LogPrintf("|\\\n");
  2410. }
  2411. else if (nCol < nPrevCol)
  2412. {
  2413. for (int i = 0; i < nCol; i++)
  2414. LogPrintf("| ");
  2415. LogPrintf("|\n");
  2416. }
  2417. nPrevCol = nCol;
  2418. // print columns
  2419. for (int i = 0; i < nCol; i++)
  2420. LogPrintf("| ");
  2421. // print item
  2422. CBlock block;
  2423. ReadBlockFromDisk(block, pindex);
  2424. LogPrintf("%d (blk%05u.dat:0x%x) %s tx %"PRIszu"",
  2425. pindex->nHeight,
  2426. pindex->GetBlockPos().nFile, pindex->GetBlockPos().nPos,
  2427. DateTimeStrFormat("%Y-%m-%d %H:%M:%S", block.GetBlockTime()).c_str(),
  2428. block.vtx.size());
  2429. // put the main time-chain first
  2430. vector<CBlockIndex*>& vNext = mapNext[pindex];
  2431. for (unsigned int i = 0; i < vNext.size(); i++)
  2432. {
  2433. if (chainActive.Next(vNext[i]))
  2434. {
  2435. swap(vNext[0], vNext[i]);
  2436. break;
  2437. }
  2438. }
  2439. // iterate children
  2440. for (unsigned int i = 0; i < vNext.size(); i++)
  2441. vStack.push_back(make_pair(nCol+i, vNext[i]));
  2442. }
  2443. }
  2444. bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp)
  2445. {
  2446. int64_t nStart = GetTimeMillis();
  2447. int nLoaded = 0;
  2448. try {
  2449. CBufferedFile blkdat(fileIn, 2*MAX_BLOCK_SIZE, MAX_BLOCK_SIZE+8, SER_DISK, CLIENT_VERSION);
  2450. uint64_t nStartByte = 0;
  2451. if (dbp) {
  2452. // (try to) skip already indexed part
  2453. CBlockFileInfo info;
  2454. if (pblocktree->ReadBlockFileInfo(dbp->nFile, info)) {
  2455. nStartByte = info.nSize;
  2456. blkdat.Seek(info.nSize);
  2457. }
  2458. }
  2459. uint64_t nRewind = blkdat.GetPos();
  2460. while (blkdat.good() && !blkdat.eof()) {
  2461. boost::this_thread::interruption_point();
  2462. blkdat.SetPos(nRewind);
  2463. nRewind++; // start one byte further next time, in case of failure
  2464. blkdat.SetLimit(); // remove former limit
  2465. unsigned int nSize = 0;
  2466. try {
  2467. // locate a header
  2468. unsigned char buf[4];
  2469. blkdat.FindByte(Params().MessageStart()[0]);
  2470. nRewind = blkdat.GetPos()+1;
  2471. blkdat >> FLATDATA(buf);
  2472. if (memcmp(buf, Params().MessageStart(), 4))
  2473. continue;
  2474. // read size
  2475. blkdat >> nSize;
  2476. if (nSize < 80 || nSize > MAX_BLOCK_SIZE)
  2477. continue;
  2478. } catch (std::exception &e) {
  2479. // no valid block header found; don't complain
  2480. break;
  2481. }
  2482. try {
  2483. // read block
  2484. uint64_t nBlockPos = blkdat.GetPos();
  2485. blkdat.SetLimit(nBlockPos + nSize);
  2486. CBlock block;
  2487. blkdat >> block;
  2488. nRewind = blkdat.GetPos();
  2489. // process block
  2490. if (nBlockPos >= nStartByte) {
  2491. LOCK(cs_main);
  2492. if (dbp)
  2493. dbp->nPos = nBlockPos;
  2494. CValidationState state;
  2495. if (ProcessBlock(state, NULL, &block, dbp))
  2496. nLoaded++;
  2497. if (state.IsError())
  2498. break;
  2499. }
  2500. } catch (std::exception &e) {
  2501. LogPrintf("%s() : Deserialize or I/O error caught during load\n", __PRETTY_FUNCTION__);
  2502. }
  2503. }
  2504. fclose(fileIn);
  2505. } catch(std::runtime_error &e) {
  2506. AbortNode(_("Error: system error: ") + e.what());
  2507. }
  2508. if (nLoaded > 0)
  2509. LogPrintf("Loaded %i blocks from external file in %"PRId64"ms\n", nLoaded, GetTimeMillis() - nStart);
  2510. return nLoaded > 0;
  2511. }
  2512. //////////////////////////////////////////////////////////////////////////////
  2513. //
  2514. // CAlert
  2515. //
  2516. string GetWarnings(string strFor)
  2517. {
  2518. int nPriority = 0;
  2519. string strStatusBar;
  2520. string strRPC;
  2521. if (GetBoolArg("-testsafemode", false))
  2522. strRPC = "test";
  2523. if (!CLIENT_VERSION_IS_RELEASE)
  2524. strStatusBar = _("This is a pre-release test build - use at your own risk - do not use for mining or merchant applications");
  2525. // Misc warnings like out of disk space and clock is wrong
  2526. if (strMiscWarning != "")
  2527. {
  2528. nPriority = 1000;
  2529. strStatusBar = strMiscWarning;
  2530. }
  2531. if (fLargeWorkForkFound)
  2532. {
  2533. nPriority = 2000;
  2534. strStatusBar = strRPC = _("Warning: The network does not appear to fully agree! Some miners appear to be experiencing issues.");
  2535. }
  2536. else if (fLargeWorkInvalidChainFound)
  2537. {
  2538. nPriority = 2000;
  2539. strStatusBar = strRPC = _("Warning: We do not appear to fully agree with our peers! You may need to upgrade, or other nodes may need to upgrade.");
  2540. }
  2541. // Alerts
  2542. {
  2543. LOCK(cs_mapAlerts);
  2544. BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts)
  2545. {
  2546. const CAlert& alert = item.second;
  2547. if (alert.AppliesToMe() && alert.nPriority > nPriority)
  2548. {
  2549. nPriority = alert.nPriority;
  2550. strStatusBar = alert.strStatusBar;
  2551. }
  2552. }
  2553. }
  2554. if (strFor == "statusbar")
  2555. return strStatusBar;
  2556. else if (strFor == "rpc")
  2557. return strRPC;
  2558. assert(!"GetWarnings() : invalid parameter");
  2559. return "error";
  2560. }
  2561. //////////////////////////////////////////////////////////////////////////////
  2562. //
  2563. // Messages
  2564. //
  2565. bool static AlreadyHave(const CInv& inv)
  2566. {
  2567. switch (inv.type)
  2568. {
  2569. case MSG_TX:
  2570. {
  2571. bool txInMap = false;
  2572. txInMap = mempool.exists(inv.hash);
  2573. return txInMap || mapOrphanTransactions.count(inv.hash) ||
  2574. pcoinsTip->HaveCoins(inv.hash);
  2575. }
  2576. case MSG_BLOCK:
  2577. return mapBlockIndex.count(inv.hash) ||
  2578. mapOrphanBlocks.count(inv.hash);
  2579. }
  2580. // Don't know what it is, just say we already got one
  2581. return true;
  2582. }
  2583. void static ProcessGetData(CNode* pfrom)
  2584. {
  2585. std::deque<CInv>::iterator it = pfrom->vRecvGetData.begin();
  2586. vector<CInv> vNotFound;
  2587. LOCK(cs_main);
  2588. while (it != pfrom->vRecvGetData.end()) {
  2589. // Don't bother if send buffer is too full to respond anyway
  2590. if (pfrom->nSendSize >= SendBufferSize())
  2591. break;
  2592. const CInv &inv = *it;
  2593. {
  2594. boost::this_thread::interruption_point();
  2595. it++;
  2596. if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK)
  2597. {
  2598. // Send block from disk
  2599. map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(inv.hash);
  2600. if (mi != mapBlockIndex.end())
  2601. {
  2602. CBlock block;
  2603. ReadBlockFromDisk(block, (*mi).second);
  2604. if (inv.type == MSG_BLOCK)
  2605. pfrom->PushMessage("block", block);
  2606. else // MSG_FILTERED_BLOCK)
  2607. {
  2608. LOCK(pfrom->cs_filter);
  2609. if (pfrom->pfilter)
  2610. {
  2611. CMerkleBlock merkleBlock(block, *pfrom->pfilter);
  2612. pfrom->PushMessage("merkleblock", merkleBlock);
  2613. // CMerkleBlock just contains hashes, so also push any transactions in the block the client did not see
  2614. // This avoids hurting performance by pointlessly requiring a round-trip
  2615. // Note that there is currently no way for a node to request any single transactions we didnt send here -
  2616. // they must either disconnect and retry or request the full block.
  2617. // Thus, the protocol spec specified allows for us to provide duplicate txn here,
  2618. // however we MUST always provide at least what the remote peer needs
  2619. typedef std::pair<unsigned int, uint256> PairType;
  2620. BOOST_FOREACH(PairType& pair, merkleBlock.vMatchedTxn)
  2621. if (!pfrom->setInventoryKnown.count(CInv(MSG_TX, pair.second)))
  2622. pfrom->PushMessage("tx", block.vtx[pair.first]);
  2623. }
  2624. // else
  2625. // no response
  2626. }
  2627. // Trigger them to send a getblocks request for the next batch of inventory
  2628. if (inv.hash == pfrom->hashContinue)
  2629. {
  2630. // Bypass PushInventory, this must send even if redundant,
  2631. // and we want it right after the last block so they don't
  2632. // wait for other stuff first.
  2633. vector<CInv> vInv;
  2634. vInv.push_back(CInv(MSG_BLOCK, chainActive.Tip()->GetBlockHash()));
  2635. pfrom->PushMessage("inv", vInv);
  2636. pfrom->hashContinue = 0;
  2637. }
  2638. }
  2639. }
  2640. else if (inv.IsKnownType())
  2641. {
  2642. // Send stream from relay memory
  2643. bool pushed = false;
  2644. {
  2645. LOCK(cs_mapRelay);
  2646. map<CInv, CDataStream>::iterator mi = mapRelay.find(inv);
  2647. if (mi != mapRelay.end()) {
  2648. pfrom->PushMessage(inv.GetCommand(), (*mi).second);
  2649. pushed = true;
  2650. }
  2651. }
  2652. if (!pushed && inv.type == MSG_TX) {
  2653. CTransaction tx;
  2654. if (mempool.lookup(inv.hash, tx)) {
  2655. CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
  2656. ss.reserve(1000);
  2657. ss << tx;
  2658. pfrom->PushMessage("tx", ss);
  2659. pushed = true;
  2660. }
  2661. }
  2662. if (!pushed) {
  2663. vNotFound.push_back(inv);
  2664. }
  2665. }
  2666. // Track requests for our stuff.
  2667. g_signals.Inventory(inv.hash);
  2668. if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK)
  2669. break;
  2670. }
  2671. }
  2672. pfrom->vRecvGetData.erase(pfrom->vRecvGetData.begin(), it);
  2673. if (!vNotFound.empty()) {
  2674. // Let the peer know that we didn't find what it asked for, so it doesn't
  2675. // have to wait around forever. Currently only SPV clients actually care
  2676. // about this message: it's needed when they are recursively walking the
  2677. // dependencies of relevant unconfirmed transactions. SPV clients want to
  2678. // do that because they want to know about (and store and rebroadcast and
  2679. // risk analyze) the dependencies of transactions relevant to them, without
  2680. // having to download the entire memory pool.
  2681. pfrom->PushMessage("notfound", vNotFound);
  2682. }
  2683. }
  2684. bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
  2685. {
  2686. RandAddSeedPerfmon();
  2687. LogPrint("net", "received: %s (%"PRIszu" bytes)\n", strCommand.c_str(), vRecv.size());
  2688. if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0)
  2689. {
  2690. LogPrintf("dropmessagestest DROPPING RECV MESSAGE\n");
  2691. return true;
  2692. }
  2693. if (strCommand == "version")
  2694. {
  2695. // Each connection can only send one version message
  2696. if (pfrom->nVersion != 0)
  2697. {
  2698. pfrom->Misbehaving(1);
  2699. return false;
  2700. }
  2701. int64_t nTime;
  2702. CAddress addrMe;
  2703. CAddress addrFrom;
  2704. uint64_t nNonce = 1;
  2705. vRecv >> pfrom->nVersion >> pfrom->nServices >> nTime >> addrMe;
  2706. if (pfrom->nVersion < MIN_PEER_PROTO_VERSION)
  2707. {
  2708. // disconnect from peers older than this proto version
  2709. LogPrintf("partner %s using obsolete version %i; disconnecting\n", pfrom->addr.ToString().c_str(), pfrom->nVersion);
  2710. pfrom->fDisconnect = true;
  2711. return false;
  2712. }
  2713. if (pfrom->nVersion == 10300)
  2714. pfrom->nVersion = 300;
  2715. if (!vRecv.empty())
  2716. vRecv >> addrFrom >> nNonce;
  2717. if (!vRecv.empty())
  2718. vRecv >> pfrom->strSubVer;
  2719. if (!vRecv.empty())
  2720. vRecv >> pfrom->nStartingHeight;
  2721. if (!vRecv.empty())
  2722. vRecv >> pfrom->fRelayTxes; // set to true after we get the first filter* message
  2723. else
  2724. pfrom->fRelayTxes = true;
  2725. if (pfrom->fInbound && addrMe.IsRoutable())
  2726. {
  2727. pfrom->addrLocal = addrMe;
  2728. SeenLocal(addrMe);
  2729. }
  2730. // Disconnect if we connected to ourself
  2731. if (nNonce == nLocalHostNonce && nNonce > 1)
  2732. {
  2733. LogPrintf("connected to self at %s, disconnecting\n", pfrom->addr.ToString().c_str());
  2734. pfrom->fDisconnect = true;
  2735. return true;
  2736. }
  2737. // Be shy and don't send version until we hear
  2738. if (pfrom->fInbound)
  2739. pfrom->PushVersion();
  2740. pfrom->fClient = !(pfrom->nServices & NODE_NETWORK);
  2741. // Change version
  2742. pfrom->PushMessage("verack");
  2743. pfrom->ssSend.SetVersion(min(pfrom->nVersion, PROTOCOL_VERSION));
  2744. if (!pfrom->fInbound)
  2745. {
  2746. // Advertise our address
  2747. if (!fNoListen && !IsInitialBlockDownload())
  2748. {
  2749. CAddress addr = GetLocalAddress(&pfrom->addr);
  2750. if (addr.IsRoutable())
  2751. pfrom->PushAddress(addr);
  2752. }
  2753. // Get recent addresses
  2754. if (pfrom->fOneShot || pfrom->nVersion >= CADDR_TIME_VERSION || addrman.size() < 1000)
  2755. {
  2756. pfrom->PushMessage("getaddr");
  2757. pfrom->fGetAddr = true;
  2758. }
  2759. addrman.Good(pfrom->addr);
  2760. } else {
  2761. if (((CNetAddr)pfrom->addr) == (CNetAddr)addrFrom)
  2762. {
  2763. addrman.Add(addrFrom, addrFrom);
  2764. addrman.Good(addrFrom);
  2765. }
  2766. }
  2767. // Relay alerts
  2768. {
  2769. LOCK(cs_mapAlerts);
  2770. BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts)
  2771. item.second.RelayTo(pfrom);
  2772. }
  2773. pfrom->fSuccessfullyConnected = true;
  2774. LogPrintf("receive version message: version %d, blocks=%d, us=%s, them=%s, peer=%s\n", pfrom->nVersion, pfrom->nStartingHeight, addrMe.ToString().c_str(), addrFrom.ToString().c_str(), pfrom->addr.ToString().c_str());
  2775. AddTimeData(pfrom->addr, nTime);
  2776. LOCK(cs_main);
  2777. cPeerBlockCounts.input(pfrom->nStartingHeight);
  2778. }
  2779. else if (pfrom->nVersion == 0)
  2780. {
  2781. // Must have a version message before anything else
  2782. pfrom->Misbehaving(1);
  2783. return false;
  2784. }
  2785. else if (strCommand == "verack")
  2786. {
  2787. pfrom->SetRecvVersion(min(pfrom->nVersion, PROTOCOL_VERSION));
  2788. }
  2789. else if (strCommand == "addr")
  2790. {
  2791. vector<CAddress> vAddr;
  2792. vRecv >> vAddr;
  2793. // Don't want addr from older versions unless seeding
  2794. if (pfrom->nVersion < CADDR_TIME_VERSION && addrman.size() > 1000)
  2795. return true;
  2796. if (vAddr.size() > 1000)
  2797. {
  2798. pfrom->Misbehaving(20);
  2799. return error("message addr size() = %"PRIszu"", vAddr.size());
  2800. }
  2801. // Store the new addresses
  2802. vector<CAddress> vAddrOk;
  2803. int64_t nNow = GetAdjustedTime();
  2804. int64_t nSince = nNow - 10 * 60;
  2805. BOOST_FOREACH(CAddress& addr, vAddr)
  2806. {
  2807. boost::this_thread::interruption_point();
  2808. if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60)
  2809. addr.nTime = nNow - 5 * 24 * 60 * 60;
  2810. pfrom->AddAddressKnown(addr);
  2811. bool fReachable = IsReachable(addr);
  2812. if (addr.nTime > nSince && !pfrom->fGetAddr && vAddr.size() <= 10 && addr.IsRoutable())
  2813. {
  2814. // Relay to a limited number of other nodes
  2815. {
  2816. LOCK(cs_vNodes);
  2817. // Use deterministic randomness to send to the same nodes for 24 hours
  2818. // at a time so the setAddrKnowns of the chosen nodes prevent repeats
  2819. static uint256 hashSalt;
  2820. if (hashSalt == 0)
  2821. hashSalt = GetRandHash();
  2822. uint64_t hashAddr = addr.GetHash();
  2823. uint256 hashRand = hashSalt ^ (hashAddr<<32) ^ ((GetTime()+hashAddr)/(24*60*60));
  2824. hashRand = Hash(BEGIN(hashRand), END(hashRand));
  2825. multimap<uint256, CNode*> mapMix;
  2826. BOOST_FOREACH(CNode* pnode, vNodes)
  2827. {
  2828. if (pnode->nVersion < CADDR_TIME_VERSION)
  2829. continue;
  2830. unsigned int nPointer;
  2831. memcpy(&nPointer, &pnode, sizeof(nPointer));
  2832. uint256 hashKey = hashRand ^ nPointer;
  2833. hashKey = Hash(BEGIN(hashKey), END(hashKey));
  2834. mapMix.insert(make_pair(hashKey, pnode));
  2835. }
  2836. int nRelayNodes = fReachable ? 2 : 1; // limited relaying of addresses outside our network(s)
  2837. for (multimap<uint256, CNode*>::iterator mi = mapMix.begin(); mi != mapMix.end() && nRelayNodes-- > 0; ++mi)
  2838. ((*mi).second)->PushAddress(addr);
  2839. }
  2840. }
  2841. // Do not store addresses outside our network
  2842. if (fReachable)
  2843. vAddrOk.push_back(addr);
  2844. }
  2845. addrman.Add(vAddrOk, pfrom->addr, 2 * 60 * 60);
  2846. if (vAddr.size() < 1000)
  2847. pfrom->fGetAddr = false;
  2848. if (pfrom->fOneShot)
  2849. pfrom->fDisconnect = true;
  2850. }
  2851. else if (strCommand == "inv")
  2852. {
  2853. vector<CInv> vInv;
  2854. vRecv >> vInv;
  2855. if (vInv.size() > MAX_INV_SZ)
  2856. {
  2857. pfrom->Misbehaving(20);
  2858. return error("message inv size() = %"PRIszu"", vInv.size());
  2859. }
  2860. // find last block in inv vector
  2861. unsigned int nLastBlock = (unsigned int)(-1);
  2862. for (unsigned int nInv = 0; nInv < vInv.size(); nInv++) {
  2863. if (vInv[vInv.size() - 1 - nInv].type == MSG_BLOCK) {
  2864. nLastBlock = vInv.size() - 1 - nInv;
  2865. break;
  2866. }
  2867. }
  2868. LOCK(cs_main);
  2869. for (unsigned int nInv = 0; nInv < vInv.size(); nInv++)
  2870. {
  2871. const CInv &inv = vInv[nInv];
  2872. boost::this_thread::interruption_point();
  2873. pfrom->AddInventoryKnown(inv);
  2874. bool fAlreadyHave = AlreadyHave(inv);
  2875. LogPrint("net", " got inventory: %s %s\n", inv.ToString().c_str(), fAlreadyHave ? "have" : "new");
  2876. if (!fAlreadyHave) {
  2877. if (!fImporting && !fReindex)
  2878. pfrom->AskFor(inv);
  2879. } else if (inv.type == MSG_BLOCK && mapOrphanBlocks.count(inv.hash)) {
  2880. PushGetBlocks(pfrom, chainActive.Tip(), GetOrphanRoot(mapOrphanBlocks[inv.hash]));
  2881. } else if (nInv == nLastBlock) {
  2882. // In case we are on a very long side-chain, it is possible that we already have
  2883. // the last block in an inv bundle sent in response to getblocks. Try to detect
  2884. // this situation and push another getblocks to continue.
  2885. PushGetBlocks(pfrom, mapBlockIndex[inv.hash], uint256(0));
  2886. if (fDebug)
  2887. LogPrintf("force request: %s\n", inv.ToString().c_str());
  2888. }
  2889. // Track requests for our stuff
  2890. g_signals.Inventory(inv.hash);
  2891. }
  2892. }
  2893. else if (strCommand == "getdata")
  2894. {
  2895. vector<CInv> vInv;
  2896. vRecv >> vInv;
  2897. if (vInv.size() > MAX_INV_SZ)
  2898. {
  2899. pfrom->Misbehaving(20);
  2900. return error("message getdata size() = %"PRIszu"", vInv.size());
  2901. }
  2902. if (fDebug || (vInv.size() != 1))
  2903. LogPrint("net", "received getdata (%"PRIszu" invsz)\n", vInv.size());
  2904. if ((fDebug && vInv.size() > 0) || (vInv.size() == 1))
  2905. LogPrint("net", "received getdata for: %s\n", vInv[0].ToString().c_str());
  2906. pfrom->vRecvGetData.insert(pfrom->vRecvGetData.end(), vInv.begin(), vInv.end());
  2907. ProcessGetData(pfrom);
  2908. }
  2909. else if (strCommand == "getblocks")
  2910. {
  2911. CBlockLocator locator;
  2912. uint256 hashStop;
  2913. vRecv >> locator >> hashStop;
  2914. LOCK(cs_main);
  2915. // Find the last block the caller has in the main chain
  2916. CBlockIndex* pindex = chainActive.FindFork(locator);
  2917. // Send the rest of the chain
  2918. if (pindex)
  2919. pindex = chainActive.Next(pindex);
  2920. int nLimit = 500;
  2921. LogPrint("net", "getblocks %d to %s limit %d\n", (pindex ? pindex->nHeight : -1), hashStop.ToString().c_str(), nLimit);
  2922. for (; pindex; pindex = chainActive.Next(pindex))
  2923. {
  2924. if (pindex->GetBlockHash() == hashStop)
  2925. {
  2926. LogPrint("net", " getblocks stopping at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString().c_str());
  2927. break;
  2928. }
  2929. pfrom->PushInventory(CInv(MSG_BLOCK, pindex->GetBlockHash()));
  2930. if (--nLimit <= 0)
  2931. {
  2932. // When this block is requested, we'll send an inv that'll make them
  2933. // getblocks the next batch of inventory.
  2934. LogPrint("net", " getblocks stopping at limit %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString().c_str());
  2935. pfrom->hashContinue = pindex->GetBlockHash();
  2936. break;
  2937. }
  2938. }
  2939. }
  2940. else if (strCommand == "getheaders")
  2941. {
  2942. CBlockLocator locator;
  2943. uint256 hashStop;
  2944. vRecv >> locator >> hashStop;
  2945. LOCK(cs_main);
  2946. CBlockIndex* pindex = NULL;
  2947. if (locator.IsNull())
  2948. {
  2949. // If locator is null, return the hashStop block
  2950. map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashStop);
  2951. if (mi == mapBlockIndex.end())
  2952. return true;
  2953. pindex = (*mi).second;
  2954. }
  2955. else
  2956. {
  2957. // Find the last block the caller has in the main chain
  2958. pindex = chainActive.FindFork(locator);
  2959. if (pindex)
  2960. pindex = chainActive.Next(pindex);
  2961. }
  2962. // we must use CBlocks, as CBlockHeaders won't include the 0x00 nTx count at the end
  2963. vector<CBlock> vHeaders;
  2964. int nLimit = 2000;
  2965. LogPrint("net", "getheaders %d to %s\n", (pindex ? pindex->nHeight : -1), hashStop.ToString().c_str());
  2966. for (; pindex; pindex = chainActive.Next(pindex))
  2967. {
  2968. vHeaders.push_back(pindex->GetBlockHeader());
  2969. if (--nLimit <= 0 || pindex->GetBlockHash() == hashStop)
  2970. break;
  2971. }
  2972. pfrom->PushMessage("headers", vHeaders);
  2973. }
  2974. else if (strCommand == "tx")
  2975. {
  2976. vector<uint256> vWorkQueue;
  2977. vector<uint256> vEraseQueue;
  2978. CTransaction tx;
  2979. vRecv >> tx;
  2980. CInv inv(MSG_TX, tx.GetHash());
  2981. pfrom->AddInventoryKnown(inv);
  2982. LOCK(cs_main);
  2983. bool fMissingInputs = false;
  2984. CValidationState state;
  2985. if (AcceptToMemoryPool(mempool, state, tx, true, &fMissingInputs))
  2986. {
  2987. mempool.check(&LookupFromTip);
  2988. RelayTransaction(tx, inv.hash);
  2989. mapAlreadyAskedFor.erase(inv);
  2990. vWorkQueue.push_back(inv.hash);
  2991. vEraseQueue.push_back(inv.hash);
  2992. // Recursively process any orphan transactions that depended on this one
  2993. for (unsigned int i = 0; i < vWorkQueue.size(); i++)
  2994. {
  2995. uint256 hashPrev = vWorkQueue[i];
  2996. for (set<uint256>::iterator mi = mapOrphanTransactionsByPrev[hashPrev].begin();
  2997. mi != mapOrphanTransactionsByPrev[hashPrev].end();
  2998. ++mi)
  2999. {
  3000. const uint256& orphanHash = *mi;
  3001. const CTransaction& orphanTx = mapOrphanTransactions[orphanHash];
  3002. bool fMissingInputs2 = false;
  3003. // Use a dummy CValidationState so someone can't setup nodes to counter-DoS based on orphan
  3004. // resolution (that is, feeding people an invalid transaction based on LegitTxX in order to get
  3005. // anyone relaying LegitTxX banned)
  3006. CValidationState stateDummy;
  3007. if (AcceptToMemoryPool(mempool, stateDummy, orphanTx, true, &fMissingInputs2))
  3008. {
  3009. LogPrint("mempool", " accepted orphan tx %s\n", orphanHash.ToString().c_str());
  3010. RelayTransaction(orphanTx, orphanHash);
  3011. mapAlreadyAskedFor.erase(CInv(MSG_TX, orphanHash));
  3012. vWorkQueue.push_back(orphanHash);
  3013. vEraseQueue.push_back(orphanHash);
  3014. }
  3015. else if (!fMissingInputs2)
  3016. {
  3017. // invalid or too-little-fee orphan
  3018. vEraseQueue.push_back(orphanHash);
  3019. LogPrint("mempool", " removed orphan tx %s\n", orphanHash.ToString().c_str());
  3020. }
  3021. mempool.check(&LookupFromTip);
  3022. }
  3023. }
  3024. BOOST_FOREACH(uint256 hash, vEraseQueue)
  3025. EraseOrphanTx(hash);
  3026. }
  3027. else if (fMissingInputs)
  3028. {
  3029. AddOrphanTx(tx);
  3030. // DoS prevention: do not allow mapOrphanTransactions to grow unbounded
  3031. unsigned int nEvicted = LimitOrphanTxSize(MAX_ORPHAN_TRANSACTIONS);
  3032. if (nEvicted > 0)
  3033. LogPrint("mempool", "mapOrphan overflow, removed %u tx\n", nEvicted);
  3034. }
  3035. int nDoS = 0;
  3036. if (state.IsInvalid(nDoS))
  3037. if (nDoS > 0)
  3038. pfrom->Misbehaving(nDoS);
  3039. }
  3040. else if (strCommand == "block" && !fImporting && !fReindex) // Ignore blocks received while importing
  3041. {
  3042. CBlock block;
  3043. vRecv >> block;
  3044. LogPrint("net", "received block %s\n", block.GetHash().ToString().c_str());
  3045. // block.print();
  3046. CInv inv(MSG_BLOCK, block.GetHash());
  3047. pfrom->AddInventoryKnown(inv);
  3048. LOCK(cs_main);
  3049. CValidationState state;
  3050. if (ProcessBlock(state, pfrom, &block) || state.CorruptionPossible())
  3051. mapAlreadyAskedFor.erase(inv);
  3052. int nDoS = 0;
  3053. if (state.IsInvalid(nDoS))
  3054. if (nDoS > 0)
  3055. pfrom->Misbehaving(nDoS);
  3056. }
  3057. else if (strCommand == "getaddr")
  3058. {
  3059. pfrom->vAddrToSend.clear();
  3060. vector<CAddress> vAddr = addrman.GetAddr();
  3061. BOOST_FOREACH(const CAddress &addr, vAddr)
  3062. pfrom->PushAddress(addr);
  3063. }
  3064. else if (strCommand == "mempool")
  3065. {
  3066. LOCK2(cs_main, pfrom->cs_filter);
  3067. std::vector<uint256> vtxid;
  3068. mempool.queryHashes(vtxid);
  3069. vector<CInv> vInv;
  3070. BOOST_FOREACH(uint256& hash, vtxid) {
  3071. CInv inv(MSG_TX, hash);
  3072. CTransaction tx;
  3073. bool fInMemPool = mempool.lookup(hash, tx);
  3074. if (!fInMemPool) continue; // another thread removed since queryHashes, maybe...
  3075. if ((pfrom->pfilter && pfrom->pfilter->IsRelevantAndUpdate(tx, hash)) ||
  3076. (!pfrom->pfilter))
  3077. vInv.push_back(inv);
  3078. if (vInv.size() == MAX_INV_SZ) {
  3079. pfrom->PushMessage("inv", vInv);
  3080. vInv.clear();
  3081. }
  3082. }
  3083. if (vInv.size() > 0)
  3084. pfrom->PushMessage("inv", vInv);
  3085. }
  3086. else if (strCommand == "ping")
  3087. {
  3088. if (pfrom->nVersion > BIP0031_VERSION)
  3089. {
  3090. uint64_t nonce = 0;
  3091. vRecv >> nonce;
  3092. // Echo the message back with the nonce. This allows for two useful features:
  3093. //
  3094. // 1) A remote node can quickly check if the connection is operational
  3095. // 2) Remote nodes can measure the latency of the network thread. If this node
  3096. // is overloaded it won't respond to pings quickly and the remote node can
  3097. // avoid sending us more work, like chain download requests.
  3098. //
  3099. // The nonce stops the remote getting confused between different pings: without
  3100. // it, if the remote node sends a ping once per second and this node takes 5
  3101. // seconds to respond to each, the 5th ping the remote sends would appear to
  3102. // return very quickly.
  3103. pfrom->PushMessage("pong", nonce);
  3104. }
  3105. }
  3106. else if (strCommand == "pong")
  3107. {
  3108. int64_t pingUsecEnd = GetTimeMicros();
  3109. uint64_t nonce = 0;
  3110. size_t nAvail = vRecv.in_avail();
  3111. bool bPingFinished = false;
  3112. std::string sProblem;
  3113. if (nAvail >= sizeof(nonce)) {
  3114. vRecv >> nonce;
  3115. // Only process pong message if there is an outstanding ping (old ping without nonce should never pong)
  3116. if (pfrom->nPingNonceSent != 0) {
  3117. if (nonce == pfrom->nPingNonceSent) {
  3118. // Matching pong received, this ping is no longer outstanding
  3119. bPingFinished = true;
  3120. int64_t pingUsecTime = pingUsecEnd - pfrom->nPingUsecStart;
  3121. if (pingUsecTime > 0) {
  3122. // Successful ping time measurement, replace previous
  3123. pfrom->nPingUsecTime = pingUsecTime;
  3124. } else {
  3125. // This should never happen
  3126. sProblem = "Timing mishap";
  3127. }
  3128. } else {
  3129. // Nonce mismatches are normal when pings are overlapping
  3130. sProblem = "Nonce mismatch";
  3131. if (nonce == 0) {
  3132. // This is most likely a bug in another implementation somewhere, cancel this ping
  3133. bPingFinished = true;
  3134. sProblem = "Nonce zero";
  3135. }
  3136. }
  3137. } else {
  3138. sProblem = "Unsolicited pong without ping";
  3139. }
  3140. } else {
  3141. // This is most likely a bug in another implementation somewhere, cancel this ping
  3142. bPingFinished = true;
  3143. sProblem = "Short payload";
  3144. }
  3145. if (!(sProblem.empty())) {
  3146. LogPrint("net", "pong %s %s: %s, %"PRIx64" expected, %"PRIx64" received, %"PRIszu" bytes\n",
  3147. pfrom->addr.ToString().c_str(),
  3148. pfrom->strSubVer.c_str(),
  3149. sProblem.c_str(),
  3150. pfrom->nPingNonceSent,
  3151. nonce,
  3152. nAvail);
  3153. }
  3154. if (bPingFinished) {
  3155. pfrom->nPingNonceSent = 0;
  3156. }
  3157. }
  3158. else if (strCommand == "alert")
  3159. {
  3160. CAlert alert;
  3161. vRecv >> alert;
  3162. uint256 alertHash = alert.GetHash();
  3163. if (pfrom->setKnown.count(alertHash) == 0)
  3164. {
  3165. if (alert.ProcessAlert())
  3166. {
  3167. // Relay
  3168. pfrom->setKnown.insert(alertHash);
  3169. {
  3170. LOCK(cs_vNodes);
  3171. BOOST_FOREACH(CNode* pnode, vNodes)
  3172. alert.RelayTo(pnode);
  3173. }
  3174. }
  3175. else {
  3176. // Small DoS penalty so peers that send us lots of
  3177. // duplicate/expired/invalid-signature/whatever alerts
  3178. // eventually get banned.
  3179. // This isn't a Misbehaving(100) (immediate ban) because the
  3180. // peer might be an older or different implementation with
  3181. // a different signature key, etc.
  3182. pfrom->Misbehaving(10);
  3183. }
  3184. }
  3185. }
  3186. else if (strCommand == "filterload")
  3187. {
  3188. CBloomFilter filter;
  3189. vRecv >> filter;
  3190. if (!filter.IsWithinSizeConstraints())
  3191. // There is no excuse for sending a too-large filter
  3192. pfrom->Misbehaving(100);
  3193. else
  3194. {
  3195. LOCK(pfrom->cs_filter);
  3196. delete pfrom->pfilter;
  3197. pfrom->pfilter = new CBloomFilter(filter);
  3198. pfrom->pfilter->UpdateEmptyFull();
  3199. }
  3200. pfrom->fRelayTxes = true;
  3201. }
  3202. else if (strCommand == "filteradd")
  3203. {
  3204. vector<unsigned char> vData;
  3205. vRecv >> vData;
  3206. // Nodes must NEVER send a data item > 520 bytes (the max size for a script data object,
  3207. // and thus, the maximum size any matched object can have) in a filteradd message
  3208. if (vData.size() > MAX_SCRIPT_ELEMENT_SIZE)
  3209. {
  3210. pfrom->Misbehaving(100);
  3211. } else {
  3212. LOCK(pfrom->cs_filter);
  3213. if (pfrom->pfilter)
  3214. pfrom->pfilter->insert(vData);
  3215. else
  3216. pfrom->Misbehaving(100);
  3217. }
  3218. }
  3219. else if (strCommand == "filterclear")
  3220. {
  3221. LOCK(pfrom->cs_filter);
  3222. delete pfrom->pfilter;
  3223. pfrom->pfilter = new CBloomFilter();
  3224. pfrom->fRelayTxes = true;
  3225. }
  3226. else
  3227. {
  3228. // Ignore unknown commands for extensibility
  3229. }
  3230. // Update the last seen time for this node's address
  3231. if (pfrom->fNetworkNode)
  3232. if (strCommand == "version" || strCommand == "addr" || strCommand == "inv" || strCommand == "getdata" || strCommand == "ping")
  3233. AddressCurrentlyConnected(pfrom->addr);
  3234. return true;
  3235. }
  3236. // requires LOCK(cs_vRecvMsg)
  3237. bool ProcessMessages(CNode* pfrom)
  3238. {
  3239. //if (fDebug)
  3240. // LogPrintf("ProcessMessages(%"PRIszu" messages)\n", pfrom->vRecvMsg.size());
  3241. //
  3242. // Message format
  3243. // (4) message start
  3244. // (12) command
  3245. // (4) size
  3246. // (4) checksum
  3247. // (x) data
  3248. //
  3249. bool fOk = true;
  3250. if (!pfrom->vRecvGetData.empty())
  3251. ProcessGetData(pfrom);
  3252. // this maintains the order of responses
  3253. if (!pfrom->vRecvGetData.empty()) return fOk;
  3254. std::deque<CNetMessage>::iterator it = pfrom->vRecvMsg.begin();
  3255. while (!pfrom->fDisconnect && it != pfrom->vRecvMsg.end()) {
  3256. // Don't bother if send buffer is too full to respond anyway
  3257. if (pfrom->nSendSize >= SendBufferSize())
  3258. break;
  3259. // get next message
  3260. CNetMessage& msg = *it;
  3261. //if (fDebug)
  3262. // LogPrintf("ProcessMessages(message %u msgsz, %"PRIszu" bytes, complete:%s)\n",
  3263. // msg.hdr.nMessageSize, msg.vRecv.size(),
  3264. // msg.complete() ? "Y" : "N");
  3265. // end, if an incomplete message is found
  3266. if (!msg.complete())
  3267. break;
  3268. // at this point, any failure means we can delete the current message
  3269. it++;
  3270. // Scan for message start
  3271. if (memcmp(msg.hdr.pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE) != 0) {
  3272. LogPrintf("\n\nPROCESSMESSAGE: INVALID MESSAGESTART\n\n");
  3273. fOk = false;
  3274. break;
  3275. }
  3276. // Read header
  3277. CMessageHeader& hdr = msg.hdr;
  3278. if (!hdr.IsValid())
  3279. {
  3280. LogPrintf("\n\nPROCESSMESSAGE: ERRORS IN HEADER %s\n\n\n", hdr.GetCommand().c_str());
  3281. continue;
  3282. }
  3283. string strCommand = hdr.GetCommand();
  3284. // Message size
  3285. unsigned int nMessageSize = hdr.nMessageSize;
  3286. // Checksum
  3287. CDataStream& vRecv = msg.vRecv;
  3288. uint256 hash = Hash(vRecv.begin(), vRecv.begin() + nMessageSize);
  3289. unsigned int nChecksum = 0;
  3290. memcpy(&nChecksum, &hash, sizeof(nChecksum));
  3291. if (nChecksum != hdr.nChecksum)
  3292. {
  3293. LogPrintf("ProcessMessages(%s, %u bytes) : CHECKSUM ERROR nChecksum=%08x hdr.nChecksum=%08x\n",
  3294. strCommand.c_str(), nMessageSize, nChecksum, hdr.nChecksum);
  3295. continue;
  3296. }
  3297. // Process message
  3298. bool fRet = false;
  3299. try
  3300. {
  3301. fRet = ProcessMessage(pfrom, strCommand, vRecv);
  3302. boost::this_thread::interruption_point();
  3303. }
  3304. catch (std::ios_base::failure& e)
  3305. {
  3306. if (strstr(e.what(), "end of data"))
  3307. {
  3308. // Allow exceptions from under-length message on vRecv
  3309. LogPrintf("ProcessMessages(%s, %u bytes) : Exception '%s' caught, normally caused by a message being shorter than its stated length\n", strCommand.c_str(), nMessageSize, e.what());
  3310. }
  3311. else if (strstr(e.what(), "size too large"))
  3312. {
  3313. // Allow exceptions from over-long size
  3314. LogPrintf("ProcessMessages(%s, %u bytes) : Exception '%s' caught\n", strCommand.c_str(), nMessageSize, e.what());
  3315. }
  3316. else
  3317. {
  3318. PrintExceptionContinue(&e, "ProcessMessages()");
  3319. }
  3320. }
  3321. catch (boost::thread_interrupted) {
  3322. throw;
  3323. }
  3324. catch (std::exception& e) {
  3325. PrintExceptionContinue(&e, "ProcessMessages()");
  3326. } catch (...) {
  3327. PrintExceptionContinue(NULL, "ProcessMessages()");
  3328. }
  3329. if (!fRet)
  3330. LogPrintf("ProcessMessage(%s, %u bytes) FAILED\n", strCommand.c_str(), nMessageSize);
  3331. break;
  3332. }
  3333. // In case the connection got shut down, its receive buffer was wiped
  3334. if (!pfrom->fDisconnect)
  3335. pfrom->vRecvMsg.erase(pfrom->vRecvMsg.begin(), it);
  3336. return fOk;
  3337. }
  3338. bool SendMessages(CNode* pto, bool fSendTrickle)
  3339. {
  3340. {
  3341. // Don't send anything until we get their version message
  3342. if (pto->nVersion == 0)
  3343. return true;
  3344. //
  3345. // Message: ping
  3346. //
  3347. bool pingSend = false;
  3348. if (pto->fPingQueued) {
  3349. // RPC ping request by user
  3350. pingSend = true;
  3351. }
  3352. if (pto->nLastSend && GetTime() - pto->nLastSend > 30 * 60 && pto->vSendMsg.empty()) {
  3353. // Ping automatically sent as a keepalive
  3354. pingSend = true;
  3355. }
  3356. if (pingSend) {
  3357. uint64_t nonce = 0;
  3358. while (nonce == 0) {
  3359. RAND_bytes((unsigned char*)&nonce, sizeof(nonce));
  3360. }
  3361. pto->nPingNonceSent = nonce;
  3362. pto->fPingQueued = false;
  3363. if (pto->nVersion > BIP0031_VERSION) {
  3364. // Take timestamp as close as possible before transmitting ping
  3365. pto->nPingUsecStart = GetTimeMicros();
  3366. pto->PushMessage("ping", nonce);
  3367. } else {
  3368. // Peer is too old to support ping command with nonce, pong will never arrive, disable timing
  3369. pto->nPingUsecStart = 0;
  3370. pto->PushMessage("ping");
  3371. }
  3372. }
  3373. // Address refresh broadcast
  3374. static int64_t nLastRebroadcast;
  3375. if (!IsInitialBlockDownload() && (GetTime() - nLastRebroadcast > 24 * 60 * 60))
  3376. {
  3377. {
  3378. LOCK(cs_vNodes);
  3379. BOOST_FOREACH(CNode* pnode, vNodes)
  3380. {
  3381. // Periodically clear setAddrKnown to allow refresh broadcasts
  3382. if (nLastRebroadcast)
  3383. pnode->setAddrKnown.clear();
  3384. // Rebroadcast our address
  3385. if (!fNoListen)
  3386. {
  3387. CAddress addr = GetLocalAddress(&pnode->addr);
  3388. if (addr.IsRoutable())
  3389. pnode->PushAddress(addr);
  3390. }
  3391. }
  3392. }
  3393. nLastRebroadcast = GetTime();
  3394. }
  3395. //
  3396. // Message: addr
  3397. //
  3398. if (fSendTrickle)
  3399. {
  3400. vector<CAddress> vAddr;
  3401. vAddr.reserve(pto->vAddrToSend.size());
  3402. BOOST_FOREACH(const CAddress& addr, pto->vAddrToSend)
  3403. {
  3404. // returns true if wasn't already contained in the set
  3405. if (pto->setAddrKnown.insert(addr).second)
  3406. {
  3407. vAddr.push_back(addr);
  3408. // receiver rejects addr messages larger than 1000
  3409. if (vAddr.size() >= 1000)
  3410. {
  3411. pto->PushMessage("addr", vAddr);
  3412. vAddr.clear();
  3413. }
  3414. }
  3415. }
  3416. pto->vAddrToSend.clear();
  3417. if (!vAddr.empty())
  3418. pto->PushMessage("addr", vAddr);
  3419. }
  3420. TRY_LOCK(cs_main, lockMain);
  3421. if (!lockMain)
  3422. return true;
  3423. // Start block sync
  3424. if (pto->fStartSync && !fImporting && !fReindex) {
  3425. pto->fStartSync = false;
  3426. PushGetBlocks(pto, chainActive.Tip(), uint256(0));
  3427. }
  3428. // Resend wallet transactions that haven't gotten in a block yet
  3429. // Except during reindex, importing and IBD, when old wallet
  3430. // transactions become unconfirmed and spams other nodes.
  3431. if (!fReindex && !fImporting && !IsInitialBlockDownload())
  3432. {
  3433. g_signals.Broadcast();
  3434. }
  3435. //
  3436. // Message: inventory
  3437. //
  3438. vector<CInv> vInv;
  3439. vector<CInv> vInvWait;
  3440. {
  3441. LOCK(pto->cs_inventory);
  3442. vInv.reserve(pto->vInventoryToSend.size());
  3443. vInvWait.reserve(pto->vInventoryToSend.size());
  3444. BOOST_FOREACH(const CInv& inv, pto->vInventoryToSend)
  3445. {
  3446. if (pto->setInventoryKnown.count(inv))
  3447. continue;
  3448. // trickle out tx inv to protect privacy
  3449. if (inv.type == MSG_TX && !fSendTrickle)
  3450. {
  3451. // 1/4 of tx invs blast to all immediately
  3452. static uint256 hashSalt;
  3453. if (hashSalt == 0)
  3454. hashSalt = GetRandHash();
  3455. uint256 hashRand = inv.hash ^ hashSalt;
  3456. hashRand = Hash(BEGIN(hashRand), END(hashRand));
  3457. bool fTrickleWait = ((hashRand & 3) != 0);
  3458. if (fTrickleWait)
  3459. {
  3460. vInvWait.push_back(inv);
  3461. continue;
  3462. }
  3463. }
  3464. // returns true if wasn't already contained in the set
  3465. if (pto->setInventoryKnown.insert(inv).second)
  3466. {
  3467. vInv.push_back(inv);
  3468. if (vInv.size() >= 1000)
  3469. {
  3470. pto->PushMessage("inv", vInv);
  3471. vInv.clear();
  3472. }
  3473. }
  3474. }
  3475. pto->vInventoryToSend = vInvWait;
  3476. }
  3477. if (!vInv.empty())
  3478. pto->PushMessage("inv", vInv);
  3479. //
  3480. // Message: getdata
  3481. //
  3482. vector<CInv> vGetData;
  3483. int64_t nNow = GetTime() * 1000000;
  3484. while (!pto->mapAskFor.empty() && (*pto->mapAskFor.begin()).first <= nNow)
  3485. {
  3486. const CInv& inv = (*pto->mapAskFor.begin()).second;
  3487. if (!AlreadyHave(inv))
  3488. {
  3489. if (fDebug)
  3490. LogPrint("net", "sending getdata: %s\n", inv.ToString().c_str());
  3491. vGetData.push_back(inv);
  3492. if (vGetData.size() >= 1000)
  3493. {
  3494. pto->PushMessage("getdata", vGetData);
  3495. vGetData.clear();
  3496. }
  3497. }
  3498. pto->mapAskFor.erase(pto->mapAskFor.begin());
  3499. }
  3500. if (!vGetData.empty())
  3501. pto->PushMessage("getdata", vGetData);
  3502. }
  3503. return true;
  3504. }
  3505. class CMainCleanup
  3506. {
  3507. public:
  3508. CMainCleanup() {}
  3509. ~CMainCleanup() {
  3510. // block headers
  3511. std::map<uint256, CBlockIndex*>::iterator it1 = mapBlockIndex.begin();
  3512. for (; it1 != mapBlockIndex.end(); it1++)
  3513. delete (*it1).second;
  3514. mapBlockIndex.clear();
  3515. // orphan blocks
  3516. std::map<uint256, CBlock*>::iterator it2 = mapOrphanBlocks.begin();
  3517. for (; it2 != mapOrphanBlocks.end(); it2++)
  3518. delete (*it2).second;
  3519. mapOrphanBlocks.clear();
  3520. // orphan transactions
  3521. mapOrphanTransactions.clear();
  3522. }
  3523. } instance_of_cmaincleanup;