aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorfanquake <fanquake@gmail.com>2020-04-27 11:48:30 +0800
committerfanquake <fanquake@gmail.com>2020-04-29 11:20:31 +0800
commit692f8307fc1449299b90182e7d79efb81a55d7ab (patch)
treeeaf24bf3f39a4d84774632eccfe5d82387e6e60f /src/test
parent06442549f8b725f46c1c727e9eb6fde6b843503c (diff)
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.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/validation_block_tests.cpp34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/test/validation_block_tests.cpp b/src/test/validation_block_tests.cpp
index b8af869b8e..c345f1eafb 100644
--- a/src/test/validation_block_tests.cpp
+++ b/src/test/validation_block_tests.cpp
@@ -340,4 +340,38 @@ BOOST_AUTO_TEST_CASE(mempool_locks_reorg)
rpc_thread.join();
}
}
+
+BOOST_AUTO_TEST_CASE(witness_commitment_index)
+{
+ CScript pubKey;
+ pubKey << 1 << OP_TRUE;
+ auto ptemplate = BlockAssembler(*m_node.mempool, Params()).CreateNewBlock(pubKey);
+ CBlock pblock = ptemplate->block;
+
+ CTxOut witness;
+ witness.scriptPubKey.resize(MINIMUM_WITNESS_COMMITMENT);
+ witness.scriptPubKey[0] = OP_RETURN;
+ witness.scriptPubKey[1] = 0x24;
+ witness.scriptPubKey[2] = 0xaa;
+ witness.scriptPubKey[3] = 0x21;
+ witness.scriptPubKey[4] = 0xa9;
+ witness.scriptPubKey[5] = 0xed;
+
+ // A witness larger than the minimum size is still valid
+ CTxOut min_plus_one = witness;
+ min_plus_one.scriptPubKey.resize(MINIMUM_WITNESS_COMMITMENT + 1);
+
+ CTxOut invalid = witness;
+ invalid.scriptPubKey[0] = OP_VERIFY;
+
+ CMutableTransaction txCoinbase(*pblock.vtx[0]);
+ txCoinbase.vout.resize(4);
+ txCoinbase.vout[0] = witness;
+ txCoinbase.vout[1] = witness;
+ txCoinbase.vout[2] = min_plus_one;
+ txCoinbase.vout[3] = invalid;
+ pblock.vtx[0] = MakeTransactionRef(std::move(txCoinbase));
+
+ BOOST_CHECK_EQUAL(GetWitnessCommitmentIndex(pblock), 2);
+}
BOOST_AUTO_TEST_SUITE_END()