Bitcoin Core  29.1.0
P2P Digital Currency
signet.cpp
Go to the documentation of this file.
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 
30 
31 static bool FetchAndClearCommitmentSection(const Span<const uint8_t> header, CScript& witness_commitment, std::vector<uint8_t>& result)
32 {
33  CScript replacement;
34  bool found_header = false;
35  result.clear();
36 
37  opcodetype opcode;
38  CScript::const_iterator pc = witness_commitment.begin();
39  std::vector<uint8_t> pushdata;
40  while (witness_commitment.GetOp(pc, opcode, pushdata)) {
41  if (pushdata.size() > 0) {
42  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  result.insert(result.end(), pushdata.begin() + header.size(), pushdata.end());
45  pushdata.erase(pushdata.begin() + header.size(), pushdata.end());
46  found_header = true;
47  }
48  replacement << pushdata;
49  } else {
50  replacement << opcode;
51  }
52  }
53 
54  if (found_header) witness_commitment = replacement;
55  return found_header;
56 }
57 
59 {
60  std::vector<uint256> leaves;
61  leaves.resize(block.vtx.size());
62  leaves[0] = cb.GetHash();
63  for (size_t s = 1; s < block.vtx.size(); ++s) {
64  leaves[s] = block.vtx[s]->GetHash();
65  }
66  return ComputeMerkleRoot(std::move(leaves));
67 }
68 
69 std::optional<SignetTxs> SignetTxs::Create(const CBlock& block, const CScript& challenge)
70 {
71  CMutableTransaction tx_to_spend;
72  tx_to_spend.version = 0;
73  tx_to_spend.nLockTime = 0;
74  tx_to_spend.vin.emplace_back(COutPoint(), CScript(OP_0), 0);
75  tx_to_spend.vout.emplace_back(0, challenge);
76 
77  CMutableTransaction tx_spending;
78  tx_spending.version = 0;
79  tx_spending.nLockTime = 0;
80  tx_spending.vin.emplace_back(COutPoint(), CScript(), 0);
81  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  if (block.vtx.empty()) return std::nullopt; // no coinbase tx in block; invalid
88  CMutableTransaction modified_cb(*block.vtx.at(0));
89 
90  const int cidx = GetWitnessCommitmentIndex(block);
91  if (cidx == NO_WITNESS_COMMITMENT) {
92  return std::nullopt; // require a witness commitment
93  }
94 
95  CScript& witness_commitment = modified_cb.vout.at(cidx).scriptPubKey;
96 
97  std::vector<uint8_t> signet_solution;
98  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  try {
102  SpanReader v{signet_solution};
103  v >> tx_spending.vin[0].scriptSig;
104  v >> tx_spending.vin[0].scriptWitness.stack;
105  if (!v.empty()) return std::nullopt; // extraneous data encountered
106  } catch (const std::exception&) {
107  return std::nullopt; // parsing error
108  }
109  }
110  uint256 signet_merkle = ComputeModifiedMerkleRoot(modified_cb, block);
111 
112  std::vector<uint8_t> block_data;
113  VectorWriter writer{block_data, 0};
114  writer << block.nVersion;
115  writer << block.hashPrevBlock;
116  writer << signet_merkle;
117  writer << block.nTime;
118  tx_to_spend.vin[0].scriptSig << block_data;
119  tx_spending.vin[0].prevout = COutPoint(tx_to_spend.GetHash(), 0);
120 
121  return SignetTxs{tx_to_spend, tx_spending};
122 }
123 
124 // Signet block solution checker
125 bool CheckSignetBlockSolution(const CBlock& block, const Consensus::Params& consensusParams)
126 {
127  if (block.GetHash() == consensusParams.hashGenesisBlock) {
128  // genesis block solution is always valid
129  return true;
130  }
131 
132  const CScript challenge(consensusParams.signet_challenge.begin(), consensusParams.signet_challenge.end());
133  const std::optional<SignetTxs> signet_txs = SignetTxs::Create(block, challenge);
134 
135  if (!signet_txs) {
136  LogDebug(BCLog::VALIDATION, "CheckSignetBlockSolution: Errors in block (block solution parse failure)\n");
137  return false;
138  }
139 
140  const CScript& scriptSig = signet_txs->m_to_sign.vin[0].scriptSig;
141  const CScriptWitness& witness = signet_txs->m_to_sign.vin[0].scriptWitness;
142 
144  txdata.Init(signet_txs->m_to_sign, {signet_txs->m_to_spend.vout[0]});
145  TransactionSignatureChecker sigcheck(&signet_txs->m_to_sign, /* nInIn= */ 0, /* amountIn= */ signet_txs->m_to_spend.vout[0].nValue, txdata, MissingDataBehavior::ASSERT_FAIL);
146 
147  if (!VerifyScript(scriptSig, signet_txs->m_to_spend.vout[0].scriptPubKey, &witness, BLOCK_SCRIPT_VERIFY_FLAGS, sigcheck)) {
148  LogDebug(BCLog::VALIDATION, "CheckSignetBlockSolution: Errors in block (block solution invalid)\n");
149  return false;
150  }
151  return true;
152 }
CONSTEXPR_IF_NOT_DEBUG Span< C > first(std::size_t count) const noexcept
Definition: span.h:205
static std::optional< SignetTxs > Create(const CBlock &block, const CScript &challenge)
Definition: signet.cpp:69
bool GetOp(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet) const
Definition: script.h:506
Definition: block.h:68
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
std::vector< CTxIn > vin
Definition: transaction.h:379
constexpr std::size_t size() const noexcept
Definition: span.h:187
Definition: script.h:76
static constexpr int NO_WITNESS_COMMITMENT
Index marker for when no witness commitment is present in a coinbase transaction. ...
Definition: validation.h:15
uint32_t nTime
Definition: block.h:28
Minimal stream for reading from an existing byte array by Span.
Definition: streams.h:100
Abort execution through assertion failure (for consensus code)
void Init(const T &tx, std::vector< CTxOut > &&spent_outputs, bool force=false)
Initialize this PrecomputedTransactionData with transaction data.
static bool FetchAndClearCommitmentSection(const Span< const uint8_t > header, CScript &witness_commitment, std::vector< uint8_t > &result)
Definition: signet.cpp:31
uint256 hashPrevBlock
Definition: block.h:26
opcodetype
Script opcodes.
Definition: script.h:73
Txid GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:69
Parameters that influence chain consensus.
Definition: params.h:74
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:28
std::vector< CTxOut > vout
Definition: transaction.h:380
uint256 GetHash() const
Definition: block.cpp:11
256-bit opaque blob.
Definition: uint256.h:201
uint256 ComputeMerkleRoot(std::vector< uint256 > hashes, bool *mutated)
Definition: merkle.cpp:46
std::vector< CTransactionRef > vtx
Definition: block.h:72
auto result
Definition: common-types.h:74
#define LogDebug(category,...)
Definition: logging.h:381
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:414
std::vector< uint8_t > signet_challenge
Definition: params.h:134
static uint256 ComputeModifiedMerkleRoot(const CMutableTransaction &cb, const CBlock &block)
Definition: signet.cpp:58
iterator begin()
Definition: prevector.h:302
A mutable version of CTransaction.
Definition: transaction.h:377
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:97
static constexpr unsigned int BLOCK_SCRIPT_VERIFY_FLAGS
Definition: signet.cpp:29
Generate the signet tx corresponding to the given block.
Definition: signet.h:26
int GetWitnessCommitmentIndex(const CBlock &block)
Compute at which vout of the block&#39;s coinbase transaction the witness commitment occurs, or -1 if not found.
Definition: validation.h:148
int32_t nVersion
Definition: block.h:25
uint256 hashGenesisBlock
Definition: params.h:75
static constexpr uint8_t SIGNET_HEADER[4]
Definition: signet.cpp:27
bool CheckSignetBlockSolution(const CBlock &block, const Consensus::Params &consensusParams)
Extract signature and check whether a block has a valid solution.
Definition: signet.cpp:125