Skip to content

Commit 6ecb9fc

Browse files
committed
chore: use std::vector<std::byte> for BlockManager::ReadRawBlock()
1 parent 19765dc commit 6ecb9fc

12 files changed

+20
-19
lines changed

src/bench/readwriteblock.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static void ReadRawBlockBench(benchmark::Bench& bench)
5555
const auto testing_setup{MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN)};
5656
auto& blockman{testing_setup->m_node.chainman->m_blockman};
5757
const auto pos{blockman.WriteBlock(CreateTestBlock(), 413'567)};
58-
std::vector<uint8_t> block_data;
58+
std::vector<std::byte> block_data;
5959
blockman.ReadRawBlock(block_data, pos); // warmup
6060
bench.run([&] {
6161
const auto success{blockman.ReadRawBlock(block_data, pos)};

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1677,7 +1677,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
16771677

16781678
#ifdef ENABLE_ZMQ
16791679
g_zmq_notification_interface = CZMQNotificationInterface::Create(
1680-
[&chainman = node.chainman](std::vector<uint8_t>& block, const CBlockIndex& index) {
1680+
[&chainman = node.chainman](std::vector<std::byte>& block, const CBlockIndex& index) {
16811681
assert(chainman);
16821682
return chainman->m_blockman.ReadRawBlock(block, WITH_LOCK(cs_main, return index.GetBlockPos()));
16831683
});

src/net_processing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2303,7 +2303,7 @@ void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv&
23032303
} else if (inv.IsMsgWitnessBlk()) {
23042304
// Fast-path: in this case it is possible to serve the block directly from disk,
23052305
// as the network format matches the format on disk
2306-
std::vector<uint8_t> block_data;
2306+
std::vector<std::byte> block_data;
23072307
if (!m_chainman.m_blockman.ReadRawBlock(block_data, block_pos)) {
23082308
if (WITH_LOCK(m_chainman.GetMutex(), return m_chainman.m_blockman.IsBlockPruned(*pindex))) {
23092309
LogDebug(BCLog::NET, "Block was pruned before it could be read, %s\n", pfrom.DisconnectMsg(fLogIPs));

src/node/blockstorage.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ bool BlockManager::ReadBlock(CBlock& block, const FlatFilePos& pos, const std::o
995995
block.SetNull();
996996

997997
// Open history file to read
998-
std::vector<uint8_t> block_data;
998+
std::vector<std::byte> block_data;
999999
if (!ReadRawBlock(block_data, pos)) {
10001000
return false;
10011001
}
@@ -1037,7 +1037,7 @@ bool BlockManager::ReadBlock(CBlock& block, const CBlockIndex& index) const
10371037
return ReadBlock(block, block_pos, index.GetBlockHash());
10381038
}
10391039

1040-
bool BlockManager::ReadRawBlock(std::vector<uint8_t>& block, const FlatFilePos& pos) const
1040+
bool BlockManager::ReadRawBlock(std::vector<std::byte>& block, const FlatFilePos& pos) const
10411041
{
10421042
if (pos.nPos < STORAGE_HEADER_BYTES) {
10431043
// If nPos is less than STORAGE_HEADER_BYTES, we can't read the header that precedes the block data
@@ -1071,7 +1071,7 @@ bool BlockManager::ReadRawBlock(std::vector<uint8_t>& block, const FlatFilePos&
10711071
}
10721072

10731073
block.resize(blk_size); // Zeroing of memory is intentional here
1074-
filein.read(MakeWritableByteSpan(block));
1074+
filein.read(block);
10751075
} catch (const std::exception& e) {
10761076
LogError("Read from block file failed: %s for %s while reading raw block", e.what(), pos.ToString());
10771077
return false;

src/node/blockstorage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ class BlockManager
413413
/** Functions for disk access for blocks */
414414
bool ReadBlock(CBlock& block, const FlatFilePos& pos, const std::optional<uint256>& expected_hash = {}) const;
415415
bool ReadBlock(CBlock& block, const CBlockIndex& index) const;
416-
bool ReadRawBlock(std::vector<uint8_t>& block, const FlatFilePos& pos) const;
416+
bool ReadRawBlock(std::vector<std::byte>& block, const FlatFilePos& pos) const;
417417

418418
bool ReadBlockUndo(CBlockUndo& blockundo, const CBlockIndex& index) const;
419419

src/rest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,15 +318,15 @@ static bool rest_block(const std::any& context,
318318
pos = pblockindex->GetBlockPos();
319319
}
320320

321-
std::vector<uint8_t> block_data{};
321+
std::vector<std::byte> block_data{};
322322
if (!chainman.m_blockman.ReadRawBlock(block_data, pos)) {
323323
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
324324
}
325325

326326
switch (rf) {
327327
case RESTResponseFormat::BINARY: {
328328
req->WriteHeader("Content-Type", "application/octet-stream");
329-
req->WriteReply(HTTP_OK, std::as_bytes(std::span{block_data}));
329+
req->WriteReply(HTTP_OK, block_data);
330330
return true;
331331
}
332332

src/rpc/blockchain.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -660,9 +660,9 @@ static CBlock GetBlockChecked(BlockManager& blockman, const CBlockIndex& blockin
660660
return block;
661661
}
662662

663-
static std::vector<uint8_t> GetRawBlockChecked(BlockManager& blockman, const CBlockIndex& blockindex)
663+
static std::vector<std::byte> GetRawBlockChecked(BlockManager& blockman, const CBlockIndex& blockindex)
664664
{
665-
std::vector<uint8_t> data{};
665+
std::vector<std::byte> data{};
666666
FlatFilePos pos{};
667667
{
668668
LOCK(cs_main);
@@ -812,7 +812,7 @@ static RPCHelpMan getblock()
812812
}
813813
}
814814

815-
const std::vector<uint8_t> block_data{GetRawBlockChecked(chainman.m_blockman, *pblockindex)};
815+
const std::vector<std::byte> block_data{GetRawBlockChecked(chainman.m_blockman, *pblockindex)};
816816

817817
if (verbosity <= 0) {
818818
return HexStr(block_data);

src/streams.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,14 @@ class VectorWriter
100100
class SpanReader
101101
{
102102
private:
103-
std::span<const unsigned char> m_data;
103+
std::span<const std::byte> m_data;
104104

105105
public:
106106
/**
107107
* @param[in] data Referenced byte vector to overwrite/append
108108
*/
109-
explicit SpanReader(std::span<const unsigned char> data) : m_data{data} {}
109+
explicit SpanReader(std::span<const unsigned char> data) : m_data{std::as_bytes(data)} {}
110+
explicit SpanReader(std::span<const std::byte> data) : m_data{data} {}
110111

111112
template<typename T>
112113
SpanReader& operator>>(T&& obj)

src/zmq/zmqnotificationinterface.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ std::list<const CZMQAbstractNotifier*> CZMQNotificationInterface::GetActiveNotif
4040
return result;
4141
}
4242

43-
std::unique_ptr<CZMQNotificationInterface> CZMQNotificationInterface::Create(std::function<bool(std::vector<uint8_t>&, const CBlockIndex&)> get_block_by_index)
43+
std::unique_ptr<CZMQNotificationInterface> CZMQNotificationInterface::Create(std::function<bool(std::vector<std::byte>&, const CBlockIndex&)> get_block_by_index)
4444
{
4545
std::map<std::string, CZMQNotifierFactory> factories;
4646
factories["pubhashblock"] = CZMQAbstractNotifier::Create<CZMQPublishHashBlockNotifier>;

src/zmq/zmqnotificationinterface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class CZMQNotificationInterface final : public CValidationInterface
2626

2727
std::list<const CZMQAbstractNotifier*> GetActiveNotifiers() const;
2828

29-
static std::unique_ptr<CZMQNotificationInterface> Create(std::function<bool(std::vector<uint8_t>&, const CBlockIndex&)> get_block_by_index);
29+
static std::unique_ptr<CZMQNotificationInterface> Create(std::function<bool(std::vector<std::byte>&, const CBlockIndex&)> get_block_by_index);
3030

3131
protected:
3232
bool Initialize();

0 commit comments

Comments
 (0)