aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorAva Chow <github@achow101.com>2024-05-15 15:09:56 -0400
committerAva Chow <github@achow101.com>2024-05-15 15:09:56 -0400
commit71f0f2273f6258e466c7b299c11982b4a04ae0d7 (patch)
treef658a4f7da5d2ddc82526ea79dab701340bc97eb /src/test
parent7a40f2a3f1cf744d136ecf534979114e79c5e71d (diff)
parent8d491ae9ecf1948ea29f67b50ca7259123f602aa (diff)
Merge bitcoin/bitcoin#28929: serialization: Support for multiple parameters
8d491ae9ecf1948ea29f67b50ca7259123f602aa serialization: Add ParamsStream GetStream() method (Ryan Ofsky) 951203bcc496c4415b7754cd764544659b76067f net: Simplify ParamsStream usage (Ryan Ofsky) e6794e475c84d9edca4a2876e2342cbb1d85f804 serialization: Accept multiple parameters in ParamsStream constructor (Ryan Ofsky) cb28849a88339c1e7ba03ffc7e38998339074e6e serialization: Reverse ParamsStream constructor order (Ryan Ofsky) 83436d14f06551026bcf5529df3b63b4e8a679fb serialization: Drop unnecessary ParamsStream references (Ryan Ofsky) 84502b755bcc35413ad466047893b5edf134c53f serialization: Drop references to GetVersion/GetType (Ryan Ofsky) f3a2b5237688e9f574444e793724664b00fb7f2a serialization: Support for multiple parameters (Ryan Ofsky) Pull request description: Currently it is only possible to attach one serialization parameter to a stream at a time. For example, it is not possible to set a parameter controlling the transaction format and a parameter controlling the address format at the same time because one parameter will override the other. This limitation is inconvenient for multiprocess code since it is not possible to create just one type of stream and serialize any object to it. Instead it is necessary to create different streams for different object types, which requires extra boilerplate and makes using the new parameter fields a lot more awkward than the older version and type fields. Fix this problem by allowing an unlimited number of serialization stream parameters to be set, and allowing them to be requested by type. Later parameters will still override earlier parameters, but only if they have the same type. For an example of different ways multiple parameters can be set, see the new [`with_params_multi`](https://github.com/bitcoin/bitcoin/blob/40f505583f4edeb2859aeb70da20c6374d331a4f/src/test/serialize_tests.cpp#L394-L410) unit test. This change requires replacing the `stream.GetParams()` method with a `stream.GetParams<T>()` method in order for serialization code to retrieve the desired parameters. The change is more verbose, but probably a good thing for readability because previously it could be difficult to know what type the `GetParams()` method would return, and now it is more obvious. --- This PR is part of the [process separation project](https://github.com/bitcoin/bitcoin/issues/28722). ACKs for top commit: maflcko: ACK 8d491ae9ecf1948ea29f67b50ca7259123f602aa 🔵 sipa: utACK 8d491ae9ecf1948ea29f67b50ca7259123f602aa TheCharlatan: ACK 8d491ae9ecf1948ea29f67b50ca7259123f602aa Tree-SHA512: 40b7041ee01c0372b1f86f7fd6f3b6af56ef24a6383f91ffcedd04d388e63407006457bf7ed056b0e37b4dec9ffd5ca006cb8192e488ea2c64678567e38d4647
Diffstat (limited to 'src/test')
-rw-r--r--src/test/serialize_tests.cpp89
1 files changed, 86 insertions, 3 deletions
diff --git a/src/test/serialize_tests.cpp b/src/test/serialize_tests.cpp
index d75eb499b4..b28e1b4196 100644
--- a/src/test/serialize_tests.cpp
+++ b/src/test/serialize_tests.cpp
@@ -15,6 +15,18 @@
BOOST_FIXTURE_TEST_SUITE(serialize_tests, BasicTestingSetup)
+// For testing move-semantics, declare a version of datastream that can be moved
+// but is not copyable.
+class UncopyableStream : public DataStream
+{
+public:
+ using DataStream::DataStream;
+ UncopyableStream(const UncopyableStream&) = delete;
+ UncopyableStream& operator=(const UncopyableStream&) = delete;
+ UncopyableStream(UncopyableStream&&) noexcept = default;
+ UncopyableStream& operator=(UncopyableStream&&) noexcept = default;
+};
+
class CSerializeMethodsTestSingle
{
protected:
@@ -289,7 +301,7 @@ public:
template <typename Stream>
void Serialize(Stream& s) const
{
- if (s.GetParams().m_base_format == BaseFormat::RAW) {
+ if (s.template GetParams<BaseFormat>().m_base_format == BaseFormat::RAW) {
s << m_base_data;
} else {
s << Span{HexStr(Span{&m_base_data, 1})};
@@ -299,7 +311,7 @@ public:
template <typename Stream>
void Unserialize(Stream& s)
{
- if (s.GetParams().m_base_format == BaseFormat::RAW) {
+ if (s.template GetParams<BaseFormat>().m_base_format == BaseFormat::RAW) {
s >> m_base_data;
} else {
std::string hex{"aa"};
@@ -327,8 +339,9 @@ class Derived : public Base
public:
std::string m_derived_data;
- SERIALIZE_METHODS_PARAMS(Derived, obj, DerivedAndBaseFormat, fmt)
+ SERIALIZE_METHODS(Derived, obj)
{
+ auto& fmt = SER_PARAMS(DerivedAndBaseFormat);
READWRITE(fmt.m_base_format(AsBase<Base>(obj)));
if (ser_action.ForRead()) {
@@ -343,6 +356,76 @@ public:
}
};
+struct OtherParam {
+ uint8_t param;
+ SER_PARAMS_OPFUNC
+};
+
+//! Checker for value of OtherParam. When being serialized, serializes the
+//! param to the stream. When being unserialized, verifies the value in the
+//! stream matches the param.
+class OtherParamChecker
+{
+public:
+ template <typename Stream>
+ void Serialize(Stream& s) const
+ {
+ const uint8_t param = s.template GetParams<OtherParam>().param;
+ s << param;
+ }
+
+ template <typename Stream>
+ void Unserialize(Stream& s) const
+ {
+ const uint8_t param = s.template GetParams<OtherParam>().param;
+ uint8_t value;
+ s >> value;
+ BOOST_CHECK_EQUAL(value, param);
+ }
+};
+
+//! Test creating a stream with multiple parameters and making sure
+//! serialization code requiring different parameters can retrieve them. Also
+//! test that earlier parameters take precedence if the same parameter type is
+//! specified twice. (Choice of whether earlier or later values take precedence
+//! or multiple values of the same type are allowed was arbitrary, and just
+//! decided based on what would require smallest amount of ugly C++ template
+//! code. Intent of the test is to just ensure there is no unexpected behavior.)
+BOOST_AUTO_TEST_CASE(with_params_multi)
+{
+ const OtherParam other_param_used{.param = 0x10};
+ const OtherParam other_param_ignored{.param = 0x11};
+ const OtherParam other_param_override{.param = 0x12};
+ const OtherParamChecker check;
+ DataStream stream;
+ ParamsStream pstream{stream, RAW, other_param_used, other_param_ignored};
+
+ Base base1{0x20};
+ pstream << base1 << check << other_param_override(check);
+ BOOST_CHECK_EQUAL(stream.str(), "\x20\x10\x12");
+
+ Base base2;
+ pstream >> base2 >> check >> other_param_override(check);
+ BOOST_CHECK_EQUAL(base2.m_base_data, 0x20);
+}
+
+//! Test creating a ParamsStream that moves from a stream argument.
+BOOST_AUTO_TEST_CASE(with_params_move)
+{
+ UncopyableStream stream{MakeByteSpan(std::string_view{"abc"})};
+ ParamsStream pstream{std::move(stream), RAW, HEX, RAW};
+ BOOST_CHECK_EQUAL(pstream.GetStream().str(), "abc");
+ pstream.GetStream().clear();
+
+ Base base1{0x20};
+ pstream << base1;
+ BOOST_CHECK_EQUAL(pstream.GetStream().str(), "\x20");
+
+ Base base2;
+ pstream >> base2;
+ BOOST_CHECK_EQUAL(base2.m_base_data, 0x20);
+}
+
BOOST_AUTO_TEST_CASE(with_params_base)
{
Base b{0x0F};