PRCYCoin  2.0.0.7rc1
P2P Digital Currency
txdb.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Copyright (c) 2015-2018 The PIVX developers
4 // Copyright (c) 2018-2020 The DAPS Project developers
5 // Distributed under the MIT software license, see the accompanying
6 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 
8 #ifndef BITCOIN_TXDB_H
9 #define BITCOIN_TXDB_H
10 
11 #include "dbwrapper.h"
12 #include "main.h"
13 
14 #include <map>
15 #include <string>
16 #include <utility>
17 #include <vector>
18 
19 class CCoins;
20 class uint256;
21 
23 static const int64_t nDefaultDbCache = 100;
25 static const int64_t nMaxDbCache = sizeof(void*) > 4 ? 16384 : 1024;
27 static const int64_t nMinDbCache = 4;
28 
29 struct CDiskTxPos : public CDiskBlockPos {
30  unsigned int nTxOffset; // after header
31 
33 
34  template <typename Stream, typename Operation>
35  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
36  {
37  READWRITE(*(CDiskBlockPos*)this);
39  }
40 
41  CDiskTxPos(const CDiskBlockPos& blockIn, unsigned int nTxOffsetIn) : CDiskBlockPos(blockIn.nFile, blockIn.nPos), nTxOffset(nTxOffsetIn)
42  {
43  }
44 
46  {
47  SetNull();
48  }
49 
50  void SetNull()
51  {
53  nTxOffset = 0;
54  }
55 };
56 
58 class CCoinsViewDB : public CCoinsView
59 {
60 protected:
62 
63 public:
64  CCoinsViewDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
65 
66  bool GetCoins(const uint256& txid, CCoins& coins) const;
67  bool HaveCoins(const uint256& txid) const;
68  uint256 GetBestBlock() const;
69  bool BatchWrite(CCoinsMap& mapCoins, const uint256& hashBlock);
70  bool GetStats(CCoinsStats& stats) const;
71 };
72 
74 class CBlockTreeDB : public CDBWrapper
75 {
76 public:
77  CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
78 
79 private:
80  CBlockTreeDB(const CBlockTreeDB&);
81  void operator=(const CBlockTreeDB&);
82 
83 public:
84  bool WriteBlockIndex(const CDiskBlockIndex& blockindex);
85  bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
86  bool ReadBlockFileInfo(int nFile, CBlockFileInfo& fileinfo);
87  bool ReadLastBlockFile(int& nFile);
88  bool WriteReindexing(bool fReindex);
89  bool ReadReindexing(bool& fReindex);
90  bool ReadTxIndex(const uint256& txid, CDiskTxPos& pos);
91  bool WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> >& list);
92  bool WriteFlag(const std::string& name, bool fValue);
93  bool ReadFlag(const std::string& name, bool& fValue);
94  bool WriteInt(const std::string& name, int nValue);
95  bool ReadInt(const std::string& name, int& nValue);
96  bool LoadBlockIndexGuts();
97 
98  bool ReadKeyImage(const std::string& keyImage, uint256& bh);
99  bool ReadKeyImages(const std::string& keyImage, std::vector<uint256>& bhs);
100 
101  bool WriteKeyImage(const std::string& keyImage, const uint256& height);
102 };
103 #endif // BITCOIN_TXDB_H
CCoinsViewDB::GetCoins
bool GetCoins(const uint256 &txid, CCoins &coins) const
Retrieve the CCoins (unspent transaction outputs) for a given txid.
Definition: txdb.cpp:35
CBlockTreeDB::WriteInt
bool WriteInt(const std::string &name, int nValue)
Definition: txdb.cpp:228
CCoinsViewDB
CCoinsView backed by the LevelDB coin database (chainstate/)
Definition: txdb.h:58
dbwrapper.h
CCoinsViewDB::GetStats
bool GetStats(CCoinsStats &stats) const
Calculate statistics about the unspent transaction output set.
Definition: txdb.cpp:111
CDiskBlockPos::SetNull
void SetNull()
Definition: chain.h:110
CBlockTreeDB::LoadBlockIndexGuts
bool LoadBlockIndexGuts()
Definition: txdb.cpp:238
CDiskBlockPos::nFile
int nFile
Definition: chain.h:77
CCoinsStats
Definition: coins.h:332
CDiskTxPos
Definition: txdb.h:29
CDiskTxPos::nTxOffset
unsigned int nTxOffset
Definition: txdb.h:30
CDiskBlockIndex
Used to marshal pointers into hashes for db storage.
Definition: chain.h:485
CBlockTreeDB::ReadReindexing
bool ReadReindexing(bool &fReindex)
Definition: txdb.cpp:100
CBlockTreeDB::operator=
void operator=(const CBlockTreeDB &)
CCoinsView
Abstract view on the open txout dataset.
Definition: coins.h:346
CBlockTreeDB::WriteBlockIndex
bool WriteBlockIndex(const CDiskBlockIndex &blockindex)
Definition: txdb.cpp:81
CCoinsViewDB::HaveCoins
bool HaveCoins(const uint256 &txid) const
Just check whether we have data for a given txid.
Definition: txdb.cpp:40
CBlockTreeDB::WriteBatchSync
bool WriteBatchSync(const std::vector< std::pair< int, const CBlockFileInfo * > > &fileInfo, int nLastFile, const std::vector< const CBlockIndex * > &blockinfo)
Definition: txdb.cpp:156
VARINT
#define VARINT(obj)
Definition: serialize.h:366
CBlockTreeDB::ReadBlockFileInfo
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &fileinfo)
Definition: txdb.cpp:87
CBlockTreeDB
Access to the block database (blocks/index/)
Definition: txdb.h:74
CDiskBlockPos
Definition: chain.h:76
CDiskTxPos::SetNull
void SetNull()
Definition: txdb.h:50
uint256
256-bit unsigned big integer.
Definition: uint256.h:38
CBlockTreeDB::ReadFlag
bool ReadFlag(const std::string &name, bool &fValue)
Definition: txdb.cpp:219
CCoinsViewDB::GetBestBlock
uint256 GetBestBlock() const
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: txdb.cpp:45
CCoinsViewDB::db
CDBWrapper db
Definition: txdb.h:61
CCoinsViewDB::CCoinsViewDB
CCoinsViewDB(size_t nCacheSize, bool fMemory=false, bool fWipe=false)
Definition: txdb.cpp:31
CCoinsViewDB::BatchWrite
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock)
Do a bulk modification (multiple CCoins changes + BestBlock change).
Definition: txdb.cpp:53
CBlockTreeDB::ReadInt
bool ReadInt(const std::string &name, int &nValue)
Definition: txdb.cpp:233
CBlockTreeDB::WriteTxIndex
bool WriteTxIndex(const std::vector< std::pair< uint256, CDiskTxPos > > &list)
Definition: txdb.cpp:173
name
const char * name
Definition: rest.cpp:34
CDBWrapper
Definition: dbwrapper.h:134
CBlockTreeDB::WriteReindexing
bool WriteReindexing(bool fReindex)
Definition: txdb.cpp:92
CDiskBlockPos::nPos
unsigned int nPos
Definition: chain.h:78
CBlockTreeDB::WriteFlag
bool WriteFlag(const std::string &name, bool fValue)
Definition: txdb.cpp:214
CBlockTreeDB::ReadTxIndex
bool ReadTxIndex(const uint256 &txid, CDiskTxPos &pos)
Definition: txdb.cpp:168
READWRITE
#define READWRITE(obj)
Definition: serialize.h:164
fReindex
std::atomic< bool > fReindex
Definition: main.cpp:81
main.h
CBlockFileInfo
Definition: chain.h:18
CBlockTreeDB::CBlockTreeDB
CBlockTreeDB(size_t nCacheSize, bool fMemory=false, bool fWipe=false)
Definition: txdb.cpp:77
CCoinsMap
boost::unordered_map< uint256, CCoinsCacheEntry, CCoinsKeyHasher > CCoinsMap
Definition: coins.h:330
CCoins
Definition: coins.h:77
CBlockTreeDB::ReadKeyImages
bool ReadKeyImages(const std::string &keyImage, std::vector< uint256 > &bhs)
Definition: txdb.cpp:188
CDiskTxPos::ADD_SERIALIZE_METHODS
ADD_SERIALIZE_METHODS
Definition: txdb.h:32
CDiskTxPos::CDiskTxPos
CDiskTxPos()
Definition: txdb.h:45
CBlockTreeDB::WriteKeyImage
bool WriteKeyImage(const std::string &keyImage, const uint256 &height)
Definition: txdb.cpp:201
CDiskTxPos::CDiskTxPos
CDiskTxPos(const CDiskBlockPos &blockIn, unsigned int nTxOffsetIn)
Definition: txdb.h:41
CBlockTreeDB::ReadKeyImage
bool ReadKeyImage(const std::string &keyImage, uint256 &bh)
Definition: txdb.cpp:183
CDiskTxPos::SerializationOp
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: txdb.h:35
CBlockTreeDB::ReadLastBlockFile
bool ReadLastBlockFile(int &nFile)
Definition: txdb.cpp:106