Coverage Report

Created: 2026-09-01 13:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/signet.cpp
Line
Count
Source
1
// Copyright (c) 2019-present 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 <consensus/merkle.h>
8
#include <consensus/params.h>
9
#include <consensus/validation.h>
10
#include <primitives/block.h>
11
#include <primitives/transaction.h>
12
#include <script/interpreter.h>
13
#include <script/script.h>
14
#include <script/verify_flags.h>
15
#include <streams.h>
16
#include <uint256.h>
17
#include <util/log.h>
18
19
#include <algorithm>
20
#include <cstddef>
21
#include <cstdint>
22
#include <exception>
23
#include <memory>
24
#include <span>
25
#include <utility>
26
#include <vector>
27
28
static constexpr uint8_t SIGNET_HEADER[4] = {0xec, 0xc7, 0xda, 0xa2};
29
30
static constexpr script_verify_flags BLOCK_SCRIPT_VERIFY_FLAGS = SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_DERSIG | SCRIPT_VERIFY_NULLDUMMY;
31
32
static bool FetchAndClearCommitmentSection(const std::span<const uint8_t> header, CScript& witness_commitment, std::vector<uint8_t>& result)
33
4.28k
{
34
4.28k
    CScript replacement;
35
4.28k
    bool found_header = false;
36
4.28k
    result.clear();
37
38
4.28k
    opcodetype opcode;
39
4.28k
    CScript::const_iterator pc = witness_commitment.begin();
40
4.28k
    std::vector<uint8_t> pushdata;
41
1.29M
    while (witness_commitment.GetOp(pc, opcode, pushdata)) {
  Branch (41:12): [True: 1.29M, False: 4.28k]
42
1.29M
        if (pushdata.size() > 0) {
  Branch (42:13): [True: 79.0k, False: 1.21M]
43
79.0k
            if (!found_header && pushdata.size() > header.size() && std::ranges::equal(std::span{pushdata}.first(header.size()), header)) {
  Branch (43:17): [True: 27.6k, False: 51.4k]
  Branch (43:17): [True: 4.13k, False: 74.8k]
  Branch (43:34): [True: 15.1k, False: 12.4k]
  Branch (43:69): [True: 4.13k, False: 11.0k]
44
                // pushdata only counts if it has the header _and_ some data
45
4.13k
                result.insert(result.end(), pushdata.begin() + header.size(), pushdata.end());
46
4.13k
                pushdata.erase(pushdata.begin() + header.size(), pushdata.end());
47
4.13k
                found_header = true;
48
4.13k
            }
49
79.0k
            replacement << pushdata;
50
1.21M
        } else {
51
1.21M
            replacement << opcode;
52
1.21M
        }
53
1.29M
    }
54
55
4.28k
    if (found_header) witness_commitment = replacement;
  Branch (55:9): [True: 4.13k, False: 144]
56
4.28k
    return found_header;
57
4.28k
}
58
59
static uint256 ComputeModifiedMerkleRoot(const CMutableTransaction& cb, const CBlock& block)
60
4.15k
{
61
4.15k
    std::vector<uint256> leaves;
62
4.15k
    leaves.reserve((block.vtx.size() + 1) & ~1ULL); // capacity rounded up to even
63
4.15k
    leaves.push_back(cb.GetHash().ToUint256());
64
393k
    for (size_t s = 1; s < block.vtx.size(); ++s) {
  Branch (64:24): [True: 389k, False: 4.15k]
65
389k
        leaves.push_back(block.vtx[s]->GetHash().ToUint256());
66
389k
    }
67
4.15k
    return ComputeMerkleRoot(std::move(leaves));
68
4.15k
}
69
70
std::optional<SignetTxs> SignetTxs::Create(const CBlock& block, const CScript& challenge)
71
4.89k
{
72
4.89k
    CMutableTransaction tx_to_spend;
73
4.89k
    tx_to_spend.version = 0;
74
4.89k
    tx_to_spend.nLockTime = 0;
75
4.89k
    tx_to_spend.vin.emplace_back(COutPoint(), CScript(OP_0), 0);
76
4.89k
    tx_to_spend.vout.emplace_back(0, challenge);
77
78
4.89k
    CMutableTransaction tx_spending;
79
4.89k
    tx_spending.version = 0;
80
4.89k
    tx_spending.nLockTime = 0;
81
4.89k
    tx_spending.vin.emplace_back(COutPoint(), CScript(), 0);
82
4.89k
    tx_spending.vout.emplace_back(0, CScript(OP_RETURN));
83
84
    // can't fill any other fields before extracting signet
85
    // responses from block coinbase tx
86
87
    // find and delete signet signature
88
4.89k
    if (block.vtx.empty()) return std::nullopt; // no coinbase tx in block; invalid
  Branch (88:9): [True: 367, False: 4.52k]
89
4.52k
    CMutableTransaction modified_cb(*block.vtx.at(0));
90
91
4.52k
    const int cidx = GetWitnessCommitmentIndex(block);
92
4.52k
    if (cidx == NO_WITNESS_COMMITMENT) {
  Branch (92:9): [True: 240, False: 4.28k]
93
240
        return std::nullopt; // require a witness commitment
94
240
    }
95
96
4.28k
    CScript& witness_commitment = modified_cb.vout.at(cidx).scriptPubKey;
97
98
4.28k
    std::vector<uint8_t> signet_solution;
99
4.28k
    if (!FetchAndClearCommitmentSection(SIGNET_HEADER, witness_commitment, signet_solution)) {
  Branch (99:9): [True: 144, False: 4.13k]
100
        // no signet solution -- allow this to support OP_TRUE as trivial block challenge
101
4.13k
    } else {
102
4.13k
        try {
103
4.13k
            SpanReader v{signet_solution};
104
4.13k
            v >> tx_spending.vin[0].scriptSig;
105
4.13k
            v >> tx_spending.vin[0].scriptWitness.stack;
106
4.13k
            if (!v.empty()) return std::nullopt; // extraneous data encountered
  Branch (106:17): [True: 11, False: 4.12k]
107
4.13k
        } catch (const std::exception&) {
108
120
            return std::nullopt; // parsing error
109
120
        }
110
4.13k
    }
111
4.15k
    uint256 signet_merkle = ComputeModifiedMerkleRoot(modified_cb, block);
112
113
4.15k
    std::vector<uint8_t> block_data;
114
4.15k
    VectorWriter writer{block_data, 0};
115
4.15k
    writer << block.nVersion;
116
4.15k
    writer << block.hashPrevBlock;
117
4.15k
    writer << signet_merkle;
118
4.15k
    writer << block.nTime;
119
4.15k
    tx_to_spend.vin[0].scriptSig << block_data;
120
4.15k
    tx_spending.vin[0].prevout = COutPoint(tx_to_spend.GetHash(), 0);
121
122
4.15k
    return SignetTxs{tx_to_spend, tx_spending};
123
4.28k
}
124
125
// Signet block solution checker
126
bool CheckSignetBlockSolution(const CBlock& block, const Consensus::Params& consensusParams)
127
2.50k
{
128
2.50k
    if (block.GetHash() == consensusParams.hashGenesisBlock) {
  Branch (128:9): [True: 126, False: 2.38k]
129
        // genesis block solution is always valid
130
126
        return true;
131
126
    }
132
133
2.38k
    const CScript challenge(consensusParams.signet_challenge.begin(), consensusParams.signet_challenge.end());
134
2.38k
    const std::optional<SignetTxs> signet_txs = SignetTxs::Create(block, challenge);
135
136
2.38k
    if (!signet_txs) {
  Branch (136:9): [True: 329, False: 2.05k]
137
329
        LogDebug(BCLog::VALIDATION, "CheckSignetBlockSolution: Errors in block (block solution parse failure)\n");
138
329
        return false;
139
329
    }
140
141
2.05k
    const CScript& scriptSig = signet_txs->m_to_sign.vin[0].scriptSig;
142
2.05k
    const CScriptWitness& witness = signet_txs->m_to_sign.vin[0].scriptWitness;
143
144
2.05k
    PrecomputedTransactionData txdata;
145
2.05k
    txdata.Init(signet_txs->m_to_sign, {signet_txs->m_to_spend.vout[0]});
146
2.05k
    TransactionSignatureChecker sigcheck(&signet_txs->m_to_sign, /* nInIn= */ 0, /* amountIn= */ signet_txs->m_to_spend.vout[0].nValue, txdata, MissingDataBehavior::ASSERT_FAIL);
147
148
2.05k
    if (!VerifyScript(scriptSig, signet_txs->m_to_spend.vout[0].scriptPubKey, &witness, BLOCK_SCRIPT_VERIFY_FLAGS, sigcheck)) {
  Branch (148:9): [True: 2.05k, False: 0]
149
2.05k
        LogDebug(BCLog::VALIDATION, "CheckSignetBlockSolution: Errors in block (block solution invalid)\n");
150
2.05k
        return false;
151
2.05k
    }
152
0
    return true;
153
2.05k
}