Skip to content

Commit a2147d7

Browse files
committed
validation: move GetWitnessCommitmentIndex to consensus/validation
1 parent 15886b0 commit a2147d7

File tree

3 files changed

+30
-28
lines changed

3 files changed

+30
-28
lines changed

src/consensus/validation.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
#include <primitives/transaction.h>
1313
#include <primitives/block.h>
1414

15+
/** Index marker for when no witness commitment is present in a coinbase transaction. */
16+
static constexpr int NO_WITNESS_COMMITMENT{-1};
17+
18+
/** Minimum size of a witness commitment structure. Defined in BIP 141. **/
19+
static constexpr size_t MINIMUM_WITNESS_COMMITMENT{38};
20+
1521
/** A "reason" why a transaction was invalid, suitable for determining whether the
1622
* provider of the transaction should be banned/ignored/disconnected/etc.
1723
*/
@@ -151,4 +157,25 @@ static inline int64_t GetTransactionInputWeight(const CTxIn& txin)
151157
return ::GetSerializeSize(txin, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(txin, PROTOCOL_VERSION) + ::GetSerializeSize(txin.scriptWitness.stack, PROTOCOL_VERSION);
152158
}
153159

160+
/** Compute at which vout of the block's coinbase transaction the witness commitment occurs, or -1 if not found */
161+
inline int GetWitnessCommitmentIndex(const CBlock& block)
162+
{
163+
int commitpos = NO_WITNESS_COMMITMENT;
164+
if (!block.vtx.empty()) {
165+
for (size_t o = 0; o < block.vtx[0]->vout.size(); o++) {
166+
const CTxOut& vout = block.vtx[0]->vout[o];
167+
if (vout.scriptPubKey.size() >= MINIMUM_WITNESS_COMMITMENT &&
168+
vout.scriptPubKey[0] == OP_RETURN &&
169+
vout.scriptPubKey[1] == 0x24 &&
170+
vout.scriptPubKey[2] == 0xaa &&
171+
vout.scriptPubKey[3] == 0x21 &&
172+
vout.scriptPubKey[4] == 0xa9 &&
173+
vout.scriptPubKey[5] == 0xed) {
174+
commitpos = o;
175+
}
176+
}
177+
}
178+
return commitpos;
179+
}
180+
154181
#endif // BITCOIN_CONSENSUS_VALIDATION_H

src/validation.cpp

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3395,31 +3395,11 @@ bool IsWitnessEnabled(const CBlockIndex* pindexPrev, const Consensus::Params& pa
33953395
return (height >= params.SegwitHeight);
33963396
}
33973397

3398-
int GetWitnessCommitmentIndex(const CBlock& block)
3399-
{
3400-
int commitpos = -1;
3401-
if (!block.vtx.empty()) {
3402-
for (size_t o = 0; o < block.vtx[0]->vout.size(); o++) {
3403-
const CTxOut& vout = block.vtx[0]->vout[o];
3404-
if (vout.scriptPubKey.size() >= MINIMUM_WITNESS_COMMITMENT &&
3405-
vout.scriptPubKey[0] == OP_RETURN &&
3406-
vout.scriptPubKey[1] == 0x24 &&
3407-
vout.scriptPubKey[2] == 0xaa &&
3408-
vout.scriptPubKey[3] == 0x21 &&
3409-
vout.scriptPubKey[4] == 0xa9 &&
3410-
vout.scriptPubKey[5] == 0xed) {
3411-
commitpos = o;
3412-
}
3413-
}
3414-
}
3415-
return commitpos;
3416-
}
3417-
34183398
void UpdateUncommittedBlockStructures(CBlock& block, const CBlockIndex* pindexPrev, const Consensus::Params& consensusParams)
34193399
{
34203400
int commitpos = GetWitnessCommitmentIndex(block);
34213401
static const std::vector<unsigned char> nonce(32, 0x00);
3422-
if (commitpos != -1 && IsWitnessEnabled(pindexPrev, consensusParams) && !block.vtx[0]->HasWitness()) {
3402+
if (commitpos != NO_WITNESS_COMMITMENT && IsWitnessEnabled(pindexPrev, consensusParams) && !block.vtx[0]->HasWitness()) {
34233403
CMutableTransaction tx(*block.vtx[0]);
34243404
tx.vin[0].scriptWitness.stack.resize(1);
34253405
tx.vin[0].scriptWitness.stack[0] = nonce;
@@ -3433,7 +3413,7 @@ std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBloc
34333413
int commitpos = GetWitnessCommitmentIndex(block);
34343414
std::vector<unsigned char> ret(32, 0x00);
34353415
if (consensusParams.SegwitHeight != std::numeric_limits<int>::max()) {
3436-
if (commitpos == -1) {
3416+
if (commitpos == NO_WITNESS_COMMITMENT) {
34373417
uint256 witnessroot = BlockWitnessMerkleRoot(block, nullptr);
34383418
CHash256().Write(witnessroot).Write(ret).Finalize(witnessroot);
34393419
CTxOut out;
@@ -3571,7 +3551,7 @@ static bool ContextualCheckBlock(const CBlock& block, BlockValidationState& stat
35713551
bool fHaveWitness = false;
35723552
if (nHeight >= consensusParams.SegwitHeight) {
35733553
int commitpos = GetWitnessCommitmentIndex(block);
3574-
if (commitpos != -1) {
3554+
if (commitpos != NO_WITNESS_COMMITMENT) {
35753555
bool malleated = false;
35763556
uint256 hashWitness = BlockWitnessMerkleRoot(block, &malleated);
35773557
// The malleation check is ignored; as the transaction tree itself

src/validation.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ static const unsigned int DEFAULT_CHECKLEVEL = 3;
9393
// one 128MB block file + added 15% undo data = 147MB greater for a total of 545MB
9494
// Setting the target to >= 550 MiB will make it likely we can respect the target.
9595
static const uint64_t MIN_DISK_SPACE_FOR_BLOCK_FILES = 550 * 1024 * 1024;
96-
/** Minimum size of a witness commitment structure. Defined in BIP 141. **/
97-
static constexpr size_t MINIMUM_WITNESS_COMMITMENT{38};
9896

9997
struct BlockHasher
10098
{
@@ -306,9 +304,6 @@ bool TestBlockValidity(BlockValidationState& state, const CChainParams& chainpar
306304
* Note that transaction witness validation rules are always enforced when P2SH is enforced. */
307305
bool IsWitnessEnabled(const CBlockIndex* pindexPrev, const Consensus::Params& params);
308306

309-
/** Compute at which vout of the block's coinbase transaction the witness commitment occurs, or -1 if not found */
310-
int GetWitnessCommitmentIndex(const CBlock& block);
311-
312307
/** Update uncommitted block structures (currently: only the witness reserved value). This is safe for submitted blocks. */
313308
void UpdateUncommittedBlockStructures(CBlock& block, const CBlockIndex* pindexPrev, const Consensus::Params& consensusParams);
314309

0 commit comments

Comments
 (0)