第三课_电子加密货币与区块链(二)作业

1、找到并阅读结点独立验证一笔交易的源代码
2、加入调试信息并打印到终端

作业以文字描述或图片方式提交。
下次上课前,提交完即可。
老师会在下次直播课讲解作业题

liu2en

赞同来自:

感觉应该是/src/consensus/tx_verify. cpp,但是不清楚怎么验证(我是用 -regtest模式,用其他模式要下载blocks,太慢,是不是因为用了这个regtest模式,所以因为我是私有链,所以没有其他peer发送transaction或者block过来,从而无法调用tx_verify),所以我先测试了转账,log打印出来了  

李丰翔W

赞同来自:

1. 检查block位于validation.cpp 文件中 bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW, bool fCheckMerkleRoot) {     // These are checks that are independent of context.     if (block.fChecked)         return true;     // 检查区块头是否有效:区块头的哈希值小于目标难度(确认包含足够的工作 量证明)     // Check that the header is valid (particularly PoW).  This is mostly     // redundant with the call in AcceptBlockHeader.     if (!CheckBlockHeader(block, state, consensusParams, fCheckPOW))         return false;     // 检查merkle 树根     // Check the merkle root.     if (fCheckMerkleRoot) {         bool mutated;         uint256 hashMerkleRoot2 = BlockMerkleRoot(block, &mutated);         if (block.hashMerkleRoot != hashMerkleRoot2)             return state.DoS(100, false, REJECT_INVALID, "bad-txnmrklroot", true, "hashMerkleRoot mismatch");         // Check for merkle tree malleability (CVE-2012-2459): repeating sequences         // of transactions in a block without affecting the merkle root of a block,         // while still invalidating it.         if (mutated)             return state.DoS(100, false, REJECT_INVALID, "bad-txns-duplicate", true, "duplicate transaction");     }     // All potential-corruption validation must be done before we do any     // transaction validation, as otherwise we may mark the header as invalid     // because we receive the wrong transactions for it.     // Note that witness malleability is checked in ContextualCheckBlock, so no     // checks that use witness data may be performed here.     // 检查区块大小     // Size limits     if (block.vtx.empty() || block.vtx.size() * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT || ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT)         return state.DoS(100, false, REJECT_INVALID, "bad-blk-length", false, "size limits failed");     // 第一个交易(且只有第一个)是coinbase交易     // First transaction must be coinbase, the rest must not be     if (block.vtx.empty() || !block.vtx[0]->IsCoinBase())         return state.DoS(100, false, REJECT_INVALID, "bad-cb-missing", false, "first tx is not coinbase");     for (unsigned int i = 1; i < block.vtx.size(); i++)         if (block.vtx[i]->IsCoinBase())             return state.DoS(100, false, REJECT_INVALID, "bad-cb-multiple", false, "more than one coinbase");     // 验证区块内的交易并确保它们的有效性     // Check transactions     for (const auto& tx : block.vtx)         if (!CheckTransaction(*tx, state, false))             return state.Invalid(false, state.GetRejectCode(), state.GetRejectReason(),                                  strprintf("Transaction check failed (tx hash %s) %s", tx->GetHash().ToString(), state.GetDebugMessage()));     unsigned int nSigOps = 0;     for (const auto& tx : block.vtx)     {         nSigOps += GetLegacySigOpCount(*tx);     }     if (nSigOps * WITNESS_SCALE_FACTOR > MAX_BLOCK_SIGOPS_COST)         return state.DoS(100, false, REJECT_INVALID, "bad-blk-sigops", false, "out-of-bounds SigOpCount");     if (fCheckPOW && fCheckMerkleRoot)         block.fChecked = true;     return true; } 2. 检查transaction位于:tx_verify.cpp bool CheckTransaction(const CTransaction& tx, CValidationState &state, bool fCheckDuplicateInputs) {     // Basic checks that don't depend on any context     if (tx.vin.empty())         return state.DoS(10, false, REJECT_INVALID, "bad-txns-vin-empty");     if (tx.vout.empty())         return state.DoS(10, false, REJECT_INVALID, "bad-txns-vout-empty");     // Size limits (this doesn't take the witness into account, as that hasn't been checked for malleability)     if (::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT)         return state.DoS(100, false, REJECT_INVALID, "bad-txns-oversize");     // Check for negative or overflow output values     CAmount nValueOut = 0;     for (const auto& txout : tx.vout)     {         if (txout.nValue < 0)             return state.DoS(100, false, REJECT_INVALID, "bad-txns-vout-negative");         if (txout.nValue > MAX_MONEY)             return state.DoS(100, false, REJECT_INVALID, "bad-txns-vout-toolarge");         nValueOut += txout.nValue;         if (!MoneyRange(nValueOut))             return state.DoS(100, false, REJECT_INVALID, "bad-txns-txouttotal-toolarge");     }     // Check for duplicate inputs - note that this check is slow so we skip it in CheckBlock     if (fCheckDuplicateInputs) {         std::set<COutPoint> vInOutPoints;         for (const auto& txin : tx.vin)         {             if (!vInOutPoints.insert(txin.prevout).second)                 return state.DoS(100, false, REJECT_INVALID, "bad-txns-inputs-duplicate");         }     }     if (tx.IsCoinBase())     {         if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100)             return state.DoS(100, false, REJECT_INVALID, "bad-cb-length");     }     else     {         for (const auto& txin : tx.vin)             if (txin.prevout.IsNull())                 return state.DoS(10, false, REJECT_INVALID, "bad-txns-prevout-null");     }     return true; }

要回复问题请先登录注册