

(1).png)
比特币源码分析:utxo刷盘
摘要
utxo的刷盘逻辑主要在txdb.cpp中实现,主要是 CoinsViewDB::batchwrite这个函数。下面我们来分析一下:bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) { CDBBa 。

币界网报道:
utxo的刷盘逻辑主要在txdb.cpp
中实现,主要是 CoinsViewDB::batchwrite
这个函数。下面我们来分析一下:
bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) { CDBBatch batch(db); size_t count = 0; size_t changed = 0; for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) { if (it->second.flags & CCoinsCacheEntry::DIRTY) { CoinEntry entry(&it->first); if (it->second.coin.IsSpent()) { batch.Erase(entry); } else { batch.Write(entry, it->second.coin); } changed++; } count++; CCoinsMap::iterator itOld = it++; mapCoins.erase(itOld); } if (!hashBlock.IsNull()) { batch.Write(DB_BEST_BLOCK, hashBlock); } bool ret = db.WriteBatch(batch); LogPrint("coindb", "Committed %u changed transaction outputs (out of %u) " "to coin database...\n", (unsigned int)changed, (unsigned int)count); return ret; }
在前面我们介绍过 CDBWrapper
主要是对 leveldb的一个简单封装,定义一个CDBWrapper db;
我们拿着 db 就可以实现相应的操作。
接下来迭代mapCoins,并填充其值,这里最主要的就是作为k-v数据库的leveldb中的key与value如何获得:
key
CoinEntry是一个辅助工具类。
struct CoinEntry { COutPoint *outpoint; char key; CoinEntry(const COutPoint *ptr) : outpoint(const_cast(ptr)), key(DB_COIN) {} template void Serialize(Stream &s) const { s << key; s << outpoint->hash; s << VARINT(outpoint->n); } };
key指向的是outpoint,具体结构如下:
我们将序列化后的值当作key,作为entry的参数,同时作为db.write
的key。
关于db.write和db.WriteBatch二者之间的联系,前面已经详细分析。
value
value的值就是 coin 序列化后的值,而 coin 又包含了txout,如下:
class Coin { //! Unspent transaction output. CTxOut out; //! Whether containing transaction was a coinbase and height at which the //! transaction was included into a block. uint32_t nHeightAndIsCoinBase;
同样的,我们进行序列化并使用CTxOutCompressor
对txout进行压缩,REF是一个宏定义,是非const转换,我们首先断言这个币是否被消费:
template void Serialize(Stream &s) const { assert(!IsSpent()); ::Serialize(s, VARINT(nHeightAndIsCoinBase)); ::Serialize(s, CTxOutCompressor(REF(out))); }
txout主要包含:
class CTxOut { public: Amount nValue; CScript scriptPubKey;
对nValue和scriptPubKey采用了不同的压缩方式来进行序列化,如下:
class CTxOutCompressor { private: CTxOut &txout; public: template inline void SerializationOp(Stream &s, Operation ser_action) { if (!ser_action.ForRead()) { uint64_t nVal = CompressAmount(txout.nValue); READWRITE(VARINT(nVal)); } else { uint64_t nVal = 0; READWRITE(VARINT(nVal)); txout.nValue = DecompressAmount(nVal); } CScriptCompressor cscript(REF(txout.scriptPubKey)); READWRITE(cscript); } };
这时候我们就拿到了db.write
的value值,这时候我们通过for循环,不断迭代,将值写入磁盘。

分享





.png)


发表评论

暂无评论

相关阅读

2024年,比特币的价格飙升至疯狂的新高,让金融界大吃一惊。它达到了10.8万美元,这是一个巨大的124%的涨幅
比特币
2025-01-08 09:24:13


.png)

如果历史重演,死亡十字架可能标志着比特币主导地位的终结。但哪一个会领先——山寨币还是模因币?
比特币
2025-01-08 08:55:48


.png)

一位备受关注的分析师认为,模因代币Floki(Floki)正在为反弹做准备,并有望创下历史新高(ATH)。
区块链
2025-01-08 07:45:59


.png)

埃隆·马斯克赞扬了比特币倡导者、加拿大总理候选人Pierre Poilievre,他希望削减政府开支。
比特币
2025-01-08 07:43:53


.png)

比特币重回10万美元,市场情绪转暖,机构增持加速推动价格上涨。川普上任或带来行情波动,建议稳重操作,关注ETH、川普相关币种及以太生态、AI板块。
比特币
2025-01-08 06:31:44


.png)
推荐专栏
Fully On-Chain & AI-Powered Meme Trading | #Xbit #DEX #Web3 | English: @XBITDEX | Chinese 华语 : @XBITDEX_ZH | Support: @XbitHelpDesk
热门币种
更多

币种
美元价格
24H涨跌幅
热搜币种
更多

币种
美元价格
24H涨跌幅
最新快讯
更多

2025-01-08 12:00:34
2025-01-08 12:00:02
2025-01-08 11:59:02
2025-01-08 11:58:42
2025-01-08 11:57:41
2025-01-08 11:54:57
2025-01-08 11:53:10