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

net_processing.cpp 146KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2016 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 "net_processing.h"
  6. #include "addrman.h"
  7. #include "arith_uint256.h"
  8. #include "blockencodings.h"
  9. #include "chainparams.h"
  10. #include "consensus/validation.h"
  11. #include "hash.h"
  12. #include "init.h"
  13. #include "validation.h"
  14. #include "merkleblock.h"
  15. #include "net.h"
  16. #include "netmessagemaker.h"
  17. #include "netbase.h"
  18. #include "policy/fees.h"
  19. #include "policy/policy.h"
  20. #include "primitives/block.h"
  21. #include "primitives/transaction.h"
  22. #include "random.h"
  23. #include "tinyformat.h"
  24. #include "txmempool.h"
  25. #include "ui_interface.h"
  26. #include "util.h"
  27. #include "utilmoneystr.h"
  28. #include "utilstrencodings.h"
  29. #include "validationinterface.h"
  30. #include <boost/thread.hpp>
  31. #if defined(NDEBUG)
  32. # error "Bitcoin cannot be compiled without assertions."
  33. #endif
  34. std::atomic<int64_t> nTimeBestReceived(0); // Used only to inform the wallet of when we last received a block
  35. struct IteratorComparator
  36. {
  37. template<typename I>
  38. bool operator()(const I& a, const I& b)
  39. {
  40. return &(*a) < &(*b);
  41. }
  42. };
  43. struct COrphanTx {
  44. // When modifying, adapt the copy of this definition in tests/DoS_tests.
  45. CTransactionRef tx;
  46. NodeId fromPeer;
  47. int64_t nTimeExpire;
  48. };
  49. std::map<uint256, COrphanTx> mapOrphanTransactions GUARDED_BY(cs_main);
  50. std::map<COutPoint, std::set<std::map<uint256, COrphanTx>::iterator, IteratorComparator>> mapOrphanTransactionsByPrev GUARDED_BY(cs_main);
  51. void EraseOrphansFor(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
  52. static size_t vExtraTxnForCompactIt = 0;
  53. static std::vector<std::pair<uint256, CTransactionRef>> vExtraTxnForCompact GUARDED_BY(cs_main);
  54. static const uint64_t RANDOMIZER_ID_ADDRESS_RELAY = 0x3cac0035b5866b90ULL; // SHA256("main address relay")[0:8]
  55. // Internal stuff
  56. namespace {
  57. /** Number of nodes with fSyncStarted. */
  58. int nSyncStarted = 0;
  59. /**
  60. * Sources of received blocks, saved to be able to send them reject
  61. * messages or ban them when processing happens afterwards. Protected by
  62. * cs_main.
  63. * Set mapBlockSource[hash].second to false if the node should not be
  64. * punished if the block is invalid.
  65. */
  66. std::map<uint256, std::pair<NodeId, bool>> mapBlockSource;
  67. /**
  68. * Filter for transactions that were recently rejected by
  69. * AcceptToMemoryPool. These are not rerequested until the chain tip
  70. * changes, at which point the entire filter is reset. Protected by
  71. * cs_main.
  72. *
  73. * Without this filter we'd be re-requesting txs from each of our peers,
  74. * increasing bandwidth consumption considerably. For instance, with 100
  75. * peers, half of which relay a tx we don't accept, that might be a 50x
  76. * bandwidth increase. A flooding attacker attempting to roll-over the
  77. * filter using minimum-sized, 60byte, transactions might manage to send
  78. * 1000/sec if we have fast peers, so we pick 120,000 to give our peers a
  79. * two minute window to send invs to us.
  80. *
  81. * Decreasing the false positive rate is fairly cheap, so we pick one in a
  82. * million to make it highly unlikely for users to have issues with this
  83. * filter.
  84. *
  85. * Memory used: 1.3 MB
  86. */
  87. std::unique_ptr<CRollingBloomFilter> recentRejects;
  88. uint256 hashRecentRejectsChainTip;
  89. /** Blocks that are in flight, and that are in the queue to be downloaded. Protected by cs_main. */
  90. struct QueuedBlock {
  91. uint256 hash;
  92. const CBlockIndex* pindex; //!< Optional.
  93. bool fValidatedHeaders; //!< Whether this block has validated headers at the time of request.
  94. std::unique_ptr<PartiallyDownloadedBlock> partialBlock; //!< Optional, used for CMPCTBLOCK downloads
  95. };
  96. std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> > mapBlocksInFlight;
  97. /** Stack of nodes which we have set to announce using compact blocks */
  98. std::list<NodeId> lNodesAnnouncingHeaderAndIDs;
  99. /** Number of preferable block download peers. */
  100. int nPreferredDownload = 0;
  101. /** Number of peers from which we're downloading blocks. */
  102. int nPeersWithValidatedDownloads = 0;
  103. /** Relay map, protected by cs_main. */
  104. typedef std::map<uint256, CTransactionRef> MapRelay;
  105. MapRelay mapRelay;
  106. /** Expiration-time ordered list of (expire time, relay map entry) pairs, protected by cs_main). */
  107. std::deque<std::pair<int64_t, MapRelay::iterator>> vRelayExpiration;
  108. } // anon namespace
  109. //////////////////////////////////////////////////////////////////////////////
  110. //
  111. // Registration of network node signals.
  112. //
  113. namespace {
  114. struct CBlockReject {
  115. unsigned char chRejectCode;
  116. std::string strRejectReason;
  117. uint256 hashBlock;
  118. };
  119. /**
  120. * Maintain validation-specific state about nodes, protected by cs_main, instead
  121. * by CNode's own locks. This simplifies asynchronous operation, where
  122. * processing of incoming data is done after the ProcessMessage call returns,
  123. * and we're no longer holding the node's locks.
  124. */
  125. struct CNodeState {
  126. //! The peer's address
  127. const CService address;
  128. //! Whether we have a fully established connection.
  129. bool fCurrentlyConnected;
  130. //! Accumulated misbehaviour score for this peer.
  131. int nMisbehavior;
  132. //! Whether this peer should be disconnected and banned (unless whitelisted).
  133. bool fShouldBan;
  134. //! String name of this peer (debugging/logging purposes).
  135. const std::string name;
  136. //! List of asynchronously-determined block rejections to notify this peer about.
  137. std::vector<CBlockReject> rejects;
  138. //! The best known block we know this peer has announced.
  139. const CBlockIndex *pindexBestKnownBlock;
  140. //! The hash of the last unknown block this peer has announced.
  141. uint256 hashLastUnknownBlock;
  142. //! The last full block we both have.
  143. const CBlockIndex *pindexLastCommonBlock;
  144. //! The best header we have sent our peer.
  145. const CBlockIndex *pindexBestHeaderSent;
  146. //! Length of current-streak of unconnecting headers announcements
  147. int nUnconnectingHeaders;
  148. //! Whether we've started headers synchronization with this peer.
  149. bool fSyncStarted;
  150. //! Since when we're stalling block download progress (in microseconds), or 0.
  151. int64_t nStallingSince;
  152. std::list<QueuedBlock> vBlocksInFlight;
  153. //! When the first entry in vBlocksInFlight started downloading. Don't care when vBlocksInFlight is empty.
  154. int64_t nDownloadingSince;
  155. int nBlocksInFlight;
  156. int nBlocksInFlightValidHeaders;
  157. //! Whether we consider this a preferred download peer.
  158. bool fPreferredDownload;
  159. //! Whether this peer wants invs or headers (when possible) for block announcements.
  160. bool fPreferHeaders;
  161. //! Whether this peer wants invs or cmpctblocks (when possible) for block announcements.
  162. bool fPreferHeaderAndIDs;
  163. /**
  164. * Whether this peer will send us cmpctblocks if we request them.
  165. * This is not used to gate request logic, as we really only care about fSupportsDesiredCmpctVersion,
  166. * but is used as a flag to "lock in" the version of compact blocks (fWantsCmpctWitness) we send.
  167. */
  168. bool fProvidesHeaderAndIDs;
  169. //! Whether this peer can give us witnesses
  170. bool fHaveWitness;
  171. //! Whether this peer wants witnesses in cmpctblocks/blocktxns
  172. bool fWantsCmpctWitness;
  173. /**
  174. * If we've announced NODE_WITNESS to this peer: whether the peer sends witnesses in cmpctblocks/blocktxns,
  175. * otherwise: whether this peer sends non-witnesses in cmpctblocks/blocktxns.
  176. */
  177. bool fSupportsDesiredCmpctVersion;
  178. CNodeState(CAddress addrIn, std::string addrNameIn) : address(addrIn), name(addrNameIn) {
  179. fCurrentlyConnected = false;
  180. nMisbehavior = 0;
  181. fShouldBan = false;
  182. pindexBestKnownBlock = NULL;
  183. hashLastUnknownBlock.SetNull();
  184. pindexLastCommonBlock = NULL;
  185. pindexBestHeaderSent = NULL;
  186. nUnconnectingHeaders = 0;
  187. fSyncStarted = false;
  188. nStallingSince = 0;
  189. nDownloadingSince = 0;
  190. nBlocksInFlight = 0;
  191. nBlocksInFlightValidHeaders = 0;
  192. fPreferredDownload = false;
  193. fPreferHeaders = false;
  194. fPreferHeaderAndIDs = false;
  195. fProvidesHeaderAndIDs = false;
  196. fHaveWitness = false;
  197. fWantsCmpctWitness = false;
  198. fSupportsDesiredCmpctVersion = false;
  199. }
  200. };
  201. /** Map maintaining per-node state. Requires cs_main. */
  202. std::map<NodeId, CNodeState> mapNodeState;
  203. // Requires cs_main.
  204. CNodeState *State(NodeId pnode) {
  205. std::map<NodeId, CNodeState>::iterator it = mapNodeState.find(pnode);
  206. if (it == mapNodeState.end())
  207. return NULL;
  208. return &it->second;
  209. }
  210. void UpdatePreferredDownload(CNode* node, CNodeState* state)
  211. {
  212. nPreferredDownload -= state->fPreferredDownload;
  213. // Whether this node should be marked as a preferred download node.
  214. state->fPreferredDownload = (!node->fInbound || node->fWhitelisted) && !node->fOneShot && !node->fClient;
  215. nPreferredDownload += state->fPreferredDownload;
  216. }
  217. void PushNodeVersion(CNode *pnode, CConnman& connman, int64_t nTime)
  218. {
  219. ServiceFlags nLocalNodeServices = pnode->GetLocalServices();
  220. uint64_t nonce = pnode->GetLocalNonce();
  221. int nNodeStartingHeight = pnode->GetMyStartingHeight();
  222. NodeId nodeid = pnode->GetId();
  223. CAddress addr = pnode->addr;
  224. CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService(), addr.nServices));
  225. CAddress addrMe = CAddress(CService(), nLocalNodeServices);
  226. connman.PushMessage(pnode, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::VERSION, PROTOCOL_VERSION, (uint64_t)nLocalNodeServices, nTime, addrYou, addrMe,
  227. nonce, strSubVersion, nNodeStartingHeight, ::fRelayTxes));
  228. if (fLogIPs) {
  229. LogPrint(BCLog::NET, "send version message: version %d, blocks=%d, us=%s, them=%s, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addrMe.ToString(), addrYou.ToString(), nodeid);
  230. } else {
  231. LogPrint(BCLog::NET, "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addrMe.ToString(), nodeid);
  232. }
  233. }
  234. void InitializeNode(CNode *pnode, CConnman& connman) {
  235. CAddress addr = pnode->addr;
  236. std::string addrName = pnode->GetAddrName();
  237. NodeId nodeid = pnode->GetId();
  238. {
  239. LOCK(cs_main);
  240. mapNodeState.emplace_hint(mapNodeState.end(), std::piecewise_construct, std::forward_as_tuple(nodeid), std::forward_as_tuple(addr, std::move(addrName)));
  241. }
  242. if(!pnode->fInbound)
  243. PushNodeVersion(pnode, connman, GetTime());
  244. }
  245. void FinalizeNode(NodeId nodeid, bool& fUpdateConnectionTime) {
  246. fUpdateConnectionTime = false;
  247. LOCK(cs_main);
  248. CNodeState *state = State(nodeid);
  249. if (state->fSyncStarted)
  250. nSyncStarted--;
  251. if (state->nMisbehavior == 0 && state->fCurrentlyConnected) {
  252. fUpdateConnectionTime = true;
  253. }
  254. BOOST_FOREACH(const QueuedBlock& entry, state->vBlocksInFlight) {
  255. mapBlocksInFlight.erase(entry.hash);
  256. }
  257. EraseOrphansFor(nodeid);
  258. nPreferredDownload -= state->fPreferredDownload;
  259. nPeersWithValidatedDownloads -= (state->nBlocksInFlightValidHeaders != 0);
  260. assert(nPeersWithValidatedDownloads >= 0);
  261. mapNodeState.erase(nodeid);
  262. if (mapNodeState.empty()) {
  263. // Do a consistency check after the last peer is removed.
  264. assert(mapBlocksInFlight.empty());
  265. assert(nPreferredDownload == 0);
  266. assert(nPeersWithValidatedDownloads == 0);
  267. }
  268. }
  269. // Requires cs_main.
  270. // Returns a bool indicating whether we requested this block.
  271. // Also used if a block was /not/ received and timed out or started with another peer
  272. bool MarkBlockAsReceived(const uint256& hash) {
  273. std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
  274. if (itInFlight != mapBlocksInFlight.end()) {
  275. CNodeState *state = State(itInFlight->second.first);
  276. state->nBlocksInFlightValidHeaders -= itInFlight->second.second->fValidatedHeaders;
  277. if (state->nBlocksInFlightValidHeaders == 0 && itInFlight->second.second->fValidatedHeaders) {
  278. // Last validated block on the queue was received.
  279. nPeersWithValidatedDownloads--;
  280. }
  281. if (state->vBlocksInFlight.begin() == itInFlight->second.second) {
  282. // First block on the queue was received, update the start download time for the next one
  283. state->nDownloadingSince = std::max(state->nDownloadingSince, GetTimeMicros());
  284. }
  285. state->vBlocksInFlight.erase(itInFlight->second.second);
  286. state->nBlocksInFlight--;
  287. state->nStallingSince = 0;
  288. mapBlocksInFlight.erase(itInFlight);
  289. return true;
  290. }
  291. return false;
  292. }
  293. // Requires cs_main.
  294. // returns false, still setting pit, if the block was already in flight from the same peer
  295. // pit will only be valid as long as the same cs_main lock is being held
  296. bool MarkBlockAsInFlight(NodeId nodeid, const uint256& hash, const Consensus::Params& consensusParams, const CBlockIndex* pindex = NULL, std::list<QueuedBlock>::iterator** pit = NULL) {
  297. CNodeState *state = State(nodeid);
  298. assert(state != NULL);
  299. // Short-circuit most stuff in case its from the same node
  300. std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
  301. if (itInFlight != mapBlocksInFlight.end() && itInFlight->second.first == nodeid) {
  302. *pit = &itInFlight->second.second;
  303. return false;
  304. }
  305. // Make sure it's not listed somewhere already.
  306. MarkBlockAsReceived(hash);
  307. std::list<QueuedBlock>::iterator it = state->vBlocksInFlight.insert(state->vBlocksInFlight.end(),
  308. {hash, pindex, pindex != NULL, std::unique_ptr<PartiallyDownloadedBlock>(pit ? new PartiallyDownloadedBlock(&mempool) : NULL)});
  309. state->nBlocksInFlight++;
  310. state->nBlocksInFlightValidHeaders += it->fValidatedHeaders;
  311. if (state->nBlocksInFlight == 1) {
  312. // We're starting a block download (batch) from this peer.
  313. state->nDownloadingSince = GetTimeMicros();
  314. }
  315. if (state->nBlocksInFlightValidHeaders == 1 && pindex != NULL) {
  316. nPeersWithValidatedDownloads++;
  317. }
  318. itInFlight = mapBlocksInFlight.insert(std::make_pair(hash, std::make_pair(nodeid, it))).first;
  319. if (pit)
  320. *pit = &itInFlight->second.second;
  321. return true;
  322. }
  323. /** Check whether the last unknown block a peer advertised is not yet known. */
  324. void ProcessBlockAvailability(NodeId nodeid) {
  325. CNodeState *state = State(nodeid);
  326. assert(state != NULL);
  327. if (!state->hashLastUnknownBlock.IsNull()) {
  328. BlockMap::iterator itOld = mapBlockIndex.find(state->hashLastUnknownBlock);
  329. if (itOld != mapBlockIndex.end() && itOld->second->nChainWork > 0) {
  330. if (state->pindexBestKnownBlock == NULL || itOld->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
  331. state->pindexBestKnownBlock = itOld->second;
  332. state->hashLastUnknownBlock.SetNull();
  333. }
  334. }
  335. }
  336. /** Update tracking information about which blocks a peer is assumed to have. */
  337. void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) {
  338. CNodeState *state = State(nodeid);
  339. assert(state != NULL);
  340. ProcessBlockAvailability(nodeid);
  341. BlockMap::iterator it = mapBlockIndex.find(hash);
  342. if (it != mapBlockIndex.end() && it->second->nChainWork > 0) {
  343. // An actually better block was announced.
  344. if (state->pindexBestKnownBlock == NULL || it->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
  345. state->pindexBestKnownBlock = it->second;
  346. } else {
  347. // An unknown block was announced; just assume that the latest one is the best one.
  348. state->hashLastUnknownBlock = hash;
  349. }
  350. }
  351. void MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid, CConnman& connman) {
  352. AssertLockHeld(cs_main);
  353. CNodeState* nodestate = State(nodeid);
  354. if (!nodestate || !nodestate->fSupportsDesiredCmpctVersion) {
  355. // Never ask from peers who can't provide witnesses.
  356. return;
  357. }
  358. if (nodestate->fProvidesHeaderAndIDs) {
  359. for (std::list<NodeId>::iterator it = lNodesAnnouncingHeaderAndIDs.begin(); it != lNodesAnnouncingHeaderAndIDs.end(); it++) {
  360. if (*it == nodeid) {
  361. lNodesAnnouncingHeaderAndIDs.erase(it);
  362. lNodesAnnouncingHeaderAndIDs.push_back(nodeid);
  363. return;
  364. }
  365. }
  366. connman.ForNode(nodeid, [&connman](CNode* pfrom){
  367. bool fAnnounceUsingCMPCTBLOCK = false;
  368. uint64_t nCMPCTBLOCKVersion = (pfrom->GetLocalServices() & NODE_WITNESS) ? 2 : 1;
  369. if (lNodesAnnouncingHeaderAndIDs.size() >= 3) {
  370. // As per BIP152, we only get 3 of our peers to announce
  371. // blocks using compact encodings.
  372. connman.ForNode(lNodesAnnouncingHeaderAndIDs.front(), [&connman, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion](CNode* pnodeStop){
  373. connman.PushMessage(pnodeStop, CNetMsgMaker(pnodeStop->GetSendVersion()).Make(NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion));
  374. return true;
  375. });
  376. lNodesAnnouncingHeaderAndIDs.pop_front();
  377. }
  378. fAnnounceUsingCMPCTBLOCK = true;
  379. connman.PushMessage(pfrom, CNetMsgMaker(pfrom->GetSendVersion()).Make(NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion));
  380. lNodesAnnouncingHeaderAndIDs.push_back(pfrom->GetId());
  381. return true;
  382. });
  383. }
  384. }
  385. // Requires cs_main
  386. bool CanDirectFetch(const Consensus::Params &consensusParams)
  387. {
  388. return chainActive.Tip()->GetBlockTime() > GetAdjustedTime() - consensusParams.nPowTargetSpacing * 20;
  389. }
  390. // Requires cs_main
  391. bool PeerHasHeader(CNodeState *state, const CBlockIndex *pindex)
  392. {
  393. if (state->pindexBestKnownBlock && pindex == state->pindexBestKnownBlock->GetAncestor(pindex->nHeight))
  394. return true;
  395. if (state->pindexBestHeaderSent && pindex == state->pindexBestHeaderSent->GetAncestor(pindex->nHeight))
  396. return true;
  397. return false;
  398. }
  399. /** Find the last common ancestor two blocks have.
  400. * Both pa and pb must be non-NULL. */
  401. const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex* pb) {
  402. if (pa->nHeight > pb->nHeight) {
  403. pa = pa->GetAncestor(pb->nHeight);
  404. } else if (pb->nHeight > pa->nHeight) {
  405. pb = pb->GetAncestor(pa->nHeight);
  406. }
  407. while (pa != pb && pa && pb) {
  408. pa = pa->pprev;
  409. pb = pb->pprev;
  410. }
  411. // Eventually all chain branches meet at the genesis block.
  412. assert(pa == pb);
  413. return pa;
  414. }
  415. /** Update pindexLastCommonBlock and add not-in-flight missing successors to vBlocks, until it has
  416. * at most count entries. */
  417. void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller, const Consensus::Params& consensusParams) {
  418. if (count == 0)
  419. return;
  420. vBlocks.reserve(vBlocks.size() + count);
  421. CNodeState *state = State(nodeid);
  422. assert(state != NULL);
  423. // Make sure pindexBestKnownBlock is up to date, we'll need it.
  424. ProcessBlockAvailability(nodeid);
  425. if (state->pindexBestKnownBlock == NULL || state->pindexBestKnownBlock->nChainWork < chainActive.Tip()->nChainWork || state->pindexBestKnownBlock->nChainWork < UintToArith256(consensusParams.nMinimumChainWork)) {
  426. // This peer has nothing interesting.
  427. return;
  428. }
  429. if (state->pindexLastCommonBlock == NULL) {
  430. // Bootstrap quickly by guessing a parent of our best tip is the forking point.
  431. // Guessing wrong in either direction is not a problem.
  432. state->pindexLastCommonBlock = chainActive[std::min(state->pindexBestKnownBlock->nHeight, chainActive.Height())];
  433. }
  434. // If the peer reorganized, our previous pindexLastCommonBlock may not be an ancestor
  435. // of its current tip anymore. Go back enough to fix that.
  436. state->pindexLastCommonBlock = LastCommonAncestor(state->pindexLastCommonBlock, state->pindexBestKnownBlock);
  437. if (state->pindexLastCommonBlock == state->pindexBestKnownBlock)
  438. return;
  439. std::vector<const CBlockIndex*> vToFetch;
  440. const CBlockIndex *pindexWalk = state->pindexLastCommonBlock;
  441. // Never fetch further than the best block we know the peer has, or more than BLOCK_DOWNLOAD_WINDOW + 1 beyond the last
  442. // linked block we have in common with this peer. The +1 is so we can detect stalling, namely if we would be able to
  443. // download that next block if the window were 1 larger.
  444. int nWindowEnd = state->pindexLastCommonBlock->nHeight + BLOCK_DOWNLOAD_WINDOW;
  445. int nMaxHeight = std::min<int>(state->pindexBestKnownBlock->nHeight, nWindowEnd + 1);
  446. NodeId waitingfor = -1;
  447. while (pindexWalk->nHeight < nMaxHeight) {
  448. // Read up to 128 (or more, if more blocks than that are needed) successors of pindexWalk (towards
  449. // pindexBestKnownBlock) into vToFetch. We fetch 128, because CBlockIndex::GetAncestor may be as expensive
  450. // as iterating over ~100 CBlockIndex* entries anyway.
  451. int nToFetch = std::min(nMaxHeight - pindexWalk->nHeight, std::max<int>(count - vBlocks.size(), 128));
  452. vToFetch.resize(nToFetch);
  453. pindexWalk = state->pindexBestKnownBlock->GetAncestor(pindexWalk->nHeight + nToFetch);
  454. vToFetch[nToFetch - 1] = pindexWalk;
  455. for (unsigned int i = nToFetch - 1; i > 0; i--) {
  456. vToFetch[i - 1] = vToFetch[i]->pprev;
  457. }
  458. // Iterate over those blocks in vToFetch (in forward direction), adding the ones that
  459. // are not yet downloaded and not in flight to vBlocks. In the mean time, update
  460. // pindexLastCommonBlock as long as all ancestors are already downloaded, or if it's
  461. // already part of our chain (and therefore don't need it even if pruned).
  462. BOOST_FOREACH(const CBlockIndex* pindex, vToFetch) {
  463. if (!pindex->IsValid(BLOCK_VALID_TREE)) {
  464. // We consider the chain that this peer is on invalid.
  465. return;
  466. }
  467. if (!State(nodeid)->fHaveWitness && IsWitnessEnabled(pindex->pprev, consensusParams)) {
  468. // We wouldn't download this block or its descendants from this peer.
  469. return;
  470. }
  471. if (pindex->nStatus & BLOCK_HAVE_DATA || chainActive.Contains(pindex)) {
  472. if (pindex->nChainTx)
  473. state->pindexLastCommonBlock = pindex;
  474. } else if (mapBlocksInFlight.count(pindex->GetBlockHash()) == 0) {
  475. // The block is not already downloaded, and not yet in flight.
  476. if (pindex->nHeight > nWindowEnd) {
  477. // We reached the end of the window.
  478. if (vBlocks.size() == 0 && waitingfor != nodeid) {
  479. // We aren't able to fetch anything, but we would be if the download window was one larger.
  480. nodeStaller = waitingfor;
  481. }
  482. return;
  483. }
  484. vBlocks.push_back(pindex);
  485. if (vBlocks.size() == count) {
  486. return;
  487. }
  488. } else if (waitingfor == -1) {
  489. // This is the first already-in-flight block.
  490. waitingfor = mapBlocksInFlight[pindex->GetBlockHash()].first;
  491. }
  492. }
  493. }
  494. }
  495. } // anon namespace
  496. bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
  497. LOCK(cs_main);
  498. CNodeState *state = State(nodeid);
  499. if (state == NULL)
  500. return false;
  501. stats.nMisbehavior = state->nMisbehavior;
  502. stats.nSyncHeight = state->pindexBestKnownBlock ? state->pindexBestKnownBlock->nHeight : -1;
  503. stats.nCommonHeight = state->pindexLastCommonBlock ? state->pindexLastCommonBlock->nHeight : -1;
  504. BOOST_FOREACH(const QueuedBlock& queue, state->vBlocksInFlight) {
  505. if (queue.pindex)
  506. stats.vHeightInFlight.push_back(queue.pindex->nHeight);
  507. }
  508. return true;
  509. }
  510. void RegisterNodeSignals(CNodeSignals& nodeSignals)
  511. {
  512. nodeSignals.ProcessMessages.connect(&ProcessMessages);
  513. nodeSignals.SendMessages.connect(&SendMessages);
  514. nodeSignals.InitializeNode.connect(&InitializeNode);
  515. nodeSignals.FinalizeNode.connect(&FinalizeNode);
  516. }
  517. void UnregisterNodeSignals(CNodeSignals& nodeSignals)
  518. {
  519. nodeSignals.ProcessMessages.disconnect(&ProcessMessages);
  520. nodeSignals.SendMessages.disconnect(&SendMessages);
  521. nodeSignals.InitializeNode.disconnect(&InitializeNode);
  522. nodeSignals.FinalizeNode.disconnect(&FinalizeNode);
  523. }
  524. //////////////////////////////////////////////////////////////////////////////
  525. //
  526. // mapOrphanTransactions
  527. //
  528. void AddToCompactExtraTransactions(const CTransactionRef& tx)
  529. {
  530. size_t max_extra_txn = GetArg("-blockreconstructionextratxn", DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN);
  531. if (max_extra_txn <= 0)
  532. return;
  533. if (!vExtraTxnForCompact.size())
  534. vExtraTxnForCompact.resize(max_extra_txn);
  535. vExtraTxnForCompact[vExtraTxnForCompactIt] = std::make_pair(tx->GetWitnessHash(), tx);
  536. vExtraTxnForCompactIt = (vExtraTxnForCompactIt + 1) % max_extra_txn;
  537. }
  538. bool AddOrphanTx(const CTransactionRef& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
  539. {
  540. const uint256& hash = tx->GetHash();
  541. if (mapOrphanTransactions.count(hash))
  542. return false;
  543. // Ignore big transactions, to avoid a
  544. // send-big-orphans memory exhaustion attack. If a peer has a legitimate
  545. // large transaction with a missing parent then we assume
  546. // it will rebroadcast it later, after the parent transaction(s)
  547. // have been mined or received.
  548. // 100 orphans, each of which is at most 99,999 bytes big is
  549. // at most 10 megabytes of orphans and somewhat more byprev index (in the worst case):
  550. unsigned int sz = GetTransactionWeight(*tx);
  551. if (sz >= MAX_STANDARD_TX_WEIGHT)
  552. {
  553. LogPrint(BCLog::MEMPOOL, "ignoring large orphan tx (size: %u, hash: %s)\n", sz, hash.ToString());
  554. return false;
  555. }
  556. auto ret = mapOrphanTransactions.emplace(hash, COrphanTx{tx, peer, GetTime() + ORPHAN_TX_EXPIRE_TIME});
  557. assert(ret.second);
  558. BOOST_FOREACH(const CTxIn& txin, tx->vin) {
  559. mapOrphanTransactionsByPrev[txin.prevout].insert(ret.first);
  560. }
  561. AddToCompactExtraTransactions(tx);
  562. LogPrint(BCLog::MEMPOOL, "stored orphan tx %s (mapsz %u outsz %u)\n", hash.ToString(),
  563. mapOrphanTransactions.size(), mapOrphanTransactionsByPrev.size());
  564. return true;
  565. }
  566. int static EraseOrphanTx(uint256 hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
  567. {
  568. std::map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.find(hash);
  569. if (it == mapOrphanTransactions.end())
  570. return 0;
  571. BOOST_FOREACH(const CTxIn& txin, it->second.tx->vin)
  572. {
  573. auto itPrev = mapOrphanTransactionsByPrev.find(txin.prevout);
  574. if (itPrev == mapOrphanTransactionsByPrev.end())
  575. continue;
  576. itPrev->second.erase(it);
  577. if (itPrev->second.empty())
  578. mapOrphanTransactionsByPrev.erase(itPrev);
  579. }
  580. mapOrphanTransactions.erase(it);
  581. return 1;
  582. }
  583. void EraseOrphansFor(NodeId peer)
  584. {
  585. int nErased = 0;
  586. std::map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin();
  587. while (iter != mapOrphanTransactions.end())
  588. {
  589. std::map<uint256, COrphanTx>::iterator maybeErase = iter++; // increment to avoid iterator becoming invalid
  590. if (maybeErase->second.fromPeer == peer)
  591. {
  592. nErased += EraseOrphanTx(maybeErase->second.tx->GetHash());
  593. }
  594. }
  595. if (nErased > 0) LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx from peer=%d\n", nErased, peer);
  596. }
  597. unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
  598. {
  599. unsigned int nEvicted = 0;
  600. static int64_t nNextSweep;
  601. int64_t nNow = GetTime();
  602. if (nNextSweep <= nNow) {
  603. // Sweep out expired orphan pool entries:
  604. int nErased = 0;
  605. int64_t nMinExpTime = nNow + ORPHAN_TX_EXPIRE_TIME - ORPHAN_TX_EXPIRE_INTERVAL;
  606. std::map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin();
  607. while (iter != mapOrphanTransactions.end())
  608. {
  609. std::map<uint256, COrphanTx>::iterator maybeErase = iter++;
  610. if (maybeErase->second.nTimeExpire <= nNow) {
  611. nErased += EraseOrphanTx(maybeErase->second.tx->GetHash());
  612. } else {
  613. nMinExpTime = std::min(maybeErase->second.nTimeExpire, nMinExpTime);
  614. }
  615. }
  616. // Sweep again 5 minutes after the next entry that expires in order to batch the linear scan.
  617. nNextSweep = nMinExpTime + ORPHAN_TX_EXPIRE_INTERVAL;
  618. if (nErased > 0) LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx due to expiration\n", nErased);
  619. }
  620. while (mapOrphanTransactions.size() > nMaxOrphans)
  621. {
  622. // Evict a random orphan:
  623. uint256 randomhash = GetRandHash();
  624. std::map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.lower_bound(randomhash);
  625. if (it == mapOrphanTransactions.end())
  626. it = mapOrphanTransactions.begin();
  627. EraseOrphanTx(it->first);
  628. ++nEvicted;
  629. }
  630. return nEvicted;
  631. }
  632. // Requires cs_main.
  633. void Misbehaving(NodeId pnode, int howmuch)
  634. {
  635. if (howmuch == 0)
  636. return;
  637. CNodeState *state = State(pnode);
  638. if (state == NULL)
  639. return;
  640. state->nMisbehavior += howmuch;
  641. int banscore = GetArg("-banscore", DEFAULT_BANSCORE_THRESHOLD);
  642. if (state->nMisbehavior >= banscore && state->nMisbehavior - howmuch < banscore)
  643. {
  644. LogPrintf("%s: %s peer=%d (%d -> %d) BAN THRESHOLD EXCEEDED\n", __func__, state->name, pnode, state->nMisbehavior-howmuch, state->nMisbehavior);
  645. state->fShouldBan = true;
  646. } else
  647. LogPrintf("%s: %s peer=%d (%d -> %d)\n", __func__, state->name, pnode, state->nMisbehavior-howmuch, state->nMisbehavior);
  648. }
  649. //////////////////////////////////////////////////////////////////////////////
  650. //
  651. // blockchain -> download logic notification
  652. //
  653. PeerLogicValidation::PeerLogicValidation(CConnman* connmanIn) : connman(connmanIn) {
  654. // Initialize global variables that cannot be constructed at startup.
  655. recentRejects.reset(new CRollingBloomFilter(120000, 0.000001));
  656. }
  657. void PeerLogicValidation::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex, const std::vector<CTransactionRef>& vtxConflicted) {
  658. LOCK(cs_main);
  659. std::vector<uint256> vOrphanErase;
  660. for (const CTransactionRef& ptx : pblock->vtx) {
  661. const CTransaction& tx = *ptx;
  662. // Which orphan pool entries must we evict?
  663. for (size_t j = 0; j < tx.vin.size(); j++) {
  664. auto itByPrev = mapOrphanTransactionsByPrev.find(tx.vin[j].prevout);
  665. if (itByPrev == mapOrphanTransactionsByPrev.end()) continue;
  666. for (auto mi = itByPrev->second.begin(); mi != itByPrev->second.end(); ++mi) {
  667. const CTransaction& orphanTx = *(*mi)->second.tx;
  668. const uint256& orphanHash = orphanTx.GetHash();
  669. vOrphanErase.push_back(orphanHash);
  670. }
  671. }
  672. }
  673. // Erase orphan transactions include or precluded by this block
  674. if (vOrphanErase.size()) {
  675. int nErased = 0;
  676. BOOST_FOREACH(uint256 &orphanHash, vOrphanErase) {
  677. nErased += EraseOrphanTx(orphanHash);
  678. }
  679. LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx included or conflicted by block\n", nErased);
  680. }
  681. }
  682. // All of the following cache a recent block, and are protected by cs_most_recent_block
  683. static CCriticalSection cs_most_recent_block;
  684. static std::shared_ptr<const CBlock> most_recent_block;
  685. static std::shared_ptr<const CBlockHeaderAndShortTxIDs> most_recent_compact_block;
  686. static uint256 most_recent_block_hash;
  687. static bool fWitnessesPresentInMostRecentCompactBlock;
  688. void PeerLogicValidation::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) {
  689. std::shared_ptr<const CBlockHeaderAndShortTxIDs> pcmpctblock = std::make_shared<const CBlockHeaderAndShortTxIDs> (*pblock, true);
  690. const CNetMsgMaker msgMaker(PROTOCOL_VERSION);
  691. LOCK(cs_main);
  692. static int nHighestFastAnnounce = 0;
  693. if (pindex->nHeight <= nHighestFastAnnounce)
  694. return;
  695. nHighestFastAnnounce = pindex->nHeight;
  696. bool fWitnessEnabled = IsWitnessEnabled(pindex->pprev, Params().GetConsensus());
  697. uint256 hashBlock(pblock->GetHash());
  698. {
  699. LOCK(cs_most_recent_block);
  700. most_recent_block_hash = hashBlock;
  701. most_recent_block = pblock;
  702. most_recent_compact_block = pcmpctblock;
  703. fWitnessesPresentInMostRecentCompactBlock = fWitnessEnabled;
  704. }
  705. connman->ForEachNode([this, &pcmpctblock, pindex, &msgMaker, fWitnessEnabled, &hashBlock](CNode* pnode) {
  706. // TODO: Avoid the repeated-serialization here
  707. if (pnode->nVersion < INVALID_CB_NO_BAN_VERSION || pnode->fDisconnect)
  708. return;
  709. ProcessBlockAvailability(pnode->GetId());
  710. CNodeState &state = *State(pnode->GetId());
  711. // If the peer has, or we announced to them the previous block already,
  712. // but we don't think they have this one, go ahead and announce it
  713. if (state.fPreferHeaderAndIDs && (!fWitnessEnabled || state.fWantsCmpctWitness) &&
  714. !PeerHasHeader(&state, pindex) && PeerHasHeader(&state, pindex->pprev)) {
  715. LogPrint(BCLog::NET, "%s sending header-and-ids %s to peer=%d\n", "PeerLogicValidation::NewPoWValidBlock",
  716. hashBlock.ToString(), pnode->GetId());
  717. connman->PushMessage(pnode, msgMaker.Make(NetMsgType::CMPCTBLOCK, *pcmpctblock));
  718. state.pindexBestHeaderSent = pindex;
  719. }
  720. });
  721. }
  722. void PeerLogicValidation::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {
  723. const int nNewHeight = pindexNew->nHeight;
  724. connman->SetBestHeight(nNewHeight);
  725. if (!fInitialDownload) {
  726. // Find the hashes of all blocks that weren't previously in the best chain.
  727. std::vector<uint256> vHashes;
  728. const CBlockIndex *pindexToAnnounce = pindexNew;
  729. while (pindexToAnnounce != pindexFork) {
  730. vHashes.push_back(pindexToAnnounce->GetBlockHash());
  731. pindexToAnnounce = pindexToAnnounce->pprev;
  732. if (vHashes.size() == MAX_BLOCKS_TO_ANNOUNCE) {
  733. // Limit announcements in case of a huge reorganization.
  734. // Rely on the peer's synchronization mechanism in that case.
  735. break;
  736. }
  737. }
  738. // Relay inventory, but don't relay old inventory during initial block download.
  739. connman->ForEachNode([nNewHeight, &vHashes](CNode* pnode) {
  740. if (nNewHeight > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : 0)) {
  741. BOOST_REVERSE_FOREACH(const uint256& hash, vHashes) {
  742. pnode->PushBlockHash(hash);
  743. }
  744. }
  745. });
  746. connman->WakeMessageHandler();
  747. }
  748. nTimeBestReceived = GetTime();
  749. }
  750. void PeerLogicValidation::BlockChecked(const CBlock& block, const CValidationState& state) {
  751. LOCK(cs_main);
  752. const uint256 hash(block.GetHash());
  753. std::map<uint256, std::pair<NodeId, bool>>::iterator it = mapBlockSource.find(hash);
  754. int nDoS = 0;
  755. if (state.IsInvalid(nDoS)) {
  756. // Don't send reject message with code 0 or an internal reject code.
  757. if (it != mapBlockSource.end() && State(it->second.first) && state.GetRejectCode() > 0 && state.GetRejectCode() < REJECT_INTERNAL) {
  758. CBlockReject reject = {(unsigned char)state.GetRejectCode(), state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), hash};
  759. State(it->second.first)->rejects.push_back(reject);
  760. if (nDoS > 0 && it->second.second)
  761. Misbehaving(it->second.first, nDoS);
  762. }
  763. }
  764. // Check that:
  765. // 1. The block is valid
  766. // 2. We're not in initial block download
  767. // 3. This is currently the best block we're aware of. We haven't updated
  768. // the tip yet so we have no way to check this directly here. Instead we
  769. // just check that there are currently no other blocks in flight.
  770. else if (state.IsValid() &&
  771. !IsInitialBlockDownload() &&
  772. mapBlocksInFlight.count(hash) == mapBlocksInFlight.size()) {
  773. if (it != mapBlockSource.end()) {
  774. MaybeSetPeerAsAnnouncingHeaderAndIDs(it->second.first, *connman);
  775. }
  776. }
  777. if (it != mapBlockSource.end())
  778. mapBlockSource.erase(it);
  779. }
  780. //////////////////////////////////////////////////////////////////////////////
  781. //
  782. // Messages
  783. //
  784. bool static AlreadyHave(const CInv& inv) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
  785. {
  786. switch (inv.type)
  787. {
  788. case MSG_TX:
  789. case MSG_WITNESS_TX:
  790. {
  791. assert(recentRejects);
  792. if (chainActive.Tip()->GetBlockHash() != hashRecentRejectsChainTip)
  793. {
  794. // If the chain tip has changed previously rejected transactions
  795. // might be now valid, e.g. due to a nLockTime'd tx becoming valid,
  796. // or a double-spend. Reset the rejects filter and give those
  797. // txs a second chance.
  798. hashRecentRejectsChainTip = chainActive.Tip()->GetBlockHash();
  799. recentRejects->reset();
  800. }
  801. // Use pcoinsTip->HaveCoinsInCache as a quick approximation to exclude
  802. // requesting or processing some txs which have already been included in a block
  803. return recentRejects->contains(inv.hash) ||
  804. mempool.exists(inv.hash) ||
  805. mapOrphanTransactions.count(inv.hash) ||
  806. pcoinsTip->HaveCoinsInCache(inv.hash);
  807. }
  808. case MSG_BLOCK:
  809. case MSG_WITNESS_BLOCK:
  810. return mapBlockIndex.count(inv.hash);
  811. }
  812. // Don't know what it is, just say we already got one
  813. return true;
  814. }
  815. static void RelayTransaction(const CTransaction& tx, CConnman& connman)
  816. {
  817. CInv inv(MSG_TX, tx.GetHash());
  818. connman.ForEachNode([&inv](CNode* pnode)
  819. {
  820. pnode->PushInventory(inv);
  821. });
  822. }
  823. static void RelayAddress(const CAddress& addr, bool fReachable, CConnman& connman)
  824. {
  825. unsigned int nRelayNodes = fReachable ? 2 : 1; // limited relaying of addresses outside our network(s)
  826. // Relay to a limited number of other nodes
  827. // Use deterministic randomness to send to the same nodes for 24 hours
  828. // at a time so the addrKnowns of the chosen nodes prevent repeats
  829. uint64_t hashAddr = addr.GetHash();
  830. const CSipHasher hasher = connman.GetDeterministicRandomizer(RANDOMIZER_ID_ADDRESS_RELAY).Write(hashAddr << 32).Write((GetTime() + hashAddr) / (24*60*60));
  831. FastRandomContext insecure_rand;
  832. std::array<std::pair<uint64_t, CNode*>,2> best{{{0, nullptr}, {0, nullptr}}};
  833. assert(nRelayNodes <= best.size());
  834. auto sortfunc = [&best, &hasher, nRelayNodes](CNode* pnode) {
  835. if (pnode->nVersion >= CADDR_TIME_VERSION) {
  836. uint64_t hashKey = CSipHasher(hasher).Write(pnode->GetId()).Finalize();
  837. for (unsigned int i = 0; i < nRelayNodes; i++) {
  838. if (hashKey > best[i].first) {
  839. std::copy(best.begin() + i, best.begin() + nRelayNodes - 1, best.begin() + i + 1);
  840. best[i] = std::make_pair(hashKey, pnode);
  841. break;
  842. }
  843. }
  844. }
  845. };
  846. auto pushfunc = [&addr, &best, nRelayNodes, &insecure_rand] {
  847. for (unsigned int i = 0; i < nRelayNodes && best[i].first != 0; i++) {
  848. best[i].second->PushAddress(addr, insecure_rand);
  849. }
  850. };
  851. connman.ForEachNodeThen(std::move(sortfunc), std::move(pushfunc));
  852. }
  853. void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParams, CConnman& connman, const std::atomic<bool>& interruptMsgProc)
  854. {
  855. std::deque<CInv>::iterator it = pfrom->vRecvGetData.begin();
  856. std::vector<CInv> vNotFound;
  857. const CNetMsgMaker msgMaker(pfrom->GetSendVersion());
  858. LOCK(cs_main);
  859. while (it != pfrom->vRecvGetData.end()) {
  860. // Don't bother if send buffer is too full to respond anyway
  861. if (pfrom->fPauseSend)
  862. break;
  863. const CInv &inv = *it;
  864. {
  865. if (interruptMsgProc)
  866. return;
  867. it++;
  868. if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK || inv.type == MSG_CMPCT_BLOCK || inv.type == MSG_WITNESS_BLOCK)
  869. {
  870. bool send = false;
  871. BlockMap::iterator mi = mapBlockIndex.find(inv.hash);
  872. std::shared_ptr<const CBlock> a_recent_block;
  873. std::shared_ptr<const CBlockHeaderAndShortTxIDs> a_recent_compact_block;
  874. bool fWitnessesPresentInARecentCompactBlock;
  875. {
  876. LOCK(cs_most_recent_block);
  877. a_recent_block = most_recent_block;
  878. a_recent_compact_block = most_recent_compact_block;
  879. fWitnessesPresentInARecentCompactBlock = fWitnessesPresentInMostRecentCompactBlock;
  880. }
  881. if (mi != mapBlockIndex.end())
  882. {
  883. if (mi->second->nChainTx && !mi->second->IsValid(BLOCK_VALID_SCRIPTS) &&
  884. mi->second->IsValid(BLOCK_VALID_TREE)) {
  885. // If we have the block and all of its parents, but have not yet validated it,
  886. // we might be in the middle of connecting it (ie in the unlock of cs_main
  887. // before ActivateBestChain but after AcceptBlock).
  888. // In this case, we need to run ActivateBestChain prior to checking the relay
  889. // conditions below.
  890. CValidationState dummy;
  891. ActivateBestChain(dummy, Params(), a_recent_block);
  892. }
  893. if (chainActive.Contains(mi->second)) {
  894. send = true;
  895. } else {
  896. static const int nOneMonth = 30 * 24 * 60 * 60;
  897. // To prevent fingerprinting attacks, only send blocks outside of the active
  898. // chain if they are valid, and no more than a month older (both in time, and in
  899. // best equivalent proof of work) than the best header chain we know about.
  900. send = mi->second->IsValid(BLOCK_VALID_SCRIPTS) && (pindexBestHeader != NULL) &&
  901. (pindexBestHeader->GetBlockTime() - mi->second->GetBlockTime() < nOneMonth) &&
  902. (GetBlockProofEquivalentTime(*pindexBestHeader, *mi->second, *pindexBestHeader, consensusParams) < nOneMonth);
  903. if (!send) {
  904. LogPrintf("%s: ignoring request from peer=%i for old block that isn't in the main chain\n", __func__, pfrom->GetId());
  905. }
  906. }
  907. }
  908. // disconnect node in case we have reached the outbound limit for serving historical blocks
  909. // never disconnect whitelisted nodes
  910. static const int nOneWeek = 7 * 24 * 60 * 60; // assume > 1 week = historical
  911. if (send && connman.OutboundTargetReached(true) && ( ((pindexBestHeader != NULL) && (pindexBestHeader->GetBlockTime() - mi->second->GetBlockTime() > nOneWeek)) || inv.type == MSG_FILTERED_BLOCK) && !pfrom->fWhitelisted)
  912. {
  913. LogPrint(BCLog::NET, "historical block serving limit reached, disconnect peer=%d\n", pfrom->GetId());
  914. //disconnect node
  915. pfrom->fDisconnect = true;
  916. send = false;
  917. }
  918. // Pruned nodes may have deleted the block, so check whether
  919. // it's available before trying to send.
  920. if (send && (mi->second->nStatus & BLOCK_HAVE_DATA))
  921. {
  922. std::shared_ptr<const CBlock> pblock;
  923. if (a_recent_block && a_recent_block->GetHash() == (*mi).second->GetBlockHash()) {
  924. pblock = a_recent_block;
  925. } else {
  926. // Send block from disk
  927. std::shared_ptr<CBlock> pblockRead = std::make_shared<CBlock>();
  928. if (!ReadBlockFromDisk(*pblockRead, (*mi).second, consensusParams))
  929. assert(!"cannot load block from disk");
  930. pblock = pblockRead;
  931. }
  932. if (inv.type == MSG_BLOCK)
  933. connman.PushMessage(pfrom, msgMaker.Make(SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::BLOCK, *pblock));
  934. else if (inv.type == MSG_WITNESS_BLOCK)
  935. connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::BLOCK, *pblock));
  936. else if (inv.type == MSG_FILTERED_BLOCK)
  937. {
  938. bool sendMerkleBlock = false;
  939. CMerkleBlock merkleBlock;
  940. {
  941. LOCK(pfrom->cs_filter);
  942. if (pfrom->pfilter) {
  943. sendMerkleBlock = true;
  944. merkleBlock = CMerkleBlock(*pblock, *pfrom->pfilter);
  945. }
  946. }
  947. if (sendMerkleBlock) {
  948. connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::MERKLEBLOCK, merkleBlock));
  949. // CMerkleBlock just contains hashes, so also push any transactions in the block the client did not see
  950. // This avoids hurting performance by pointlessly requiring a round-trip
  951. // Note that there is currently no way for a node to request any single transactions we didn't send here -
  952. // they must either disconnect and retry or request the full block.
  953. // Thus, the protocol spec specified allows for us to provide duplicate txn here,
  954. // however we MUST always provide at least what the remote peer needs
  955. typedef std::pair<unsigned int, uint256> PairType;
  956. BOOST_FOREACH(PairType& pair, merkleBlock.vMatchedTxn)
  957. connman.PushMessage(pfrom, msgMaker.Make(SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::TX, *pblock->vtx[pair.first]));
  958. }
  959. // else
  960. // no response
  961. }
  962. else if (inv.type == MSG_CMPCT_BLOCK)
  963. {
  964. // If a peer is asking for old blocks, we're almost guaranteed
  965. // they won't have a useful mempool to match against a compact block,
  966. // and we don't feel like constructing the object for them, so
  967. // instead we respond with the full, non-compact block.
  968. bool fPeerWantsWitness = State(pfrom->GetId())->fWantsCmpctWitness;
  969. int nSendFlags = fPeerWantsWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS;
  970. if (CanDirectFetch(consensusParams) && mi->second->nHeight >= chainActive.Height() - MAX_CMPCTBLOCK_DEPTH) {
  971. if ((fPeerWantsWitness || !fWitnessesPresentInARecentCompactBlock) && a_recent_compact_block && a_recent_compact_block->header.GetHash() == mi->second->GetBlockHash()) {
  972. connman.PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, *a_recent_compact_block));
  973. } else {
  974. CBlockHeaderAndShortTxIDs cmpctblock(*pblock, fPeerWantsWitness);
  975. connman.PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, cmpctblock));
  976. }
  977. } else {
  978. connman.PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::BLOCK, *pblock));
  979. }
  980. }
  981. // Trigger the peer node to send a getblocks request for the next batch of inventory
  982. if (inv.hash == pfrom->hashContinue)
  983. {
  984. // Bypass PushInventory, this must send even if redundant,
  985. // and we want it right after the last block so they don't
  986. // wait for other stuff first.
  987. std::vector<CInv> vInv;
  988. vInv.push_back(CInv(MSG_BLOCK, chainActive.Tip()->GetBlockHash()));
  989. connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::INV, vInv));
  990. pfrom->hashContinue.SetNull();
  991. }
  992. }
  993. }
  994. else if (inv.type == MSG_TX || inv.type == MSG_WITNESS_TX)
  995. {
  996. // Send stream from relay memory
  997. bool push = false;
  998. auto mi = mapRelay.find(inv.hash);
  999. int nSendFlags = (inv.type == MSG_TX ? SERIALIZE_TRANSACTION_NO_WITNESS : 0);
  1000. if (mi != mapRelay.end()) {
  1001. connman.PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::TX, *mi->second));
  1002. push = true;
  1003. } else if (pfrom->timeLastMempoolReq) {
  1004. auto txinfo = mempool.info(inv.hash);
  1005. // To protect privacy, do not answer getdata using the mempool when
  1006. // that TX couldn't have been INVed in reply to a MEMPOOL request.
  1007. if (txinfo.tx && txinfo.nTime <= pfrom->timeLastMempoolReq) {
  1008. connman.PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::TX, *txinfo.tx));
  1009. push = true;
  1010. }
  1011. }
  1012. if (!push) {
  1013. vNotFound.push_back(inv);
  1014. }
  1015. }
  1016. // Track requests for our stuff.
  1017. GetMainSignals().Inventory(inv.hash);
  1018. if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK || inv.type == MSG_CMPCT_BLOCK || inv.type == MSG_WITNESS_BLOCK)
  1019. break;
  1020. }
  1021. }
  1022. pfrom->vRecvGetData.erase(pfrom->vRecvGetData.begin(), it);
  1023. if (!vNotFound.empty()) {
  1024. // Let the peer know that we didn't find what it asked for, so it doesn't
  1025. // have to wait around forever. Currently only SPV clients actually care
  1026. // about this message: it's needed when they are recursively walking the
  1027. // dependencies of relevant unconfirmed transactions. SPV clients want to
  1028. // do that because they want to know about (and store and rebroadcast and
  1029. // risk analyze) the dependencies of transactions relevant to them, without
  1030. // having to download the entire memory pool.
  1031. connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::NOTFOUND, vNotFound));
  1032. }
  1033. }
  1034. uint32_t GetFetchFlags(CNode* pfrom) {
  1035. uint32_t nFetchFlags = 0;
  1036. if ((pfrom->GetLocalServices() & NODE_WITNESS) && State(pfrom->GetId())->fHaveWitness) {
  1037. nFetchFlags |= MSG_WITNESS_FLAG;
  1038. }
  1039. return nFetchFlags;
  1040. }
  1041. inline void static SendBlockTransactions(const CBlock& block, const BlockTransactionsRequest& req, CNode* pfrom, CConnman& connman) {
  1042. BlockTransactions resp(req);
  1043. for (size_t i = 0; i < req.indexes.size(); i++) {
  1044. if (req.indexes[i] >= block.vtx.size()) {
  1045. LOCK(cs_main);
  1046. Misbehaving(pfrom->GetId(), 100);
  1047. LogPrintf("Peer %d sent us a getblocktxn with out-of-bounds tx indices", pfrom->GetId());
  1048. return;
  1049. }
  1050. resp.txn[i] = block.vtx[req.indexes[i]];
  1051. }
  1052. LOCK(cs_main);
  1053. const CNetMsgMaker msgMaker(pfrom->GetSendVersion());
  1054. int nSendFlags = State(pfrom->GetId())->fWantsCmpctWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS;
  1055. connman.PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::BLOCKTXN, resp));
  1056. }
  1057. bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, int64_t nTimeReceived, const CChainParams& chainparams, CConnman& connman, const std::atomic<bool>& interruptMsgProc)
  1058. {
  1059. LogPrint(BCLog::NET, "received: %s (%u bytes) peer=%d\n", SanitizeString(strCommand), vRecv.size(), pfrom->GetId());
  1060. if (IsArgSet("-dropmessagestest") && GetRand(GetArg("-dropmessagestest", 0)) == 0)
  1061. {
  1062. LogPrintf("dropmessagestest DROPPING RECV MESSAGE\n");
  1063. return true;
  1064. }
  1065. if (!(pfrom->GetLocalServices() & NODE_BLOOM) &&
  1066. (strCommand == NetMsgType::FILTERLOAD ||
  1067. strCommand == NetMsgType::FILTERADD))
  1068. {
  1069. if (pfrom->nVersion >= NO_BLOOM_VERSION) {
  1070. LOCK(cs_main);
  1071. Misbehaving(pfrom->GetId(), 100);
  1072. return false;
  1073. } else {
  1074. pfrom->fDisconnect = true;
  1075. return false;
  1076. }
  1077. }
  1078. if (strCommand == NetMsgType::REJECT)
  1079. {
  1080. if (LogAcceptCategory(BCLog::NET)) {
  1081. try {
  1082. std::string strMsg; unsigned char ccode; std::string strReason;
  1083. vRecv >> LIMITED_STRING(strMsg, CMessageHeader::COMMAND_SIZE) >> ccode >> LIMITED_STRING(strReason, MAX_REJECT_MESSAGE_LENGTH);
  1084. std::ostringstream ss;
  1085. ss << strMsg << " code " << itostr(ccode) << ": " << strReason;
  1086. if (strMsg == NetMsgType::BLOCK || strMsg == NetMsgType::TX)
  1087. {
  1088. uint256 hash;
  1089. vRecv >> hash;
  1090. ss << ": hash " << hash.ToString();
  1091. }
  1092. LogPrint(BCLog::NET, "Reject %s\n", SanitizeString(ss.str()));
  1093. } catch (const std::ios_base::failure&) {
  1094. // Avoid feedback loops by preventing reject messages from triggering a new reject message.
  1095. LogPrint(BCLog::NET, "Unparseable reject message received\n");
  1096. }
  1097. }
  1098. }
  1099. else if (strCommand == NetMsgType::VERSION)
  1100. {
  1101. // Each connection can only send one version message
  1102. if (pfrom->nVersion != 0)
  1103. {
  1104. connman.PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, strCommand, REJECT_DUPLICATE, std::string("Duplicate version message")));
  1105. LOCK(cs_main);
  1106. Misbehaving(pfrom->GetId(), 1);
  1107. return false;
  1108. }
  1109. int64_t nTime;
  1110. CAddress addrMe;
  1111. CAddress addrFrom;
  1112. uint64_t nNonce = 1;
  1113. uint64_t nServiceInt;
  1114. ServiceFlags nServices;
  1115. int nVersion;
  1116. int nSendVersion;
  1117. std::string strSubVer;
  1118. std::string cleanSubVer;
  1119. int nStartingHeight = -1;
  1120. bool fRelay = true;
  1121. vRecv >> nVersion >> nServiceInt >> nTime >> addrMe;
  1122. nSendVersion = std::min(nVersion, PROTOCOL_VERSION);
  1123. nServices = ServiceFlags(nServiceInt);
  1124. if (!pfrom->fInbound)
  1125. {
  1126. connman.SetServices(pfrom->addr, nServices);
  1127. }
  1128. if (pfrom->nServicesExpected & ~nServices)
  1129. {
  1130. LogPrint(BCLog::NET, "peer=%d does not offer the expected services (%08x offered, %08x expected); disconnecting\n", pfrom->GetId(), nServices, pfrom->nServicesExpected);
  1131. connman.PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, strCommand, REJECT_NONSTANDARD,
  1132. strprintf("Expected to offer services %08x", pfrom->nServicesExpected)));
  1133. pfrom->fDisconnect = true;
  1134. return false;
  1135. }
  1136. if (nVersion < MIN_PEER_PROTO_VERSION)
  1137. {
  1138. // disconnect from peers older than this proto version
  1139. LogPrintf("peer=%d using obsolete version %i; disconnecting\n", pfrom->GetId(), nVersion);
  1140. connman.PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, strCommand, REJECT_OBSOLETE,
  1141. strprintf("Version must be %d or greater", MIN_PEER_PROTO_VERSION)));
  1142. pfrom->fDisconnect = true;
  1143. return false;
  1144. }
  1145. if (nVersion == 10300)
  1146. nVersion = 300;
  1147. if (!vRecv.empty())
  1148. vRecv >> addrFrom >> nNonce;
  1149. if (!vRecv.empty()) {
  1150. vRecv >> LIMITED_STRING(strSubVer, MAX_SUBVERSION_LENGTH);
  1151. cleanSubVer = SanitizeString(strSubVer);
  1152. }
  1153. if (!vRecv.empty()) {
  1154. vRecv >> nStartingHeight;
  1155. }
  1156. if (!vRecv.empty())
  1157. vRecv >> fRelay;
  1158. // Disconnect if we connected to ourself
  1159. if (pfrom->fInbound && !connman.CheckIncomingNonce(nNonce))
  1160. {
  1161. LogPrintf("connected to self at %s, disconnecting\n", pfrom->addr.ToString());
  1162. pfrom->fDisconnect = true;
  1163. return true;
  1164. }
  1165. if (pfrom->fInbound && addrMe.IsRoutable())
  1166. {
  1167. SeenLocal(addrMe);
  1168. }
  1169. // Be shy and don't send version until we hear
  1170. if (pfrom->fInbound)
  1171. PushNodeVersion(pfrom, connman, GetAdjustedTime());
  1172. connman.PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::VERACK));
  1173. pfrom->nServices = nServices;
  1174. pfrom->SetAddrLocal(addrMe);
  1175. {
  1176. LOCK(pfrom->cs_SubVer);
  1177. pfrom->strSubVer = strSubVer;
  1178. pfrom->cleanSubVer = cleanSubVer;
  1179. }
  1180. pfrom->nStartingHeight = nStartingHeight;
  1181. pfrom->fClient = !(nServices & NODE_NETWORK);
  1182. {
  1183. LOCK(pfrom->cs_filter);
  1184. pfrom->fRelayTxes = fRelay; // set to true after we get the first filter* message
  1185. }
  1186. // Change version
  1187. pfrom->SetSendVersion(nSendVersion);
  1188. pfrom->nVersion = nVersion;
  1189. if((nServices & NODE_WITNESS))
  1190. {
  1191. LOCK(cs_main);
  1192. State(pfrom->GetId())->fHaveWitness = true;
  1193. }
  1194. // Potentially mark this peer as a preferred download peer.
  1195. {
  1196. LOCK(cs_main);
  1197. UpdatePreferredDownload(pfrom, State(pfrom->GetId()));
  1198. }
  1199. if (!pfrom->fInbound)
  1200. {
  1201. // Advertise our address
  1202. if (fListen && !IsInitialBlockDownload())
  1203. {
  1204. CAddress addr = GetLocalAddress(&pfrom->addr, pfrom->GetLocalServices());
  1205. FastRandomContext insecure_rand;
  1206. if (addr.IsRoutable())
  1207. {
  1208. LogPrint(BCLog::NET, "ProcessMessages: advertising address %s\n", addr.ToString());
  1209. pfrom->PushAddress(addr, insecure_rand);
  1210. } else if (IsPeerAddrLocalGood(pfrom)) {
  1211. addr.SetIP(addrMe);
  1212. LogPrint(BCLog::NET, "ProcessMessages: advertising address %s\n", addr.ToString());
  1213. pfrom->PushAddress(addr, insecure_rand);
  1214. }
  1215. }
  1216. // Get recent addresses
  1217. if (pfrom->fOneShot || pfrom->nVersion >= CADDR_TIME_VERSION || connman.GetAddressCount() < 1000)
  1218. {
  1219. connman.PushMessage(pfrom, CNetMsgMaker(nSendVersion).Make(NetMsgType::GETADDR));
  1220. pfrom->fGetAddr = true;
  1221. }
  1222. connman.MarkAddressGood(pfrom->addr);
  1223. }
  1224. std::string remoteAddr;
  1225. if (fLogIPs)
  1226. remoteAddr = ", peeraddr=" + pfrom->addr.ToString();
  1227. LogPrintf("receive version message: %s: version %d, blocks=%d, us=%s, peer=%d%s\n",
  1228. cleanSubVer, pfrom->nVersion,
  1229. pfrom->nStartingHeight, addrMe.ToString(), pfrom->GetId(),
  1230. remoteAddr);
  1231. int64_t nTimeOffset = nTime - GetTime();
  1232. pfrom->nTimeOffset = nTimeOffset;
  1233. AddTimeData(pfrom->addr, nTimeOffset);
  1234. // If the peer is old enough to have the old alert system, send it the final alert.
  1235. if (pfrom->nVersion <= 70012) {
  1236. CDataStream finalAlert(ParseHex("60010000000000000000000000ffffff7f00000000ffffff7ffeffff7f01ffffff7f00000000ffffff7f00ffffff7f002f555247454e543a20416c657274206b657920636f6d70726f6d697365642c2075706772616465207265717569726564004630440220653febd6410f470f6bae11cad19c48413becb1ac2c17f908fd0fd53bdc3abd5202206d0e9c96fe88d4a0f01ed9dedae2b6f9e00da94cad0fecaae66ecf689bf71b50"), SER_NETWORK, PROTOCOL_VERSION);
  1237. connman.PushMessage(pfrom, CNetMsgMaker(nSendVersion).Make("alert", finalAlert));
  1238. }
  1239. // Feeler connections exist only to verify if address is online.
  1240. if (pfrom->fFeeler) {
  1241. assert(pfrom->fInbound == false);
  1242. pfrom->fDisconnect = true;
  1243. }
  1244. return true;
  1245. }
  1246. else if (pfrom->nVersion == 0)
  1247. {
  1248. // Must have a version message before anything else
  1249. LOCK(cs_main);
  1250. Misbehaving(pfrom->GetId(), 1);
  1251. return false;
  1252. }
  1253. // At this point, the outgoing message serialization version can't change.
  1254. const CNetMsgMaker msgMaker(pfrom->GetSendVersion());
  1255. if (strCommand == NetMsgType::VERACK)
  1256. {
  1257. pfrom->SetRecvVersion(std::min(pfrom->nVersion.load(), PROTOCOL_VERSION));
  1258. if (!pfrom->fInbound) {
  1259. // Mark this node as currently connected, so we update its timestamp later.
  1260. LOCK(cs_main);
  1261. State(pfrom->GetId())->fCurrentlyConnected = true;
  1262. }
  1263. if (pfrom->nVersion >= SENDHEADERS_VERSION) {
  1264. // Tell our peer we prefer to receive headers rather than inv's
  1265. // We send this to non-NODE NETWORK peers as well, because even
  1266. // non-NODE NETWORK peers can announce blocks (such as pruning
  1267. // nodes)
  1268. connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::SENDHEADERS));
  1269. }
  1270. if (pfrom->nVersion >= SHORT_IDS_BLOCKS_VERSION) {
  1271. // Tell our peer we are willing to provide version 1 or 2 cmpctblocks
  1272. // However, we do not request new block announcements using
  1273. // cmpctblock messages.
  1274. // We send this to non-NODE NETWORK peers as well, because
  1275. // they may wish to request compact blocks from us
  1276. bool fAnnounceUsingCMPCTBLOCK = false;
  1277. uint64_t nCMPCTBLOCKVersion = 2;
  1278. if (pfrom->GetLocalServices() & NODE_WITNESS)
  1279. connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion));
  1280. nCMPCTBLOCKVersion = 1;
  1281. connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion));
  1282. }
  1283. pfrom->fSuccessfullyConnected = true;
  1284. }
  1285. else if (!pfrom->fSuccessfullyConnected)
  1286. {
  1287. // Must have a verack message before anything else
  1288. LOCK(cs_main);
  1289. Misbehaving(pfrom->GetId(), 1);
  1290. return false;
  1291. }
  1292. else if (strCommand == NetMsgType::ADDR)
  1293. {
  1294. std::vector<CAddress> vAddr;
  1295. vRecv >> vAddr;
  1296. // Don't want addr from older versions unless seeding
  1297. if (pfrom->nVersion < CADDR_TIME_VERSION && connman.GetAddressCount() > 1000)
  1298. return true;
  1299. if (vAddr.size() > 1000)
  1300. {
  1301. LOCK(cs_main);
  1302. Misbehaving(pfrom->GetId(), 20);
  1303. return error("message addr size() = %u", vAddr.size());
  1304. }
  1305. // Store the new addresses
  1306. std::vector<CAddress> vAddrOk;
  1307. int64_t nNow = GetAdjustedTime();
  1308. int64_t nSince = nNow - 10 * 60;
  1309. BOOST_FOREACH(CAddress& addr, vAddr)
  1310. {
  1311. if (interruptMsgProc)
  1312. return true;
  1313. if ((addr.nServices & REQUIRED_SERVICES) != REQUIRED_SERVICES)
  1314. continue;
  1315. if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60)
  1316. addr.nTime = nNow - 5 * 24 * 60 * 60;
  1317. pfrom->AddAddressKnown(addr);
  1318. bool fReachable = IsReachable(addr);
  1319. if (addr.nTime > nSince && !pfrom->fGetAddr && vAddr.size() <= 10 && addr.IsRoutable())
  1320. {
  1321. // Relay to a limited number of other nodes
  1322. RelayAddress(addr, fReachable, connman);
  1323. }
  1324. // Do not store addresses outside our network
  1325. if (fReachable)
  1326. vAddrOk.push_back(addr);
  1327. }
  1328. connman.AddNewAddresses(vAddrOk, pfrom->addr, 2 * 60 * 60);
  1329. if (vAddr.size() < 1000)
  1330. pfrom->fGetAddr = false;
  1331. if (pfrom->fOneShot)
  1332. pfrom->fDisconnect = true;
  1333. }
  1334. else if (strCommand == NetMsgType::SENDHEADERS)
  1335. {
  1336. LOCK(cs_main);
  1337. State(pfrom->GetId())->fPreferHeaders = true;
  1338. }
  1339. else if (strCommand == NetMsgType::SENDCMPCT)
  1340. {
  1341. bool fAnnounceUsingCMPCTBLOCK = false;
  1342. uint64_t nCMPCTBLOCKVersion = 0;
  1343. vRecv >> fAnnounceUsingCMPCTBLOCK >> nCMPCTBLOCKVersion;
  1344. if (nCMPCTBLOCKVersion == 1 || ((pfrom->GetLocalServices() & NODE_WITNESS) && nCMPCTBLOCKVersion == 2)) {
  1345. LOCK(cs_main);
  1346. // fProvidesHeaderAndIDs is used to "lock in" version of compact blocks we send (fWantsCmpctWitness)
  1347. if (!State(pfrom->GetId())->fProvidesHeaderAndIDs) {
  1348. State(pfrom->GetId())->fProvidesHeaderAndIDs = true;
  1349. State(pfrom->GetId())->fWantsCmpctWitness = nCMPCTBLOCKVersion == 2;
  1350. }
  1351. if (State(pfrom->GetId())->fWantsCmpctWitness == (nCMPCTBLOCKVersion == 2)) // ignore later version announces
  1352. State(pfrom->GetId())->fPreferHeaderAndIDs = fAnnounceUsingCMPCTBLOCK;
  1353. if (!State(pfrom->GetId())->fSupportsDesiredCmpctVersion) {
  1354. if (pfrom->GetLocalServices() & NODE_WITNESS)
  1355. State(pfrom->GetId())->fSupportsDesiredCmpctVersion = (nCMPCTBLOCKVersion == 2);
  1356. else
  1357. State(pfrom->GetId())->fSupportsDesiredCmpctVersion = (nCMPCTBLOCKVersion == 1);
  1358. }
  1359. }
  1360. }
  1361. else if (strCommand == NetMsgType::INV)
  1362. {
  1363. std::vector<CInv> vInv;
  1364. vRecv >> vInv;
  1365. if (vInv.size() > MAX_INV_SZ)
  1366. {
  1367. LOCK(cs_main);
  1368. Misbehaving(pfrom->GetId(), 20);
  1369. return error("message inv size() = %u", vInv.size());
  1370. }
  1371. bool fBlocksOnly = !fRelayTxes;
  1372. // Allow whitelisted peers to send data other than blocks in blocks only mode if whitelistrelay is true
  1373. if (pfrom->fWhitelisted && GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY))
  1374. fBlocksOnly = false;
  1375. LOCK(cs_main);
  1376. uint32_t nFetchFlags = GetFetchFlags(pfrom);
  1377. for (unsigned int nInv = 0; nInv < vInv.size(); nInv++)
  1378. {
  1379. CInv &inv = vInv[nInv];
  1380. if (interruptMsgProc)
  1381. return true;
  1382. bool fAlreadyHave = AlreadyHave(inv);
  1383. LogPrint(BCLog::NET, "got inv: %s %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom->GetId());
  1384. if (inv.type == MSG_TX) {
  1385. inv.type |= nFetchFlags;
  1386. }
  1387. if (inv.type == MSG_BLOCK) {
  1388. UpdateBlockAvailability(pfrom->GetId(), inv.hash);
  1389. if (!fAlreadyHave && !fImporting && !fReindex && !mapBlocksInFlight.count(inv.hash)) {
  1390. // We used to request the full block here, but since headers-announcements are now the
  1391. // primary method of announcement on the network, and since, in the case that a node
  1392. // fell back to inv we probably have a reorg which we should get the headers for first,
  1393. // we now only provide a getheaders response here. When we receive the headers, we will
  1394. // then ask for the blocks we need.
  1395. connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::GETHEADERS, chainActive.GetLocator(pindexBestHeader), inv.hash));
  1396. LogPrint(BCLog::NET, "getheaders (%d) %s to peer=%d\n", pindexBestHeader->nHeight, inv.hash.ToString(), pfrom->GetId());
  1397. }
  1398. }
  1399. else
  1400. {
  1401. pfrom->AddInventoryKnown(inv);
  1402. if (fBlocksOnly) {
  1403. LogPrint(BCLog::NET, "transaction (%s) inv sent in violation of protocol peer=%d\n", inv.hash.ToString(), pfrom->GetId());
  1404. } else if (!fAlreadyHave && !fImporting && !fReindex && !IsInitialBlockDownload()) {
  1405. pfrom->AskFor(inv);
  1406. }
  1407. }
  1408. // Track requests for our stuff
  1409. GetMainSignals().Inventory(inv.hash);
  1410. }
  1411. }
  1412. else if (strCommand == NetMsgType::GETDATA)
  1413. {
  1414. std::vector<CInv> vInv;
  1415. vRecv >> vInv;
  1416. if (vInv.size() > MAX_INV_SZ)
  1417. {
  1418. LOCK(cs_main);
  1419. Misbehaving(pfrom->GetId(), 20);
  1420. return error("message getdata size() = %u", vInv.size());
  1421. }
  1422. LogPrint(BCLog::NET, "received getdata (%u invsz) peer=%d\n", vInv.size(), pfrom->GetId());
  1423. if (vInv.size() > 0) {
  1424. LogPrint(BCLog::NET, "received getdata for: %s peer=%d\n", vInv[0].ToString(), pfrom->GetId());
  1425. }
  1426. pfrom->vRecvGetData.insert(pfrom->vRecvGetData.end(), vInv.begin(), vInv.end());
  1427. ProcessGetData(pfrom, chainparams.GetConsensus(), connman, interruptMsgProc);
  1428. }
  1429. else if (strCommand == NetMsgType::GETBLOCKS)
  1430. {
  1431. CBlockLocator locator;
  1432. uint256 hashStop;
  1433. vRecv >> locator >> hashStop;
  1434. // We might have announced the currently-being-connected tip using a
  1435. // compact block, which resulted in the peer sending a getblocks
  1436. // request, which we would otherwise respond to without the new block.
  1437. // To avoid this situation we simply verify that we are on our best
  1438. // known chain now. This is super overkill, but we handle it better
  1439. // for getheaders requests, and there are no known nodes which support
  1440. // compact blocks but still use getblocks to request blocks.
  1441. {
  1442. std::shared_ptr<const CBlock> a_recent_block;
  1443. {
  1444. LOCK(cs_most_recent_block);
  1445. a_recent_block = most_recent_block;
  1446. }
  1447. CValidationState dummy;
  1448. ActivateBestChain(dummy, Params(), a_recent_block);
  1449. }
  1450. LOCK(cs_main);
  1451. // Find the last block the caller has in the main chain
  1452. const CBlockIndex* pindex = FindForkInGlobalIndex(chainActive, locator);
  1453. // Send the rest of the chain
  1454. if (pindex)
  1455. pindex = chainActive.Next(pindex);
  1456. int nLimit = 500;
  1457. LogPrint(BCLog::NET, "getblocks %d to %s limit %d from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.IsNull() ? "end" : hashStop.ToString(), nLimit, pfrom->GetId());
  1458. for (; pindex; pindex = chainActive.Next(pindex))
  1459. {
  1460. if (pindex->GetBlockHash() == hashStop)
  1461. {
  1462. LogPrint(BCLog::NET, " getblocks stopping at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
  1463. break;
  1464. }
  1465. // If pruning, don't inv blocks unless we have on disk and are likely to still have
  1466. // for some reasonable time window (1 hour) that block relay might require.
  1467. const int nPrunedBlocksLikelyToHave = MIN_BLOCKS_TO_KEEP - 3600 / chainparams.GetConsensus().nPowTargetSpacing;
  1468. if (fPruneMode && (!(pindex->nStatus & BLOCK_HAVE_DATA) || pindex->nHeight <= chainActive.Tip()->nHeight - nPrunedBlocksLikelyToHave))
  1469. {
  1470. LogPrint(BCLog::NET, " getblocks stopping, pruned or too old block at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
  1471. break;
  1472. }
  1473. pfrom->PushInventory(CInv(MSG_BLOCK, pindex->GetBlockHash()));
  1474. if (--nLimit <= 0)
  1475. {
  1476. // When this block is requested, we'll send an inv that'll
  1477. // trigger the peer to getblocks the next batch of inventory.
  1478. LogPrint(BCLog::NET, " getblocks stopping at limit %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
  1479. pfrom->hashContinue = pindex->GetBlockHash();
  1480. break;
  1481. }
  1482. }
  1483. }
  1484. else if (strCommand == NetMsgType::GETBLOCKTXN)
  1485. {
  1486. BlockTransactionsRequest req;
  1487. vRecv >> req;
  1488. std::shared_ptr<const CBlock> recent_block;
  1489. {
  1490. LOCK(cs_most_recent_block);
  1491. if (most_recent_block_hash == req.blockhash)
  1492. recent_block = most_recent_block;
  1493. // Unlock cs_most_recent_block to avoid cs_main lock inversion
  1494. }
  1495. if (recent_block) {
  1496. SendBlockTransactions(*recent_block, req, pfrom, connman);
  1497. return true;
  1498. }
  1499. LOCK(cs_main);
  1500. BlockMap::iterator it = mapBlockIndex.find(req.blockhash);
  1501. if (it == mapBlockIndex.end() || !(it->second->nStatus & BLOCK_HAVE_DATA)) {
  1502. LogPrintf("Peer %d sent us a getblocktxn for a block we don't have", pfrom->GetId());
  1503. return true;
  1504. }
  1505. if (it->second->nHeight < chainActive.Height() - MAX_BLOCKTXN_DEPTH) {
  1506. // If an older block is requested (should never happen in practice,
  1507. // but can happen in tests) send a block response instead of a
  1508. // blocktxn response. Sending a full block response instead of a
  1509. // small blocktxn response is preferable in the case where a peer
  1510. // might maliciously send lots of getblocktxn requests to trigger
  1511. // expensive disk reads, because it will require the peer to
  1512. // actually receive all the data read from disk over the network.
  1513. LogPrint(BCLog::NET, "Peer %d sent us a getblocktxn for a block > %i deep", pfrom->GetId(), MAX_BLOCKTXN_DEPTH);
  1514. CInv inv;
  1515. inv.type = State(pfrom->GetId())->fWantsCmpctWitness ? MSG_WITNESS_BLOCK : MSG_BLOCK;
  1516. inv.hash = req.blockhash;
  1517. pfrom->vRecvGetData.push_back(inv);
  1518. ProcessGetData(pfrom, chainparams.GetConsensus(), connman, interruptMsgProc);
  1519. return true;
  1520. }
  1521. CBlock block;
  1522. bool ret = ReadBlockFromDisk(block, it->second, chainparams.GetConsensus());
  1523. assert(ret);
  1524. SendBlockTransactions(block, req, pfrom, connman);
  1525. }
  1526. else if (strCommand == NetMsgType::GETHEADERS)
  1527. {
  1528. CBlockLocator locator;
  1529. uint256 hashStop;
  1530. vRecv >> locator >> hashStop;
  1531. LOCK(cs_main);
  1532. if (IsInitialBlockDownload() && !pfrom->fWhitelisted) {
  1533. LogPrint(BCLog::NET, "Ignoring getheaders from peer=%d because node is in initial block download\n", pfrom->GetId());
  1534. return true;
  1535. }
  1536. CNodeState *nodestate = State(pfrom->GetId());
  1537. const CBlockIndex* pindex = NULL;
  1538. if (locator.IsNull())
  1539. {
  1540. // If locator is null, return the hashStop block
  1541. BlockMap::iterator mi = mapBlockIndex.find(hashStop);
  1542. if (mi == mapBlockIndex.end())
  1543. return true;
  1544. pindex = (*mi).second;
  1545. }
  1546. else
  1547. {
  1548. // Find the last block the caller has in the main chain
  1549. pindex = FindForkInGlobalIndex(chainActive, locator);
  1550. if (pindex)
  1551. pindex = chainActive.Next(pindex);
  1552. }
  1553. // we must use CBlocks, as CBlockHeaders won't include the 0x00 nTx count at the end
  1554. std::vector<CBlock> vHeaders;
  1555. int nLimit = MAX_HEADERS_RESULTS;
  1556. LogPrint(BCLog::NET, "getheaders %d to %s from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.IsNull() ? "end" : hashStop.ToString(), pfrom->GetId());
  1557. for (; pindex; pindex = chainActive.Next(pindex))
  1558. {
  1559. vHeaders.push_back(pindex->GetBlockHeader());
  1560. if (--nLimit <= 0 || pindex->GetBlockHash() == hashStop)
  1561. break;
  1562. }
  1563. // pindex can be NULL either if we sent chainActive.Tip() OR
  1564. // if our peer has chainActive.Tip() (and thus we are sending an empty
  1565. // headers message). In both cases it's safe to update
  1566. // pindexBestHeaderSent to be our tip.
  1567. //
  1568. // It is important that we simply reset the BestHeaderSent value here,
  1569. // and not max(BestHeaderSent, newHeaderSent). We might have announced
  1570. // the currently-being-connected tip using a compact block, which
  1571. // resulted in the peer sending a headers request, which we respond to
  1572. // without the new block. By resetting the BestHeaderSent, we ensure we
  1573. // will re-announce the new block via headers (or compact blocks again)
  1574. // in the SendMessages logic.
  1575. nodestate->pindexBestHeaderSent = pindex ? pindex : chainActive.Tip();
  1576. connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::HEADERS, vHeaders));
  1577. }
  1578. else if (strCommand == NetMsgType::TX)
  1579. {
  1580. // Stop processing the transaction early if
  1581. // We are in blocks only mode and peer is either not whitelisted or whitelistrelay is off
  1582. if (!fRelayTxes && (!pfrom->fWhitelisted || !GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY)))
  1583. {
  1584. LogPrint(BCLog::NET, "transaction sent in violation of protocol peer=%d\n", pfrom->GetId());
  1585. return true;
  1586. }
  1587. std::deque<COutPoint> vWorkQueue;
  1588. std::vector<uint256> vEraseQueue;
  1589. CTransactionRef ptx;
  1590. vRecv >> ptx;
  1591. const CTransaction& tx = *ptx;
  1592. CInv inv(MSG_TX, tx.GetHash());
  1593. pfrom->AddInventoryKnown(inv);
  1594. LOCK(cs_main);
  1595. bool fMissingInputs = false;
  1596. CValidationState state;
  1597. pfrom->setAskFor.erase(inv.hash);
  1598. mapAlreadyAskedFor.erase(inv.hash);
  1599. std::list<CTransactionRef> lRemovedTxn;
  1600. if (!AlreadyHave(inv) && AcceptToMemoryPool(mempool, state, ptx, true, &fMissingInputs, &lRemovedTxn)) {
  1601. mempool.check(pcoinsTip);
  1602. RelayTransaction(tx, connman);
  1603. for (unsigned int i = 0; i < tx.vout.size(); i++) {
  1604. vWorkQueue.emplace_back(inv.hash, i);
  1605. }
  1606. pfrom->nLastTXTime = GetTime();
  1607. LogPrint(BCLog::MEMPOOL, "AcceptToMemoryPool: peer=%d: accepted %s (poolsz %u txn, %u kB)\n",
  1608. pfrom->GetId(),
  1609. tx.GetHash().ToString(),
  1610. mempool.size(), mempool.DynamicMemoryUsage() / 1000);
  1611. // Recursively process any orphan transactions that depended on this one
  1612. std::set<NodeId> setMisbehaving;
  1613. while (!vWorkQueue.empty()) {
  1614. auto itByPrev = mapOrphanTransactionsByPrev.find(vWorkQueue.front());
  1615. vWorkQueue.pop_front();
  1616. if (itByPrev == mapOrphanTransactionsByPrev.end())
  1617. continue;
  1618. for (auto mi = itByPrev->second.begin();
  1619. mi != itByPrev->second.end();
  1620. ++mi)
  1621. {
  1622. const CTransactionRef& porphanTx = (*mi)->second.tx;
  1623. const CTransaction& orphanTx = *porphanTx;
  1624. const uint256& orphanHash = orphanTx.GetHash();
  1625. NodeId fromPeer = (*mi)->second.fromPeer;
  1626. bool fMissingInputs2 = false;
  1627. // Use a dummy CValidationState so someone can't setup nodes to counter-DoS based on orphan
  1628. // resolution (that is, feeding people an invalid transaction based on LegitTxX in order to get
  1629. // anyone relaying LegitTxX banned)
  1630. CValidationState stateDummy;
  1631. if (setMisbehaving.count(fromPeer))
  1632. continue;
  1633. if (AcceptToMemoryPool(mempool, stateDummy, porphanTx, true, &fMissingInputs2, &lRemovedTxn)) {
  1634. LogPrint(BCLog::MEMPOOL, " accepted orphan tx %s\n", orphanHash.ToString());
  1635. RelayTransaction(orphanTx, connman);
  1636. for (unsigned int i = 0; i < orphanTx.vout.size(); i++) {
  1637. vWorkQueue.emplace_back(orphanHash, i);
  1638. }
  1639. vEraseQueue.push_back(orphanHash);
  1640. }
  1641. else if (!fMissingInputs2)
  1642. {
  1643. int nDos = 0;
  1644. if (stateDummy.IsInvalid(nDos) && nDos > 0)
  1645. {
  1646. // Punish peer that gave us an invalid orphan tx
  1647. Misbehaving(fromPeer, nDos);
  1648. setMisbehaving.insert(fromPeer);
  1649. LogPrint(BCLog::MEMPOOL, " invalid orphan tx %s\n", orphanHash.ToString());
  1650. }
  1651. // Has inputs but not accepted to mempool
  1652. // Probably non-standard or insufficient fee
  1653. LogPrint(BCLog::MEMPOOL, " removed orphan tx %s\n", orphanHash.ToString());
  1654. vEraseQueue.push_back(orphanHash);
  1655. if (!orphanTx.HasWitness() && !stateDummy.CorruptionPossible()) {
  1656. // Do not use rejection cache for witness transactions or
  1657. // witness-stripped transactions, as they can have been malleated.
  1658. // See https://github.com/bitcoin/bitcoin/issues/8279 for details.
  1659. assert(recentRejects);
  1660. recentRejects->insert(orphanHash);
  1661. }
  1662. }
  1663. mempool.check(pcoinsTip);
  1664. }
  1665. }
  1666. BOOST_FOREACH(uint256 hash, vEraseQueue)
  1667. EraseOrphanTx(hash);
  1668. }
  1669. else if (fMissingInputs)
  1670. {
  1671. bool fRejectedParents = false; // It may be the case that the orphans parents have all been rejected
  1672. BOOST_FOREACH(const CTxIn& txin, tx.vin) {
  1673. if (recentRejects->contains(txin.prevout.hash)) {
  1674. fRejectedParents = true;
  1675. break;
  1676. }
  1677. }
  1678. if (!fRejectedParents) {
  1679. uint32_t nFetchFlags = GetFetchFlags(pfrom);
  1680. BOOST_FOREACH(const CTxIn& txin, tx.vin) {
  1681. CInv _inv(MSG_TX | nFetchFlags, txin.prevout.hash);
  1682. pfrom->AddInventoryKnown(_inv);
  1683. if (!AlreadyHave(_inv)) pfrom->AskFor(_inv);
  1684. }
  1685. AddOrphanTx(ptx, pfrom->GetId());
  1686. // DoS prevention: do not allow mapOrphanTransactions to grow unbounded
  1687. unsigned int nMaxOrphanTx = (unsigned int)std::max((int64_t)0, GetArg("-maxorphantx", DEFAULT_MAX_ORPHAN_TRANSACTIONS));
  1688. unsigned int nEvicted = LimitOrphanTxSize(nMaxOrphanTx);
  1689. if (nEvicted > 0) {
  1690. LogPrint(BCLog::MEMPOOL, "mapOrphan overflow, removed %u tx\n", nEvicted);
  1691. }
  1692. } else {
  1693. LogPrint(BCLog::MEMPOOL, "not keeping orphan with rejected parents %s\n",tx.GetHash().ToString());
  1694. // We will continue to reject this tx since it has rejected
  1695. // parents so avoid re-requesting it from other peers.
  1696. recentRejects->insert(tx.GetHash());
  1697. }
  1698. } else {
  1699. if (!tx.HasWitness() && !state.CorruptionPossible()) {
  1700. // Do not use rejection cache for witness transactions or
  1701. // witness-stripped transactions, as they can have been malleated.
  1702. // See https://github.com/bitcoin/bitcoin/issues/8279 for details.
  1703. assert(recentRejects);
  1704. recentRejects->insert(tx.GetHash());
  1705. if (RecursiveDynamicUsage(*ptx) < 100000) {
  1706. AddToCompactExtraTransactions(ptx);
  1707. }
  1708. } else if (tx.HasWitness() && RecursiveDynamicUsage(*ptx) < 100000) {
  1709. AddToCompactExtraTransactions(ptx);
  1710. }
  1711. if (pfrom->fWhitelisted && GetBoolArg("-whitelistforcerelay", DEFAULT_WHITELISTFORCERELAY)) {
  1712. // Always relay transactions received from whitelisted peers, even
  1713. // if they were already in the mempool or rejected from it due
  1714. // to policy, allowing the node to function as a gateway for
  1715. // nodes hidden behind it.
  1716. //
  1717. // Never relay transactions that we would assign a non-zero DoS
  1718. // score for, as we expect peers to do the same with us in that
  1719. // case.
  1720. int nDoS = 0;
  1721. if (!state.IsInvalid(nDoS) || nDoS == 0) {
  1722. LogPrintf("Force relaying tx %s from whitelisted peer=%d\n", tx.GetHash().ToString(), pfrom->GetId());
  1723. RelayTransaction(tx, connman);
  1724. } else {
  1725. LogPrintf("Not relaying invalid transaction %s from whitelisted peer=%d (%s)\n", tx.GetHash().ToString(), pfrom->GetId(), FormatStateMessage(state));
  1726. }
  1727. }
  1728. }
  1729. for (const CTransactionRef& removedTx : lRemovedTxn)
  1730. AddToCompactExtraTransactions(removedTx);
  1731. int nDoS = 0;
  1732. if (state.IsInvalid(nDoS))
  1733. {
  1734. LogPrint(BCLog::MEMPOOLREJ, "%s from peer=%d was not accepted: %s\n", tx.GetHash().ToString(),
  1735. pfrom->GetId(),
  1736. FormatStateMessage(state));
  1737. if (state.GetRejectCode() > 0 && state.GetRejectCode() < REJECT_INTERNAL) // Never send AcceptToMemoryPool's internal codes over P2P
  1738. connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::REJECT, strCommand, (unsigned char)state.GetRejectCode(),
  1739. state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), inv.hash));
  1740. if (nDoS > 0) {
  1741. Misbehaving(pfrom->GetId(), nDoS);
  1742. }
  1743. }
  1744. }
  1745. else if (strCommand == NetMsgType::CMPCTBLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
  1746. {
  1747. CBlockHeaderAndShortTxIDs cmpctblock;
  1748. vRecv >> cmpctblock;
  1749. {
  1750. LOCK(cs_main);
  1751. if (mapBlockIndex.find(cmpctblock.header.hashPrevBlock) == mapBlockIndex.end()) {
  1752. // Doesn't connect (or is genesis), instead of DoSing in AcceptBlockHeader, request deeper headers
  1753. if (!IsInitialBlockDownload())
  1754. connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::GETHEADERS, chainActive.GetLocator(pindexBestHeader), uint256()));
  1755. return true;
  1756. }
  1757. }
  1758. const CBlockIndex *pindex = NULL;
  1759. CValidationState state;
  1760. if (!ProcessNewBlockHeaders({cmpctblock.header}, state, chainparams, &pindex)) {
  1761. int nDoS;
  1762. if (state.IsInvalid(nDoS)) {
  1763. if (nDoS > 0) {
  1764. LOCK(cs_main);
  1765. Misbehaving(pfrom->GetId(), nDoS);
  1766. }
  1767. LogPrintf("Peer %d sent us invalid header via cmpctblock\n", pfrom->GetId());
  1768. return true;
  1769. }
  1770. }
  1771. // When we succeed in decoding a block's txids from a cmpctblock
  1772. // message we typically jump to the BLOCKTXN handling code, with a
  1773. // dummy (empty) BLOCKTXN message, to re-use the logic there in
  1774. // completing processing of the putative block (without cs_main).
  1775. bool fProcessBLOCKTXN = false;
  1776. CDataStream blockTxnMsg(SER_NETWORK, PROTOCOL_VERSION);
  1777. // If we end up treating this as a plain headers message, call that as well
  1778. // without cs_main.
  1779. bool fRevertToHeaderProcessing = false;
  1780. CDataStream vHeadersMsg(SER_NETWORK, PROTOCOL_VERSION);
  1781. // Keep a CBlock for "optimistic" compactblock reconstructions (see
  1782. // below)
  1783. std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
  1784. bool fBlockReconstructed = false;
  1785. {
  1786. LOCK(cs_main);
  1787. // If AcceptBlockHeader returned true, it set pindex
  1788. assert(pindex);
  1789. UpdateBlockAvailability(pfrom->GetId(), pindex->GetBlockHash());
  1790. std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator blockInFlightIt = mapBlocksInFlight.find(pindex->GetBlockHash());
  1791. bool fAlreadyInFlight = blockInFlightIt != mapBlocksInFlight.end();
  1792. if (pindex->nStatus & BLOCK_HAVE_DATA) // Nothing to do here
  1793. return true;
  1794. if (pindex->nChainWork <= chainActive.Tip()->nChainWork || // We know something better
  1795. pindex->nTx != 0) { // We had this block at some point, but pruned it
  1796. if (fAlreadyInFlight) {
  1797. // We requested this block for some reason, but our mempool will probably be useless
  1798. // so we just grab the block via normal getdata
  1799. std::vector<CInv> vInv(1);
  1800. vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(pfrom), cmpctblock.header.GetHash());
  1801. connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
  1802. }
  1803. return true;
  1804. }
  1805. // If we're not close to tip yet, give up and let parallel block fetch work its magic
  1806. if (!fAlreadyInFlight && !CanDirectFetch(chainparams.GetConsensus()))
  1807. return true;
  1808. CNodeState *nodestate = State(pfrom->GetId());
  1809. if (IsWitnessEnabled(pindex->pprev, chainparams.GetConsensus()) && !nodestate->fSupportsDesiredCmpctVersion) {
  1810. // Don't bother trying to process compact blocks from v1 peers
  1811. // after segwit activates.
  1812. return true;
  1813. }
  1814. // We want to be a bit conservative just to be extra careful about DoS
  1815. // possibilities in compact block processing...
  1816. if (pindex->nHeight <= chainActive.Height() + 2) {
  1817. if ((!fAlreadyInFlight && nodestate->nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) ||
  1818. (fAlreadyInFlight && blockInFlightIt->second.first == pfrom->GetId())) {
  1819. std::list<QueuedBlock>::iterator* queuedBlockIt = NULL;
  1820. if (!MarkBlockAsInFlight(pfrom->GetId(), pindex->GetBlockHash(), chainparams.GetConsensus(), pindex, &queuedBlockIt)) {
  1821. if (!(*queuedBlockIt)->partialBlock)
  1822. (*queuedBlockIt)->partialBlock.reset(new PartiallyDownloadedBlock(&mempool));
  1823. else {
  1824. // The block was already in flight using compact blocks from the same peer
  1825. LogPrint(BCLog::NET, "Peer sent us compact block we were already syncing!\n");
  1826. return true;
  1827. }
  1828. }
  1829. PartiallyDownloadedBlock& partialBlock = *(*queuedBlockIt)->partialBlock;
  1830. ReadStatus status = partialBlock.InitData(cmpctblock, vExtraTxnForCompact);
  1831. if (status == READ_STATUS_INVALID) {
  1832. MarkBlockAsReceived(pindex->GetBlockHash()); // Reset in-flight state in case of whitelist
  1833. Misbehaving(pfrom->GetId(), 100);
  1834. LogPrintf("Peer %d sent us invalid compact block\n", pfrom->GetId());
  1835. return true;
  1836. } else if (status == READ_STATUS_FAILED) {
  1837. // Duplicate txindexes, the block is now in-flight, so just request it
  1838. std::vector<CInv> vInv(1);
  1839. vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(pfrom), cmpctblock.header.GetHash());
  1840. connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
  1841. return true;
  1842. }
  1843. BlockTransactionsRequest req;
  1844. for (size_t i = 0; i < cmpctblock.BlockTxCount(); i++) {
  1845. if (!partialBlock.IsTxAvailable(i))
  1846. req.indexes.push_back(i);
  1847. }
  1848. if (req.indexes.empty()) {
  1849. // Dirty hack to jump to BLOCKTXN code (TODO: move message handling into their own functions)
  1850. BlockTransactions txn;
  1851. txn.blockhash = cmpctblock.header.GetHash();
  1852. blockTxnMsg << txn;
  1853. fProcessBLOCKTXN = true;
  1854. } else {
  1855. req.blockhash = pindex->GetBlockHash();
  1856. connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::GETBLOCKTXN, req));
  1857. }
  1858. } else {
  1859. // This block is either already in flight from a different
  1860. // peer, or this peer has too many blocks outstanding to
  1861. // download from.
  1862. // Optimistically try to reconstruct anyway since we might be
  1863. // able to without any round trips.
  1864. PartiallyDownloadedBlock tempBlock(&mempool);
  1865. ReadStatus status = tempBlock.InitData(cmpctblock, vExtraTxnForCompact);
  1866. if (status != READ_STATUS_OK) {
  1867. // TODO: don't ignore failures
  1868. return true;
  1869. }
  1870. std::vector<CTransactionRef> dummy;
  1871. status = tempBlock.FillBlock(*pblock, dummy);
  1872. if (status == READ_STATUS_OK) {
  1873. fBlockReconstructed = true;
  1874. }
  1875. }
  1876. } else {
  1877. if (fAlreadyInFlight) {
  1878. // We requested this block, but its far into the future, so our
  1879. // mempool will probably be useless - request the block normally
  1880. std::vector<CInv> vInv(1);
  1881. vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(pfrom), cmpctblock.header.GetHash());
  1882. connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
  1883. return true;
  1884. } else {
  1885. // If this was an announce-cmpctblock, we want the same treatment as a header message
  1886. // Dirty hack to process as if it were just a headers message (TODO: move message handling into their own functions)
  1887. std::vector<CBlock> headers;
  1888. headers.push_back(cmpctblock.header);
  1889. vHeadersMsg << headers;
  1890. fRevertToHeaderProcessing = true;
  1891. }
  1892. }
  1893. } // cs_main
  1894. if (fProcessBLOCKTXN)
  1895. return ProcessMessage(pfrom, NetMsgType::BLOCKTXN, blockTxnMsg, nTimeReceived, chainparams, connman, interruptMsgProc);
  1896. if (fRevertToHeaderProcessing)
  1897. return ProcessMessage(pfrom, NetMsgType::HEADERS, vHeadersMsg, nTimeReceived, chainparams, connman, interruptMsgProc);
  1898. if (fBlockReconstructed) {
  1899. // If we got here, we were able to optimistically reconstruct a
  1900. // block that is in flight from some other peer.
  1901. {
  1902. LOCK(cs_main);
  1903. mapBlockSource.emplace(pblock->GetHash(), std::make_pair(pfrom->GetId(), false));
  1904. }
  1905. bool fNewBlock = false;
  1906. ProcessNewBlock(chainparams, pblock, true, &fNewBlock);
  1907. if (fNewBlock)
  1908. pfrom->nLastBlockTime = GetTime();
  1909. LOCK(cs_main); // hold cs_main for CBlockIndex::IsValid()
  1910. if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS)) {
  1911. // Clear download state for this block, which is in
  1912. // process from some other peer. We do this after calling
  1913. // ProcessNewBlock so that a malleated cmpctblock announcement
  1914. // can't be used to interfere with block relay.
  1915. MarkBlockAsReceived(pblock->GetHash());
  1916. }
  1917. }
  1918. }
  1919. else if (strCommand == NetMsgType::BLOCKTXN && !fImporting && !fReindex) // Ignore blocks received while importing
  1920. {
  1921. BlockTransactions resp;
  1922. vRecv >> resp;
  1923. std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
  1924. bool fBlockRead = false;
  1925. {
  1926. LOCK(cs_main);
  1927. std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator it = mapBlocksInFlight.find(resp.blockhash);
  1928. if (it == mapBlocksInFlight.end() || !it->second.second->partialBlock ||
  1929. it->second.first != pfrom->GetId()) {
  1930. LogPrint(BCLog::NET, "Peer %d sent us block transactions for block we weren't expecting\n", pfrom->GetId());
  1931. return true;
  1932. }
  1933. PartiallyDownloadedBlock& partialBlock = *it->second.second->partialBlock;
  1934. ReadStatus status = partialBlock.FillBlock(*pblock, resp.txn);
  1935. if (status == READ_STATUS_INVALID) {
  1936. MarkBlockAsReceived(resp.blockhash); // Reset in-flight state in case of whitelist
  1937. Misbehaving(pfrom->GetId(), 100);
  1938. LogPrintf("Peer %d sent us invalid compact block/non-matching block transactions\n", pfrom->GetId());
  1939. return true;
  1940. } else if (status == READ_STATUS_FAILED) {
  1941. // Might have collided, fall back to getdata now :(
  1942. std::vector<CInv> invs;
  1943. invs.push_back(CInv(MSG_BLOCK | GetFetchFlags(pfrom), resp.blockhash));
  1944. connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::GETDATA, invs));
  1945. } else {
  1946. // Block is either okay, or possibly we received
  1947. // READ_STATUS_CHECKBLOCK_FAILED.
  1948. // Note that CheckBlock can only fail for one of a few reasons:
  1949. // 1. bad-proof-of-work (impossible here, because we've already
  1950. // accepted the header)
  1951. // 2. merkleroot doesn't match the transactions given (already
  1952. // caught in FillBlock with READ_STATUS_FAILED, so
  1953. // impossible here)
  1954. // 3. the block is otherwise invalid (eg invalid coinbase,
  1955. // block is too big, too many legacy sigops, etc).
  1956. // So if CheckBlock failed, #3 is the only possibility.
  1957. // Under BIP 152, we don't DoS-ban unless proof of work is
  1958. // invalid (we don't require all the stateless checks to have
  1959. // been run). This is handled below, so just treat this as
  1960. // though the block was successfully read, and rely on the
  1961. // handling in ProcessNewBlock to ensure the block index is
  1962. // updated, reject messages go out, etc.
  1963. MarkBlockAsReceived(resp.blockhash); // it is now an empty pointer
  1964. fBlockRead = true;
  1965. // mapBlockSource is only used for sending reject messages and DoS scores,
  1966. // so the race between here and cs_main in ProcessNewBlock is fine.
  1967. // BIP 152 permits peers to relay compact blocks after validating
  1968. // the header only; we should not punish peers if the block turns
  1969. // out to be invalid.
  1970. mapBlockSource.emplace(resp.blockhash, std::make_pair(pfrom->GetId(), false));
  1971. }
  1972. } // Don't hold cs_main when we call into ProcessNewBlock
  1973. if (fBlockRead) {
  1974. bool fNewBlock = false;
  1975. // Since we requested this block (it was in mapBlocksInFlight), force it to be processed,
  1976. // even if it would not be a candidate for new tip (missing previous block, chain not long enough, etc)
  1977. ProcessNewBlock(chainparams, pblock, true, &fNewBlock);
  1978. if (fNewBlock)
  1979. pfrom->nLastBlockTime = GetTime();
  1980. }
  1981. }
  1982. else if (strCommand == NetMsgType::HEADERS && !fImporting && !fReindex) // Ignore headers received while importing
  1983. {
  1984. std::vector<CBlockHeader> headers;
  1985. // Bypass the normal CBlock deserialization, as we don't want to risk deserializing 2000 full blocks.
  1986. unsigned int nCount = ReadCompactSize(vRecv);
  1987. if (nCount > MAX_HEADERS_RESULTS) {
  1988. LOCK(cs_main);
  1989. Misbehaving(pfrom->GetId(), 20);
  1990. return error("headers message size = %u", nCount);
  1991. }
  1992. headers.resize(nCount);
  1993. for (unsigned int n = 0; n < nCount; n++) {
  1994. vRecv >> headers[n];
  1995. ReadCompactSize(vRecv); // ignore tx count; assume it is 0.
  1996. }
  1997. if (nCount == 0) {
  1998. // Nothing interesting. Stop asking this peers for more headers.
  1999. return true;
  2000. }
  2001. const CBlockIndex *pindexLast = NULL;
  2002. {
  2003. LOCK(cs_main);
  2004. CNodeState *nodestate = State(pfrom->GetId());
  2005. // If this looks like it could be a block announcement (nCount <
  2006. // MAX_BLOCKS_TO_ANNOUNCE), use special logic for handling headers that
  2007. // don't connect:
  2008. // - Send a getheaders message in response to try to connect the chain.
  2009. // - The peer can send up to MAX_UNCONNECTING_HEADERS in a row that
  2010. // don't connect before giving DoS points
  2011. // - Once a headers message is received that is valid and does connect,
  2012. // nUnconnectingHeaders gets reset back to 0.
  2013. if (mapBlockIndex.find(headers[0].hashPrevBlock) == mapBlockIndex.end() && nCount < MAX_BLOCKS_TO_ANNOUNCE) {
  2014. nodestate->nUnconnectingHeaders++;
  2015. connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::GETHEADERS, chainActive.GetLocator(pindexBestHeader), uint256()));
  2016. LogPrint(BCLog::NET, "received header %s: missing prev block %s, sending getheaders (%d) to end (peer=%d, nUnconnectingHeaders=%d)\n",
  2017. headers[0].GetHash().ToString(),
  2018. headers[0].hashPrevBlock.ToString(),
  2019. pindexBestHeader->nHeight,
  2020. pfrom->GetId(), nodestate->nUnconnectingHeaders);
  2021. // Set hashLastUnknownBlock for this peer, so that if we
  2022. // eventually get the headers - even from a different peer -
  2023. // we can use this peer to download.
  2024. UpdateBlockAvailability(pfrom->GetId(), headers.back().GetHash());
  2025. if (nodestate->nUnconnectingHeaders % MAX_UNCONNECTING_HEADERS == 0) {
  2026. Misbehaving(pfrom->GetId(), 20);
  2027. }
  2028. return true;
  2029. }
  2030. uint256 hashLastBlock;
  2031. for (const CBlockHeader& header : headers) {
  2032. if (!hashLastBlock.IsNull() && header.hashPrevBlock != hashLastBlock) {
  2033. Misbehaving(pfrom->GetId(), 20);
  2034. return error("non-continuous headers sequence");
  2035. }
  2036. hashLastBlock = header.GetHash();
  2037. }
  2038. }
  2039. CValidationState state;
  2040. if (!ProcessNewBlockHeaders(headers, state, chainparams, &pindexLast)) {
  2041. int nDoS;
  2042. if (state.IsInvalid(nDoS)) {
  2043. if (nDoS > 0) {
  2044. LOCK(cs_main);
  2045. Misbehaving(pfrom->GetId(), nDoS);
  2046. }
  2047. return error("invalid header received");
  2048. }
  2049. }
  2050. {
  2051. LOCK(cs_main);
  2052. CNodeState *nodestate = State(pfrom->GetId());
  2053. if (nodestate->nUnconnectingHeaders > 0) {
  2054. LogPrint(BCLog::NET, "peer=%d: resetting nUnconnectingHeaders (%d -> 0)\n", pfrom->GetId(), nodestate->nUnconnectingHeaders);
  2055. }
  2056. nodestate->nUnconnectingHeaders = 0;
  2057. assert(pindexLast);
  2058. UpdateBlockAvailability(pfrom->GetId(), pindexLast->GetBlockHash());
  2059. if (nCount == MAX_HEADERS_RESULTS) {
  2060. // Headers message had its maximum size; the peer may have more headers.
  2061. // TODO: optimize: if pindexLast is an ancestor of chainActive.Tip or pindexBestHeader, continue
  2062. // from there instead.
  2063. LogPrint(BCLog::NET, "more getheaders (%d) to end to peer=%d (startheight:%d)\n", pindexLast->nHeight, pfrom->GetId(), pfrom->nStartingHeight);
  2064. connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::GETHEADERS, chainActive.GetLocator(pindexLast), uint256()));
  2065. }
  2066. bool fCanDirectFetch = CanDirectFetch(chainparams.GetConsensus());
  2067. // If this set of headers is valid and ends in a block with at least as
  2068. // much work as our tip, download as much as possible.
  2069. if (fCanDirectFetch && pindexLast->IsValid(BLOCK_VALID_TREE) && chainActive.Tip()->nChainWork <= pindexLast->nChainWork) {
  2070. std::vector<const CBlockIndex*> vToFetch;
  2071. const CBlockIndex *pindexWalk = pindexLast;
  2072. // Calculate all the blocks we'd need to switch to pindexLast, up to a limit.
  2073. while (pindexWalk && !chainActive.Contains(pindexWalk) && vToFetch.size() <= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
  2074. if (!(pindexWalk->nStatus & BLOCK_HAVE_DATA) &&
  2075. !mapBlocksInFlight.count(pindexWalk->GetBlockHash()) &&
  2076. (!IsWitnessEnabled(pindexWalk->pprev, chainparams.GetConsensus()) || State(pfrom->GetId())->fHaveWitness)) {
  2077. // We don't have this block, and it's not yet in flight.
  2078. vToFetch.push_back(pindexWalk);
  2079. }
  2080. pindexWalk = pindexWalk->pprev;
  2081. }
  2082. // If pindexWalk still isn't on our main chain, we're looking at a
  2083. // very large reorg at a time we think we're close to caught up to
  2084. // the main chain -- this shouldn't really happen. Bail out on the
  2085. // direct fetch and rely on parallel download instead.
  2086. if (!chainActive.Contains(pindexWalk)) {
  2087. LogPrint(BCLog::NET, "Large reorg, won't direct fetch to %s (%d)\n",
  2088. pindexLast->GetBlockHash().ToString(),
  2089. pindexLast->nHeight);
  2090. } else {
  2091. std::vector<CInv> vGetData;
  2092. // Download as much as possible, from earliest to latest.
  2093. BOOST_REVERSE_FOREACH(const CBlockIndex *pindex, vToFetch) {
  2094. if (nodestate->nBlocksInFlight >= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
  2095. // Can't download any more from this peer
  2096. break;
  2097. }
  2098. uint32_t nFetchFlags = GetFetchFlags(pfrom);
  2099. vGetData.push_back(CInv(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash()));
  2100. MarkBlockAsInFlight(pfrom->GetId(), pindex->GetBlockHash(), chainparams.GetConsensus(), pindex);
  2101. LogPrint(BCLog::NET, "Requesting block %s from peer=%d\n",
  2102. pindex->GetBlockHash().ToString(), pfrom->GetId());
  2103. }
  2104. if (vGetData.size() > 1) {
  2105. LogPrint(BCLog::NET, "Downloading blocks toward %s (%d) via headers direct fetch\n",
  2106. pindexLast->GetBlockHash().ToString(), pindexLast->nHeight);
  2107. }
  2108. if (vGetData.size() > 0) {
  2109. if (nodestate->fSupportsDesiredCmpctVersion && vGetData.size() == 1 && mapBlocksInFlight.size() == 1 && pindexLast->pprev->IsValid(BLOCK_VALID_CHAIN)) {
  2110. // In any case, we want to download using a compact block, not a regular one
  2111. vGetData[0] = CInv(MSG_CMPCT_BLOCK, vGetData[0].hash);
  2112. }
  2113. connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::GETDATA, vGetData));
  2114. }
  2115. }
  2116. }
  2117. }
  2118. }
  2119. else if (strCommand == NetMsgType::BLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
  2120. {
  2121. std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
  2122. vRecv >> *pblock;
  2123. LogPrint(BCLog::NET, "received block %s peer=%d\n", pblock->GetHash().ToString(), pfrom->GetId());
  2124. // Process all blocks from whitelisted peers, even if not requested,
  2125. // unless we're still syncing with the network.
  2126. // Such an unrequested block may still be processed, subject to the
  2127. // conditions in AcceptBlock().
  2128. bool forceProcessing = pfrom->fWhitelisted && !IsInitialBlockDownload();
  2129. const uint256 hash(pblock->GetHash());
  2130. {
  2131. LOCK(cs_main);
  2132. // Also always process if we requested the block explicitly, as we may
  2133. // need it even though it is not a candidate for a new best tip.
  2134. forceProcessing |= MarkBlockAsReceived(hash);
  2135. // mapBlockSource is only used for sending reject messages and DoS scores,
  2136. // so the race between here and cs_main in ProcessNewBlock is fine.
  2137. mapBlockSource.emplace(hash, std::make_pair(pfrom->GetId(), true));
  2138. }
  2139. bool fNewBlock = false;
  2140. ProcessNewBlock(chainparams, pblock, forceProcessing, &fNewBlock);
  2141. if (fNewBlock)
  2142. pfrom->nLastBlockTime = GetTime();
  2143. }
  2144. else if (strCommand == NetMsgType::GETADDR)
  2145. {
  2146. // This asymmetric behavior for inbound and outbound connections was introduced
  2147. // to prevent a fingerprinting attack: an attacker can send specific fake addresses
  2148. // to users' AddrMan and later request them by sending getaddr messages.
  2149. // Making nodes which are behind NAT and can only make outgoing connections ignore
  2150. // the getaddr message mitigates the attack.
  2151. if (!pfrom->fInbound) {
  2152. LogPrint(BCLog::NET, "Ignoring \"getaddr\" from outbound connection. peer=%d\n", pfrom->GetId());
  2153. return true;
  2154. }
  2155. // Only send one GetAddr response per connection to reduce resource waste
  2156. // and discourage addr stamping of INV announcements.
  2157. if (pfrom->fSentAddr) {
  2158. LogPrint(BCLog::NET, "Ignoring repeated \"getaddr\". peer=%d\n", pfrom->GetId());
  2159. return true;
  2160. }
  2161. pfrom->fSentAddr = true;
  2162. pfrom->vAddrToSend.clear();
  2163. std::vector<CAddress> vAddr = connman.GetAddresses();
  2164. FastRandomContext insecure_rand;
  2165. BOOST_FOREACH(const CAddress &addr, vAddr)
  2166. pfrom->PushAddress(addr, insecure_rand);
  2167. }
  2168. else if (strCommand == NetMsgType::MEMPOOL)
  2169. {
  2170. if (!(pfrom->GetLocalServices() & NODE_BLOOM) && !pfrom->fWhitelisted)
  2171. {
  2172. LogPrint(BCLog::NET, "mempool request with bloom filters disabled, disconnect peer=%d\n", pfrom->GetId());
  2173. pfrom->fDisconnect = true;
  2174. return true;
  2175. }
  2176. if (connman.OutboundTargetReached(false) && !pfrom->fWhitelisted)
  2177. {
  2178. LogPrint(BCLog::NET, "mempool request with bandwidth limit reached, disconnect peer=%d\n", pfrom->GetId());
  2179. pfrom->fDisconnect = true;
  2180. return true;
  2181. }
  2182. LOCK(pfrom->cs_inventory);
  2183. pfrom->fSendMempool = true;
  2184. }
  2185. else if (strCommand == NetMsgType::PING)
  2186. {
  2187. if (pfrom->nVersion > BIP0031_VERSION)
  2188. {
  2189. uint64_t nonce = 0;
  2190. vRecv >> nonce;
  2191. // Echo the message back with the nonce. This allows for two useful features:
  2192. //
  2193. // 1) A remote node can quickly check if the connection is operational
  2194. // 2) Remote nodes can measure the latency of the network thread. If this node
  2195. // is overloaded it won't respond to pings quickly and the remote node can
  2196. // avoid sending us more work, like chain download requests.
  2197. //
  2198. // The nonce stops the remote getting confused between different pings: without
  2199. // it, if the remote node sends a ping once per second and this node takes 5
  2200. // seconds to respond to each, the 5th ping the remote sends would appear to
  2201. // return very quickly.
  2202. connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::PONG, nonce));
  2203. }
  2204. }
  2205. else if (strCommand == NetMsgType::PONG)
  2206. {
  2207. int64_t pingUsecEnd = nTimeReceived;
  2208. uint64_t nonce = 0;
  2209. size_t nAvail = vRecv.in_avail();
  2210. bool bPingFinished = false;
  2211. std::string sProblem;
  2212. if (nAvail >= sizeof(nonce)) {
  2213. vRecv >> nonce;
  2214. // Only process pong message if there is an outstanding ping (old ping without nonce should never pong)
  2215. if (pfrom->nPingNonceSent != 0) {
  2216. if (nonce == pfrom->nPingNonceSent) {
  2217. // Matching pong received, this ping is no longer outstanding
  2218. bPingFinished = true;
  2219. int64_t pingUsecTime = pingUsecEnd - pfrom->nPingUsecStart;
  2220. if (pingUsecTime > 0) {
  2221. // Successful ping time measurement, replace previous
  2222. pfrom->nPingUsecTime = pingUsecTime;
  2223. pfrom->nMinPingUsecTime = std::min(pfrom->nMinPingUsecTime.load(), pingUsecTime);
  2224. } else {
  2225. // This should never happen
  2226. sProblem = "Timing mishap";
  2227. }
  2228. } else {
  2229. // Nonce mismatches are normal when pings are overlapping
  2230. sProblem = "Nonce mismatch";
  2231. if (nonce == 0) {
  2232. // This is most likely a bug in another implementation somewhere; cancel this ping
  2233. bPingFinished = true;
  2234. sProblem = "Nonce zero";
  2235. }
  2236. }
  2237. } else {
  2238. sProblem = "Unsolicited pong without ping";
  2239. }
  2240. } else {
  2241. // This is most likely a bug in another implementation somewhere; cancel this ping
  2242. bPingFinished = true;
  2243. sProblem = "Short payload";
  2244. }
  2245. if (!(sProblem.empty())) {
  2246. LogPrint(BCLog::NET, "pong peer=%d: %s, %x expected, %x received, %u bytes\n",
  2247. pfrom->GetId(),
  2248. sProblem,
  2249. pfrom->nPingNonceSent,
  2250. nonce,
  2251. nAvail);
  2252. }
  2253. if (bPingFinished) {
  2254. pfrom->nPingNonceSent = 0;
  2255. }
  2256. }
  2257. else if (strCommand == NetMsgType::FILTERLOAD)
  2258. {
  2259. CBloomFilter filter;
  2260. vRecv >> filter;
  2261. if (!filter.IsWithinSizeConstraints())
  2262. {
  2263. // There is no excuse for sending a too-large filter
  2264. LOCK(cs_main);
  2265. Misbehaving(pfrom->GetId(), 100);
  2266. }
  2267. else
  2268. {
  2269. LOCK(pfrom->cs_filter);
  2270. delete pfrom->pfilter;
  2271. pfrom->pfilter = new CBloomFilter(filter);
  2272. pfrom->pfilter->UpdateEmptyFull();
  2273. pfrom->fRelayTxes = true;
  2274. }
  2275. }
  2276. else if (strCommand == NetMsgType::FILTERADD)
  2277. {
  2278. std::vector<unsigned char> vData;
  2279. vRecv >> vData;
  2280. // Nodes must NEVER send a data item > 520 bytes (the max size for a script data object,
  2281. // and thus, the maximum size any matched object can have) in a filteradd message
  2282. bool bad = false;
  2283. if (vData.size() > MAX_SCRIPT_ELEMENT_SIZE) {
  2284. bad = true;
  2285. } else {
  2286. LOCK(pfrom->cs_filter);
  2287. if (pfrom->pfilter) {
  2288. pfrom->pfilter->insert(vData);
  2289. } else {
  2290. bad = true;
  2291. }
  2292. }
  2293. if (bad) {
  2294. LOCK(cs_main);
  2295. Misbehaving(pfrom->GetId(), 100);
  2296. }
  2297. }
  2298. else if (strCommand == NetMsgType::FILTERCLEAR)
  2299. {
  2300. LOCK(pfrom->cs_filter);
  2301. if (pfrom->GetLocalServices() & NODE_BLOOM) {
  2302. delete pfrom->pfilter;
  2303. pfrom->pfilter = new CBloomFilter();
  2304. }
  2305. pfrom->fRelayTxes = true;
  2306. }
  2307. else if (strCommand == NetMsgType::FEEFILTER) {
  2308. CAmount newFeeFilter = 0;
  2309. vRecv >> newFeeFilter;
  2310. if (MoneyRange(newFeeFilter)) {
  2311. {
  2312. LOCK(pfrom->cs_feeFilter);
  2313. pfrom->minFeeFilter = newFeeFilter;
  2314. }
  2315. LogPrint(BCLog::NET, "received: feefilter of %s from peer=%d\n", CFeeRate(newFeeFilter).ToString(), pfrom->GetId());
  2316. }
  2317. }
  2318. else if (strCommand == NetMsgType::NOTFOUND) {
  2319. // We do not care about the NOTFOUND message, but logging an Unknown Command
  2320. // message would be undesirable as we transmit it ourselves.
  2321. }
  2322. else {
  2323. // Ignore unknown commands for extensibility
  2324. LogPrint(BCLog::NET, "Unknown command \"%s\" from peer=%d\n", SanitizeString(strCommand), pfrom->GetId());
  2325. }
  2326. return true;
  2327. }
  2328. static bool SendRejectsAndCheckIfBanned(CNode* pnode, CConnman& connman)
  2329. {
  2330. AssertLockHeld(cs_main);
  2331. CNodeState &state = *State(pnode->GetId());
  2332. BOOST_FOREACH(const CBlockReject& reject, state.rejects) {
  2333. connman.PushMessage(pnode, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, (std::string)NetMsgType::BLOCK, reject.chRejectCode, reject.strRejectReason, reject.hashBlock));
  2334. }
  2335. state.rejects.clear();
  2336. if (state.fShouldBan) {
  2337. state.fShouldBan = false;
  2338. if (pnode->fWhitelisted)
  2339. LogPrintf("Warning: not punishing whitelisted peer %s!\n", pnode->addr.ToString());
  2340. else if (pnode->fAddnode)
  2341. LogPrintf("Warning: not punishing addnoded peer %s!\n", pnode->addr.ToString());
  2342. else {
  2343. pnode->fDisconnect = true;
  2344. if (pnode->addr.IsLocal())
  2345. LogPrintf("Warning: not banning local peer %s!\n", pnode->addr.ToString());
  2346. else
  2347. {
  2348. connman.Ban(pnode->addr, BanReasonNodeMisbehaving);
  2349. }
  2350. }
  2351. return true;
  2352. }
  2353. return false;
  2354. }
  2355. bool ProcessMessages(CNode* pfrom, CConnman& connman, const std::atomic<bool>& interruptMsgProc)
  2356. {
  2357. const CChainParams& chainparams = Params();
  2358. //
  2359. // Message format
  2360. // (4) message start
  2361. // (12) command
  2362. // (4) size
  2363. // (4) checksum
  2364. // (x) data
  2365. //
  2366. bool fMoreWork = false;
  2367. if (!pfrom->vRecvGetData.empty())
  2368. ProcessGetData(pfrom, chainparams.GetConsensus(), connman, interruptMsgProc);
  2369. if (pfrom->fDisconnect)
  2370. return false;
  2371. // this maintains the order of responses
  2372. if (!pfrom->vRecvGetData.empty()) return true;
  2373. // Don't bother if send buffer is too full to respond anyway
  2374. if (pfrom->fPauseSend)
  2375. return false;
  2376. std::list<CNetMessage> msgs;
  2377. {
  2378. LOCK(pfrom->cs_vProcessMsg);
  2379. if (pfrom->vProcessMsg.empty())
  2380. return false;
  2381. // Just take one message
  2382. msgs.splice(msgs.begin(), pfrom->vProcessMsg, pfrom->vProcessMsg.begin());
  2383. pfrom->nProcessQueueSize -= msgs.front().vRecv.size() + CMessageHeader::HEADER_SIZE;
  2384. pfrom->fPauseRecv = pfrom->nProcessQueueSize > connman.GetReceiveFloodSize();
  2385. fMoreWork = !pfrom->vProcessMsg.empty();
  2386. }
  2387. CNetMessage& msg(msgs.front());
  2388. msg.SetVersion(pfrom->GetRecvVersion());
  2389. // Scan for message start
  2390. if (memcmp(msg.hdr.pchMessageStart, chainparams.MessageStart(), CMessageHeader::MESSAGE_START_SIZE) != 0) {
  2391. LogPrintf("PROCESSMESSAGE: INVALID MESSAGESTART %s peer=%d\n", SanitizeString(msg.hdr.GetCommand()), pfrom->GetId());
  2392. pfrom->fDisconnect = true;
  2393. return false;
  2394. }
  2395. // Read header
  2396. CMessageHeader& hdr = msg.hdr;
  2397. if (!hdr.IsValid(chainparams.MessageStart()))
  2398. {
  2399. LogPrintf("PROCESSMESSAGE: ERRORS IN HEADER %s peer=%d\n", SanitizeString(hdr.GetCommand()), pfrom->GetId());
  2400. return fMoreWork;
  2401. }
  2402. std::string strCommand = hdr.GetCommand();
  2403. // Message size
  2404. unsigned int nMessageSize = hdr.nMessageSize;
  2405. // Checksum
  2406. CDataStream& vRecv = msg.vRecv;
  2407. const uint256& hash = msg.GetMessageHash();
  2408. if (memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0)
  2409. {
  2410. LogPrintf("%s(%s, %u bytes): CHECKSUM ERROR expected %s was %s\n", __func__,
  2411. SanitizeString(strCommand), nMessageSize,
  2412. HexStr(hash.begin(), hash.begin()+CMessageHeader::CHECKSUM_SIZE),
  2413. HexStr(hdr.pchChecksum, hdr.pchChecksum+CMessageHeader::CHECKSUM_SIZE));
  2414. return fMoreWork;
  2415. }
  2416. // Process message
  2417. bool fRet = false;
  2418. try
  2419. {
  2420. fRet = ProcessMessage(pfrom, strCommand, vRecv, msg.nTime, chainparams, connman, interruptMsgProc);
  2421. if (interruptMsgProc)
  2422. return false;
  2423. if (!pfrom->vRecvGetData.empty())
  2424. fMoreWork = true;
  2425. }
  2426. catch (const std::ios_base::failure& e)
  2427. {
  2428. connman.PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, strCommand, REJECT_MALFORMED, std::string("error parsing message")));
  2429. if (strstr(e.what(), "end of data"))
  2430. {
  2431. // Allow exceptions from under-length message on vRecv
  2432. LogPrintf("%s(%s, %u bytes): Exception '%s' caught, normally caused by a message being shorter than its stated length\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
  2433. }
  2434. else if (strstr(e.what(), "size too large"))
  2435. {
  2436. // Allow exceptions from over-long size
  2437. LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
  2438. }
  2439. else if (strstr(e.what(), "non-canonical ReadCompactSize()"))
  2440. {
  2441. // Allow exceptions from non-canonical encoding
  2442. LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
  2443. }
  2444. else
  2445. {
  2446. PrintExceptionContinue(&e, "ProcessMessages()");
  2447. }
  2448. }
  2449. catch (const std::exception& e) {
  2450. PrintExceptionContinue(&e, "ProcessMessages()");
  2451. } catch (...) {
  2452. PrintExceptionContinue(NULL, "ProcessMessages()");
  2453. }
  2454. if (!fRet) {
  2455. LogPrintf("%s(%s, %u bytes) FAILED peer=%d\n", __func__, SanitizeString(strCommand), nMessageSize, pfrom->GetId());
  2456. }
  2457. LOCK(cs_main);
  2458. SendRejectsAndCheckIfBanned(pfrom, connman);
  2459. return fMoreWork;
  2460. }
  2461. class CompareInvMempoolOrder
  2462. {
  2463. CTxMemPool *mp;
  2464. public:
  2465. CompareInvMempoolOrder(CTxMemPool *_mempool)
  2466. {
  2467. mp = _mempool;
  2468. }
  2469. bool operator()(std::set<uint256>::iterator a, std::set<uint256>::iterator b)
  2470. {
  2471. /* As std::make_heap produces a max-heap, we want the entries with the
  2472. * fewest ancestors/highest fee to sort later. */
  2473. return mp->CompareDepthAndScore(*b, *a);
  2474. }
  2475. };
  2476. bool SendMessages(CNode* pto, CConnman& connman, const std::atomic<bool>& interruptMsgProc)
  2477. {
  2478. const Consensus::Params& consensusParams = Params().GetConsensus();
  2479. {
  2480. // Don't send anything until the version handshake is complete
  2481. if (!pto->fSuccessfullyConnected || pto->fDisconnect)
  2482. return true;
  2483. // If we get here, the outgoing message serialization version is set and can't change.
  2484. const CNetMsgMaker msgMaker(pto->GetSendVersion());
  2485. //
  2486. // Message: ping
  2487. //
  2488. bool pingSend = false;
  2489. if (pto->fPingQueued) {
  2490. // RPC ping request by user
  2491. pingSend = true;
  2492. }
  2493. if (pto->nPingNonceSent == 0 && pto->nPingUsecStart + PING_INTERVAL * 1000000 < GetTimeMicros()) {
  2494. // Ping automatically sent as a latency probe & keepalive.
  2495. pingSend = true;
  2496. }
  2497. if (pingSend) {
  2498. uint64_t nonce = 0;
  2499. while (nonce == 0) {
  2500. GetRandBytes((unsigned char*)&nonce, sizeof(nonce));
  2501. }
  2502. pto->fPingQueued = false;
  2503. pto->nPingUsecStart = GetTimeMicros();
  2504. if (pto->nVersion > BIP0031_VERSION) {
  2505. pto->nPingNonceSent = nonce;
  2506. connman.PushMessage(pto, msgMaker.Make(NetMsgType::PING, nonce));
  2507. } else {
  2508. // Peer is too old to support ping command with nonce, pong will never arrive.
  2509. pto->nPingNonceSent = 0;
  2510. connman.PushMessage(pto, msgMaker.Make(NetMsgType::PING));
  2511. }
  2512. }
  2513. TRY_LOCK(cs_main, lockMain); // Acquire cs_main for IsInitialBlockDownload() and CNodeState()
  2514. if (!lockMain)
  2515. return true;
  2516. if (SendRejectsAndCheckIfBanned(pto, connman))
  2517. return true;
  2518. CNodeState &state = *State(pto->GetId());
  2519. // Address refresh broadcast
  2520. int64_t nNow = GetTimeMicros();
  2521. if (!IsInitialBlockDownload() && pto->nNextLocalAddrSend < nNow) {
  2522. AdvertiseLocal(pto);
  2523. pto->nNextLocalAddrSend = PoissonNextSend(nNow, AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL);
  2524. }
  2525. //
  2526. // Message: addr
  2527. //
  2528. if (pto->nNextAddrSend < nNow) {
  2529. pto->nNextAddrSend = PoissonNextSend(nNow, AVG_ADDRESS_BROADCAST_INTERVAL);
  2530. std::vector<CAddress> vAddr;
  2531. vAddr.reserve(pto->vAddrToSend.size());
  2532. BOOST_FOREACH(const CAddress& addr, pto->vAddrToSend)
  2533. {
  2534. if (!pto->addrKnown.contains(addr.GetKey()))
  2535. {
  2536. pto->addrKnown.insert(addr.GetKey());
  2537. vAddr.push_back(addr);
  2538. // receiver rejects addr messages larger than 1000
  2539. if (vAddr.size() >= 1000)
  2540. {
  2541. connman.PushMessage(pto, msgMaker.Make(NetMsgType::ADDR, vAddr));
  2542. vAddr.clear();
  2543. }
  2544. }
  2545. }
  2546. pto->vAddrToSend.clear();
  2547. if (!vAddr.empty())
  2548. connman.PushMessage(pto, msgMaker.Make(NetMsgType::ADDR, vAddr));
  2549. // we only send the big addr message once
  2550. if (pto->vAddrToSend.capacity() > 40)
  2551. pto->vAddrToSend.shrink_to_fit();
  2552. }
  2553. // Start block sync
  2554. if (pindexBestHeader == NULL)
  2555. pindexBestHeader = chainActive.Tip();
  2556. bool fFetch = state.fPreferredDownload || (nPreferredDownload == 0 && !pto->fClient && !pto->fOneShot); // Download if this is a nice peer, or we have no nice peers and this one might do.
  2557. if (!state.fSyncStarted && !pto->fClient && !fImporting && !fReindex) {
  2558. // Only actively request headers from a single peer, unless we're close to today.
  2559. if ((nSyncStarted == 0 && fFetch) || pindexBestHeader->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) {
  2560. state.fSyncStarted = true;
  2561. nSyncStarted++;
  2562. const CBlockIndex *pindexStart = pindexBestHeader;
  2563. /* If possible, start at the block preceding the currently
  2564. best known header. This ensures that we always get a
  2565. non-empty list of headers back as long as the peer
  2566. is up-to-date. With a non-empty response, we can initialise
  2567. the peer's known best block. This wouldn't be possible
  2568. if we requested starting at pindexBestHeader and
  2569. got back an empty response. */
  2570. if (pindexStart->pprev)
  2571. pindexStart = pindexStart->pprev;
  2572. LogPrint(BCLog::NET, "initial getheaders (%d) to peer=%d (startheight:%d)\n", pindexStart->nHeight, pto->GetId(), pto->nStartingHeight);
  2573. connman.PushMessage(pto, msgMaker.Make(NetMsgType::GETHEADERS, chainActive.GetLocator(pindexStart), uint256()));
  2574. }
  2575. }
  2576. // Resend wallet transactions that haven't gotten in a block yet
  2577. // Except during reindex, importing and IBD, when old wallet
  2578. // transactions become unconfirmed and spams other nodes.
  2579. if (!fReindex && !fImporting && !IsInitialBlockDownload())
  2580. {
  2581. GetMainSignals().Broadcast(nTimeBestReceived, &connman);
  2582. }
  2583. //
  2584. // Try sending block announcements via headers
  2585. //
  2586. {
  2587. // If we have less than MAX_BLOCKS_TO_ANNOUNCE in our
  2588. // list of block hashes we're relaying, and our peer wants
  2589. // headers announcements, then find the first header
  2590. // not yet known to our peer but would connect, and send.
  2591. // If no header would connect, or if we have too many
  2592. // blocks, or if the peer doesn't want headers, just
  2593. // add all to the inv queue.
  2594. LOCK(pto->cs_inventory);
  2595. std::vector<CBlock> vHeaders;
  2596. bool fRevertToInv = ((!state.fPreferHeaders &&
  2597. (!state.fPreferHeaderAndIDs || pto->vBlockHashesToAnnounce.size() > 1)) ||
  2598. pto->vBlockHashesToAnnounce.size() > MAX_BLOCKS_TO_ANNOUNCE);
  2599. const CBlockIndex *pBestIndex = NULL; // last header queued for delivery
  2600. ProcessBlockAvailability(pto->GetId()); // ensure pindexBestKnownBlock is up-to-date
  2601. if (!fRevertToInv) {
  2602. bool fFoundStartingHeader = false;
  2603. // Try to find first header that our peer doesn't have, and
  2604. // then send all headers past that one. If we come across any
  2605. // headers that aren't on chainActive, give up.
  2606. BOOST_FOREACH(const uint256 &hash, pto->vBlockHashesToAnnounce) {
  2607. BlockMap::iterator mi = mapBlockIndex.find(hash);
  2608. assert(mi != mapBlockIndex.end());
  2609. const CBlockIndex *pindex = mi->second;
  2610. if (chainActive[pindex->nHeight] != pindex) {
  2611. // Bail out if we reorged away from this block
  2612. fRevertToInv = true;
  2613. break;
  2614. }
  2615. if (pBestIndex != NULL && pindex->pprev != pBestIndex) {
  2616. // This means that the list of blocks to announce don't
  2617. // connect to each other.
  2618. // This shouldn't really be possible to hit during
  2619. // regular operation (because reorgs should take us to
  2620. // a chain that has some block not on the prior chain,
  2621. // which should be caught by the prior check), but one
  2622. // way this could happen is by using invalidateblock /
  2623. // reconsiderblock repeatedly on the tip, causing it to
  2624. // be added multiple times to vBlockHashesToAnnounce.
  2625. // Robustly deal with this rare situation by reverting
  2626. // to an inv.
  2627. fRevertToInv = true;
  2628. break;
  2629. }
  2630. pBestIndex = pindex;
  2631. if (fFoundStartingHeader) {
  2632. // add this to the headers message
  2633. vHeaders.push_back(pindex->GetBlockHeader());
  2634. } else if (PeerHasHeader(&state, pindex)) {
  2635. continue; // keep looking for the first new block
  2636. } else if (pindex->pprev == NULL || PeerHasHeader(&state, pindex->pprev)) {
  2637. // Peer doesn't have this header but they do have the prior one.
  2638. // Start sending headers.
  2639. fFoundStartingHeader = true;
  2640. vHeaders.push_back(pindex->GetBlockHeader());
  2641. } else {
  2642. // Peer doesn't have this header or the prior one -- nothing will
  2643. // connect, so bail out.
  2644. fRevertToInv = true;
  2645. break;
  2646. }
  2647. }
  2648. }
  2649. if (!fRevertToInv && !vHeaders.empty()) {
  2650. if (vHeaders.size() == 1 && state.fPreferHeaderAndIDs) {
  2651. // We only send up to 1 block as header-and-ids, as otherwise
  2652. // probably means we're doing an initial-ish-sync or they're slow
  2653. LogPrint(BCLog::NET, "%s sending header-and-ids %s to peer=%d\n", __func__,
  2654. vHeaders.front().GetHash().ToString(), pto->GetId());
  2655. int nSendFlags = state.fWantsCmpctWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS;
  2656. bool fGotBlockFromCache = false;
  2657. {
  2658. LOCK(cs_most_recent_block);
  2659. if (most_recent_block_hash == pBestIndex->GetBlockHash()) {
  2660. if (state.fWantsCmpctWitness || !fWitnessesPresentInMostRecentCompactBlock)
  2661. connman.PushMessage(pto, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, *most_recent_compact_block));
  2662. else {
  2663. CBlockHeaderAndShortTxIDs cmpctblock(*most_recent_block, state.fWantsCmpctWitness);
  2664. connman.PushMessage(pto, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, cmpctblock));
  2665. }
  2666. fGotBlockFromCache = true;
  2667. }
  2668. }
  2669. if (!fGotBlockFromCache) {
  2670. CBlock block;
  2671. bool ret = ReadBlockFromDisk(block, pBestIndex, consensusParams);
  2672. assert(ret);
  2673. CBlockHeaderAndShortTxIDs cmpctblock(block, state.fWantsCmpctWitness);
  2674. connman.PushMessage(pto, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, cmpctblock));
  2675. }
  2676. state.pindexBestHeaderSent = pBestIndex;
  2677. } else if (state.fPreferHeaders) {
  2678. if (vHeaders.size() > 1) {
  2679. LogPrint(BCLog::NET, "%s: %u headers, range (%s, %s), to peer=%d\n", __func__,
  2680. vHeaders.size(),
  2681. vHeaders.front().GetHash().ToString(),
  2682. vHeaders.back().GetHash().ToString(), pto->GetId());
  2683. } else {
  2684. LogPrint(BCLog::NET, "%s: sending header %s to peer=%d\n", __func__,
  2685. vHeaders.front().GetHash().ToString(), pto->GetId());
  2686. }
  2687. connman.PushMessage(pto, msgMaker.Make(NetMsgType::HEADERS, vHeaders));
  2688. state.pindexBestHeaderSent = pBestIndex;
  2689. } else
  2690. fRevertToInv = true;
  2691. }
  2692. if (fRevertToInv) {
  2693. // If falling back to using an inv, just try to inv the tip.
  2694. // The last entry in vBlockHashesToAnnounce was our tip at some point
  2695. // in the past.
  2696. if (!pto->vBlockHashesToAnnounce.empty()) {
  2697. const uint256 &hashToAnnounce = pto->vBlockHashesToAnnounce.back();
  2698. BlockMap::iterator mi = mapBlockIndex.find(hashToAnnounce);
  2699. assert(mi != mapBlockIndex.end());
  2700. const CBlockIndex *pindex = mi->second;
  2701. // Warn if we're announcing a block that is not on the main chain.
  2702. // This should be very rare and could be optimized out.
  2703. // Just log for now.
  2704. if (chainActive[pindex->nHeight] != pindex) {
  2705. LogPrint(BCLog::NET, "Announcing block %s not on main chain (tip=%s)\n",
  2706. hashToAnnounce.ToString(), chainActive.Tip()->GetBlockHash().ToString());
  2707. }
  2708. // If the peer's chain has this block, don't inv it back.
  2709. if (!PeerHasHeader(&state, pindex)) {
  2710. pto->PushInventory(CInv(MSG_BLOCK, hashToAnnounce));
  2711. LogPrint(BCLog::NET, "%s: sending inv peer=%d hash=%s\n", __func__,
  2712. pto->GetId(), hashToAnnounce.ToString());
  2713. }
  2714. }
  2715. }
  2716. pto->vBlockHashesToAnnounce.clear();
  2717. }
  2718. //
  2719. // Message: inventory
  2720. //
  2721. std::vector<CInv> vInv;
  2722. {
  2723. LOCK(pto->cs_inventory);
  2724. vInv.reserve(std::max<size_t>(pto->vInventoryBlockToSend.size(), INVENTORY_BROADCAST_MAX));
  2725. // Add blocks
  2726. BOOST_FOREACH(const uint256& hash, pto->vInventoryBlockToSend) {
  2727. vInv.push_back(CInv(MSG_BLOCK, hash));
  2728. if (vInv.size() == MAX_INV_SZ) {
  2729. connman.PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
  2730. vInv.clear();
  2731. }
  2732. }
  2733. pto->vInventoryBlockToSend.clear();
  2734. // Check whether periodic sends should happen
  2735. bool fSendTrickle = pto->fWhitelisted;
  2736. if (pto->nNextInvSend < nNow) {
  2737. fSendTrickle = true;
  2738. // Use half the delay for outbound peers, as there is less privacy concern for them.
  2739. pto->nNextInvSend = PoissonNextSend(nNow, INVENTORY_BROADCAST_INTERVAL >> !pto->fInbound);
  2740. }
  2741. // Time to send but the peer has requested we not relay transactions.
  2742. if (fSendTrickle) {
  2743. LOCK(pto->cs_filter);
  2744. if (!pto->fRelayTxes) pto->setInventoryTxToSend.clear();
  2745. }
  2746. // Respond to BIP35 mempool requests
  2747. if (fSendTrickle && pto->fSendMempool) {
  2748. auto vtxinfo = mempool.infoAll();
  2749. pto->fSendMempool = false;
  2750. CAmount filterrate = 0;
  2751. {
  2752. LOCK(pto->cs_feeFilter);
  2753. filterrate = pto->minFeeFilter;
  2754. }
  2755. LOCK(pto->cs_filter);
  2756. for (const auto& txinfo : vtxinfo) {
  2757. const uint256& hash = txinfo.tx->GetHash();
  2758. CInv inv(MSG_TX, hash);
  2759. pto->setInventoryTxToSend.erase(hash);
  2760. if (filterrate) {
  2761. if (txinfo.feeRate.GetFeePerK() < filterrate)
  2762. continue;
  2763. }
  2764. if (pto->pfilter) {
  2765. if (!pto->pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue;
  2766. }
  2767. pto->filterInventoryKnown.insert(hash);
  2768. vInv.push_back(inv);
  2769. if (vInv.size() == MAX_INV_SZ) {
  2770. connman.PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
  2771. vInv.clear();
  2772. }
  2773. }
  2774. pto->timeLastMempoolReq = GetTime();
  2775. }
  2776. // Determine transactions to relay
  2777. if (fSendTrickle) {
  2778. // Produce a vector with all candidates for sending
  2779. std::vector<std::set<uint256>::iterator> vInvTx;
  2780. vInvTx.reserve(pto->setInventoryTxToSend.size());
  2781. for (std::set<uint256>::iterator it = pto->setInventoryTxToSend.begin(); it != pto->setInventoryTxToSend.end(); it++) {
  2782. vInvTx.push_back(it);
  2783. }
  2784. CAmount filterrate = 0;
  2785. {
  2786. LOCK(pto->cs_feeFilter);
  2787. filterrate = pto->minFeeFilter;
  2788. }
  2789. // Topologically and fee-rate sort the inventory we send for privacy and priority reasons.
  2790. // A heap is used so that not all items need sorting if only a few are being sent.
  2791. CompareInvMempoolOrder compareInvMempoolOrder(&mempool);
  2792. std::make_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
  2793. // No reason to drain out at many times the network's capacity,
  2794. // especially since we have many peers and some will draw much shorter delays.
  2795. unsigned int nRelayedTransactions = 0;
  2796. LOCK(pto->cs_filter);
  2797. while (!vInvTx.empty() && nRelayedTransactions < INVENTORY_BROADCAST_MAX) {
  2798. // Fetch the top element from the heap
  2799. std::pop_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
  2800. std::set<uint256>::iterator it = vInvTx.back();
  2801. vInvTx.pop_back();
  2802. uint256 hash = *it;
  2803. // Remove it from the to-be-sent set
  2804. pto->setInventoryTxToSend.erase(it);
  2805. // Check if not in the filter already
  2806. if (pto->filterInventoryKnown.contains(hash)) {
  2807. continue;
  2808. }
  2809. // Not in the mempool anymore? don't bother sending it.
  2810. auto txinfo = mempool.info(hash);
  2811. if (!txinfo.tx) {
  2812. continue;
  2813. }
  2814. if (filterrate && txinfo.feeRate.GetFeePerK() < filterrate) {
  2815. continue;
  2816. }
  2817. if (pto->pfilter && !pto->pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue;
  2818. // Send
  2819. vInv.push_back(CInv(MSG_TX, hash));
  2820. nRelayedTransactions++;
  2821. {
  2822. // Expire old relay messages
  2823. while (!vRelayExpiration.empty() && vRelayExpiration.front().first < nNow)
  2824. {
  2825. mapRelay.erase(vRelayExpiration.front().second);
  2826. vRelayExpiration.pop_front();
  2827. }
  2828. auto ret = mapRelay.insert(std::make_pair(hash, std::move(txinfo.tx)));
  2829. if (ret.second) {
  2830. vRelayExpiration.push_back(std::make_pair(nNow + 15 * 60 * 1000000, ret.first));
  2831. }
  2832. }
  2833. if (vInv.size() == MAX_INV_SZ) {
  2834. connman.PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
  2835. vInv.clear();
  2836. }
  2837. pto->filterInventoryKnown.insert(hash);
  2838. }
  2839. }
  2840. }
  2841. if (!vInv.empty())
  2842. connman.PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
  2843. // Detect whether we're stalling
  2844. nNow = GetTimeMicros();
  2845. if (state.nStallingSince && state.nStallingSince < nNow - 1000000 * BLOCK_STALLING_TIMEOUT) {
  2846. // Stalling only triggers when the block download window cannot move. During normal steady state,
  2847. // the download window should be much larger than the to-be-downloaded set of blocks, so disconnection
  2848. // should only happen during initial block download.
  2849. LogPrintf("Peer=%d is stalling block download, disconnecting\n", pto->GetId());
  2850. pto->fDisconnect = true;
  2851. return true;
  2852. }
  2853. // In case there is a block that has been in flight from this peer for 2 + 0.5 * N times the block interval
  2854. // (with N the number of peers from which we're downloading validated blocks), disconnect due to timeout.
  2855. // We compensate for other peers to prevent killing off peers due to our own downstream link
  2856. // being saturated. We only count validated in-flight blocks so peers can't advertise non-existing block hashes
  2857. // to unreasonably increase our timeout.
  2858. if (state.vBlocksInFlight.size() > 0) {
  2859. QueuedBlock &queuedBlock = state.vBlocksInFlight.front();
  2860. int nOtherPeersWithValidatedDownloads = nPeersWithValidatedDownloads - (state.nBlocksInFlightValidHeaders > 0);
  2861. if (nNow > state.nDownloadingSince + consensusParams.nPowTargetSpacing * (BLOCK_DOWNLOAD_TIMEOUT_BASE + BLOCK_DOWNLOAD_TIMEOUT_PER_PEER * nOtherPeersWithValidatedDownloads)) {
  2862. LogPrintf("Timeout downloading block %s from peer=%d, disconnecting\n", queuedBlock.hash.ToString(), pto->GetId());
  2863. pto->fDisconnect = true;
  2864. return true;
  2865. }
  2866. }
  2867. //
  2868. // Message: getdata (blocks)
  2869. //
  2870. std::vector<CInv> vGetData;
  2871. if (!pto->fClient && (fFetch || !IsInitialBlockDownload()) && state.nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
  2872. std::vector<const CBlockIndex*> vToDownload;
  2873. NodeId staller = -1;
  2874. FindNextBlocksToDownload(pto->GetId(), MAX_BLOCKS_IN_TRANSIT_PER_PEER - state.nBlocksInFlight, vToDownload, staller, consensusParams);
  2875. BOOST_FOREACH(const CBlockIndex *pindex, vToDownload) {
  2876. uint32_t nFetchFlags = GetFetchFlags(pto);
  2877. vGetData.push_back(CInv(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash()));
  2878. MarkBlockAsInFlight(pto->GetId(), pindex->GetBlockHash(), consensusParams, pindex);
  2879. LogPrint(BCLog::NET, "Requesting block %s (%d) peer=%d\n", pindex->GetBlockHash().ToString(),
  2880. pindex->nHeight, pto->GetId());
  2881. }
  2882. if (state.nBlocksInFlight == 0 && staller != -1) {
  2883. if (State(staller)->nStallingSince == 0) {
  2884. State(staller)->nStallingSince = nNow;
  2885. LogPrint(BCLog::NET, "Stall started peer=%d\n", staller);
  2886. }
  2887. }
  2888. }
  2889. //
  2890. // Message: getdata (non-blocks)
  2891. //
  2892. while (!pto->mapAskFor.empty() && (*pto->mapAskFor.begin()).first <= nNow)
  2893. {
  2894. const CInv& inv = (*pto->mapAskFor.begin()).second;
  2895. if (!AlreadyHave(inv))
  2896. {
  2897. LogPrint(BCLog::NET, "Requesting %s peer=%d\n", inv.ToString(), pto->GetId());
  2898. vGetData.push_back(inv);
  2899. if (vGetData.size() >= 1000)
  2900. {
  2901. connman.PushMessage(pto, msgMaker.Make(NetMsgType::GETDATA, vGetData));
  2902. vGetData.clear();
  2903. }
  2904. } else {
  2905. //If we're not going to ask, don't expect a response.
  2906. pto->setAskFor.erase(inv.hash);
  2907. }
  2908. pto->mapAskFor.erase(pto->mapAskFor.begin());
  2909. }
  2910. if (!vGetData.empty())
  2911. connman.PushMessage(pto, msgMaker.Make(NetMsgType::GETDATA, vGetData));
  2912. //
  2913. // Message: feefilter
  2914. //
  2915. // We don't want white listed peers to filter txs to us if we have -whitelistforcerelay
  2916. if (pto->nVersion >= FEEFILTER_VERSION && GetBoolArg("-feefilter", DEFAULT_FEEFILTER) &&
  2917. !(pto->fWhitelisted && GetBoolArg("-whitelistforcerelay", DEFAULT_WHITELISTFORCERELAY))) {
  2918. CAmount currentFilter = mempool.GetMinFee(GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFeePerK();
  2919. int64_t timeNow = GetTimeMicros();
  2920. if (timeNow > pto->nextSendTimeFeeFilter) {
  2921. static CFeeRate default_feerate(DEFAULT_MIN_RELAY_TX_FEE);
  2922. static FeeFilterRounder filterRounder(default_feerate);
  2923. CAmount filterToSend = filterRounder.round(currentFilter);
  2924. // We always have a fee filter of at least minRelayTxFee
  2925. filterToSend = std::max(filterToSend, ::minRelayTxFee.GetFeePerK());
  2926. if (filterToSend != pto->lastSentFeeFilter) {
  2927. connman.PushMessage(pto, msgMaker.Make(NetMsgType::FEEFILTER, filterToSend));
  2928. pto->lastSentFeeFilter = filterToSend;
  2929. }
  2930. pto->nextSendTimeFeeFilter = PoissonNextSend(timeNow, AVG_FEEFILTER_BROADCAST_INTERVAL);
  2931. }
  2932. // If the fee filter has changed substantially and it's still more than MAX_FEEFILTER_CHANGE_DELAY
  2933. // until scheduled broadcast, then move the broadcast to within MAX_FEEFILTER_CHANGE_DELAY.
  2934. else if (timeNow + MAX_FEEFILTER_CHANGE_DELAY * 1000000 < pto->nextSendTimeFeeFilter &&
  2935. (currentFilter < 3 * pto->lastSentFeeFilter / 4 || currentFilter > 4 * pto->lastSentFeeFilter / 3)) {
  2936. pto->nextSendTimeFeeFilter = timeNow + GetRandInt(MAX_FEEFILTER_CHANGE_DELAY) * 1000000;
  2937. }
  2938. }
  2939. }
  2940. return true;
  2941. }
  2942. class CNetProcessingCleanup
  2943. {
  2944. public:
  2945. CNetProcessingCleanup() {}
  2946. ~CNetProcessingCleanup() {
  2947. // orphan transactions
  2948. mapOrphanTransactions.clear();
  2949. mapOrphanTransactionsByPrev.clear();
  2950. }
  2951. } instance_of_cnetprocessingcleanup;