Skip to content

Commit 692f830

Browse files
committed
test: add test for witness commitment index
As per BIP 141, if there is more than 1 pubkey that matches the witness commitment structure, the one with the highest output index should be chosen. This adds a sanity check that we are doing that, which will fail if anyone trys to "optimise" GetWitnessCommitmentIndex() be returning early.
1 parent 0644254 commit 692f830

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/test/validation_block_tests.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,4 +340,38 @@ BOOST_AUTO_TEST_CASE(mempool_locks_reorg)
340340
rpc_thread.join();
341341
}
342342
}
343+
344+
BOOST_AUTO_TEST_CASE(witness_commitment_index)
345+
{
346+
CScript pubKey;
347+
pubKey << 1 << OP_TRUE;
348+
auto ptemplate = BlockAssembler(*m_node.mempool, Params()).CreateNewBlock(pubKey);
349+
CBlock pblock = ptemplate->block;
350+
351+
CTxOut witness;
352+
witness.scriptPubKey.resize(MINIMUM_WITNESS_COMMITMENT);
353+
witness.scriptPubKey[0] = OP_RETURN;
354+
witness.scriptPubKey[1] = 0x24;
355+
witness.scriptPubKey[2] = 0xaa;
356+
witness.scriptPubKey[3] = 0x21;
357+
witness.scriptPubKey[4] = 0xa9;
358+
witness.scriptPubKey[5] = 0xed;
359+
360+
// A witness larger than the minimum size is still valid
361+
CTxOut min_plus_one = witness;
362+
min_plus_one.scriptPubKey.resize(MINIMUM_WITNESS_COMMITMENT + 1);
363+
364+
CTxOut invalid = witness;
365+
invalid.scriptPubKey[0] = OP_VERIFY;
366+
367+
CMutableTransaction txCoinbase(*pblock.vtx[0]);
368+
txCoinbase.vout.resize(4);
369+
txCoinbase.vout[0] = witness;
370+
txCoinbase.vout[1] = witness;
371+
txCoinbase.vout[2] = min_plus_one;
372+
txCoinbase.vout[3] = invalid;
373+
pblock.vtx[0] = MakeTransactionRef(std::move(txCoinbase));
374+
375+
BOOST_CHECK_EQUAL(GetWitnessCommitmentIndex(pblock), 2);
376+
}
343377
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)