diff options
| author | Bitflash <btf01@proton.me> | 2026-07-31 14:26:50 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-07-31 14:26:50 -0300 |
| commit | e2d5c1bbe01370dbf75e2118d697a159c4ee582b (patch) | |
| tree | f109392ac356c46bdb248c554110813c8f7ad19c /src/net.h | |
| parent | 3268ea95c1443238d380dea26e7b1bb3bd108aed (diff) | |
net: bound orphan blocks and stop asking a peer the same question twice (#70)
Two defects that feed each other. Measured on a 32-core production node:
1287 of 4117 processed blocks arrived with a missing parent, 97 arrived
that the node already had, and 490 transactions arrived whose inputs were
unknown.
mapOrphanBlocks had no ceiling at all. Orphan transactions have had one
since 0.1.0 -- MAX_ORPHAN_TRANSACTIONS with LimitOrphanTx -- and blocks
never did. An orphan block whose parent never turns up stays in memory for
the life of the process, each one holding a whole block, and a peer can
send an unlimited number of them just by inventing the previous-block hash.
LimitOrphanBlocks mirrors the transaction one: random eviction, both maps
kept in step, and the CBlock deleted because mapOrphanBlocks owns it.
Every orphan block also fired a getblocks at the sender, and an inv for a
block already held as an orphan fired another, with no filtering. So a node
missing part of the chain asked the same peer the same question over and
over, and the duplicate answers made more orphans, which asked again.
CNode::PushGetBlocks remembers the last request and drops a repeat -- the
same fix Bitcoin made in e2c2648c1.
One trap found by testing rather than by reading: eviction picks at random
and may pick the block just inserted, so GetOrphanRoot has to be read out
of pblock before LimitOrphanBlocks runs, not after. The first version had
that backwards and would have read freed memory.
Verified with fabricated orphans, four of the seven sharing one
hashPrevBlock so the multimap path is exercised: 7 in, limit(3) leaves 3,
limit(0) leaves 0, both maps in step at every stage, no crash. Then a run
against the live network to 4348 with no scaffolding left behind.
The storm itself was not reproducible on demand -- the network is healthy
today and produced one orphan block in seven minutes -- so there is no
before/after on the metric that motivated this.
Diffstat (limited to 'src/net.h')
| -rw-r--r-- | src/net.h | 26 |
1 files changed, 26 insertions, 0 deletions
| @@ -7,6 +7,9 @@ class CAddress; | |||
| 7 | class CInv; | 7 | class CInv; |
| 8 | class CRequestTracker; | 8 | class CRequestTracker; |
| 9 | class CNode; | 9 | class CNode; |
| 10 | // Defined in main.h, which is included after this file. Only ever used here as | ||
| 11 | // a pointer, so the forward declaration is enough. | ||
| 12 | class CBlockIndex; | ||
| 10 | 13 | ||
| 11 | 14 | ||
| 12 | 15 | ||
| @@ -533,6 +536,11 @@ public: | |||
| 533 | // Last "btfpeers" we accepted from this node, to rate-limit the exchange. | 536 | // Last "btfpeers" we accepted from this node, to rate-limit the exchange. |
| 534 | int64 nLastPexRecv; | 537 | int64 nLastPexRecv; |
| 535 | 538 | ||
| 539 | // Last getblocks we sent this peer, so we do not ask the same question | ||
| 540 | // over and over. See CNode::PushGetBlocks. | ||
| 541 | CBlockIndex* pindexLastGetBlocksBegin; | ||
| 542 | uint256 hashLastGetBlocksEnd; | ||
| 543 | |||
| 536 | // Liveness. A peer whose network path dies silently -- NAT drops an idle | 544 | // Liveness. A peer whose network path dies silently -- NAT drops an idle |
| 537 | // mapping, a relay restarts, a route changes -- never sends FIN, so the | 545 | // mapping, a relay restarts, a route changes -- never sends FIN, so the |
| 538 | // socket stays readable-never and the node simply stops hearing from it | 546 | // socket stays readable-never and the node simply stops hearing from it |
| @@ -565,6 +573,8 @@ public: | |||
| 565 | nRefCount = 0; | 573 | nRefCount = 0; |
| 566 | nReleaseTime = 0; | 574 | nReleaseTime = 0; |
| 567 | nLastPexRecv = 0; | 575 | nLastPexRecv = 0; |
| 576 | pindexLastGetBlocksBegin = NULL; | ||
| 577 | hashLastGetBlocksEnd = 0; | ||
| 568 | nTimeConnected = GetTime(); | 578 | nTimeConnected = GetTime(); |
| 569 | nLastSend = 0; | 579 | nLastSend = 0; |
| 570 | nLastRecv = 0; | 580 | nLastRecv = 0; |
| @@ -717,6 +727,22 @@ public: | |||
| 717 | 727 | ||
| 718 | 728 | ||
| 719 | 729 | ||
| 730 | // Ask this peer to fill in blocks we are missing, but only once per | ||
| 731 | // question. | ||
| 732 | // | ||
| 733 | // Every orphan block fires one of these, and an inv for a block we already | ||
| 734 | // hold as an orphan fires another. A node that is missing part of the | ||
| 735 | // chain therefore asks the same peer the same thing repeatedly: measured | ||
| 736 | // on a production node, 1287 orphan blocks out of 4117 processed, and 97 | ||
| 737 | // "already have block" from the answers coming back more than once. The | ||
| 738 | // duplicate answers create more orphans, which fire more requests. | ||
| 739 | // | ||
| 740 | // Remembering the last question asked breaks that loop. Same fix Bitcoin | ||
| 741 | // made in e2c2648c1. | ||
| 742 | // Body lives in net.cpp: it builds a CBlockLocator, which main.h defines, | ||
| 743 | // and main.h is included after this file. | ||
| 744 | void PushGetBlocks(CBlockIndex* pindexBegin, uint256 hashEnd); | ||
| 745 | |||
| 720 | void PushMessage(const char* pszCommand) | 746 | void PushMessage(const char* pszCommand) |
| 721 | { | 747 | { |
| 722 | try | 748 | try |