Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions src/bench/checkblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,38 @@
#include <optional>
#include <vector>

static void SizeComputerBlock(benchmark::Bench& bench) {
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8339284: bench: measure block (size)serialization speed

pending comment

CBlock block;
DataStream(benchmark::data::block413567) >> TX_WITH_WITNESS(block);
Copy link
Copy Markdown
Owner Author

@l0rinc l0rinc Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no header for some reason?!


bench.unit("block").run([&] {
SizeComputer size_computer;
size_computer << TX_WITH_WITNESS(block);
assert(size_computer.size() == benchmark::data::block413567.size());
});
}

static void SerializeBlock(benchmark::Bench& bench) {
CBlock block;
DataStream(benchmark::data::block413567) >> TX_WITH_WITNESS(block);

// Create output stream and verify first serialization matches input
bench.unit("block").run([&] {
DataStream output_stream(benchmark::data::block413567.size());
output_stream << TX_WITH_WITNESS(block);
assert(output_stream.size() == benchmark::data::block413567.size());
});
}

// These are the two major time-sinks which happen after we have fully received
// a block off the wire, but before we can relay the block on to peers using
// compact block relay.

static void DeserializeBlockTest(benchmark::Bench& bench)
static void DeserializeBlock(benchmark::Bench& bench)
{
DataStream stream(benchmark::data::block413567);
std::byte a{0};
stream.write({&a, 1}); // Prevent compaction
stream.write(std::span{&a, 1}); // Prevent compaction

bench.unit("block").run([&] {
CBlock block;
Expand All @@ -39,11 +62,11 @@ static void DeserializeBlockTest(benchmark::Bench& bench)
});
}

static void DeserializeAndCheckBlockTest(benchmark::Bench& bench)
static void DeserializeAndCheckBlock(benchmark::Bench& bench)
{
DataStream stream(benchmark::data::block413567);
std::byte a{0};
stream.write({&a, 1}); // Prevent compaction
stream.write(std::span{&a, 1}); // Prevent compaction

ArgsManager bench_args;
const auto chainParams = CreateChainParams(bench_args, ChainType::MAIN);
Expand All @@ -60,5 +83,7 @@ static void DeserializeAndCheckBlockTest(benchmark::Bench& bench)
});
}

BENCHMARK(DeserializeBlockTest);
BENCHMARK(DeserializeAndCheckBlockTest);
BENCHMARK(SizeComputerBlock);
BENCHMARK(SerializeBlock);
BENCHMARK(DeserializeBlock);
BENCHMARK(DeserializeAndCheckBlock);
2 changes: 1 addition & 1 deletion src/bench/rpc_blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct TestBlockAndIndex {
{
DataStream stream{benchmark::data::block413567};
std::byte a{0};
stream.write({&a, 1}); // Prevent compaction
stream.write(std::span{&a, 1}); // Prevent compaction

stream >> TX_WITH_WITNESS(block);

Expand Down
15 changes: 15 additions & 0 deletions src/crypto/sha256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,21 @@ CSHA256& CSHA256::Write(const unsigned char* data, size_t len)
}
return *this;
}
CSHA256& CSHA256::Write(unsigned char data)
{
size_t bufsize = bytes % 64;

// Add the single byte to the buffer
buf[bufsize] = data;
bytes += 1;

if (bufsize == 63) {
// Process the buffer if full
Transform(s, buf, 1);
}

return *this;
}

void CSHA256::Finalize(unsigned char hash[OUTPUT_SIZE])
{
Expand Down
1 change: 1 addition & 0 deletions src/crypto/sha256.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class CSHA256

CSHA256();
CSHA256& Write(const unsigned char* data, size_t len);
CSHA256& Write(unsigned char data);
void Finalize(unsigned char hash[OUTPUT_SIZE]);
CSHA256& Reset();
};
Expand Down
24 changes: 23 additions & 1 deletion src/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class CHash256 {
sha.Write(input.data(), input.size());
return *this;
}
CHash256& Write(std::span<const unsigned char, 1> input) {
sha.Write(input[0]);
return *this;
}

CHash256& Reset() {
sha.Reset();
Expand All @@ -63,6 +67,10 @@ class CHash160 {
sha.Write(input.data(), input.size());
return *this;
}
CHash160& Write(std::span<const unsigned char, 1> input) {
sha.Write(input[0]);
return *this;
}

CHash160& Reset() {
sha.Reset();
Expand Down Expand Up @@ -107,6 +115,10 @@ class HashWriter
{
ctx.Write(UCharCast(src.data()), src.size());
}
void write(std::span<const std::byte, 1> src)
{
ctx.Write(*UCharCast(&src[0]));
}

/** Compute the double-SHA256 hash of all data written to this object.
*
Expand Down Expand Up @@ -160,13 +172,18 @@ class HashVerifier : public HashWriter
m_source.read(dst);
this->write(dst);
}
void read(std::span<std::byte, 1> dst)
{
m_source.read(dst);
this->write(std::span<const std::byte, 1>{dst});
}

void ignore(size_t num_bytes)
{
std::byte data[1024];
while (num_bytes > 0) {
size_t now = std::min<size_t>(num_bytes, 1024);
read({data, now});
read(std::span{data, now});
num_bytes -= now;
}
}
Expand Down Expand Up @@ -194,6 +211,11 @@ class HashedSourceWriter : public HashWriter
m_source.write(src);
HashWriter::write(src);
}
void write(std::span<const std::byte, 1> src)
{
m_source.write(src);
HashWriter::write(src);
}

template <typename T>
HashedSourceWriter& operator<<(const T& obj)
Expand Down
Loading
Loading