LCOV - code coverage report
Current view: top level - src - signet.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 83 84 98.8 %
Date: 2023-10-05 15:40:34 Functions: 5 5 100.0 %
Branches: 93 168 55.4 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2019-2021 The Bitcoin Core developers
       2                 :            : // Distributed under the MIT software license, see the accompanying
       3                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :            : 
       5                 :            : #include <signet.h>
       6                 :            : 
       7                 :            : #include <array>
       8                 :            : #include <cstdint>
       9                 :            : #include <vector>
      10                 :            : 
      11                 :            : #include <common/system.h>
      12                 :            : #include <consensus/merkle.h>
      13                 :            : #include <consensus/params.h>
      14                 :            : #include <consensus/validation.h>
      15                 :            : #include <core_io.h>
      16                 :            : #include <hash.h>
      17                 :            : #include <logging.h>
      18                 :            : #include <primitives/block.h>
      19                 :            : #include <primitives/transaction.h>
      20                 :            : #include <script/interpreter.h>
      21                 :            : #include <span.h>
      22                 :            : #include <streams.h>
      23                 :            : #include <uint256.h>
      24                 :            : #include <util/strencodings.h>
      25                 :            : 
      26                 :            : static constexpr uint8_t SIGNET_HEADER[4] = {0xec, 0xc7, 0xda, 0xa2};
      27                 :            : 
      28                 :            : static constexpr unsigned int BLOCK_SCRIPT_VERIFY_FLAGS = SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_DERSIG | SCRIPT_VERIFY_NULLDUMMY;
      29                 :            : 
      30                 :       1200 : static bool FetchAndClearCommitmentSection(const Span<const uint8_t> header, CScript& witness_commitment, std::vector<uint8_t>& result)
      31                 :            : {
      32                 :       1200 :     CScript replacement;
      33                 :       1200 :     bool found_header = false;
      34                 :       1200 :     result.clear();
      35                 :            : 
      36                 :            :     opcodetype opcode;
      37   [ +  -  +  - ]:       1200 :     CScript::const_iterator pc = witness_commitment.begin();
      38                 :       1200 :     std::vector<uint8_t> pushdata;
      39   [ +  -  +  + ]:     642533 :     while (witness_commitment.GetOp(pc, opcode, pushdata)) {
      40         [ +  + ]:     641333 :         if (pushdata.size() > 0) {
      41   [ +  +  +  +  :      90145 :             if (!found_header && pushdata.size() > (size_t)header.size() && Span{pushdata}.first(header.size()) == header) {
             +  -  +  + ]
      42                 :            :                 // pushdata only counts if it has the header _and_ some data
      43         [ +  - ]:       1108 :                 result.insert(result.end(), pushdata.begin() + header.size(), pushdata.end());
      44         [ +  - ]:       1108 :                 pushdata.erase(pushdata.begin() + header.size(), pushdata.end());
      45                 :       1108 :                 found_header = true;
      46                 :       1108 :             }
      47         [ +  - ]:      90145 :             replacement << pushdata;
      48                 :      90145 :         } else {
      49         [ +  - ]:     551188 :             replacement << opcode;
      50                 :            :         }
      51                 :            :     }
      52                 :            : 
      53   [ +  +  +  - ]:       1200 :     if (found_header) witness_commitment = replacement;
      54                 :       1200 :     return found_header;
      55                 :       1200 : }
      56                 :            : 
      57                 :       1147 : static uint256 ComputeModifiedMerkleRoot(const CMutableTransaction& cb, const CBlock& block)
      58                 :            : {
      59                 :       1147 :     std::vector<uint256> leaves;
      60         [ +  - ]:       1147 :     leaves.resize(block.vtx.size());
      61         [ +  - ]:       1147 :     leaves[0] = cb.GetHash();
      62         [ +  + ]:     147250 :     for (size_t s = 1; s < block.vtx.size(); ++s) {
      63         [ +  - ]:     146103 :         leaves[s] = block.vtx[s]->GetHash();
      64                 :     146103 :     }
      65         [ -  + ]:       1147 :     return ComputeMerkleRoot(std::move(leaves));
      66                 :       1147 : }
      67                 :            : 
      68                 :       1571 : std::optional<SignetTxs> SignetTxs::Create(const CBlock& block, const CScript& challenge)
      69                 :            : {
      70                 :       1571 :     CMutableTransaction tx_to_spend;
      71                 :       1571 :     tx_to_spend.nVersion = 0;
      72                 :       1571 :     tx_to_spend.nLockTime = 0;
      73   [ +  -  +  -  :       1571 :     tx_to_spend.vin.emplace_back(COutPoint(), CScript(OP_0), 0);
                   +  - ]
      74         [ +  - ]:       1744 :     tx_to_spend.vout.emplace_back(0, challenge);
      75                 :            : 
      76         [ +  - ]:       1571 :     CMutableTransaction tx_spending;
      77                 :       1571 :     tx_spending.nVersion = 0;
      78                 :       1571 :     tx_spending.nLockTime = 0;
      79   [ +  -  +  -  :       1571 :     tx_spending.vin.emplace_back(COutPoint(), CScript(), 0);
                   +  - ]
      80   [ +  -  +  - ]:       1571 :     tx_spending.vout.emplace_back(0, CScript(OP_RETURN));
      81                 :            : 
      82                 :            :     // can't fill any other fields before extracting signet
      83                 :            :     // responses from block coinbase tx
      84                 :            : 
      85                 :            :     // find and delete signet signature
      86         [ +  + ]:       1571 :     if (block.vtx.empty()) return std::nullopt; // no coinbase tx in block; invalid
      87   [ +  -  +  - ]:       1354 :     CMutableTransaction modified_cb(*block.vtx.at(0));
      88                 :            : 
      89         [ +  - ]:       1354 :     const int cidx = GetWitnessCommitmentIndex(block);
      90         [ +  + ]:       1354 :     if (cidx == NO_WITNESS_COMMITMENT) {
      91                 :        154 :         return std::nullopt; // require a witness commitment
      92                 :            :     }
      93                 :            : 
      94         [ +  - ]:       1200 :     CScript& witness_commitment = modified_cb.vout.at(cidx).scriptPubKey;
      95                 :            : 
      96                 :       1200 :     std::vector<uint8_t> signet_solution;
      97   [ +  -  +  + ]:       1200 :     if (!FetchAndClearCommitmentSection(SIGNET_HEADER, witness_commitment, signet_solution)) {
      98                 :            :         // no signet solution -- allow this to support OP_TRUE as trivial block challenge
      99                 :         92 :     } else {
     100                 :            :         try {
     101   [ +  -  +  - ]:       1108 :             SpanReader v{INIT_PROTO_VERSION, signet_solution};
     102         [ +  + ]:       1108 :             v >> tx_spending.vin[0].scriptSig;
     103         [ +  + ]:       1098 :             v >> tx_spending.vin[0].scriptWitness.stack;
     104   [ +  -  +  + ]:       1060 :             if (!v.empty()) return std::nullopt; // extraneous data encountered
     105         [ +  - ]:       1103 :         } catch (const std::exception&) {
     106                 :         48 :             return std::nullopt; // parsing error
     107         [ +  - ]:         48 :         }
     108                 :            :     }
     109         [ +  - ]:       1147 :     uint256 signet_merkle = ComputeModifiedMerkleRoot(modified_cb, block);
     110                 :            : 
     111                 :       1147 :     std::vector<uint8_t> block_data;
     112         [ +  - ]:       1147 :     CVectorWriter writer{INIT_PROTO_VERSION, block_data, 0};
     113         [ +  - ]:       1147 :     writer << block.nVersion;
     114         [ +  - ]:       1147 :     writer << block.hashPrevBlock;
     115         [ +  - ]:       1147 :     writer << signet_merkle;
     116         [ +  - ]:       1147 :     writer << block.nTime;
     117         [ +  - ]:       1147 :     tx_to_spend.vin[0].scriptSig << block_data;
     118   [ +  -  +  - ]:       1147 :     tx_spending.vin[0].prevout = COutPoint(tx_to_spend.GetHash(), 0);
     119                 :            : 
     120   [ +  -  +  - ]:       1147 :     return SignetTxs{tx_to_spend, tx_spending};
     121                 :       1619 : }
     122                 :            : 
     123                 :            : // Signet block solution checker
     124                 :        814 : bool CheckSignetBlockSolution(const CBlock& block, const Consensus::Params& consensusParams)
     125                 :            : {
     126         [ +  + ]:        814 :     if (block.GetHash() == consensusParams.hashGenesisBlock) {
     127                 :            :         // genesis block solution is always valid
     128                 :         57 :         return true;
     129                 :            :     }
     130                 :            : 
     131                 :        757 :     const CScript challenge(consensusParams.signet_challenge.begin(), consensusParams.signet_challenge.end());
     132         [ +  - ]:        757 :     const std::optional<SignetTxs> signet_txs = SignetTxs::Create(block, challenge);
     133                 :            : 
     134         [ +  + ]:        757 :     if (!signet_txs) {
     135   [ +  -  +  -  :        195 :         LogPrint(BCLog::VALIDATION, "CheckSignetBlockSolution: Errors in block (block solution parse failure)\n");
          #  #  #  #  #  
                      # ]
     136                 :        195 :         return false;
     137                 :            :     }
     138                 :            : 
     139         [ +  - ]:        562 :     const CScript& scriptSig = signet_txs->m_to_sign.vin[0].scriptSig;
     140         [ +  - ]:        562 :     const CScriptWitness& witness = signet_txs->m_to_sign.vin[0].scriptWitness;
     141                 :            : 
     142         [ +  - ]:        562 :     PrecomputedTransactionData txdata;
     143   [ +  -  +  -  :        562 :     txdata.Init(signet_txs->m_to_sign, {signet_txs->m_to_spend.vout[0]});
          +  -  +  -  +  
                      - ]
     144   [ +  -  +  -  :        562 :     TransactionSignatureChecker sigcheck(&signet_txs->m_to_sign, /* nInIn= */ 0, /* amountIn= */ signet_txs->m_to_spend.vout[0].nValue, txdata, MissingDataBehavior::ASSERT_FAIL);
                   +  - ]
     145                 :            : 
     146   [ +  -  +  -  :        562 :     if (!VerifyScript(scriptSig, signet_txs->m_to_spend.vout[0].scriptPubKey, &witness, BLOCK_SCRIPT_VERIFY_FLAGS, sigcheck)) {
                   -  + ]
     147   [ +  -  +  -  :        562 :         LogPrint(BCLog::VALIDATION, "CheckSignetBlockSolution: Errors in block (block solution invalid)\n");
          #  #  #  #  #  
                      # ]
     148                 :        562 :         return false;
     149                 :            :     }
     150                 :          0 :     return true;
     151                 :        814 : }

Generated by: LCOV version 1.14