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.

validation.cpp 208KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2017 The Bitcoin Core developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #include <validation.h>
  6. #include <arith_uint256.h>
  7. #include <chain.h>
  8. #include <chainparams.h>
  9. #include <checkpoints.h>
  10. #include <checkqueue.h>
  11. #include <consensus/consensus.h>
  12. #include <consensus/merkle.h>
  13. #include <consensus/tx_verify.h>
  14. #include <consensus/validation.h>
  15. #include <cuckoocache.h>
  16. #include <hash.h>
  17. #include <init.h>
  18. #include <policy/fees.h>
  19. #include <policy/policy.h>
  20. #include <policy/rbf.h>
  21. #include <pow.h>
  22. #include <primitives/block.h>
  23. #include <primitives/transaction.h>
  24. #include <random.h>
  25. #include <reverse_iterator.h>
  26. #include <script/script.h>
  27. #include <script/sigcache.h>
  28. #include <script/standard.h>
  29. #include <timedata.h>
  30. #include <tinyformat.h>
  31. #include <txdb.h>
  32. #include <txmempool.h>
  33. #include <ui_interface.h>
  34. #include <undo.h>
  35. #include <util.h>
  36. #include <utilmoneystr.h>
  37. #include <utilstrencodings.h>
  38. #include <validationinterface.h>
  39. #include <warnings.h>
  40. #include <future>
  41. #include <sstream>
  42. #include <boost/algorithm/string/replace.hpp>
  43. #include <boost/algorithm/string/join.hpp>
  44. #include <boost/thread.hpp>
  45. #if defined(NDEBUG)
  46. # error "Bitcoin cannot be compiled without assertions."
  47. #endif
  48. #define MICRO 0.000001
  49. #define MILLI 0.001
  50. /**
  51. * Global state
  52. */
  53. namespace {
  54. struct CBlockIndexWorkComparator
  55. {
  56. bool operator()(const CBlockIndex *pa, const CBlockIndex *pb) const {
  57. // First sort by most total work, ...
  58. if (pa->nChainWork > pb->nChainWork) return false;
  59. if (pa->nChainWork < pb->nChainWork) return true;
  60. // ... then by earliest time received, ...
  61. if (pa->nSequenceId < pb->nSequenceId) return false;
  62. if (pa->nSequenceId > pb->nSequenceId) return true;
  63. // Use pointer address as tie breaker (should only happen with blocks
  64. // loaded from disk, as those all have id 0).
  65. if (pa < pb) return false;
  66. if (pa > pb) return true;
  67. // Identical blocks.
  68. return false;
  69. }
  70. };
  71. } // anon namespace
  72. enum DisconnectResult
  73. {
  74. DISCONNECT_OK, // All good.
  75. DISCONNECT_UNCLEAN, // Rolled back, but UTXO set was inconsistent with block.
  76. DISCONNECT_FAILED // Something else went wrong.
  77. };
  78. class ConnectTrace;
  79. /**
  80. * CChainState stores and provides an API to update our local knowledge of the
  81. * current best chain and header tree.
  82. *
  83. * It generally provides access to the current block tree, as well as functions
  84. * to provide new data, which it will appropriately validate and incorporate in
  85. * its state as necessary.
  86. *
  87. * Eventually, the API here is targeted at being exposed externally as a
  88. * consumable libconsensus library, so any functions added must only call
  89. * other class member functions, pure functions in other parts of the consensus
  90. * library, callbacks via the validation interface, or read/write-to-disk
  91. * functions (eventually this will also be via callbacks).
  92. */
  93. class CChainState {
  94. private:
  95. /**
  96. * The set of all CBlockIndex entries with BLOCK_VALID_TRANSACTIONS (for itself and all ancestors) and
  97. * as good as our current tip or better. Entries may be failed, though, and pruning nodes may be
  98. * missing the data for the block.
  99. */
  100. std::set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexCandidates;
  101. /**
  102. * Every received block is assigned a unique and increasing identifier, so we
  103. * know which one to give priority in case of a fork.
  104. */
  105. CCriticalSection cs_nBlockSequenceId;
  106. /** Blocks loaded from disk are assigned id 0, so start the counter at 1. */
  107. int32_t nBlockSequenceId = 1;
  108. /** Decreasing counter (used by subsequent preciousblock calls). */
  109. int32_t nBlockReverseSequenceId = -1;
  110. /** chainwork for the last block that preciousblock has been applied to. */
  111. arith_uint256 nLastPreciousChainwork = 0;
  112. /** In order to efficiently track invalidity of headers, we keep the set of
  113. * blocks which we tried to connect and found to be invalid here (ie which
  114. * were set to BLOCK_FAILED_VALID since the last restart). We can then
  115. * walk this set and check if a new header is a descendant of something in
  116. * this set, preventing us from having to walk mapBlockIndex when we try
  117. * to connect a bad block and fail.
  118. *
  119. * While this is more complicated than marking everything which descends
  120. * from an invalid block as invalid at the time we discover it to be
  121. * invalid, doing so would require walking all of mapBlockIndex to find all
  122. * descendants. Since this case should be very rare, keeping track of all
  123. * BLOCK_FAILED_VALID blocks in a set should be just fine and work just as
  124. * well.
  125. *
  126. * Because we already walk mapBlockIndex in height-order at startup, we go
  127. * ahead and mark descendants of invalid blocks as FAILED_CHILD at that time,
  128. * instead of putting things in this set.
  129. */
  130. std::set<CBlockIndex*> g_failed_blocks;
  131. public:
  132. CChain chainActive;
  133. BlockMap mapBlockIndex;
  134. std::multimap<CBlockIndex*, CBlockIndex*> mapBlocksUnlinked;
  135. CBlockIndex *pindexBestInvalid = nullptr;
  136. bool LoadBlockIndex(const Consensus::Params& consensus_params, CBlockTreeDB& blocktree);
  137. bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams, std::shared_ptr<const CBlock> pblock);
  138. bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex);
  139. bool AcceptBlock(const std::shared_ptr<const CBlock>& pblock, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex, bool fRequested, const CDiskBlockPos* dbp, bool* fNewBlock);
  140. // Block (dis)connection on a given view:
  141. DisconnectResult DisconnectBlock(const CBlock& block, const CBlockIndex* pindex, CCoinsViewCache& view);
  142. bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pindex,
  143. CCoinsViewCache& view, const CChainParams& chainparams, bool fJustCheck = false);
  144. // Block disconnection on our pcoinsTip:
  145. bool DisconnectTip(CValidationState& state, const CChainParams& chainparams, DisconnectedBlockTransactions *disconnectpool);
  146. // Manual block validity manipulation:
  147. bool PreciousBlock(CValidationState& state, const CChainParams& params, CBlockIndex *pindex);
  148. bool InvalidateBlock(CValidationState& state, const CChainParams& chainparams, CBlockIndex *pindex);
  149. bool ResetBlockFailureFlags(CBlockIndex *pindex);
  150. bool ReplayBlocks(const CChainParams& params, CCoinsView* view);
  151. bool RewindBlockIndex(const CChainParams& params);
  152. bool LoadGenesisBlock(const CChainParams& chainparams);
  153. void PruneBlockIndexCandidates();
  154. void UnloadBlockIndex();
  155. private:
  156. bool ActivateBestChainStep(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool& fInvalidFound, ConnectTrace& connectTrace);
  157. bool ConnectTip(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace, DisconnectedBlockTransactions &disconnectpool);
  158. CBlockIndex* AddToBlockIndex(const CBlockHeader& block);
  159. /** Create a new block index entry for a given block hash */
  160. CBlockIndex * InsertBlockIndex(const uint256& hash);
  161. void CheckBlockIndex(const Consensus::Params& consensusParams);
  162. void InvalidBlockFound(CBlockIndex *pindex, const CValidationState &state);
  163. CBlockIndex* FindMostWorkChain();
  164. bool ReceivedBlockTransactions(const CBlock &block, CValidationState& state, CBlockIndex *pindexNew, const CDiskBlockPos& pos, const Consensus::Params& consensusParams);
  165. bool RollforwardBlock(const CBlockIndex* pindex, CCoinsViewCache& inputs, const CChainParams& params);
  166. } g_chainstate;
  167. CCriticalSection cs_main;
  168. BlockMap& mapBlockIndex = g_chainstate.mapBlockIndex;
  169. CChain& chainActive = g_chainstate.chainActive;
  170. CBlockIndex *pindexBestHeader = nullptr;
  171. CWaitableCriticalSection csBestBlock;
  172. CConditionVariable cvBlockChange;
  173. int nScriptCheckThreads = 0;
  174. std::atomic_bool fImporting(false);
  175. std::atomic_bool fReindex(false);
  176. bool fTxIndex = false;
  177. bool fHavePruned = false;
  178. bool fPruneMode = false;
  179. bool fIsBareMultisigStd = DEFAULT_PERMIT_BAREMULTISIG;
  180. bool fRequireStandard = true;
  181. bool fCheckBlockIndex = false;
  182. bool fCheckpointsEnabled = DEFAULT_CHECKPOINTS_ENABLED;
  183. size_t nCoinCacheUsage = 5000 * 300;
  184. uint64_t nPruneTarget = 0;
  185. int64_t nMaxTipAge = DEFAULT_MAX_TIP_AGE;
  186. bool fEnableReplacement = DEFAULT_ENABLE_REPLACEMENT;
  187. uint256 hashAssumeValid;
  188. arith_uint256 nMinimumChainWork;
  189. CFeeRate minRelayTxFee = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE);
  190. CAmount maxTxFee = DEFAULT_TRANSACTION_MAXFEE;
  191. CBlockPolicyEstimator feeEstimator;
  192. CTxMemPool mempool(&feeEstimator);
  193. /** Constant stuff for coinbase transactions we create: */
  194. CScript COINBASE_FLAGS;
  195. const std::string strMessageMagic = "Bitcoin Signed Message:\n";
  196. // Internal stuff
  197. namespace {
  198. CBlockIndex *&pindexBestInvalid = g_chainstate.pindexBestInvalid;
  199. /** All pairs A->B, where A (or one of its ancestors) misses transactions, but B has transactions.
  200. * Pruned nodes may have entries where B is missing data.
  201. */
  202. std::multimap<CBlockIndex*, CBlockIndex*>& mapBlocksUnlinked = g_chainstate.mapBlocksUnlinked;
  203. CCriticalSection cs_LastBlockFile;
  204. std::vector<CBlockFileInfo> vinfoBlockFile;
  205. int nLastBlockFile = 0;
  206. /** Global flag to indicate we should check to see if there are
  207. * block/undo files that should be deleted. Set on startup
  208. * or if we allocate more file space when we're in prune mode
  209. */
  210. bool fCheckForPruning = false;
  211. /** Dirty block index entries. */
  212. std::set<CBlockIndex*> setDirtyBlockIndex;
  213. /** Dirty block file entries. */
  214. std::set<int> setDirtyFileInfo;
  215. } // anon namespace
  216. CBlockIndex* FindForkInGlobalIndex(const CChain& chain, const CBlockLocator& locator)
  217. {
  218. // Find the first block the caller has in the main chain
  219. for (const uint256& hash : locator.vHave) {
  220. BlockMap::iterator mi = mapBlockIndex.find(hash);
  221. if (mi != mapBlockIndex.end())
  222. {
  223. CBlockIndex* pindex = (*mi).second;
  224. if (chain.Contains(pindex))
  225. return pindex;
  226. if (pindex->GetAncestor(chain.Height()) == chain.Tip()) {
  227. return chain.Tip();
  228. }
  229. }
  230. }
  231. return chain.Genesis();
  232. }
  233. std::unique_ptr<CCoinsViewDB> pcoinsdbview;
  234. std::unique_ptr<CCoinsViewCache> pcoinsTip;
  235. std::unique_ptr<CBlockTreeDB> pblocktree;
  236. enum FlushStateMode {
  237. FLUSH_STATE_NONE,
  238. FLUSH_STATE_IF_NEEDED,
  239. FLUSH_STATE_PERIODIC,
  240. FLUSH_STATE_ALWAYS
  241. };
  242. // See definition for documentation
  243. static bool FlushStateToDisk(const CChainParams& chainParams, CValidationState &state, FlushStateMode mode, int nManualPruneHeight=0);
  244. static void FindFilesToPruneManual(std::set<int>& setFilesToPrune, int nManualPruneHeight);
  245. static void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight);
  246. bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, bool cacheSigStore, bool cacheFullScriptStore, PrecomputedTransactionData& txdata, std::vector<CScriptCheck> *pvChecks = nullptr);
  247. static FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly = false);
  248. bool CheckFinalTx(const CTransaction &tx, int flags)
  249. {
  250. AssertLockHeld(cs_main);
  251. // By convention a negative value for flags indicates that the
  252. // current network-enforced consensus rules should be used. In
  253. // a future soft-fork scenario that would mean checking which
  254. // rules would be enforced for the next block and setting the
  255. // appropriate flags. At the present time no soft-forks are
  256. // scheduled, so no flags are set.
  257. flags = std::max(flags, 0);
  258. // CheckFinalTx() uses chainActive.Height()+1 to evaluate
  259. // nLockTime because when IsFinalTx() is called within
  260. // CBlock::AcceptBlock(), the height of the block *being*
  261. // evaluated is what is used. Thus if we want to know if a
  262. // transaction can be part of the *next* block, we need to call
  263. // IsFinalTx() with one more than chainActive.Height().
  264. const int nBlockHeight = chainActive.Height() + 1;
  265. // BIP113 requires that time-locked transactions have nLockTime set to
  266. // less than the median time of the previous block they're contained in.
  267. // When the next block is created its previous block will be the current
  268. // chain tip, so we use that to calculate the median time passed to
  269. // IsFinalTx() if LOCKTIME_MEDIAN_TIME_PAST is set.
  270. const int64_t nBlockTime = (flags & LOCKTIME_MEDIAN_TIME_PAST)
  271. ? chainActive.Tip()->GetMedianTimePast()
  272. : GetAdjustedTime();
  273. return IsFinalTx(tx, nBlockHeight, nBlockTime);
  274. }
  275. bool TestLockPointValidity(const LockPoints* lp)
  276. {
  277. AssertLockHeld(cs_main);
  278. assert(lp);
  279. // If there are relative lock times then the maxInputBlock will be set
  280. // If there are no relative lock times, the LockPoints don't depend on the chain
  281. if (lp->maxInputBlock) {
  282. // Check whether chainActive is an extension of the block at which the LockPoints
  283. // calculation was valid. If not LockPoints are no longer valid
  284. if (!chainActive.Contains(lp->maxInputBlock)) {
  285. return false;
  286. }
  287. }
  288. // LockPoints still valid
  289. return true;
  290. }
  291. bool CheckSequenceLocks(const CTransaction &tx, int flags, LockPoints* lp, bool useExistingLockPoints)
  292. {
  293. AssertLockHeld(cs_main);
  294. AssertLockHeld(mempool.cs);
  295. CBlockIndex* tip = chainActive.Tip();
  296. assert(tip != nullptr);
  297. CBlockIndex index;
  298. index.pprev = tip;
  299. // CheckSequenceLocks() uses chainActive.Height()+1 to evaluate
  300. // height based locks because when SequenceLocks() is called within
  301. // ConnectBlock(), the height of the block *being*
  302. // evaluated is what is used.
  303. // Thus if we want to know if a transaction can be part of the
  304. // *next* block, we need to use one more than chainActive.Height()
  305. index.nHeight = tip->nHeight + 1;
  306. std::pair<int, int64_t> lockPair;
  307. if (useExistingLockPoints) {
  308. assert(lp);
  309. lockPair.first = lp->height;
  310. lockPair.second = lp->time;
  311. }
  312. else {
  313. // pcoinsTip contains the UTXO set for chainActive.Tip()
  314. CCoinsViewMemPool viewMemPool(pcoinsTip.get(), mempool);
  315. std::vector<int> prevheights;
  316. prevheights.resize(tx.vin.size());
  317. for (size_t txinIndex = 0; txinIndex < tx.vin.size(); txinIndex++) {
  318. const CTxIn& txin = tx.vin[txinIndex];
  319. Coin coin;
  320. if (!viewMemPool.GetCoin(txin.prevout, coin)) {
  321. return error("%s: Missing input", __func__);
  322. }
  323. if (coin.nHeight == MEMPOOL_HEIGHT) {
  324. // Assume all mempool transaction confirm in the next block
  325. prevheights[txinIndex] = tip->nHeight + 1;
  326. } else {
  327. prevheights[txinIndex] = coin.nHeight;
  328. }
  329. }
  330. lockPair = CalculateSequenceLocks(tx, flags, &prevheights, index);
  331. if (lp) {
  332. lp->height = lockPair.first;
  333. lp->time = lockPair.second;
  334. // Also store the hash of the block with the highest height of
  335. // all the blocks which have sequence locked prevouts.
  336. // This hash needs to still be on the chain
  337. // for these LockPoint calculations to be valid
  338. // Note: It is impossible to correctly calculate a maxInputBlock
  339. // if any of the sequence locked inputs depend on unconfirmed txs,
  340. // except in the special case where the relative lock time/height
  341. // is 0, which is equivalent to no sequence lock. Since we assume
  342. // input height of tip+1 for mempool txs and test the resulting
  343. // lockPair from CalculateSequenceLocks against tip+1. We know
  344. // EvaluateSequenceLocks will fail if there was a non-zero sequence
  345. // lock on a mempool input, so we can use the return value of
  346. // CheckSequenceLocks to indicate the LockPoints validity
  347. int maxInputHeight = 0;
  348. for (int height : prevheights) {
  349. // Can ignore mempool inputs since we'll fail if they had non-zero locks
  350. if (height != tip->nHeight+1) {
  351. maxInputHeight = std::max(maxInputHeight, height);
  352. }
  353. }
  354. lp->maxInputBlock = tip->GetAncestor(maxInputHeight);
  355. }
  356. }
  357. return EvaluateSequenceLocks(index, lockPair);
  358. }
  359. // Returns the script flags which should be checked for a given block
  360. static unsigned int GetBlockScriptFlags(const CBlockIndex* pindex, const Consensus::Params& chainparams);
  361. static void LimitMempoolSize(CTxMemPool& pool, size_t limit, unsigned long age) {
  362. int expired = pool.Expire(GetTime() - age);
  363. if (expired != 0) {
  364. LogPrint(BCLog::MEMPOOL, "Expired %i transactions from the memory pool\n", expired);
  365. }
  366. std::vector<COutPoint> vNoSpendsRemaining;
  367. pool.TrimToSize(limit, &vNoSpendsRemaining);
  368. for (const COutPoint& removed : vNoSpendsRemaining)
  369. pcoinsTip->Uncache(removed);
  370. }
  371. /** Convert CValidationState to a human-readable message for logging */
  372. std::string FormatStateMessage(const CValidationState &state)
  373. {
  374. return strprintf("%s%s (code %i)",
  375. state.GetRejectReason(),
  376. state.GetDebugMessage().empty() ? "" : ", "+state.GetDebugMessage(),
  377. state.GetRejectCode());
  378. }
  379. static bool IsCurrentForFeeEstimation()
  380. {
  381. AssertLockHeld(cs_main);
  382. if (IsInitialBlockDownload())
  383. return false;
  384. if (chainActive.Tip()->GetBlockTime() < (GetTime() - MAX_FEE_ESTIMATION_TIP_AGE))
  385. return false;
  386. if (chainActive.Height() < pindexBestHeader->nHeight - 1)
  387. return false;
  388. return true;
  389. }
  390. /* Make mempool consistent after a reorg, by re-adding or recursively erasing
  391. * disconnected block transactions from the mempool, and also removing any
  392. * other transactions from the mempool that are no longer valid given the new
  393. * tip/height.
  394. *
  395. * Note: we assume that disconnectpool only contains transactions that are NOT
  396. * confirmed in the current chain nor already in the mempool (otherwise,
  397. * in-mempool descendants of such transactions would be removed).
  398. *
  399. * Passing fAddToMempool=false will skip trying to add the transactions back,
  400. * and instead just erase from the mempool as needed.
  401. */
  402. void UpdateMempoolForReorg(DisconnectedBlockTransactions &disconnectpool, bool fAddToMempool)
  403. {
  404. AssertLockHeld(cs_main);
  405. std::vector<uint256> vHashUpdate;
  406. // disconnectpool's insertion_order index sorts the entries from
  407. // oldest to newest, but the oldest entry will be the last tx from the
  408. // latest mined block that was disconnected.
  409. // Iterate disconnectpool in reverse, so that we add transactions
  410. // back to the mempool starting with the earliest transaction that had
  411. // been previously seen in a block.
  412. auto it = disconnectpool.queuedTx.get<insertion_order>().rbegin();
  413. while (it != disconnectpool.queuedTx.get<insertion_order>().rend()) {
  414. // ignore validation errors in resurrected transactions
  415. CValidationState stateDummy;
  416. if (!fAddToMempool || (*it)->IsCoinBase() ||
  417. !AcceptToMemoryPool(mempool, stateDummy, *it, nullptr /* pfMissingInputs */,
  418. nullptr /* plTxnReplaced */, true /* bypass_limits */, 0 /* nAbsurdFee */)) {
  419. // If the transaction doesn't make it in to the mempool, remove any
  420. // transactions that depend on it (which would now be orphans).
  421. mempool.removeRecursive(**it, MemPoolRemovalReason::REORG);
  422. } else if (mempool.exists((*it)->GetHash())) {
  423. vHashUpdate.push_back((*it)->GetHash());
  424. }
  425. ++it;
  426. }
  427. disconnectpool.queuedTx.clear();
  428. // AcceptToMemoryPool/addUnchecked all assume that new mempool entries have
  429. // no in-mempool children, which is generally not true when adding
  430. // previously-confirmed transactions back to the mempool.
  431. // UpdateTransactionsFromBlock finds descendants of any transactions in
  432. // the disconnectpool that were added back and cleans up the mempool state.
  433. mempool.UpdateTransactionsFromBlock(vHashUpdate);
  434. // We also need to remove any now-immature transactions
  435. mempool.removeForReorg(pcoinsTip.get(), chainActive.Tip()->nHeight + 1, STANDARD_LOCKTIME_VERIFY_FLAGS);
  436. // Re-limit mempool size, in case we added any transactions
  437. LimitMempoolSize(mempool, gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000, gArgs.GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60);
  438. }
  439. // Used to avoid mempool polluting consensus critical paths if CCoinsViewMempool
  440. // were somehow broken and returning the wrong scriptPubKeys
  441. static bool CheckInputsFromMempoolAndCache(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &view, CTxMemPool& pool,
  442. unsigned int flags, bool cacheSigStore, PrecomputedTransactionData& txdata) {
  443. AssertLockHeld(cs_main);
  444. // pool.cs should be locked already, but go ahead and re-take the lock here
  445. // to enforce that mempool doesn't change between when we check the view
  446. // and when we actually call through to CheckInputs
  447. LOCK(pool.cs);
  448. assert(!tx.IsCoinBase());
  449. for (const CTxIn& txin : tx.vin) {
  450. const Coin& coin = view.AccessCoin(txin.prevout);
  451. // At this point we haven't actually checked if the coins are all
  452. // available (or shouldn't assume we have, since CheckInputs does).
  453. // So we just return failure if the inputs are not available here,
  454. // and then only have to check equivalence for available inputs.
  455. if (coin.IsSpent()) return false;
  456. const CTransactionRef& txFrom = pool.get(txin.prevout.hash);
  457. if (txFrom) {
  458. assert(txFrom->GetHash() == txin.prevout.hash);
  459. assert(txFrom->vout.size() > txin.prevout.n);
  460. assert(txFrom->vout[txin.prevout.n] == coin.out);
  461. } else {
  462. const Coin& coinFromDisk = pcoinsTip->AccessCoin(txin.prevout);
  463. assert(!coinFromDisk.IsSpent());
  464. assert(coinFromDisk.out == coin.out);
  465. }
  466. }
  467. return CheckInputs(tx, state, view, true, flags, cacheSigStore, true, txdata);
  468. }
  469. static bool AcceptToMemoryPoolWorker(const CChainParams& chainparams, CTxMemPool& pool, CValidationState& state, const CTransactionRef& ptx,
  470. bool* pfMissingInputs, int64_t nAcceptTime, std::list<CTransactionRef>* plTxnReplaced,
  471. bool bypass_limits, const CAmount& nAbsurdFee, std::vector<COutPoint>& coins_to_uncache)
  472. {
  473. const CTransaction& tx = *ptx;
  474. const uint256 hash = tx.GetHash();
  475. AssertLockHeld(cs_main);
  476. LOCK(pool.cs); // mempool "read lock" (held through GetMainSignals().TransactionAddedToMempool())
  477. if (pfMissingInputs) {
  478. *pfMissingInputs = false;
  479. }
  480. if (!CheckTransaction(tx, state))
  481. return false; // state filled in by CheckTransaction
  482. // Coinbase is only valid in a block, not as a loose transaction
  483. if (tx.IsCoinBase())
  484. return state.DoS(100, false, REJECT_INVALID, "coinbase");
  485. // Reject transactions with witness before segregated witness activates (override with -prematurewitness)
  486. bool witnessEnabled = IsWitnessEnabled(chainActive.Tip(), chainparams.GetConsensus());
  487. if (!gArgs.GetBoolArg("-prematurewitness", false) && tx.HasWitness() && !witnessEnabled) {
  488. return state.DoS(0, false, REJECT_NONSTANDARD, "no-witness-yet", true);
  489. }
  490. // Rather not work on nonstandard transactions (unless -testnet/-regtest)
  491. std::string reason;
  492. if (fRequireStandard && !IsStandardTx(tx, reason, witnessEnabled))
  493. return state.DoS(0, false, REJECT_NONSTANDARD, reason);
  494. // Only accept nLockTime-using transactions that can be mined in the next
  495. // block; we don't want our mempool filled up with transactions that can't
  496. // be mined yet.
  497. if (!CheckFinalTx(tx, STANDARD_LOCKTIME_VERIFY_FLAGS))
  498. return state.DoS(0, false, REJECT_NONSTANDARD, "non-final");
  499. // is it already in the memory pool?
  500. if (pool.exists(hash)) {
  501. return state.Invalid(false, REJECT_DUPLICATE, "txn-already-in-mempool");
  502. }
  503. // Check for conflicts with in-memory transactions
  504. std::set<uint256> setConflicts;
  505. for (const CTxIn &txin : tx.vin)
  506. {
  507. auto itConflicting = pool.mapNextTx.find(txin.prevout);
  508. if (itConflicting != pool.mapNextTx.end())
  509. {
  510. const CTransaction *ptxConflicting = itConflicting->second;
  511. if (!setConflicts.count(ptxConflicting->GetHash()))
  512. {
  513. // Allow opt-out of transaction replacement by setting
  514. // nSequence > MAX_BIP125_RBF_SEQUENCE (SEQUENCE_FINAL-2) on all inputs.
  515. //
  516. // SEQUENCE_FINAL-1 is picked to still allow use of nLockTime by
  517. // non-replaceable transactions. All inputs rather than just one
  518. // is for the sake of multi-party protocols, where we don't
  519. // want a single party to be able to disable replacement.
  520. //
  521. // The opt-out ignores descendants as anyone relying on
  522. // first-seen mempool behavior should be checking all
  523. // unconfirmed ancestors anyway; doing otherwise is hopelessly
  524. // insecure.
  525. bool fReplacementOptOut = true;
  526. if (fEnableReplacement)
  527. {
  528. for (const CTxIn &_txin : ptxConflicting->vin)
  529. {
  530. if (_txin.nSequence <= MAX_BIP125_RBF_SEQUENCE)
  531. {
  532. fReplacementOptOut = false;
  533. break;
  534. }
  535. }
  536. }
  537. if (fReplacementOptOut) {
  538. return state.Invalid(false, REJECT_DUPLICATE, "txn-mempool-conflict");
  539. }
  540. setConflicts.insert(ptxConflicting->GetHash());
  541. }
  542. }
  543. }
  544. {
  545. CCoinsView dummy;
  546. CCoinsViewCache view(&dummy);
  547. LockPoints lp;
  548. CCoinsViewMemPool viewMemPool(pcoinsTip.get(), pool);
  549. view.SetBackend(viewMemPool);
  550. // do all inputs exist?
  551. for (const CTxIn txin : tx.vin) {
  552. if (!pcoinsTip->HaveCoinInCache(txin.prevout)) {
  553. coins_to_uncache.push_back(txin.prevout);
  554. }
  555. if (!view.HaveCoin(txin.prevout)) {
  556. // Are inputs missing because we already have the tx?
  557. for (size_t out = 0; out < tx.vout.size(); out++) {
  558. // Optimistically just do efficient check of cache for outputs
  559. if (pcoinsTip->HaveCoinInCache(COutPoint(hash, out))) {
  560. return state.Invalid(false, REJECT_DUPLICATE, "txn-already-known");
  561. }
  562. }
  563. // Otherwise assume this might be an orphan tx for which we just haven't seen parents yet
  564. if (pfMissingInputs) {
  565. *pfMissingInputs = true;
  566. }
  567. return false; // fMissingInputs and !state.IsInvalid() is used to detect this condition, don't set state.Invalid()
  568. }
  569. }
  570. // Bring the best block into scope
  571. view.GetBestBlock();
  572. // we have all inputs cached now, so switch back to dummy, so we don't need to keep lock on mempool
  573. view.SetBackend(dummy);
  574. // Only accept BIP68 sequence locked transactions that can be mined in the next
  575. // block; we don't want our mempool filled up with transactions that can't
  576. // be mined yet.
  577. // Must keep pool.cs for this unless we change CheckSequenceLocks to take a
  578. // CoinsViewCache instead of create its own
  579. if (!CheckSequenceLocks(tx, STANDARD_LOCKTIME_VERIFY_FLAGS, &lp))
  580. return state.DoS(0, false, REJECT_NONSTANDARD, "non-BIP68-final");
  581. CAmount nFees = 0;
  582. if (!Consensus::CheckTxInputs(tx, state, view, GetSpendHeight(view), nFees)) {
  583. return error("%s: Consensus::CheckTxInputs: %s, %s", __func__, tx.GetHash().ToString(), FormatStateMessage(state));
  584. }
  585. // Check for non-standard pay-to-script-hash in inputs
  586. if (fRequireStandard && !AreInputsStandard(tx, view))
  587. return state.Invalid(false, REJECT_NONSTANDARD, "bad-txns-nonstandard-inputs");
  588. // Check for non-standard witness in P2WSH
  589. if (tx.HasWitness() && fRequireStandard && !IsWitnessStandard(tx, view))
  590. return state.DoS(0, false, REJECT_NONSTANDARD, "bad-witness-nonstandard", true);
  591. int64_t nSigOpsCost = GetTransactionSigOpCost(tx, view, STANDARD_SCRIPT_VERIFY_FLAGS);
  592. // nModifiedFees includes any fee deltas from PrioritiseTransaction
  593. CAmount nModifiedFees = nFees;
  594. pool.ApplyDelta(hash, nModifiedFees);
  595. // Keep track of transactions that spend a coinbase, which we re-scan
  596. // during reorgs to ensure COINBASE_MATURITY is still met.
  597. bool fSpendsCoinbase = false;
  598. for (const CTxIn &txin : tx.vin) {
  599. const Coin &coin = view.AccessCoin(txin.prevout);
  600. if (coin.IsCoinBase()) {
  601. fSpendsCoinbase = true;
  602. break;
  603. }
  604. }
  605. CTxMemPoolEntry entry(ptx, nFees, nAcceptTime, chainActive.Height(),
  606. fSpendsCoinbase, nSigOpsCost, lp);
  607. unsigned int nSize = entry.GetTxSize();
  608. // Check that the transaction doesn't have an excessive number of
  609. // sigops, making it impossible to mine. Since the coinbase transaction
  610. // itself can contain sigops MAX_STANDARD_TX_SIGOPS is less than
  611. // MAX_BLOCK_SIGOPS; we still consider this an invalid rather than
  612. // merely non-standard transaction.
  613. if (nSigOpsCost > MAX_STANDARD_TX_SIGOPS_COST)
  614. return state.DoS(0, false, REJECT_NONSTANDARD, "bad-txns-too-many-sigops", false,
  615. strprintf("%d", nSigOpsCost));
  616. CAmount mempoolRejectFee = pool.GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFee(nSize);
  617. if (!bypass_limits && mempoolRejectFee > 0 && nModifiedFees < mempoolRejectFee) {
  618. return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "mempool min fee not met", false, strprintf("%d < %d", nFees, mempoolRejectFee));
  619. }
  620. // No transactions are allowed below minRelayTxFee except from disconnected blocks
  621. if (!bypass_limits && nModifiedFees < ::minRelayTxFee.GetFee(nSize)) {
  622. return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "min relay fee not met");
  623. }
  624. if (nAbsurdFee && nFees > nAbsurdFee)
  625. return state.Invalid(false,
  626. REJECT_HIGHFEE, "absurdly-high-fee",
  627. strprintf("%d > %d", nFees, nAbsurdFee));
  628. // Calculate in-mempool ancestors, up to a limit.
  629. CTxMemPool::setEntries setAncestors;
  630. size_t nLimitAncestors = gArgs.GetArg("-limitancestorcount", DEFAULT_ANCESTOR_LIMIT);
  631. size_t nLimitAncestorSize = gArgs.GetArg("-limitancestorsize", DEFAULT_ANCESTOR_SIZE_LIMIT)*1000;
  632. size_t nLimitDescendants = gArgs.GetArg("-limitdescendantcount", DEFAULT_DESCENDANT_LIMIT);
  633. size_t nLimitDescendantSize = gArgs.GetArg("-limitdescendantsize", DEFAULT_DESCENDANT_SIZE_LIMIT)*1000;
  634. std::string errString;
  635. if (!pool.CalculateMemPoolAncestors(entry, setAncestors, nLimitAncestors, nLimitAncestorSize, nLimitDescendants, nLimitDescendantSize, errString)) {
  636. return state.DoS(0, false, REJECT_NONSTANDARD, "too-long-mempool-chain", false, errString);
  637. }
  638. // A transaction that spends outputs that would be replaced by it is invalid. Now
  639. // that we have the set of all ancestors we can detect this
  640. // pathological case by making sure setConflicts and setAncestors don't
  641. // intersect.
  642. for (CTxMemPool::txiter ancestorIt : setAncestors)
  643. {
  644. const uint256 &hashAncestor = ancestorIt->GetTx().GetHash();
  645. if (setConflicts.count(hashAncestor))
  646. {
  647. return state.DoS(10, false,
  648. REJECT_INVALID, "bad-txns-spends-conflicting-tx", false,
  649. strprintf("%s spends conflicting transaction %s",
  650. hash.ToString(),
  651. hashAncestor.ToString()));
  652. }
  653. }
  654. // Check if it's economically rational to mine this transaction rather
  655. // than the ones it replaces.
  656. CAmount nConflictingFees = 0;
  657. size_t nConflictingSize = 0;
  658. uint64_t nConflictingCount = 0;
  659. CTxMemPool::setEntries allConflicting;
  660. // If we don't hold the lock allConflicting might be incomplete; the
  661. // subsequent RemoveStaged() and addUnchecked() calls don't guarantee
  662. // mempool consistency for us.
  663. const bool fReplacementTransaction = setConflicts.size();
  664. if (fReplacementTransaction)
  665. {
  666. CFeeRate newFeeRate(nModifiedFees, nSize);
  667. std::set<uint256> setConflictsParents;
  668. const int maxDescendantsToVisit = 100;
  669. CTxMemPool::setEntries setIterConflicting;
  670. for (const uint256 &hashConflicting : setConflicts)
  671. {
  672. CTxMemPool::txiter mi = pool.mapTx.find(hashConflicting);
  673. if (mi == pool.mapTx.end())
  674. continue;
  675. // Save these to avoid repeated lookups
  676. setIterConflicting.insert(mi);
  677. // Don't allow the replacement to reduce the feerate of the
  678. // mempool.
  679. //
  680. // We usually don't want to accept replacements with lower
  681. // feerates than what they replaced as that would lower the
  682. // feerate of the next block. Requiring that the feerate always
  683. // be increased is also an easy-to-reason about way to prevent
  684. // DoS attacks via replacements.
  685. //
  686. // The mining code doesn't (currently) take children into
  687. // account (CPFP) so we only consider the feerates of
  688. // transactions being directly replaced, not their indirect
  689. // descendants. While that does mean high feerate children are
  690. // ignored when deciding whether or not to replace, we do
  691. // require the replacement to pay more overall fees too,
  692. // mitigating most cases.
  693. CFeeRate oldFeeRate(mi->GetModifiedFee(), mi->GetTxSize());
  694. if (newFeeRate <= oldFeeRate)
  695. {
  696. return state.DoS(0, false,
  697. REJECT_INSUFFICIENTFEE, "insufficient fee", false,
  698. strprintf("rejecting replacement %s; new feerate %s <= old feerate %s",
  699. hash.ToString(),
  700. newFeeRate.ToString(),
  701. oldFeeRate.ToString()));
  702. }
  703. for (const CTxIn &txin : mi->GetTx().vin)
  704. {
  705. setConflictsParents.insert(txin.prevout.hash);
  706. }
  707. nConflictingCount += mi->GetCountWithDescendants();
  708. }
  709. // This potentially overestimates the number of actual descendants
  710. // but we just want to be conservative to avoid doing too much
  711. // work.
  712. if (nConflictingCount <= maxDescendantsToVisit) {
  713. // If not too many to replace, then calculate the set of
  714. // transactions that would have to be evicted
  715. for (CTxMemPool::txiter it : setIterConflicting) {
  716. pool.CalculateDescendants(it, allConflicting);
  717. }
  718. for (CTxMemPool::txiter it : allConflicting) {
  719. nConflictingFees += it->GetModifiedFee();
  720. nConflictingSize += it->GetTxSize();
  721. }
  722. } else {
  723. return state.DoS(0, false,
  724. REJECT_NONSTANDARD, "too many potential replacements", false,
  725. strprintf("rejecting replacement %s; too many potential replacements (%d > %d)\n",
  726. hash.ToString(),
  727. nConflictingCount,
  728. maxDescendantsToVisit));
  729. }
  730. for (unsigned int j = 0; j < tx.vin.size(); j++)
  731. {
  732. // We don't want to accept replacements that require low
  733. // feerate junk to be mined first. Ideally we'd keep track of
  734. // the ancestor feerates and make the decision based on that,
  735. // but for now requiring all new inputs to be confirmed works.
  736. if (!setConflictsParents.count(tx.vin[j].prevout.hash))
  737. {
  738. // Rather than check the UTXO set - potentially expensive -
  739. // it's cheaper to just check if the new input refers to a
  740. // tx that's in the mempool.
  741. if (pool.mapTx.find(tx.vin[j].prevout.hash) != pool.mapTx.end())
  742. return state.DoS(0, false,
  743. REJECT_NONSTANDARD, "replacement-adds-unconfirmed", false,
  744. strprintf("replacement %s adds unconfirmed input, idx %d",
  745. hash.ToString(), j));
  746. }
  747. }
  748. // The replacement must pay greater fees than the transactions it
  749. // replaces - if we did the bandwidth used by those conflicting
  750. // transactions would not be paid for.
  751. if (nModifiedFees < nConflictingFees)
  752. {
  753. return state.DoS(0, false,
  754. REJECT_INSUFFICIENTFEE, "insufficient fee", false,
  755. strprintf("rejecting replacement %s, less fees than conflicting txs; %s < %s",
  756. hash.ToString(), FormatMoney(nModifiedFees), FormatMoney(nConflictingFees)));
  757. }
  758. // Finally in addition to paying more fees than the conflicts the
  759. // new transaction must pay for its own bandwidth.
  760. CAmount nDeltaFees = nModifiedFees - nConflictingFees;
  761. if (nDeltaFees < ::incrementalRelayFee.GetFee(nSize))
  762. {
  763. return state.DoS(0, false,
  764. REJECT_INSUFFICIENTFEE, "insufficient fee", false,
  765. strprintf("rejecting replacement %s, not enough additional fees to relay; %s < %s",
  766. hash.ToString(),
  767. FormatMoney(nDeltaFees),
  768. FormatMoney(::incrementalRelayFee.GetFee(nSize))));
  769. }
  770. }
  771. unsigned int scriptVerifyFlags = STANDARD_SCRIPT_VERIFY_FLAGS;
  772. if (!chainparams.RequireStandard()) {
  773. scriptVerifyFlags = gArgs.GetArg("-promiscuousmempoolflags", scriptVerifyFlags);
  774. }
  775. // Check against previous transactions
  776. // This is done last to help prevent CPU exhaustion denial-of-service attacks.
  777. PrecomputedTransactionData txdata(tx);
  778. if (!CheckInputs(tx, state, view, true, scriptVerifyFlags, true, false, txdata)) {
  779. // SCRIPT_VERIFY_CLEANSTACK requires SCRIPT_VERIFY_WITNESS, so we
  780. // need to turn both off, and compare against just turning off CLEANSTACK
  781. // to see if the failure is specifically due to witness validation.
  782. CValidationState stateDummy; // Want reported failures to be from first CheckInputs
  783. if (!tx.HasWitness() && CheckInputs(tx, stateDummy, view, true, scriptVerifyFlags & ~(SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_CLEANSTACK), true, false, txdata) &&
  784. !CheckInputs(tx, stateDummy, view, true, scriptVerifyFlags & ~SCRIPT_VERIFY_CLEANSTACK, true, false, txdata)) {
  785. // Only the witness is missing, so the transaction itself may be fine.
  786. state.SetCorruptionPossible();
  787. }
  788. return false; // state filled in by CheckInputs
  789. }
  790. // Check again against the current block tip's script verification
  791. // flags to cache our script execution flags. This is, of course,
  792. // useless if the next block has different script flags from the
  793. // previous one, but because the cache tracks script flags for us it
  794. // will auto-invalidate and we'll just have a few blocks of extra
  795. // misses on soft-fork activation.
  796. //
  797. // This is also useful in case of bugs in the standard flags that cause
  798. // transactions to pass as valid when they're actually invalid. For
  799. // instance the STRICTENC flag was incorrectly allowing certain
  800. // CHECKSIG NOT scripts to pass, even though they were invalid.
  801. //
  802. // There is a similar check in CreateNewBlock() to prevent creating
  803. // invalid blocks (using TestBlockValidity), however allowing such
  804. // transactions into the mempool can be exploited as a DoS attack.
  805. unsigned int currentBlockScriptVerifyFlags = GetBlockScriptFlags(chainActive.Tip(), Params().GetConsensus());
  806. if (!CheckInputsFromMempoolAndCache(tx, state, view, pool, currentBlockScriptVerifyFlags, true, txdata))
  807. {
  808. // If we're using promiscuousmempoolflags, we may hit this normally
  809. // Check if current block has some flags that scriptVerifyFlags
  810. // does not before printing an ominous warning
  811. if (!(~scriptVerifyFlags & currentBlockScriptVerifyFlags)) {
  812. return error("%s: BUG! PLEASE REPORT THIS! ConnectInputs failed against latest-block but not STANDARD flags %s, %s",
  813. __func__, hash.ToString(), FormatStateMessage(state));
  814. } else {
  815. if (!CheckInputs(tx, state, view, true, MANDATORY_SCRIPT_VERIFY_FLAGS, true, false, txdata)) {
  816. return error("%s: ConnectInputs failed against MANDATORY but not STANDARD flags due to promiscuous mempool %s, %s",
  817. __func__, hash.ToString(), FormatStateMessage(state));
  818. } else {
  819. LogPrintf("Warning: -promiscuousmempool flags set to not include currently enforced soft forks, this may break mining or otherwise cause instability!\n");
  820. }
  821. }
  822. }
  823. // Remove conflicting transactions from the mempool
  824. for (const CTxMemPool::txiter it : allConflicting)
  825. {
  826. LogPrint(BCLog::MEMPOOL, "replacing tx %s with %s for %s BTC additional fees, %d delta bytes\n",
  827. it->GetTx().GetHash().ToString(),
  828. hash.ToString(),
  829. FormatMoney(nModifiedFees - nConflictingFees),
  830. (int)nSize - (int)nConflictingSize);
  831. if (plTxnReplaced)
  832. plTxnReplaced->push_back(it->GetSharedTx());
  833. }
  834. pool.RemoveStaged(allConflicting, false, MemPoolRemovalReason::REPLACED);
  835. // This transaction should only count for fee estimation if:
  836. // - it isn't a BIP 125 replacement transaction (may not be widely supported)
  837. // - it's not being readded during a reorg which bypasses typical mempool fee limits
  838. // - the node is not behind
  839. // - the transaction is not dependent on any other transactions in the mempool
  840. bool validForFeeEstimation = !fReplacementTransaction && !bypass_limits && IsCurrentForFeeEstimation() && pool.HasNoInputsOf(tx);
  841. // Store transaction in memory
  842. pool.addUnchecked(hash, entry, setAncestors, validForFeeEstimation);
  843. // trim mempool and check if tx was trimmed
  844. if (!bypass_limits) {
  845. LimitMempoolSize(pool, gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000, gArgs.GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60);
  846. if (!pool.exists(hash))
  847. return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "mempool full");
  848. }
  849. }
  850. GetMainSignals().TransactionAddedToMempool(ptx);
  851. return true;
  852. }
  853. /** (try to) add transaction to memory pool with a specified acceptance time **/
  854. static bool AcceptToMemoryPoolWithTime(const CChainParams& chainparams, CTxMemPool& pool, CValidationState &state, const CTransactionRef &tx,
  855. bool* pfMissingInputs, int64_t nAcceptTime, std::list<CTransactionRef>* plTxnReplaced,
  856. bool bypass_limits, const CAmount nAbsurdFee)
  857. {
  858. std::vector<COutPoint> coins_to_uncache;
  859. bool res = AcceptToMemoryPoolWorker(chainparams, pool, state, tx, pfMissingInputs, nAcceptTime, plTxnReplaced, bypass_limits, nAbsurdFee, coins_to_uncache);
  860. if (!res) {
  861. for (const COutPoint& hashTx : coins_to_uncache)
  862. pcoinsTip->Uncache(hashTx);
  863. }
  864. // After we've (potentially) uncached entries, ensure our coins cache is still within its size limits
  865. CValidationState stateDummy;
  866. FlushStateToDisk(chainparams, stateDummy, FLUSH_STATE_PERIODIC);
  867. return res;
  868. }
  869. bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransactionRef &tx,
  870. bool* pfMissingInputs, std::list<CTransactionRef>* plTxnReplaced,
  871. bool bypass_limits, const CAmount nAbsurdFee)
  872. {
  873. const CChainParams& chainparams = Params();
  874. return AcceptToMemoryPoolWithTime(chainparams, pool, state, tx, pfMissingInputs, GetTime(), plTxnReplaced, bypass_limits, nAbsurdFee);
  875. }
  876. /**
  877. * Return transaction in txOut, and if it was found inside a block, its hash is placed in hashBlock.
  878. * If blockIndex is provided, the transaction is fetched from the corresponding block.
  879. */
  880. bool GetTransaction(const uint256& hash, CTransactionRef& txOut, const Consensus::Params& consensusParams, uint256& hashBlock, bool fAllowSlow, CBlockIndex* blockIndex)
  881. {
  882. CBlockIndex* pindexSlow = blockIndex;
  883. LOCK(cs_main);
  884. if (!blockIndex) {
  885. CTransactionRef ptx = mempool.get(hash);
  886. if (ptx) {
  887. txOut = ptx;
  888. return true;
  889. }
  890. if (fTxIndex) {
  891. CDiskTxPos postx;
  892. if (pblocktree->ReadTxIndex(hash, postx)) {
  893. CAutoFile file(OpenBlockFile(postx, true), SER_DISK, CLIENT_VERSION);
  894. if (file.IsNull())
  895. return error("%s: OpenBlockFile failed", __func__);
  896. CBlockHeader header;
  897. try {
  898. file >> header;
  899. fseek(file.Get(), postx.nTxOffset, SEEK_CUR);
  900. file >> txOut;
  901. } catch (const std::exception& e) {
  902. return error("%s: Deserialize or I/O error - %s", __func__, e.what());
  903. }
  904. hashBlock = header.GetHash();
  905. if (txOut->GetHash() != hash)
  906. return error("%s: txid mismatch", __func__);
  907. return true;
  908. }
  909. // transaction not found in index, nothing more can be done
  910. return false;
  911. }
  912. if (fAllowSlow) { // use coin database to locate block that contains transaction, and scan it
  913. const Coin& coin = AccessByTxid(*pcoinsTip, hash);
  914. if (!coin.IsSpent()) pindexSlow = chainActive[coin.nHeight];
  915. }
  916. }
  917. if (pindexSlow) {
  918. CBlock block;
  919. if (ReadBlockFromDisk(block, pindexSlow, consensusParams)) {
  920. for (const auto& tx : block.vtx) {
  921. if (tx->GetHash() == hash) {
  922. txOut = tx;
  923. hashBlock = pindexSlow->GetBlockHash();
  924. return true;
  925. }
  926. }
  927. }
  928. }
  929. return false;
  930. }
  931. //////////////////////////////////////////////////////////////////////////////
  932. //
  933. // CBlock and CBlockIndex
  934. //
  935. static bool WriteBlockToDisk(const CBlock& block, CDiskBlockPos& pos, const CMessageHeader::MessageStartChars& messageStart)
  936. {
  937. // Open history file to append
  938. CAutoFile fileout(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION);
  939. if (fileout.IsNull())
  940. return error("WriteBlockToDisk: OpenBlockFile failed");
  941. // Write index header
  942. unsigned int nSize = GetSerializeSize(fileout, block);
  943. fileout << FLATDATA(messageStart) << nSize;
  944. // Write block
  945. long fileOutPos = ftell(fileout.Get());
  946. if (fileOutPos < 0)
  947. return error("WriteBlockToDisk: ftell failed");
  948. pos.nPos = (unsigned int)fileOutPos;
  949. fileout << block;
  950. return true;
  951. }
  952. bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos, const Consensus::Params& consensusParams)
  953. {
  954. block.SetNull();
  955. // Open history file to read
  956. CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);
  957. if (filein.IsNull())
  958. return error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString());
  959. // Read block
  960. try {
  961. filein >> block;
  962. }
  963. catch (const std::exception& e) {
  964. return error("%s: Deserialize or I/O error - %s at %s", __func__, e.what(), pos.ToString());
  965. }
  966. // Check the header
  967. if (!CheckProofOfWork(block.GetHash(), block.nBits, consensusParams))
  968. return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString());
  969. return true;
  970. }
  971. bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams)
  972. {
  973. CDiskBlockPos blockPos;
  974. {
  975. LOCK(cs_main);
  976. blockPos = pindex->GetBlockPos();
  977. }
  978. if (!ReadBlockFromDisk(block, blockPos, consensusParams))
  979. return false;
  980. if (block.GetHash() != pindex->GetBlockHash())
  981. return error("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() doesn't match index for %s at %s",
  982. pindex->ToString(), pindex->GetBlockPos().ToString());
  983. return true;
  984. }
  985. CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams)
  986. {
  987. int halvings = nHeight / consensusParams.nSubsidyHalvingInterval;
  988. // Force block reward to zero when right shift is undefined.
  989. if (halvings >= 64)
  990. return 0;
  991. CAmount nSubsidy = 50 * COIN;
  992. // Subsidy is cut in half every 210,000 blocks which will occur approximately every 4 years.
  993. nSubsidy >>= halvings;
  994. return nSubsidy;
  995. }
  996. bool IsInitialBlockDownload()
  997. {
  998. // Once this function has returned false, it must remain false.
  999. static std::atomic<bool> latchToFalse{false};
  1000. // Optimization: pre-test latch before taking the lock.
  1001. if (latchToFalse.load(std::memory_order_relaxed))
  1002. return false;
  1003. LOCK(cs_main);
  1004. if (latchToFalse.load(std::memory_order_relaxed))
  1005. return false;
  1006. if (fImporting || fReindex)
  1007. return true;
  1008. if (chainActive.Tip() == nullptr)
  1009. return true;
  1010. if (chainActive.Tip()->nChainWork < nMinimumChainWork)
  1011. return true;
  1012. if (chainActive.Tip()->GetBlockTime() < (GetTime() - nMaxTipAge))
  1013. return true;
  1014. LogPrintf("Leaving InitialBlockDownload (latching to false)\n");
  1015. latchToFalse.store(true, std::memory_order_relaxed);
  1016. return false;
  1017. }
  1018. CBlockIndex *pindexBestForkTip = nullptr, *pindexBestForkBase = nullptr;
  1019. static void AlertNotify(const std::string& strMessage)
  1020. {
  1021. uiInterface.NotifyAlertChanged();
  1022. std::string strCmd = gArgs.GetArg("-alertnotify", "");
  1023. if (strCmd.empty()) return;
  1024. // Alert text should be plain ascii coming from a trusted source, but to
  1025. // be safe we first strip anything not in safeChars, then add single quotes around
  1026. // the whole string before passing it to the shell:
  1027. std::string singleQuote("'");
  1028. std::string safeStatus = SanitizeString(strMessage);
  1029. safeStatus = singleQuote+safeStatus+singleQuote;
  1030. boost::replace_all(strCmd, "%s", safeStatus);
  1031. boost::thread t(runCommand, strCmd); // thread runs free
  1032. }
  1033. static void CheckForkWarningConditions()
  1034. {
  1035. AssertLockHeld(cs_main);
  1036. // Before we get past initial download, we cannot reliably alert about forks
  1037. // (we assume we don't get stuck on a fork before finishing our initial sync)
  1038. if (IsInitialBlockDownload())
  1039. return;
  1040. // If our best fork is no longer within 72 blocks (+/- 12 hours if no one mines it)
  1041. // of our head, drop it
  1042. if (pindexBestForkTip && chainActive.Height() - pindexBestForkTip->nHeight >= 72)
  1043. pindexBestForkTip = nullptr;
  1044. if (pindexBestForkTip || (pindexBestInvalid && pindexBestInvalid->nChainWork > chainActive.Tip()->nChainWork + (GetBlockProof(*chainActive.Tip()) * 6)))
  1045. {
  1046. if (!GetfLargeWorkForkFound() && pindexBestForkBase)
  1047. {
  1048. std::string warning = std::string("'Warning: Large-work fork detected, forking after block ") +
  1049. pindexBestForkBase->phashBlock->ToString() + std::string("'");
  1050. AlertNotify(warning);
  1051. }
  1052. if (pindexBestForkTip && pindexBestForkBase)
  1053. {
  1054. LogPrintf("%s: 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", __func__,
  1055. pindexBestForkBase->nHeight, pindexBestForkBase->phashBlock->ToString(),
  1056. pindexBestForkTip->nHeight, pindexBestForkTip->phashBlock->ToString());
  1057. SetfLargeWorkForkFound(true);
  1058. }
  1059. else
  1060. {
  1061. LogPrintf("%s: Warning: Found invalid chain at least ~6 blocks longer than our best chain.\nChain state database corruption likely.\n", __func__);
  1062. SetfLargeWorkInvalidChainFound(true);
  1063. }
  1064. }
  1065. else
  1066. {
  1067. SetfLargeWorkForkFound(false);
  1068. SetfLargeWorkInvalidChainFound(false);
  1069. }
  1070. }
  1071. static void CheckForkWarningConditionsOnNewFork(CBlockIndex* pindexNewForkTip)
  1072. {
  1073. AssertLockHeld(cs_main);
  1074. // If we are on a fork that is sufficiently large, set a warning flag
  1075. CBlockIndex* pfork = pindexNewForkTip;
  1076. CBlockIndex* plonger = chainActive.Tip();
  1077. while (pfork && pfork != plonger)
  1078. {
  1079. while (plonger && plonger->nHeight > pfork->nHeight)
  1080. plonger = plonger->pprev;
  1081. if (pfork == plonger)
  1082. break;
  1083. pfork = pfork->pprev;
  1084. }
  1085. // We define a condition where we should warn the user about as a fork of at least 7 blocks
  1086. // with a tip within 72 blocks (+/- 12 hours if no one mines it) of ours
  1087. // We use 7 blocks rather arbitrarily as it represents just under 10% of sustained network
  1088. // hash rate operating on the fork.
  1089. // or a chain that is entirely longer than ours and invalid (note that this should be detected by both)
  1090. // We define it this way because it allows us to only store the highest fork tip (+ base) which meets
  1091. // the 7-block condition and from this always have the most-likely-to-cause-warning fork
  1092. if (pfork && (!pindexBestForkTip || pindexNewForkTip->nHeight > pindexBestForkTip->nHeight) &&
  1093. pindexNewForkTip->nChainWork - pfork->nChainWork > (GetBlockProof(*pfork) * 7) &&
  1094. chainActive.Height() - pindexNewForkTip->nHeight < 72)
  1095. {
  1096. pindexBestForkTip = pindexNewForkTip;
  1097. pindexBestForkBase = pfork;
  1098. }
  1099. CheckForkWarningConditions();
  1100. }
  1101. void static InvalidChainFound(CBlockIndex* pindexNew)
  1102. {
  1103. if (!pindexBestInvalid || pindexNew->nChainWork > pindexBestInvalid->nChainWork)
  1104. pindexBestInvalid = pindexNew;
  1105. LogPrintf("%s: invalid block=%s height=%d log2_work=%.8g date=%s\n", __func__,
  1106. pindexNew->GetBlockHash().ToString(), pindexNew->nHeight,
  1107. log(pindexNew->nChainWork.getdouble())/log(2.0), DateTimeStrFormat("%Y-%m-%d %H:%M:%S",
  1108. pindexNew->GetBlockTime()));
  1109. CBlockIndex *tip = chainActive.Tip();
  1110. assert (tip);
  1111. LogPrintf("%s: current best=%s height=%d log2_work=%.8g date=%s\n", __func__,
  1112. tip->GetBlockHash().ToString(), chainActive.Height(), log(tip->nChainWork.getdouble())/log(2.0),
  1113. DateTimeStrFormat("%Y-%m-%d %H:%M:%S", tip->GetBlockTime()));
  1114. CheckForkWarningConditions();
  1115. }
  1116. void CChainState::InvalidBlockFound(CBlockIndex *pindex, const CValidationState &state) {
  1117. if (!state.CorruptionPossible()) {
  1118. pindex->nStatus |= BLOCK_FAILED_VALID;
  1119. g_failed_blocks.insert(pindex);
  1120. setDirtyBlockIndex.insert(pindex);
  1121. setBlockIndexCandidates.erase(pindex);
  1122. InvalidChainFound(pindex);
  1123. }
  1124. }
  1125. void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, CTxUndo &txundo, int nHeight)
  1126. {
  1127. // mark inputs spent
  1128. if (!tx.IsCoinBase()) {
  1129. txundo.vprevout.reserve(tx.vin.size());
  1130. for (const CTxIn &txin : tx.vin) {
  1131. txundo.vprevout.emplace_back();
  1132. bool is_spent = inputs.SpendCoin(txin.prevout, &txundo.vprevout.back());
  1133. assert(is_spent);
  1134. }
  1135. }
  1136. // add outputs
  1137. AddCoins(inputs, tx, nHeight);
  1138. }
  1139. void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, int nHeight)
  1140. {
  1141. CTxUndo txundo;
  1142. UpdateCoins(tx, inputs, txundo, nHeight);
  1143. }
  1144. bool CScriptCheck::operator()() {
  1145. const CScript &scriptSig = ptxTo->vin[nIn].scriptSig;
  1146. const CScriptWitness *witness = &ptxTo->vin[nIn].scriptWitness;
  1147. return VerifyScript(scriptSig, m_tx_out.scriptPubKey, witness, nFlags, CachingTransactionSignatureChecker(ptxTo, nIn, m_tx_out.nValue, cacheStore, *txdata), &error);
  1148. }
  1149. int GetSpendHeight(const CCoinsViewCache& inputs)
  1150. {
  1151. LOCK(cs_main);
  1152. CBlockIndex* pindexPrev = mapBlockIndex.find(inputs.GetBestBlock())->second;
  1153. return pindexPrev->nHeight + 1;
  1154. }
  1155. static CuckooCache::cache<uint256, SignatureCacheHasher> scriptExecutionCache;
  1156. static uint256 scriptExecutionCacheNonce(GetRandHash());
  1157. void InitScriptExecutionCache() {
  1158. // nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero,
  1159. // setup_bytes creates the minimum possible cache (2 elements).
  1160. size_t nMaxCacheSize = std::min(std::max((int64_t)0, gArgs.GetArg("-maxsigcachesize", DEFAULT_MAX_SIG_CACHE_SIZE) / 2), MAX_MAX_SIG_CACHE_SIZE) * ((size_t) 1 << 20);
  1161. size_t nElems = scriptExecutionCache.setup_bytes(nMaxCacheSize);
  1162. LogPrintf("Using %zu MiB out of %zu/2 requested for script execution cache, able to store %zu elements\n",
  1163. (nElems*sizeof(uint256)) >>20, (nMaxCacheSize*2)>>20, nElems);
  1164. }
  1165. /**
  1166. * Check whether all inputs of this transaction are valid (no double spends, scripts & sigs, amounts)
  1167. * This does not modify the UTXO set.
  1168. *
  1169. * If pvChecks is not nullptr, script checks are pushed onto it instead of being performed inline. Any
  1170. * script checks which are not necessary (eg due to script execution cache hits) are, obviously,
  1171. * not pushed onto pvChecks/run.
  1172. *
  1173. * Setting cacheSigStore/cacheFullScriptStore to false will remove elements from the corresponding cache
  1174. * which are matched. This is useful for checking blocks where we will likely never need the cache
  1175. * entry again.
  1176. *
  1177. * Non-static (and re-declared) in src/test/txvalidationcache_tests.cpp
  1178. */
  1179. bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, bool cacheSigStore, bool cacheFullScriptStore, PrecomputedTransactionData& txdata, std::vector<CScriptCheck> *pvChecks)
  1180. {
  1181. if (!tx.IsCoinBase())
  1182. {
  1183. if (pvChecks)
  1184. pvChecks->reserve(tx.vin.size());
  1185. // The first loop above does all the inexpensive checks.
  1186. // Only if ALL inputs pass do we perform expensive ECDSA signature checks.
  1187. // Helps prevent CPU exhaustion attacks.
  1188. // Skip script verification when connecting blocks under the
  1189. // assumevalid block. Assuming the assumevalid block is valid this
  1190. // is safe because block merkle hashes are still computed and checked,
  1191. // Of course, if an assumed valid block is invalid due to false scriptSigs
  1192. // this optimization would allow an invalid chain to be accepted.
  1193. if (fScriptChecks) {
  1194. // First check if script executions have been cached with the same
  1195. // flags. Note that this assumes that the inputs provided are
  1196. // correct (ie that the transaction hash which is in tx's prevouts
  1197. // properly commits to the scriptPubKey in the inputs view of that
  1198. // transaction).
  1199. uint256 hashCacheEntry;
  1200. // We only use the first 19 bytes of nonce to avoid a second SHA
  1201. // round - giving us 19 + 32 + 4 = 55 bytes (+ 8 + 1 = 64)
  1202. static_assert(55 - sizeof(flags) - 32 >= 128/8, "Want at least 128 bits of nonce for script execution cache");
  1203. CSHA256().Write(scriptExecutionCacheNonce.begin(), 55 - sizeof(flags) - 32).Write(tx.GetWitnessHash().begin(), 32).Write((unsigned char*)&flags, sizeof(flags)).Finalize(hashCacheEntry.begin());
  1204. AssertLockHeld(cs_main); //TODO: Remove this requirement by making CuckooCache not require external locks
  1205. if (scriptExecutionCache.contains(hashCacheEntry, !cacheFullScriptStore)) {
  1206. return true;
  1207. }
  1208. for (unsigned int i = 0; i < tx.vin.size(); i++) {
  1209. const COutPoint &prevout = tx.vin[i].prevout;
  1210. const Coin& coin = inputs.AccessCoin(prevout);
  1211. assert(!coin.IsSpent());
  1212. // We very carefully only pass in things to CScriptCheck which
  1213. // are clearly committed to by tx' witness hash. This provides
  1214. // a sanity check that our caching is not introducing consensus
  1215. // failures through additional data in, eg, the coins being
  1216. // spent being checked as a part of CScriptCheck.
  1217. // Verify signature
  1218. CScriptCheck check(coin.out, tx, i, flags, cacheSigStore, &txdata);
  1219. if (pvChecks) {
  1220. pvChecks->push_back(CScriptCheck());
  1221. check.swap(pvChecks->back());
  1222. } else if (!check()) {
  1223. if (flags & STANDARD_NOT_MANDATORY_VERIFY_FLAGS) {
  1224. // Check whether the failure was caused by a
  1225. // non-mandatory script verification check, such as
  1226. // non-standard DER encodings or non-null dummy
  1227. // arguments; if so, don't trigger DoS protection to
  1228. // avoid splitting the network between upgraded and
  1229. // non-upgraded nodes.
  1230. CScriptCheck check2(coin.out, tx, i,
  1231. flags & ~STANDARD_NOT_MANDATORY_VERIFY_FLAGS, cacheSigStore, &txdata);
  1232. if (check2())
  1233. return state.Invalid(false, REJECT_NONSTANDARD, strprintf("non-mandatory-script-verify-flag (%s)", ScriptErrorString(check.GetScriptError())));
  1234. }
  1235. // Failures of other flags indicate a transaction that is
  1236. // invalid in new blocks, e.g. an invalid P2SH. We DoS ban
  1237. // such nodes as they are not following the protocol. That
  1238. // said during an upgrade careful thought should be taken
  1239. // as to the correct behavior - we may want to continue
  1240. // peering with non-upgraded nodes even after soft-fork
  1241. // super-majority signaling has occurred.
  1242. return state.DoS(100,false, REJECT_INVALID, strprintf("mandatory-script-verify-flag-failed (%s)", ScriptErrorString(check.GetScriptError())));
  1243. }
  1244. }
  1245. if (cacheFullScriptStore && !pvChecks) {
  1246. // We executed all of the provided scripts, and were told to
  1247. // cache the result. Do so now.
  1248. scriptExecutionCache.insert(hashCacheEntry);
  1249. }
  1250. }
  1251. }
  1252. return true;
  1253. }
  1254. namespace {
  1255. bool UndoWriteToDisk(const CBlockUndo& blockundo, CDiskBlockPos& pos, const uint256& hashBlock, const CMessageHeader::MessageStartChars& messageStart)
  1256. {
  1257. // Open history file to append
  1258. CAutoFile fileout(OpenUndoFile(pos), SER_DISK, CLIENT_VERSION);
  1259. if (fileout.IsNull())
  1260. return error("%s: OpenUndoFile failed", __func__);
  1261. // Write index header
  1262. unsigned int nSize = GetSerializeSize(fileout, blockundo);
  1263. fileout << FLATDATA(messageStart) << nSize;
  1264. // Write undo data
  1265. long fileOutPos = ftell(fileout.Get());
  1266. if (fileOutPos < 0)
  1267. return error("%s: ftell failed", __func__);
  1268. pos.nPos = (unsigned int)fileOutPos;
  1269. fileout << blockundo;
  1270. // calculate & write checksum
  1271. CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
  1272. hasher << hashBlock;
  1273. hasher << blockundo;
  1274. fileout << hasher.GetHash();
  1275. return true;
  1276. }
  1277. static bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex *pindex)
  1278. {
  1279. CDiskBlockPos pos = pindex->GetUndoPos();
  1280. if (pos.IsNull()) {
  1281. return error("%s: no undo data available", __func__);
  1282. }
  1283. // Open history file to read
  1284. CAutoFile filein(OpenUndoFile(pos, true), SER_DISK, CLIENT_VERSION);
  1285. if (filein.IsNull())
  1286. return error("%s: OpenUndoFile failed", __func__);
  1287. // Read block
  1288. uint256 hashChecksum;
  1289. CHashVerifier<CAutoFile> verifier(&filein); // We need a CHashVerifier as reserializing may lose data
  1290. try {
  1291. verifier << pindex->pprev->GetBlockHash();
  1292. verifier >> blockundo;
  1293. filein >> hashChecksum;
  1294. }
  1295. catch (const std::exception& e) {
  1296. return error("%s: Deserialize or I/O error - %s", __func__, e.what());
  1297. }
  1298. // Verify checksum
  1299. if (hashChecksum != verifier.GetHash())
  1300. return error("%s: Checksum mismatch", __func__);
  1301. return true;
  1302. }
  1303. /** Abort with a message */
  1304. bool AbortNode(const std::string& strMessage, const std::string& userMessage="")
  1305. {
  1306. SetMiscWarning(strMessage);
  1307. LogPrintf("*** %s\n", strMessage);
  1308. uiInterface.ThreadSafeMessageBox(
  1309. userMessage.empty() ? _("Error: A fatal internal error occurred, see debug.log for details") : userMessage,
  1310. "", CClientUIInterface::MSG_ERROR);
  1311. StartShutdown();
  1312. return false;
  1313. }
  1314. bool AbortNode(CValidationState& state, const std::string& strMessage, const std::string& userMessage="")
  1315. {
  1316. AbortNode(strMessage, userMessage);
  1317. return state.Error(strMessage);
  1318. }
  1319. } // namespace
  1320. /**
  1321. * Restore the UTXO in a Coin at a given COutPoint
  1322. * @param undo The Coin to be restored.
  1323. * @param view The coins view to which to apply the changes.
  1324. * @param out The out point that corresponds to the tx input.
  1325. * @return A DisconnectResult as an int
  1326. */
  1327. int ApplyTxInUndo(Coin&& undo, CCoinsViewCache& view, const COutPoint& out)
  1328. {
  1329. bool fClean = true;
  1330. if (view.HaveCoin(out)) fClean = false; // overwriting transaction output
  1331. if (undo.nHeight == 0) {
  1332. // Missing undo metadata (height and coinbase). Older versions included this
  1333. // information only in undo records for the last spend of a transactions'
  1334. // outputs. This implies that it must be present for some other output of the same tx.
  1335. const Coin& alternate = AccessByTxid(view, out.hash);
  1336. if (!alternate.IsSpent()) {
  1337. undo.nHeight = alternate.nHeight;
  1338. undo.fCoinBase = alternate.fCoinBase;
  1339. } else {
  1340. return DISCONNECT_FAILED; // adding output for transaction without known metadata
  1341. }
  1342. }
  1343. // The potential_overwrite parameter to AddCoin is only allowed to be false if we know for
  1344. // sure that the coin did not already exist in the cache. As we have queried for that above
  1345. // using HaveCoin, we don't need to guess. When fClean is false, a coin already existed and
  1346. // it is an overwrite.
  1347. view.AddCoin(out, std::move(undo), !fClean);
  1348. return fClean ? DISCONNECT_OK : DISCONNECT_UNCLEAN;
  1349. }
  1350. /** Undo the effects of this block (with given index) on the UTXO set represented by coins.
  1351. * When FAILED is returned, view is left in an indeterminate state. */
  1352. DisconnectResult CChainState::DisconnectBlock(const CBlock& block, const CBlockIndex* pindex, CCoinsViewCache& view)
  1353. {
  1354. bool fClean = true;
  1355. CBlockUndo blockUndo;
  1356. if (!UndoReadFromDisk(blockUndo, pindex)) {
  1357. error("DisconnectBlock(): failure reading undo data");
  1358. return DISCONNECT_FAILED;
  1359. }
  1360. if (blockUndo.vtxundo.size() + 1 != block.vtx.size()) {
  1361. error("DisconnectBlock(): block and undo data inconsistent");
  1362. return DISCONNECT_FAILED;
  1363. }
  1364. // undo transactions in reverse order
  1365. for (int i = block.vtx.size() - 1; i >= 0; i--) {
  1366. const CTransaction &tx = *(block.vtx[i]);
  1367. uint256 hash = tx.GetHash();
  1368. bool is_coinbase = tx.IsCoinBase();
  1369. // Check that all outputs are available and match the outputs in the block itself
  1370. // exactly.
  1371. for (size_t o = 0; o < tx.vout.size(); o++) {
  1372. if (!tx.vout[o].scriptPubKey.IsUnspendable()) {
  1373. COutPoint out(hash, o);
  1374. Coin coin;
  1375. bool is_spent = view.SpendCoin(out, &coin);
  1376. if (!is_spent || tx.vout[o] != coin.out || pindex->nHeight != coin.nHeight || is_coinbase != coin.fCoinBase) {
  1377. fClean = false; // transaction output mismatch
  1378. }
  1379. }
  1380. }
  1381. // restore inputs
  1382. if (i > 0) { // not coinbases
  1383. CTxUndo &txundo = blockUndo.vtxundo[i-1];
  1384. if (txundo.vprevout.size() != tx.vin.size()) {
  1385. error("DisconnectBlock(): transaction and undo data inconsistent");
  1386. return DISCONNECT_FAILED;
  1387. }
  1388. for (unsigned int j = tx.vin.size(); j-- > 0;) {
  1389. const COutPoint &out = tx.vin[j].prevout;
  1390. int res = ApplyTxInUndo(std::move(txundo.vprevout[j]), view, out);
  1391. if (res == DISCONNECT_FAILED) return DISCONNECT_FAILED;
  1392. fClean = fClean && res != DISCONNECT_UNCLEAN;
  1393. }
  1394. // At this point, all of txundo.vprevout should have been moved out.
  1395. }
  1396. }
  1397. // move best block pointer to prevout block
  1398. view.SetBestBlock(pindex->pprev->GetBlockHash());
  1399. return fClean ? DISCONNECT_OK : DISCONNECT_UNCLEAN;
  1400. }
  1401. void static FlushBlockFile(bool fFinalize = false)
  1402. {
  1403. LOCK(cs_LastBlockFile);
  1404. CDiskBlockPos posOld(nLastBlockFile, 0);
  1405. FILE *fileOld = OpenBlockFile(posOld);
  1406. if (fileOld) {
  1407. if (fFinalize)
  1408. TruncateFile(fileOld, vinfoBlockFile[nLastBlockFile].nSize);
  1409. FileCommit(fileOld);
  1410. fclose(fileOld);
  1411. }
  1412. fileOld = OpenUndoFile(posOld);
  1413. if (fileOld) {
  1414. if (fFinalize)
  1415. TruncateFile(fileOld, vinfoBlockFile[nLastBlockFile].nUndoSize);
  1416. FileCommit(fileOld);
  1417. fclose(fileOld);
  1418. }
  1419. }
  1420. static bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize);
  1421. static bool WriteUndoDataForBlock(const CBlockUndo& blockundo, CValidationState& state, CBlockIndex* pindex, const CChainParams& chainparams)
  1422. {
  1423. // Write undo information to disk
  1424. if (pindex->GetUndoPos().IsNull()) {
  1425. CDiskBlockPos _pos;
  1426. if (!FindUndoPos(state, pindex->nFile, _pos, ::GetSerializeSize(blockundo, SER_DISK, CLIENT_VERSION) + 40))
  1427. return error("ConnectBlock(): FindUndoPos failed");
  1428. if (!UndoWriteToDisk(blockundo, _pos, pindex->pprev->GetBlockHash(), chainparams.MessageStart()))
  1429. return AbortNode(state, "Failed to write undo data");
  1430. // update nUndoPos in block index
  1431. pindex->nUndoPos = _pos.nPos;
  1432. pindex->nStatus |= BLOCK_HAVE_UNDO;
  1433. setDirtyBlockIndex.insert(pindex);
  1434. }
  1435. return true;
  1436. }
  1437. static bool WriteTxIndexDataForBlock(const CBlock& block, CValidationState& state, CBlockIndex* pindex)
  1438. {
  1439. if (!fTxIndex) return true;
  1440. CDiskTxPos pos(pindex->GetBlockPos(), GetSizeOfCompactSize(block.vtx.size()));
  1441. std::vector<std::pair<uint256, CDiskTxPos> > vPos;
  1442. vPos.reserve(block.vtx.size());
  1443. for (const CTransactionRef& tx : block.vtx)
  1444. {
  1445. vPos.push_back(std::make_pair(tx->GetHash(), pos));
  1446. pos.nTxOffset += ::GetSerializeSize(*tx, SER_DISK, CLIENT_VERSION);
  1447. }
  1448. if (!pblocktree->WriteTxIndex(vPos)) {
  1449. return AbortNode(state, "Failed to write transaction index");
  1450. }
  1451. return true;
  1452. }
  1453. static CCheckQueue<CScriptCheck> scriptcheckqueue(128);
  1454. void ThreadScriptCheck() {
  1455. RenameThread("bitcoin-scriptch");
  1456. scriptcheckqueue.Thread();
  1457. }
  1458. // Protected by cs_main
  1459. VersionBitsCache versionbitscache;
  1460. int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params)
  1461. {
  1462. LOCK(cs_main);
  1463. int32_t nVersion = VERSIONBITS_TOP_BITS;
  1464. for (int i = 0; i < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; i++) {
  1465. ThresholdState state = VersionBitsState(pindexPrev, params, (Consensus::DeploymentPos)i, versionbitscache);
  1466. if (state == THRESHOLD_LOCKED_IN || state == THRESHOLD_STARTED) {
  1467. nVersion |= VersionBitsMask(params, (Consensus::DeploymentPos)i);
  1468. }
  1469. }
  1470. return nVersion;
  1471. }
  1472. /**
  1473. * Threshold condition checker that triggers when unknown versionbits are seen on the network.
  1474. */
  1475. class WarningBitsConditionChecker : public AbstractThresholdConditionChecker
  1476. {
  1477. private:
  1478. int bit;
  1479. public:
  1480. explicit WarningBitsConditionChecker(int bitIn) : bit(bitIn) {}
  1481. int64_t BeginTime(const Consensus::Params& params) const override { return 0; }
  1482. int64_t EndTime(const Consensus::Params& params) const override { return std::numeric_limits<int64_t>::max(); }
  1483. int Period(const Consensus::Params& params) const override { return params.nMinerConfirmationWindow; }
  1484. int Threshold(const Consensus::Params& params) const override { return params.nRuleChangeActivationThreshold; }
  1485. bool Condition(const CBlockIndex* pindex, const Consensus::Params& params) const override
  1486. {
  1487. return ((pindex->nVersion & VERSIONBITS_TOP_MASK) == VERSIONBITS_TOP_BITS) &&
  1488. ((pindex->nVersion >> bit) & 1) != 0 &&
  1489. ((ComputeBlockVersion(pindex->pprev, params) >> bit) & 1) == 0;
  1490. }
  1491. };
  1492. // Protected by cs_main
  1493. static ThresholdConditionCache warningcache[VERSIONBITS_NUM_BITS];
  1494. static unsigned int GetBlockScriptFlags(const CBlockIndex* pindex, const Consensus::Params& consensusparams) {
  1495. AssertLockHeld(cs_main);
  1496. unsigned int flags = SCRIPT_VERIFY_NONE;
  1497. // Start enforcing P2SH (BIP16)
  1498. if (pindex->nHeight >= consensusparams.BIP16Height) {
  1499. flags |= SCRIPT_VERIFY_P2SH;
  1500. }
  1501. // Start enforcing the DERSIG (BIP66) rule
  1502. if (pindex->nHeight >= consensusparams.BIP66Height) {
  1503. flags |= SCRIPT_VERIFY_DERSIG;
  1504. }
  1505. // Start enforcing CHECKLOCKTIMEVERIFY (BIP65) rule
  1506. if (pindex->nHeight >= consensusparams.BIP65Height) {
  1507. flags |= SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY;
  1508. }
  1509. // Start enforcing BIP68 (sequence locks) and BIP112 (CHECKSEQUENCEVERIFY) using versionbits logic.
  1510. if (VersionBitsState(pindex->pprev, consensusparams, Consensus::DEPLOYMENT_CSV, versionbitscache) == THRESHOLD_ACTIVE) {
  1511. flags |= SCRIPT_VERIFY_CHECKSEQUENCEVERIFY;
  1512. }
  1513. // Start enforcing WITNESS rules using versionbits logic.
  1514. if (IsWitnessEnabled(pindex->pprev, consensusparams)) {
  1515. flags |= SCRIPT_VERIFY_WITNESS;
  1516. flags |= SCRIPT_VERIFY_NULLDUMMY;
  1517. }
  1518. return flags;
  1519. }
  1520. static int64_t nTimeCheck = 0;
  1521. static int64_t nTimeForks = 0;
  1522. static int64_t nTimeVerify = 0;
  1523. static int64_t nTimeConnect = 0;
  1524. static int64_t nTimeIndex = 0;
  1525. static int64_t nTimeCallbacks = 0;
  1526. static int64_t nTimeTotal = 0;
  1527. static int64_t nBlocksTotal = 0;
  1528. /** Apply the effects of this block (with given index) on the UTXO set represented by coins.
  1529. * Validity checks that depend on the UTXO set are also done; ConnectBlock()
  1530. * can fail if those validity checks fail (among other reasons). */
  1531. bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pindex,
  1532. CCoinsViewCache& view, const CChainParams& chainparams, bool fJustCheck)
  1533. {
  1534. AssertLockHeld(cs_main);
  1535. assert(pindex);
  1536. // pindex->phashBlock can be null if called by CreateNewBlock/TestBlockValidity
  1537. assert((pindex->phashBlock == nullptr) ||
  1538. (*pindex->phashBlock == block.GetHash()));
  1539. int64_t nTimeStart = GetTimeMicros();
  1540. // Check it again in case a previous version let a bad block in
  1541. // NOTE: We don't currently (re-)invoke ContextualCheckBlock() or
  1542. // ContextualCheckBlockHeader() here. This means that if we add a new
  1543. // consensus rule that is enforced in one of those two functions, then we
  1544. // may have let in a block that violates the rule prior to updating the
  1545. // software, and we would NOT be enforcing the rule here. Fully solving
  1546. // upgrade from one software version to the next after a consensus rule
  1547. // change is potentially tricky and issue-specific (see RewindBlockIndex()
  1548. // for one general approach that was used for BIP 141 deployment).
  1549. // Also, currently the rule against blocks more than 2 hours in the future
  1550. // is enforced in ContextualCheckBlockHeader(); we wouldn't want to
  1551. // re-enforce that rule here (at least until we make it impossible for
  1552. // GetAdjustedTime() to go backward).
  1553. if (!CheckBlock(block, state, chainparams.GetConsensus(), !fJustCheck, !fJustCheck))
  1554. return error("%s: Consensus::CheckBlock: %s", __func__, FormatStateMessage(state));
  1555. // verify that the view's current state corresponds to the previous block
  1556. uint256 hashPrevBlock = pindex->pprev == nullptr ? uint256() : pindex->pprev->GetBlockHash();
  1557. assert(hashPrevBlock == view.GetBestBlock());
  1558. // Special case for the genesis block, skipping connection of its transactions
  1559. // (its coinbase is unspendable)
  1560. if (block.GetHash() == chainparams.GetConsensus().hashGenesisBlock) {
  1561. if (!fJustCheck)
  1562. view.SetBestBlock(pindex->GetBlockHash());
  1563. return true;
  1564. }
  1565. nBlocksTotal++;
  1566. bool fScriptChecks = true;
  1567. if (!hashAssumeValid.IsNull()) {
  1568. // We've been configured with the hash of a block which has been externally verified to have a valid history.
  1569. // A suitable default value is included with the software and updated from time to time. Because validity
  1570. // relative to a piece of software is an objective fact these defaults can be easily reviewed.
  1571. // This setting doesn't force the selection of any particular chain but makes validating some faster by
  1572. // effectively caching the result of part of the verification.
  1573. BlockMap::const_iterator it = mapBlockIndex.find(hashAssumeValid);
  1574. if (it != mapBlockIndex.end()) {
  1575. if (it->second->GetAncestor(pindex->nHeight) == pindex &&
  1576. pindexBestHeader->GetAncestor(pindex->nHeight) == pindex &&
  1577. pindexBestHeader->nChainWork >= nMinimumChainWork) {
  1578. // This block is a member of the assumed verified chain and an ancestor of the best header.
  1579. // The equivalent time check discourages hash power from extorting the network via DOS attack
  1580. // into accepting an invalid block through telling users they must manually set assumevalid.
  1581. // Requiring a software change or burying the invalid block, regardless of the setting, makes
  1582. // it hard to hide the implication of the demand. This also avoids having release candidates
  1583. // that are hardly doing any signature verification at all in testing without having to
  1584. // artificially set the default assumed verified block further back.
  1585. // The test against nMinimumChainWork prevents the skipping when denied access to any chain at
  1586. // least as good as the expected chain.
  1587. fScriptChecks = (GetBlockProofEquivalentTime(*pindexBestHeader, *pindex, *pindexBestHeader, chainparams.GetConsensus()) <= 60 * 60 * 24 * 7 * 2);
  1588. }
  1589. }
  1590. }
  1591. int64_t nTime1 = GetTimeMicros(); nTimeCheck += nTime1 - nTimeStart;
  1592. LogPrint(BCLog::BENCH, " - Sanity checks: %.2fms [%.2fs (%.2fms/blk)]\n", MILLI * (nTime1 - nTimeStart), nTimeCheck * MICRO, nTimeCheck * MILLI / nBlocksTotal);
  1593. // Do not allow blocks that contain transactions which 'overwrite' older transactions,
  1594. // unless those are already completely spent.
  1595. // If such overwrites are allowed, coinbases and transactions depending upon those
  1596. // can be duplicated to remove the ability to spend the first instance -- even after
  1597. // being sent to another address.
  1598. // See BIP30 and http://r6.ca/blog/20120206T005236Z.html for more information.
  1599. // This logic is not necessary for memory pool transactions, as AcceptToMemoryPool
  1600. // already refuses previously-known transaction ids entirely.
  1601. // This rule was originally applied to all blocks with a timestamp after March 15, 2012, 0:00 UTC.
  1602. // Now that the whole chain is irreversibly beyond that time it is applied to all blocks except the
  1603. // two in the chain that violate it. This prevents exploiting the issue against nodes during their
  1604. // initial block download.
  1605. bool fEnforceBIP30 = (!pindex->phashBlock) || // Enforce on CreateNewBlock invocations which don't have a hash.
  1606. !((pindex->nHeight==91842 && pindex->GetBlockHash() == uint256S("0x00000000000a4d0a398161ffc163c503763b1f4360639393e0e4c8e300e0caec")) ||
  1607. (pindex->nHeight==91880 && pindex->GetBlockHash() == uint256S("0x00000000000743f190a18c5577a3c2d2a1f610ae9601ac046a38084ccb7cd721")));
  1608. // Once BIP34 activated it was not possible to create new duplicate coinbases and thus other than starting
  1609. // with the 2 existing duplicate coinbase pairs, not possible to create overwriting txs. But by the
  1610. // time BIP34 activated, in each of the existing pairs the duplicate coinbase had overwritten the first
  1611. // before the first had been spent. Since those coinbases are sufficiently buried its no longer possible to create further
  1612. // duplicate transactions descending from the known pairs either.
  1613. // If we're on the known chain at height greater than where BIP34 activated, we can save the db accesses needed for the BIP30 check.
  1614. assert(pindex->pprev);
  1615. CBlockIndex *pindexBIP34height = pindex->pprev->GetAncestor(chainparams.GetConsensus().BIP34Height);
  1616. //Only continue to enforce if we're below BIP34 activation height or the block hash at that height doesn't correspond.
  1617. fEnforceBIP30 = fEnforceBIP30 && (!pindexBIP34height || !(pindexBIP34height->GetBlockHash() == chainparams.GetConsensus().BIP34Hash));
  1618. if (fEnforceBIP30) {
  1619. for (const auto& tx : block.vtx) {
  1620. for (size_t o = 0; o < tx->vout.size(); o++) {
  1621. if (view.HaveCoin(COutPoint(tx->GetHash(), o))) {
  1622. return state.DoS(100, error("ConnectBlock(): tried to overwrite transaction"),
  1623. REJECT_INVALID, "bad-txns-BIP30");
  1624. }
  1625. }
  1626. }
  1627. }
  1628. // Start enforcing BIP68 (sequence locks) and BIP112 (CHECKSEQUENCEVERIFY) using versionbits logic.
  1629. int nLockTimeFlags = 0;
  1630. if (VersionBitsState(pindex->pprev, chainparams.GetConsensus(), Consensus::DEPLOYMENT_CSV, versionbitscache) == THRESHOLD_ACTIVE) {
  1631. nLockTimeFlags |= LOCKTIME_VERIFY_SEQUENCE;
  1632. }
  1633. // Get the script flags for this block
  1634. unsigned int flags = GetBlockScriptFlags(pindex, chainparams.GetConsensus());
  1635. int64_t nTime2 = GetTimeMicros(); nTimeForks += nTime2 - nTime1;
  1636. LogPrint(BCLog::BENCH, " - Fork checks: %.2fms [%.2fs (%.2fms/blk)]\n", MILLI * (nTime2 - nTime1), nTimeForks * MICRO, nTimeForks * MILLI / nBlocksTotal);
  1637. CBlockUndo blockundo;
  1638. CCheckQueueControl<CScriptCheck> control(fScriptChecks && nScriptCheckThreads ? &scriptcheckqueue : nullptr);
  1639. std::vector<int> prevheights;
  1640. CAmount nFees = 0;
  1641. int nInputs = 0;
  1642. int64_t nSigOpsCost = 0;
  1643. blockundo.vtxundo.reserve(block.vtx.size() - 1);
  1644. std::vector<PrecomputedTransactionData> txdata;
  1645. txdata.reserve(block.vtx.size()); // Required so that pointers to individual PrecomputedTransactionData don't get invalidated
  1646. for (unsigned int i = 0; i < block.vtx.size(); i++)
  1647. {
  1648. const CTransaction &tx = *(block.vtx[i]);
  1649. nInputs += tx.vin.size();
  1650. if (!tx.IsCoinBase())
  1651. {
  1652. CAmount txfee = 0;
  1653. if (!Consensus::CheckTxInputs(tx, state, view, pindex->nHeight, txfee)) {
  1654. return error("%s: Consensus::CheckTxInputs: %s, %s", __func__, tx.GetHash().ToString(), FormatStateMessage(state));
  1655. }
  1656. nFees += txfee;
  1657. if (!MoneyRange(nFees)) {
  1658. return state.DoS(100, error("%s: accumulated fee in the block out of range.", __func__),
  1659. REJECT_INVALID, "bad-txns-accumulated-fee-outofrange");
  1660. }
  1661. // Check that transaction is BIP68 final
  1662. // BIP68 lock checks (as opposed to nLockTime checks) must
  1663. // be in ConnectBlock because they require the UTXO set
  1664. prevheights.resize(tx.vin.size());
  1665. for (size_t j = 0; j < tx.vin.size(); j++) {
  1666. prevheights[j] = view.AccessCoin(tx.vin[j].prevout).nHeight;
  1667. }
  1668. if (!SequenceLocks(tx, nLockTimeFlags, &prevheights, *pindex)) {
  1669. return state.DoS(100, error("%s: contains a non-BIP68-final transaction", __func__),
  1670. REJECT_INVALID, "bad-txns-nonfinal");
  1671. }
  1672. }
  1673. // GetTransactionSigOpCost counts 3 types of sigops:
  1674. // * legacy (always)
  1675. // * p2sh (when P2SH enabled in flags and excludes coinbase)
  1676. // * witness (when witness enabled in flags and excludes coinbase)
  1677. nSigOpsCost += GetTransactionSigOpCost(tx, view, flags);
  1678. if (nSigOpsCost > MAX_BLOCK_SIGOPS_COST)
  1679. return state.DoS(100, error("ConnectBlock(): too many sigops"),
  1680. REJECT_INVALID, "bad-blk-sigops");
  1681. txdata.emplace_back(tx);
  1682. if (!tx.IsCoinBase())
  1683. {
  1684. std::vector<CScriptCheck> vChecks;
  1685. bool fCacheResults = fJustCheck; /* Don't cache results if we're actually connecting blocks (still consult the cache, though) */
  1686. if (!CheckInputs(tx, state, view, fScriptChecks, flags, fCacheResults, fCacheResults, txdata[i], nScriptCheckThreads ? &vChecks : nullptr))
  1687. return error("ConnectBlock(): CheckInputs on %s failed with %s",
  1688. tx.GetHash().ToString(), FormatStateMessage(state));
  1689. control.Add(vChecks);
  1690. }
  1691. CTxUndo undoDummy;
  1692. if (i > 0) {
  1693. blockundo.vtxundo.push_back(CTxUndo());
  1694. }
  1695. UpdateCoins(tx, view, i == 0 ? undoDummy : blockundo.vtxundo.back(), pindex->nHeight);
  1696. }
  1697. int64_t nTime3 = GetTimeMicros(); nTimeConnect += nTime3 - nTime2;
  1698. LogPrint(BCLog::BENCH, " - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) [%.2fs (%.2fms/blk)]\n", (unsigned)block.vtx.size(), MILLI * (nTime3 - nTime2), MILLI * (nTime3 - nTime2) / block.vtx.size(), nInputs <= 1 ? 0 : MILLI * (nTime3 - nTime2) / (nInputs-1), nTimeConnect * MICRO, nTimeConnect * MILLI / nBlocksTotal);
  1699. CAmount blockReward = nFees + GetBlockSubsidy(pindex->nHeight, chainparams.GetConsensus());
  1700. if (block.vtx[0]->GetValueOut() > blockReward)
  1701. return state.DoS(100,
  1702. error("ConnectBlock(): coinbase pays too much (actual=%d vs limit=%d)",
  1703. block.vtx[0]->GetValueOut(), blockReward),
  1704. REJECT_INVALID, "bad-cb-amount");
  1705. if (!control.Wait())
  1706. return state.DoS(100, error("%s: CheckQueue failed", __func__), REJECT_INVALID, "block-validation-failed");
  1707. int64_t nTime4 = GetTimeMicros(); nTimeVerify += nTime4 - nTime2;
  1708. LogPrint(BCLog::BENCH, " - Verify %u txins: %.2fms (%.3fms/txin) [%.2fs (%.2fms/blk)]\n", nInputs - 1, MILLI * (nTime4 - nTime2), nInputs <= 1 ? 0 : MILLI * (nTime4 - nTime2) / (nInputs-1), nTimeVerify * MICRO, nTimeVerify * MILLI / nBlocksTotal);
  1709. if (fJustCheck)
  1710. return true;
  1711. if (!WriteUndoDataForBlock(blockundo, state, pindex, chainparams))
  1712. return false;
  1713. if (!pindex->IsValid(BLOCK_VALID_SCRIPTS)) {
  1714. pindex->RaiseValidity(BLOCK_VALID_SCRIPTS);
  1715. setDirtyBlockIndex.insert(pindex);
  1716. }
  1717. if (!WriteTxIndexDataForBlock(block, state, pindex))
  1718. return false;
  1719. assert(pindex->phashBlock);
  1720. // add this block to the view's block chain
  1721. view.SetBestBlock(pindex->GetBlockHash());
  1722. int64_t nTime5 = GetTimeMicros(); nTimeIndex += nTime5 - nTime4;
  1723. LogPrint(BCLog::BENCH, " - Index writing: %.2fms [%.2fs (%.2fms/blk)]\n", MILLI * (nTime5 - nTime4), nTimeIndex * MICRO, nTimeIndex * MILLI / nBlocksTotal);
  1724. int64_t nTime6 = GetTimeMicros(); nTimeCallbacks += nTime6 - nTime5;
  1725. LogPrint(BCLog::BENCH, " - Callbacks: %.2fms [%.2fs (%.2fms/blk)]\n", MILLI * (nTime6 - nTime5), nTimeCallbacks * MICRO, nTimeCallbacks * MILLI / nBlocksTotal);
  1726. return true;
  1727. }
  1728. /**
  1729. * Update the on-disk chain state.
  1730. * The caches and indexes are flushed depending on the mode we're called with
  1731. * if they're too large, if it's been a while since the last write,
  1732. * or always and in all cases if we're in prune mode and are deleting files.
  1733. */
  1734. bool static FlushStateToDisk(const CChainParams& chainparams, CValidationState &state, FlushStateMode mode, int nManualPruneHeight) {
  1735. int64_t nMempoolUsage = mempool.DynamicMemoryUsage();
  1736. LOCK(cs_main);
  1737. static int64_t nLastWrite = 0;
  1738. static int64_t nLastFlush = 0;
  1739. static int64_t nLastSetChain = 0;
  1740. std::set<int> setFilesToPrune;
  1741. bool fFlushForPrune = false;
  1742. bool fDoFullFlush = false;
  1743. int64_t nNow = 0;
  1744. try {
  1745. {
  1746. LOCK(cs_LastBlockFile);
  1747. if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0) && !fReindex) {
  1748. if (nManualPruneHeight > 0) {
  1749. FindFilesToPruneManual(setFilesToPrune, nManualPruneHeight);
  1750. } else {
  1751. FindFilesToPrune(setFilesToPrune, chainparams.PruneAfterHeight());
  1752. fCheckForPruning = false;
  1753. }
  1754. if (!setFilesToPrune.empty()) {
  1755. fFlushForPrune = true;
  1756. if (!fHavePruned) {
  1757. pblocktree->WriteFlag("prunedblockfiles", true);
  1758. fHavePruned = true;
  1759. }
  1760. }
  1761. }
  1762. nNow = GetTimeMicros();
  1763. // Avoid writing/flushing immediately after startup.
  1764. if (nLastWrite == 0) {
  1765. nLastWrite = nNow;
  1766. }
  1767. if (nLastFlush == 0) {
  1768. nLastFlush = nNow;
  1769. }
  1770. if (nLastSetChain == 0) {
  1771. nLastSetChain = nNow;
  1772. }
  1773. int64_t nMempoolSizeMax = gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000;
  1774. int64_t cacheSize = pcoinsTip->DynamicMemoryUsage();
  1775. int64_t nTotalSpace = nCoinCacheUsage + std::max<int64_t>(nMempoolSizeMax - nMempoolUsage, 0);
  1776. // The cache is large and we're within 10% and 10 MiB of the limit, but we have time now (not in the middle of a block processing).
  1777. bool fCacheLarge = mode == FLUSH_STATE_PERIODIC && cacheSize > std::max((9 * nTotalSpace) / 10, nTotalSpace - MAX_BLOCK_COINSDB_USAGE * 1024 * 1024);
  1778. // The cache is over the limit, we have to write now.
  1779. bool fCacheCritical = mode == FLUSH_STATE_IF_NEEDED && cacheSize > nTotalSpace;
  1780. // It's been a while since we wrote the block index to disk. Do this frequently, so we don't need to redownload after a crash.
  1781. bool fPeriodicWrite = mode == FLUSH_STATE_PERIODIC && nNow > nLastWrite + (int64_t)DATABASE_WRITE_INTERVAL * 1000000;
  1782. // It's been very long since we flushed the cache. Do this infrequently, to optimize cache usage.
  1783. bool fPeriodicFlush = mode == FLUSH_STATE_PERIODIC && nNow > nLastFlush + (int64_t)DATABASE_FLUSH_INTERVAL * 1000000;
  1784. // Combine all conditions that result in a full cache flush.
  1785. fDoFullFlush = (mode == FLUSH_STATE_ALWAYS) || fCacheLarge || fCacheCritical || fPeriodicFlush || fFlushForPrune;
  1786. // Write blocks and block index to disk.
  1787. if (fDoFullFlush || fPeriodicWrite) {
  1788. // Depend on nMinDiskSpace to ensure we can write block index
  1789. if (!CheckDiskSpace(0))
  1790. return state.Error("out of disk space");
  1791. // First make sure all block and undo data is flushed to disk.
  1792. FlushBlockFile();
  1793. // Then update all block file information (which may refer to block and undo files).
  1794. {
  1795. std::vector<std::pair<int, const CBlockFileInfo*> > vFiles;
  1796. vFiles.reserve(setDirtyFileInfo.size());
  1797. for (std::set<int>::iterator it = setDirtyFileInfo.begin(); it != setDirtyFileInfo.end(); ) {
  1798. vFiles.push_back(std::make_pair(*it, &vinfoBlockFile[*it]));
  1799. setDirtyFileInfo.erase(it++);
  1800. }
  1801. std::vector<const CBlockIndex*> vBlocks;
  1802. vBlocks.reserve(setDirtyBlockIndex.size());
  1803. for (std::set<CBlockIndex*>::iterator it = setDirtyBlockIndex.begin(); it != setDirtyBlockIndex.end(); ) {
  1804. vBlocks.push_back(*it);
  1805. setDirtyBlockIndex.erase(it++);
  1806. }
  1807. if (!pblocktree->WriteBatchSync(vFiles, nLastBlockFile, vBlocks)) {
  1808. return AbortNode(state, "Failed to write to block index database");
  1809. }
  1810. }
  1811. // Finally remove any pruned files
  1812. if (fFlushForPrune)
  1813. UnlinkPrunedFiles(setFilesToPrune);
  1814. nLastWrite = nNow;
  1815. }
  1816. // Flush best chain related state. This can only be done if the blocks / block index write was also done.
  1817. if (fDoFullFlush && !pcoinsTip->GetBestBlock().IsNull()) {
  1818. // Typical Coin structures on disk are around 48 bytes in size.
  1819. // Pushing a new one to the database can cause it to be written
  1820. // twice (once in the log, and once in the tables). This is already
  1821. // an overestimation, as most will delete an existing entry or
  1822. // overwrite one. Still, use a conservative safety factor of 2.
  1823. if (!CheckDiskSpace(48 * 2 * 2 * pcoinsTip->GetCacheSize()))
  1824. return state.Error("out of disk space");
  1825. // Flush the chainstate (which may refer to block index entries).
  1826. if (!pcoinsTip->Flush())
  1827. return AbortNode(state, "Failed to write to coin database");
  1828. nLastFlush = nNow;
  1829. }
  1830. }
  1831. if (fDoFullFlush || ((mode == FLUSH_STATE_ALWAYS || mode == FLUSH_STATE_PERIODIC) && nNow > nLastSetChain + (int64_t)DATABASE_WRITE_INTERVAL * 1000000)) {
  1832. // Update best block in wallet (so we can detect restored wallets).
  1833. GetMainSignals().SetBestChain(chainActive.GetLocator());
  1834. nLastSetChain = nNow;
  1835. }
  1836. } catch (const std::runtime_error& e) {
  1837. return AbortNode(state, std::string("System error while flushing: ") + e.what());
  1838. }
  1839. return true;
  1840. }
  1841. void FlushStateToDisk() {
  1842. CValidationState state;
  1843. const CChainParams& chainparams = Params();
  1844. FlushStateToDisk(chainparams, state, FLUSH_STATE_ALWAYS);
  1845. }
  1846. void PruneAndFlush() {
  1847. CValidationState state;
  1848. fCheckForPruning = true;
  1849. const CChainParams& chainparams = Params();
  1850. FlushStateToDisk(chainparams, state, FLUSH_STATE_NONE);
  1851. }
  1852. static void DoWarning(const std::string& strWarning)
  1853. {
  1854. static bool fWarned = false;
  1855. SetMiscWarning(strWarning);
  1856. if (!fWarned) {
  1857. AlertNotify(strWarning);
  1858. fWarned = true;
  1859. }
  1860. }
  1861. /** Check warning conditions and do some notifications on new chain tip set. */
  1862. void static UpdateTip(const CBlockIndex *pindexNew, const CChainParams& chainParams) {
  1863. // New best block
  1864. mempool.AddTransactionsUpdated(1);
  1865. cvBlockChange.notify_all();
  1866. std::vector<std::string> warningMessages;
  1867. if (!IsInitialBlockDownload())
  1868. {
  1869. int nUpgraded = 0;
  1870. const CBlockIndex* pindex = pindexNew;
  1871. for (int bit = 0; bit < VERSIONBITS_NUM_BITS; bit++) {
  1872. WarningBitsConditionChecker checker(bit);
  1873. ThresholdState state = checker.GetStateFor(pindex, chainParams.GetConsensus(), warningcache[bit]);
  1874. if (state == THRESHOLD_ACTIVE || state == THRESHOLD_LOCKED_IN) {
  1875. const std::string strWarning = strprintf(_("Warning: unknown new rules activated (versionbit %i)"), bit);
  1876. if (state == THRESHOLD_ACTIVE) {
  1877. DoWarning(strWarning);
  1878. } else {
  1879. warningMessages.push_back(strWarning);
  1880. }
  1881. }
  1882. }
  1883. // Check the version of the last 100 blocks to see if we need to upgrade:
  1884. for (int i = 0; i < 100 && pindex != nullptr; i++)
  1885. {
  1886. int32_t nExpectedVersion = ComputeBlockVersion(pindex->pprev, chainParams.GetConsensus());
  1887. if (pindex->nVersion > VERSIONBITS_LAST_OLD_BLOCK_VERSION && (pindex->nVersion & ~nExpectedVersion) != 0)
  1888. ++nUpgraded;
  1889. pindex = pindex->pprev;
  1890. }
  1891. if (nUpgraded > 0)
  1892. warningMessages.push_back(strprintf(_("%d of last 100 blocks have unexpected version"), nUpgraded));
  1893. if (nUpgraded > 100/2)
  1894. {
  1895. std::string strWarning = _("Warning: Unknown block versions being mined! It's possible unknown rules are in effect");
  1896. // notify GetWarnings(), called by Qt and the JSON-RPC code to warn the user:
  1897. DoWarning(strWarning);
  1898. }
  1899. }
  1900. LogPrintf("%s: new best=%s height=%d version=0x%08x log2_work=%.8g tx=%lu date='%s' progress=%f cache=%.1fMiB(%utxo)", __func__,
  1901. pindexNew->GetBlockHash().ToString(), pindexNew->nHeight, pindexNew->nVersion,
  1902. log(pindexNew->nChainWork.getdouble())/log(2.0), (unsigned long)pindexNew->nChainTx,
  1903. DateTimeStrFormat("%Y-%m-%d %H:%M:%S", pindexNew->GetBlockTime()),
  1904. GuessVerificationProgress(chainParams.TxData(), pindexNew), pcoinsTip->DynamicMemoryUsage() * (1.0 / (1<<20)), pcoinsTip->GetCacheSize());
  1905. if (!warningMessages.empty())
  1906. LogPrintf(" warning='%s'", boost::algorithm::join(warningMessages, ", "));
  1907. LogPrintf("\n");
  1908. }
  1909. /** Disconnect chainActive's tip.
  1910. * After calling, the mempool will be in an inconsistent state, with
  1911. * transactions from disconnected blocks being added to disconnectpool. You
  1912. * should make the mempool consistent again by calling UpdateMempoolForReorg.
  1913. * with cs_main held.
  1914. *
  1915. * If disconnectpool is nullptr, then no disconnected transactions are added to
  1916. * disconnectpool (note that the caller is responsible for mempool consistency
  1917. * in any case).
  1918. */
  1919. bool CChainState::DisconnectTip(CValidationState& state, const CChainParams& chainparams, DisconnectedBlockTransactions *disconnectpool)
  1920. {
  1921. CBlockIndex *pindexDelete = chainActive.Tip();
  1922. assert(pindexDelete);
  1923. // Read block from disk.
  1924. std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
  1925. CBlock& block = *pblock;
  1926. if (!ReadBlockFromDisk(block, pindexDelete, chainparams.GetConsensus()))
  1927. return AbortNode(state, "Failed to read block");
  1928. // Apply the block atomically to the chain state.
  1929. int64_t nStart = GetTimeMicros();
  1930. {
  1931. CCoinsViewCache view(pcoinsTip.get());
  1932. assert(view.GetBestBlock() == pindexDelete->GetBlockHash());
  1933. if (DisconnectBlock(block, pindexDelete, view) != DISCONNECT_OK)
  1934. return error("DisconnectTip(): DisconnectBlock %s failed", pindexDelete->GetBlockHash().ToString());
  1935. bool flushed = view.Flush();
  1936. assert(flushed);
  1937. }
  1938. LogPrint(BCLog::BENCH, "- Disconnect block: %.2fms\n", (GetTimeMicros() - nStart) * MILLI);
  1939. // Write the chain state to disk, if necessary.
  1940. if (!FlushStateToDisk(chainparams, state, FLUSH_STATE_IF_NEEDED))
  1941. return false;
  1942. if (disconnectpool) {
  1943. // Save transactions to re-add to mempool at end of reorg
  1944. for (auto it = block.vtx.rbegin(); it != block.vtx.rend(); ++it) {
  1945. disconnectpool->addTransaction(*it);
  1946. }
  1947. while (disconnectpool->DynamicMemoryUsage() > MAX_DISCONNECTED_TX_POOL_SIZE * 1000) {
  1948. // Drop the earliest entry, and remove its children from the mempool.
  1949. auto it = disconnectpool->queuedTx.get<insertion_order>().begin();
  1950. mempool.removeRecursive(**it, MemPoolRemovalReason::REORG);
  1951. disconnectpool->removeEntry(it);
  1952. }
  1953. }
  1954. chainActive.SetTip(pindexDelete->pprev);
  1955. UpdateTip(pindexDelete->pprev, chainparams);
  1956. // Let wallets know transactions went from 1-confirmed to
  1957. // 0-confirmed or conflicted:
  1958. GetMainSignals().BlockDisconnected(pblock);
  1959. return true;
  1960. }
  1961. static int64_t nTimeReadFromDisk = 0;
  1962. static int64_t nTimeConnectTotal = 0;
  1963. static int64_t nTimeFlush = 0;
  1964. static int64_t nTimeChainState = 0;
  1965. static int64_t nTimePostConnect = 0;
  1966. struct PerBlockConnectTrace {
  1967. CBlockIndex* pindex = nullptr;
  1968. std::shared_ptr<const CBlock> pblock;
  1969. std::shared_ptr<std::vector<CTransactionRef>> conflictedTxs;
  1970. PerBlockConnectTrace() : conflictedTxs(std::make_shared<std::vector<CTransactionRef>>()) {}
  1971. };
  1972. /**
  1973. * Used to track blocks whose transactions were applied to the UTXO state as a
  1974. * part of a single ActivateBestChainStep call.
  1975. *
  1976. * This class also tracks transactions that are removed from the mempool as
  1977. * conflicts (per block) and can be used to pass all those transactions
  1978. * through SyncTransaction.
  1979. *
  1980. * This class assumes (and asserts) that the conflicted transactions for a given
  1981. * block are added via mempool callbacks prior to the BlockConnected() associated
  1982. * with those transactions. If any transactions are marked conflicted, it is
  1983. * assumed that an associated block will always be added.
  1984. *
  1985. * This class is single-use, once you call GetBlocksConnected() you have to throw
  1986. * it away and make a new one.
  1987. */
  1988. class ConnectTrace {
  1989. private:
  1990. std::vector<PerBlockConnectTrace> blocksConnected;
  1991. CTxMemPool &pool;
  1992. public:
  1993. explicit ConnectTrace(CTxMemPool &_pool) : blocksConnected(1), pool(_pool) {
  1994. pool.NotifyEntryRemoved.connect(boost::bind(&ConnectTrace::NotifyEntryRemoved, this, _1, _2));
  1995. }
  1996. ~ConnectTrace() {
  1997. pool.NotifyEntryRemoved.disconnect(boost::bind(&ConnectTrace::NotifyEntryRemoved, this, _1, _2));
  1998. }
  1999. void BlockConnected(CBlockIndex* pindex, std::shared_ptr<const CBlock> pblock) {
  2000. assert(!blocksConnected.back().pindex);
  2001. assert(pindex);
  2002. assert(pblock);
  2003. blocksConnected.back().pindex = pindex;
  2004. blocksConnected.back().pblock = std::move(pblock);
  2005. blocksConnected.emplace_back();
  2006. }
  2007. std::vector<PerBlockConnectTrace>& GetBlocksConnected() {
  2008. // We always keep one extra block at the end of our list because
  2009. // blocks are added after all the conflicted transactions have
  2010. // been filled in. Thus, the last entry should always be an empty
  2011. // one waiting for the transactions from the next block. We pop
  2012. // the last entry here to make sure the list we return is sane.
  2013. assert(!blocksConnected.back().pindex);
  2014. assert(blocksConnected.back().conflictedTxs->empty());
  2015. blocksConnected.pop_back();
  2016. return blocksConnected;
  2017. }
  2018. void NotifyEntryRemoved(CTransactionRef txRemoved, MemPoolRemovalReason reason) {
  2019. assert(!blocksConnected.back().pindex);
  2020. if (reason == MemPoolRemovalReason::CONFLICT) {
  2021. blocksConnected.back().conflictedTxs->emplace_back(std::move(txRemoved));
  2022. }
  2023. }
  2024. };
  2025. /**
  2026. * Connect a new block to chainActive. pblock is either nullptr or a pointer to a CBlock
  2027. * corresponding to pindexNew, to bypass loading it again from disk.
  2028. *
  2029. * The block is added to connectTrace if connection succeeds.
  2030. */
  2031. bool CChainState::ConnectTip(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace, DisconnectedBlockTransactions &disconnectpool)
  2032. {
  2033. assert(pindexNew->pprev == chainActive.Tip());
  2034. // Read block from disk.
  2035. int64_t nTime1 = GetTimeMicros();
  2036. std::shared_ptr<const CBlock> pthisBlock;
  2037. if (!pblock) {
  2038. std::shared_ptr<CBlock> pblockNew = std::make_shared<CBlock>();
  2039. if (!ReadBlockFromDisk(*pblockNew, pindexNew, chainparams.GetConsensus()))
  2040. return AbortNode(state, "Failed to read block");
  2041. pthisBlock = pblockNew;
  2042. } else {
  2043. pthisBlock = pblock;
  2044. }
  2045. const CBlock& blockConnecting = *pthisBlock;
  2046. // Apply the block atomically to the chain state.
  2047. int64_t nTime2 = GetTimeMicros(); nTimeReadFromDisk += nTime2 - nTime1;
  2048. int64_t nTime3;
  2049. LogPrint(BCLog::BENCH, " - Load block from disk: %.2fms [%.2fs]\n", (nTime2 - nTime1) * MILLI, nTimeReadFromDisk * MICRO);
  2050. {
  2051. CCoinsViewCache view(pcoinsTip.get());
  2052. bool rv = ConnectBlock(blockConnecting, state, pindexNew, view, chainparams);
  2053. GetMainSignals().BlockChecked(blockConnecting, state);
  2054. if (!rv) {
  2055. if (state.IsInvalid())
  2056. InvalidBlockFound(pindexNew, state);
  2057. return error("ConnectTip(): ConnectBlock %s failed", pindexNew->GetBlockHash().ToString());
  2058. }
  2059. nTime3 = GetTimeMicros(); nTimeConnectTotal += nTime3 - nTime2;
  2060. LogPrint(BCLog::BENCH, " - Connect total: %.2fms [%.2fs (%.2fms/blk)]\n", (nTime3 - nTime2) * MILLI, nTimeConnectTotal * MICRO, nTimeConnectTotal * MILLI / nBlocksTotal);
  2061. bool flushed = view.Flush();
  2062. assert(flushed);
  2063. }
  2064. int64_t nTime4 = GetTimeMicros(); nTimeFlush += nTime4 - nTime3;
  2065. LogPrint(BCLog::BENCH, " - Flush: %.2fms [%.2fs (%.2fms/blk)]\n", (nTime4 - nTime3) * MILLI, nTimeFlush * MICRO, nTimeFlush * MILLI / nBlocksTotal);
  2066. // Write the chain state to disk, if necessary.
  2067. if (!FlushStateToDisk(chainparams, state, FLUSH_STATE_IF_NEEDED))
  2068. return false;
  2069. int64_t nTime5 = GetTimeMicros(); nTimeChainState += nTime5 - nTime4;
  2070. LogPrint(BCLog::BENCH, " - Writing chainstate: %.2fms [%.2fs (%.2fms/blk)]\n", (nTime5 - nTime4) * MILLI, nTimeChainState * MICRO, nTimeChainState * MILLI / nBlocksTotal);
  2071. // Remove conflicting transactions from the mempool.;
  2072. mempool.removeForBlock(blockConnecting.vtx, pindexNew->nHeight);
  2073. disconnectpool.removeForBlock(blockConnecting.vtx);
  2074. // Update chainActive & related variables.
  2075. chainActive.SetTip(pindexNew);
  2076. UpdateTip(pindexNew, chainparams);
  2077. int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
  2078. LogPrint(BCLog::BENCH, " - Connect postprocess: %.2fms [%.2fs (%.2fms/blk)]\n", (nTime6 - nTime5) * MILLI, nTimePostConnect * MICRO, nTimePostConnect * MILLI / nBlocksTotal);
  2079. LogPrint(BCLog::BENCH, "- Connect block: %.2fms [%.2fs (%.2fms/blk)]\n", (nTime6 - nTime1) * MILLI, nTimeTotal * MICRO, nTimeTotal * MILLI / nBlocksTotal);
  2080. connectTrace.BlockConnected(pindexNew, std::move(pthisBlock));
  2081. return true;
  2082. }
  2083. /**
  2084. * Return the tip of the chain with the most work in it, that isn't
  2085. * known to be invalid (it's however far from certain to be valid).
  2086. */
  2087. CBlockIndex* CChainState::FindMostWorkChain() {
  2088. do {
  2089. CBlockIndex *pindexNew = nullptr;
  2090. // Find the best candidate header.
  2091. {
  2092. std::set<CBlockIndex*, CBlockIndexWorkComparator>::reverse_iterator it = setBlockIndexCandidates.rbegin();
  2093. if (it == setBlockIndexCandidates.rend())
  2094. return nullptr;
  2095. pindexNew = *it;
  2096. }
  2097. // Check whether all blocks on the path between the currently active chain and the candidate are valid.
  2098. // Just going until the active chain is an optimization, as we know all blocks in it are valid already.
  2099. CBlockIndex *pindexTest = pindexNew;
  2100. bool fInvalidAncestor = false;
  2101. while (pindexTest && !chainActive.Contains(pindexTest)) {
  2102. assert(pindexTest->nChainTx || pindexTest->nHeight == 0);
  2103. // Pruned nodes may have entries in setBlockIndexCandidates for
  2104. // which block files have been deleted. Remove those as candidates
  2105. // for the most work chain if we come across them; we can't switch
  2106. // to a chain unless we have all the non-active-chain parent blocks.
  2107. bool fFailedChain = pindexTest->nStatus & BLOCK_FAILED_MASK;
  2108. bool fMissingData = !(pindexTest->nStatus & BLOCK_HAVE_DATA);
  2109. if (fFailedChain || fMissingData) {
  2110. // Candidate chain is not usable (either invalid or missing data)
  2111. if (fFailedChain && (pindexBestInvalid == nullptr || pindexNew->nChainWork > pindexBestInvalid->nChainWork))
  2112. pindexBestInvalid = pindexNew;
  2113. CBlockIndex *pindexFailed = pindexNew;
  2114. // Remove the entire chain from the set.
  2115. while (pindexTest != pindexFailed) {
  2116. if (fFailedChain) {
  2117. pindexFailed->nStatus |= BLOCK_FAILED_CHILD;
  2118. } else if (fMissingData) {
  2119. // If we're missing data, then add back to mapBlocksUnlinked,
  2120. // so that if the block arrives in the future we can try adding
  2121. // to setBlockIndexCandidates again.
  2122. mapBlocksUnlinked.insert(std::make_pair(pindexFailed->pprev, pindexFailed));
  2123. }
  2124. setBlockIndexCandidates.erase(pindexFailed);
  2125. pindexFailed = pindexFailed->pprev;
  2126. }
  2127. setBlockIndexCandidates.erase(pindexTest);
  2128. fInvalidAncestor = true;
  2129. break;
  2130. }
  2131. pindexTest = pindexTest->pprev;
  2132. }
  2133. if (!fInvalidAncestor)
  2134. return pindexNew;
  2135. } while(true);
  2136. }
  2137. /** Delete all entries in setBlockIndexCandidates that are worse than the current tip. */
  2138. void CChainState::PruneBlockIndexCandidates() {
  2139. // Note that we can't delete the current block itself, as we may need to return to it later in case a
  2140. // reorganization to a better block fails.
  2141. std::set<CBlockIndex*, CBlockIndexWorkComparator>::iterator it = setBlockIndexCandidates.begin();
  2142. while (it != setBlockIndexCandidates.end() && setBlockIndexCandidates.value_comp()(*it, chainActive.Tip())) {
  2143. setBlockIndexCandidates.erase(it++);
  2144. }
  2145. // Either the current tip or a successor of it we're working towards is left in setBlockIndexCandidates.
  2146. assert(!setBlockIndexCandidates.empty());
  2147. }
  2148. /**
  2149. * Try to make some progress towards making pindexMostWork the active block.
  2150. * pblock is either nullptr or a pointer to a CBlock corresponding to pindexMostWork.
  2151. */
  2152. bool CChainState::ActivateBestChainStep(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool& fInvalidFound, ConnectTrace& connectTrace)
  2153. {
  2154. AssertLockHeld(cs_main);
  2155. const CBlockIndex *pindexOldTip = chainActive.Tip();
  2156. const CBlockIndex *pindexFork = chainActive.FindFork(pindexMostWork);
  2157. // Disconnect active blocks which are no longer in the best chain.
  2158. bool fBlocksDisconnected = false;
  2159. DisconnectedBlockTransactions disconnectpool;
  2160. while (chainActive.Tip() && chainActive.Tip() != pindexFork) {
  2161. if (!DisconnectTip(state, chainparams, &disconnectpool)) {
  2162. // This is likely a fatal error, but keep the mempool consistent,
  2163. // just in case. Only remove from the mempool in this case.
  2164. UpdateMempoolForReorg(disconnectpool, false);
  2165. return false;
  2166. }
  2167. fBlocksDisconnected = true;
  2168. }
  2169. // Build list of new blocks to connect.
  2170. std::vector<CBlockIndex*> vpindexToConnect;
  2171. bool fContinue = true;
  2172. int nHeight = pindexFork ? pindexFork->nHeight : -1;
  2173. while (fContinue && nHeight != pindexMostWork->nHeight) {
  2174. // Don't iterate the entire list of potential improvements toward the best tip, as we likely only need
  2175. // a few blocks along the way.
  2176. int nTargetHeight = std::min(nHeight + 32, pindexMostWork->nHeight);
  2177. vpindexToConnect.clear();
  2178. vpindexToConnect.reserve(nTargetHeight - nHeight);
  2179. CBlockIndex *pindexIter = pindexMostWork->GetAncestor(nTargetHeight);
  2180. while (pindexIter && pindexIter->nHeight != nHeight) {
  2181. vpindexToConnect.push_back(pindexIter);
  2182. pindexIter = pindexIter->pprev;
  2183. }
  2184. nHeight = nTargetHeight;
  2185. // Connect new blocks.
  2186. for (CBlockIndex *pindexConnect : reverse_iterate(vpindexToConnect)) {
  2187. if (!ConnectTip(state, chainparams, pindexConnect, pindexConnect == pindexMostWork ? pblock : std::shared_ptr<const CBlock>(), connectTrace, disconnectpool)) {
  2188. if (state.IsInvalid()) {
  2189. // The block violates a consensus rule.
  2190. if (!state.CorruptionPossible())
  2191. InvalidChainFound(vpindexToConnect.back());
  2192. state = CValidationState();
  2193. fInvalidFound = true;
  2194. fContinue = false;
  2195. break;
  2196. } else {
  2197. // A system error occurred (disk space, database error, ...).
  2198. // Make the mempool consistent with the current tip, just in case
  2199. // any observers try to use it before shutdown.
  2200. UpdateMempoolForReorg(disconnectpool, false);
  2201. return false;
  2202. }
  2203. } else {
  2204. PruneBlockIndexCandidates();
  2205. if (!pindexOldTip || chainActive.Tip()->nChainWork > pindexOldTip->nChainWork) {
  2206. // We're in a better position than we were. Return temporarily to release the lock.
  2207. fContinue = false;
  2208. break;
  2209. }
  2210. }
  2211. }
  2212. }
  2213. if (fBlocksDisconnected) {
  2214. // If any blocks were disconnected, disconnectpool may be non empty. Add
  2215. // any disconnected transactions back to the mempool.
  2216. UpdateMempoolForReorg(disconnectpool, true);
  2217. }
  2218. mempool.check(pcoinsTip.get());
  2219. // Callbacks/notifications for a new best chain.
  2220. if (fInvalidFound)
  2221. CheckForkWarningConditionsOnNewFork(vpindexToConnect.back());
  2222. else
  2223. CheckForkWarningConditions();
  2224. return true;
  2225. }
  2226. static void NotifyHeaderTip() {
  2227. bool fNotify = false;
  2228. bool fInitialBlockDownload = false;
  2229. static CBlockIndex* pindexHeaderOld = nullptr;
  2230. CBlockIndex* pindexHeader = nullptr;
  2231. {
  2232. LOCK(cs_main);
  2233. pindexHeader = pindexBestHeader;
  2234. if (pindexHeader != pindexHeaderOld) {
  2235. fNotify = true;
  2236. fInitialBlockDownload = IsInitialBlockDownload();
  2237. pindexHeaderOld = pindexHeader;
  2238. }
  2239. }
  2240. // Send block tip changed notifications without cs_main
  2241. if (fNotify) {
  2242. uiInterface.NotifyHeaderTip(fInitialBlockDownload, pindexHeader);
  2243. }
  2244. }
  2245. /**
  2246. * Make the best chain active, in multiple steps. The result is either failure
  2247. * or an activated best chain. pblock is either nullptr or a pointer to a block
  2248. * that is already loaded (to avoid loading it again from disk).
  2249. */
  2250. bool CChainState::ActivateBestChain(CValidationState &state, const CChainParams& chainparams, std::shared_ptr<const CBlock> pblock) {
  2251. // Note that while we're often called here from ProcessNewBlock, this is
  2252. // far from a guarantee. Things in the P2P/RPC will often end up calling
  2253. // us in the middle of ProcessNewBlock - do not assume pblock is set
  2254. // sanely for performance or correctness!
  2255. AssertLockNotHeld(cs_main);
  2256. CBlockIndex *pindexMostWork = nullptr;
  2257. CBlockIndex *pindexNewTip = nullptr;
  2258. int nStopAtHeight = gArgs.GetArg("-stopatheight", DEFAULT_STOPATHEIGHT);
  2259. do {
  2260. boost::this_thread::interruption_point();
  2261. if (GetMainSignals().CallbacksPending() > 10) {
  2262. // Block until the validation queue drains. This should largely
  2263. // never happen in normal operation, however may happen during
  2264. // reindex, causing memory blowup if we run too far ahead.
  2265. SyncWithValidationInterfaceQueue();
  2266. }
  2267. const CBlockIndex *pindexFork;
  2268. bool fInitialDownload;
  2269. {
  2270. LOCK(cs_main);
  2271. ConnectTrace connectTrace(mempool); // Destructed before cs_main is unlocked
  2272. CBlockIndex *pindexOldTip = chainActive.Tip();
  2273. if (pindexMostWork == nullptr) {
  2274. pindexMostWork = FindMostWorkChain();
  2275. }
  2276. // Whether we have anything to do at all.
  2277. if (pindexMostWork == nullptr || pindexMostWork == chainActive.Tip())
  2278. return true;
  2279. bool fInvalidFound = false;
  2280. std::shared_ptr<const CBlock> nullBlockPtr;
  2281. if (!ActivateBestChainStep(state, chainparams, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : nullBlockPtr, fInvalidFound, connectTrace))
  2282. return false;
  2283. if (fInvalidFound) {
  2284. // Wipe cache, we may need another branch now.
  2285. pindexMostWork = nullptr;
  2286. }
  2287. pindexNewTip = chainActive.Tip();
  2288. pindexFork = chainActive.FindFork(pindexOldTip);
  2289. fInitialDownload = IsInitialBlockDownload();
  2290. for (const PerBlockConnectTrace& trace : connectTrace.GetBlocksConnected()) {
  2291. assert(trace.pblock && trace.pindex);
  2292. GetMainSignals().BlockConnected(trace.pblock, trace.pindex, trace.conflictedTxs);
  2293. }
  2294. // Notify external listeners about the new tip.
  2295. // Enqueue while holding cs_main to ensure that UpdatedBlockTip is called in the order in which blocks are connected
  2296. GetMainSignals().UpdatedBlockTip(pindexNewTip, pindexFork, fInitialDownload);
  2297. // Always notify the UI if a new block tip was connected
  2298. if (pindexFork != pindexNewTip) {
  2299. uiInterface.NotifyBlockTip(fInitialDownload, pindexNewTip);
  2300. }
  2301. }
  2302. // When we reach this point, we switched to a new tip (stored in pindexNewTip).
  2303. if (nStopAtHeight && pindexNewTip && pindexNewTip->nHeight >= nStopAtHeight) StartShutdown();
  2304. // We check shutdown only after giving ActivateBestChainStep a chance to run once so that we
  2305. // never shutdown before connecting the genesis block during LoadChainTip(). Previously this
  2306. // caused an assert() failure during shutdown in such cases as the UTXO DB flushing checks
  2307. // that the best block hash is non-null.
  2308. if (ShutdownRequested())
  2309. break;
  2310. } while (pindexNewTip != pindexMostWork);
  2311. CheckBlockIndex(chainparams.GetConsensus());
  2312. // Write changes periodically to disk, after relay.
  2313. if (!FlushStateToDisk(chainparams, state, FLUSH_STATE_PERIODIC)) {
  2314. return false;
  2315. }
  2316. return true;
  2317. }
  2318. bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams, std::shared_ptr<const CBlock> pblock) {
  2319. return g_chainstate.ActivateBestChain(state, chainparams, std::move(pblock));
  2320. }
  2321. bool CChainState::PreciousBlock(CValidationState& state, const CChainParams& params, CBlockIndex *pindex)
  2322. {
  2323. {
  2324. LOCK(cs_main);
  2325. if (pindex->nChainWork < chainActive.Tip()->nChainWork) {
  2326. // Nothing to do, this block is not at the tip.
  2327. return true;
  2328. }
  2329. if (chainActive.Tip()->nChainWork > nLastPreciousChainwork) {
  2330. // The chain has been extended since the last call, reset the counter.
  2331. nBlockReverseSequenceId = -1;
  2332. }
  2333. nLastPreciousChainwork = chainActive.Tip()->nChainWork;
  2334. setBlockIndexCandidates.erase(pindex);
  2335. pindex->nSequenceId = nBlockReverseSequenceId;
  2336. if (nBlockReverseSequenceId > std::numeric_limits<int32_t>::min()) {
  2337. // We can't keep reducing the counter if somebody really wants to
  2338. // call preciousblock 2**31-1 times on the same set of tips...
  2339. nBlockReverseSequenceId--;
  2340. }
  2341. if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && pindex->nChainTx) {
  2342. setBlockIndexCandidates.insert(pindex);
  2343. PruneBlockIndexCandidates();
  2344. }
  2345. }
  2346. return ActivateBestChain(state, params, std::shared_ptr<const CBlock>());
  2347. }
  2348. bool PreciousBlock(CValidationState& state, const CChainParams& params, CBlockIndex *pindex) {
  2349. return g_chainstate.PreciousBlock(state, params, pindex);
  2350. }
  2351. bool CChainState::InvalidateBlock(CValidationState& state, const CChainParams& chainparams, CBlockIndex *pindex)
  2352. {
  2353. AssertLockHeld(cs_main);
  2354. // We first disconnect backwards and then mark the blocks as invalid.
  2355. // This prevents a case where pruned nodes may fail to invalidateblock
  2356. // and be left unable to start as they have no tip candidates (as there
  2357. // are no blocks that meet the "have data and are not invalid per
  2358. // nStatus" criteria for inclusion in setBlockIndexCandidates).
  2359. bool pindex_was_in_chain = false;
  2360. CBlockIndex *invalid_walk_tip = chainActive.Tip();
  2361. DisconnectedBlockTransactions disconnectpool;
  2362. while (chainActive.Contains(pindex)) {
  2363. pindex_was_in_chain = true;
  2364. // ActivateBestChain considers blocks already in chainActive
  2365. // unconditionally valid already, so force disconnect away from it.
  2366. if (!DisconnectTip(state, chainparams, &disconnectpool)) {
  2367. // It's probably hopeless to try to make the mempool consistent
  2368. // here if DisconnectTip failed, but we can try.
  2369. UpdateMempoolForReorg(disconnectpool, false);
  2370. return false;
  2371. }
  2372. }
  2373. // Now mark the blocks we just disconnected as descendants invalid
  2374. // (note this may not be all descendants).
  2375. while (pindex_was_in_chain && invalid_walk_tip != pindex) {
  2376. invalid_walk_tip->nStatus |= BLOCK_FAILED_CHILD;
  2377. setDirtyBlockIndex.insert(invalid_walk_tip);
  2378. setBlockIndexCandidates.erase(invalid_walk_tip);
  2379. invalid_walk_tip = invalid_walk_tip->pprev;
  2380. }
  2381. // Mark the block itself as invalid.
  2382. pindex->nStatus |= BLOCK_FAILED_VALID;
  2383. setDirtyBlockIndex.insert(pindex);
  2384. setBlockIndexCandidates.erase(pindex);
  2385. g_failed_blocks.insert(pindex);
  2386. // DisconnectTip will add transactions to disconnectpool; try to add these
  2387. // back to the mempool.
  2388. UpdateMempoolForReorg(disconnectpool, true);
  2389. // The resulting new best tip may not be in setBlockIndexCandidates anymore, so
  2390. // add it again.
  2391. BlockMap::iterator it = mapBlockIndex.begin();
  2392. while (it != mapBlockIndex.end()) {
  2393. if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->nChainTx && !setBlockIndexCandidates.value_comp()(it->second, chainActive.Tip())) {
  2394. setBlockIndexCandidates.insert(it->second);
  2395. }
  2396. it++;
  2397. }
  2398. InvalidChainFound(pindex);
  2399. uiInterface.NotifyBlockTip(IsInitialBlockDownload(), pindex->pprev);
  2400. return true;
  2401. }
  2402. bool InvalidateBlock(CValidationState& state, const CChainParams& chainparams, CBlockIndex *pindex) {
  2403. return g_chainstate.InvalidateBlock(state, chainparams, pindex);
  2404. }
  2405. bool CChainState::ResetBlockFailureFlags(CBlockIndex *pindex) {
  2406. AssertLockHeld(cs_main);
  2407. int nHeight = pindex->nHeight;
  2408. // Remove the invalidity flag from this block and all its descendants.
  2409. BlockMap::iterator it = mapBlockIndex.begin();
  2410. while (it != mapBlockIndex.end()) {
  2411. if (!it->second->IsValid() && it->second->GetAncestor(nHeight) == pindex) {
  2412. it->second->nStatus &= ~BLOCK_FAILED_MASK;
  2413. setDirtyBlockIndex.insert(it->second);
  2414. if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->nChainTx && setBlockIndexCandidates.value_comp()(chainActive.Tip(), it->second)) {
  2415. setBlockIndexCandidates.insert(it->second);
  2416. }
  2417. if (it->second == pindexBestInvalid) {
  2418. // Reset invalid block marker if it was pointing to one of those.
  2419. pindexBestInvalid = nullptr;
  2420. }
  2421. g_failed_blocks.erase(it->second);
  2422. }
  2423. it++;
  2424. }
  2425. // Remove the invalidity flag from all ancestors too.
  2426. while (pindex != nullptr) {
  2427. if (pindex->nStatus & BLOCK_FAILED_MASK) {
  2428. pindex->nStatus &= ~BLOCK_FAILED_MASK;
  2429. setDirtyBlockIndex.insert(pindex);
  2430. g_failed_blocks.erase(pindex);
  2431. }
  2432. pindex = pindex->pprev;
  2433. }
  2434. return true;
  2435. }
  2436. bool ResetBlockFailureFlags(CBlockIndex *pindex) {
  2437. return g_chainstate.ResetBlockFailureFlags(pindex);
  2438. }
  2439. CBlockIndex* CChainState::AddToBlockIndex(const CBlockHeader& block)
  2440. {
  2441. // Check for duplicate
  2442. uint256 hash = block.GetHash();
  2443. BlockMap::iterator it = mapBlockIndex.find(hash);
  2444. if (it != mapBlockIndex.end())
  2445. return it->second;
  2446. // Construct new block index object
  2447. CBlockIndex* pindexNew = new CBlockIndex(block);
  2448. // We assign the sequence id to blocks only when the full data is available,
  2449. // to avoid miners withholding blocks but broadcasting headers, to get a
  2450. // competitive advantage.
  2451. pindexNew->nSequenceId = 0;
  2452. BlockMap::iterator mi = mapBlockIndex.insert(std::make_pair(hash, pindexNew)).first;
  2453. pindexNew->phashBlock = &((*mi).first);
  2454. BlockMap::iterator miPrev = mapBlockIndex.find(block.hashPrevBlock);
  2455. if (miPrev != mapBlockIndex.end())
  2456. {
  2457. pindexNew->pprev = (*miPrev).second;
  2458. pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
  2459. pindexNew->BuildSkip();
  2460. }
  2461. pindexNew->nTimeMax = (pindexNew->pprev ? std::max(pindexNew->pprev->nTimeMax, pindexNew->nTime) : pindexNew->nTime);
  2462. pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + GetBlockProof(*pindexNew);
  2463. pindexNew->RaiseValidity(BLOCK_VALID_TREE);
  2464. if (pindexBestHeader == nullptr || pindexBestHeader->nChainWork < pindexNew->nChainWork)
  2465. pindexBestHeader = pindexNew;
  2466. setDirtyBlockIndex.insert(pindexNew);
  2467. return pindexNew;
  2468. }
  2469. /** Mark a block as having its data received and checked (up to BLOCK_VALID_TRANSACTIONS). */
  2470. bool CChainState::ReceivedBlockTransactions(const CBlock &block, CValidationState& state, CBlockIndex *pindexNew, const CDiskBlockPos& pos, const Consensus::Params& consensusParams)
  2471. {
  2472. pindexNew->nTx = block.vtx.size();
  2473. pindexNew->nChainTx = 0;
  2474. pindexNew->nFile = pos.nFile;
  2475. pindexNew->nDataPos = pos.nPos;
  2476. pindexNew->nUndoPos = 0;
  2477. pindexNew->nStatus |= BLOCK_HAVE_DATA;
  2478. if (IsWitnessEnabled(pindexNew->pprev, consensusParams)) {
  2479. pindexNew->nStatus |= BLOCK_OPT_WITNESS;
  2480. }
  2481. pindexNew->RaiseValidity(BLOCK_VALID_TRANSACTIONS);
  2482. setDirtyBlockIndex.insert(pindexNew);
  2483. if (pindexNew->pprev == nullptr || pindexNew->pprev->nChainTx) {
  2484. // If pindexNew is the genesis block or all parents are BLOCK_VALID_TRANSACTIONS.
  2485. std::deque<CBlockIndex*> queue;
  2486. queue.push_back(pindexNew);
  2487. // Recursively process any descendant blocks that now may be eligible to be connected.
  2488. while (!queue.empty()) {
  2489. CBlockIndex *pindex = queue.front();
  2490. queue.pop_front();
  2491. pindex->nChainTx = (pindex->pprev ? pindex->pprev->nChainTx : 0) + pindex->nTx;
  2492. {
  2493. LOCK(cs_nBlockSequenceId);
  2494. pindex->nSequenceId = nBlockSequenceId++;
  2495. }
  2496. if (chainActive.Tip() == nullptr || !setBlockIndexCandidates.value_comp()(pindex, chainActive.Tip())) {
  2497. setBlockIndexCandidates.insert(pindex);
  2498. }
  2499. std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> range = mapBlocksUnlinked.equal_range(pindex);
  2500. while (range.first != range.second) {
  2501. std::multimap<CBlockIndex*, CBlockIndex*>::iterator it = range.first;
  2502. queue.push_back(it->second);
  2503. range.first++;
  2504. mapBlocksUnlinked.erase(it);
  2505. }
  2506. }
  2507. } else {
  2508. if (pindexNew->pprev && pindexNew->pprev->IsValid(BLOCK_VALID_TREE)) {
  2509. mapBlocksUnlinked.insert(std::make_pair(pindexNew->pprev, pindexNew));
  2510. }
  2511. }
  2512. return true;
  2513. }
  2514. static bool FindBlockPos(CDiskBlockPos &pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime, bool fKnown = false)
  2515. {
  2516. LOCK(cs_LastBlockFile);
  2517. unsigned int nFile = fKnown ? pos.nFile : nLastBlockFile;
  2518. if (vinfoBlockFile.size() <= nFile) {
  2519. vinfoBlockFile.resize(nFile + 1);
  2520. }
  2521. if (!fKnown) {
  2522. while (vinfoBlockFile[nFile].nSize + nAddSize >= MAX_BLOCKFILE_SIZE) {
  2523. nFile++;
  2524. if (vinfoBlockFile.size() <= nFile) {
  2525. vinfoBlockFile.resize(nFile + 1);
  2526. }
  2527. }
  2528. pos.nFile = nFile;
  2529. pos.nPos = vinfoBlockFile[nFile].nSize;
  2530. }
  2531. if ((int)nFile != nLastBlockFile) {
  2532. if (!fKnown) {
  2533. LogPrintf("Leaving block file %i: %s\n", nLastBlockFile, vinfoBlockFile[nLastBlockFile].ToString());
  2534. }
  2535. FlushBlockFile(!fKnown);
  2536. nLastBlockFile = nFile;
  2537. }
  2538. vinfoBlockFile[nFile].AddBlock(nHeight, nTime);
  2539. if (fKnown)
  2540. vinfoBlockFile[nFile].nSize = std::max(pos.nPos + nAddSize, vinfoBlockFile[nFile].nSize);
  2541. else
  2542. vinfoBlockFile[nFile].nSize += nAddSize;
  2543. if (!fKnown) {
  2544. unsigned int nOldChunks = (pos.nPos + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
  2545. unsigned int nNewChunks = (vinfoBlockFile[nFile].nSize + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
  2546. if (nNewChunks > nOldChunks) {
  2547. if (fPruneMode)
  2548. fCheckForPruning = true;
  2549. if (CheckDiskSpace(nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos)) {
  2550. FILE *file = OpenBlockFile(pos);
  2551. if (file) {
  2552. LogPrintf("Pre-allocating up to position 0x%x in blk%05u.dat\n", nNewChunks * BLOCKFILE_CHUNK_SIZE, pos.nFile);
  2553. AllocateFileRange(file, pos.nPos, nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos);
  2554. fclose(file);
  2555. }
  2556. }
  2557. else
  2558. return error("out of disk space");
  2559. }
  2560. }
  2561. setDirtyFileInfo.insert(nFile);
  2562. return true;
  2563. }
  2564. static bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize)
  2565. {
  2566. pos.nFile = nFile;
  2567. LOCK(cs_LastBlockFile);
  2568. unsigned int nNewSize;
  2569. pos.nPos = vinfoBlockFile[nFile].nUndoSize;
  2570. nNewSize = vinfoBlockFile[nFile].nUndoSize += nAddSize;
  2571. setDirtyFileInfo.insert(nFile);
  2572. unsigned int nOldChunks = (pos.nPos + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
  2573. unsigned int nNewChunks = (nNewSize + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
  2574. if (nNewChunks > nOldChunks) {
  2575. if (fPruneMode)
  2576. fCheckForPruning = true;
  2577. if (CheckDiskSpace(nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos)) {
  2578. FILE *file = OpenUndoFile(pos);
  2579. if (file) {
  2580. LogPrintf("Pre-allocating up to position 0x%x in rev%05u.dat\n", nNewChunks * UNDOFILE_CHUNK_SIZE, pos.nFile);
  2581. AllocateFileRange(file, pos.nPos, nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos);
  2582. fclose(file);
  2583. }
  2584. }
  2585. else
  2586. return state.Error("out of disk space");
  2587. }
  2588. return true;
  2589. }
  2590. static bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW = true)
  2591. {
  2592. // Check proof of work matches claimed amount
  2593. if (fCheckPOW && !CheckProofOfWork(block.GetHash(), block.nBits, consensusParams))
  2594. return state.DoS(50, false, REJECT_INVALID, "high-hash", false, "proof of work failed");
  2595. return true;
  2596. }
  2597. bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW, bool fCheckMerkleRoot)
  2598. {
  2599. // These are checks that are independent of context.
  2600. if (block.fChecked)
  2601. return true;
  2602. // Check that the header is valid (particularly PoW). This is mostly
  2603. // redundant with the call in AcceptBlockHeader.
  2604. if (!CheckBlockHeader(block, state, consensusParams, fCheckPOW))
  2605. return false;
  2606. // Check the merkle root.
  2607. if (fCheckMerkleRoot) {
  2608. bool mutated;
  2609. uint256 hashMerkleRoot2 = BlockMerkleRoot(block, &mutated);
  2610. if (block.hashMerkleRoot != hashMerkleRoot2)
  2611. return state.DoS(100, false, REJECT_INVALID, "bad-txnmrklroot", true, "hashMerkleRoot mismatch");
  2612. // Check for merkle tree malleability (CVE-2012-2459): repeating sequences
  2613. // of transactions in a block without affecting the merkle root of a block,
  2614. // while still invalidating it.
  2615. if (mutated)
  2616. return state.DoS(100, false, REJECT_INVALID, "bad-txns-duplicate", true, "duplicate transaction");
  2617. }
  2618. // All potential-corruption validation must be done before we do any
  2619. // transaction validation, as otherwise we may mark the header as invalid
  2620. // because we receive the wrong transactions for it.
  2621. // Note that witness malleability is checked in ContextualCheckBlock, so no
  2622. // checks that use witness data may be performed here.
  2623. // Size limits
  2624. if (block.vtx.empty() || block.vtx.size() * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT || ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT)
  2625. return state.DoS(100, false, REJECT_INVALID, "bad-blk-length", false, "size limits failed");
  2626. // First transaction must be coinbase, the rest must not be
  2627. if (block.vtx.empty() || !block.vtx[0]->IsCoinBase())
  2628. return state.DoS(100, false, REJECT_INVALID, "bad-cb-missing", false, "first tx is not coinbase");
  2629. for (unsigned int i = 1; i < block.vtx.size(); i++)
  2630. if (block.vtx[i]->IsCoinBase())
  2631. return state.DoS(100, false, REJECT_INVALID, "bad-cb-multiple", false, "more than one coinbase");
  2632. // Check transactions
  2633. for (const auto& tx : block.vtx)
  2634. if (!CheckTransaction(*tx, state, false))
  2635. return state.Invalid(false, state.GetRejectCode(), state.GetRejectReason(),
  2636. strprintf("Transaction check failed (tx hash %s) %s", tx->GetHash().ToString(), state.GetDebugMessage()));
  2637. unsigned int nSigOps = 0;
  2638. for (const auto& tx : block.vtx)
  2639. {
  2640. nSigOps += GetLegacySigOpCount(*tx);
  2641. }
  2642. if (nSigOps * WITNESS_SCALE_FACTOR > MAX_BLOCK_SIGOPS_COST)
  2643. return state.DoS(100, false, REJECT_INVALID, "bad-blk-sigops", false, "out-of-bounds SigOpCount");
  2644. if (fCheckPOW && fCheckMerkleRoot)
  2645. block.fChecked = true;
  2646. return true;
  2647. }
  2648. bool IsWitnessEnabled(const CBlockIndex* pindexPrev, const Consensus::Params& params)
  2649. {
  2650. LOCK(cs_main);
  2651. return (VersionBitsState(pindexPrev, params, Consensus::DEPLOYMENT_SEGWIT, versionbitscache) == THRESHOLD_ACTIVE);
  2652. }
  2653. // Compute at which vout of the block's coinbase transaction the witness
  2654. // commitment occurs, or -1 if not found.
  2655. static int GetWitnessCommitmentIndex(const CBlock& block)
  2656. {
  2657. int commitpos = -1;
  2658. if (!block.vtx.empty()) {
  2659. for (size_t o = 0; o < block.vtx[0]->vout.size(); o++) {
  2660. if (block.vtx[0]->vout[o].scriptPubKey.size() >= 38 && block.vtx[0]->vout[o].scriptPubKey[0] == OP_RETURN && block.vtx[0]->vout[o].scriptPubKey[1] == 0x24 && block.vtx[0]->vout[o].scriptPubKey[2] == 0xaa && block.vtx[0]->vout[o].scriptPubKey[3] == 0x21 && block.vtx[0]->vout[o].scriptPubKey[4] == 0xa9 && block.vtx[0]->vout[o].scriptPubKey[5] == 0xed) {
  2661. commitpos = o;
  2662. }
  2663. }
  2664. }
  2665. return commitpos;
  2666. }
  2667. void UpdateUncommittedBlockStructures(CBlock& block, const CBlockIndex* pindexPrev, const Consensus::Params& consensusParams)
  2668. {
  2669. int commitpos = GetWitnessCommitmentIndex(block);
  2670. static const std::vector<unsigned char> nonce(32, 0x00);
  2671. if (commitpos != -1 && IsWitnessEnabled(pindexPrev, consensusParams) && !block.vtx[0]->HasWitness()) {
  2672. CMutableTransaction tx(*block.vtx[0]);
  2673. tx.vin[0].scriptWitness.stack.resize(1);
  2674. tx.vin[0].scriptWitness.stack[0] = nonce;
  2675. block.vtx[0] = MakeTransactionRef(std::move(tx));
  2676. }
  2677. }
  2678. std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBlockIndex* pindexPrev, const Consensus::Params& consensusParams)
  2679. {
  2680. std::vector<unsigned char> commitment;
  2681. int commitpos = GetWitnessCommitmentIndex(block);
  2682. std::vector<unsigned char> ret(32, 0x00);
  2683. if (consensusParams.vDeployments[Consensus::DEPLOYMENT_SEGWIT].nTimeout != 0) {
  2684. if (commitpos == -1) {
  2685. uint256 witnessroot = BlockWitnessMerkleRoot(block, nullptr);
  2686. CHash256().Write(witnessroot.begin(), 32).Write(ret.data(), 32).Finalize(witnessroot.begin());
  2687. CTxOut out;
  2688. out.nValue = 0;
  2689. out.scriptPubKey.resize(38);
  2690. out.scriptPubKey[0] = OP_RETURN;
  2691. out.scriptPubKey[1] = 0x24;
  2692. out.scriptPubKey[2] = 0xaa;
  2693. out.scriptPubKey[3] = 0x21;
  2694. out.scriptPubKey[4] = 0xa9;
  2695. out.scriptPubKey[5] = 0xed;
  2696. memcpy(&out.scriptPubKey[6], witnessroot.begin(), 32);
  2697. commitment = std::vector<unsigned char>(out.scriptPubKey.begin(), out.scriptPubKey.end());
  2698. CMutableTransaction tx(*block.vtx[0]);
  2699. tx.vout.push_back(out);
  2700. block.vtx[0] = MakeTransactionRef(std::move(tx));
  2701. }
  2702. }
  2703. UpdateUncommittedBlockStructures(block, pindexPrev, consensusParams);
  2704. return commitment;
  2705. }
  2706. /** Context-dependent validity checks.
  2707. * By "context", we mean only the previous block headers, but not the UTXO
  2708. * set; UTXO-related validity checks are done in ConnectBlock().
  2709. * NOTE: This function is not currently invoked by ConnectBlock(), so we
  2710. * should consider upgrade issues if we change which consensus rules are
  2711. * enforced in this function (eg by adding a new consensus rule). See comment
  2712. * in ConnectBlock().
  2713. * Note that -reindex-chainstate skips the validation that happens here!
  2714. */
  2715. static bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, const CChainParams& params, const CBlockIndex* pindexPrev, int64_t nAdjustedTime)
  2716. {
  2717. assert(pindexPrev != nullptr);
  2718. const int nHeight = pindexPrev->nHeight + 1;
  2719. // Check proof of work
  2720. const Consensus::Params& consensusParams = params.GetConsensus();
  2721. if (block.nBits != GetNextWorkRequired(pindexPrev, &block, consensusParams))
  2722. return state.DoS(100, false, REJECT_INVALID, "bad-diffbits", false, "incorrect proof of work");
  2723. // Check against checkpoints
  2724. if (fCheckpointsEnabled) {
  2725. // Don't accept any forks from the main chain prior to last checkpoint.
  2726. // GetLastCheckpoint finds the last checkpoint in MapCheckpoints that's in our
  2727. // MapBlockIndex.
  2728. CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(params.Checkpoints());
  2729. if (pcheckpoint && nHeight < pcheckpoint->nHeight)
  2730. return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d)", __func__, nHeight), REJECT_CHECKPOINT, "bad-fork-prior-to-checkpoint");
  2731. }
  2732. // Check timestamp against prev
  2733. if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast())
  2734. return state.Invalid(false, REJECT_INVALID, "time-too-old", "block's timestamp is too early");
  2735. // Check timestamp
  2736. if (block.GetBlockTime() > nAdjustedTime + MAX_FUTURE_BLOCK_TIME)
  2737. return state.Invalid(false, REJECT_INVALID, "time-too-new", "block timestamp too far in the future");
  2738. // Reject outdated version blocks when 95% (75% on testnet) of the network has upgraded:
  2739. // check for version 2, 3 and 4 upgrades
  2740. if((block.nVersion < 2 && nHeight >= consensusParams.BIP34Height) ||
  2741. (block.nVersion < 3 && nHeight >= consensusParams.BIP66Height) ||
  2742. (block.nVersion < 4 && nHeight >= consensusParams.BIP65Height))
  2743. return state.Invalid(false, REJECT_OBSOLETE, strprintf("bad-version(0x%08x)", block.nVersion),
  2744. strprintf("rejected nVersion=0x%08x block", block.nVersion));
  2745. return true;
  2746. }
  2747. /** NOTE: This function is not currently invoked by ConnectBlock(), so we
  2748. * should consider upgrade issues if we change which consensus rules are
  2749. * enforced in this function (eg by adding a new consensus rule). See comment
  2750. * in ConnectBlock().
  2751. * Note that -reindex-chainstate skips the validation that happens here!
  2752. */
  2753. static bool ContextualCheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
  2754. {
  2755. const int nHeight = pindexPrev == nullptr ? 0 : pindexPrev->nHeight + 1;
  2756. // Start enforcing BIP113 (Median Time Past) using versionbits logic.
  2757. int nLockTimeFlags = 0;
  2758. if (VersionBitsState(pindexPrev, consensusParams, Consensus::DEPLOYMENT_CSV, versionbitscache) == THRESHOLD_ACTIVE) {
  2759. nLockTimeFlags |= LOCKTIME_MEDIAN_TIME_PAST;
  2760. }
  2761. int64_t nLockTimeCutoff = (nLockTimeFlags & LOCKTIME_MEDIAN_TIME_PAST)
  2762. ? pindexPrev->GetMedianTimePast()
  2763. : block.GetBlockTime();
  2764. // Check that all transactions are finalized
  2765. for (const auto& tx : block.vtx) {
  2766. if (!IsFinalTx(*tx, nHeight, nLockTimeCutoff)) {
  2767. return state.DoS(10, false, REJECT_INVALID, "bad-txns-nonfinal", false, "non-final transaction");
  2768. }
  2769. }
  2770. // Enforce rule that the coinbase starts with serialized block height
  2771. if (nHeight >= consensusParams.BIP34Height)
  2772. {
  2773. CScript expect = CScript() << nHeight;
  2774. if (block.vtx[0]->vin[0].scriptSig.size() < expect.size() ||
  2775. !std::equal(expect.begin(), expect.end(), block.vtx[0]->vin[0].scriptSig.begin())) {
  2776. return state.DoS(100, false, REJECT_INVALID, "bad-cb-height", false, "block height mismatch in coinbase");
  2777. }
  2778. }
  2779. // Validation for witness commitments.
  2780. // * We compute the witness hash (which is the hash including witnesses) of all the block's transactions, except the
  2781. // coinbase (where 0x0000....0000 is used instead).
  2782. // * The coinbase scriptWitness is a stack of a single 32-byte vector, containing a witness nonce (unconstrained).
  2783. // * We build a merkle tree with all those witness hashes as leaves (similar to the hashMerkleRoot in the block header).
  2784. // * There must be at least one output whose scriptPubKey is a single 36-byte push, the first 4 bytes of which are
  2785. // {0xaa, 0x21, 0xa9, 0xed}, and the following 32 bytes are SHA256^2(witness root, witness nonce). In case there are
  2786. // multiple, the last one is used.
  2787. bool fHaveWitness = false;
  2788. if (VersionBitsState(pindexPrev, consensusParams, Consensus::DEPLOYMENT_SEGWIT, versionbitscache) == THRESHOLD_ACTIVE) {
  2789. int commitpos = GetWitnessCommitmentIndex(block);
  2790. if (commitpos != -1) {
  2791. bool malleated = false;
  2792. uint256 hashWitness = BlockWitnessMerkleRoot(block, &malleated);
  2793. // The malleation check is ignored; as the transaction tree itself
  2794. // already does not permit it, it is impossible to trigger in the
  2795. // witness tree.
  2796. if (block.vtx[0]->vin[0].scriptWitness.stack.size() != 1 || block.vtx[0]->vin[0].scriptWitness.stack[0].size() != 32) {
  2797. return state.DoS(100, false, REJECT_INVALID, "bad-witness-nonce-size", true, strprintf("%s : invalid witness nonce size", __func__));
  2798. }
  2799. CHash256().Write(hashWitness.begin(), 32).Write(&block.vtx[0]->vin[0].scriptWitness.stack[0][0], 32).Finalize(hashWitness.begin());
  2800. if (memcmp(hashWitness.begin(), &block.vtx[0]->vout[commitpos].scriptPubKey[6], 32)) {
  2801. return state.DoS(100, false, REJECT_INVALID, "bad-witness-merkle-match", true, strprintf("%s : witness merkle commitment mismatch", __func__));
  2802. }
  2803. fHaveWitness = true;
  2804. }
  2805. }
  2806. // No witness data is allowed in blocks that don't commit to witness data, as this would otherwise leave room for spam
  2807. if (!fHaveWitness) {
  2808. for (const auto& tx : block.vtx) {
  2809. if (tx->HasWitness()) {
  2810. return state.DoS(100, false, REJECT_INVALID, "unexpected-witness", true, strprintf("%s : unexpected witness data found", __func__));
  2811. }
  2812. }
  2813. }
  2814. // After the coinbase witness nonce and commitment are verified,
  2815. // we can check if the block weight passes (before we've checked the
  2816. // coinbase witness, it would be possible for the weight to be too
  2817. // large by filling up the coinbase witness, which doesn't change
  2818. // the block hash, so we couldn't mark the block as permanently
  2819. // failed).
  2820. if (GetBlockWeight(block) > MAX_BLOCK_WEIGHT) {
  2821. return state.DoS(100, false, REJECT_INVALID, "bad-blk-weight", false, strprintf("%s : weight limit failed", __func__));
  2822. }
  2823. return true;
  2824. }
  2825. bool CChainState::AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex)
  2826. {
  2827. AssertLockHeld(cs_main);
  2828. // Check for duplicate
  2829. uint256 hash = block.GetHash();
  2830. BlockMap::iterator miSelf = mapBlockIndex.find(hash);
  2831. CBlockIndex *pindex = nullptr;
  2832. if (hash != chainparams.GetConsensus().hashGenesisBlock) {
  2833. if (miSelf != mapBlockIndex.end()) {
  2834. // Block header is already known.
  2835. pindex = miSelf->second;
  2836. if (ppindex)
  2837. *ppindex = pindex;
  2838. if (pindex->nStatus & BLOCK_FAILED_MASK)
  2839. return state.Invalid(error("%s: block %s is marked invalid", __func__, hash.ToString()), 0, "duplicate");
  2840. return true;
  2841. }
  2842. if (!CheckBlockHeader(block, state, chainparams.GetConsensus()))
  2843. return error("%s: Consensus::CheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));
  2844. // Get prev block index
  2845. CBlockIndex* pindexPrev = nullptr;
  2846. BlockMap::iterator mi = mapBlockIndex.find(block.hashPrevBlock);
  2847. if (mi == mapBlockIndex.end())
  2848. return state.DoS(10, error("%s: prev block not found", __func__), 0, "prev-blk-not-found");
  2849. pindexPrev = (*mi).second;
  2850. if (pindexPrev->nStatus & BLOCK_FAILED_MASK)
  2851. return state.DoS(100, error("%s: prev block invalid", __func__), REJECT_INVALID, "bad-prevblk");
  2852. if (!ContextualCheckBlockHeader(block, state, chainparams, pindexPrev, GetAdjustedTime()))
  2853. return error("%s: Consensus::ContextualCheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));
  2854. if (!pindexPrev->IsValid(BLOCK_VALID_SCRIPTS)) {
  2855. for (const CBlockIndex* failedit : g_failed_blocks) {
  2856. if (pindexPrev->GetAncestor(failedit->nHeight) == failedit) {
  2857. assert(failedit->nStatus & BLOCK_FAILED_VALID);
  2858. CBlockIndex* invalid_walk = pindexPrev;
  2859. while (invalid_walk != failedit) {
  2860. invalid_walk->nStatus |= BLOCK_FAILED_CHILD;
  2861. setDirtyBlockIndex.insert(invalid_walk);
  2862. invalid_walk = invalid_walk->pprev;
  2863. }
  2864. return state.DoS(100, error("%s: prev block invalid", __func__), REJECT_INVALID, "bad-prevblk");
  2865. }
  2866. }
  2867. }
  2868. }
  2869. if (pindex == nullptr)
  2870. pindex = AddToBlockIndex(block);
  2871. if (ppindex)
  2872. *ppindex = pindex;
  2873. CheckBlockIndex(chainparams.GetConsensus());
  2874. return true;
  2875. }
  2876. // Exposed wrapper for AcceptBlockHeader
  2877. bool ProcessNewBlockHeaders(const std::vector<CBlockHeader>& headers, CValidationState& state, const CChainParams& chainparams, const CBlockIndex** ppindex, CBlockHeader *first_invalid)
  2878. {
  2879. if (first_invalid != nullptr) first_invalid->SetNull();
  2880. {
  2881. LOCK(cs_main);
  2882. for (const CBlockHeader& header : headers) {
  2883. CBlockIndex *pindex = nullptr; // Use a temp pindex instead of ppindex to avoid a const_cast
  2884. if (!g_chainstate.AcceptBlockHeader(header, state, chainparams, &pindex)) {
  2885. if (first_invalid) *first_invalid = header;
  2886. return false;
  2887. }
  2888. if (ppindex) {
  2889. *ppindex = pindex;
  2890. }
  2891. }
  2892. }
  2893. NotifyHeaderTip();
  2894. return true;
  2895. }
  2896. /** Store block on disk. If dbp is non-nullptr, the file is known to already reside on disk */
  2897. static CDiskBlockPos SaveBlockToDisk(const CBlock& block, int nHeight, const CChainParams& chainparams, const CDiskBlockPos* dbp) {
  2898. unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
  2899. CDiskBlockPos blockPos;
  2900. if (dbp != nullptr)
  2901. blockPos = *dbp;
  2902. if (!FindBlockPos(blockPos, nBlockSize+8, nHeight, block.GetBlockTime(), dbp != nullptr)) {
  2903. error("%s: FindBlockPos failed", __func__);
  2904. return CDiskBlockPos();
  2905. }
  2906. if (dbp == nullptr) {
  2907. if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart())) {
  2908. AbortNode("Failed to write block");
  2909. return CDiskBlockPos();
  2910. }
  2911. }
  2912. return blockPos;
  2913. }
  2914. /** Store block on disk. If dbp is non-nullptr, the file is known to already reside on disk */
  2915. bool CChainState::AcceptBlock(const std::shared_ptr<const CBlock>& pblock, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex, bool fRequested, const CDiskBlockPos* dbp, bool* fNewBlock)
  2916. {
  2917. const CBlock& block = *pblock;
  2918. if (fNewBlock) *fNewBlock = false;
  2919. AssertLockHeld(cs_main);
  2920. CBlockIndex *pindexDummy = nullptr;
  2921. CBlockIndex *&pindex = ppindex ? *ppindex : pindexDummy;
  2922. if (!AcceptBlockHeader(block, state, chainparams, &pindex))
  2923. return false;
  2924. // Try to process all requested blocks that we don't have, but only
  2925. // process an unrequested block if it's new and has enough work to
  2926. // advance our tip, and isn't too many blocks ahead.
  2927. bool fAlreadyHave = pindex->nStatus & BLOCK_HAVE_DATA;
  2928. bool fHasMoreOrSameWork = (chainActive.Tip() ? pindex->nChainWork >= chainActive.Tip()->nChainWork : true);
  2929. // Blocks that are too out-of-order needlessly limit the effectiveness of
  2930. // pruning, because pruning will not delete block files that contain any
  2931. // blocks which are too close in height to the tip. Apply this test
  2932. // regardless of whether pruning is enabled; it should generally be safe to
  2933. // not process unrequested blocks.
  2934. bool fTooFarAhead = (pindex->nHeight > int(chainActive.Height() + MIN_BLOCKS_TO_KEEP));
  2935. // TODO: Decouple this function from the block download logic by removing fRequested
  2936. // This requires some new chain data structure to efficiently look up if a
  2937. // block is in a chain leading to a candidate for best tip, despite not
  2938. // being such a candidate itself.
  2939. // TODO: deal better with return value and error conditions for duplicate
  2940. // and unrequested blocks.
  2941. if (fAlreadyHave) return true;
  2942. if (!fRequested) { // If we didn't ask for it:
  2943. if (pindex->nTx != 0) return true; // This is a previously-processed block that was pruned
  2944. if (!fHasMoreOrSameWork) return true; // Don't process less-work chains
  2945. if (fTooFarAhead) return true; // Block height is too high
  2946. // Protect against DoS attacks from low-work chains.
  2947. // If our tip is behind, a peer could try to send us
  2948. // low-work blocks on a fake chain that we would never
  2949. // request; don't process these.
  2950. if (pindex->nChainWork < nMinimumChainWork) return true;
  2951. }
  2952. if (fNewBlock) *fNewBlock = true;
  2953. if (!CheckBlock(block, state, chainparams.GetConsensus()) ||
  2954. !ContextualCheckBlock(block, state, chainparams.GetConsensus(), pindex->pprev)) {
  2955. if (state.IsInvalid() && !state.CorruptionPossible()) {
  2956. pindex->nStatus |= BLOCK_FAILED_VALID;
  2957. setDirtyBlockIndex.insert(pindex);
  2958. }
  2959. return error("%s: %s", __func__, FormatStateMessage(state));
  2960. }
  2961. // Header is valid/has work, merkle tree and segwit merkle tree are good...RELAY NOW
  2962. // (but if it does not build on our best tip, let the SendMessages loop relay it)
  2963. if (!IsInitialBlockDownload() && chainActive.Tip() == pindex->pprev)
  2964. GetMainSignals().NewPoWValidBlock(pindex, pblock);
  2965. // Write block to history file
  2966. try {
  2967. CDiskBlockPos blockPos = SaveBlockToDisk(block, pindex->nHeight, chainparams, dbp);
  2968. if (blockPos.IsNull()) {
  2969. state.Error(strprintf("%s: Failed to find position to write new block to disk", __func__));
  2970. return false;
  2971. }
  2972. if (!ReceivedBlockTransactions(block, state, pindex, blockPos, chainparams.GetConsensus()))
  2973. return error("AcceptBlock(): ReceivedBlockTransactions failed");
  2974. } catch (const std::runtime_error& e) {
  2975. return AbortNode(state, std::string("System error: ") + e.what());
  2976. }
  2977. if (fCheckForPruning)
  2978. FlushStateToDisk(chainparams, state, FLUSH_STATE_NONE); // we just allocated more disk space for block files
  2979. CheckBlockIndex(chainparams.GetConsensus());
  2980. return true;
  2981. }
  2982. bool ProcessNewBlock(const CChainParams& chainparams, const std::shared_ptr<const CBlock> pblock, bool fForceProcessing, bool *fNewBlock)
  2983. {
  2984. AssertLockNotHeld(cs_main);
  2985. {
  2986. CBlockIndex *pindex = nullptr;
  2987. if (fNewBlock) *fNewBlock = false;
  2988. CValidationState state;
  2989. // Ensure that CheckBlock() passes before calling AcceptBlock, as
  2990. // belt-and-suspenders.
  2991. bool ret = CheckBlock(*pblock, state, chainparams.GetConsensus());
  2992. LOCK(cs_main);
  2993. if (ret) {
  2994. // Store to disk
  2995. ret = g_chainstate.AcceptBlock(pblock, state, chainparams, &pindex, fForceProcessing, nullptr, fNewBlock);
  2996. }
  2997. if (!ret) {
  2998. GetMainSignals().BlockChecked(*pblock, state);
  2999. return error("%s: AcceptBlock FAILED (%s)", __func__, state.GetDebugMessage());
  3000. }
  3001. }
  3002. NotifyHeaderTip();
  3003. CValidationState state; // Only used to report errors, not invalidity - ignore it
  3004. if (!g_chainstate.ActivateBestChain(state, chainparams, pblock))
  3005. return error("%s: ActivateBestChain failed", __func__);
  3006. return true;
  3007. }
  3008. bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams, const CBlock& block, CBlockIndex* pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot)
  3009. {
  3010. AssertLockHeld(cs_main);
  3011. assert(pindexPrev && pindexPrev == chainActive.Tip());
  3012. CCoinsViewCache viewNew(pcoinsTip.get());
  3013. CBlockIndex indexDummy(block);
  3014. indexDummy.pprev = pindexPrev;
  3015. indexDummy.nHeight = pindexPrev->nHeight + 1;
  3016. // NOTE: CheckBlockHeader is called by CheckBlock
  3017. if (!ContextualCheckBlockHeader(block, state, chainparams, pindexPrev, GetAdjustedTime()))
  3018. return error("%s: Consensus::ContextualCheckBlockHeader: %s", __func__, FormatStateMessage(state));
  3019. if (!CheckBlock(block, state, chainparams.GetConsensus(), fCheckPOW, fCheckMerkleRoot))
  3020. return error("%s: Consensus::CheckBlock: %s", __func__, FormatStateMessage(state));
  3021. if (!ContextualCheckBlock(block, state, chainparams.GetConsensus(), pindexPrev))
  3022. return error("%s: Consensus::ContextualCheckBlock: %s", __func__, FormatStateMessage(state));
  3023. if (!g_chainstate.ConnectBlock(block, state, &indexDummy, viewNew, chainparams, true))
  3024. return false;
  3025. assert(state.IsValid());
  3026. return true;
  3027. }
  3028. /**
  3029. * BLOCK PRUNING CODE
  3030. */
  3031. /* Calculate the amount of disk space the block & undo files currently use */
  3032. uint64_t CalculateCurrentUsage()
  3033. {
  3034. LOCK(cs_LastBlockFile);
  3035. uint64_t retval = 0;
  3036. for (const CBlockFileInfo &file : vinfoBlockFile) {
  3037. retval += file.nSize + file.nUndoSize;
  3038. }
  3039. return retval;
  3040. }
  3041. /* Prune a block file (modify associated database entries)*/
  3042. void PruneOneBlockFile(const int fileNumber)
  3043. {
  3044. LOCK(cs_LastBlockFile);
  3045. for (const auto& entry : mapBlockIndex) {
  3046. CBlockIndex* pindex = entry.second;
  3047. if (pindex->nFile == fileNumber) {
  3048. pindex->nStatus &= ~BLOCK_HAVE_DATA;
  3049. pindex->nStatus &= ~BLOCK_HAVE_UNDO;
  3050. pindex->nFile = 0;
  3051. pindex->nDataPos = 0;
  3052. pindex->nUndoPos = 0;
  3053. setDirtyBlockIndex.insert(pindex);
  3054. // Prune from mapBlocksUnlinked -- any block we prune would have
  3055. // to be downloaded again in order to consider its chain, at which
  3056. // point it would be considered as a candidate for
  3057. // mapBlocksUnlinked or setBlockIndexCandidates.
  3058. std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> range = mapBlocksUnlinked.equal_range(pindex->pprev);
  3059. while (range.first != range.second) {
  3060. std::multimap<CBlockIndex *, CBlockIndex *>::iterator _it = range.first;
  3061. range.first++;
  3062. if (_it->second == pindex) {
  3063. mapBlocksUnlinked.erase(_it);
  3064. }
  3065. }
  3066. }
  3067. }
  3068. vinfoBlockFile[fileNumber].SetNull();
  3069. setDirtyFileInfo.insert(fileNumber);
  3070. }
  3071. void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune)
  3072. {
  3073. for (std::set<int>::iterator it = setFilesToPrune.begin(); it != setFilesToPrune.end(); ++it) {
  3074. CDiskBlockPos pos(*it, 0);
  3075. fs::remove(GetBlockPosFilename(pos, "blk"));
  3076. fs::remove(GetBlockPosFilename(pos, "rev"));
  3077. LogPrintf("Prune: %s deleted blk/rev (%05u)\n", __func__, *it);
  3078. }
  3079. }
  3080. /* Calculate the block/rev files to delete based on height specified by user with RPC command pruneblockchain */
  3081. static void FindFilesToPruneManual(std::set<int>& setFilesToPrune, int nManualPruneHeight)
  3082. {
  3083. assert(fPruneMode && nManualPruneHeight > 0);
  3084. LOCK2(cs_main, cs_LastBlockFile);
  3085. if (chainActive.Tip() == nullptr)
  3086. return;
  3087. // last block to prune is the lesser of (user-specified height, MIN_BLOCKS_TO_KEEP from the tip)
  3088. unsigned int nLastBlockWeCanPrune = std::min((unsigned)nManualPruneHeight, chainActive.Tip()->nHeight - MIN_BLOCKS_TO_KEEP);
  3089. int count=0;
  3090. for (int fileNumber = 0; fileNumber < nLastBlockFile; fileNumber++) {
  3091. if (vinfoBlockFile[fileNumber].nSize == 0 || vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune)
  3092. continue;
  3093. PruneOneBlockFile(fileNumber);
  3094. setFilesToPrune.insert(fileNumber);
  3095. count++;
  3096. }
  3097. LogPrintf("Prune (Manual): prune_height=%d removed %d blk/rev pairs\n", nLastBlockWeCanPrune, count);
  3098. }
  3099. /* This function is called from the RPC code for pruneblockchain */
  3100. void PruneBlockFilesManual(int nManualPruneHeight)
  3101. {
  3102. CValidationState state;
  3103. const CChainParams& chainparams = Params();
  3104. FlushStateToDisk(chainparams, state, FLUSH_STATE_NONE, nManualPruneHeight);
  3105. }
  3106. /**
  3107. * Prune block and undo files (blk???.dat and undo???.dat) so that the disk space used is less than a user-defined target.
  3108. * The user sets the target (in MB) on the command line or in config file. This will be run on startup and whenever new
  3109. * space is allocated in a block or undo file, staying below the target. Changing back to unpruned requires a reindex
  3110. * (which in this case means the blockchain must be re-downloaded.)
  3111. *
  3112. * Pruning functions are called from FlushStateToDisk when the global fCheckForPruning flag has been set.
  3113. * Block and undo files are deleted in lock-step (when blk00003.dat is deleted, so is rev00003.dat.)
  3114. * Pruning cannot take place until the longest chain is at least a certain length (100000 on mainnet, 1000 on testnet, 1000 on regtest).
  3115. * Pruning will never delete a block within a defined distance (currently 288) from the active chain's tip.
  3116. * The block index is updated by unsetting HAVE_DATA and HAVE_UNDO for any blocks that were stored in the deleted files.
  3117. * A db flag records the fact that at least some block files have been pruned.
  3118. *
  3119. * @param[out] setFilesToPrune The set of file indices that can be unlinked will be returned
  3120. */
  3121. static void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight)
  3122. {
  3123. LOCK2(cs_main, cs_LastBlockFile);
  3124. if (chainActive.Tip() == nullptr || nPruneTarget == 0) {
  3125. return;
  3126. }
  3127. if ((uint64_t)chainActive.Tip()->nHeight <= nPruneAfterHeight) {
  3128. return;
  3129. }
  3130. unsigned int nLastBlockWeCanPrune = chainActive.Tip()->nHeight - MIN_BLOCKS_TO_KEEP;
  3131. uint64_t nCurrentUsage = CalculateCurrentUsage();
  3132. // We don't check to prune until after we've allocated new space for files
  3133. // So we should leave a buffer under our target to account for another allocation
  3134. // before the next pruning.
  3135. uint64_t nBuffer = BLOCKFILE_CHUNK_SIZE + UNDOFILE_CHUNK_SIZE;
  3136. uint64_t nBytesToPrune;
  3137. int count=0;
  3138. if (nCurrentUsage + nBuffer >= nPruneTarget) {
  3139. for (int fileNumber = 0; fileNumber < nLastBlockFile; fileNumber++) {
  3140. nBytesToPrune = vinfoBlockFile[fileNumber].nSize + vinfoBlockFile[fileNumber].nUndoSize;
  3141. if (vinfoBlockFile[fileNumber].nSize == 0)
  3142. continue;
  3143. if (nCurrentUsage + nBuffer < nPruneTarget) // are we below our target?
  3144. break;
  3145. // don't prune files that could have a block within MIN_BLOCKS_TO_KEEP of the main chain's tip but keep scanning
  3146. if (vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune)
  3147. continue;
  3148. PruneOneBlockFile(fileNumber);
  3149. // Queue up the files for removal
  3150. setFilesToPrune.insert(fileNumber);
  3151. nCurrentUsage -= nBytesToPrune;
  3152. count++;
  3153. }
  3154. }
  3155. LogPrint(BCLog::PRUNE, "Prune: target=%dMiB actual=%dMiB diff=%dMiB max_prune_height=%d removed %d blk/rev pairs\n",
  3156. nPruneTarget/1024/1024, nCurrentUsage/1024/1024,
  3157. ((int64_t)nPruneTarget - (int64_t)nCurrentUsage)/1024/1024,
  3158. nLastBlockWeCanPrune, count);
  3159. }
  3160. bool CheckDiskSpace(uint64_t nAdditionalBytes)
  3161. {
  3162. uint64_t nFreeBytesAvailable = fs::space(GetDataDir()).available;
  3163. // Check for nMinDiskSpace bytes (currently 50MB)
  3164. if (nFreeBytesAvailable < nMinDiskSpace + nAdditionalBytes)
  3165. return AbortNode("Disk space is low!", _("Error: Disk space is low!"));
  3166. return true;
  3167. }
  3168. static FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly)
  3169. {
  3170. if (pos.IsNull())
  3171. return nullptr;
  3172. fs::path path = GetBlockPosFilename(pos, prefix);
  3173. fs::create_directories(path.parent_path());
  3174. FILE* file = fsbridge::fopen(path, fReadOnly ? "rb": "rb+");
  3175. if (!file && !fReadOnly)
  3176. file = fsbridge::fopen(path, "wb+");
  3177. if (!file) {
  3178. LogPrintf("Unable to open file %s\n", path.string());
  3179. return nullptr;
  3180. }
  3181. if (pos.nPos) {
  3182. if (fseek(file, pos.nPos, SEEK_SET)) {
  3183. LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, path.string());
  3184. fclose(file);
  3185. return nullptr;
  3186. }
  3187. }
  3188. return file;
  3189. }
  3190. FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly) {
  3191. return OpenDiskFile(pos, "blk", fReadOnly);
  3192. }
  3193. /** Open an undo file (rev?????.dat) */
  3194. static FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly) {
  3195. return OpenDiskFile(pos, "rev", fReadOnly);
  3196. }
  3197. fs::path GetBlockPosFilename(const CDiskBlockPos &pos, const char *prefix)
  3198. {
  3199. return GetDataDir() / "blocks" / strprintf("%s%05u.dat", prefix, pos.nFile);
  3200. }
  3201. CBlockIndex * CChainState::InsertBlockIndex(const uint256& hash)
  3202. {
  3203. if (hash.IsNull())
  3204. return nullptr;
  3205. // Return existing
  3206. BlockMap::iterator mi = mapBlockIndex.find(hash);
  3207. if (mi != mapBlockIndex.end())
  3208. return (*mi).second;
  3209. // Create new
  3210. CBlockIndex* pindexNew = new CBlockIndex();
  3211. mi = mapBlockIndex.insert(std::make_pair(hash, pindexNew)).first;
  3212. pindexNew->phashBlock = &((*mi).first);
  3213. return pindexNew;
  3214. }
  3215. bool CChainState::LoadBlockIndex(const Consensus::Params& consensus_params, CBlockTreeDB& blocktree)
  3216. {
  3217. if (!blocktree.LoadBlockIndexGuts(consensus_params, [this](const uint256& hash){ return this->InsertBlockIndex(hash); }))
  3218. return false;
  3219. boost::this_thread::interruption_point();
  3220. // Calculate nChainWork
  3221. std::vector<std::pair<int, CBlockIndex*> > vSortedByHeight;
  3222. vSortedByHeight.reserve(mapBlockIndex.size());
  3223. for (const std::pair<uint256, CBlockIndex*>& item : mapBlockIndex)
  3224. {
  3225. CBlockIndex* pindex = item.second;
  3226. vSortedByHeight.push_back(std::make_pair(pindex->nHeight, pindex));
  3227. }
  3228. sort(vSortedByHeight.begin(), vSortedByHeight.end());
  3229. for (const std::pair<int, CBlockIndex*>& item : vSortedByHeight)
  3230. {
  3231. CBlockIndex* pindex = item.second;
  3232. pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + GetBlockProof(*pindex);
  3233. pindex->nTimeMax = (pindex->pprev ? std::max(pindex->pprev->nTimeMax, pindex->nTime) : pindex->nTime);
  3234. // We can link the chain of blocks for which we've received transactions at some point.
  3235. // Pruned nodes may have deleted the block.
  3236. if (pindex->nTx > 0) {
  3237. if (pindex->pprev) {
  3238. if (pindex->pprev->nChainTx) {
  3239. pindex->nChainTx = pindex->pprev->nChainTx + pindex->nTx;
  3240. } else {
  3241. pindex->nChainTx = 0;
  3242. mapBlocksUnlinked.insert(std::make_pair(pindex->pprev, pindex));
  3243. }
  3244. } else {
  3245. pindex->nChainTx = pindex->nTx;
  3246. }
  3247. }
  3248. if (!(pindex->nStatus & BLOCK_FAILED_MASK) && pindex->pprev && (pindex->pprev->nStatus & BLOCK_FAILED_MASK)) {
  3249. pindex->nStatus |= BLOCK_FAILED_CHILD;
  3250. setDirtyBlockIndex.insert(pindex);
  3251. }
  3252. if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && (pindex->nChainTx || pindex->pprev == nullptr))
  3253. setBlockIndexCandidates.insert(pindex);
  3254. if (pindex->nStatus & BLOCK_FAILED_MASK && (!pindexBestInvalid || pindex->nChainWork > pindexBestInvalid->nChainWork))
  3255. pindexBestInvalid = pindex;
  3256. if (pindex->pprev)
  3257. pindex->BuildSkip();
  3258. if (pindex->IsValid(BLOCK_VALID_TREE) && (pindexBestHeader == nullptr || CBlockIndexWorkComparator()(pindexBestHeader, pindex)))
  3259. pindexBestHeader = pindex;
  3260. }
  3261. return true;
  3262. }
  3263. bool static LoadBlockIndexDB(const CChainParams& chainparams)
  3264. {
  3265. if (!g_chainstate.LoadBlockIndex(chainparams.GetConsensus(), *pblocktree))
  3266. return false;
  3267. // Load block file info
  3268. pblocktree->ReadLastBlockFile(nLastBlockFile);
  3269. vinfoBlockFile.resize(nLastBlockFile + 1);
  3270. LogPrintf("%s: last block file = %i\n", __func__, nLastBlockFile);
  3271. for (int nFile = 0; nFile <= nLastBlockFile; nFile++) {
  3272. pblocktree->ReadBlockFileInfo(nFile, vinfoBlockFile[nFile]);
  3273. }
  3274. LogPrintf("%s: last block file info: %s\n", __func__, vinfoBlockFile[nLastBlockFile].ToString());
  3275. for (int nFile = nLastBlockFile + 1; true; nFile++) {
  3276. CBlockFileInfo info;
  3277. if (pblocktree->ReadBlockFileInfo(nFile, info)) {
  3278. vinfoBlockFile.push_back(info);
  3279. } else {
  3280. break;
  3281. }
  3282. }
  3283. // Check presence of blk files
  3284. LogPrintf("Checking all blk files are present...\n");
  3285. std::set<int> setBlkDataFiles;
  3286. for (const std::pair<uint256, CBlockIndex*>& item : mapBlockIndex)
  3287. {
  3288. CBlockIndex* pindex = item.second;
  3289. if (pindex->nStatus & BLOCK_HAVE_DATA) {
  3290. setBlkDataFiles.insert(pindex->nFile);
  3291. }
  3292. }
  3293. for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++)
  3294. {
  3295. CDiskBlockPos pos(*it, 0);
  3296. if (CAutoFile(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION).IsNull()) {
  3297. return false;
  3298. }
  3299. }
  3300. // Check whether we have ever pruned block & undo files
  3301. pblocktree->ReadFlag("prunedblockfiles", fHavePruned);
  3302. if (fHavePruned)
  3303. LogPrintf("LoadBlockIndexDB(): Block files have previously been pruned\n");
  3304. // Check whether we need to continue reindexing
  3305. bool fReindexing = false;
  3306. pblocktree->ReadReindexing(fReindexing);
  3307. if(fReindexing) fReindex = true;
  3308. // Check whether we have a transaction index
  3309. pblocktree->ReadFlag("txindex", fTxIndex);
  3310. LogPrintf("%s: transaction index %s\n", __func__, fTxIndex ? "enabled" : "disabled");
  3311. return true;
  3312. }
  3313. bool LoadChainTip(const CChainParams& chainparams)
  3314. {
  3315. if (chainActive.Tip() && chainActive.Tip()->GetBlockHash() == pcoinsTip->GetBestBlock()) return true;
  3316. if (pcoinsTip->GetBestBlock().IsNull() && mapBlockIndex.size() == 1) {
  3317. // In case we just added the genesis block, connect it now, so
  3318. // that we always have a chainActive.Tip() when we return.
  3319. LogPrintf("%s: Connecting genesis block...\n", __func__);
  3320. CValidationState state;
  3321. if (!ActivateBestChain(state, chainparams)) {
  3322. return false;
  3323. }
  3324. }
  3325. // Load pointer to end of best chain
  3326. BlockMap::iterator it = mapBlockIndex.find(pcoinsTip->GetBestBlock());
  3327. if (it == mapBlockIndex.end())
  3328. return false;
  3329. chainActive.SetTip(it->second);
  3330. g_chainstate.PruneBlockIndexCandidates();
  3331. LogPrintf("Loaded best chain: hashBestChain=%s height=%d date=%s progress=%f\n",
  3332. chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(),
  3333. DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
  3334. GuessVerificationProgress(chainparams.TxData(), chainActive.Tip()));
  3335. return true;
  3336. }
  3337. CVerifyDB::CVerifyDB()
  3338. {
  3339. uiInterface.ShowProgress(_("Verifying blocks..."), 0, false);
  3340. }
  3341. CVerifyDB::~CVerifyDB()
  3342. {
  3343. uiInterface.ShowProgress("", 100, false);
  3344. }
  3345. bool CVerifyDB::VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview, int nCheckLevel, int nCheckDepth)
  3346. {
  3347. LOCK(cs_main);
  3348. if (chainActive.Tip() == nullptr || chainActive.Tip()->pprev == nullptr)
  3349. return true;
  3350. // Verify blocks in the best chain
  3351. if (nCheckDepth <= 0 || nCheckDepth > chainActive.Height())
  3352. nCheckDepth = chainActive.Height();
  3353. nCheckLevel = std::max(0, std::min(4, nCheckLevel));
  3354. LogPrintf("Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel);
  3355. CCoinsViewCache coins(coinsview);
  3356. CBlockIndex* pindexState = chainActive.Tip();
  3357. CBlockIndex* pindexFailure = nullptr;
  3358. int nGoodTransactions = 0;
  3359. CValidationState state;
  3360. int reportDone = 0;
  3361. LogPrintf("[0%%]...");
  3362. for (CBlockIndex* pindex = chainActive.Tip(); pindex && pindex->pprev; pindex = pindex->pprev)
  3363. {
  3364. boost::this_thread::interruption_point();
  3365. int percentageDone = std::max(1, std::min(99, (int)(((double)(chainActive.Height() - pindex->nHeight)) / (double)nCheckDepth * (nCheckLevel >= 4 ? 50 : 100))));
  3366. if (reportDone < percentageDone/10) {
  3367. // report every 10% step
  3368. LogPrintf("[%d%%]...", percentageDone);
  3369. reportDone = percentageDone/10;
  3370. }
  3371. uiInterface.ShowProgress(_("Verifying blocks..."), percentageDone, false);
  3372. if (pindex->nHeight < chainActive.Height()-nCheckDepth)
  3373. break;
  3374. if (fPruneMode && !(pindex->nStatus & BLOCK_HAVE_DATA)) {
  3375. // If pruning, only go back as far as we have data.
  3376. LogPrintf("VerifyDB(): block verification stopping at height %d (pruning, no data)\n", pindex->nHeight);
  3377. break;
  3378. }
  3379. CBlock block;
  3380. // check level 0: read from disk
  3381. if (!ReadBlockFromDisk(block, pindex, chainparams.GetConsensus()))
  3382. return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
  3383. // check level 1: verify block validity
  3384. if (nCheckLevel >= 1 && !CheckBlock(block, state, chainparams.GetConsensus()))
  3385. return error("%s: *** found bad block at %d, hash=%s (%s)\n", __func__,
  3386. pindex->nHeight, pindex->GetBlockHash().ToString(), FormatStateMessage(state));
  3387. // check level 2: verify undo validity
  3388. if (nCheckLevel >= 2 && pindex) {
  3389. CBlockUndo undo;
  3390. if (!pindex->GetUndoPos().IsNull()) {
  3391. if (!UndoReadFromDisk(undo, pindex)) {
  3392. return error("VerifyDB(): *** found bad undo data at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
  3393. }
  3394. }
  3395. }
  3396. // check level 3: check for inconsistencies during memory-only disconnect of tip blocks
  3397. if (nCheckLevel >= 3 && pindex == pindexState && (coins.DynamicMemoryUsage() + pcoinsTip->DynamicMemoryUsage()) <= nCoinCacheUsage) {
  3398. assert(coins.GetBestBlock() == pindex->GetBlockHash());
  3399. DisconnectResult res = g_chainstate.DisconnectBlock(block, pindex, coins);
  3400. if (res == DISCONNECT_FAILED) {
  3401. return error("VerifyDB(): *** irrecoverable inconsistency in block data at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
  3402. }
  3403. pindexState = pindex->pprev;
  3404. if (res == DISCONNECT_UNCLEAN) {
  3405. nGoodTransactions = 0;
  3406. pindexFailure = pindex;
  3407. } else {
  3408. nGoodTransactions += block.vtx.size();
  3409. }
  3410. }
  3411. if (ShutdownRequested())
  3412. return true;
  3413. }
  3414. if (pindexFailure)
  3415. return error("VerifyDB(): *** coin database inconsistencies found (last %i blocks, %i good transactions before that)\n", chainActive.Height() - pindexFailure->nHeight + 1, nGoodTransactions);
  3416. // check level 4: try reconnecting blocks
  3417. if (nCheckLevel >= 4) {
  3418. CBlockIndex *pindex = pindexState;
  3419. while (pindex != chainActive.Tip()) {
  3420. boost::this_thread::interruption_point();
  3421. uiInterface.ShowProgress(_("Verifying blocks..."), std::max(1, std::min(99, 100 - (int)(((double)(chainActive.Height() - pindex->nHeight)) / (double)nCheckDepth * 50))), false);
  3422. pindex = chainActive.Next(pindex);
  3423. CBlock block;
  3424. if (!ReadBlockFromDisk(block, pindex, chainparams.GetConsensus()))
  3425. return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
  3426. if (!g_chainstate.ConnectBlock(block, state, pindex, coins, chainparams))
  3427. return error("VerifyDB(): *** found unconnectable block at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
  3428. }
  3429. }
  3430. LogPrintf("[DONE].\n");
  3431. LogPrintf("No coin database inconsistencies in last %i blocks (%i transactions)\n", chainActive.Height() - pindexState->nHeight, nGoodTransactions);
  3432. return true;
  3433. }
  3434. /** Apply the effects of a block on the utxo cache, ignoring that it may already have been applied. */
  3435. bool CChainState::RollforwardBlock(const CBlockIndex* pindex, CCoinsViewCache& inputs, const CChainParams& params)
  3436. {
  3437. // TODO: merge with ConnectBlock
  3438. CBlock block;
  3439. if (!ReadBlockFromDisk(block, pindex, params.GetConsensus())) {
  3440. return error("ReplayBlock(): ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
  3441. }
  3442. for (const CTransactionRef& tx : block.vtx) {
  3443. if (!tx->IsCoinBase()) {
  3444. for (const CTxIn &txin : tx->vin) {
  3445. inputs.SpendCoin(txin.prevout);
  3446. }
  3447. }
  3448. // Pass check = true as every addition may be an overwrite.
  3449. AddCoins(inputs, *tx, pindex->nHeight, true);
  3450. }
  3451. return true;
  3452. }
  3453. bool CChainState::ReplayBlocks(const CChainParams& params, CCoinsView* view)
  3454. {
  3455. LOCK(cs_main);
  3456. CCoinsViewCache cache(view);
  3457. std::vector<uint256> hashHeads = view->GetHeadBlocks();
  3458. if (hashHeads.empty()) return true; // We're already in a consistent state.
  3459. if (hashHeads.size() != 2) return error("ReplayBlocks(): unknown inconsistent state");
  3460. uiInterface.ShowProgress(_("Replaying blocks..."), 0, false);
  3461. LogPrintf("Replaying blocks\n");
  3462. const CBlockIndex* pindexOld = nullptr; // Old tip during the interrupted flush.
  3463. const CBlockIndex* pindexNew; // New tip during the interrupted flush.
  3464. const CBlockIndex* pindexFork = nullptr; // Latest block common to both the old and the new tip.
  3465. if (mapBlockIndex.count(hashHeads[0]) == 0) {
  3466. return error("ReplayBlocks(): reorganization to unknown block requested");
  3467. }
  3468. pindexNew = mapBlockIndex[hashHeads[0]];
  3469. if (!hashHeads[1].IsNull()) { // The old tip is allowed to be 0, indicating it's the first flush.
  3470. if (mapBlockIndex.count(hashHeads[1]) == 0) {
  3471. return error("ReplayBlocks(): reorganization from unknown block requested");
  3472. }
  3473. pindexOld = mapBlockIndex[hashHeads[1]];
  3474. pindexFork = LastCommonAncestor(pindexOld, pindexNew);
  3475. assert(pindexFork != nullptr);
  3476. }
  3477. // Rollback along the old branch.
  3478. while (pindexOld != pindexFork) {
  3479. if (pindexOld->nHeight > 0) { // Never disconnect the genesis block.
  3480. CBlock block;
  3481. if (!ReadBlockFromDisk(block, pindexOld, params.GetConsensus())) {
  3482. return error("RollbackBlock(): ReadBlockFromDisk() failed at %d, hash=%s", pindexOld->nHeight, pindexOld->GetBlockHash().ToString());
  3483. }
  3484. LogPrintf("Rolling back %s (%i)\n", pindexOld->GetBlockHash().ToString(), pindexOld->nHeight);
  3485. DisconnectResult res = DisconnectBlock(block, pindexOld, cache);
  3486. if (res == DISCONNECT_FAILED) {
  3487. return error("RollbackBlock(): DisconnectBlock failed at %d, hash=%s", pindexOld->nHeight, pindexOld->GetBlockHash().ToString());
  3488. }
  3489. // If DISCONNECT_UNCLEAN is returned, it means a non-existing UTXO was deleted, or an existing UTXO was
  3490. // overwritten. It corresponds to cases where the block-to-be-disconnect never had all its operations
  3491. // applied to the UTXO set. However, as both writing a UTXO and deleting a UTXO are idempotent operations,
  3492. // the result is still a version of the UTXO set with the effects of that block undone.
  3493. }
  3494. pindexOld = pindexOld->pprev;
  3495. }
  3496. // Roll forward from the forking point to the new tip.
  3497. int nForkHeight = pindexFork ? pindexFork->nHeight : 0;
  3498. for (int nHeight = nForkHeight + 1; nHeight <= pindexNew->nHeight; ++nHeight) {
  3499. const CBlockIndex* pindex = pindexNew->GetAncestor(nHeight);
  3500. LogPrintf("Rolling forward %s (%i)\n", pindex->GetBlockHash().ToString(), nHeight);
  3501. if (!RollforwardBlock(pindex, cache, params)) return false;
  3502. }
  3503. cache.SetBestBlock(pindexNew->GetBlockHash());
  3504. cache.Flush();
  3505. uiInterface.ShowProgress("", 100, false);
  3506. return true;
  3507. }
  3508. bool ReplayBlocks(const CChainParams& params, CCoinsView* view) {
  3509. return g_chainstate.ReplayBlocks(params, view);
  3510. }
  3511. bool CChainState::RewindBlockIndex(const CChainParams& params)
  3512. {
  3513. LOCK(cs_main);
  3514. // Note that during -reindex-chainstate we are called with an empty chainActive!
  3515. int nHeight = 1;
  3516. while (nHeight <= chainActive.Height()) {
  3517. if (IsWitnessEnabled(chainActive[nHeight - 1], params.GetConsensus()) && !(chainActive[nHeight]->nStatus & BLOCK_OPT_WITNESS)) {
  3518. break;
  3519. }
  3520. nHeight++;
  3521. }
  3522. // nHeight is now the height of the first insufficiently-validated block, or tipheight + 1
  3523. CValidationState state;
  3524. CBlockIndex* pindex = chainActive.Tip();
  3525. while (chainActive.Height() >= nHeight) {
  3526. if (fPruneMode && !(chainActive.Tip()->nStatus & BLOCK_HAVE_DATA)) {
  3527. // If pruning, don't try rewinding past the HAVE_DATA point;
  3528. // since older blocks can't be served anyway, there's
  3529. // no need to walk further, and trying to DisconnectTip()
  3530. // will fail (and require a needless reindex/redownload
  3531. // of the blockchain).
  3532. break;
  3533. }
  3534. if (!DisconnectTip(state, params, nullptr)) {
  3535. return error("RewindBlockIndex: unable to disconnect block at height %i", pindex->nHeight);
  3536. }
  3537. // Occasionally flush state to disk.
  3538. if (!FlushStateToDisk(params, state, FLUSH_STATE_PERIODIC))
  3539. return false;
  3540. }
  3541. // Reduce validity flag and have-data flags.
  3542. // We do this after actual disconnecting, otherwise we'll end up writing the lack of data
  3543. // to disk before writing the chainstate, resulting in a failure to continue if interrupted.
  3544. for (const auto& entry : mapBlockIndex) {
  3545. CBlockIndex* pindexIter = entry.second;
  3546. // Note: If we encounter an insufficiently validated block that
  3547. // is on chainActive, it must be because we are a pruning node, and
  3548. // this block or some successor doesn't HAVE_DATA, so we were unable to
  3549. // rewind all the way. Blocks remaining on chainActive at this point
  3550. // must not have their validity reduced.
  3551. if (IsWitnessEnabled(pindexIter->pprev, params.GetConsensus()) && !(pindexIter->nStatus & BLOCK_OPT_WITNESS) && !chainActive.Contains(pindexIter)) {
  3552. // Reduce validity
  3553. pindexIter->nStatus = std::min<unsigned int>(pindexIter->nStatus & BLOCK_VALID_MASK, BLOCK_VALID_TREE) | (pindexIter->nStatus & ~BLOCK_VALID_MASK);
  3554. // Remove have-data flags.
  3555. pindexIter->nStatus &= ~(BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO);
  3556. // Remove storage location.
  3557. pindexIter->nFile = 0;
  3558. pindexIter->nDataPos = 0;
  3559. pindexIter->nUndoPos = 0;
  3560. // Remove various other things
  3561. pindexIter->nTx = 0;
  3562. pindexIter->nChainTx = 0;
  3563. pindexIter->nSequenceId = 0;
  3564. // Make sure it gets written.
  3565. setDirtyBlockIndex.insert(pindexIter);
  3566. // Update indexes
  3567. setBlockIndexCandidates.erase(pindexIter);
  3568. std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> ret = mapBlocksUnlinked.equal_range(pindexIter->pprev);
  3569. while (ret.first != ret.second) {
  3570. if (ret.first->second == pindexIter) {
  3571. mapBlocksUnlinked.erase(ret.first++);
  3572. } else {
  3573. ++ret.first;
  3574. }
  3575. }
  3576. } else if (pindexIter->IsValid(BLOCK_VALID_TRANSACTIONS) && pindexIter->nChainTx) {
  3577. setBlockIndexCandidates.insert(pindexIter);
  3578. }
  3579. }
  3580. if (chainActive.Tip() != nullptr) {
  3581. // We can't prune block index candidates based on our tip if we have
  3582. // no tip due to chainActive being empty!
  3583. PruneBlockIndexCandidates();
  3584. CheckBlockIndex(params.GetConsensus());
  3585. }
  3586. return true;
  3587. }
  3588. bool RewindBlockIndex(const CChainParams& params) {
  3589. if (!g_chainstate.RewindBlockIndex(params)) {
  3590. return false;
  3591. }
  3592. if (chainActive.Tip() != nullptr) {
  3593. // FlushStateToDisk can possibly read chainActive. Be conservative
  3594. // and skip it here, we're about to -reindex-chainstate anyway, so
  3595. // it'll get called a bunch real soon.
  3596. CValidationState state;
  3597. if (!FlushStateToDisk(params, state, FLUSH_STATE_ALWAYS)) {
  3598. return false;
  3599. }
  3600. }
  3601. return true;
  3602. }
  3603. void CChainState::UnloadBlockIndex() {
  3604. nBlockSequenceId = 1;
  3605. g_failed_blocks.clear();
  3606. setBlockIndexCandidates.clear();
  3607. }
  3608. // May NOT be used after any connections are up as much
  3609. // of the peer-processing logic assumes a consistent
  3610. // block index state
  3611. void UnloadBlockIndex()
  3612. {
  3613. LOCK(cs_main);
  3614. chainActive.SetTip(nullptr);
  3615. pindexBestInvalid = nullptr;
  3616. pindexBestHeader = nullptr;
  3617. mempool.clear();
  3618. mapBlocksUnlinked.clear();
  3619. vinfoBlockFile.clear();
  3620. nLastBlockFile = 0;
  3621. setDirtyBlockIndex.clear();
  3622. setDirtyFileInfo.clear();
  3623. versionbitscache.Clear();
  3624. for (int b = 0; b < VERSIONBITS_NUM_BITS; b++) {
  3625. warningcache[b].clear();
  3626. }
  3627. for (BlockMap::value_type& entry : mapBlockIndex) {
  3628. delete entry.second;
  3629. }
  3630. mapBlockIndex.clear();
  3631. fHavePruned = false;
  3632. g_chainstate.UnloadBlockIndex();
  3633. }
  3634. bool LoadBlockIndex(const CChainParams& chainparams)
  3635. {
  3636. // Load block index from databases
  3637. bool needs_init = fReindex;
  3638. if (!fReindex) {
  3639. bool ret = LoadBlockIndexDB(chainparams);
  3640. if (!ret) return false;
  3641. needs_init = mapBlockIndex.empty();
  3642. }
  3643. if (needs_init) {
  3644. // Everything here is for *new* reindex/DBs. Thus, though
  3645. // LoadBlockIndexDB may have set fReindex if we shut down
  3646. // mid-reindex previously, we don't check fReindex and
  3647. // instead only check it prior to LoadBlockIndexDB to set
  3648. // needs_init.
  3649. LogPrintf("Initializing databases...\n");
  3650. // Use the provided setting for -txindex in the new database
  3651. fTxIndex = gArgs.GetBoolArg("-txindex", DEFAULT_TXINDEX);
  3652. pblocktree->WriteFlag("txindex", fTxIndex);
  3653. }
  3654. return true;
  3655. }
  3656. bool CChainState::LoadGenesisBlock(const CChainParams& chainparams)
  3657. {
  3658. LOCK(cs_main);
  3659. // Check whether we're already initialized by checking for genesis in
  3660. // mapBlockIndex. Note that we can't use chainActive here, since it is
  3661. // set based on the coins db, not the block index db, which is the only
  3662. // thing loaded at this point.
  3663. if (mapBlockIndex.count(chainparams.GenesisBlock().GetHash()))
  3664. return true;
  3665. try {
  3666. CBlock &block = const_cast<CBlock&>(chainparams.GenesisBlock());
  3667. CDiskBlockPos blockPos = SaveBlockToDisk(block, 0, chainparams, nullptr);
  3668. if (blockPos.IsNull())
  3669. return error("%s: writing genesis block to disk failed", __func__);
  3670. CBlockIndex *pindex = AddToBlockIndex(block);
  3671. CValidationState state;
  3672. if (!ReceivedBlockTransactions(block, state, pindex, blockPos, chainparams.GetConsensus()))
  3673. return error("%s: genesis block not accepted", __func__);
  3674. } catch (const std::runtime_error& e) {
  3675. return error("%s: failed to write genesis block: %s", __func__, e.what());
  3676. }
  3677. return true;
  3678. }
  3679. bool LoadGenesisBlock(const CChainParams& chainparams)
  3680. {
  3681. return g_chainstate.LoadGenesisBlock(chainparams);
  3682. }
  3683. bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, CDiskBlockPos *dbp)
  3684. {
  3685. // Map of disk positions for blocks with unknown parent (only used for reindex)
  3686. static std::multimap<uint256, CDiskBlockPos> mapBlocksUnknownParent;
  3687. int64_t nStart = GetTimeMillis();
  3688. int nLoaded = 0;
  3689. try {
  3690. // This takes over fileIn and calls fclose() on it in the CBufferedFile destructor
  3691. CBufferedFile blkdat(fileIn, 2*MAX_BLOCK_SERIALIZED_SIZE, MAX_BLOCK_SERIALIZED_SIZE+8, SER_DISK, CLIENT_VERSION);
  3692. uint64_t nRewind = blkdat.GetPos();
  3693. while (!blkdat.eof()) {
  3694. boost::this_thread::interruption_point();
  3695. blkdat.SetPos(nRewind);
  3696. nRewind++; // start one byte further next time, in case of failure
  3697. blkdat.SetLimit(); // remove former limit
  3698. unsigned int nSize = 0;
  3699. try {
  3700. // locate a header
  3701. unsigned char buf[CMessageHeader::MESSAGE_START_SIZE];
  3702. blkdat.FindByte(chainparams.MessageStart()[0]);
  3703. nRewind = blkdat.GetPos()+1;
  3704. blkdat >> FLATDATA(buf);
  3705. if (memcmp(buf, chainparams.MessageStart(), CMessageHeader::MESSAGE_START_SIZE))
  3706. continue;
  3707. // read size
  3708. blkdat >> nSize;
  3709. if (nSize < 80 || nSize > MAX_BLOCK_SERIALIZED_SIZE)
  3710. continue;
  3711. } catch (const std::exception&) {
  3712. // no valid block header found; don't complain
  3713. break;
  3714. }
  3715. try {
  3716. // read block
  3717. uint64_t nBlockPos = blkdat.GetPos();
  3718. if (dbp)
  3719. dbp->nPos = nBlockPos;
  3720. blkdat.SetLimit(nBlockPos + nSize);
  3721. blkdat.SetPos(nBlockPos);
  3722. std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
  3723. CBlock& block = *pblock;
  3724. blkdat >> block;
  3725. nRewind = blkdat.GetPos();
  3726. // detect out of order blocks, and store them for later
  3727. uint256 hash = block.GetHash();
  3728. if (hash != chainparams.GetConsensus().hashGenesisBlock && mapBlockIndex.find(block.hashPrevBlock) == mapBlockIndex.end()) {
  3729. LogPrint(BCLog::REINDEX, "%s: Out of order block %s, parent %s not known\n", __func__, hash.ToString(),
  3730. block.hashPrevBlock.ToString());
  3731. if (dbp)
  3732. mapBlocksUnknownParent.insert(std::make_pair(block.hashPrevBlock, *dbp));
  3733. continue;
  3734. }
  3735. // process in case the block isn't known yet
  3736. if (mapBlockIndex.count(hash) == 0 || (mapBlockIndex[hash]->nStatus & BLOCK_HAVE_DATA) == 0) {
  3737. LOCK(cs_main);
  3738. CValidationState state;
  3739. if (g_chainstate.AcceptBlock(pblock, state, chainparams, nullptr, true, dbp, nullptr))
  3740. nLoaded++;
  3741. if (state.IsError())
  3742. break;
  3743. } else if (hash != chainparams.GetConsensus().hashGenesisBlock && mapBlockIndex[hash]->nHeight % 1000 == 0) {
  3744. LogPrint(BCLog::REINDEX, "Block Import: already had block %s at height %d\n", hash.ToString(), mapBlockIndex[hash]->nHeight);
  3745. }
  3746. // Activate the genesis block so normal node progress can continue
  3747. if (hash == chainparams.GetConsensus().hashGenesisBlock) {
  3748. CValidationState state;
  3749. if (!ActivateBestChain(state, chainparams)) {
  3750. break;
  3751. }
  3752. }
  3753. NotifyHeaderTip();
  3754. // Recursively process earlier encountered successors of this block
  3755. std::deque<uint256> queue;
  3756. queue.push_back(hash);
  3757. while (!queue.empty()) {
  3758. uint256 head = queue.front();
  3759. queue.pop_front();
  3760. std::pair<std::multimap<uint256, CDiskBlockPos>::iterator, std::multimap<uint256, CDiskBlockPos>::iterator> range = mapBlocksUnknownParent.equal_range(head);
  3761. while (range.first != range.second) {
  3762. std::multimap<uint256, CDiskBlockPos>::iterator it = range.first;
  3763. std::shared_ptr<CBlock> pblockrecursive = std::make_shared<CBlock>();
  3764. if (ReadBlockFromDisk(*pblockrecursive, it->second, chainparams.GetConsensus()))
  3765. {
  3766. LogPrint(BCLog::REINDEX, "%s: Processing out of order child %s of %s\n", __func__, pblockrecursive->GetHash().ToString(),
  3767. head.ToString());
  3768. LOCK(cs_main);
  3769. CValidationState dummy;
  3770. if (g_chainstate.AcceptBlock(pblockrecursive, dummy, chainparams, nullptr, true, &it->second, nullptr))
  3771. {
  3772. nLoaded++;
  3773. queue.push_back(pblockrecursive->GetHash());
  3774. }
  3775. }
  3776. range.first++;
  3777. mapBlocksUnknownParent.erase(it);
  3778. NotifyHeaderTip();
  3779. }
  3780. }
  3781. } catch (const std::exception& e) {
  3782. LogPrintf("%s: Deserialize or I/O error - %s\n", __func__, e.what());
  3783. }
  3784. }
  3785. } catch (const std::runtime_error& e) {
  3786. AbortNode(std::string("System error: ") + e.what());
  3787. }
  3788. if (nLoaded > 0)
  3789. LogPrintf("Loaded %i blocks from external file in %dms\n", nLoaded, GetTimeMillis() - nStart);
  3790. return nLoaded > 0;
  3791. }
  3792. void CChainState::CheckBlockIndex(const Consensus::Params& consensusParams)
  3793. {
  3794. if (!fCheckBlockIndex) {
  3795. return;
  3796. }
  3797. LOCK(cs_main);
  3798. // During a reindex, we read the genesis block and call CheckBlockIndex before ActivateBestChain,
  3799. // so we have the genesis block in mapBlockIndex but no active chain. (A few of the tests when
  3800. // iterating the block tree require that chainActive has been initialized.)
  3801. if (chainActive.Height() < 0) {
  3802. assert(mapBlockIndex.size() <= 1);
  3803. return;
  3804. }
  3805. // Build forward-pointing map of the entire block tree.
  3806. std::multimap<CBlockIndex*,CBlockIndex*> forward;
  3807. for (auto& entry : mapBlockIndex) {
  3808. forward.insert(std::make_pair(entry.second->pprev, entry.second));
  3809. }
  3810. assert(forward.size() == mapBlockIndex.size());
  3811. std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangeGenesis = forward.equal_range(nullptr);
  3812. CBlockIndex *pindex = rangeGenesis.first->second;
  3813. rangeGenesis.first++;
  3814. assert(rangeGenesis.first == rangeGenesis.second); // There is only one index entry with parent nullptr.
  3815. // Iterate over the entire block tree, using depth-first search.
  3816. // Along the way, remember whether there are blocks on the path from genesis
  3817. // block being explored which are the first to have certain properties.
  3818. size_t nNodes = 0;
  3819. int nHeight = 0;
  3820. CBlockIndex* pindexFirstInvalid = nullptr; // Oldest ancestor of pindex which is invalid.
  3821. CBlockIndex* pindexFirstMissing = nullptr; // Oldest ancestor of pindex which does not have BLOCK_HAVE_DATA.
  3822. CBlockIndex* pindexFirstNeverProcessed = nullptr; // Oldest ancestor of pindex for which nTx == 0.
  3823. CBlockIndex* pindexFirstNotTreeValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_TREE (regardless of being valid or not).
  3824. CBlockIndex* pindexFirstNotTransactionsValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_TRANSACTIONS (regardless of being valid or not).
  3825. CBlockIndex* pindexFirstNotChainValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_CHAIN (regardless of being valid or not).
  3826. CBlockIndex* pindexFirstNotScriptsValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_SCRIPTS (regardless of being valid or not).
  3827. while (pindex != nullptr) {
  3828. nNodes++;
  3829. if (pindexFirstInvalid == nullptr && pindex->nStatus & BLOCK_FAILED_VALID) pindexFirstInvalid = pindex;
  3830. if (pindexFirstMissing == nullptr && !(pindex->nStatus & BLOCK_HAVE_DATA)) pindexFirstMissing = pindex;
  3831. if (pindexFirstNeverProcessed == nullptr && pindex->nTx == 0) pindexFirstNeverProcessed = pindex;
  3832. if (pindex->pprev != nullptr && pindexFirstNotTreeValid == nullptr && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_TREE) pindexFirstNotTreeValid = pindex;
  3833. if (pindex->pprev != nullptr && pindexFirstNotTransactionsValid == nullptr && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_TRANSACTIONS) pindexFirstNotTransactionsValid = pindex;
  3834. if (pindex->pprev != nullptr && pindexFirstNotChainValid == nullptr && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_CHAIN) pindexFirstNotChainValid = pindex;
  3835. if (pindex->pprev != nullptr && pindexFirstNotScriptsValid == nullptr && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_SCRIPTS) pindexFirstNotScriptsValid = pindex;
  3836. // Begin: actual consistency checks.
  3837. if (pindex->pprev == nullptr) {
  3838. // Genesis block checks.
  3839. assert(pindex->GetBlockHash() == consensusParams.hashGenesisBlock); // Genesis block's hash must match.
  3840. assert(pindex == chainActive.Genesis()); // The current active chain's genesis block must be this block.
  3841. }
  3842. if (pindex->nChainTx == 0) assert(pindex->nSequenceId <= 0); // nSequenceId can't be set positive for blocks that aren't linked (negative is used for preciousblock)
  3843. // VALID_TRANSACTIONS is equivalent to nTx > 0 for all nodes (whether or not pruning has occurred).
  3844. // HAVE_DATA is only equivalent to nTx > 0 (or VALID_TRANSACTIONS) if no pruning has occurred.
  3845. if (!fHavePruned) {
  3846. // If we've never pruned, then HAVE_DATA should be equivalent to nTx > 0
  3847. assert(!(pindex->nStatus & BLOCK_HAVE_DATA) == (pindex->nTx == 0));
  3848. assert(pindexFirstMissing == pindexFirstNeverProcessed);
  3849. } else {
  3850. // If we have pruned, then we can only say that HAVE_DATA implies nTx > 0
  3851. if (pindex->nStatus & BLOCK_HAVE_DATA) assert(pindex->nTx > 0);
  3852. }
  3853. if (pindex->nStatus & BLOCK_HAVE_UNDO) assert(pindex->nStatus & BLOCK_HAVE_DATA);
  3854. assert(((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TRANSACTIONS) == (pindex->nTx > 0)); // This is pruning-independent.
  3855. // All parents having had data (at some point) is equivalent to all parents being VALID_TRANSACTIONS, which is equivalent to nChainTx being set.
  3856. assert((pindexFirstNeverProcessed != nullptr) == (pindex->nChainTx == 0)); // nChainTx != 0 is used to signal that all parent blocks have been processed (but may have been pruned).
  3857. assert((pindexFirstNotTransactionsValid != nullptr) == (pindex->nChainTx == 0));
  3858. assert(pindex->nHeight == nHeight); // nHeight must be consistent.
  3859. assert(pindex->pprev == nullptr || pindex->nChainWork >= pindex->pprev->nChainWork); // For every block except the genesis block, the chainwork must be larger than the parent's.
  3860. assert(nHeight < 2 || (pindex->pskip && (pindex->pskip->nHeight < nHeight))); // The pskip pointer must point back for all but the first 2 blocks.
  3861. assert(pindexFirstNotTreeValid == nullptr); // All mapBlockIndex entries must at least be TREE valid
  3862. if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TREE) assert(pindexFirstNotTreeValid == nullptr); // TREE valid implies all parents are TREE valid
  3863. if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_CHAIN) assert(pindexFirstNotChainValid == nullptr); // CHAIN valid implies all parents are CHAIN valid
  3864. if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_SCRIPTS) assert(pindexFirstNotScriptsValid == nullptr); // SCRIPTS valid implies all parents are SCRIPTS valid
  3865. if (pindexFirstInvalid == nullptr) {
  3866. // Checks for not-invalid blocks.
  3867. assert((pindex->nStatus & BLOCK_FAILED_MASK) == 0); // The failed mask cannot be set for blocks without invalid parents.
  3868. }
  3869. if (!CBlockIndexWorkComparator()(pindex, chainActive.Tip()) && pindexFirstNeverProcessed == nullptr) {
  3870. if (pindexFirstInvalid == nullptr) {
  3871. // If this block sorts at least as good as the current tip and
  3872. // is valid and we have all data for its parents, it must be in
  3873. // setBlockIndexCandidates. chainActive.Tip() must also be there
  3874. // even if some data has been pruned.
  3875. if (pindexFirstMissing == nullptr || pindex == chainActive.Tip()) {
  3876. assert(setBlockIndexCandidates.count(pindex));
  3877. }
  3878. // If some parent is missing, then it could be that this block was in
  3879. // setBlockIndexCandidates but had to be removed because of the missing data.
  3880. // In this case it must be in mapBlocksUnlinked -- see test below.
  3881. }
  3882. } else { // If this block sorts worse than the current tip or some ancestor's block has never been seen, it cannot be in setBlockIndexCandidates.
  3883. assert(setBlockIndexCandidates.count(pindex) == 0);
  3884. }
  3885. // Check whether this block is in mapBlocksUnlinked.
  3886. std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangeUnlinked = mapBlocksUnlinked.equal_range(pindex->pprev);
  3887. bool foundInUnlinked = false;
  3888. while (rangeUnlinked.first != rangeUnlinked.second) {
  3889. assert(rangeUnlinked.first->first == pindex->pprev);
  3890. if (rangeUnlinked.first->second == pindex) {
  3891. foundInUnlinked = true;
  3892. break;
  3893. }
  3894. rangeUnlinked.first++;
  3895. }
  3896. if (pindex->pprev && (pindex->nStatus & BLOCK_HAVE_DATA) && pindexFirstNeverProcessed != nullptr && pindexFirstInvalid == nullptr) {
  3897. // If this block has block data available, some parent was never received, and has no invalid parents, it must be in mapBlocksUnlinked.
  3898. assert(foundInUnlinked);
  3899. }
  3900. if (!(pindex->nStatus & BLOCK_HAVE_DATA)) assert(!foundInUnlinked); // Can't be in mapBlocksUnlinked if we don't HAVE_DATA
  3901. if (pindexFirstMissing == nullptr) assert(!foundInUnlinked); // We aren't missing data for any parent -- cannot be in mapBlocksUnlinked.
  3902. if (pindex->pprev && (pindex->nStatus & BLOCK_HAVE_DATA) && pindexFirstNeverProcessed == nullptr && pindexFirstMissing != nullptr) {
  3903. // We HAVE_DATA for this block, have received data for all parents at some point, but we're currently missing data for some parent.
  3904. assert(fHavePruned); // We must have pruned.
  3905. // This block may have entered mapBlocksUnlinked if:
  3906. // - it has a descendant that at some point had more work than the
  3907. // tip, and
  3908. // - we tried switching to that descendant but were missing
  3909. // data for some intermediate block between chainActive and the
  3910. // tip.
  3911. // So if this block is itself better than chainActive.Tip() and it wasn't in
  3912. // setBlockIndexCandidates, then it must be in mapBlocksUnlinked.
  3913. if (!CBlockIndexWorkComparator()(pindex, chainActive.Tip()) && setBlockIndexCandidates.count(pindex) == 0) {
  3914. if (pindexFirstInvalid == nullptr) {
  3915. assert(foundInUnlinked);
  3916. }
  3917. }
  3918. }
  3919. // assert(pindex->GetBlockHash() == pindex->GetBlockHeader().GetHash()); // Perhaps too slow
  3920. // End: actual consistency checks.
  3921. // Try descending into the first subnode.
  3922. std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> range = forward.equal_range(pindex);
  3923. if (range.first != range.second) {
  3924. // A subnode was found.
  3925. pindex = range.first->second;
  3926. nHeight++;
  3927. continue;
  3928. }
  3929. // This is a leaf node.
  3930. // Move upwards until we reach a node of which we have not yet visited the last child.
  3931. while (pindex) {
  3932. // We are going to either move to a parent or a sibling of pindex.
  3933. // If pindex was the first with a certain property, unset the corresponding variable.
  3934. if (pindex == pindexFirstInvalid) pindexFirstInvalid = nullptr;
  3935. if (pindex == pindexFirstMissing) pindexFirstMissing = nullptr;
  3936. if (pindex == pindexFirstNeverProcessed) pindexFirstNeverProcessed = nullptr;
  3937. if (pindex == pindexFirstNotTreeValid) pindexFirstNotTreeValid = nullptr;
  3938. if (pindex == pindexFirstNotTransactionsValid) pindexFirstNotTransactionsValid = nullptr;
  3939. if (pindex == pindexFirstNotChainValid) pindexFirstNotChainValid = nullptr;
  3940. if (pindex == pindexFirstNotScriptsValid) pindexFirstNotScriptsValid = nullptr;
  3941. // Find our parent.
  3942. CBlockIndex* pindexPar = pindex->pprev;
  3943. // Find which child we just visited.
  3944. std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangePar = forward.equal_range(pindexPar);
  3945. while (rangePar.first->second != pindex) {
  3946. assert(rangePar.first != rangePar.second); // Our parent must have at least the node we're coming from as child.
  3947. rangePar.first++;
  3948. }
  3949. // Proceed to the next one.
  3950. rangePar.first++;
  3951. if (rangePar.first != rangePar.second) {
  3952. // Move to the sibling.
  3953. pindex = rangePar.first->second;
  3954. break;
  3955. } else {
  3956. // Move up further.
  3957. pindex = pindexPar;
  3958. nHeight--;
  3959. continue;
  3960. }
  3961. }
  3962. }
  3963. // Check that we actually traversed the entire map.
  3964. assert(nNodes == forward.size());
  3965. }
  3966. std::string CBlockFileInfo::ToString() const
  3967. {
  3968. return strprintf("CBlockFileInfo(blocks=%u, size=%u, heights=%u...%u, time=%s...%s)", nBlocks, nSize, nHeightFirst, nHeightLast, DateTimeStrFormat("%Y-%m-%d", nTimeFirst), DateTimeStrFormat("%Y-%m-%d", nTimeLast));
  3969. }
  3970. CBlockFileInfo* GetBlockFileInfo(size_t n)
  3971. {
  3972. LOCK(cs_LastBlockFile);
  3973. return &vinfoBlockFile.at(n);
  3974. }
  3975. ThresholdState VersionBitsTipState(const Consensus::Params& params, Consensus::DeploymentPos pos)
  3976. {
  3977. LOCK(cs_main);
  3978. return VersionBitsState(chainActive.Tip(), params, pos, versionbitscache);
  3979. }
  3980. BIP9Stats VersionBitsTipStatistics(const Consensus::Params& params, Consensus::DeploymentPos pos)
  3981. {
  3982. LOCK(cs_main);
  3983. return VersionBitsStatistics(chainActive.Tip(), params, pos);
  3984. }
  3985. int VersionBitsTipStateSinceHeight(const Consensus::Params& params, Consensus::DeploymentPos pos)
  3986. {
  3987. LOCK(cs_main);
  3988. return VersionBitsStateSinceHeight(chainActive.Tip(), params, pos, versionbitscache);
  3989. }
  3990. static const uint64_t MEMPOOL_DUMP_VERSION = 1;
  3991. bool LoadMempool(void)
  3992. {
  3993. const CChainParams& chainparams = Params();
  3994. int64_t nExpiryTimeout = gArgs.GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60;
  3995. FILE* filestr = fsbridge::fopen(GetDataDir() / "mempool.dat", "rb");
  3996. CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
  3997. if (file.IsNull()) {
  3998. LogPrintf("Failed to open mempool file from disk. Continuing anyway.\n");
  3999. return false;
  4000. }
  4001. int64_t count = 0;
  4002. int64_t expired = 0;
  4003. int64_t failed = 0;
  4004. int64_t already_there = 0;
  4005. int64_t nNow = GetTime();
  4006. try {
  4007. uint64_t version;
  4008. file >> version;
  4009. if (version != MEMPOOL_DUMP_VERSION) {
  4010. return false;
  4011. }
  4012. uint64_t num;
  4013. file >> num;
  4014. while (num--) {
  4015. CTransactionRef tx;
  4016. int64_t nTime;
  4017. int64_t nFeeDelta;
  4018. file >> tx;
  4019. file >> nTime;
  4020. file >> nFeeDelta;
  4021. CAmount amountdelta = nFeeDelta;
  4022. if (amountdelta) {
  4023. mempool.PrioritiseTransaction(tx->GetHash(), amountdelta);
  4024. }
  4025. CValidationState state;
  4026. if (nTime + nExpiryTimeout > nNow) {
  4027. LOCK(cs_main);
  4028. AcceptToMemoryPoolWithTime(chainparams, mempool, state, tx, nullptr /* pfMissingInputs */, nTime,
  4029. nullptr /* plTxnReplaced */, false /* bypass_limits */, 0 /* nAbsurdFee */);
  4030. if (state.IsValid()) {
  4031. ++count;
  4032. } else {
  4033. // mempool may contain the transaction already, e.g. from
  4034. // wallet(s) having loaded it while we were processing
  4035. // mempool transactions; consider these as valid, instead of
  4036. // failed, but mark them as 'already there'
  4037. if (mempool.exists(tx->GetHash())) {
  4038. ++already_there;
  4039. } else {
  4040. ++failed;
  4041. }
  4042. }
  4043. } else {
  4044. ++expired;
  4045. }
  4046. if (ShutdownRequested())
  4047. return false;
  4048. }
  4049. std::map<uint256, CAmount> mapDeltas;
  4050. file >> mapDeltas;
  4051. for (const auto& i : mapDeltas) {
  4052. mempool.PrioritiseTransaction(i.first, i.second);
  4053. }
  4054. } catch (const std::exception& e) {
  4055. LogPrintf("Failed to deserialize mempool data on disk: %s. Continuing anyway.\n", e.what());
  4056. return false;
  4057. }
  4058. LogPrintf("Imported mempool transactions from disk: %i succeeded, %i failed, %i expired, %i already there\n", count, failed, expired, already_there);
  4059. return true;
  4060. }
  4061. bool DumpMempool(void)
  4062. {
  4063. int64_t start = GetTimeMicros();
  4064. std::map<uint256, CAmount> mapDeltas;
  4065. std::vector<TxMempoolInfo> vinfo;
  4066. {
  4067. LOCK(mempool.cs);
  4068. for (const auto &i : mempool.mapDeltas) {
  4069. mapDeltas[i.first] = i.second;
  4070. }
  4071. vinfo = mempool.infoAll();
  4072. }
  4073. int64_t mid = GetTimeMicros();
  4074. try {
  4075. FILE* filestr = fsbridge::fopen(GetDataDir() / "mempool.dat.new", "wb");
  4076. if (!filestr) {
  4077. return false;
  4078. }
  4079. CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
  4080. uint64_t version = MEMPOOL_DUMP_VERSION;
  4081. file << version;
  4082. file << (uint64_t)vinfo.size();
  4083. for (const auto& i : vinfo) {
  4084. file << *(i.tx);
  4085. file << (int64_t)i.nTime;
  4086. file << (int64_t)i.nFeeDelta;
  4087. mapDeltas.erase(i.tx->GetHash());
  4088. }
  4089. file << mapDeltas;
  4090. FileCommit(file.Get());
  4091. file.fclose();
  4092. RenameOver(GetDataDir() / "mempool.dat.new", GetDataDir() / "mempool.dat");
  4093. int64_t last = GetTimeMicros();
  4094. LogPrintf("Dumped mempool: %gs to copy, %gs to dump\n", (mid-start)*MICRO, (last-mid)*MICRO);
  4095. } catch (const std::exception& e) {
  4096. LogPrintf("Failed to dump mempool: %s. Continuing anyway.\n", e.what());
  4097. return false;
  4098. }
  4099. return true;
  4100. }
  4101. //! Guess how far we are in the verification process at the given block index
  4102. double GuessVerificationProgress(const ChainTxData& data, const CBlockIndex *pindex) {
  4103. if (pindex == nullptr)
  4104. return 0.0;
  4105. int64_t nNow = time(nullptr);
  4106. double fTxTotal;
  4107. if (pindex->nChainTx <= data.nTxCount) {
  4108. fTxTotal = data.nTxCount + (nNow - data.nTime) * data.dTxRate;
  4109. } else {
  4110. fTxTotal = pindex->nChainTx + (nNow - pindex->GetBlockTime()) * data.dTxRate;
  4111. }
  4112. return pindex->nChainTx / fTxTotal;
  4113. }
  4114. class CMainCleanup
  4115. {
  4116. public:
  4117. CMainCleanup() {}
  4118. ~CMainCleanup() {
  4119. // block headers
  4120. BlockMap::iterator it1 = mapBlockIndex.begin();
  4121. for (; it1 != mapBlockIndex.end(); it1++)
  4122. delete (*it1).second;
  4123. mapBlockIndex.clear();
  4124. }
  4125. } instance_of_cmaincleanup;