LCOV - code coverage report
Current view: top level - src - signet.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 100.0 % 80 80
Test Date: 2024-11-04 04:15:01 Functions: 100.0 % 4 4
Branches: 61.9 % 118 73

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

Generated by: LCOV version 2.0-1