1. 程式人生 > >比特幣程式碼分析8 區塊校驗和確認

比特幣程式碼分析8 區塊校驗和確認

比特幣節點接收到一個區塊以後,都會進行校驗和確認,如下參考網路圖:
比特幣程式碼分析8 區塊校驗和確認

關鍵看看對區塊中的交易進行進一步的校驗程式碼:
1.// First transaction must be coinbase, the rest must not be
2.if (vtx.empty() || !vtx[0].IsCoinBase())

  1. return error("CheckBlock() : first tx is not coinbase");
    4.for (int i = 1; i < vtx.size(); i++)
  2. if (vtx[i].IsCoinBase())
  3. return error("CheckBlock() : more than one coinbase");
  4. 8.// Check transactions 迴圈檢查所有的交易,這一步驟很關鍵,所以交易不能隨便改,大家都在檢查
    9.foreach(const CTransaction& tx, vtx)

  5. if (!tx.CheckTransaction())
  6. return error("CheckBlock() : CheckTransaction failed");
  7. 13.// Check proof of work matches claimed amount
    14.if (CBigNum().SetCompact(nBits) > bnProofOfWorkLimit)

  8. return error("CheckBlock() : nBits below minimum work");
    16.if (GetHash() > CBigNum().SetCompact(nBits).getuint256())
  9. return error("CheckBlock() : hash doesn't match nBits");
  10. 19.// Check merkleroot
    20.if (hashMerkleRoot != BuildMerkleTree())

  11. return error("CheckBlock() : hashMerkleRoot mismatch");