1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
8 #include "chainparams.h"
17 #include <boost/thread.hpp>
21 static const char DB_ANCHOR = 'A';
22 static const char DB_NULLIFIER = 's';
23 static const char DB_COINS = 'c';
24 static const char DB_BLOCK_FILES = 'f';
25 static const char DB_TXINDEX = 't';
26 static const char DB_ADDRESSINDEX = 'd';
27 static const char DB_ADDRESSUNSPENTINDEX = 'u';
28 static const char DB_TIMESTAMPINDEX = 'S';
29 static const char DB_BLOCKHASHINDEX = 'z';
30 static const char DB_SPENTINDEX = 'p';
31 static const char DB_BLOCK_INDEX = 'b';
33 static const char DB_BEST_BLOCK = 'B';
34 static const char DB_BEST_ANCHOR = 'a';
35 static const char DB_FLAG = 'F';
36 static const char DB_REINDEX_FLAG = 'R';
37 static const char DB_LAST_BLOCK = 'l';
40 void static BatchWriteAnchor(CLevelDBBatch &batch,
42 const ZCIncrementalMerkleTree &tree,
46 batch.Erase(make_pair(DB_ANCHOR, croot));
48 batch.Write(make_pair(DB_ANCHOR, croot), tree);
52 void static BatchWriteNullifier(CLevelDBBatch &batch, const uint256 &nf, const bool &entered) {
54 batch.Erase(make_pair(DB_NULLIFIER, nf));
56 batch.Write(make_pair(DB_NULLIFIER, nf), true);
59 void static BatchWriteCoins(CLevelDBBatch &batch, const uint256 &hash, const CCoins &coins) {
61 batch.Erase(make_pair(DB_COINS, hash));
63 batch.Write(make_pair(DB_COINS, hash), coins);
66 void static BatchWriteHashBestChain(CLevelDBBatch &batch, const uint256 &hash) {
67 batch.Write(DB_BEST_BLOCK, hash);
70 void static BatchWriteHashBestAnchor(CLevelDBBatch &batch, const uint256 &hash) {
71 batch.Write(DB_BEST_ANCHOR, hash);
74 CCoinsViewDB::CCoinsViewDB(std::string dbName, size_t nCacheSize, bool fMemory, bool fWipe) : db(GetDataDir() / dbName, nCacheSize, fMemory, fWipe) {
77 CCoinsViewDB::CCoinsViewDB(size_t nCacheSize, bool fMemory, bool fWipe) : db(GetDataDir() / "chainstate", nCacheSize, fMemory, fWipe, false, 64) {
81 bool CCoinsViewDB::GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tree) const {
82 if (rt == ZCIncrementalMerkleTree::empty_root()) {
83 ZCIncrementalMerkleTree new_tree;
88 bool read = db.Read(make_pair(DB_ANCHOR, rt), tree);
93 bool CCoinsViewDB::GetNullifier(const uint256 &nf) const {
95 bool read = db.Read(make_pair(DB_NULLIFIER, nf), spent);
100 bool CCoinsViewDB::GetCoins(const uint256 &txid, CCoins &coins) const {
101 return db.Read(make_pair(DB_COINS, txid), coins);
104 bool CCoinsViewDB::HaveCoins(const uint256 &txid) const {
105 return db.Exists(make_pair(DB_COINS, txid));
108 uint256 CCoinsViewDB::GetBestBlock() const {
109 uint256 hashBestChain;
110 if (!db.Read(DB_BEST_BLOCK, hashBestChain))
112 return hashBestChain;
115 uint256 CCoinsViewDB::GetBestAnchor() const {
116 uint256 hashBestAnchor;
117 if (!db.Read(DB_BEST_ANCHOR, hashBestAnchor))
118 return ZCIncrementalMerkleTree::empty_root();
119 return hashBestAnchor;
122 bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins,
123 const uint256 &hashBlock,
124 const uint256 &hashAnchor,
125 CAnchorsMap &mapAnchors,
126 CNullifiersMap &mapNullifiers) {
130 for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) {
131 if (it->second.flags & CCoinsCacheEntry::DIRTY) {
132 BatchWriteCoins(batch, it->first, it->second.coins);
136 CCoinsMap::iterator itOld = it++;
137 mapCoins.erase(itOld);
140 for (CAnchorsMap::iterator it = mapAnchors.begin(); it != mapAnchors.end();) {
141 if (it->second.flags & CAnchorsCacheEntry::DIRTY) {
142 BatchWriteAnchor(batch, it->first, it->second.tree, it->second.entered);
145 CAnchorsMap::iterator itOld = it++;
146 mapAnchors.erase(itOld);
149 for (CNullifiersMap::iterator it = mapNullifiers.begin(); it != mapNullifiers.end();) {
150 if (it->second.flags & CNullifiersCacheEntry::DIRTY) {
151 BatchWriteNullifier(batch, it->first, it->second.entered);
154 CNullifiersMap::iterator itOld = it++;
155 mapNullifiers.erase(itOld);
158 if (!hashBlock.IsNull())
159 BatchWriteHashBestChain(batch, hashBlock);
160 if (!hashAnchor.IsNull())
161 BatchWriteHashBestAnchor(batch, hashAnchor);
163 LogPrint("coindb", "Committing %u changed transactions (out of %u) to coin database...\n", (unsigned int)changed, (unsigned int)count);
164 return db.WriteBatch(batch);
167 CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool fMemory, bool fWipe, bool compression, int maxOpenFiles) : CLevelDBWrapper(GetDataDir() / "blocks" / "index", nCacheSize, fMemory, fWipe, compression, maxOpenFiles) {
170 bool CBlockTreeDB::ReadBlockFileInfo(int nFile, CBlockFileInfo &info) {
171 return Read(make_pair(DB_BLOCK_FILES, nFile), info);
174 bool CBlockTreeDB::WriteReindexing(bool fReindexing) {
176 return Write(DB_REINDEX_FLAG, '1');
178 return Erase(DB_REINDEX_FLAG);
181 bool CBlockTreeDB::ReadReindexing(bool &fReindexing) {
182 fReindexing = Exists(DB_REINDEX_FLAG);
186 bool CBlockTreeDB::ReadLastBlockFile(int &nFile) {
187 return Read(DB_LAST_BLOCK, nFile);
190 bool CCoinsViewDB::GetStats(CCoinsStats &stats) const {
191 /* It seems that there are no "const iterators" for LevelDB. Since we
192 only need read operations on it, use a const-cast to get around
194 boost::scoped_ptr<leveldb::Iterator> pcursor(const_cast<CLevelDBWrapper*>(&db)->NewIterator());
195 pcursor->SeekToFirst();
197 CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
198 stats.hashBlock = GetBestBlock();
199 ss << stats.hashBlock;
200 CAmount nTotalAmount = 0;
201 while (pcursor->Valid()) {
202 boost::this_thread::interruption_point();
204 leveldb::Slice slKey = pcursor->key();
205 CDataStream ssKey(slKey.data(), slKey.data()+slKey.size(), SER_DISK, CLIENT_VERSION);
208 if (chType == DB_COINS) {
209 leveldb::Slice slValue = pcursor->value();
210 CDataStream ssValue(slValue.data(), slValue.data()+slValue.size(), SER_DISK, CLIENT_VERSION);
216 ss << VARINT(coins.nVersion);
217 ss << (coins.fCoinBase ? 'c' : 'n');
218 ss << VARINT(coins.nHeight);
219 stats.nTransactions++;
220 for (unsigned int i=0; i<coins.vout.size(); i++) {
221 const CTxOut &out = coins.vout[i];
223 stats.nTransactionOutputs++;
226 nTotalAmount += out.nValue;
229 stats.nSerializedSize += 32 + slValue.size();
233 } catch (const std::exception& e) {
234 return error("%s: Deserialize or I/O error - %s", __func__, e.what());
239 stats.nHeight = mapBlockIndex.find(stats.hashBlock)->second->nHeight;
241 stats.hashSerialized = ss.GetHash();
242 stats.nTotalAmount = nTotalAmount;
246 bool CBlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo) {
248 for (std::vector<std::pair<int, const CBlockFileInfo*> >::const_iterator it=fileInfo.begin(); it != fileInfo.end(); it++) {
249 batch.Write(make_pair(DB_BLOCK_FILES, it->first), *it->second);
251 batch.Write(DB_LAST_BLOCK, nLastFile);
252 for (std::vector<const CBlockIndex*>::const_iterator it=blockinfo.begin(); it != blockinfo.end(); it++) {
253 batch.Write(make_pair(DB_BLOCK_INDEX, (*it)->GetBlockHash()), CDiskBlockIndex(*it));
255 return WriteBatch(batch, true);
258 bool CBlockTreeDB::ReadTxIndex(const uint256 &txid, CDiskTxPos &pos) {
259 return Read(make_pair(DB_TXINDEX, txid), pos);
262 bool CBlockTreeDB::WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> >&vect) {
264 for (std::vector<std::pair<uint256,CDiskTxPos> >::const_iterator it=vect.begin(); it!=vect.end(); it++)
265 batch.Write(make_pair(DB_TXINDEX, it->first), it->second);
266 return WriteBatch(batch);
269 bool CBlockTreeDB::ReadSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value) {
270 return Read(make_pair(DB_SPENTINDEX, key), value);
273 bool CBlockTreeDB::UpdateSpentIndex(const std::vector<std::pair<CSpentIndexKey, CSpentIndexValue> >&vect) {
275 for (std::vector<std::pair<CSpentIndexKey,CSpentIndexValue> >::const_iterator it=vect.begin(); it!=vect.end(); it++) {
276 if (it->second.IsNull()) {
277 batch.Erase(make_pair(DB_SPENTINDEX, it->first));
279 batch.Write(make_pair(DB_SPENTINDEX, it->first), it->second);
282 return WriteBatch(batch);
285 bool CBlockTreeDB::UpdateAddressUnspentIndex(const std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue > >&vect) {
287 for (std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> >::const_iterator it=vect.begin(); it!=vect.end(); it++) {
288 if (it->second.IsNull()) {
289 batch.Erase(make_pair(DB_ADDRESSUNSPENTINDEX, it->first));
291 batch.Write(make_pair(DB_ADDRESSUNSPENTINDEX, it->first), it->second);
294 return WriteBatch(batch);
297 bool CBlockTreeDB::ReadAddressUnspentIndex(uint160 addressHash, int type,
298 std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > &unspentOutputs) {
300 boost::scoped_ptr<leveldb::Iterator> pcursor(NewIterator());
302 CDataStream ssKeySet(SER_DISK, CLIENT_VERSION);
303 ssKeySet << make_pair(DB_ADDRESSUNSPENTINDEX, CAddressIndexIteratorKey(type, addressHash));
304 pcursor->Seek(ssKeySet.str());
306 while (pcursor->Valid()) {
307 boost::this_thread::interruption_point();
309 leveldb::Slice slKey = pcursor->key();
310 CDataStream ssKey(slKey.data(), slKey.data()+slKey.size(), SER_DISK, CLIENT_VERSION);
312 CAddressUnspentKey indexKey;
315 if (chType == DB_ADDRESSUNSPENTINDEX && indexKey.hashBytes == addressHash) {
317 leveldb::Slice slValue = pcursor->value();
318 CDataStream ssValue(slValue.data(), slValue.data()+slValue.size(), SER_DISK, CLIENT_VERSION);
319 CAddressUnspentValue nValue;
321 unspentOutputs.push_back(make_pair(indexKey, nValue));
323 } catch (const std::exception& e) {
324 return error("failed to get address unspent value");
329 } catch (const std::exception& e) {
337 bool CBlockTreeDB::WriteAddressIndex(const std::vector<std::pair<CAddressIndexKey, CAmount > >&vect) {
339 for (std::vector<std::pair<CAddressIndexKey, CAmount> >::const_iterator it=vect.begin(); it!=vect.end(); it++)
340 batch.Write(make_pair(DB_ADDRESSINDEX, it->first), it->second);
341 return WriteBatch(batch);
344 bool CBlockTreeDB::EraseAddressIndex(const std::vector<std::pair<CAddressIndexKey, CAmount > >&vect) {
346 for (std::vector<std::pair<CAddressIndexKey, CAmount> >::const_iterator it=vect.begin(); it!=vect.end(); it++)
347 batch.Erase(make_pair(DB_ADDRESSINDEX, it->first));
348 return WriteBatch(batch);
351 bool CBlockTreeDB::ReadAddressIndex(uint160 addressHash, int type,
352 std::vector<std::pair<CAddressIndexKey, CAmount> > &addressIndex,
353 int start, int end) {
355 boost::scoped_ptr<leveldb::Iterator> pcursor(NewIterator());
357 CDataStream ssKeySet(SER_DISK, CLIENT_VERSION);
358 if (start > 0 && end > 0) {
359 ssKeySet << make_pair(DB_ADDRESSINDEX, CAddressIndexIteratorHeightKey(type, addressHash, start));
361 ssKeySet << make_pair(DB_ADDRESSINDEX, CAddressIndexIteratorKey(type, addressHash));
363 pcursor->Seek(ssKeySet.str());
365 while (pcursor->Valid()) {
366 boost::this_thread::interruption_point();
368 leveldb::Slice slKey = pcursor->key();
369 CDataStream ssKey(slKey.data(), slKey.data()+slKey.size(), SER_DISK, CLIENT_VERSION);
371 CAddressIndexKey indexKey;
374 if (chType == DB_ADDRESSINDEX && indexKey.hashBytes == addressHash) {
375 if (end > 0 && indexKey.blockHeight > end) {
379 leveldb::Slice slValue = pcursor->value();
380 CDataStream ssValue(slValue.data(), slValue.data()+slValue.size(), SER_DISK, CLIENT_VERSION);
384 addressIndex.push_back(make_pair(indexKey, nValue));
386 } catch (const std::exception& e) {
387 return error("failed to get address index value");
392 } catch (const std::exception& e) {
400 bool getAddressFromIndex(const int &type, const uint160 &hash, std::string &address);
402 extern UniValue CBlockTreeDB::Snapshot(int top)
404 char chType; int64_t total = 0; int64_t totalAddresses = 0; std::string address;
405 int64_t utxos = 0; int64_t ignoredAddresses;
406 boost::scoped_ptr<leveldb::Iterator> iter(NewIterator());
407 std::map <std::string, CAmount> addressAmounts;
408 std::vector <std::pair<CAmount, std::string>> vaddr;
409 UniValue result(UniValue::VOBJ);
410 result.push_back(Pair("start_time", (int) time(NULL)));
412 std::map <std::string,int> ignoredMap = {
413 {"RReUxSs5hGE39ELU23DfydX8riUuzdrHAE", 1},
414 {"RMUF3UDmzWFLSKV82iFbMaqzJpUnrWjcT4", 1},
415 {"RA5imhVyJa7yHhggmBytWuDr923j2P1bxx", 1},
416 {"RBM5LofZFodMeewUzoMWcxedm3L3hYRaWg", 1},
417 {"RAdcko2d94TQUcJhtFHZZjMyWBKEVfgn4J", 1},
418 {"RLzUaZ934k2EFCsAiVjrJqM8uU1vmMRFzk", 1},
419 {"RMSZMWZXv4FhUgWhEo4R3AQXmRDJ6rsGyt", 1},
420 {"RUDrX1v5toCsJMUgtvBmScKjwCB5NaR8py", 1},
421 {"RMSZMWZXv4FhUgWhEo4R3AQXmRDJ6rsGyt", 1},
422 {"RRvwmbkxR5YRzPGL5kMFHMe1AH33MeD8rN", 1},
423 {"RQLQvSgpPAJNPgnpc8MrYsbBhep95nCS8L", 1},
424 {"RK8JtBV78HdvEPvtV5ckeMPSTojZPzHUTe", 1},
425 {"RHVs2KaCTGUMNv3cyWiG1jkEvZjigbCnD2", 1},
426 {"RE3SVaDgdjkRPYA6TRobbthsfCmxQedVgF", 1},
427 {"RW6S5Lw5ZCCvDyq4QV9vVy7jDHfnynr5mn", 1},
428 {"RTkJwAYtdXXhVsS3JXBAJPnKaBfMDEswF8", 1},
429 {"RD6GgnrMpPaTSMn8vai6yiGA7mN4QGPVMY", 1} //Burnaddress for null privkey
432 int64_t startingHeight = chainActive.Height();
433 fprintf(stderr, "Starting snapshot at height %li\n", startingHeight);
434 for (iter->SeekToLast(); iter->Valid(); iter->Prev())
436 boost::this_thread::interruption_point();
439 leveldb::Slice slKey = iter->key();
440 CDataStream ssKey(slKey.data(), slKey.data()+slKey.size(), SER_DISK, CLIENT_VERSION);
441 CAddressIndexIteratorKey indexKey;
446 //fprintf(stderr, "chType=%d\n", chType);
447 if (chType == DB_ADDRESSUNSPENTINDEX)
450 leveldb::Slice slValue = iter->value();
451 CDataStream ssValue(slValue.data(), slValue.data()+slValue.size(), SER_DISK, CLIENT_VERSION);
455 getAddressFromIndex(indexKey.type, indexKey.hashBytes, address);
457 std::map <std::string, int>::iterator ignored = ignoredMap.find(address);
458 if (ignored != ignoredMap.end()) {
459 fprintf(stderr,"ignoring %s\n", address.c_str());
464 std::map <std::string, CAmount>::iterator pos = addressAmounts.find(address);
465 if (pos == addressAmounts.end()) {
466 // insert new address + utxo amount
467 //fprintf(stderr, "inserting new address %s with amount %li\n", address.c_str(), nValue);
468 addressAmounts[address] = nValue;
471 // update unspent tally for this address
472 //fprintf(stderr, "updating address %s with new utxo amount %li\n", address.c_str(), nValue);
473 addressAmounts[address] += nValue;
475 //fprintf(stderr,"{\"%s\", %.8f},\n",address.c_str(),(double)nValue/COIN);
478 } catch (const std::exception& e) {
479 fprintf(stderr, "DONE %s: LevelDB addressindex exception! - %s\n", __func__, e.what());
483 } catch (const std::exception& e) {
484 fprintf(stderr, "DONE reading index entries\n");
489 UniValue addresses(UniValue::VARR);
490 fprintf(stderr, "total=%f, totalAddresses=%li, utxos=%li, ignored=%li\n", (double) total / COIN, totalAddresses, utxos, ignoredAddresses);
492 for (std::pair<std::string, CAmount> element : addressAmounts) {
493 vaddr.push_back( make_pair(element.second, element.first) );
495 std::sort(vaddr.rbegin(), vaddr.rend());
497 UniValue obj(UniValue::VOBJ);
498 UniValue addressesSorted(UniValue::VARR);
500 for (std::vector<std::pair<CAmount, std::string>>::iterator it = vaddr.begin(); it!=vaddr.end(); ++it) {
501 UniValue obj(UniValue::VOBJ);
502 obj.push_back( make_pair("addr", it->second.c_str() ) );
504 sprintf(amount, "%.8f", (double) it->first / COIN);
505 obj.push_back( make_pair("amount", amount) );
507 addressesSorted.push_back(obj);
509 // If requested, only show top N addresses in output JSON
515 totalAddresses = top;
517 if (totalAddresses > 0) {
518 // Array of all addreses with balances
519 result.push_back(make_pair("addresses", addressesSorted));
520 // Total amount in this snapshot, which is less than circulating supply if top parameter is used
521 result.push_back(make_pair("total", (double) total / COIN ));
522 // Average amount in each address of this snapshot
523 result.push_back(make_pair("average",(double) (total/COIN) / totalAddresses ));
525 // Total number of utxos processed in this snaphot
526 result.push_back(make_pair("utxos", utxos));
527 // Total number of addresses in this snaphot
528 result.push_back(make_pair("total_addresses", totalAddresses));
529 // Total number of ignored addresses in this snaphot
530 result.push_back(make_pair("ignored_addresses", ignoredAddresses));
531 // The snapshot began at this block height
532 result.push_back(make_pair("start_height", startingHeight));
533 // The snapshot finished at this block height
534 result.push_back(make_pair("ending_height", chainActive.Height()));
538 bool CBlockTreeDB::WriteTimestampIndex(const CTimestampIndexKey ×tampIndex) {
540 batch.Write(make_pair(DB_TIMESTAMPINDEX, timestampIndex), 0);
541 return WriteBatch(batch);
544 bool CBlockTreeDB::ReadTimestampIndex(const unsigned int &high, const unsigned int &low, const bool fActiveOnly, std::vector<std::pair<uint256, unsigned int> > &hashes) {
546 boost::scoped_ptr<leveldb::Iterator> pcursor(NewIterator());
548 CDataStream ssKeySet(SER_DISK, CLIENT_VERSION);
549 ssKeySet << make_pair(DB_TIMESTAMPINDEX, CTimestampIndexIteratorKey(low));
550 pcursor->Seek(ssKeySet.str());
552 while (pcursor->Valid()) {
553 boost::this_thread::interruption_point();
555 leveldb::Slice slKey = pcursor->key();
556 CDataStream ssKey(slKey.data(), slKey.data()+slKey.size(), SER_DISK, CLIENT_VERSION);
558 CTimestampIndexKey indexKey;
561 if (chType == DB_TIMESTAMPINDEX && indexKey.timestamp < high) {
563 if (blockOnchainActive(indexKey.blockHash)) {
564 hashes.push_back(std::make_pair(indexKey.blockHash, indexKey.timestamp));
567 hashes.push_back(std::make_pair(indexKey.blockHash, indexKey.timestamp));
574 } catch (const std::exception& e) {
582 bool CBlockTreeDB::WriteTimestampBlockIndex(const CTimestampBlockIndexKey &blockhashIndex, const CTimestampBlockIndexValue &logicalts) {
584 batch.Write(make_pair(DB_BLOCKHASHINDEX, blockhashIndex), logicalts);
585 return WriteBatch(batch);
588 bool CBlockTreeDB::ReadTimestampBlockIndex(const uint256 &hash, unsigned int <imestamp) {
590 CTimestampBlockIndexValue(lts);
591 if (!Read(std::make_pair(DB_BLOCKHASHINDEX, hash), lts))
594 ltimestamp = lts.ltimestamp;
598 bool CBlockTreeDB::WriteFlag(const std::string &name, bool fValue) {
599 return Write(std::make_pair(DB_FLAG, name), fValue ? '1' : '0');
602 bool CBlockTreeDB::ReadFlag(const std::string &name, bool &fValue) {
604 if (!Read(std::make_pair(DB_FLAG, name), ch))
610 void komodo_index2pubkey33(uint8_t *pubkey33,CBlockIndex *pindex,int32_t height);
612 bool CBlockTreeDB::blockOnchainActive(const uint256 &hash) {
613 CBlockIndex* pblockindex = mapBlockIndex[hash];
615 if (!chainActive.Contains(pblockindex)) {
622 bool CBlockTreeDB::LoadBlockIndexGuts()
624 boost::scoped_ptr<leveldb::Iterator> pcursor(NewIterator());
626 CDataStream ssKeySet(SER_DISK, CLIENT_VERSION);
627 ssKeySet << make_pair(DB_BLOCK_INDEX, uint256());
628 pcursor->Seek(ssKeySet.str());
630 // Load mapBlockIndex
631 while (pcursor->Valid()) {
632 boost::this_thread::interruption_point();
634 leveldb::Slice slKey = pcursor->key();
635 CDataStream ssKey(slKey.data(), slKey.data()+slKey.size(), SER_DISK, CLIENT_VERSION);
638 if (chType == DB_BLOCK_INDEX) {
639 leveldb::Slice slValue = pcursor->value();
640 CDataStream ssValue(slValue.data(), slValue.data()+slValue.size(), SER_DISK, CLIENT_VERSION);
641 CDiskBlockIndex diskindex;
642 ssValue >> diskindex;
644 // Construct block index object
645 CBlockIndex* pindexNew = InsertBlockIndex(diskindex.GetBlockHash());
646 pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev);
647 pindexNew->nHeight = diskindex.nHeight;
648 pindexNew->nFile = diskindex.nFile;
649 pindexNew->nDataPos = diskindex.nDataPos;
650 pindexNew->nUndoPos = diskindex.nUndoPos;
651 pindexNew->hashAnchor = diskindex.hashAnchor;
652 pindexNew->nVersion = diskindex.nVersion;
653 pindexNew->hashReserved = diskindex.hashReserved;
654 pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
655 pindexNew->hashReserved = diskindex.hashReserved;
656 pindexNew->nTime = diskindex.nTime;
657 pindexNew->nBits = diskindex.nBits;
658 pindexNew->nNonce = diskindex.nNonce;
659 pindexNew->nSolution = diskindex.nSolution;
660 pindexNew->nStatus = diskindex.nStatus;
661 pindexNew->nCachedBranchId = diskindex.nCachedBranchId;
662 pindexNew->nTx = diskindex.nTx;
663 pindexNew->nSproutValue = diskindex.nSproutValue;
665 // Consistency checks
666 auto header = pindexNew->GetBlockHeader();
667 if (header.GetHash() != pindexNew->GetBlockHash())
668 return error("LoadBlockIndex(): block header inconsistency detected: on-disk = %s, in-memory = %s",
669 diskindex.ToString(), pindexNew->ToString());
670 if ( 0 ) // POW will be checked before any block is connected
672 uint8_t pubkey33[33];
673 komodo_index2pubkey33(pubkey33,pindexNew,pindexNew->nHeight);
674 if (!CheckProofOfWork(header,pubkey33,pindexNew->nHeight,Params().GetConsensus()))
675 return error("LoadBlockIndex(): CheckProofOfWork failed: %s", pindexNew->ToString());
679 break; // if shutdown requested or finished loading block index
681 } catch (const std::exception& e) {
682 return error("%s: Deserialize or I/O error - %s", __func__, e.what());