aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfanquake <fanquake@gmail.com>2023-07-07 10:14:54 +0100
committerfanquake <fanquake@gmail.com>2023-07-07 10:48:09 +0100
commita9dde841e159296f311d50b1a7fefd67da3914a4 (patch)
tree79856551f478a5f4fa76bd8644220c68be44f1db
parent299f17a69e1b64ebbe0f10c4d833ee14a5fb821a (diff)
parentfac6af16f4a254458b8cb3522317422b40362f8d (diff)
Merge bitcoin/bitcoin#28012: util: Allow FastRandomContext::randbytes for std::byte, Allow std::byte serialization
fac6af16f4a254458b8cb3522317422b40362f8d Allow std::byte serialization (MarcoFalke) fade43edc4405e7c51cec9325d8502f3786f7438 Allow FastRandomContext::randbytes for all byte types (MarcoFalke) Pull request description: I need this for some stuff, but it should also be useful by itself for other developers that need it. ACKs for top commit: sipa: utACK fac6af16f4a254458b8cb3522317422b40362f8d dergoegge: Code review ACK fac6af16f4a254458b8cb3522317422b40362f8d Tree-SHA512: db4b1bbd6bf6ef6503d59b0b4ed1681db8d935d2d10f8d89f071978ea59b49a1d319bccb4e9717c0c88a4908bbeca4fd0cbff6c655d8a443554fd14146fe16de
-rw-r--r--src/random.cpp9
-rw-r--r--src/random.h3
-rw-r--r--src/serialize.h2
-rw-r--r--src/test/serialize_tests.cpp6
4 files changed, 14 insertions, 6 deletions
diff --git a/src/random.cpp b/src/random.cpp
index 39ceae4206..5ff6f573b8 100644
--- a/src/random.cpp
+++ b/src/random.cpp
@@ -589,15 +589,18 @@ uint256 FastRandomContext::rand256() noexcept
return ret;
}
-std::vector<unsigned char> FastRandomContext::randbytes(size_t len)
+template <typename B>
+std::vector<B> FastRandomContext::randbytes(size_t len)
{
if (requires_seed) RandomSeed();
- std::vector<unsigned char> ret(len);
+ std::vector<B> ret(len);
if (len > 0) {
- rng.Keystream(ret.data(), len);
+ rng.Keystream(UCharCast(ret.data()), len);
}
return ret;
}
+template std::vector<unsigned char> FastRandomContext::randbytes(size_t);
+template std::vector<std::byte> FastRandomContext::randbytes(size_t);
void FastRandomContext::fillrand(Span<std::byte> output)
{
diff --git a/src/random.h b/src/random.h
index 50f56ed911..3b15477ae9 100644
--- a/src/random.h
+++ b/src/random.h
@@ -211,7 +211,8 @@ public:
}
/** Generate random bytes. */
- std::vector<unsigned char> randbytes(size_t len);
+ template <typename B = unsigned char>
+ std::vector<B> randbytes(size_t len);
/** Fill a byte Span with random bytes. */
void fillrand(Span<std::byte> output);
diff --git a/src/serialize.h b/src/serialize.h
index cf865eb3f4..0cda0ac7d5 100644
--- a/src/serialize.h
+++ b/src/serialize.h
@@ -192,6 +192,7 @@ template<typename X> const X& ReadWriteAsHelper(const X& x) { return x; }
#ifndef CHAR_EQUALS_INT8
template <typename Stream> void Serialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t
#endif
+template <typename Stream> void Serialize(Stream& s, std::byte a) { ser_writedata8(s, uint8_t(a)); }
template<typename Stream> inline void Serialize(Stream& s, int8_t a ) { ser_writedata8(s, a); }
template<typename Stream> inline void Serialize(Stream& s, uint8_t a ) { ser_writedata8(s, a); }
template<typename Stream> inline void Serialize(Stream& s, int16_t a ) { ser_writedata16(s, a); }
@@ -207,6 +208,7 @@ template <typename Stream, typename B> void Serialize(Stream& s, Span<B> span) {
#ifndef CHAR_EQUALS_INT8
template <typename Stream> void Unserialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t
#endif
+template <typename Stream> void Unserialize(Stream& s, std::byte& a) { a = std::byte{ser_readdata8(s)}; }
template<typename Stream> inline void Unserialize(Stream& s, int8_t& a ) { a = ser_readdata8(s); }
template<typename Stream> inline void Unserialize(Stream& s, uint8_t& a ) { a = ser_readdata8(s); }
template<typename Stream> inline void Unserialize(Stream& s, int16_t& a ) { a = ser_readdata16(s); }
diff --git a/src/test/serialize_tests.cpp b/src/test/serialize_tests.cpp
index b445ff8ffc..2e862621bc 100644
--- a/src/test/serialize_tests.cpp
+++ b/src/test/serialize_tests.cpp
@@ -244,11 +244,13 @@ BOOST_AUTO_TEST_CASE(class_methods)
{
DataStream ds;
const std::string in{"ab"};
- ds << Span{in};
+ ds << Span{in} << std::byte{'c'};
std::array<std::byte, 2> out;
- ds >> Span{out};
+ std::byte out_3;
+ ds >> Span{out} >> out_3;
BOOST_CHECK_EQUAL(out.at(0), std::byte{'a'});
BOOST_CHECK_EQUAL(out.at(1), std::byte{'b'});
+ BOOST_CHECK_EQUAL(out_3, std::byte{'c'});
}
}