diff --git a/src/Makefile.am b/src/Makefile.am --- a/src/Makefile.am +++ b/src/Makefile.am @@ -116,6 +116,7 @@ core_io.h \ core_memusage.h \ cuckoocache.h \ + diskblockindex.h \ diskblockpos.h \ dstencode.h \ fs.h \ diff --git a/src/blockindex.h b/src/blockindex.h --- a/src/blockindex.h +++ b/src/blockindex.h @@ -220,4 +220,3 @@ }; #endif // BITCOIN_BLOCKINDEX_H - diff --git a/src/chain.h b/src/chain.h --- a/src/chain.h +++ b/src/chain.h @@ -61,68 +61,6 @@ const CBlockIndex *LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb); -/** Used to marshal pointers into hashes for db storage. */ -class CDiskBlockIndex : public CBlockIndex { -public: - uint256 hashPrev; - - CDiskBlockIndex() { hashPrev = uint256(); } - - explicit CDiskBlockIndex(const CBlockIndex *pindex) : CBlockIndex(*pindex) { - hashPrev = (pprev ? pprev->GetBlockHash() : uint256()); - } - - ADD_SERIALIZE_METHODS; - - template - inline void SerializationOp(Stream &s, Operation ser_action) { - int nVersion = s.GetVersion(); - if (!(s.GetType() & SER_GETHASH)) { - READWRITE(VARINT(nVersion)); - } - - READWRITE(VARINT(nHeight)); - READWRITE(nStatus); - READWRITE(VARINT(nTx)); - if (nStatus.hasData() || nStatus.hasUndo()) { - READWRITE(VARINT(nFile)); - } - if (nStatus.hasData()) { - READWRITE(VARINT(nDataPos)); - } - if (nStatus.hasUndo()) { - READWRITE(VARINT(nUndoPos)); - } - - // block header - READWRITE(this->nVersion); - READWRITE(hashPrev); - READWRITE(hashMerkleRoot); - READWRITE(nTime); - READWRITE(nBits); - READWRITE(nNonce); - } - - uint256 GetBlockHash() const { - CBlockHeader block; - block.nVersion = nVersion; - block.hashPrevBlock = hashPrev; - block.hashMerkleRoot = hashMerkleRoot; - block.nTime = nTime; - block.nBits = nBits; - block.nNonce = nNonce; - return block.GetHash(); - } - - std::string ToString() const { - std::string str = "CDiskBlockIndex("; - str += CBlockIndex::ToString(); - str += strprintf("\n hashBlock=%s, hashPrev=%s)", - GetBlockHash().ToString(), hashPrev.ToString()); - return str; - } -}; - /** * An in-memory indexed chain of blocks. */ diff --git a/src/diskblockindex.h b/src/diskblockindex.h new file mode 100644 --- /dev/null +++ b/src/diskblockindex.h @@ -0,0 +1,77 @@ +// Copyright (c) 2018 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_DISKBLOCKINDEX_H +#define BITCOIN_DISKBLOCKINDEX_H + +#include "blockindex.h" +#include "serialize.h" +#include "tinyformat.h" +#include "uint256.h" + +class CBlockIndex; + +/** Used to marshal pointers into hashes for db storage. */ +class CDiskBlockIndex : public CBlockIndex { +public: + uint256 hashPrev; + + CDiskBlockIndex() { hashPrev = uint256(); } + + explicit CDiskBlockIndex(const CBlockIndex *pindex) : CBlockIndex(*pindex) { + hashPrev = (pprev ? pprev->GetBlockHash() : uint256()); + } + + ADD_SERIALIZE_METHODS; + + template + inline void SerializationOp(Stream &s, Operation ser_action) { + int nVersion = s.GetVersion(); + if (!(s.GetType() & SER_GETHASH)) { + READWRITE(VARINT(nVersion)); + } + + READWRITE(VARINT(nHeight)); + READWRITE(nStatus); + READWRITE(VARINT(nTx)); + if (nStatus.hasData() || nStatus.hasUndo()) { + READWRITE(VARINT(nFile)); + } + if (nStatus.hasData()) { + READWRITE(VARINT(nDataPos)); + } + if (nStatus.hasUndo()) { + READWRITE(VARINT(nUndoPos)); + } + + // block header + READWRITE(this->nVersion); + READWRITE(hashPrev); + READWRITE(hashMerkleRoot); + READWRITE(nTime); + READWRITE(nBits); + READWRITE(nNonce); + } + + uint256 GetBlockHash() const { + CBlockHeader block; + block.nVersion = nVersion; + block.hashPrevBlock = hashPrev; + block.hashMerkleRoot = hashMerkleRoot; + block.nTime = nTime; + block.nBits = nBits; + block.nNonce = nNonce; + return block.GetHash(); + } + + std::string ToString() const { + std::string str = "CDiskBlockIndex("; + str += CBlockIndex::ToString(); + str += strprintf("\n hashBlock=%s, hashPrev=%s)", + GetBlockHash().ToString(), hashPrev.ToString()); + return str; + } +}; + +#endif // BITCOIN_DISKBLOCKINDEX_H diff --git a/src/test/test_bitcoin_fuzzy.cpp b/src/test/test_bitcoin_fuzzy.cpp --- a/src/test/test_bitcoin_fuzzy.cpp +++ b/src/test/test_bitcoin_fuzzy.cpp @@ -7,10 +7,10 @@ #endif #include "addrman.h" -#include "chain.h" #include "coins.h" #include "compressor.h" #include "consensus/merkle.h" +#include "diskblockindex.h" #include "net.h" #include "primitives/block.h" #include "protocol.h" diff --git a/src/txdb.cpp b/src/txdb.cpp --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -7,6 +7,7 @@ #include "chain.h" #include "chainparams.h" +#include "diskblockindex.h" #include "hash.h" #include "init.h" #include "pow.h"