Skip to content

Commit 0644254

Browse files
committed
validation: Add minimum witness commitment size constant
Per BIP 141, the witness commitment structure is atleast 38 bytes, OP_RETURN (0x6a) + 36 (0x24) + 4 byte header (0xaa21a9ed) + 32 byte SHA256 hash. It can be longer, however any additional data has no consensus meaning.
1 parent ba348db commit 0644254

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/validation.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3386,7 +3386,14 @@ int GetWitnessCommitmentIndex(const CBlock& block)
33863386
int commitpos = -1;
33873387
if (!block.vtx.empty()) {
33883388
for (size_t o = 0; o < block.vtx[0]->vout.size(); o++) {
3389-
if (block.vtx[0]->vout[o].scriptPubKey.size() >= 38 && block.vtx[0]->vout[o].scriptPubKey[0] == OP_RETURN && block.vtx[0]->vout[o].scriptPubKey[1] == 0x24 && block.vtx[0]->vout[o].scriptPubKey[2] == 0xaa && block.vtx[0]->vout[o].scriptPubKey[3] == 0x21 && block.vtx[0]->vout[o].scriptPubKey[4] == 0xa9 && block.vtx[0]->vout[o].scriptPubKey[5] == 0xed) {
3389+
const CTxOut& vout = block.vtx[0]->vout[o];
3390+
if (vout.scriptPubKey.size() >= MINIMUM_WITNESS_COMMITMENT &&
3391+
vout.scriptPubKey[0] == OP_RETURN &&
3392+
vout.scriptPubKey[1] == 0x24 &&
3393+
vout.scriptPubKey[2] == 0xaa &&
3394+
vout.scriptPubKey[3] == 0x21 &&
3395+
vout.scriptPubKey[4] == 0xa9 &&
3396+
vout.scriptPubKey[5] == 0xed) {
33903397
commitpos = o;
33913398
}
33923399
}
@@ -3417,7 +3424,7 @@ std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBloc
34173424
CHash256().Write(witnessroot.begin(), 32).Write(ret.data(), 32).Finalize(witnessroot.begin());
34183425
CTxOut out;
34193426
out.nValue = 0;
3420-
out.scriptPubKey.resize(38);
3427+
out.scriptPubKey.resize(MINIMUM_WITNESS_COMMITMENT);
34213428
out.scriptPubKey[0] = OP_RETURN;
34223429
out.scriptPubKey[1] = 0x24;
34233430
out.scriptPubKey[2] = 0xaa;

src/validation.h

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

9698
struct BlockHasher
9799
{

0 commit comments

Comments
 (0)