merge bitcoin#23769: Disallow copies of CChain

This commit is contained in:
Kittywhiskers Van Gogh 2021-12-14 12:39:29 +01:00
parent 5b66688160
commit b4bfacfd24
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD

View File

@ -59,37 +59,40 @@ public:
READWRITE(VARINT(obj.nTimeLast)); READWRITE(VARINT(obj.nTimeLast));
} }
void SetNull() { void SetNull()
nBlocks = 0; {
nSize = 0; nBlocks = 0;
nUndoSize = 0; nSize = 0;
nHeightFirst = 0; nUndoSize = 0;
nHeightLast = 0; nHeightFirst = 0;
nTimeFirst = 0; nHeightLast = 0;
nTimeLast = 0; nTimeFirst = 0;
} nTimeLast = 0;
}
CBlockFileInfo() { CBlockFileInfo()
SetNull(); {
} SetNull();
}
std::string ToString() const; std::string ToString() const;
/** update statistics (does not update nSize) */ /** update statistics (does not update nSize) */
void AddBlock(unsigned int nHeightIn, uint64_t nTimeIn) { void AddBlock(unsigned int nHeightIn, uint64_t nTimeIn)
if (nBlocks==0 || nHeightFirst > nHeightIn) {
nHeightFirst = nHeightIn; if (nBlocks == 0 || nHeightFirst > nHeightIn)
if (nBlocks==0 || nTimeFirst > nTimeIn) nHeightFirst = nHeightIn;
nTimeFirst = nTimeIn; if (nBlocks == 0 || nTimeFirst > nTimeIn)
nBlocks++; nTimeFirst = nTimeIn;
if (nHeightIn > nHeightLast) nBlocks++;
nHeightLast = nHeightIn; if (nHeightIn > nHeightLast)
if (nTimeIn > nTimeLast) nHeightLast = nHeightIn;
nTimeLast = nTimeIn; if (nTimeIn > nTimeLast)
} nTimeLast = nTimeIn;
}
}; };
enum BlockStatus: uint32_t { enum BlockStatus : uint32_t {
//! Unused. //! Unused.
BLOCK_VALID_UNKNOWN = 0, BLOCK_VALID_UNKNOWN = 0,
@ -226,7 +229,7 @@ public:
FlatFilePos ret; FlatFilePos ret;
if (nStatus & BLOCK_HAVE_DATA) { if (nStatus & BLOCK_HAVE_DATA) {
ret.nFile = nFile; ret.nFile = nFile;
ret.nPos = nDataPos; ret.nPos = nDataPos;
} }
return ret; return ret;
} }
@ -237,7 +240,7 @@ public:
FlatFilePos ret; FlatFilePos ret;
if (nStatus & BLOCK_HAVE_UNDO) { if (nStatus & BLOCK_HAVE_UNDO) {
ret.nFile = nFile; ret.nFile = nFile;
ret.nPos = nUndoPos; ret.nPos = nUndoPos;
} }
return ret; return ret;
} }
@ -245,13 +248,13 @@ public:
CBlockHeader GetBlockHeader() const CBlockHeader GetBlockHeader() const
{ {
CBlockHeader block; CBlockHeader block;
block.nVersion = nVersion; block.nVersion = nVersion;
if (pprev) if (pprev)
block.hashPrevBlock = pprev->GetBlockHash(); block.hashPrevBlock = pprev->GetBlockHash();
block.hashMerkleRoot = hashMerkleRoot; block.hashMerkleRoot = hashMerkleRoot;
block.nTime = nTime; block.nTime = nTime;
block.nBits = nBits; block.nBits = nBits;
block.nNonce = nNonce; block.nNonce = nNonce;
return block; return block;
} }
@ -293,7 +296,7 @@ public:
*(--pbegin) = pindex->GetBlockTime(); *(--pbegin) = pindex->GetBlockTime();
std::sort(pbegin, pend); std::sort(pbegin, pend);
return pbegin[(pend - pbegin)/2]; return pbegin[(pend - pbegin) / 2];
} }
std::string ToString() const; std::string ToString() const;
@ -360,12 +363,14 @@ public:
uint256 hash; uint256 hash;
uint256 hashPrev; uint256 hashPrev;
CDiskBlockIndex() { CDiskBlockIndex()
{
hash = uint256(); hash = uint256();
hashPrev = uint256(); hashPrev = uint256();
} }
explicit CDiskBlockIndex(const CBlockIndex* pindex) : CBlockIndex(*pindex) { explicit CDiskBlockIndex(const CBlockIndex* pindex) : CBlockIndex(*pindex)
{
hash = (hash == uint256() ? pindex->GetBlockHash() : hash); hash = (hash == uint256() ? pindex->GetBlockHash() : hash);
hashPrev = (pprev ? pprev->GetBlockHash() : uint256()); hashPrev = (pprev ? pprev->GetBlockHash() : uint256());
} }
@ -399,12 +404,12 @@ public:
if(hash != uint256()) return hash; if(hash != uint256()) return hash;
// should never really get here, keeping this as a fallback // should never really get here, keeping this as a fallback
CBlockHeader block; CBlockHeader block;
block.nVersion = nVersion; block.nVersion = nVersion;
block.hashPrevBlock = hashPrev; block.hashPrevBlock = hashPrev;
block.hashMerkleRoot = hashMerkleRoot; block.hashMerkleRoot = hashMerkleRoot;
block.nTime = nTime; block.nTime = nTime;
block.nBits = nBits; block.nBits = nBits;
block.nNonce = nNonce; block.nNonce = nNonce;
return block.GetHash(); return block.GetHash();
} }
@ -413,35 +418,45 @@ public:
}; };
/** An in-memory indexed chain of blocks. */ /** An in-memory indexed chain of blocks. */
class CChain { class CChain
{
private: private:
std::vector<CBlockIndex*> vChain; std::vector<CBlockIndex*> vChain;
public: public:
CChain() = default;
CChain(const CChain&) = delete;
CChain& operator=(const CChain&) = delete;
/** Returns the index entry for the genesis block of this chain, or nullptr if none. */ /** Returns the index entry for the genesis block of this chain, or nullptr if none. */
CBlockIndex *Genesis() const { CBlockIndex* Genesis() const
{
return vChain.size() > 0 ? vChain[0] : nullptr; return vChain.size() > 0 ? vChain[0] : nullptr;
} }
/** Returns the index entry for the tip of this chain, or nullptr if none. */ /** Returns the index entry for the tip of this chain, or nullptr if none. */
CBlockIndex *Tip() const { CBlockIndex* Tip() const
{
return vChain.size() > 0 ? vChain[vChain.size() - 1] : nullptr; return vChain.size() > 0 ? vChain[vChain.size() - 1] : nullptr;
} }
/** Returns the index entry at a particular height in this chain, or nullptr if no such height exists. */ /** Returns the index entry at a particular height in this chain, or nullptr if no such height exists. */
CBlockIndex *operator[](int nHeight) const { CBlockIndex* operator[](int nHeight) const
{
if (nHeight < 0 || nHeight >= (int)vChain.size()) if (nHeight < 0 || nHeight >= (int)vChain.size())
return nullptr; return nullptr;
return vChain[nHeight]; return vChain[nHeight];
} }
/** Efficiently check whether a block is present in this chain. */ /** Efficiently check whether a block is present in this chain. */
bool Contains(const CBlockIndex *pindex) const { bool Contains(const CBlockIndex* pindex) const
{
return (*this)[pindex->nHeight] == pindex; return (*this)[pindex->nHeight] == pindex;
} }
/** Find the successor of a block in this chain, or nullptr if the given index is not found or is the tip. */ /** Find the successor of a block in this chain, or nullptr if the given index is not found or is the tip. */
CBlockIndex *Next(const CBlockIndex *pindex) const { CBlockIndex* Next(const CBlockIndex* pindex) const
{
if (Contains(pindex)) if (Contains(pindex))
return (*this)[pindex->nHeight + 1]; return (*this)[pindex->nHeight + 1];
else else
@ -449,18 +464,19 @@ public:
} }
/** Return the maximal height in the chain. Is equal to chain.Tip() ? chain.Tip()->nHeight : -1. */ /** Return the maximal height in the chain. Is equal to chain.Tip() ? chain.Tip()->nHeight : -1. */
int Height() const { int Height() const
{
return vChain.size() - 1; return vChain.size() - 1;
} }
/** Set/initialize a chain with a given tip. */ /** Set/initialize a chain with a given tip. */
void SetTip(CBlockIndex *pindex); void SetTip(CBlockIndex* pindex);
/** Return a CBlockLocator that refers to a block in this chain (by default the tip). */ /** Return a CBlockLocator that refers to a block in this chain (by default the tip). */
CBlockLocator GetLocator(const CBlockIndex *pindex = nullptr) const; CBlockLocator GetLocator(const CBlockIndex* pindex = nullptr) const;
/** Find the last common block between this chain and a block index entry. */ /** Find the last common block between this chain and a block index entry. */
const CBlockIndex *FindFork(const CBlockIndex *pindex) const; const CBlockIndex* FindFork(const CBlockIndex* pindex) const;
/** Find the earliest block with timestamp equal or greater than the given time and height equal or greater than the given height. */ /** Find the earliest block with timestamp equal or greater than the given time and height equal or greater than the given height. */
CBlockIndex* FindEarliestAtLeast(int64_t nTime, int height) const; CBlockIndex* FindEarliestAtLeast(int64_t nTime, int height) const;