Skip to content

Commit a9dde84

Browse files
committed
Merge bitcoin/bitcoin#28012: util: Allow FastRandomContext::randbytes for std::byte, Allow std::byte serialization
fac6af1 Allow std::byte serialization (MarcoFalke) fade43e 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 fac6af1 dergoegge: Code review ACK fac6af1 Tree-SHA512: db4b1bbd6bf6ef6503d59b0b4ed1681db8d935d2d10f8d89f071978ea59b49a1d319bccb4e9717c0c88a4908bbeca4fd0cbff6c655d8a443554fd14146fe16de
2 parents 299f17a + fac6af1 commit a9dde84

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

src/random.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -589,15 +589,18 @@ uint256 FastRandomContext::rand256() noexcept
589589
return ret;
590590
}
591591

592-
std::vector<unsigned char> FastRandomContext::randbytes(size_t len)
592+
template <typename B>
593+
std::vector<B> FastRandomContext::randbytes(size_t len)
593594
{
594595
if (requires_seed) RandomSeed();
595-
std::vector<unsigned char> ret(len);
596+
std::vector<B> ret(len);
596597
if (len > 0) {
597-
rng.Keystream(ret.data(), len);
598+
rng.Keystream(UCharCast(ret.data()), len);
598599
}
599600
return ret;
600601
}
602+
template std::vector<unsigned char> FastRandomContext::randbytes(size_t);
603+
template std::vector<std::byte> FastRandomContext::randbytes(size_t);
601604

602605
void FastRandomContext::fillrand(Span<std::byte> output)
603606
{

src/random.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ class FastRandomContext
211211
}
212212

213213
/** Generate random bytes. */
214-
std::vector<unsigned char> randbytes(size_t len);
214+
template <typename B = unsigned char>
215+
std::vector<B> randbytes(size_t len);
215216

216217
/** Fill a byte Span with random bytes. */
217218
void fillrand(Span<std::byte> output);

src/serialize.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ template<typename X> const X& ReadWriteAsHelper(const X& x) { return x; }
192192
#ifndef CHAR_EQUALS_INT8
193193
template <typename Stream> void Serialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t
194194
#endif
195+
template <typename Stream> void Serialize(Stream& s, std::byte a) { ser_writedata8(s, uint8_t(a)); }
195196
template<typename Stream> inline void Serialize(Stream& s, int8_t a ) { ser_writedata8(s, a); }
196197
template<typename Stream> inline void Serialize(Stream& s, uint8_t a ) { ser_writedata8(s, a); }
197198
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) {
207208
#ifndef CHAR_EQUALS_INT8
208209
template <typename Stream> void Unserialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t
209210
#endif
211+
template <typename Stream> void Unserialize(Stream& s, std::byte& a) { a = std::byte{ser_readdata8(s)}; }
210212
template<typename Stream> inline void Unserialize(Stream& s, int8_t& a ) { a = ser_readdata8(s); }
211213
template<typename Stream> inline void Unserialize(Stream& s, uint8_t& a ) { a = ser_readdata8(s); }
212214
template<typename Stream> inline void Unserialize(Stream& s, int16_t& a ) { a = ser_readdata16(s); }

src/test/serialize_tests.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,13 @@ BOOST_AUTO_TEST_CASE(class_methods)
244244
{
245245
DataStream ds;
246246
const std::string in{"ab"};
247-
ds << Span{in};
247+
ds << Span{in} << std::byte{'c'};
248248
std::array<std::byte, 2> out;
249-
ds >> Span{out};
249+
std::byte out_3;
250+
ds >> Span{out} >> out_3;
250251
BOOST_CHECK_EQUAL(out.at(0), std::byte{'a'});
251252
BOOST_CHECK_EQUAL(out.at(1), std::byte{'b'});
253+
BOOST_CHECK_EQUAL(out_3, std::byte{'c'});
252254
}
253255
}
254256

0 commit comments

Comments
 (0)