比特幣程式碼分析8 區塊校驗和確認
阿新 • • 發佈:2018-12-07
比特幣節點接收到一個區塊以後,都會進行校驗和確認,如下參考網路圖:
關鍵看看對區塊中的交易進行進一步的校驗程式碼:
1.// First transaction must be coinbase, the rest must not be
2.if (vtx.empty() || !vtx[0].IsCoinBase())
- return error("CheckBlock() : first tx is not coinbase");
4.for (int i = 1; i < vtx.size(); i++) - if (vtx[i].IsCoinBase())
- return error("CheckBlock() : more than one coinbase");
-
8.// Check transactions 迴圈檢查所有的交易,這一步驟很關鍵,所以交易不能隨便改,大家都在檢查
9.foreach(const CTransaction& tx, vtx) - if (!tx.CheckTransaction())
- return error("CheckBlock() : CheckTransaction failed");
-
13.// Check proof of work matches claimed amount
14.if (CBigNum().SetCompact(nBits) > bnProofOfWorkLimit) - return error("CheckBlock() : nBits below minimum work");
16.if (GetHash() > CBigNum().SetCompact(nBits).getuint256()) - return error("CheckBlock() : hash doesn't match nBits");
-
19.// Check merkleroot
20.if (hashMerkleRoot != BuildMerkleTree()) - return error("CheckBlock() : hashMerkleRoot mismatch");