Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

validation.cpp 208KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687
  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. }
  2431. pindex = pindex->pprev;
  2432. }
  2433. return true;
  2434. }
  2435. bool ResetBlockFailureFlags(CBlockIndex *pindex) {
  2436. return g_chainstate.ResetBlockFailureFlags(pindex);
  2437. }
  2438. CBlockIndex* CChainState::AddToBlockIndex(const CBlockHeader& block)
  2439. {
  2440. // Check for duplicate
  2441. uint256 hash = block.GetHash();
  2442. BlockMap::iterator it = mapBlockIndex.find(hash);
  2443. if (it != mapBlockIndex.end())
  2444. return it->second;
  2445. // Construct new block index object
  2446. CBlockIndex* pindexNew = new CBlockIndex(block);
  2447. // We assign the sequence id to blocks only when the full data is available,
  2448. // to avoid miners withholding blocks but broadcasting headers, to get a
  2449. // competitive advantage.
  2450. pindexNew->nSequenceId = 0;
  2451. BlockMap::iterator mi = mapBlockIndex.insert(std::make_pair(hash, pindexNew)).first;
  2452. pindexNew->phashBlock = &((*mi).first);
  2453. BlockMap::iterator miPrev = mapBlockIndex.find(block.hashPrevBlock);
  2454. if (miPrev != mapBlockIndex.end())
  2455. {
  2456. pindexNew->pprev = (*miPrev).second;
  2457. pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
  2458. pindexNew->BuildSkip();
  2459. }
  2460. pindexNew->nTimeMax = (pindexNew->pprev ? std::max(pindexNew->pprev->nTimeMax, pindexNew->nTime) : pindexNew->nTime);
  2461. pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + GetBlockProof(*pindexNew);
  2462. pindexNew->RaiseValidity(BLOCK_VALID_TREE);
  2463. if (pindexBestHeader == nullptr || pindexBestHeader->nChainWork < pindexNew->nChainWork)
  2464. pindexBestHeader = pindexNew;
  2465. setDirtyBlockIndex.insert(pindexNew);
  2466. return pindexNew;
  2467. }
  2468. /** Mark a block as having its data received and checked (up to BLOCK_VALID_TRANSACTIONS). */
  2469. bool CChainState::ReceivedBlockTransactions(const CBlock &block, CValidationState& state, CBlockIndex *pindexNew, const CDiskBlockPos& pos, const Consensus::Params& consensusParams)
  2470. {
  2471. pindexNew->nTx = block.vtx.size();
  2472. pindexNew->nChainTx = 0;
  2473. pindexNew->nFile = pos.nFile;
  2474. pindexNew->nDataPos = pos.nPos;
  2475. pindexNew->nUndoPos = 0;
  2476. pindexNew->nStatus |= BLOCK_HAVE_DATA;
  2477. if (IsWitnessEnabled(pindexNew->pprev, consensusParams)) {
  2478. pindexNew->nStatus |= BLOCK_OPT_WITNESS;
  2479. }
  2480. pindexNew->RaiseValidity(BLOCK_VALID_TRANSACTIONS);
  2481. setDirtyBlockIndex.insert(pindexNew);
  2482. if (pindexNew->pprev == nullptr || pindexNew->pprev->nChainTx) {
  2483. // If pindexNew is the genesis block or all parents are BLOCK_VALID_TRANSACTIONS.
  2484. std::deque<CBlockIndex*> queue;
  2485. queue.push_back(pindexNew);
  2486. // Recursively process any descendant blocks that now may be eligible to be connected.
  2487. while (!queue.empty()) {
  2488. CBlockIndex *pindex = queue.front();
  2489. queue.pop_front();
  2490. pindex->nChainTx = (pindex->pprev ? pindex->pprev->nChainTx : 0) + pindex->nTx;
  2491. {
  2492. LOCK(cs_nBlockSequenceId);
  2493. pindex->nSequenceId = nBlockSequenceId++;
  2494. }
  2495. if (chainActive.Tip() == nullptr || !setBlockIndexCandidates.value_comp()(pindex, chainActive.Tip())) {
  2496. setBlockIndexCandidates.insert(pindex);
  2497. }
  2498. std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> range = mapBlocksUnlinked.equal_range(pindex);
  2499. while (range.first != range.second) {
  2500. std::multimap<CBlockIndex*, CBlockIndex*>::iterator it = range.first;
  2501. queue.push_back(it->second);
  2502. range.first++;
  2503. mapBlocksUnlinked.erase(it);
  2504. }
  2505. }
  2506. } else {
  2507. if (pindexNew->pprev && pindexNew->pprev->IsValid(BLOCK_VALID_TREE)) {
  2508. mapBlocksUnlinked.insert(std::make_pair(pindexNew->pprev, pindexNew));
  2509. }
  2510. }
  2511. return true;
  2512. }
  2513. static bool FindBlockPos(CDiskBlockPos &pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime, bool fKnown = false)
  2514. {
  2515. LOCK(cs_LastBlockFile);
  2516. unsigned int nFile = fKnown ? pos.nFile : nLastBlockFile;
  2517. if (vinfoBlockFile.size() <= nFile) {
  2518. vinfoBlockFile.resize(nFile + 1);
  2519. }
  2520. if (!fKnown) {
  2521. while (vinfoBlockFile[nFile].nSize + nAddSize >= MAX_BLOCKFILE_SIZE) {
  2522. nFile++;
  2523. if (vinfoBlockFile.size() <= nFile) {
  2524. vinfoBlockFile.resize(nFile + 1);
  2525. }
  2526. }
  2527. pos.nFile = nFile;
  2528. pos.nPos = vinfoBlockFile[nFile].nSize;
  2529. }
  2530. if ((int)nFile != nLastBlockFile) {
  2531. if (!fKnown) {
  2532. LogPrintf("Leaving block file %i: %s\n", nLastBlockFile, vinfoBlockFile[nLastBlockFile].ToString());
  2533. }
  2534. FlushBlockFile(!fKnown);
  2535. nLastBlockFile = nFile;
  2536. }
  2537. vinfoBlockFile[nFile].AddBlock(nHeight, nTime);
  2538. if (fKnown)
  2539. vinfoBlockFile[nFile].nSize = std::max(pos.nPos + nAddSize, vinfoBlockFile[nFile].nSize);
  2540. else
  2541. vinfoBlockFile[nFile].nSize += nAddSize;
  2542. if (!fKnown) {
  2543. unsigned int nOldChunks = (pos.nPos + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
  2544. unsigned int nNewChunks = (vinfoBlockFile[nFile].nSize + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
  2545. if (nNewChunks > nOldChunks) {
  2546. if (fPruneMode)
  2547. fCheckForPruning = true;
  2548. if (CheckDiskSpace(nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos)) {
  2549. FILE *file = OpenBlockFile(pos);
  2550. if (file) {
  2551. LogPrintf("Pre-allocating up to position 0x%x in blk%05u.dat\n", nNewChunks * BLOCKFILE_CHUNK_SIZE, pos.nFile);
  2552. AllocateFileRange(file, pos.nPos, nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos);
  2553. fclose(file);
  2554. }
  2555. }
  2556. else
  2557. return error("out of disk space");
  2558. }
  2559. }
  2560. setDirtyFileInfo.insert(nFile);
  2561. return true;
  2562. }
  2563. static bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize)
  2564. {
  2565. pos.nFile = nFile;
  2566. LOCK(cs_LastBlockFile);
  2567. unsigned int nNewSize;
  2568. pos.nPos = vinfoBlockFile[nFile].nUndoSize;
  2569. nNewSize = vinfoBlockFile[nFile].nUndoSize += nAddSize;
  2570. setDirtyFileInfo.insert(nFile);
  2571. unsigned int nOldChunks = (pos.nPos + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
  2572. unsigned int nNewChunks = (nNewSize + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
  2573. if (nNewChunks > nOldChunks) {
  2574. if (fPruneMode)
  2575. fCheckForPruning = true;
  2576. if (CheckDiskSpace(nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos)) {
  2577. FILE *file = OpenUndoFile(pos);
  2578. if (file) {
  2579. LogPrintf("Pre-allocating up to position 0x%x in rev%05u.dat\n", nNewChunks * UNDOFILE_CHUNK_SIZE, pos.nFile);
  2580. AllocateFileRange(file, pos.nPos, nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos);
  2581. fclose(file);
  2582. }
  2583. }
  2584. else
  2585. return state.Error("out of disk space");
  2586. }
  2587. return true;
  2588. }
  2589. static bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW = true)
  2590. {
  2591. // Check proof of work matches claimed amount
  2592. if (fCheckPOW && !CheckProofOfWork(block.GetHash(), block.nBits, consensusParams))
  2593. return state.DoS(50, false, REJECT_INVALID, "high-hash", false, "proof of work failed");
  2594. return true;
  2595. }
  2596. bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW, bool fCheckMerkleRoot)
  2597. {
  2598. // These are checks that are independent of context.
  2599. if (block.fChecked)
  2600. return true;
  2601. // Check that the header is valid (particularly PoW). This is mostly
  2602. // redundant with the call in AcceptBlockHeader.
  2603. if (!CheckBlockHeader(block, state, consensusParams, fCheckPOW))
  2604. return false;
  2605. // Check the merkle root.
  2606. if (fCheckMerkleRoot) {
  2607. bool mutated;
  2608. uint256 hashMerkleRoot2 = BlockMerkleRoot(block, &mutated);
  2609. if (block.hashMerkleRoot != hashMerkleRoot2)
  2610. return state.DoS(100, false, REJECT_INVALID, "bad-txnmrklroot", true, "hashMerkleRoot mismatch");
  2611. // Check for merkle tree malleability (CVE-2012-2459): repeating sequences
  2612. // of transactions in a block without affecting the merkle root of a block,
  2613. // while still invalidating it.
  2614. if (mutated)
  2615. return state.DoS(100, false, REJECT_INVALID, "bad-txns-duplicate", true, "duplicate transaction");
  2616. }
  2617. // All potential-corruption validation must be done before we do any
  2618. // transaction validation, as otherwise we may mark the header as invalid
  2619. // because we receive the wrong transactions for it.
  2620. // Note that witness malleability is checked in ContextualCheckBlock, so no
  2621. // checks that use witness data may be performed here.
  2622. // Size limits
  2623. 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)
  2624. return state.DoS(100, false, REJECT_INVALID, "bad-blk-length", false, "size limits failed");
  2625. // First transaction must be coinbase, the rest must not be
  2626. if (block.vtx.empty() || !block.vtx[0]->IsCoinBase())
  2627. return state.DoS(100, false, REJECT_INVALID, "bad-cb-missing", false, "first tx is not coinbase");
  2628. for (unsigned int i = 1; i < block.vtx.size(); i++)
  2629. if (block.vtx[i]->IsCoinBase())
  2630. return state.DoS(100, false, REJECT_INVALID, "bad-cb-multiple", false, "more than one coinbase");
  2631. // Check transactions
  2632. for (const auto& tx : block.vtx)
  2633. if (!CheckTransaction(*tx, state, false))
  2634. return state.Invalid(false, state.GetRejectCode(), state.GetRejectReason(),
  2635. strprintf("Transaction check failed (tx hash %s) %s", tx->GetHash().ToString(), state.GetDebugMessage()));
  2636. unsigned int nSigOps = 0;
  2637. for (const auto& tx : block.vtx)
  2638. {
  2639. nSigOps += GetLegacySigOpCount(*tx);
  2640. }
  2641. if (nSigOps * WITNESS_SCALE_FACTOR > MAX_BLOCK_SIGOPS_COST)
  2642. return state.DoS(100, false, REJECT_INVALID, "bad-blk-sigops", false, "out-of-bounds SigOpCount");
  2643. if (fCheckPOW && fCheckMerkleRoot)
  2644. block.fChecked = true;
  2645. return true;
  2646. }
  2647. bool IsWitnessEnabled(const CBlockIndex* pindexPrev, const Consensus::Params& params)
  2648. {
  2649. LOCK(cs_main);
  2650. return (VersionBitsState(pindexPrev, params, Consensus::DEPLOYMENT_SEGWIT, versionbitscache) == THRESHOLD_ACTIVE);
  2651. }
  2652. // Compute at which vout of the block's coinbase transaction the witness
  2653. // commitment occurs, or -1 if not found.
  2654. static int GetWitnessCommitmentIndex(const CBlock& block)
  2655. {
  2656. int commitpos = -1;
  2657. if (!block.vtx.empty()) {
  2658. for (size_t o = 0; o < block.vtx[0]->vout.size(); o++) {
  2659. 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) {
  2660. commitpos = o;
  2661. }
  2662. }
  2663. }
  2664. return commitpos;
  2665. }
  2666. void UpdateUncommittedBlockStructures(CBlock& block, const CBlockIndex* pindexPrev, const Consensus::Params& consensusParams)
  2667. {
  2668. int commitpos = GetWitnessCommitmentIndex(block);
  2669. static const std::vector<unsigned char> nonce(32, 0x00);
  2670. if (commitpos != -1 && IsWitnessEnabled(pindexPrev, consensusParams) && !block.vtx[0]->HasWitness()) {
  2671. CMutableTransaction tx(*block.vtx[0]);
  2672. tx.vin[0].scriptWitness.stack.resize(1);
  2673. tx.vin[0].scriptWitness.stack[0] = nonce;
  2674. block.vtx[0] = MakeTransactionRef(std::move(tx));
  2675. }
  2676. }
  2677. std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBlockIndex* pindexPrev, const Consensus::Params& consensusParams)
  2678. {
  2679. std::vector<unsigned char> commitment;
  2680. int commitpos = GetWitnessCommitmentIndex(block);
  2681. std::vector<unsigned char> ret(32, 0x00);
  2682. if (consensusParams.vDeployments[Consensus::DEPLOYMENT_SEGWIT].nTimeout != 0) {
  2683. if (commitpos == -1) {
  2684. uint256 witnessroot = BlockWitnessMerkleRoot(block, nullptr);
  2685. CHash256().Write(witnessroot.begin(), 32).Write(ret.data(), 32).Finalize(witnessroot.begin());
  2686. CTxOut out;
  2687. out.nValue = 0;
  2688. out.scriptPubKey.resize(38);
  2689. out.scriptPubKey[0] = OP_RETURN;
  2690. out.scriptPubKey[1] = 0x24;
  2691. out.scriptPubKey[2] = 0xaa;
  2692. out.scriptPubKey[3] = 0x21;
  2693. out.scriptPubKey[4] = 0xa9;
  2694. out.scriptPubKey[5] = 0xed;
  2695. memcpy(&out.scriptPubKey[6], witnessroot.begin(), 32);
  2696. commitment = std::vector<unsigned char>(out.scriptPubKey.begin(), out.scriptPubKey.end());
  2697. CMutableTransaction tx(*block.vtx[0]);
  2698. tx.vout.push_back(out);
  2699. block.vtx[0] = MakeTransactionRef(std::move(tx));
  2700. }
  2701. }
  2702. UpdateUncommittedBlockStructures(block, pindexPrev, consensusParams);
  2703. return commitment;
  2704. }
  2705. /** Context-dependent validity checks.
  2706. * By "context", we mean only the previous block headers, but not the UTXO
  2707. * set; UTXO-related validity checks are done in ConnectBlock().
  2708. * NOTE: This function is not currently invoked by ConnectBlock(), so we
  2709. * should consider upgrade issues if we change which consensus rules are
  2710. * enforced in this function (eg by adding a new consensus rule). See comment
  2711. * in ConnectBlock().
  2712. * Note that -reindex-chainstate skips the validation that happens here!
  2713. */
  2714. static bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, const CChainParams& params, const CBlockIndex* pindexPrev, int64_t nAdjustedTime)
  2715. {
  2716. assert(pindexPrev != nullptr);
  2717. const int nHeight = pindexPrev->nHeight + 1;
  2718. // Check proof of work
  2719. const Consensus::Params& consensusParams = params.GetConsensus();
  2720. if (block.nBits != GetNextWorkRequired(pindexPrev, &block, consensusParams))
  2721. return state.DoS(100, false, REJECT_INVALID, "bad-diffbits", false, "incorrect proof of work");
  2722. // Check against checkpoints
  2723. if (fCheckpointsEnabled) {
  2724. // Don't accept any forks from the main chain prior to last checkpoint.
  2725. // GetLastCheckpoint finds the last checkpoint in MapCheckpoints that's in our
  2726. // MapBlockIndex.
  2727. CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(params.Checkpoints());
  2728. if (pcheckpoint && nHeight < pcheckpoint->nHeight)
  2729. return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d)", __func__, nHeight), REJECT_CHECKPOINT, "bad-fork-prior-to-checkpoint");
  2730. }
  2731. // Check timestamp against prev
  2732. if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast())
  2733. return state.Invalid(false, REJECT_INVALID, "time-too-old", "block's timestamp is too early");
  2734. // Check timestamp
  2735. if (block.GetBlockTime() > nAdjustedTime + MAX_FUTURE_BLOCK_TIME)
  2736. return state.Invalid(false, REJECT_INVALID, "time-too-new", "block timestamp too far in the future");
  2737. // Reject outdated version blocks when 95% (75% on testnet) of the network has upgraded:
  2738. // check for version 2, 3 and 4 upgrades
  2739. if((block.nVersion < 2 && nHeight >= consensusParams.BIP34Height) ||
  2740. (block.nVersion < 3 && nHeight >= consensusParams.BIP66Height) ||
  2741. (block.nVersion < 4 && nHeight >= consensusParams.BIP65Height))
  2742. return state.Invalid(false, REJECT_OBSOLETE, strprintf("bad-version(0x%08x)", block.nVersion),
  2743. strprintf("rejected nVersion=0x%08x block", block.nVersion));
  2744. return true;
  2745. }
  2746. /** NOTE: This function is not currently invoked by ConnectBlock(), so we
  2747. * should consider upgrade issues if we change which consensus rules are
  2748. * enforced in this function (eg by adding a new consensus rule). See comment
  2749. * in ConnectBlock().
  2750. * Note that -reindex-chainstate skips the validation that happens here!
  2751. */
  2752. static bool ContextualCheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
  2753. {
  2754. const int nHeight = pindexPrev == nullptr ? 0 : pindexPrev->nHeight + 1;
  2755. // Start enforcing BIP113 (Median Time Past) using versionbits logic.
  2756. int nLockTimeFlags = 0;
  2757. if (VersionBitsState(pindexPrev, consensusParams, Consensus::DEPLOYMENT_CSV, versionbitscache) == THRESHOLD_ACTIVE) {
  2758. nLockTimeFlags |= LOCKTIME_MEDIAN_TIME_PAST;
  2759. }
  2760. int64_t nLockTimeCutoff = (nLockTimeFlags & LOCKTIME_MEDIAN_TIME_PAST)
  2761. ? pindexPrev->GetMedianTimePast()
  2762. : block.GetBlockTime();
  2763. // Check that all transactions are finalized
  2764. for (const auto& tx : block.vtx) {
  2765. if (!IsFinalTx(*tx, nHeight, nLockTimeCutoff)) {
  2766. return state.DoS(10, false, REJECT_INVALID, "bad-txns-nonfinal", false, "non-final transaction");
  2767. }
  2768. }
  2769. // Enforce rule that the coinbase starts with serialized block height
  2770. if (nHeight >= consensusParams.BIP34Height)
  2771. {
  2772. CScript expect = CScript() << nHeight;
  2773. if (block.vtx[0]->vin[0].scriptSig.size() < expect.size() ||
  2774. !std::equal(expect.begin(), expect.end(), block.vtx[0]->vin[0].scriptSig.begin())) {
  2775. return state.DoS(100, false, REJECT_INVALID, "bad-cb-height", false, "block height mismatch in coinbase");
  2776. }
  2777. }
  2778. // Validation for witness commitments.
  2779. // * We compute the witness hash (which is the hash including witnesses) of all the block's transactions, except the
  2780. // coinbase (where 0x0000....0000 is used instead).
  2781. // * The coinbase scriptWitness is a stack of a single 32-byte vector, containing a witness nonce (unconstrained).
  2782. // * We build a merkle tree with all those witness hashes as leaves (similar to the hashMerkleRoot in the block header).
  2783. // * There must be at least one output whose scriptPubKey is a single 36-byte push, the first 4 bytes of which are
  2784. // {0xaa, 0x21, 0xa9, 0xed}, and the following 32 bytes are SHA256^2(witness root, witness nonce). In case there are
  2785. // multiple, the last one is used.
  2786. bool fHaveWitness = false;
  2787. if (VersionBitsState(pindexPrev, consensusParams, Consensus::DEPLOYMENT_SEGWIT, versionbitscache) == THRESHOLD_ACTIVE) {
  2788. int commitpos = GetWitnessCommitmentIndex(block);
  2789. if (commitpos != -1) {
  2790. bool malleated = false;
  2791. uint256 hashWitness = BlockWitnessMerkleRoot(block, &malleated);
  2792. // The malleation check is ignored; as the transaction tree itself
  2793. // already does not permit it, it is impossible to trigger in the
  2794. // witness tree.
  2795. if (block.vtx[0]->vin[0].scriptWitness.stack.size() != 1 || block.vtx[0]->vin[0].scriptWitness.stack[0].size() != 32) {
  2796. return state.DoS(100, false, REJECT_INVALID, "bad-witness-nonce-size", true, strprintf("%s : invalid witness nonce size", __func__));
  2797. }
  2798. CHash256().Write(hashWitness.begin(), 32).Write(&block.vtx[0]->vin[0].scriptWitness.stack[0][0], 32).Finalize(hashWitness.begin());
  2799. if (memcmp(hashWitness.begin(), &block.vtx[0]->vout[commitpos].scriptPubKey[6], 32)) {
  2800. return state.DoS(100, false, REJECT_INVALID, "bad-witness-merkle-match", true, strprintf("%s : witness merkle commitment mismatch", __func__));
  2801. }
  2802. fHaveWitness = true;
  2803. }
  2804. }
  2805. // No witness data is allowed in blocks that don't commit to witness data, as this would otherwise leave room for spam
  2806. if (!fHaveWitness) {
  2807. for (const auto& tx : block.vtx) {
  2808. if (tx->HasWitness()) {
  2809. return state.DoS(100, false, REJECT_INVALID, "unexpected-witness", true, strprintf("%s : unexpected witness data found", __func__));
  2810. }
  2811. }
  2812. }
  2813. // After the coinbase witness nonce and commitment are verified,
  2814. // we can check if the block weight passes (before we've checked the
  2815. // coinbase witness, it would be possible for the weight to be too
  2816. // large by filling up the coinbase witness, which doesn't change
  2817. // the block hash, so we couldn't mark the block as permanently
  2818. // failed).
  2819. if (GetBlockWeight(block) > MAX_BLOCK_WEIGHT) {
  2820. return state.DoS(100, false, REJECT_INVALID, "bad-blk-weight", false, strprintf("%s : weight limit failed", __func__));
  2821. }
  2822. return true;
  2823. }
  2824. bool CChainState::AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex)
  2825. {
  2826. AssertLockHeld(cs_main);
  2827. // Check for duplicate
  2828. uint256 hash = block.GetHash();
  2829. BlockMap::iterator miSelf = mapBlockIndex.find(hash);
  2830. CBlockIndex *pindex = nullptr;
  2831. if (hash != chainparams.GetConsensus().hashGenesisBlock) {
  2832. if (miSelf != mapBlockIndex.end()) {
  2833. // Block header is already known.
  2834. pindex = miSelf->second;
  2835. if (ppindex)
  2836. *ppindex = pindex;
  2837. if (pindex->nStatus & BLOCK_FAILED_MASK)
  2838. return state.Invalid(error("%s: block %s is marked invalid", __func__, hash.ToString()), 0, "duplicate");
  2839. return true;
  2840. }
  2841. if (!CheckBlockHeader(block, state, chainparams.GetConsensus()))
  2842. return error("%s: Consensus::CheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));
  2843. // Get prev block index
  2844. CBlockIndex* pindexPrev = nullptr;
  2845. BlockMap::iterator mi = mapBlockIndex.find(block.hashPrevBlock);
  2846. if (mi == mapBlockIndex.end())
  2847. return state.DoS(10, error("%s: prev block not found", __func__), 0, "prev-blk-not-found");
  2848. pindexPrev = (*mi).second;
  2849. if (pindexPrev->nStatus & BLOCK_FAILED_MASK)
  2850. return state.DoS(100, error("%s: prev block invalid", __func__), REJECT_INVALID, "bad-prevblk");
  2851. if (!ContextualCheckBlockHeader(block, state, chainparams, pindexPrev, GetAdjustedTime()))
  2852. return error("%s: Consensus::ContextualCheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));
  2853. if (!pindexPrev->IsValid(BLOCK_VALID_SCRIPTS)) {
  2854. for (const CBlockIndex* failedit : g_failed_blocks) {
  2855. if (pindexPrev->GetAncestor(failedit->nHeight) == failedit) {
  2856. assert(failedit->nStatus & BLOCK_FAILED_VALID);
  2857. CBlockIndex* invalid_walk = pindexPrev;
  2858. while (invalid_walk != failedit) {
  2859. invalid_walk->nStatus |= BLOCK_FAILED_CHILD;
  2860. setDirtyBlockIndex.insert(invalid_walk);
  2861. invalid_walk = invalid_walk->pprev;
  2862. }
  2863. return state.DoS(100, error("%s: prev block invalid", __func__), REJECT_INVALID, "bad-prevblk");
  2864. }
  2865. }
  2866. }
  2867. }
  2868. if (pindex == nullptr)
  2869. pindex = AddToBlockIndex(block);
  2870. if (ppindex)
  2871. *ppindex = pindex;
  2872. CheckBlockIndex(chainparams.GetConsensus());
  2873. return true;
  2874. }
  2875. // Exposed wrapper for AcceptBlockHeader
  2876. bool ProcessNewBlockHeaders(const std::vector<CBlockHeader>& headers, CValidationState& state, const CChainParams& chainparams, const CBlockIndex** ppindex, CBlockHeader *first_invalid)
  2877. {
  2878. if (first_invalid != nullptr) first_invalid->SetNull();
  2879. {
  2880. LOCK(cs_main);
  2881. for (const CBlockHeader& header : headers) {
  2882. CBlockIndex *pindex = nullptr; // Use a temp pindex instead of ppindex to avoid a const_cast
  2883. if (!g_chainstate.AcceptBlockHeader(header, state, chainparams, &pindex)) {
  2884. if (first_invalid) *first_invalid = header;
  2885. return false;
  2886. }
  2887. if (ppindex) {
  2888. *ppindex = pindex;
  2889. }
  2890. }
  2891. }
  2892. NotifyHeaderTip();
  2893. return true;
  2894. }
  2895. /** Store block on disk. If dbp is non-nullptr, the file is known to already reside on disk */
  2896. static CDiskBlockPos SaveBlockToDisk(const CBlock& block, int nHeight, const CChainParams& chainparams, const CDiskBlockPos* dbp) {
  2897. unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
  2898. CDiskBlockPos blockPos;
  2899. if (dbp != nullptr)
  2900. blockPos = *dbp;
  2901. if (!FindBlockPos(blockPos, nBlockSize+8, nHeight, block.GetBlockTime(), dbp != nullptr)) {
  2902. error("%s: FindBlockPos failed", __func__);
  2903. return CDiskBlockPos();
  2904. }
  2905. if (dbp == nullptr) {
  2906. if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart())) {
  2907. AbortNode("Failed to write block");
  2908. return CDiskBlockPos();
  2909. }
  2910. }
  2911. return blockPos;
  2912. }
  2913. /** Store block on disk. If dbp is non-nullptr, the file is known to already reside on disk */
  2914. bool CChainState::AcceptBlock(const std::shared_ptr<const CBlock>& pblock, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex, bool fRequested, const CDiskBlockPos* dbp, bool* fNewBlock)
  2915. {
  2916. const CBlock& block = *pblock;
  2917. if (fNewBlock) *fNewBlock = false;
  2918. AssertLockHeld(cs_main);
  2919. CBlockIndex *pindexDummy = nullptr;
  2920. CBlockIndex *&pindex = ppindex ? *ppindex : pindexDummy;
  2921. if (!AcceptBlockHeader(block, state, chainparams, &pindex))
  2922. return false;
  2923. // Try to process all requested blocks that we don't have, but only
  2924. // process an unrequested block if it's new and has enough work to
  2925. // advance our tip, and isn't too many blocks ahead.
  2926. bool fAlreadyHave = pindex->nStatus & BLOCK_HAVE_DATA;
  2927. bool fHasMoreOrSameWork = (chainActive.Tip() ? pindex->nChainWork >= chainActive.Tip()->nChainWork : true);
  2928. // Blocks that are too out-of-order needlessly limit the effectiveness of
  2929. // pruning, because pruning will not delete block files that contain any
  2930. // blocks which are too close in height to the tip. Apply this test
  2931. // regardless of whether pruning is enabled; it should generally be safe to
  2932. // not process unrequested blocks.
  2933. bool fTooFarAhead = (pindex->nHeight > int(chainActive.Height() + MIN_BLOCKS_TO_KEEP));
  2934. // TODO: Decouple this function from the block download logic by removing fRequested
  2935. // This requires some new chain data structure to efficiently look up if a
  2936. // block is in a chain leading to a candidate for best tip, despite not
  2937. // being such a candidate itself.
  2938. // TODO: deal better with return value and error conditions for duplicate
  2939. // and unrequested blocks.
  2940. if (fAlreadyHave) return true;
  2941. if (!fRequested) { // If we didn't ask for it:
  2942. if (pindex->nTx != 0) return true; // This is a previously-processed block that was pruned
  2943. if (!fHasMoreOrSameWork) return true; // Don't process less-work chains
  2944. if (fTooFarAhead) return true; // Block height is too high
  2945. // Protect against DoS attacks from low-work chains.
  2946. // If our tip is behind, a peer could try to send us
  2947. // low-work blocks on a fake chain that we would never
  2948. // request; don't process these.
  2949. if (pindex->nChainWork < nMinimumChainWork) return true;
  2950. }
  2951. if (fNewBlock) *fNewBlock = true;
  2952. if (!CheckBlock(block, state, chainparams.GetConsensus()) ||
  2953. !ContextualCheckBlock(block, state, chainparams.GetConsensus(), pindex->pprev)) {
  2954. if (state.IsInvalid() && !state.CorruptionPossible()) {
  2955. pindex->nStatus |= BLOCK_FAILED_VALID;
  2956. setDirtyBlockIndex.insert(pindex);
  2957. }
  2958. return error("%s: %s", __func__, FormatStateMessage(state));
  2959. }
  2960. // Header is valid/has work, merkle tree and segwit merkle tree are good...RELAY NOW
  2961. // (but if it does not build on our best tip, let the SendMessages loop relay it)
  2962. if (!IsInitialBlockDownload() && chainActive.Tip() == pindex->pprev)
  2963. GetMainSignals().NewPoWValidBlock(pindex, pblock);
  2964. // Write block to history file
  2965. try {
  2966. CDiskBlockPos blockPos = SaveBlockToDisk(block, pindex->nHeight, chainparams, dbp);
  2967. if (blockPos.IsNull()) {
  2968. state.Error(strprintf("%s: Failed to find position to write new block to disk", __func__));
  2969. return false;
  2970. }
  2971. if (!ReceivedBlockTransactions(block, state, pindex, blockPos, chainparams.GetConsensus()))
  2972. return error("AcceptBlock(): ReceivedBlockTransactions failed");
  2973. } catch (const std::runtime_error& e) {
  2974. return AbortNode(state, std::string("System error: ") + e.what());
  2975. }
  2976. if (fCheckForPruning)
  2977. FlushStateToDisk(chainparams, state, FLUSH_STATE_NONE); // we just allocated more disk space for block files
  2978. CheckBlockIndex(chainparams.GetConsensus());
  2979. return true;
  2980. }
  2981. bool ProcessNewBlock(const CChainParams& chainparams, const std::shared_ptr<const CBlock> pblock, bool fForceProcessing, bool *fNewBlock)
  2982. {
  2983. AssertLockNotHeld(cs_main);
  2984. {
  2985. CBlockIndex *pindex = nullptr;
  2986. if (fNewBlock) *fNewBlock = false;
  2987. CValidationState state;
  2988. // Ensure that CheckBlock() passes before calling AcceptBlock, as
  2989. // belt-and-suspenders.
  2990. bool ret = CheckBlock(*pblock, state, chainparams.GetConsensus());
  2991. LOCK(cs_main);
  2992. if (ret) {
  2993. // Store to disk
  2994. ret = g_chainstate.AcceptBlock(pblock, state, chainparams, &pindex, fForceProcessing, nullptr, fNewBlock);
  2995. }
  2996. if (!ret) {
  2997. GetMainSignals().BlockChecked(*pblock, state);
  2998. return error("%s: AcceptBlock FAILED (%s)", __func__, state.GetDebugMessage());
  2999. }
  3000. }
  3001. NotifyHeaderTip();
  3002. CValidationState state; // Only used to report errors, not invalidity - ignore it
  3003. if (!g_chainstate.ActivateBestChain(state, chainparams, pblock))
  3004. return error("%s: ActivateBestChain failed", __func__);
  3005. return true;
  3006. }
  3007. bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams, const CBlock& block, CBlockIndex* pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot)
  3008. {
  3009. AssertLockHeld(cs_main);
  3010. assert(pindexPrev && pindexPrev == chainActive.Tip());
  3011. CCoinsViewCache viewNew(pcoinsTip.get());
  3012. CBlockIndex indexDummy(block);
  3013. indexDummy.pprev = pindexPrev;
  3014. indexDummy.nHeight = pindexPrev->nHeight + 1;
  3015. // NOTE: CheckBlockHeader is called by CheckBlock
  3016. if (!ContextualCheckBlockHeader(block, state, chainparams, pindexPrev, GetAdjustedTime()))
  3017. return error("%s: Consensus::ContextualCheckBlockHeader: %s", __func__, FormatStateMessage(state));
  3018. if (!CheckBlock(block, state, chainparams.GetConsensus(), fCheckPOW, fCheckMerkleRoot))
  3019. return error("%s: Consensus::CheckBlock: %s", __func__, FormatStateMessage(state));
  3020. if (!ContextualCheckBlock(block, state, chainparams.GetConsensus(), pindexPrev))
  3021. return error("%s: Consensus::ContextualCheckBlock: %s", __func__, FormatStateMessage(state));
  3022. if (!g_chainstate.ConnectBlock(block, state, &indexDummy, viewNew, chainparams, true))
  3023. return false;
  3024. assert(state.IsValid());
  3025. return true;
  3026. }
  3027. /**
  3028. * BLOCK PRUNING CODE
  3029. */
  3030. /* Calculate the amount of disk space the block & undo files currently use */
  3031. uint64_t CalculateCurrentUsage()
  3032. {
  3033. LOCK(cs_LastBlockFile);
  3034. uint64_t retval = 0;
  3035. for (const CBlockFileInfo &file : vinfoBlockFile) {
  3036. retval += file.nSize + file.nUndoSize;
  3037. }
  3038. return retval;
  3039. }
  3040. /* Prune a block file (modify associated database entries)*/
  3041. void PruneOneBlockFile(const int fileNumber)
  3042. {
  3043. LOCK(cs_LastBlockFile);
  3044. for (const auto& entry : mapBlockIndex) {
  3045. CBlockIndex* pindex = entry.second;
  3046. if (pindex->nFile == fileNumber) {
  3047. pindex->nStatus &= ~BLOCK_HAVE_DATA;
  3048. pindex->nStatus &= ~BLOCK_HAVE_UNDO;
  3049. pindex->nFile = 0;
  3050. pindex->nDataPos = 0;
  3051. pindex->nUndoPos = 0;
  3052. setDirtyBlockIndex.insert(pindex);
  3053. // Prune from mapBlocksUnlinked -- any block we prune would have
  3054. // to be downloaded again in order to consider its chain, at which
  3055. // point it would be considered as a candidate for
  3056. // mapBlocksUnlinked or setBlockIndexCandidates.
  3057. std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> range = mapBlocksUnlinked.equal_range(pindex->pprev);
  3058. while (range.first != range.second) {
  3059. std::multimap<CBlockIndex *, CBlockIndex *>::iterator _it = range.first;
  3060. range.first++;
  3061. if (_it->second == pindex) {
  3062. mapBlocksUnlinked.erase(_it);
  3063. }
  3064. }
  3065. }
  3066. }
  3067. vinfoBlockFile[fileNumber].SetNull();
  3068. setDirtyFileInfo.insert(fileNumber);
  3069. }
  3070. void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune)
  3071. {
  3072. for (std::set<int>::iterator it = setFilesToPrune.begin(); it != setFilesToPrune.end(); ++it) {
  3073. CDiskBlockPos pos(*it, 0);
  3074. fs::remove(GetBlockPosFilename(pos, "blk"));
  3075. fs::remove(GetBlockPosFilename(pos, "rev"));
  3076. LogPrintf("Prune: %s deleted blk/rev (%05u)\n", __func__, *it);
  3077. }
  3078. }
  3079. /* Calculate the block/rev files to delete based on height specified by user with RPC command pruneblockchain */
  3080. static void FindFilesToPruneManual(std::set<int>& setFilesToPrune, int nManualPruneHeight)
  3081. {
  3082. assert(fPruneMode && nManualPruneHeight > 0);
  3083. LOCK2(cs_main, cs_LastBlockFile);
  3084. if (chainActive.Tip() == nullptr)
  3085. return;
  3086. // last block to prune is the lesser of (user-specified height, MIN_BLOCKS_TO_KEEP from the tip)
  3087. unsigned int nLastBlockWeCanPrune = std::min((unsigned)nManualPruneHeight, chainActive.Tip()->nHeight - MIN_BLOCKS_TO_KEEP);
  3088. int count=0;
  3089. for (int fileNumber = 0; fileNumber < nLastBlockFile; fileNumber++) {
  3090. if (vinfoBlockFile[fileNumber].nSize == 0 || vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune)
  3091. continue;
  3092. PruneOneBlockFile(fileNumber);
  3093. setFilesToPrune.insert(fileNumber);
  3094. count++;
  3095. }
  3096. LogPrintf("Prune (Manual): prune_height=%d removed %d blk/rev pairs\n", nLastBlockWeCanPrune, count);
  3097. }
  3098. /* This function is called from the RPC code for pruneblockchain */
  3099. void PruneBlockFilesManual(int nManualPruneHeight)
  3100. {
  3101. CValidationState state;
  3102. const CChainParams& chainparams = Params();
  3103. FlushStateToDisk(chainparams, state, FLUSH_STATE_NONE, nManualPruneHeight);
  3104. }
  3105. /**
  3106. * Prune block and undo files (blk???.dat and undo???.dat) so that the disk space used is less than a user-defined target.
  3107. * The user sets the target (in MB) on the command line or in config file. This will be run on startup and whenever new
  3108. * space is allocated in a block or undo file, staying below the target. Changing back to unpruned requires a reindex
  3109. * (which in this case means the blockchain must be re-downloaded.)
  3110. *
  3111. * Pruning functions are called from FlushStateToDisk when the global fCheckForPruning flag has been set.
  3112. * Block and undo files are deleted in lock-step (when blk00003.dat is deleted, so is rev00003.dat.)
  3113. * Pruning cannot take place until the longest chain is at least a certain length (100000 on mainnet, 1000 on testnet, 1000 on regtest).
  3114. * Pruning will never delete a block within a defined distance (currently 288) from the active chain's tip.
  3115. * The block index is updated by unsetting HAVE_DATA and HAVE_UNDO for any blocks that were stored in the deleted files.
  3116. * A db flag records the fact that at least some block files have been pruned.
  3117. *
  3118. * @param[out] setFilesToPrune The set of file indices that can be unlinked will be returned
  3119. */
  3120. static void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight)
  3121. {
  3122. LOCK2(cs_main, cs_LastBlockFile);
  3123. if (chainActive.Tip() == nullptr || nPruneTarget == 0) {
  3124. return;
  3125. }
  3126. if ((uint64_t)chainActive.Tip()->nHeight <= nPruneAfterHeight) {
  3127. return;
  3128. }
  3129. unsigned int nLastBlockWeCanPrune = chainActive.Tip()->nHeight - MIN_BLOCKS_TO_KEEP;
  3130. uint64_t nCurrentUsage = CalculateCurrentUsage();
  3131. // We don't check to prune until after we've allocated new space for files
  3132. // So we should leave a buffer under our target to account for another allocation
  3133. // before the next pruning.
  3134. uint64_t nBuffer = BLOCKFILE_CHUNK_SIZE + UNDOFILE_CHUNK_SIZE;
  3135. uint64_t nBytesToPrune;
  3136. int count=0;
  3137. if (nCurrentUsage + nBuffer >= nPruneTarget) {
  3138. for (int fileNumber = 0; fileNumber < nLastBlockFile; fileNumber++) {
  3139. nBytesToPrune = vinfoBlockFile[fileNumber].nSize + vinfoBlockFile[fileNumber].nUndoSize;
  3140. if (vinfoBlockFile[fileNumber].nSize == 0)
  3141. continue;
  3142. if (nCurrentUsage + nBuffer < nPruneTarget) // are we below our target?
  3143. break;
  3144. // don't prune files that could have a block within MIN_BLOCKS_TO_KEEP of the main chain's tip but keep scanning
  3145. if (vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune)
  3146. continue;
  3147. PruneOneBlockFile(fileNumber);
  3148. // Queue up the files for removal
  3149. setFilesToPrune.insert(fileNumber);
  3150. nCurrentUsage -= nBytesToPrune;
  3151. count++;
  3152. }
  3153. }
  3154. LogPrint(BCLog::PRUNE, "Prune: target=%dMiB actual=%dMiB diff=%dMiB max_prune_height=%d removed %d blk/rev pairs\n",
  3155. nPruneTarget/1024/1024, nCurrentUsage/1024/1024,
  3156. ((int64_t)nPruneTarget - (int64_t)nCurrentUsage)/1024/1024,
  3157. nLastBlockWeCanPrune, count);
  3158. }
  3159. bool CheckDiskSpace(uint64_t nAdditionalBytes)
  3160. {
  3161. uint64_t nFreeBytesAvailable = fs::space(GetDataDir()).available;
  3162. // Check for nMinDiskSpace bytes (currently 50MB)
  3163. if (nFreeBytesAvailable < nMinDiskSpace + nAdditionalBytes)
  3164. return AbortNode("Disk space is low!", _("Error: Disk space is low!"));
  3165. return true;
  3166. }
  3167. static FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly)
  3168. {
  3169. if (pos.IsNull())
  3170. return nullptr;
  3171. fs::path path = GetBlockPosFilename(pos, prefix);
  3172. fs::create_directories(path.parent_path());
  3173. FILE* file = fsbridge::fopen(path, fReadOnly ? "rb": "rb+");
  3174. if (!file && !fReadOnly)
  3175. file = fsbridge::fopen(path, "wb+");
  3176. if (!file) {
  3177. LogPrintf("Unable to open file %s\n", path.string());
  3178. return nullptr;
  3179. }
  3180. if (pos.nPos) {
  3181. if (fseek(file, pos.nPos, SEEK_SET)) {
  3182. LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, path.string());
  3183. fclose(file);
  3184. return nullptr;
  3185. }
  3186. }
  3187. return file;
  3188. }
  3189. FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly) {
  3190. return OpenDiskFile(pos, "blk", fReadOnly);
  3191. }
  3192. /** Open an undo file (rev?????.dat) */
  3193. static FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly) {
  3194. return OpenDiskFile(pos, "rev", fReadOnly);
  3195. }
  3196. fs::path GetBlockPosFilename(const CDiskBlockPos &pos, const char *prefix)
  3197. {
  3198. return GetDataDir() / "blocks" / strprintf("%s%05u.dat", prefix, pos.nFile);
  3199. }
  3200. CBlockIndex * CChainState::InsertBlockIndex(const uint256& hash)
  3201. {
  3202. if (hash.IsNull())
  3203. return nullptr;
  3204. // Return existing
  3205. BlockMap::iterator mi = mapBlockIndex.find(hash);
  3206. if (mi != mapBlockIndex.end())
  3207. return (*mi).second;
  3208. // Create new
  3209. CBlockIndex* pindexNew = new CBlockIndex();
  3210. mi = mapBlockIndex.insert(std::make_pair(hash, pindexNew)).first;
  3211. pindexNew->phashBlock = &((*mi).first);
  3212. return pindexNew;
  3213. }
  3214. bool CChainState::LoadBlockIndex(const Consensus::Params& consensus_params, CBlockTreeDB& blocktree)
  3215. {
  3216. if (!blocktree.LoadBlockIndexGuts(consensus_params, [this](const uint256& hash){ return this->InsertBlockIndex(hash); }))
  3217. return false;
  3218. boost::this_thread::interruption_point();
  3219. // Calculate nChainWork
  3220. std::vector<std::pair<int, CBlockIndex*> > vSortedByHeight;
  3221. vSortedByHeight.reserve(mapBlockIndex.size());
  3222. for (const std::pair<uint256, CBlockIndex*>& item : mapBlockIndex)
  3223. {
  3224. CBlockIndex* pindex = item.second;
  3225. vSortedByHeight.push_back(std::make_pair(pindex->nHeight, pindex));
  3226. }
  3227. sort(vSortedByHeight.begin(), vSortedByHeight.end());
  3228. for (const std::pair<int, CBlockIndex*>& item : vSortedByHeight)
  3229. {
  3230. CBlockIndex* pindex = item.second;
  3231. pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + GetBlockProof(*pindex);
  3232. pindex->nTimeMax = (pindex->pprev ? std::max(pindex->pprev->nTimeMax, pindex->nTime) : pindex->nTime);
  3233. // We can link the chain of blocks for which we've received transactions at some point.
  3234. // Pruned nodes may have deleted the block.
  3235. if (pindex->nTx > 0) {
  3236. if (pindex->pprev) {
  3237. if (pindex->pprev->nChainTx) {
  3238. pindex->nChainTx = pindex->pprev->nChainTx + pindex->nTx;
  3239. } else {
  3240. pindex->nChainTx = 0;
  3241. mapBlocksUnlinked.insert(std::make_pair(pindex->pprev, pindex));
  3242. }
  3243. } else {
  3244. pindex->nChainTx = pindex->nTx;
  3245. }
  3246. }
  3247. if (!(pindex->nStatus & BLOCK_FAILED_MASK) && pindex->pprev && (pindex->pprev->nStatus & BLOCK_FAILED_MASK)) {
  3248. pindex->nStatus |= BLOCK_FAILED_CHILD;
  3249. setDirtyBlockIndex.insert(pindex);
  3250. }
  3251. if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && (pindex->nChainTx || pindex->pprev == nullptr))
  3252. setBlockIndexCandidates.insert(pindex);
  3253. if (pindex->nStatus & BLOCK_FAILED_MASK && (!pindexBestInvalid || pindex->nChainWork > pindexBestInvalid->nChainWork))
  3254. pindexBestInvalid = pindex;
  3255. if (pindex->pprev)
  3256. pindex->BuildSkip();
  3257. if (pindex->IsValid(BLOCK_VALID_TREE) && (pindexBestHeader == nullptr || CBlockIndexWorkComparator()(pindexBestHeader, pindex)))
  3258. pindexBestHeader = pindex;
  3259. }
  3260. return true;
  3261. }
  3262. bool static LoadBlockIndexDB(const CChainParams& chainparams)
  3263. {
  3264. if (!g_chainstate.LoadBlockIndex(chainparams.GetConsensus(), *pblocktree))
  3265. return false;
  3266. // Load block file info
  3267. pblocktree->ReadLastBlockFile(nLastBlockFile);
  3268. vinfoBlockFile.resize(nLastBlockFile + 1);
  3269. LogPrintf("%s: last block file = %i\n", __func__, nLastBlockFile);
  3270. for (int nFile = 0; nFile <= nLastBlockFile; nFile++) {
  3271. pblocktree->ReadBlockFileInfo(nFile, vinfoBlockFile[nFile]);
  3272. }
  3273. LogPrintf("%s: last block file info: %s\n", __func__, vinfoBlockFile[nLastBlockFile].ToString());
  3274. for (int nFile = nLastBlockFile + 1; true; nFile++) {
  3275. CBlockFileInfo info;
  3276. if (pblocktree->ReadBlockFileInfo(nFile, info)) {
  3277. vinfoBlockFile.push_back(info);
  3278. } else {
  3279. break;
  3280. }
  3281. }
  3282. // Check presence of blk files
  3283. LogPrintf("Checking all blk files are present...\n");
  3284. std::set<int> setBlkDataFiles;
  3285. for (const std::pair<uint256, CBlockIndex*>& item : mapBlockIndex)
  3286. {
  3287. CBlockIndex* pindex = item.second;
  3288. if (pindex->nStatus & BLOCK_HAVE_DATA) {
  3289. setBlkDataFiles.insert(pindex->nFile);
  3290. }
  3291. }
  3292. for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++)
  3293. {
  3294. CDiskBlockPos pos(*it, 0);
  3295. if (CAutoFile(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION).IsNull()) {
  3296. return false;
  3297. }
  3298. }
  3299. // Check whether we have ever pruned block & undo files
  3300. pblocktree->ReadFlag("prunedblockfiles", fHavePruned);
  3301. if (fHavePruned)
  3302. LogPrintf("LoadBlockIndexDB(): Block files have previously been pruned\n");
  3303. // Check whether we need to continue reindexing
  3304. bool fReindexing = false;
  3305. pblocktree->ReadReindexing(fReindexing);
  3306. if(fReindexing) fReindex = true;
  3307. // Check whether we have a transaction index
  3308. pblocktree->ReadFlag("txindex", fTxIndex);
  3309. LogPrintf("%s: transaction index %s\n", __func__, fTxIndex ? "enabled" : "disabled");
  3310. return true;
  3311. }
  3312. bool LoadChainTip(const CChainParams& chainparams)
  3313. {
  3314. if (chainActive.Tip() && chainActive.Tip()->GetBlockHash() == pcoinsTip->GetBestBlock()) return true;
  3315. if (pcoinsTip->GetBestBlock().IsNull() && mapBlockIndex.size() == 1) {
  3316. // In case we just added the genesis block, connect it now, so
  3317. // that we always have a chainActive.Tip() when we return.
  3318. LogPrintf("%s: Connecting genesis block...\n", __func__);
  3319. CValidationState state;
  3320. if (!ActivateBestChain(state, chainparams)) {
  3321. return false;
  3322. }
  3323. }
  3324. // Load pointer to end of best chain
  3325. BlockMap::iterator it = mapBlockIndex.find(pcoinsTip->GetBestBlock());
  3326. if (it == mapBlockIndex.end())
  3327. return false;
  3328. chainActive.SetTip(it->second);
  3329. g_chainstate.PruneBlockIndexCandidates();
  3330. LogPrintf("Loaded best chain: hashBestChain=%s height=%d date=%s progress=%f\n",
  3331. chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(),
  3332. DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
  3333. GuessVerificationProgress(chainparams.TxData(), chainActive.Tip()));
  3334. return true;
  3335. }
  3336. CVerifyDB::CVerifyDB()
  3337. {
  3338. uiInterface.ShowProgress(_("Verifying blocks..."), 0, false);
  3339. }
  3340. CVerifyDB::~CVerifyDB()
  3341. {
  3342. uiInterface.ShowProgress("", 100, false);
  3343. }
  3344. bool CVerifyDB::VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview, int nCheckLevel, int nCheckDepth)
  3345. {
  3346. LOCK(cs_main);
  3347. if (chainActive.Tip() == nullptr || chainActive.Tip()->pprev == nullptr)
  3348. return true;
  3349. // Verify blocks in the best chain
  3350. if (nCheckDepth <= 0 || nCheckDepth > chainActive.Height())
  3351. nCheckDepth = chainActive.Height();
  3352. nCheckLevel = std::max(0, std::min(4, nCheckLevel));
  3353. LogPrintf("Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel);
  3354. CCoinsViewCache coins(coinsview);
  3355. CBlockIndex* pindexState = chainActive.Tip();
  3356. CBlockIndex* pindexFailure = nullptr;
  3357. int nGoodTransactions = 0;
  3358. CValidationState state;
  3359. int reportDone = 0;
  3360. LogPrintf("[0%%]...");
  3361. for (CBlockIndex* pindex = chainActive.Tip(); pindex && pindex->pprev; pindex = pindex->pprev)
  3362. {
  3363. boost::this_thread::interruption_point();
  3364. int percentageDone = std::max(1, std::min(99, (int)(((double)(chainActive.Height() - pindex->nHeight)) / (double)nCheckDepth * (nCheckLevel >= 4 ? 50 : 100))));
  3365. if (reportDone < percentageDone/10) {
  3366. // report every 10% step
  3367. LogPrintf("[%d%%]...", percentageDone);
  3368. reportDone = percentageDone/10;
  3369. }
  3370. uiInterface.ShowProgress(_("Verifying blocks..."), percentageDone, false);
  3371. if (pindex->nHeight < chainActive.Height()-nCheckDepth)
  3372. break;
  3373. if (fPruneMode && !(pindex->nStatus & BLOCK_HAVE_DATA)) {
  3374. // If pruning, only go back as far as we have data.
  3375. LogPrintf("VerifyDB(): block verification stopping at height %d (pruning, no data)\n", pindex->nHeight);
  3376. break;
  3377. }
  3378. CBlock block;
  3379. // check level 0: read from disk
  3380. if (!ReadBlockFromDisk(block, pindex, chainparams.GetConsensus()))
  3381. return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
  3382. // check level 1: verify block validity
  3383. if (nCheckLevel >= 1 && !CheckBlock(block, state, chainparams.GetConsensus()))
  3384. return error("%s: *** found bad block at %d, hash=%s (%s)\n", __func__,
  3385. pindex->nHeight, pindex->GetBlockHash().ToString(), FormatStateMessage(state));
  3386. // check level 2: verify undo validity
  3387. if (nCheckLevel >= 2 && pindex) {
  3388. CBlockUndo undo;
  3389. if (!pindex->GetUndoPos().IsNull()) {
  3390. if (!UndoReadFromDisk(undo, pindex)) {
  3391. return error("VerifyDB(): *** found bad undo data at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
  3392. }
  3393. }
  3394. }
  3395. // check level 3: check for inconsistencies during memory-only disconnect of tip blocks
  3396. if (nCheckLevel >= 3 && pindex == pindexState && (coins.DynamicMemoryUsage() + pcoinsTip->DynamicMemoryUsage()) <= nCoinCacheUsage) {
  3397. assert(coins.GetBestBlock() == pindex->GetBlockHash());
  3398. DisconnectResult res = g_chainstate.DisconnectBlock(block, pindex, coins);
  3399. if (res == DISCONNECT_FAILED) {
  3400. return error("VerifyDB(): *** irrecoverable inconsistency in block data at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
  3401. }
  3402. pindexState = pindex->pprev;
  3403. if (res == DISCONNECT_UNCLEAN) {
  3404. nGoodTransactions = 0;
  3405. pindexFailure = pindex;
  3406. } else {
  3407. nGoodTransactions += block.vtx.size();
  3408. }
  3409. }
  3410. if (ShutdownRequested())
  3411. return true;
  3412. }
  3413. if (pindexFailure)
  3414. return error("VerifyDB(): *** coin database inconsistencies found (last %i blocks, %i good transactions before that)\n", chainActive.Height() - pindexFailure->nHeight + 1, nGoodTransactions);
  3415. // check level 4: try reconnecting blocks
  3416. if (nCheckLevel >= 4) {
  3417. CBlockIndex *pindex = pindexState;
  3418. while (pindex != chainActive.Tip()) {
  3419. boost::this_thread::interruption_point();
  3420. uiInterface.ShowProgress(_("Verifying blocks..."), std::max(1, std::min(99, 100 - (int)(((double)(chainActive.Height() - pindex->nHeight)) / (double)nCheckDepth * 50))), false);
  3421. pindex = chainActive.Next(pindex);
  3422. CBlock block;
  3423. if (!ReadBlockFromDisk(block, pindex, chainparams.GetConsensus()))
  3424. return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
  3425. if (!g_chainstate.ConnectBlock(block, state, pindex, coins, chainparams))
  3426. return error("VerifyDB(): *** found unconnectable block at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
  3427. }
  3428. }
  3429. LogPrintf("[DONE].\n");
  3430. LogPrintf("No coin database inconsistencies in last %i blocks (%i transactions)\n", chainActive.Height() - pindexState->nHeight, nGoodTransactions);
  3431. return true;
  3432. }
  3433. /** Apply the effects of a block on the utxo cache, ignoring that it may already have been applied. */
  3434. bool CChainState::RollforwardBlock(const CBlockIndex* pindex, CCoinsViewCache& inputs, const CChainParams& params)
  3435. {
  3436. // TODO: merge with ConnectBlock
  3437. CBlock block;
  3438. if (!ReadBlockFromDisk(block, pindex, params.GetConsensus())) {
  3439. return error("ReplayBlock(): ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
  3440. }
  3441. for (const CTransactionRef& tx : block.vtx) {
  3442. if (!tx->IsCoinBase()) {
  3443. for (const CTxIn &txin : tx->vin) {
  3444. inputs.SpendCoin(txin.prevout);
  3445. }
  3446. }
  3447. // Pass check = true as every addition may be an overwrite.
  3448. AddCoins(inputs, *tx, pindex->nHeight, true);
  3449. }
  3450. return true;
  3451. }
  3452. bool CChainState::ReplayBlocks(const CChainParams& params, CCoinsView* view)
  3453. {
  3454. LOCK(cs_main);
  3455. CCoinsViewCache cache(view);
  3456. std::vector<uint256> hashHeads = view->GetHeadBlocks();
  3457. if (hashHeads.empty()) return true; // We're already in a consistent state.
  3458. if (hashHeads.size() != 2) return error("ReplayBlocks(): unknown inconsistent state");
  3459. uiInterface.ShowProgress(_("Replaying blocks..."), 0, false);
  3460. LogPrintf("Replaying blocks\n");
  3461. const CBlockIndex* pindexOld = nullptr; // Old tip during the interrupted flush.
  3462. const CBlockIndex* pindexNew; // New tip during the interrupted flush.
  3463. const CBlockIndex* pindexFork = nullptr; // Latest block common to both the old and the new tip.
  3464. if (mapBlockIndex.count(hashHeads[0]) == 0) {
  3465. return error("ReplayBlocks(): reorganization to unknown block requested");
  3466. }
  3467. pindexNew = mapBlockIndex[hashHeads[0]];
  3468. if (!hashHeads[1].IsNull()) { // The old tip is allowed to be 0, indicating it's the first flush.
  3469. if (mapBlockIndex.count(hashHeads[1]) == 0) {
  3470. return error("ReplayBlocks(): reorganization from unknown block requested");
  3471. }
  3472. pindexOld = mapBlockIndex[hashHeads[1]];
  3473. pindexFork = LastCommonAncestor(pindexOld, pindexNew);
  3474. assert(pindexFork != nullptr);
  3475. }
  3476. // Rollback along the old branch.
  3477. while (pindexOld != pindexFork) {
  3478. if (pindexOld->nHeight > 0) { // Never disconnect the genesis block.
  3479. CBlock block;
  3480. if (!ReadBlockFromDisk(block, pindexOld, params.GetConsensus())) {
  3481. return error("RollbackBlock(): ReadBlockFromDisk() failed at %d, hash=%s", pindexOld->nHeight, pindexOld->GetBlockHash().ToString());
  3482. }
  3483. LogPrintf("Rolling back %s (%i)\n", pindexOld->GetBlockHash().ToString(), pindexOld->nHeight);
  3484. DisconnectResult res = DisconnectBlock(block, pindexOld, cache);
  3485. if (res == DISCONNECT_FAILED) {
  3486. return error("RollbackBlock(): DisconnectBlock failed at %d, hash=%s", pindexOld->nHeight, pindexOld->GetBlockHash().ToString());
  3487. }
  3488. // If DISCONNECT_UNCLEAN is returned, it means a non-existing UTXO was deleted, or an existing UTXO was
  3489. // overwritten. It corresponds to cases where the block-to-be-disconnect never had all its operations
  3490. // applied to the UTXO set. However, as both writing a UTXO and deleting a UTXO are idempotent operations,
  3491. // the result is still a version of the UTXO set with the effects of that block undone.
  3492. }
  3493. pindexOld = pindexOld->pprev;
  3494. }
  3495. // Roll forward from the forking point to the new tip.
  3496. int nForkHeight = pindexFork ? pindexFork->nHeight : 0;
  3497. for (int nHeight = nForkHeight + 1; nHeight <= pindexNew->nHeight; ++nHeight) {
  3498. const CBlockIndex* pindex = pindexNew->GetAncestor(nHeight);
  3499. LogPrintf("Rolling forward %s (%i)\n", pindex->GetBlockHash().ToString(), nHeight);
  3500. if (!RollforwardBlock(pindex, cache, params)) return false;
  3501. }
  3502. cache.SetBestBlock(pindexNew->GetBlockHash());
  3503. cache.Flush();
  3504. uiInterface.ShowProgress("", 100, false);
  3505. return true;
  3506. }
  3507. bool ReplayBlocks(const CChainParams& params, CCoinsView* view) {
  3508. return g_chainstate.ReplayBlocks(params, view);
  3509. }
  3510. bool CChainState::RewindBlockIndex(const CChainParams& params)
  3511. {
  3512. LOCK(cs_main);
  3513. // Note that during -reindex-chainstate we are called with an empty chainActive!
  3514. int nHeight = 1;
  3515. while (nHeight <= chainActive.Height()) {
  3516. if (IsWitnessEnabled(chainActive[nHeight - 1], params.GetConsensus()) && !(chainActive[nHeight]->nStatus & BLOCK_OPT_WITNESS)) {
  3517. break;
  3518. }
  3519. nHeight++;
  3520. }
  3521. // nHeight is now the height of the first insufficiently-validated block, or tipheight + 1
  3522. CValidationState state;
  3523. CBlockIndex* pindex = chainActive.Tip();
  3524. while (chainActive.Height() >= nHeight) {
  3525. if (fPruneMode && !(chainActive.Tip()->nStatus & BLOCK_HAVE_DATA)) {
  3526. // If pruning, don't try rewinding past the HAVE_DATA point;
  3527. // since older blocks can't be served anyway, there's
  3528. // no need to walk further, and trying to DisconnectTip()
  3529. // will fail (and require a needless reindex/redownload
  3530. // of the blockchain).
  3531. break;
  3532. }
  3533. if (!DisconnectTip(state, params, nullptr)) {
  3534. return error("RewindBlockIndex: unable to disconnect block at height %i", pindex->nHeight);
  3535. }
  3536. // Occasionally flush state to disk.
  3537. if (!FlushStateToDisk(params, state, FLUSH_STATE_PERIODIC))
  3538. return false;
  3539. }
  3540. // Reduce validity flag and have-data flags.
  3541. // We do this after actual disconnecting, otherwise we'll end up writing the lack of data
  3542. // to disk before writing the chainstate, resulting in a failure to continue if interrupted.
  3543. for (const auto& entry : mapBlockIndex) {
  3544. CBlockIndex* pindexIter = entry.second;
  3545. // Note: If we encounter an insufficiently validated block that
  3546. // is on chainActive, it must be because we are a pruning node, and
  3547. // this block or some successor doesn't HAVE_DATA, so we were unable to
  3548. // rewind all the way. Blocks remaining on chainActive at this point
  3549. // must not have their validity reduced.
  3550. if (IsWitnessEnabled(pindexIter->pprev, params.GetConsensus()) && !(pindexIter->nStatus & BLOCK_OPT_WITNESS) && !chainActive.Contains(pindexIter)) {
  3551. // Reduce validity
  3552. pindexIter->nStatus = std::min<unsigned int>(pindexIter->nStatus & BLOCK_VALID_MASK, BLOCK_VALID_TREE) | (pindexIter->nStatus & ~BLOCK_VALID_MASK);
  3553. // Remove have-data flags.
  3554. pindexIter->nStatus &= ~(BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO);
  3555. // Remove storage location.
  3556. pindexIter->nFile = 0;
  3557. pindexIter->nDataPos = 0;
  3558. pindexIter->nUndoPos = 0;
  3559. // Remove various other things
  3560. pindexIter->nTx = 0;
  3561. pindexIter->nChainTx = 0;
  3562. pindexIter->nSequenceId = 0;
  3563. // Make sure it gets written.
  3564. setDirtyBlockIndex.insert(pindexIter);
  3565. // Update indexes
  3566. setBlockIndexCandidates.erase(pindexIter);
  3567. std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> ret = mapBlocksUnlinked.equal_range(pindexIter->pprev);
  3568. while (ret.first != ret.second) {
  3569. if (ret.first->second == pindexIter) {
  3570. mapBlocksUnlinked.erase(ret.first++);
  3571. } else {
  3572. ++ret.first;
  3573. }
  3574. }
  3575. } else if (pindexIter->IsValid(BLOCK_VALID_TRANSACTIONS) && pindexIter->nChainTx) {
  3576. setBlockIndexCandidates.insert(pindexIter);
  3577. }
  3578. }
  3579. if (chainActive.Tip() != nullptr) {
  3580. // We can't prune block index candidates based on our tip if we have
  3581. // no tip due to chainActive being empty!
  3582. PruneBlockIndexCandidates();
  3583. CheckBlockIndex(params.GetConsensus());
  3584. }
  3585. return true;
  3586. }
  3587. bool RewindBlockIndex(const CChainParams& params) {
  3588. if (!g_chainstate.RewindBlockIndex(params)) {
  3589. return false;
  3590. }
  3591. if (chainActive.Tip() != nullptr) {
  3592. // FlushStateToDisk can possibly read chainActive. Be conservative
  3593. // and skip it here, we're about to -reindex-chainstate anyway, so
  3594. // it'll get called a bunch real soon.
  3595. CValidationState state;
  3596. if (!FlushStateToDisk(params, state, FLUSH_STATE_ALWAYS)) {
  3597. return false;
  3598. }
  3599. }
  3600. return true;
  3601. }
  3602. void CChainState::UnloadBlockIndex() {
  3603. nBlockSequenceId = 1;
  3604. g_failed_blocks.clear();
  3605. setBlockIndexCandidates.clear();
  3606. }
  3607. // May NOT be used after any connections are up as much
  3608. // of the peer-processing logic assumes a consistent
  3609. // block index state
  3610. void UnloadBlockIndex()
  3611. {
  3612. LOCK(cs_main);
  3613. chainActive.SetTip(nullptr);
  3614. pindexBestInvalid = nullptr;
  3615. pindexBestHeader = nullptr;
  3616. mempool.clear();
  3617. mapBlocksUnlinked.clear();
  3618. vinfoBlockFile.clear();
  3619. nLastBlockFile = 0;
  3620. setDirtyBlockIndex.clear();
  3621. setDirtyFileInfo.clear();
  3622. versionbitscache.Clear();
  3623. for (int b = 0; b < VERSIONBITS_NUM_BITS; b++) {
  3624. warningcache[b].clear();
  3625. }
  3626. for (BlockMap::value_type& entry : mapBlockIndex) {
  3627. delete entry.second;
  3628. }
  3629. mapBlockIndex.clear();
  3630. fHavePruned = false;
  3631. g_chainstate.UnloadBlockIndex();
  3632. }
  3633. bool LoadBlockIndex(const CChainParams& chainparams)
  3634. {
  3635. // Load block index from databases
  3636. bool needs_init = fReindex;
  3637. if (!fReindex) {
  3638. bool ret = LoadBlockIndexDB(chainparams);
  3639. if (!ret) return false;
  3640. needs_init = mapBlockIndex.empty();
  3641. }
  3642. if (needs_init) {
  3643. // Everything here is for *new* reindex/DBs. Thus, though
  3644. // LoadBlockIndexDB may have set fReindex if we shut down
  3645. // mid-reindex previously, we don't check fReindex and
  3646. // instead only check it prior to LoadBlockIndexDB to set
  3647. // needs_init.
  3648. LogPrintf("Initializing databases...\n");
  3649. // Use the provided setting for -txindex in the new database
  3650. fTxIndex = gArgs.GetBoolArg("-txindex", DEFAULT_TXINDEX);
  3651. pblocktree->WriteFlag("txindex", fTxIndex);
  3652. }
  3653. return true;
  3654. }
  3655. bool CChainState::LoadGenesisBlock(const CChainParams& chainparams)
  3656. {
  3657. LOCK(cs_main);
  3658. // Check whether we're already initialized by checking for genesis in
  3659. // mapBlockIndex. Note that we can't use chainActive here, since it is
  3660. // set based on the coins db, not the block index db, which is the only
  3661. // thing loaded at this point.
  3662. if (mapBlockIndex.count(chainparams.GenesisBlock().GetHash()))
  3663. return true;
  3664. try {
  3665. CBlock &block = const_cast<CBlock&>(chainparams.GenesisBlock());
  3666. CDiskBlockPos blockPos = SaveBlockToDisk(block, 0, chainparams, nullptr);
  3667. if (blockPos.IsNull())
  3668. return error("%s: writing genesis block to disk failed", __func__);
  3669. CBlockIndex *pindex = AddToBlockIndex(block);
  3670. CValidationState state;
  3671. if (!ReceivedBlockTransactions(block, state, pindex, blockPos, chainparams.GetConsensus()))
  3672. return error("%s: genesis block not accepted", __func__);
  3673. } catch (const std::runtime_error& e) {
  3674. return error("%s: failed to write genesis block: %s", __func__, e.what());
  3675. }
  3676. return true;
  3677. }
  3678. bool LoadGenesisBlock(const CChainParams& chainparams)
  3679. {
  3680. return g_chainstate.LoadGenesisBlock(chainparams);
  3681. }
  3682. bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, CDiskBlockPos *dbp)
  3683. {
  3684. // Map of disk positions for blocks with unknown parent (only used for reindex)
  3685. static std::multimap<uint256, CDiskBlockPos> mapBlocksUnknownParent;
  3686. int64_t nStart = GetTimeMillis();
  3687. int nLoaded = 0;
  3688. try {
  3689. // This takes over fileIn and calls fclose() on it in the CBufferedFile destructor
  3690. CBufferedFile blkdat(fileIn, 2*MAX_BLOCK_SERIALIZED_SIZE, MAX_BLOCK_SERIALIZED_SIZE+8, SER_DISK, CLIENT_VERSION);
  3691. uint64_t nRewind = blkdat.GetPos();
  3692. while (!blkdat.eof()) {
  3693. boost::this_thread::interruption_point();
  3694. blkdat.SetPos(nRewind);
  3695. nRewind++; // start one byte further next time, in case of failure
  3696. blkdat.SetLimit(); // remove former limit
  3697. unsigned int nSize = 0;
  3698. try {
  3699. // locate a header
  3700. unsigned char buf[CMessageHeader::MESSAGE_START_SIZE];
  3701. blkdat.FindByte(chainparams.MessageStart()[0]);
  3702. nRewind = blkdat.GetPos()+1;
  3703. blkdat >> FLATDATA(buf);
  3704. if (memcmp(buf, chainparams.MessageStart(), CMessageHeader::MESSAGE_START_SIZE))
  3705. continue;
  3706. // read size
  3707. blkdat >> nSize;
  3708. if (nSize < 80 || nSize > MAX_BLOCK_SERIALIZED_SIZE)
  3709. continue;
  3710. } catch (const std::exception&) {
  3711. // no valid block header found; don't complain
  3712. break;
  3713. }
  3714. try {
  3715. // read block
  3716. uint64_t nBlockPos = blkdat.GetPos();
  3717. if (dbp)
  3718. dbp->nPos = nBlockPos;
  3719. blkdat.SetLimit(nBlockPos + nSize);
  3720. blkdat.SetPos(nBlockPos);
  3721. std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
  3722. CBlock& block = *pblock;
  3723. blkdat >> block;
  3724. nRewind = blkdat.GetPos();
  3725. // detect out of order blocks, and store them for later
  3726. uint256 hash = block.GetHash();
  3727. if (hash != chainparams.GetConsensus().hashGenesisBlock && mapBlockIndex.find(block.hashPrevBlock) == mapBlockIndex.end()) {
  3728. LogPrint(BCLog::REINDEX, "%s: Out of order block %s, parent %s not known\n", __func__, hash.ToString(),
  3729. block.hashPrevBlock.ToString());
  3730. if (dbp)
  3731. mapBlocksUnknownParent.insert(std::make_pair(block.hashPrevBlock, *dbp));
  3732. continue;
  3733. }
  3734. // process in case the block isn't known yet
  3735. if (mapBlockIndex.count(hash) == 0 || (mapBlockIndex[hash]->nStatus & BLOCK_HAVE_DATA) == 0) {
  3736. LOCK(cs_main);
  3737. CValidationState state;
  3738. if (g_chainstate.AcceptBlock(pblock, state, chainparams, nullptr, true, dbp, nullptr))
  3739. nLoaded++;
  3740. if (state.IsError())
  3741. break;
  3742. } else if (hash != chainparams.GetConsensus().hashGenesisBlock && mapBlockIndex[hash]->nHeight % 1000 == 0) {
  3743. LogPrint(BCLog::REINDEX, "Block Import: already had block %s at height %d\n", hash.ToString(), mapBlockIndex[hash]->nHeight);
  3744. }
  3745. // Activate the genesis block so normal node progress can continue
  3746. if (hash == chainparams.GetConsensus().hashGenesisBlock) {
  3747. CValidationState state;
  3748. if (!ActivateBestChain(state, chainparams)) {
  3749. break;
  3750. }
  3751. }
  3752. NotifyHeaderTip();
  3753. // Recursively process earlier encountered successors of this block
  3754. std::deque<uint256> queue;
  3755. queue.push_back(hash);
  3756. while (!queue.empty()) {
  3757. uint256 head = queue.front();
  3758. queue.pop_front();
  3759. std::pair<std::multimap<uint256, CDiskBlockPos>::iterator, std::multimap<uint256, CDiskBlockPos>::iterator> range = mapBlocksUnknownParent.equal_range(head);
  3760. while (range.first != range.second) {
  3761. std::multimap<uint256, CDiskBlockPos>::iterator it = range.first;
  3762. std::shared_ptr<CBlock> pblockrecursive = std::make_shared<CBlock>();
  3763. if (ReadBlockFromDisk(*pblockrecursive, it->second, chainparams.GetConsensus()))
  3764. {
  3765. LogPrint(BCLog::REINDEX, "%s: Processing out of order child %s of %s\n", __func__, pblockrecursive->GetHash().ToString(),
  3766. head.ToString());
  3767. LOCK(cs_main);
  3768. CValidationState dummy;
  3769. if (g_chainstate.AcceptBlock(pblockrecursive, dummy, chainparams, nullptr, true, &it->second, nullptr))
  3770. {
  3771. nLoaded++;
  3772. queue.push_back(pblockrecursive->GetHash());
  3773. }
  3774. }
  3775. range.first++;
  3776. mapBlocksUnknownParent.erase(it);
  3777. NotifyHeaderTip();
  3778. }
  3779. }
  3780. } catch (const std::exception& e) {
  3781. LogPrintf("%s: Deserialize or I/O error - %s\n", __func__, e.what());
  3782. }
  3783. }
  3784. } catch (const std::runtime_error& e) {
  3785. AbortNode(std::string("System error: ") + e.what());
  3786. }
  3787. if (nLoaded > 0)
  3788. LogPrintf("Loaded %i blocks from external file in %dms\n", nLoaded, GetTimeMillis() - nStart);
  3789. return nLoaded > 0;
  3790. }
  3791. void CChainState::CheckBlockIndex(const Consensus::Params& consensusParams)
  3792. {
  3793. if (!fCheckBlockIndex) {
  3794. return;
  3795. }
  3796. LOCK(cs_main);
  3797. // During a reindex, we read the genesis block and call CheckBlockIndex before ActivateBestChain,
  3798. // so we have the genesis block in mapBlockIndex but no active chain. (A few of the tests when
  3799. // iterating the block tree require that chainActive has been initialized.)
  3800. if (chainActive.Height() < 0) {
  3801. assert(mapBlockIndex.size() <= 1);
  3802. return;
  3803. }
  3804. // Build forward-pointing map of the entire block tree.
  3805. std::multimap<CBlockIndex*,CBlockIndex*> forward;
  3806. for (auto& entry : mapBlockIndex) {
  3807. forward.insert(std::make_pair(entry.second->pprev, entry.second));
  3808. }
  3809. assert(forward.size() == mapBlockIndex.size());
  3810. std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangeGenesis = forward.equal_range(nullptr);
  3811. CBlockIndex *pindex = rangeGenesis.first->second;
  3812. rangeGenesis.first++;
  3813. assert(rangeGenesis.first == rangeGenesis.second); // There is only one index entry with parent nullptr.
  3814. // Iterate over the entire block tree, using depth-first search.
  3815. // Along the way, remember whether there are blocks on the path from genesis
  3816. // block being explored which are the first to have certain properties.
  3817. size_t nNodes = 0;
  3818. int nHeight = 0;
  3819. CBlockIndex* pindexFirstInvalid = nullptr; // Oldest ancestor of pindex which is invalid.
  3820. CBlockIndex* pindexFirstMissing = nullptr; // Oldest ancestor of pindex which does not have BLOCK_HAVE_DATA.
  3821. CBlockIndex* pindexFirstNeverProcessed = nullptr; // Oldest ancestor of pindex for which nTx == 0.
  3822. CBlockIndex* pindexFirstNotTreeValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_TREE (regardless of being valid or not).
  3823. CBlockIndex* pindexFirstNotTransactionsValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_TRANSACTIONS (regardless of being valid or not).
  3824. CBlockIndex* pindexFirstNotChainValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_CHAIN (regardless of being valid or not).
  3825. CBlockIndex* pindexFirstNotScriptsValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_SCRIPTS (regardless of being valid or not).
  3826. while (pindex != nullptr) {
  3827. nNodes++;
  3828. if (pindexFirstInvalid == nullptr && pindex->nStatus & BLOCK_FAILED_VALID) pindexFirstInvalid = pindex;
  3829. if (pindexFirstMissing == nullptr && !(pindex->nStatus & BLOCK_HAVE_DATA)) pindexFirstMissing = pindex;
  3830. if (pindexFirstNeverProcessed == nullptr && pindex->nTx == 0) pindexFirstNeverProcessed = pindex;
  3831. if (pindex->pprev != nullptr && pindexFirstNotTreeValid == nullptr && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_TREE) pindexFirstNotTreeValid = pindex;
  3832. if (pindex->pprev != nullptr && pindexFirstNotTransactionsValid == nullptr && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_TRANSACTIONS) pindexFirstNotTransactionsValid = pindex;
  3833. if (pindex->pprev != nullptr && pindexFirstNotChainValid == nullptr && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_CHAIN) pindexFirstNotChainValid = pindex;
  3834. if (pindex->pprev != nullptr && pindexFirstNotScriptsValid == nullptr && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_SCRIPTS) pindexFirstNotScriptsValid = pindex;
  3835. // Begin: actual consistency checks.
  3836. if (pindex->pprev == nullptr) {
  3837. // Genesis block checks.
  3838. assert(pindex->GetBlockHash() == consensusParams.hashGenesisBlock); // Genesis block's hash must match.
  3839. assert(pindex == chainActive.Genesis()); // The current active chain's genesis block must be this block.
  3840. }
  3841. 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)
  3842. // VALID_TRANSACTIONS is equivalent to nTx > 0 for all nodes (whether or not pruning has occurred).
  3843. // HAVE_DATA is only equivalent to nTx > 0 (or VALID_TRANSACTIONS) if no pruning has occurred.
  3844. if (!fHavePruned) {
  3845. // If we've never pruned, then HAVE_DATA should be equivalent to nTx > 0
  3846. assert(!(pindex->nStatus & BLOCK_HAVE_DATA) == (pindex->nTx == 0));
  3847. assert(pindexFirstMissing == pindexFirstNeverProcessed);
  3848. } else {
  3849. // If we have pruned, then we can only say that HAVE_DATA implies nTx > 0
  3850. if (pindex->nStatus & BLOCK_HAVE_DATA) assert(pindex->nTx > 0);
  3851. }
  3852. if (pindex->nStatus & BLOCK_HAVE_UNDO) assert(pindex->nStatus & BLOCK_HAVE_DATA);
  3853. assert(((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TRANSACTIONS) == (pindex->nTx > 0)); // This is pruning-independent.
  3854. // All parents having had data (at some point) is equivalent to all parents being VALID_TRANSACTIONS, which is equivalent to nChainTx being set.
  3855. assert((pindexFirstNeverProcessed != nullptr) == (pindex->nChainTx == 0)); // nChainTx != 0 is used to signal that all parent blocks have been processed (but may have been pruned).
  3856. assert((pindexFirstNotTransactionsValid != nullptr) == (pindex->nChainTx == 0));
  3857. assert(pindex->nHeight == nHeight); // nHeight must be consistent.
  3858. 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.
  3859. assert(nHeight < 2 || (pindex->pskip && (pindex->pskip->nHeight < nHeight))); // The pskip pointer must point back for all but the first 2 blocks.
  3860. assert(pindexFirstNotTreeValid == nullptr); // All mapBlockIndex entries must at least be TREE valid
  3861. if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TREE) assert(pindexFirstNotTreeValid == nullptr); // TREE valid implies all parents are TREE valid
  3862. if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_CHAIN) assert(pindexFirstNotChainValid == nullptr); // CHAIN valid implies all parents are CHAIN valid
  3863. if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_SCRIPTS) assert(pindexFirstNotScriptsValid == nullptr); // SCRIPTS valid implies all parents are SCRIPTS valid
  3864. if (pindexFirstInvalid == nullptr) {
  3865. // Checks for not-invalid blocks.
  3866. assert((pindex->nStatus & BLOCK_FAILED_MASK) == 0); // The failed mask cannot be set for blocks without invalid parents.
  3867. }
  3868. if (!CBlockIndexWorkComparator()(pindex, chainActive.Tip()) && pindexFirstNeverProcessed == nullptr) {
  3869. if (pindexFirstInvalid == nullptr) {
  3870. // If this block sorts at least as good as the current tip and
  3871. // is valid and we have all data for its parents, it must be in
  3872. // setBlockIndexCandidates. chainActive.Tip() must also be there
  3873. // even if some data has been pruned.
  3874. if (pindexFirstMissing == nullptr || pindex == chainActive.Tip()) {
  3875. assert(setBlockIndexCandidates.count(pindex));
  3876. }
  3877. // If some parent is missing, then it could be that this block was in
  3878. // setBlockIndexCandidates but had to be removed because of the missing data.
  3879. // In this case it must be in mapBlocksUnlinked -- see test below.
  3880. }
  3881. } else { // If this block sorts worse than the current tip or some ancestor's block has never been seen, it cannot be in setBlockIndexCandidates.
  3882. assert(setBlockIndexCandidates.count(pindex) == 0);
  3883. }
  3884. // Check whether this block is in mapBlocksUnlinked.
  3885. std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangeUnlinked = mapBlocksUnlinked.equal_range(pindex->pprev);
  3886. bool foundInUnlinked = false;
  3887. while (rangeUnlinked.first != rangeUnlinked.second) {
  3888. assert(rangeUnlinked.first->first == pindex->pprev);
  3889. if (rangeUnlinked.first->second == pindex) {
  3890. foundInUnlinked = true;
  3891. break;
  3892. }
  3893. rangeUnlinked.first++;
  3894. }
  3895. if (pindex->pprev && (pindex->nStatus & BLOCK_HAVE_DATA) && pindexFirstNeverProcessed != nullptr && pindexFirstInvalid == nullptr) {
  3896. // If this block has block data available, some parent was never received, and has no invalid parents, it must be in mapBlocksUnlinked.
  3897. assert(foundInUnlinked);
  3898. }
  3899. if (!(pindex->nStatus & BLOCK_HAVE_DATA)) assert(!foundInUnlinked); // Can't be in mapBlocksUnlinked if we don't HAVE_DATA
  3900. if (pindexFirstMissing == nullptr) assert(!foundInUnlinked); // We aren't missing data for any parent -- cannot be in mapBlocksUnlinked.
  3901. if (pindex->pprev && (pindex->nStatus & BLOCK_HAVE_DATA) && pindexFirstNeverProcessed == nullptr && pindexFirstMissing != nullptr) {
  3902. // We HAVE_DATA for this block, have received data for all parents at some point, but we're currently missing data for some parent.
  3903. assert(fHavePruned); // We must have pruned.
  3904. // This block may have entered mapBlocksUnlinked if:
  3905. // - it has a descendant that at some point had more work than the
  3906. // tip, and
  3907. // - we tried switching to that descendant but were missing
  3908. // data for some intermediate block between chainActive and the
  3909. // tip.
  3910. // So if this block is itself better than chainActive.Tip() and it wasn't in
  3911. // setBlockIndexCandidates, then it must be in mapBlocksUnlinked.
  3912. if (!CBlockIndexWorkComparator()(pindex, chainActive.Tip()) && setBlockIndexCandidates.count(pindex) == 0) {
  3913. if (pindexFirstInvalid == nullptr) {
  3914. assert(foundInUnlinked);
  3915. }
  3916. }
  3917. }
  3918. // assert(pindex->GetBlockHash() == pindex->GetBlockHeader().GetHash()); // Perhaps too slow
  3919. // End: actual consistency checks.
  3920. // Try descending into the first subnode.
  3921. std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> range = forward.equal_range(pindex);
  3922. if (range.first != range.second) {
  3923. // A subnode was found.
  3924. pindex = range.first->second;
  3925. nHeight++;
  3926. continue;
  3927. }
  3928. // This is a leaf node.
  3929. // Move upwards until we reach a node of which we have not yet visited the last child.
  3930. while (pindex) {
  3931. // We are going to either move to a parent or a sibling of pindex.
  3932. // If pindex was the first with a certain property, unset the corresponding variable.
  3933. if (pindex == pindexFirstInvalid) pindexFirstInvalid = nullptr;
  3934. if (pindex == pindexFirstMissing) pindexFirstMissing = nullptr;
  3935. if (pindex == pindexFirstNeverProcessed) pindexFirstNeverProcessed = nullptr;
  3936. if (pindex == pindexFirstNotTreeValid) pindexFirstNotTreeValid = nullptr;
  3937. if (pindex == pindexFirstNotTransactionsValid) pindexFirstNotTransactionsValid = nullptr;
  3938. if (pindex == pindexFirstNotChainValid) pindexFirstNotChainValid = nullptr;
  3939. if (pindex == pindexFirstNotScriptsValid) pindexFirstNotScriptsValid = nullptr;
  3940. // Find our parent.
  3941. CBlockIndex* pindexPar = pindex->pprev;
  3942. // Find which child we just visited.
  3943. std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangePar = forward.equal_range(pindexPar);
  3944. while (rangePar.first->second != pindex) {
  3945. assert(rangePar.first != rangePar.second); // Our parent must have at least the node we're coming from as child.
  3946. rangePar.first++;
  3947. }
  3948. // Proceed to the next one.
  3949. rangePar.first++;
  3950. if (rangePar.first != rangePar.second) {
  3951. // Move to the sibling.
  3952. pindex = rangePar.first->second;
  3953. break;
  3954. } else {
  3955. // Move up further.
  3956. pindex = pindexPar;
  3957. nHeight--;
  3958. continue;
  3959. }
  3960. }
  3961. }
  3962. // Check that we actually traversed the entire map.
  3963. assert(nNodes == forward.size());
  3964. }
  3965. std::string CBlockFileInfo::ToString() const
  3966. {
  3967. 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));
  3968. }
  3969. CBlockFileInfo* GetBlockFileInfo(size_t n)
  3970. {
  3971. LOCK(cs_LastBlockFile);
  3972. return &vinfoBlockFile.at(n);
  3973. }
  3974. ThresholdState VersionBitsTipState(const Consensus::Params& params, Consensus::DeploymentPos pos)
  3975. {
  3976. LOCK(cs_main);
  3977. return VersionBitsState(chainActive.Tip(), params, pos, versionbitscache);
  3978. }
  3979. BIP9Stats VersionBitsTipStatistics(const Consensus::Params& params, Consensus::DeploymentPos pos)
  3980. {
  3981. LOCK(cs_main);
  3982. return VersionBitsStatistics(chainActive.Tip(), params, pos);
  3983. }
  3984. int VersionBitsTipStateSinceHeight(const Consensus::Params& params, Consensus::DeploymentPos pos)
  3985. {
  3986. LOCK(cs_main);
  3987. return VersionBitsStateSinceHeight(chainActive.Tip(), params, pos, versionbitscache);
  3988. }
  3989. static const uint64_t MEMPOOL_DUMP_VERSION = 1;
  3990. bool LoadMempool(void)
  3991. {
  3992. const CChainParams& chainparams = Params();
  3993. int64_t nExpiryTimeout = gArgs.GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60;
  3994. FILE* filestr = fsbridge::fopen(GetDataDir() / "mempool.dat", "rb");
  3995. CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
  3996. if (file.IsNull()) {
  3997. LogPrintf("Failed to open mempool file from disk. Continuing anyway.\n");
  3998. return false;
  3999. }
  4000. int64_t count = 0;
  4001. int64_t expired = 0;
  4002. int64_t failed = 0;
  4003. int64_t already_there = 0;
  4004. int64_t nNow = GetTime();
  4005. try {
  4006. uint64_t version;
  4007. file >> version;
  4008. if (version != MEMPOOL_DUMP_VERSION) {
  4009. return false;
  4010. }
  4011. uint64_t num;
  4012. file >> num;
  4013. while (num--) {
  4014. CTransactionRef tx;
  4015. int64_t nTime;
  4016. int64_t nFeeDelta;
  4017. file >> tx;
  4018. file >> nTime;
  4019. file >> nFeeDelta;
  4020. CAmount amountdelta = nFeeDelta;
  4021. if (amountdelta) {
  4022. mempool.PrioritiseTransaction(tx->GetHash(), amountdelta);
  4023. }
  4024. CValidationState state;
  4025. if (nTime + nExpiryTimeout > nNow) {
  4026. LOCK(cs_main);
  4027. AcceptToMemoryPoolWithTime(chainparams, mempool, state, tx, nullptr /* pfMissingInputs */, nTime,
  4028. nullptr /* plTxnReplaced */, false /* bypass_limits */, 0 /* nAbsurdFee */);
  4029. if (state.IsValid()) {
  4030. ++count;
  4031. } else {
  4032. // mempool may contain the transaction already, e.g. from
  4033. // wallet(s) having loaded it while we were processing
  4034. // mempool transactions; consider these as valid, instead of
  4035. // failed, but mark them as 'already there'
  4036. if (mempool.exists(tx->GetHash())) {
  4037. ++already_there;
  4038. } else {
  4039. ++failed;
  4040. }
  4041. }
  4042. } else {
  4043. ++expired;
  4044. }
  4045. if (ShutdownRequested())
  4046. return false;
  4047. }
  4048. std::map<uint256, CAmount> mapDeltas;
  4049. file >> mapDeltas;
  4050. for (const auto& i : mapDeltas) {
  4051. mempool.PrioritiseTransaction(i.first, i.second);
  4052. }
  4053. } catch (const std::exception& e) {
  4054. LogPrintf("Failed to deserialize mempool data on disk: %s. Continuing anyway.\n", e.what());
  4055. return false;
  4056. }
  4057. LogPrintf("Imported mempool transactions from disk: %i succeeded, %i failed, %i expired, %i already there\n", count, failed, expired, already_there);
  4058. return true;
  4059. }
  4060. bool DumpMempool(void)
  4061. {
  4062. int64_t start = GetTimeMicros();
  4063. std::map<uint256, CAmount> mapDeltas;
  4064. std::vector<TxMempoolInfo> vinfo;
  4065. {
  4066. LOCK(mempool.cs);
  4067. for (const auto &i : mempool.mapDeltas) {
  4068. mapDeltas[i.first] = i.second;
  4069. }
  4070. vinfo = mempool.infoAll();
  4071. }
  4072. int64_t mid = GetTimeMicros();
  4073. try {
  4074. FILE* filestr = fsbridge::fopen(GetDataDir() / "mempool.dat.new", "wb");
  4075. if (!filestr) {
  4076. return false;
  4077. }
  4078. CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
  4079. uint64_t version = MEMPOOL_DUMP_VERSION;
  4080. file << version;
  4081. file << (uint64_t)vinfo.size();
  4082. for (const auto& i : vinfo) {
  4083. file << *(i.tx);
  4084. file << (int64_t)i.nTime;
  4085. file << (int64_t)i.nFeeDelta;
  4086. mapDeltas.erase(i.tx->GetHash());
  4087. }
  4088. file << mapDeltas;
  4089. FileCommit(file.Get());
  4090. file.fclose();
  4091. RenameOver(GetDataDir() / "mempool.dat.new", GetDataDir() / "mempool.dat");
  4092. int64_t last = GetTimeMicros();
  4093. LogPrintf("Dumped mempool: %gs to copy, %gs to dump\n", (mid-start)*MICRO, (last-mid)*MICRO);
  4094. } catch (const std::exception& e) {
  4095. LogPrintf("Failed to dump mempool: %s. Continuing anyway.\n", e.what());
  4096. return false;
  4097. }
  4098. return true;
  4099. }
  4100. //! Guess how far we are in the verification process at the given block index
  4101. double GuessVerificationProgress(const ChainTxData& data, const CBlockIndex *pindex) {
  4102. if (pindex == nullptr)
  4103. return 0.0;
  4104. int64_t nNow = time(nullptr);
  4105. double fTxTotal;
  4106. if (pindex->nChainTx <= data.nTxCount) {
  4107. fTxTotal = data.nTxCount + (nNow - data.nTime) * data.dTxRate;
  4108. } else {
  4109. fTxTotal = pindex->nChainTx + (nNow - pindex->GetBlockTime()) * data.dTxRate;
  4110. }
  4111. return pindex->nChainTx / fTxTotal;
  4112. }
  4113. class CMainCleanup
  4114. {
  4115. public:
  4116. CMainCleanup() {}
  4117. ~CMainCleanup() {
  4118. // block headers
  4119. BlockMap::iterator it1 = mapBlockIndex.begin();
  4120. for (; it1 != mapBlockIndex.end(); it1++)
  4121. delete (*it1).second;
  4122. mapBlockIndex.clear();
  4123. }
  4124. } instance_of_cmaincleanup;