mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-05-05 17:30:59 +02:00
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.
This commit is contained in:
parent
fa2ad5dae1
commit
77771a03df
@ -65,7 +65,7 @@ static uint256 ComputeModifiedMerkleRoot(const CMutableTransaction& cb, const CB
|
||||
return ComputeMerkleRoot(std::move(leaves));
|
||||
}
|
||||
|
||||
SignetTxs SignetTxs::Create(const CBlock& block, const CScript& challenge)
|
||||
Optional<SignetTxs> SignetTxs::Create(const CBlock& block, const CScript& challenge)
|
||||
{
|
||||
CMutableTransaction tx_to_spend;
|
||||
tx_to_spend.nVersion = 0;
|
||||
@ -83,12 +83,12 @@ SignetTxs SignetTxs::Create(const CBlock& block, const CScript& challenge)
|
||||
// responses from block coinbase tx
|
||||
|
||||
// find and delete signet signature
|
||||
if (block.vtx.empty()) return invalid(); // no coinbase tx in block; invalid
|
||||
if (block.vtx.empty()) return nullopt; // no coinbase tx in block; invalid
|
||||
CMutableTransaction modified_cb(*block.vtx.at(0));
|
||||
|
||||
const int cidx = GetWitnessCommitmentIndex(block);
|
||||
if (cidx == NO_WITNESS_COMMITMENT) {
|
||||
return invalid(); // require a witness commitment
|
||||
return nullopt; // require a witness commitment
|
||||
}
|
||||
|
||||
CScript& witness_commitment = modified_cb.vout.at(cidx).scriptPubKey;
|
||||
@ -101,9 +101,9 @@ SignetTxs SignetTxs::Create(const CBlock& block, const CScript& challenge)
|
||||
VectorReader v(SER_NETWORK, INIT_PROTO_VERSION, signet_solution, 0);
|
||||
v >> tx_spending.vin[0].scriptSig;
|
||||
v >> tx_spending.vin[0].scriptWitness.stack;
|
||||
if (!v.empty()) return invalid(); // extraneous data encountered
|
||||
if (!v.empty()) return nullopt; // extraneous data encountered
|
||||
} catch (const std::exception&) {
|
||||
return invalid(); // parsing error
|
||||
return nullopt; // parsing error
|
||||
}
|
||||
}
|
||||
uint256 signet_merkle = ComputeModifiedMerkleRoot(modified_cb, block);
|
||||
@ -117,7 +117,7 @@ SignetTxs SignetTxs::Create(const CBlock& block, const CScript& challenge)
|
||||
tx_to_spend.vin[0].scriptSig << block_data;
|
||||
tx_spending.vin[0].prevout = COutPoint(tx_to_spend.GetHash(), 0);
|
||||
|
||||
return {tx_to_spend, tx_spending};
|
||||
return SignetTxs{tx_to_spend, tx_spending};
|
||||
}
|
||||
|
||||
// Signet block solution checker
|
||||
@ -129,19 +129,19 @@ bool CheckSignetBlockSolution(const CBlock& block, const Consensus::Params& cons
|
||||
}
|
||||
|
||||
const CScript challenge(consensusParams.signet_challenge.begin(), consensusParams.signet_challenge.end());
|
||||
const SignetTxs signet_txs(block, challenge);
|
||||
const Optional<SignetTxs> signet_txs = SignetTxs::Create(block, challenge);
|
||||
|
||||
if (!signet_txs.m_valid) {
|
||||
if (!signet_txs) {
|
||||
LogPrint(BCLog::VALIDATION, "CheckSignetBlockSolution: Errors in block (block solution parse failure)\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
const CScript& scriptSig = signet_txs.m_to_sign.vin[0].scriptSig;
|
||||
const CScriptWitness& witness = signet_txs.m_to_sign.vin[0].scriptWitness;
|
||||
const CScript& scriptSig = signet_txs->m_to_sign.vin[0].scriptSig;
|
||||
const CScriptWitness& witness = signet_txs->m_to_sign.vin[0].scriptWitness;
|
||||
|
||||
TransactionSignatureChecker sigcheck(&signet_txs.m_to_sign, /*nIn=*/ 0, /*amount=*/ signet_txs.m_to_spend.vout[0].nValue);
|
||||
TransactionSignatureChecker sigcheck(&signet_txs->m_to_sign, /*nIn=*/ 0, /*amount=*/ signet_txs->m_to_spend.vout[0].nValue);
|
||||
|
||||
if (!VerifyScript(scriptSig, signet_txs.m_to_spend.vout[0].scriptPubKey, &witness, BLOCK_SCRIPT_VERIFY_FLAGS, sigcheck)) {
|
||||
if (!VerifyScript(scriptSig, signet_txs->m_to_spend.vout[0].scriptPubKey, &witness, BLOCK_SCRIPT_VERIFY_FLAGS, sigcheck)) {
|
||||
LogPrint(BCLog::VALIDATION, "CheckSignetBlockSolution: Errors in block (block solution invalid)\n");
|
||||
return false;
|
||||
}
|
||||
|
13
src/signet.h
13
src/signet.h
@ -9,6 +9,8 @@
|
||||
#include <primitives/block.h>
|
||||
#include <primitives/transaction.h>
|
||||
|
||||
#include <optional.h>
|
||||
|
||||
/**
|
||||
* Extract signature and check whether a block has a valid solution
|
||||
*/
|
||||
@ -22,21 +24,14 @@ bool CheckSignetBlockSolution(const CBlock& block, const Consensus::Params& cons
|
||||
* 2. It skips the nonce.
|
||||
*/
|
||||
class SignetTxs {
|
||||
private:
|
||||
struct invalid {};
|
||||
SignetTxs(invalid i) : m_to_spend(), m_to_sign(), m_valid(false) { }
|
||||
|
||||
template<class T1, class T2>
|
||||
SignetTxs(const T1& to_spend, const T2& to_sign) : m_to_spend{to_spend}, m_to_sign{to_sign}, m_valid(true) { }
|
||||
|
||||
static SignetTxs Create(const CBlock& block, const CScript& challenge);
|
||||
SignetTxs(const T1& to_spend, const T2& to_sign) : m_to_spend{to_spend}, m_to_sign{to_sign} { }
|
||||
|
||||
public:
|
||||
SignetTxs(const CBlock& block, const CScript& challenge) : SignetTxs(Create(block, challenge)) { }
|
||||
static Optional<SignetTxs> Create(const CBlock& block, const CScript& challenge);
|
||||
|
||||
const CTransaction m_to_spend;
|
||||
const CTransaction m_to_sign;
|
||||
const bool m_valid;
|
||||
};
|
||||
|
||||
#endif // BITCOIN_SIGNET_H
|
||||
|
@ -29,6 +29,6 @@ void test_one_input(const std::vector<uint8_t>& buffer)
|
||||
}
|
||||
(void)CheckSignetBlockSolution(*block, Params().GetConsensus());
|
||||
if (GetWitnessCommitmentIndex(*block) != NO_WITNESS_COMMITMENT) {
|
||||
(void)SignetTxs(*block, ConsumeScript(fuzzed_data_provider));
|
||||
(void)SignetTxs::Create(*block, ConsumeScript(fuzzed_data_provider));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user