19 #include <boost/thread.hpp> 21 static const char DB_COIN =
'C';
22 static const char DB_COINS =
'c';
23 static const char DB_BLOCK_FILES =
'f';
24 static const char DB_BLOCK_INDEX =
'b';
27 static const char DB_HEAD_BLOCKS =
'H';
28 static const char DB_FLAG =
'F';
29 static const char DB_REINDEX_FLAG =
'R';
30 static const char DB_LAST_BLOCK =
'l';
37 explicit CoinEntry(
const COutPoint* ptr) : outpoint(const_cast<
COutPoint*>(ptr)), key(DB_COIN) {}
39 template<
typename Stream>
46 template<
typename Stream>
61 return db.
Read(CoinEntry(&outpoint), coin);
65 return db.
Exists(CoinEntry(&outpoint));
76 std::vector<uint256> vhashHeadBlocks;
77 if (!
db.
Read(DB_HEAD_BLOCKS, vhashHeadBlocks)) {
78 return std::vector<uint256>();
80 return vhashHeadBlocks;
87 size_t batch_size = (size_t)
gArgs.
GetArg(
"-dbbatchsize", nDefaultDbBatchSize);
88 int crash_simulate =
gArgs.
GetArg(
"-dbcrashratio", 0);
89 assert(!hashBlock.
IsNull());
92 if (old_tip.IsNull()) {
95 if (old_heads.size() == 2) {
96 assert(old_heads[0] == hashBlock);
97 old_tip = old_heads[1];
106 batch.
Write(DB_HEAD_BLOCKS, std::vector<uint256>{hashBlock, old_tip});
108 for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) {
110 CoinEntry entry(&it->first);
111 if (it->second.coin.IsSpent())
114 batch.
Write(entry, it->second.coin);
118 CCoinsMap::iterator itOld = it++;
119 mapCoins.erase(itOld);
124 if (crash_simulate) {
126 if (rng.
randrange(crash_simulate) == 0) {
127 LogPrintf(
"Simulating a crash. Goodbye.\n");
135 batch.
Erase(DB_HEAD_BLOCKS);
140 LogPrint(
BCLog::COINDB,
"Committed %u changed transaction outputs (out of %u) to coin database...\n", (
unsigned int)changed, (
unsigned int)count);
153 return Read(std::make_pair(DB_BLOCK_FILES, nFile), info);
158 return Write(DB_REINDEX_FLAG,
'1');
160 return Erase(DB_REINDEX_FLAG);
164 fReindexing =
Exists(DB_REINDEX_FLAG);
168 return Read(DB_LAST_BLOCK, nFile);
180 CoinEntry entry(&i->
keyTmp.second);
182 i->
keyTmp.first = entry.key;
192 if (
keyTmp.first == DB_COIN) {
201 return pcursor->GetValue(coin);
206 return pcursor->GetValueSize();
211 return keyTmp.first == DB_COIN;
217 CoinEntry entry(&
keyTmp.second);
225 bool CBlockTreeDB::WriteBatchSync(
const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo,
int nLastFile,
const std::vector<const CBlockIndex*>& blockinfo) {
227 for (std::vector<std::pair<int, const CBlockFileInfo*> >::const_iterator it=fileInfo.begin(); it != fileInfo.end(); it++) {
228 batch.
Write(std::make_pair(DB_BLOCK_FILES, it->first), *it->second);
230 batch.
Write(DB_LAST_BLOCK, nLastFile);
231 for (std::vector<const CBlockIndex*>::const_iterator it=blockinfo.begin(); it != blockinfo.end(); it++) {
238 return Write(std::make_pair(DB_FLAG,
name), fValue ?
'1' :
'0');
243 if (!
Read(std::make_pair(DB_FLAG,
name), ch))
251 std::unique_ptr<CDBIterator> pcursor(
NewIterator());
253 pcursor->Seek(std::make_pair(DB_BLOCK_INDEX,
uint256()));
256 while (pcursor->Valid()) {
257 boost::this_thread::interruption_point();
258 std::pair<char, uint256> key;
259 if (pcursor->GetKey(key) && key.first == DB_BLOCK_INDEX) {
261 if (pcursor->GetValue(diskindex)) {
275 pindexNew->
nTx = diskindex.
nTx;
278 return error(
"%s: CheckProofOfWork failed: %s", __func__, pindexNew->
ToString());
282 return error(
"%s: failed to read value", __func__);
302 std::vector<CTxOut> vout;
308 CCoins() : fCoinBase(false), vout(0), nHeight(0) { }
310 template<
typename Stream>
312 unsigned int nCode = 0;
314 unsigned int nVersionDummy;
318 fCoinBase = nCode & 1;
319 std::vector<bool> vAvail(2,
false);
320 vAvail[0] = (nCode & 2) != 0;
321 vAvail[1] = (nCode & 4) != 0;
322 unsigned int nMaskCode = (nCode / 8) + ((nCode & 6) != 0 ? 0 : 1);
324 while (nMaskCode > 0) {
325 unsigned char chAvail = 0;
327 for (
unsigned int p = 0; p < 8; p++) {
328 bool f = (chAvail & (1 << p)) != 0;
335 vout.assign(vAvail.size(),
CTxOut());
336 for (
unsigned int i = 0; i < vAvail.size(); i++) {
353 pcursor->Seek(std::make_pair(DB_COINS,
uint256()));
354 if (!pcursor->Valid()) {
359 LogPrintf(
"Upgrading utxo-set database...\n");
360 LogPrintf(
"[0%%]...");
361 uiInterface.ShowProgress(
_(
"Upgrading UTXO database"), 0,
true);
362 size_t batch_size = 1 << 24;
365 std::pair<unsigned char, uint256> key;
366 std::pair<unsigned char, uint256> prev_key = {DB_COINS,
uint256()};
367 while (pcursor->Valid()) {
368 boost::this_thread::interruption_point();
372 if (pcursor->GetKey(key) && key.first == DB_COINS) {
373 if (count++ % 256 == 0) {
374 uint32_t high = 0x100 * *key.second.begin() + *(key.second.begin() + 1);
375 int percentageDone = (int)(high * 100.0 / 65536.0 + 0.5);
376 uiInterface.ShowProgress(
_(
"Upgrading UTXO database"), percentageDone,
true);
377 if (reportDone < percentageDone/10) {
379 LogPrintf(
"[%d%%]...", percentageDone);
380 reportDone = percentageDone/10;
384 if (!pcursor->GetValue(old_coins)) {
385 return error(
"%s: cannot parse CCoins record", __func__);
388 for (
size_t i = 0; i < old_coins.vout.size(); ++i) {
389 if (!old_coins.vout[i].IsNull() && !old_coins.vout[i].scriptPubKey.IsUnspendable()) {
390 Coin newcoin(std::move(old_coins.vout[i]), old_coins.nHeight, old_coins.fCoinBase);
392 CoinEntry entry(&outpoint);
393 batch.
Write(entry, newcoin);
bool Exists(const K &key) const
bool GetValue(Coin &coin) const override
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
std::string ToString() const
bool Upgrade()
Attempt to update from an older database format. Returns whether an error occurred.
Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB.
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Batch of changes queued to be written to a CDBWrapper.
uint32_t nStatus
Verification status of this block. See enum BlockStatus.
bool ReadLastBlockFile(int &nFile)
wrapper for CTxOut that provides a more compact serialization
UniValue ret(UniValue::VARR)
std::unique_ptr< CDBIterator > pcursor
void ReadReindexing(bool &fReindexing)
uint64_t randrange(uint64_t range)
Generate a random integer in the range [0..range).
int nFile
Which # file this block is stored in (blk?????.dat)
bool GetKey(COutPoint &key) const override
bool WriteReindexing(bool fReindexing)
CDBIterator * NewIterator()
void Serialize(Stream &s, char a)
uint256 GetBlockHash() const
bool Erase(const K &key, bool fSync=false)
unsigned int nDataPos
Byte offset within blk?????.dat where this block's data is stored.
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
void Write(const K &key, const V &value)
size_t SizeEstimate() const
An output of a transaction.
Used to marshal pointers into hashes for db storage.
size_t EstimateSize() const override
Estimate database size (0 if not implemented)
Parameters that influence chain consensus.
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params ¶ms)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
An outpoint - a combination of a transaction hash and an index n into its vout.
std::pair< char, COutPoint > keyTmp
bool Read(const K &key, V &value) const
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
bool ReadFlag(const std::string &name, bool &fValue)
CBlockTreeDB(size_t nCacheSize, bool fMemory=false, bool fWipe=false)
bool WriteBatchSync(const std::vector< std::pair< int, const CBlockFileInfo *> > &fileInfo, int nLastFile, const std::vector< const CBlockIndex *> &blockinfo)
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info)
const fs::path & GetBlocksDir(bool fNetSpecific)
CCoinsViewDB(size_t nCacheSize, bool fMemory=false, bool fWipe=false)
unsigned int nUndoPos
Byte offset within rev?????.dat where this block's undo data is stored.
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
int32_t nVersion
block header
bool Write(const K &key, const V &value, bool fSync=false)
void CompactRange(const K &key_begin, const K &key_end) const
Compact a certain range of keys in the database.
The block chain is a tree shaped structure starting with the genesis block at the root...
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
constexpr char DB_BEST_BLOCK
void Unserialize(Stream &s, char &a)
bool error(const char *fmt, const Args &... args)
bool LoadBlockIndexGuts(const Consensus::Params &consensusParams, std::function< CBlockIndex *(const uint256 &)> insertBlockIndex)
bool WriteFlag(const std::string &name, bool fValue)
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
const fs::path & GetDataDir(bool fNetSpecific)
CCoinsViewCursor * Cursor() const override
Get a cursor to iterate over the whole state.
bool WriteBatch(CDBBatch &batch, bool fSync=false)
bool Valid() const override
CClientUIInterface uiInterface
int nHeight
height of the entry in the chain. The genesis block has height 0
uint256 GetBlockHash() const
unsigned int GetValueSize() const override
size_t EstimateSize(const K &key_begin, const K &key_end) const
std::vector< uint256 > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
unsigned int nTx
Number of transactions in this block.
std::string _(const char *psz)
Translation function.
Cursor for iterating over CoinsView state.