Skip to content

Commit 2b26a87

Browse files
kwvgPastaPastaPasta
authored andcommitted
merge bitcoin#28012: Allow FastRandomContext::randbytes for std::byte, Allow std::byte serialization
1 parent 4eeafa2 commit 2b26a87

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
@@ -630,15 +630,18 @@ uint256 FastRandomContext::rand256() noexcept
630630
return ret;
631631
}
632632

633-
std::vector<unsigned char> FastRandomContext::randbytes(size_t len)
633+
template <typename B>
634+
std::vector<B> FastRandomContext::randbytes(size_t len)
634635
{
635636
if (requires_seed) RandomSeed();
636-
std::vector<unsigned char> ret(len);
637+
std::vector<B> ret(len);
637638
if (len > 0) {
638-
rng.Keystream(ret.data(), len);
639+
rng.Keystream(UCharCast(ret.data()), len);
639640
}
640641
return ret;
641642
}
643+
template std::vector<unsigned char> FastRandomContext::randbytes(size_t);
644+
template std::vector<std::byte> FastRandomContext::randbytes(size_t);
642645

643646
void FastRandomContext::fillrand(Span<std::byte> output)
644647
{

src/random.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ class FastRandomContext
200200
}
201201

202202
/** Generate random bytes. */
203-
std::vector<unsigned char> randbytes(size_t len);
203+
template <typename B = unsigned char>
204+
std::vector<B> randbytes(size_t len);
204205

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

src/serialize.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ template<typename X> const X& ReadWriteAsHelper(const X& x) { return x; }
196196
#ifndef CHAR_EQUALS_INT8
197197
template <typename Stream> void Serialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t
198198
#endif
199+
template <typename Stream> void Serialize(Stream& s, std::byte a) { ser_writedata8(s, uint8_t(a)); }
199200
template<typename Stream> inline void Serialize(Stream& s, int8_t a ) { ser_writedata8(s, a); }
200201
template<typename Stream> inline void Serialize(Stream& s, uint8_t a ) { ser_writedata8(s, a); }
201202
template<typename Stream> inline void Serialize(Stream& s, int16_t a ) { ser_writedata16(s, a); }
@@ -211,6 +212,7 @@ template <typename Stream, typename B> void Serialize(Stream& s, Span<B> span) {
211212
#ifndef CHAR_EQUALS_INT8
212213
template <typename Stream> void Unserialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t
213214
#endif
215+
template <typename Stream> void Unserialize(Stream& s, std::byte& a) { a = std::byte{ser_readdata8(s)}; }
214216
template<typename Stream> inline void Unserialize(Stream& s, int8_t& a ) { a = ser_readdata8(s); }
215217
template<typename Stream> inline void Unserialize(Stream& s, uint8_t& a ) { a = ser_readdata8(s); }
216218
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
@@ -295,11 +295,13 @@ BOOST_AUTO_TEST_CASE(class_methods)
295295
{
296296
DataStream ds;
297297
const std::string in{"ab"};
298-
ds << Span{in};
298+
ds << Span{in} << std::byte{'c'};
299299
std::array<std::byte, 2> out;
300-
ds >> Span{out};
300+
std::byte out_3;
301+
ds >> Span{out} >> out_3;
301302
BOOST_CHECK_EQUAL(out.at(0), std::byte{'a'});
302303
BOOST_CHECK_EQUAL(out.at(1), std::byte{'b'});
304+
BOOST_CHECK_EQUAL(out_3, std::byte{'c'});
303305
}
304306
}
305307

0 commit comments

Comments
 (0)