Prepare block connection logic for headers-first.

This changes the block processing logic from "try to atomically switch
to a new block" to a continuous "(dis)connect a block, aiming for the
assumed best chain".

This means the smallest atomic operations on the chainstate become
individual block connections or disconnections, instead of entire
reorganizations. It may mean that we try to reorganize to one block,
fail, and rereorganize again to the old block. This is slower, but
doesn't require unbounded RAM.

It also means that a ConnectBlock which fails may be no longer called
from the ProcessBlock which knows which node sent it. To deal with that,
a mapBlockSource is kept, and invalid blocks cause asynchronous "reject"
messages and banning (if necessary).
This commit is contained in:
Pieter Wuille
2013-11-16 19:28:24 +01:00
parent 0ec16f35d6
commit 75f51f2a63
3 changed files with 265 additions and 213 deletions

View File

@@ -165,10 +165,8 @@ bool IsInitialBlockDownload();
std::string GetWarnings(std::string strFor);
/** Retrieve a transaction (from memory pool, or from disk, if possible) */
bool GetTransaction(const uint256 &hash, CTransaction &tx, uint256 &hashBlock, bool fAllowSlow = false);
/** Connect/disconnect blocks until pindexNew is the new tip of the active block chain */
bool SetBestChain(CValidationState &state, CBlockIndex* pindexNew);
/** Find the best known block, and make it the tip of the block chain */
bool ConnectBestBlock(CValidationState &state);
bool ActivateBestChain(CValidationState &state);
int64_t GetBlockValue(int nHeight, int64_t nFees);
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock);
@@ -716,6 +714,8 @@ public:
unsigned int nBits;
unsigned int nNonce;
// (memory only) Sequencial id assigned to distinguish order in which blocks are received.
uint32_t nSequenceId;
CBlockIndex()
{
@@ -729,6 +729,7 @@ public:
nTx = 0;
nChainTx = 0;
nStatus = 0;
nSequenceId = 0;
nVersion = 0;
hashMerkleRoot = 0;
@@ -749,6 +750,7 @@ public:
nTx = 0;
nChainTx = 0;
nStatus = 0;
nSequenceId = 0;
nVersion = block.nVersion;
hashMerkleRoot = block.hashMerkleRoot;
@@ -958,23 +960,23 @@ public:
AbortNode(msg);
return Error();
}
bool IsValid() {
bool IsValid() const {
return mode == MODE_VALID;
}
bool IsInvalid() {
bool IsInvalid() const {
return mode == MODE_INVALID;
}
bool IsError() {
bool IsError() const {
return mode == MODE_ERROR;
}
bool IsInvalid(int &nDoSOut) {
bool IsInvalid(int &nDoSOut) const {
if (IsInvalid()) {
nDoSOut = nDoS;
return true;
}
return false;
}
bool CorruptionPossible() {
bool CorruptionPossible() const {
return corruptionPossible;
}
unsigned char GetRejectCode() const { return chRejectCode; }
@@ -1041,6 +1043,8 @@ public:
/** The currently-connected chain of blocks. */
extern CChain chainActive;
/** The currently best known chain of headers (some of which may be invalid). */
extern CChain chainMostWork;
/** Global variable that points to the active CCoinsView (protected by cs_main) */
extern CCoinsViewCache *pcoinsTip;