Skip to content

Commit 7952a59

Browse files
committed
Merge bitcoin/bitcoin#27927: util: Allow std::byte and char Span serialization
fa38d86 Use only Span{} constructor for byte-like types where possible (MarcoFalke) fa257bc util: Allow std::byte and char Span serialization (MarcoFalke) Pull request description: Seems odd to require developers to cast all byte-like spans passed to serialization to `unsigned char`-spans. Fix that by passing and accepting byte-like spans as-is. Finally, add tests and update the code to use just `Span` where possible. ACKs for top commit: sipa: utACK fa38d86 achow101: ACK fa38d86 ryanofsky: Code review ACK fa38d86. This looks great. The second commit really removes a lot of boilerplate and shows why the first commit is useful. Tree-SHA512: 788592d9ff515c3ebe73d48f9ecbb8d239f5b985af86f09974e508cafb0ca6d73a959350295246b4dfb496149bc56330a0b5d659fc434ba6723dbaba0b7a49e5
2 parents d6ee035 + fa38d86 commit 7952a59

File tree

11 files changed

+36
-26
lines changed

11 files changed

+36
-26
lines changed

src/bench/load_external.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static void LoadExternalBlockFile(benchmark::Bench& bench)
3434
ss << static_cast<uint32_t>(benchmark::data::block413567.size());
3535
// We can't use the streaming serialization (ss << benchmark::data::block413567)
3636
// because that first writes a compact size.
37-
ss.write(MakeByteSpan(benchmark::data::block413567));
37+
ss << Span{benchmark::data::block413567};
3838

3939
// Create the test file.
4040
{

src/net.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2933,13 +2933,13 @@ void CaptureMessageToFile(const CAddress& addr,
29332933
AutoFile f{fsbridge::fopen(path, "ab")};
29342934

29352935
ser_writedata64(f, now.count());
2936-
f.write(MakeByteSpan(msg_type));
2936+
f << Span{msg_type};
29372937
for (auto i = msg_type.length(); i < CMessageHeader::COMMAND_SIZE; ++i) {
29382938
f << uint8_t{'\0'};
29392939
}
29402940
uint32_t size = data.size();
29412941
ser_writedata32(f, size);
2942-
f.write(AsBytes(data));
2942+
f << data;
29432943
}
29442944

29452945
std::function<void(const CAddress& addr,

src/pubkey.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,14 @@ class CPubKey
142142
{
143143
unsigned int len = size();
144144
::WriteCompactSize(s, len);
145-
s.write(AsBytes(Span{vch, len}));
145+
s << Span{vch, len};
146146
}
147147
template <typename Stream>
148148
void Unserialize(Stream& s)
149149
{
150150
const unsigned int len(::ReadCompactSize(s));
151151
if (len <= SIZE) {
152-
s.read(AsWritableBytes(Span{vch, len}));
152+
s >> Span{vch, len};
153153
if (len != size()) {
154154
Invalidate();
155155
}

src/serialize.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ template<typename X> const X& ReadWriteAsHelper(const X& x) { return x; }
188188
} \
189189
FORMATTER_METHODS(cls, obj)
190190

191+
// clang-format off
191192
#ifndef CHAR_EQUALS_INT8
192193
template <typename Stream> void Serialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t
193194
#endif
@@ -201,8 +202,7 @@ template<typename Stream> inline void Serialize(Stream& s, int64_t a ) { ser_wri
201202
template<typename Stream> inline void Serialize(Stream& s, uint64_t a) { ser_writedata64(s, a); }
202203
template<typename Stream, int N> inline void Serialize(Stream& s, const char (&a)[N]) { s.write(MakeByteSpan(a)); }
203204
template<typename Stream, int N> inline void Serialize(Stream& s, const unsigned char (&a)[N]) { s.write(MakeByteSpan(a)); }
204-
template<typename Stream> inline void Serialize(Stream& s, const Span<const unsigned char>& span) { s.write(AsBytes(span)); }
205-
template<typename Stream> inline void Serialize(Stream& s, const Span<unsigned char>& span) { s.write(AsBytes(span)); }
205+
template <typename Stream, typename B> void Serialize(Stream& s, Span<B> span) { (void)/* force byte-type */UCharCast(span.data()); s.write(AsBytes(span)); }
206206

207207
#ifndef CHAR_EQUALS_INT8
208208
template <typename Stream> void Unserialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t
@@ -217,10 +217,11 @@ template<typename Stream> inline void Unserialize(Stream& s, int64_t& a ) { a =
217217
template<typename Stream> inline void Unserialize(Stream& s, uint64_t& a) { a = ser_readdata64(s); }
218218
template<typename Stream, int N> inline void Unserialize(Stream& s, char (&a)[N]) { s.read(MakeWritableByteSpan(a)); }
219219
template<typename Stream, int N> inline void Unserialize(Stream& s, unsigned char (&a)[N]) { s.read(MakeWritableByteSpan(a)); }
220-
template<typename Stream> inline void Unserialize(Stream& s, Span<unsigned char>& span) { s.read(AsWritableBytes(span)); }
220+
template <typename Stream, typename B> void Unserialize(Stream& s, Span<B> span) { (void)/* force byte-type */UCharCast(span.data()); s.read(AsWritableBytes(span)); }
221221

222222
template <typename Stream> inline void Serialize(Stream& s, bool a) { uint8_t f = a; ser_writedata8(s, f); }
223223
template <typename Stream> inline void Unserialize(Stream& s, bool& a) { uint8_t f = ser_readdata8(s); a = f; }
224+
// clang-format on
224225

225226

226227
/**

src/test/fuzz/p2p_transport_serialization.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ FUZZ_TARGET_INIT(p2p_transport_serialization, initialize_p2p_transport_serializa
7777
assert(msg.m_time == m_time);
7878

7979
std::vector<unsigned char> header;
80-
auto msg2 = CNetMsgMaker{msg.m_recv.GetVersion()}.Make(msg.m_type, MakeUCharSpan(msg.m_recv));
80+
auto msg2 = CNetMsgMaker{msg.m_recv.GetVersion()}.Make(msg.m_type, Span{msg.m_recv});
8181
serializer.prepareForTransport(msg2, header);
8282
}
8383
}

src/test/serialize_tests.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,32 +186,32 @@ BOOST_AUTO_TEST_CASE(noncanonical)
186186
std::vector<char>::size_type n;
187187

188188
// zero encoded with three bytes:
189-
ss.write(MakeByteSpan("\xfd\x00\x00").first(3));
189+
ss << Span{"\xfd\x00\x00"}.first(3);
190190
BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
191191

192192
// 0xfc encoded with three bytes:
193-
ss.write(MakeByteSpan("\xfd\xfc\x00").first(3));
193+
ss << Span{"\xfd\xfc\x00"}.first(3);
194194
BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
195195

196196
// 0xfd encoded with three bytes is OK:
197-
ss.write(MakeByteSpan("\xfd\xfd\x00").first(3));
197+
ss << Span{"\xfd\xfd\x00"}.first(3);
198198
n = ReadCompactSize(ss);
199199
BOOST_CHECK(n == 0xfd);
200200

201201
// zero encoded with five bytes:
202-
ss.write(MakeByteSpan("\xfe\x00\x00\x00\x00").first(5));
202+
ss << Span{"\xfe\x00\x00\x00\x00"}.first(5);
203203
BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
204204

205205
// 0xffff encoded with five bytes:
206-
ss.write(MakeByteSpan("\xfe\xff\xff\x00\x00").first(5));
206+
ss << Span{"\xfe\xff\xff\x00\x00"}.first(5);
207207
BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
208208

209209
// zero encoded with nine bytes:
210-
ss.write(MakeByteSpan("\xff\x00\x00\x00\x00\x00\x00\x00\x00").first(9));
210+
ss << Span{"\xff\x00\x00\x00\x00\x00\x00\x00\x00"}.first(9);
211211
BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
212212

213213
// 0x01ffffff encoded with nine bytes:
214-
ss.write(MakeByteSpan("\xff\xff\xff\xff\x01\x00\x00\x00\x00").first(9));
214+
ss << Span{"\xff\xff\xff\xff\x01\x00\x00\x00\x00"}.first(9);
215215
BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
216216
}
217217

@@ -241,6 +241,15 @@ BOOST_AUTO_TEST_CASE(class_methods)
241241
ss2 << intval << boolval << stringval << charstrval << txval;
242242
ss2 >> methodtest3;
243243
BOOST_CHECK(methodtest3 == methodtest4);
244+
{
245+
DataStream ds;
246+
const std::string in{"ab"};
247+
ds << Span{in};
248+
std::array<std::byte, 2> out;
249+
ds >> Span{out};
250+
BOOST_CHECK_EQUAL(out.at(0), std::byte{'a'});
251+
BOOST_CHECK_EQUAL(out.at(1), std::byte{'b'});
252+
}
244253
}
245254

246255
BOOST_AUTO_TEST_SUITE_END()

src/uint256.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class base_blob
7878
template<typename Stream>
7979
void Serialize(Stream& s) const
8080
{
81-
s.write(MakeByteSpan(m_data));
81+
s << Span(m_data);
8282
}
8383

8484
template<typename Stream>

src/wallet/dump.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ bool DumpWallet(const ArgsManager& args, CWallet& wallet, bilingual_str& error)
5757
// Write out a magic string with version
5858
std::string line = strprintf("%s,%u\n", DUMP_MAGIC, DUMP_VERSION);
5959
dump_file.write(line.data(), line.size());
60-
hasher.write(MakeByteSpan(line));
60+
hasher << Span{line};
6161

6262
// Write out the file format
6363
line = strprintf("%s,%s\n", "format", db.Format());
6464
dump_file.write(line.data(), line.size());
65-
hasher.write(MakeByteSpan(line));
65+
hasher << Span{line};
6666

6767
if (ret) {
6868

@@ -83,7 +83,7 @@ bool DumpWallet(const ArgsManager& args, CWallet& wallet, bilingual_str& error)
8383
std::string value_str = HexStr(ss_value);
8484
line = strprintf("%s,%s\n", key_str, value_str);
8585
dump_file.write(line.data(), line.size());
86-
hasher.write(MakeByteSpan(line));
86+
hasher << Span{line};
8787
}
8888
}
8989

@@ -160,7 +160,7 @@ bool CreateFromDump(const ArgsManager& args, const std::string& name, const fs::
160160
return false;
161161
}
162162
std::string magic_hasher_line = strprintf("%s,%s\n", magic_key, version_value);
163-
hasher.write(MakeByteSpan(magic_hasher_line));
163+
hasher << Span{magic_hasher_line};
164164

165165
// Get the stored file format
166166
std::string format_key;
@@ -191,7 +191,7 @@ bool CreateFromDump(const ArgsManager& args, const std::string& name, const fs::
191191
warnings.push_back(strprintf(_("Warning: Dumpfile wallet format \"%s\" does not match command line specified format \"%s\"."), format_value, file_format));
192192
}
193193
std::string format_hasher_line = strprintf("%s,%s\n", format_key, format_value);
194-
hasher.write(MakeByteSpan(format_hasher_line));
194+
hasher << Span{format_hasher_line};
195195

196196
DatabaseOptions options;
197197
DatabaseStatus status;
@@ -236,7 +236,7 @@ bool CreateFromDump(const ArgsManager& args, const std::string& name, const fs::
236236
}
237237

238238
std::string line = strprintf("%s,%s\n", key, value);
239-
hasher.write(MakeByteSpan(line));
239+
hasher << Span{line};
240240

241241
if (key.empty() || value.empty()) {
242242
continue;

src/wallet/test/db_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ BOOST_AUTO_TEST_CASE(db_cursor_prefix_byte_test)
196196
for (const auto& database : TestDatabases(m_path_root)) {
197197
std::unique_ptr<DatabaseBatch> batch = database->MakeBatch();
198198
for (const auto& [k, v] : {e, p, ps, f, fs, ff, ffs}) {
199-
batch->Write(MakeUCharSpan(k), MakeUCharSpan(v));
199+
batch->Write(Span{k}, Span{v});
200200
}
201201
CheckPrefix(*batch, StringBytes(""), {e, p, ps, f, fs, ff, ffs});
202202
CheckPrefix(*batch, StringBytes("prefix"), {p, ps});

src/wallet/wallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3878,7 +3878,7 @@ bool CWallet::MigrateToSQLite(bilingual_str& error)
38783878
bool began = batch->TxnBegin();
38793879
assert(began); // This is a critical error, the new db could not be written to. The original db exists as a backup, but we should not continue execution.
38803880
for (const auto& [key, value] : records) {
3881-
if (!batch->Write(MakeUCharSpan(key), MakeUCharSpan(value))) {
3881+
if (!batch->Write(Span{key}, Span{value})) {
38823882
batch->TxnAbort();
38833883
m_database->Close();
38843884
fs::remove(m_database->Filename());

0 commit comments

Comments
 (0)