Skip to content

Commit 77771a0

Browse files
author
MarcoFalke
committed
refactor: Remove SignetTxs::m_valid and use optional instead
m_valid implies the block solution has been checked, which is not the case. It only means the txs could be parsed. C++17 comes with std::optional, so just use that instead.
1 parent fa2ad5d commit 77771a0

File tree

3 files changed

+17
-22
lines changed

3 files changed

+17
-22
lines changed

src/signet.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ static uint256 ComputeModifiedMerkleRoot(const CMutableTransaction& cb, const CB
6565
return ComputeMerkleRoot(std::move(leaves));
6666
}
6767

68-
SignetTxs SignetTxs::Create(const CBlock& block, const CScript& challenge)
68+
Optional<SignetTxs> SignetTxs::Create(const CBlock& block, const CScript& challenge)
6969
{
7070
CMutableTransaction tx_to_spend;
7171
tx_to_spend.nVersion = 0;
@@ -83,12 +83,12 @@ SignetTxs SignetTxs::Create(const CBlock& block, const CScript& challenge)
8383
// responses from block coinbase tx
8484

8585
// find and delete signet signature
86-
if (block.vtx.empty()) return invalid(); // no coinbase tx in block; invalid
86+
if (block.vtx.empty()) return nullopt; // no coinbase tx in block; invalid
8787
CMutableTransaction modified_cb(*block.vtx.at(0));
8888

8989
const int cidx = GetWitnessCommitmentIndex(block);
9090
if (cidx == NO_WITNESS_COMMITMENT) {
91-
return invalid(); // require a witness commitment
91+
return nullopt; // require a witness commitment
9292
}
9393

9494
CScript& witness_commitment = modified_cb.vout.at(cidx).scriptPubKey;
@@ -101,9 +101,9 @@ SignetTxs SignetTxs::Create(const CBlock& block, const CScript& challenge)
101101
VectorReader v(SER_NETWORK, INIT_PROTO_VERSION, signet_solution, 0);
102102
v >> tx_spending.vin[0].scriptSig;
103103
v >> tx_spending.vin[0].scriptWitness.stack;
104-
if (!v.empty()) return invalid(); // extraneous data encountered
104+
if (!v.empty()) return nullopt; // extraneous data encountered
105105
} catch (const std::exception&) {
106-
return invalid(); // parsing error
106+
return nullopt; // parsing error
107107
}
108108
}
109109
uint256 signet_merkle = ComputeModifiedMerkleRoot(modified_cb, block);
@@ -117,7 +117,7 @@ SignetTxs SignetTxs::Create(const CBlock& block, const CScript& challenge)
117117
tx_to_spend.vin[0].scriptSig << block_data;
118118
tx_spending.vin[0].prevout = COutPoint(tx_to_spend.GetHash(), 0);
119119

120-
return {tx_to_spend, tx_spending};
120+
return SignetTxs{tx_to_spend, tx_spending};
121121
}
122122

123123
// Signet block solution checker
@@ -129,19 +129,19 @@ bool CheckSignetBlockSolution(const CBlock& block, const Consensus::Params& cons
129129
}
130130

131131
const CScript challenge(consensusParams.signet_challenge.begin(), consensusParams.signet_challenge.end());
132-
const SignetTxs signet_txs(block, challenge);
132+
const Optional<SignetTxs> signet_txs = SignetTxs::Create(block, challenge);
133133

134-
if (!signet_txs.m_valid) {
134+
if (!signet_txs) {
135135
LogPrint(BCLog::VALIDATION, "CheckSignetBlockSolution: Errors in block (block solution parse failure)\n");
136136
return false;
137137
}
138138

139-
const CScript& scriptSig = signet_txs.m_to_sign.vin[0].scriptSig;
140-
const CScriptWitness& witness = signet_txs.m_to_sign.vin[0].scriptWitness;
139+
const CScript& scriptSig = signet_txs->m_to_sign.vin[0].scriptSig;
140+
const CScriptWitness& witness = signet_txs->m_to_sign.vin[0].scriptWitness;
141141

142-
TransactionSignatureChecker sigcheck(&signet_txs.m_to_sign, /*nIn=*/ 0, /*amount=*/ signet_txs.m_to_spend.vout[0].nValue);
142+
TransactionSignatureChecker sigcheck(&signet_txs->m_to_sign, /*nIn=*/ 0, /*amount=*/ signet_txs->m_to_spend.vout[0].nValue);
143143

144-
if (!VerifyScript(scriptSig, signet_txs.m_to_spend.vout[0].scriptPubKey, &witness, BLOCK_SCRIPT_VERIFY_FLAGS, sigcheck)) {
144+
if (!VerifyScript(scriptSig, signet_txs->m_to_spend.vout[0].scriptPubKey, &witness, BLOCK_SCRIPT_VERIFY_FLAGS, sigcheck)) {
145145
LogPrint(BCLog::VALIDATION, "CheckSignetBlockSolution: Errors in block (block solution invalid)\n");
146146
return false;
147147
}

src/signet.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <primitives/block.h>
1010
#include <primitives/transaction.h>
1111

12+
#include <optional.h>
13+
1214
/**
1315
* Extract signature and check whether a block has a valid solution
1416
*/
@@ -22,21 +24,14 @@ bool CheckSignetBlockSolution(const CBlock& block, const Consensus::Params& cons
2224
* 2. It skips the nonce.
2325
*/
2426
class SignetTxs {
25-
private:
26-
struct invalid {};
27-
SignetTxs(invalid i) : m_to_spend(), m_to_sign(), m_valid(false) { }
28-
2927
template<class T1, class T2>
30-
SignetTxs(const T1& to_spend, const T2& to_sign) : m_to_spend{to_spend}, m_to_sign{to_sign}, m_valid(true) { }
31-
32-
static SignetTxs Create(const CBlock& block, const CScript& challenge);
28+
SignetTxs(const T1& to_spend, const T2& to_sign) : m_to_spend{to_spend}, m_to_sign{to_sign} { }
3329

3430
public:
35-
SignetTxs(const CBlock& block, const CScript& challenge) : SignetTxs(Create(block, challenge)) { }
31+
static Optional<SignetTxs> Create(const CBlock& block, const CScript& challenge);
3632

3733
const CTransaction m_to_spend;
3834
const CTransaction m_to_sign;
39-
const bool m_valid;
4035
};
4136

4237
#endif // BITCOIN_SIGNET_H

src/test/fuzz/signet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ void test_one_input(const std::vector<uint8_t>& buffer)
2929
}
3030
(void)CheckSignetBlockSolution(*block, Params().GetConsensus());
3131
if (GetWitnessCommitmentIndex(*block) != NO_WITNESS_COMMITMENT) {
32-
(void)SignetTxs(*block, ConsumeScript(fuzzed_data_provider));
32+
(void)SignetTxs::Create(*block, ConsumeScript(fuzzed_data_provider));
3333
}
3434
}

0 commit comments

Comments
 (0)