49c0f21971050b24cecde56bdb42653af3910fc6
A couple suggestions for tests here. 3 to 5 compactsize boundary which IIUC is consensus legal in a block, and every way of making an empty witness giving the same object, since HasWitness() and so the witness flag and wtxid depend on it.
diff --git a/src/test/script_tests.cpp b/src/test/script_tests.cpp
index cc6fa3843a..3b46f468d4 100644
--- a/src/test/script_tests.cpp
+++ b/src/test/script_tests.cpp
@@ -1774,26 +1774,110 @@ BOOST_AUTO_TEST_CASE(script_witness_representation)
if (!stack.empty()) BOOST_CHECK(std::ranges::equal(deser.back(), stack.back()));
// Moving leaves the source empty.
CScriptWitness moved{std::move(deser)};
BOOST_CHECK(moved == witness);
BOOST_CHECK(deser.empty() && deser.size() == 0 && deser.begin() == deser.end()); // NOLINT(bugprone-use-after-move)
// Copy and move assignment replace the stack that was there.
CScriptWitness copy{witness};
CScriptWitness assigned{{{0xff}}};
assigned = copy;
copy = {};
BOOST_CHECK(copy.empty());
BOOST_CHECK(copy.begin() == copy.end());
BOOST_CHECK(assigned == witness);
CScriptWitness move_assigned{{{0xff}}};
move_assigned = std::move(assigned);
BOOST_CHECK(move_assigned == witness);
BOOST_CHECK(assigned.empty() && assigned.begin() == assigned.end()); // NOLINT(bugprone-use-after-move)
if (!stack.empty()) BOOST_CHECK(std::ranges::equal(move_assigned.back(), stack.back()));
}
}
}
+BOOST_AUTO_TEST_CASE(script_witness_large_count)
+{
+ // Element counts on both sides of the boundary between the 3-byte and 5-byte CompactSize encodings. Elements are
+ // small, and only a few are accessed by index, as indexing may take time linear in the index.
+ for (const size_t count : {size_t{0xffff}, size_t{0x10000}}) {
+ std::vector<std::vector<unsigned char>> stack(count);
+ for (auto& element : stack) element = m_rng.randbytes(m_rng.randrange(3));
+ const CScriptWitness witness{stack};
+
+ BOOST_CHECK_EQUAL(witness.size(), count);
+ BOOST_CHECK_EQUAL(std::distance(witness.begin(), witness.end()), (std::ptrdiff_t)count);
+ BOOST_CHECK(witness.ToVectors() == stack);
+ BOOST_CHECK(std::ranges::equal(witness.front(), stack.front()));
+ BOOST_CHECK(std::ranges::equal(witness.back(), stack.back()));
+ for (const size_t i : {size_t{1}, count / 2, count - 2}) BOOST_CHECK(std::ranges::equal(witness[i], stack[i]));
+
+ std::vector<unsigned char> ser_vec, ser_wit;
+ VectorWriter{ser_vec, 0, stack};
+ VectorWriter{ser_wit, 0, witness};
+ BOOST_CHECK(ser_vec == ser_wit);
+ // The count is encoded as 0xfd followed by 2 bytes, or 0xfe followed by 4.
+ BOOST_CHECK_EQUAL(int{ser_wit.at(0)}, count <= 0xffff ? 0xfd : 0xfe);
+ CScriptWitness deser;
+ SpanReader{ser_vec} >> deser;
+ BOOST_CHECK(deser == witness);
+ BOOST_CHECK_EQUAL(deser.size(), count);
+ BOOST_CHECK(std::ranges::equal(deser.back(), stack.back()));
+ }
+}
+
+BOOST_AUTO_TEST_CASE(script_witness_empty)
+{
+ // Every way of producing a witness without elements gives the same witness. A transaction is serialized with
+ // witness data exactly when one of its inputs has a non-empty witness, so this determines its wtxid and whether its
+ // serialization round-trips.
+ using Stack = std::vector<std::vector<unsigned char>>;
+ const std::vector<unsigned char> ser_empty{0x00};
+ const CScriptWitness from_default;
+ const CScriptWitness from_vector{Stack{}};
+ CScriptWitness from_deser{{{0x01}}};
+ SpanReader{ser_empty} >> from_deser;
+ CScriptWitness moved_from{{{0x01}}};
+ const CScriptWitness moved_to{std::move(moved_from)};
+ BOOST_CHECK_EQUAL(moved_to.size(), 1U);
+ CScriptWitness reset{{{0x01}}};
+ reset = {};
+ const CScriptWitness copied{from_default};
+ CScriptWitness copy_assigned{{{0x01}}};
+ copy_assigned = from_default;
+
+ const std::vector<const CScriptWitness*> empties{&from_default, &from_vector, &from_deser, &moved_from, &reset, &copied, ©_assigned}; // NOLINT(bugprone-use-after-move)
+ for (const CScriptWitness* witness : empties) {
+ BOOST_CHECK(witness->empty());
+ BOOST_CHECK_EQUAL(witness->size(), 0U);
+ BOOST_CHECK(witness->begin() == witness->end());
+ BOOST_CHECK(witness->ToVectors().empty());
+ BOOST_CHECK(*witness == from_default);
+ std::vector<unsigned char> ser;
+ VectorWriter{ser, 0, *witness};
+ BOOST_CHECK(ser == ser_empty);
+ CMutableTransaction tx;
+ tx.vin.resize(1);
+ tx.vin[0].scriptWitness = *witness;
+ BOOST_CHECK(!tx.HasWitness());
+ }
+
+ // A single empty element is a different stack.
+ const CScriptWitness one_empty{Stack(1)};
+ BOOST_CHECK(!one_empty.empty());
+ BOOST_CHECK_EQUAL(one_empty.size(), 1U);
+ BOOST_CHECK(one_empty.back().empty());
+ BOOST_CHECK(one_empty != from_default);
+ std::vector<unsigned char> ser;
+ VectorWriter{ser, 0, one_empty};
+ BOOST_CHECK(ser == (std::vector<unsigned char>{0x01, 0x00}));
+ CScriptWitness deser;
+ SpanReader{ser} >> deser;
+ BOOST_CHECK(deser == one_empty);
+ CMutableTransaction tx;
+ tx.vin.resize(1);
+ tx.vin[0].scriptWitness = one_empty;
+ BOOST_CHECK(tx.HasWitness());
+}
+
BOOST_AUTO_TEST_SUITE_END()