Skip to content

Commit 341735e

Browse files
committed
Headers-first synchronization
Many changes: * Do not use 'getblocks', but 'getheaders', and use it to build a headers tree. * Blocks are fetched in parallel from all available outbound peers, using a limited moving window. When one peer stalls the movement of the window, it is disconnected. * No more orphan blocks. At all. We only ever request a block for which we have verified the headers, and store it to disk immediately. This means that a disk-fill attack would require PoW. * Require protocol version 31800 for every peer (released in december 2010). * No more syncnode (we sync from everyone we can, though limited to 1 during initial *headers* sync). * Introduce some extra named constants, comments and asserts.
1 parent 992ab87 commit 341735e

File tree

9 files changed

+375
-370
lines changed

9 files changed

+375
-370
lines changed

src/chain.h

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,29 @@ struct CDiskBlockPos
4949
};
5050

5151
enum BlockStatus {
52+
// Unused.
5253
BLOCK_VALID_UNKNOWN = 0,
53-
BLOCK_VALID_HEADER = 1, // parsed, version ok, hash satisfies claimed PoW, 1 <= vtx count <= max, timestamp not in future
54-
BLOCK_VALID_TREE = 2, // parent found, difficulty matches, timestamp >= median previous, checkpoint
55-
BLOCK_VALID_TRANSACTIONS = 3, // only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid, no duplicate txids, sigops, size, merkle root
56-
BLOCK_VALID_CHAIN = 4, // outputs do not overspend inputs, no double spends, coinbase output ok, immature coinbase spends, BIP30
57-
BLOCK_VALID_SCRIPTS = 5, // scripts/signatures ok
54+
55+
// Parsed, version ok, hash satisfies claimed PoW, 1 <= vtx count <= max, timestamp not in future
56+
BLOCK_VALID_HEADER = 1,
57+
58+
// All parent headers found, difficulty matches, timestamp >= median previous, checkpoint. Implies all parents
59+
// are also at least TREE.
60+
BLOCK_VALID_TREE = 2,
61+
62+
// Only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid, no duplicate txids,
63+
// sigops, size, merkle root. Implies all parents are at least TREE but not necessarily TRANSACTIONS. When all
64+
// parent blocks also have TRANSACTIONS, CBlockIndex::nChainTx will be set.
65+
BLOCK_VALID_TRANSACTIONS = 3,
66+
67+
// Outputs do not overspend inputs, no double spends, coinbase output ok, immature coinbase spends, BIP30.
68+
// Implies all parents are also at least CHAIN.
69+
BLOCK_VALID_CHAIN = 4,
70+
71+
// Scripts & signatures ok. Implies all parents are also at least SCRIPTS.
72+
BLOCK_VALID_SCRIPTS = 5,
73+
74+
// All validity bits.
5875
BLOCK_VALID_MASK = BLOCK_VALID_HEADER | BLOCK_VALID_TREE | BLOCK_VALID_TRANSACTIONS |
5976
BLOCK_VALID_CHAIN | BLOCK_VALID_SCRIPTS,
6077

@@ -103,7 +120,8 @@ class CBlockIndex
103120
// Note: in a potential headers-first mode, this number cannot be relied upon
104121
unsigned int nTx;
105122

106-
// (memory only) Number of transactions in the chain up to and including this block
123+
// (memory only) Number of transactions in the chain up to and including this block.
124+
// This value will be non-zero only if and only if transactions for this block and all its parents are available.
107125
unsigned int nChainTx; // change to 64-bit type when necessary; won't happen before 2030
108126

109127
// Verification status of this block. See enum BlockStatus
@@ -146,7 +164,7 @@ class CBlockIndex
146164
SetNull();
147165
}
148166

149-
CBlockIndex(CBlockHeader& block)
167+
CBlockIndex(const CBlockHeader& block)
150168
{
151169
SetNull();
152170

0 commit comments

Comments
 (0)