美文网首页比特币源码学习笔记
比特币源码阅读(leveldb-写入CBlockIndex和CB

比特币源码阅读(leveldb-写入CBlockIndex和CB

作者: 坠叶飘香 | 来源:发表于2018-08-07 16:39 被阅读0次
flb.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);
}

相关文章

网友评论

    本文标题:比特币源码阅读(leveldb-写入CBlockIndex和CB

    本文链接:https://www.haomeiwen.com/subject/iueavftx.html