Skip to content

Commit 9620f95

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

File tree

3 files changed

+29
-28
lines changed

3 files changed

+29
-28
lines changed

src/consensus/validation.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,30 @@ static inline int64_t GetTransactionInputWeight(const CTxIn& txin)
151151
return ::GetSerializeSize(txin, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(txin, PROTOCOL_VERSION) + ::GetSerializeSize(txin.scriptWitness.stack, PROTOCOL_VERSION);
152152
}
153153

154+
/** Index marker for when no witness commitment is present in a coinbase transaction. */
155+
static constexpr int NO_WITNESS_COMMITMENT{-1};
156+
/** Minimum size of a witness commitment structure. Defined in BIP 141. **/
157+
static constexpr size_t MINIMUM_WITNESS_COMMITMENT{38};
158+
159+
/** Compute at which vout of the block's coinbase transaction the witness commitment occurs, or -1 if not found */
160+
inline int GetWitnessCommitmentIndex(const CBlock& block)
161+
{
162+
int commitpos = NO_WITNESS_COMMITMENT;
163+
if (!block.vtx.empty()) {
164+
for (size_t o = 0; o < block.vtx[0]->vout.size(); o++) {
165+
const CTxOut& vout = block.vtx[0]->vout[o];
166+
if (vout.scriptPubKey.size() >= MINIMUM_WITNESS_COMMITMENT &&
167+
vout.scriptPubKey[0] == OP_RETURN &&
168+
vout.scriptPubKey[1] == 0x24 &&
169+
vout.scriptPubKey[2] == 0xaa &&
170+
vout.scriptPubKey[3] == 0x21 &&
171+
vout.scriptPubKey[4] == 0xa9 &&
172+
vout.scriptPubKey[5] == 0xed) {
173+
commitpos = o;
174+
}
175+
}
176+
}
177+
return commitpos;
178+
}
179+
154180
#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)