比特币源码阅读(leveldb-写入CBlockIndex和CB
2018-08-07 本文已影响0人
坠叶飘香
![](https://img.haomeiwen.com/i12094850/f628ab22232bd5bd.png)
src/txdb.cpp
bool CBlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo) {
CDBBatch batch(*this);
//DB_BLOCK_FILES = 'f'
//将fileInfo存入leveldb
for (std::vector<std::pair<int, const CBlockFileInfo*> >::const_iterator it=fileInfo.begin(); it != fileInfo.end(); it++) {
batch.Write(std::make_pair(DB_BLOCK_FILES, it->first), *it->second);
}
//DB_LAST_BLOCK = 'l'
//'l' -> 4-byte file number: the last block file number used.
batch.Write(DB_LAST_BLOCK, nLastFile);
//DB_BLOCK_INDEX: 'b'
//将vector<const CBlockIndex*>写入leveldb
//key: 'b' + 32-byte block hash
//value:CDiskBlockIndex (父CBlockIndex)
for (std::vector<const CBlockIndex*>::const_iterator it=blockinfo.begin(); it != blockinfo.end(); it++) {
batch.Write(std::make_pair(DB_BLOCK_INDEX, (*it)->GetBlockHash()), CDiskBlockIndex(*it));
}
return WriteBatch(batch, true);
}