bool CDbNameIterator::next(valtype& name, CNameData& data) { if (!iter->Valid()) return false; std::pair<char, valtype> key; if (!iter->GetKey(key) || key.first != DB_NAME) return false; name = key.second; if (!iter->GetValue(data)) return error("%s : failed to read data from iterator", __func__); iter->Next (); return true; }
bool CBlockTreeDB::ReadAddrIndex(uint160 addrid, std::vector<CExtDiskTxPos> &list) { CLevelDBIterator *iter = NewIterator(); uint64_t lookupid; { CHashWriter ss(SER_GETHASH, 0); ss << salt; ss << addrid; lookupid = ss.GetHash().GetLow64(); } iter->Seek(make_pair('a', lookupid)); while (iter->Valid()) { std::pair<std::pair<char, uint64_t>, CExtDiskTxPos> key; if (iter->GetKey(key) && key.first.first == 'a' && key.first.second == lookupid) { list.push_back(key.second); } else { break; } iter->Next(); } return true; }
bool CBlockTreeDB::LoadBlockIndexGuts() { CLevelDBIterator *pcursor = NewIterator(); pcursor->Seek(make_pair('b', uint256(0))); // Load mapBlockIndex while (pcursor->Valid()) { boost::this_thread::interruption_point(); std::pair<char, uint256> key; try { if (pcursor->GetKey(key) && key.first == 'b') { CDiskBlockIndex diskindex; if (pcursor->GetValue(diskindex)) { // Construct block index object CBlockIndex* pindexNew = InsertBlockIndex(diskindex.GetBlockHash()); pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev); pindexNew->nHeight = diskindex.nHeight; pindexNew->nFile = diskindex.nFile; pindexNew->nDataPos = diskindex.nDataPos; pindexNew->nUndoPos = diskindex.nUndoPos; pindexNew->nVersion = diskindex.nVersion; pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot; pindexNew->nTime = diskindex.nTime; pindexNew->nBits = diskindex.nBits; pindexNew->nNonce = diskindex.nNonce; pindexNew->nStatus = diskindex.nStatus; pindexNew->nTx = diskindex.nTx; if (!pindexNew->CheckIndex()) return error("LoadBlockIndex() : CheckIndex failed: %s", pindexNew->ToString()); pcursor->Next(); } } else { break; } } catch (std::exception &e) { return error("%s : Deserialize or I/O error -%s", __PRETTY_FUNCTION__, e.what()); } } delete pcursor; return true; }
bool CCoinsViewDB::GetStats(CCoinsStats &stats) { CLevelDBIterator *pcursor = db.NewIterator(); pcursor->Seek('c'); CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION); stats.hashBlock = GetBestBlock(); ss << stats.hashBlock; int64_t nTotalAmount = 0; while (pcursor->Valid()) { boost::this_thread::interruption_point(); std::pair<char, uint256> key; CCoins coins; try { if (pcursor->GetKey(key) && key.first == 'c') { if (pcursor->GetValue(coins)) { stats.nTransactions++; for (unsigned int i=0; i<coins.vout.size(); i++) { const CTxOut &out = coins.vout[i]; if (!out.IsNull()) { stats.nTransactionOutputs++; ss << VARINT(i+1); ss << out; nTotalAmount += out.nValue; } } stats.nSerializedSize += 32 + pcursor->GetKeySize(); ss << VARINT(0); } } pcursor->Next(); } catch (std::exception &e) { return error("%s : Deserialize or I/O error - %s", __PRETTY_FUNCTION__, e.what()); } } delete pcursor; stats.nHeight = mapBlockIndex.find(GetBestBlock())->second->nHeight; stats.hashSerialized = ss.GetHash(); stats.nTotalAmount = nTotalAmount; return true; }
void CDbNameIterator::seek(const valtype& start) { iter->Seek(std::make_pair(DB_NAME, start)); }