]> Git Repo - VerusCoin.git/blob - src/txdb.cpp
Merge remote-tracking branch 'origin/jl777' into cctests
[VerusCoin.git] / src / txdb.cpp
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.
5
6 #include "txdb.h"
7
8 #include "chainparams.h"
9 #include "hash.h"
10 #include "main.h"
11 #include "pow.h"
12 #include "uint256.h"
13 #include "core_io.h"
14
15 #include <stdint.h>
16
17 #include <boost/thread.hpp>
18
19 using namespace std;
20
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';
32
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';
38
39
40 void static BatchWriteAnchor(CLevelDBBatch &batch,
41                              const uint256 &croot,
42                              const ZCIncrementalMerkleTree &tree,
43                              const bool &entered)
44 {
45     if (!entered)
46         batch.Erase(make_pair(DB_ANCHOR, croot));
47     else {
48         batch.Write(make_pair(DB_ANCHOR, croot), tree);
49     }
50 }
51
52 void static BatchWriteNullifier(CLevelDBBatch &batch, const uint256 &nf, const bool &entered) {
53     if (!entered)
54         batch.Erase(make_pair(DB_NULLIFIER, nf));
55     else
56         batch.Write(make_pair(DB_NULLIFIER, nf), true);
57 }
58
59 void static BatchWriteCoins(CLevelDBBatch &batch, const uint256 &hash, const CCoins &coins) {
60     if (coins.IsPruned())
61         batch.Erase(make_pair(DB_COINS, hash));
62     else
63         batch.Write(make_pair(DB_COINS, hash), coins);
64 }
65
66 void static BatchWriteHashBestChain(CLevelDBBatch &batch, const uint256 &hash) {
67     batch.Write(DB_BEST_BLOCK, hash);
68 }
69
70 void static BatchWriteHashBestAnchor(CLevelDBBatch &batch, const uint256 &hash) {
71     batch.Write(DB_BEST_ANCHOR, hash);
72 }
73
74 CCoinsViewDB::CCoinsViewDB(std::string dbName, size_t nCacheSize, bool fMemory, bool fWipe) : db(GetDataDir() / dbName, nCacheSize, fMemory, fWipe) {
75 }
76
77 CCoinsViewDB::CCoinsViewDB(size_t nCacheSize, bool fMemory, bool fWipe) : db(GetDataDir() / "chainstate", nCacheSize, fMemory, fWipe, false, 64) {
78 }
79
80
81 bool CCoinsViewDB::GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tree) const {
82     if (rt == ZCIncrementalMerkleTree::empty_root()) {
83         ZCIncrementalMerkleTree new_tree;
84         tree = new_tree;
85         return true;
86     }
87
88     bool read = db.Read(make_pair(DB_ANCHOR, rt), tree);
89
90     return read;
91 }
92
93 bool CCoinsViewDB::GetNullifier(const uint256 &nf) const {
94     bool spent = false;
95     bool read = db.Read(make_pair(DB_NULLIFIER, nf), spent);
96
97     return read;
98 }
99
100 bool CCoinsViewDB::GetCoins(const uint256 &txid, CCoins &coins) const {
101     return db.Read(make_pair(DB_COINS, txid), coins);
102 }
103
104 bool CCoinsViewDB::HaveCoins(const uint256 &txid) const {
105     return db.Exists(make_pair(DB_COINS, txid));
106 }
107
108 uint256 CCoinsViewDB::GetBestBlock() const {
109     uint256 hashBestChain;
110     if (!db.Read(DB_BEST_BLOCK, hashBestChain))
111         return uint256();
112     return hashBestChain;
113 }
114
115 uint256 CCoinsViewDB::GetBestAnchor() const {
116     uint256 hashBestAnchor;
117     if (!db.Read(DB_BEST_ANCHOR, hashBestAnchor))
118         return ZCIncrementalMerkleTree::empty_root();
119     return hashBestAnchor;
120 }
121
122 bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins,
123                               const uint256 &hashBlock,
124                               const uint256 &hashAnchor,
125                               CAnchorsMap &mapAnchors,
126                               CNullifiersMap &mapNullifiers) {
127     CLevelDBBatch batch;
128     size_t count = 0;
129     size_t changed = 0;
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);
133             changed++;
134         }
135         count++;
136         CCoinsMap::iterator itOld = it++;
137         mapCoins.erase(itOld);
138     }
139
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);
143             // TODO: changed++?
144         }
145         CAnchorsMap::iterator itOld = it++;
146         mapAnchors.erase(itOld);
147     }
148
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);
152             // TODO: changed++?
153         }
154         CNullifiersMap::iterator itOld = it++;
155         mapNullifiers.erase(itOld);
156     }
157
158     if (!hashBlock.IsNull())
159         BatchWriteHashBestChain(batch, hashBlock);
160     if (!hashAnchor.IsNull())
161         BatchWriteHashBestAnchor(batch, hashAnchor);
162
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);
165 }
166
167 CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool fMemory, bool fWipe, bool compression, int maxOpenFiles) : CLevelDBWrapper(GetDataDir() / "blocks" / "index", nCacheSize, fMemory, fWipe, compression, maxOpenFiles) {
168 }
169
170 bool CBlockTreeDB::ReadBlockFileInfo(int nFile, CBlockFileInfo &info) {
171     return Read(make_pair(DB_BLOCK_FILES, nFile), info);
172 }
173
174 bool CBlockTreeDB::WriteReindexing(bool fReindexing) {
175     if (fReindexing)
176         return Write(DB_REINDEX_FLAG, '1');
177     else
178         return Erase(DB_REINDEX_FLAG);
179 }
180
181 bool CBlockTreeDB::ReadReindexing(bool &fReindexing) {
182     fReindexing = Exists(DB_REINDEX_FLAG);
183     return true;
184 }
185
186 bool CBlockTreeDB::ReadLastBlockFile(int &nFile) {
187     return Read(DB_LAST_BLOCK, nFile);
188 }
189
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
193        that restriction.  */
194     boost::scoped_ptr<leveldb::Iterator> pcursor(const_cast<CLevelDBWrapper*>(&db)->NewIterator());
195     pcursor->SeekToFirst();
196
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();
203         try {
204             leveldb::Slice slKey = pcursor->key();
205             CDataStream ssKey(slKey.data(), slKey.data()+slKey.size(), SER_DISK, CLIENT_VERSION);
206             char chType;
207             ssKey >> chType;
208             if (chType == DB_COINS) {
209                 leveldb::Slice slValue = pcursor->value();
210                 CDataStream ssValue(slValue.data(), slValue.data()+slValue.size(), SER_DISK, CLIENT_VERSION);
211                 CCoins coins;
212                 ssValue >> coins;
213                 uint256 txhash;
214                 ssKey >> txhash;
215                 ss << txhash;
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];
222                     if (!out.IsNull()) {
223                         stats.nTransactionOutputs++;
224                         ss << VARINT(i+1);
225                         ss << out;
226                         nTotalAmount += out.nValue;
227                     }
228                 }
229                 stats.nSerializedSize += 32 + slValue.size();
230                 ss << VARINT(0);
231             }
232             pcursor->Next();
233         } catch (const std::exception& e) {
234             return error("%s: Deserialize or I/O error - %s", __func__, e.what());
235         }
236     }
237     {
238         LOCK(cs_main);
239         stats.nHeight = mapBlockIndex.find(stats.hashBlock)->second->nHeight;
240     }
241     stats.hashSerialized = ss.GetHash();
242     stats.nTotalAmount = nTotalAmount;
243     return true;
244 }
245
246 bool CBlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo) {
247     CLevelDBBatch batch;
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);
250     }
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));
254     }
255     return WriteBatch(batch, true);
256 }
257
258 bool CBlockTreeDB::ReadTxIndex(const uint256 &txid, CDiskTxPos &pos) {
259     return Read(make_pair(DB_TXINDEX, txid), pos);
260 }
261
262 bool CBlockTreeDB::WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> >&vect) {
263     CLevelDBBatch batch;
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);
267 }
268
269 bool CBlockTreeDB::ReadSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value) {
270     return Read(make_pair(DB_SPENTINDEX, key), value);
271 }
272
273 bool CBlockTreeDB::UpdateSpentIndex(const std::vector<std::pair<CSpentIndexKey, CSpentIndexValue> >&vect) {
274     CLevelDBBatch batch;
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));
278         } else {
279             batch.Write(make_pair(DB_SPENTINDEX, it->first), it->second);
280         }
281     }
282     return WriteBatch(batch);
283 }
284
285 bool CBlockTreeDB::UpdateAddressUnspentIndex(const std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue > >&vect) {
286     CLevelDBBatch batch;
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));
290         } else {
291             batch.Write(make_pair(DB_ADDRESSUNSPENTINDEX, it->first), it->second);
292         }
293     }
294     return WriteBatch(batch);
295 }
296
297 bool CBlockTreeDB::ReadAddressUnspentIndex(uint160 addressHash, int type,
298                                            std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > &unspentOutputs) {
299
300     boost::scoped_ptr<leveldb::Iterator> pcursor(NewIterator());
301
302     CDataStream ssKeySet(SER_DISK, CLIENT_VERSION);
303     ssKeySet << make_pair(DB_ADDRESSUNSPENTINDEX, CAddressIndexIteratorKey(type, addressHash));
304     pcursor->Seek(ssKeySet.str());
305
306     while (pcursor->Valid()) {
307         boost::this_thread::interruption_point();
308         try {
309             leveldb::Slice slKey = pcursor->key();
310             CDataStream ssKey(slKey.data(), slKey.data()+slKey.size(), SER_DISK, CLIENT_VERSION);
311             char chType;
312             CAddressUnspentKey indexKey;
313             ssKey >> chType;
314             ssKey >> indexKey;
315             if (chType == DB_ADDRESSUNSPENTINDEX && indexKey.hashBytes == addressHash) {
316                 try {
317                     leveldb::Slice slValue = pcursor->value();
318                     CDataStream ssValue(slValue.data(), slValue.data()+slValue.size(), SER_DISK, CLIENT_VERSION);
319                     CAddressUnspentValue nValue;
320                     ssValue >> nValue;
321                     unspentOutputs.push_back(make_pair(indexKey, nValue));
322                     pcursor->Next();
323                 } catch (const std::exception& e) {
324                     return error("failed to get address unspent value");
325                 }
326             } else {
327                 break;
328             }
329         } catch (const std::exception& e) {
330             break;
331         }
332     }
333
334     return true;
335 }
336
337 bool CBlockTreeDB::WriteAddressIndex(const std::vector<std::pair<CAddressIndexKey, CAmount > >&vect) {
338     CLevelDBBatch batch;
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);
342 }
343
344 bool CBlockTreeDB::EraseAddressIndex(const std::vector<std::pair<CAddressIndexKey, CAmount > >&vect) {
345     CLevelDBBatch batch;
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);
349 }
350
351 bool CBlockTreeDB::ReadAddressIndex(uint160 addressHash, int type,
352                                     std::vector<std::pair<CAddressIndexKey, CAmount> > &addressIndex,
353                                     int start, int end) {
354
355     boost::scoped_ptr<leveldb::Iterator> pcursor(NewIterator());
356
357     CDataStream ssKeySet(SER_DISK, CLIENT_VERSION);
358     if (start > 0 && end > 0) {
359         ssKeySet << make_pair(DB_ADDRESSINDEX, CAddressIndexIteratorHeightKey(type, addressHash, start));
360     } else {
361         ssKeySet << make_pair(DB_ADDRESSINDEX, CAddressIndexIteratorKey(type, addressHash));
362     }
363     pcursor->Seek(ssKeySet.str());
364
365     while (pcursor->Valid()) {
366         boost::this_thread::interruption_point();
367         try {
368             leveldb::Slice slKey = pcursor->key();
369             CDataStream ssKey(slKey.data(), slKey.data()+slKey.size(), SER_DISK, CLIENT_VERSION);
370             char chType;
371             CAddressIndexKey indexKey;
372             ssKey >> chType;
373             ssKey >> indexKey;
374             if (chType == DB_ADDRESSINDEX && indexKey.hashBytes == addressHash) {
375                 if (end > 0 && indexKey.blockHeight > end) {
376                     break;
377                 }
378                 try {
379                     leveldb::Slice slValue = pcursor->value();
380                     CDataStream ssValue(slValue.data(), slValue.data()+slValue.size(), SER_DISK, CLIENT_VERSION);
381                     CAmount nValue;
382                     ssValue >> nValue;
383
384                     addressIndex.push_back(make_pair(indexKey, nValue));
385                     pcursor->Next();
386                 } catch (const std::exception& e) {
387                     return error("failed to get address index value");
388                 }
389             } else {
390                 break;
391             }
392         } catch (const std::exception& e) {
393             break;
394         }
395     }
396
397     return true;
398 }
399
400 bool getAddressFromIndex(const int &type, const uint160 &hash, std::string &address);
401
402 extern UniValue CBlockTreeDB::Snapshot(int top)
403 {
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)));
411
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
430     };
431
432     int64_t startingHeight = chainActive.Height();
433     //fprintf(stderr, "Starting snapshot at height %lli\n", startingHeight);
434     for (iter->SeekToLast(); iter->Valid(); iter->Prev())
435     {
436         boost::this_thread::interruption_point();
437         try
438         {
439             leveldb::Slice slKey = iter->key();
440             CDataStream ssKey(slKey.data(), slKey.data()+slKey.size(), SER_DISK, CLIENT_VERSION);
441             CAddressIndexIteratorKey indexKey;
442
443             ssKey >> chType;
444             ssKey >> indexKey;
445
446             //fprintf(stderr, "chType=%d\n", chType);
447             if (chType == DB_ADDRESSUNSPENTINDEX)
448             {
449                 try {
450                     leveldb::Slice slValue = iter->value();
451                     CDataStream ssValue(slValue.data(), slValue.data()+slValue.size(), SER_DISK, CLIENT_VERSION);
452                     CAmount nValue;
453                     ssValue >> nValue;
454
455                     getAddressFromIndex(indexKey.type, indexKey.hashBytes, address);
456
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());
460                         ignoredAddresses++;
461                         continue;
462                     }
463
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;
469                         totalAddresses++;
470                     } else {
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;
474                     }
475                     //fprintf(stderr,"{\"%s\", %.8f},\n",address.c_str(),(double)nValue/COIN);
476                     // total += nValue;
477                     utxos++;
478                 } catch (const std::exception& e) {
479                     fprintf(stderr, "DONE %s: LevelDB addressindex exception! - %s\n", __func__, e.what());
480                     break;
481                 }
482             }
483         } catch (const std::exception& e) {
484             fprintf(stderr, "DONE reading index entries\n");
485             break;
486         }
487     }
488
489     UniValue addresses(UniValue::VARR);
490     //fprintf(stderr, "total=%f, totalAddresses=%li, utxos=%li, ignored=%li\n", (double) total / COIN, totalAddresses, utxos, ignoredAddresses);
491
492     for (std::pair<std::string, CAmount> element : addressAmounts) {
493         vaddr.push_back( make_pair(element.second, element.first) );
494     }
495     std::sort(vaddr.rbegin(), vaddr.rend());
496
497     UniValue obj(UniValue::VOBJ);
498     UniValue addressesSorted(UniValue::VARR);
499     int topN = 0;
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() ) );
503         char amount[32];
504         sprintf(amount, "%.8f", (double) it->first / COIN);
505         obj.push_back( make_pair("amount", amount) );
506         total += it->first;
507         addressesSorted.push_back(obj);
508         topN++;
509         // If requested, only show top N addresses in output JSON
510         if (top == topN)
511             break;
512     }
513
514     if (top)
515         totalAddresses = top;
516
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 ));
524     }
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()));
535     return(result);
536 }
537
538 bool CBlockTreeDB::WriteTimestampIndex(const CTimestampIndexKey &timestampIndex) {
539     CLevelDBBatch batch;
540     batch.Write(make_pair(DB_TIMESTAMPINDEX, timestampIndex), 0);
541     return WriteBatch(batch);
542 }
543
544 bool CBlockTreeDB::ReadTimestampIndex(const unsigned int &high, const unsigned int &low, const bool fActiveOnly, std::vector<std::pair<uint256, unsigned int> > &hashes) {
545
546     boost::scoped_ptr<leveldb::Iterator> pcursor(NewIterator());
547
548     CDataStream ssKeySet(SER_DISK, CLIENT_VERSION);
549     ssKeySet << make_pair(DB_TIMESTAMPINDEX, CTimestampIndexIteratorKey(low));
550     pcursor->Seek(ssKeySet.str());
551
552     while (pcursor->Valid()) {
553         boost::this_thread::interruption_point();
554         try {
555             leveldb::Slice slKey = pcursor->key();
556             CDataStream ssKey(slKey.data(), slKey.data()+slKey.size(), SER_DISK, CLIENT_VERSION);
557             char chType;
558             CTimestampIndexKey indexKey;
559             ssKey >> chType;
560             ssKey >> indexKey;
561             if (chType == DB_TIMESTAMPINDEX && indexKey.timestamp < high) {
562                 if (fActiveOnly) {
563                     if (blockOnchainActive(indexKey.blockHash)) {
564                         hashes.push_back(std::make_pair(indexKey.blockHash, indexKey.timestamp));
565                     }
566                 } else {
567                     hashes.push_back(std::make_pair(indexKey.blockHash, indexKey.timestamp));
568                 }
569
570                 pcursor->Next();
571             } else {
572                 break;
573             }
574         } catch (const std::exception& e) {
575             break;
576         }
577     }
578
579     return true;
580 }
581
582 bool CBlockTreeDB::WriteTimestampBlockIndex(const CTimestampBlockIndexKey &blockhashIndex, const CTimestampBlockIndexValue &logicalts) {
583     CLevelDBBatch batch;
584     batch.Write(make_pair(DB_BLOCKHASHINDEX, blockhashIndex), logicalts);
585     return WriteBatch(batch);
586 }
587
588 bool CBlockTreeDB::ReadTimestampBlockIndex(const uint256 &hash, unsigned int &ltimestamp) {
589
590     CTimestampBlockIndexValue(lts);
591     if (!Read(std::make_pair(DB_BLOCKHASHINDEX, hash), lts))
592         return false;
593
594     ltimestamp = lts.ltimestamp;
595     return true;
596 }
597
598 bool CBlockTreeDB::WriteFlag(const std::string &name, bool fValue) {
599     return Write(std::make_pair(DB_FLAG, name), fValue ? '1' : '0');
600 }
601
602 bool CBlockTreeDB::ReadFlag(const std::string &name, bool &fValue) {
603     char ch;
604     if (!Read(std::make_pair(DB_FLAG, name), ch))
605         return false;
606     fValue = ch == '1';
607     return true;
608 }
609
610 void komodo_index2pubkey33(uint8_t *pubkey33,CBlockIndex *pindex,int32_t height);
611
612 bool CBlockTreeDB::blockOnchainActive(const uint256 &hash) {
613     CBlockIndex* pblockindex = mapBlockIndex[hash];
614
615     if (!chainActive.Contains(pblockindex)) {
616         return false;
617     }
618
619     return true;
620 }
621
622 bool CBlockTreeDB::LoadBlockIndexGuts()
623 {
624     boost::scoped_ptr<leveldb::Iterator> pcursor(NewIterator());
625
626     CDataStream ssKeySet(SER_DISK, CLIENT_VERSION);
627     ssKeySet << make_pair(DB_BLOCK_INDEX, uint256());
628     pcursor->Seek(ssKeySet.str());
629
630     // Load mapBlockIndex
631     while (pcursor->Valid()) {
632         boost::this_thread::interruption_point();
633         try {
634             leveldb::Slice slKey = pcursor->key();
635             CDataStream ssKey(slKey.data(), slKey.data()+slKey.size(), SER_DISK, CLIENT_VERSION);
636             char chType;
637             ssKey >> chType;
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;
643
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;
664                 
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
671                 {
672                     uint8_t pubkey33[33];
673                     komodo_index2pubkey33(pubkey33,pindexNew,pindexNew->nHeight);
674                     if (!CheckProofOfWork(pindexNew->nHeight,pubkey33,pindexNew->GetBlockHash(), pindexNew->nBits, Params().GetConsensus(),pindexNew->nTime))
675                         return error("LoadBlockIndex(): CheckProofOfWork failed: %s", pindexNew->ToString());
676                 }
677                 pcursor->Next();
678             } else {
679                 break; // if shutdown requested or finished loading block index
680             }
681         } catch (const std::exception& e) {
682             return error("%s: Deserialize or I/O error - %s", __func__, e.what());
683         }
684     }
685
686     return true;
687 }
This page took 0.065163 seconds and 4 git commands to generate.