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 167KB

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