chore: use std::vector<std::byte> for BlockManager::ReadRawBlock()
This commit is contained in:
12 files changed
+20
-19
No files matched your search
@@ -55,7 +55,7 @@ static void ReadRawBlockBench(benchmark::Bench& bench)
|
|||||||
const auto testing_setup{MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN)};
|
const auto testing_setup{MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN)};
|
||||||
auto& blockman{testing_setup->m_node.chainman->m_blockman};
|
auto& blockman{testing_setup->m_node.chainman->m_blockman};
|
||||||
const auto pos{blockman.WriteBlock(CreateTestBlock(), 413'567)};
|
const auto pos{blockman.WriteBlock(CreateTestBlock(), 413'567)};
|
||||||
std::vector<uint8_t> block_data;
|
std::vector<std::byte> block_data;
|
||||||
blockman.ReadRawBlock(block_data, pos); // warmup
|
blockman.ReadRawBlock(block_data, pos); // warmup
|
||||||
bench.run([&] {
|
bench.run([&] {
|
||||||
const auto success{blockman.ReadRawBlock(block_data, pos)};
|
const auto success{blockman.ReadRawBlock(block_data, pos)};
|
||||||
|
|||||||
+1
-1
@@ -1677,7 +1677,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||||||
|
|
||||||
#ifdef ENABLE_ZMQ
|
#ifdef ENABLE_ZMQ
|
||||||
g_zmq_notification_interface = CZMQNotificationInterface::Create(
|
g_zmq_notification_interface = CZMQNotificationInterface::Create(
|
||||||
[&chainman = node.chainman](std::vector<uint8_t>& block, const CBlockIndex& index) {
|
[&chainman = node.chainman](std::vector<std::byte>& block, const CBlockIndex& index) {
|
||||||
assert(chainman);
|
assert(chainman);
|
||||||
return chainman->m_blockman.ReadRawBlock(block, WITH_LOCK(cs_main, return index.GetBlockPos()));
|
return chainman->m_blockman.ReadRawBlock(block, WITH_LOCK(cs_main, return index.GetBlockPos()));
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -2303,7 +2303,7 @@ void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv&
|
|||||||
} else if (inv.IsMsgWitnessBlk()) {
|
} else if (inv.IsMsgWitnessBlk()) {
|
||||||
// Fast-path: in this case it is possible to serve the block directly from disk,
|
// Fast-path: in this case it is possible to serve the block directly from disk,
|
||||||
// as the network format matches the format on disk
|
// as the network format matches the format on disk
|
||||||
std::vector<uint8_t> block_data;
|
std::vector<std::byte> block_data;
|
||||||
if (!m_chainman.m_blockman.ReadRawBlock(block_data, block_pos)) {
|
if (!m_chainman.m_blockman.ReadRawBlock(block_data, block_pos)) {
|
||||||
if (WITH_LOCK(m_chainman.GetMutex(), return m_chainman.m_blockman.IsBlockPruned(*pindex))) {
|
if (WITH_LOCK(m_chainman.GetMutex(), return m_chainman.m_blockman.IsBlockPruned(*pindex))) {
|
||||||
LogDebug(BCLog::NET, "Block was pruned before it could be read, %s\n", pfrom.DisconnectMsg(fLogIPs));
|
LogDebug(BCLog::NET, "Block was pruned before it could be read, %s\n", pfrom.DisconnectMsg(fLogIPs));
|
||||||
|
|||||||
@@ -995,7 +995,7 @@ bool BlockManager::ReadBlock(CBlock& block, const FlatFilePos& pos, const std::o
|
|||||||
block.SetNull();
|
block.SetNull();
|
||||||
|
|
||||||
// Open history file to read
|
// Open history file to read
|
||||||
std::vector<uint8_t> block_data;
|
std::vector<std::byte> block_data;
|
||||||
if (!ReadRawBlock(block_data, pos)) {
|
if (!ReadRawBlock(block_data, pos)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -1037,7 +1037,7 @@ bool BlockManager::ReadBlock(CBlock& block, const CBlockIndex& index) const
|
|||||||
return ReadBlock(block, block_pos, index.GetBlockHash());
|
return ReadBlock(block, block_pos, index.GetBlockHash());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BlockManager::ReadRawBlock(std::vector<uint8_t>& block, const FlatFilePos& pos) const
|
bool BlockManager::ReadRawBlock(std::vector<std::byte>& block, const FlatFilePos& pos) const
|
||||||
{
|
{
|
||||||
if (pos.nPos < STORAGE_HEADER_BYTES) {
|
if (pos.nPos < STORAGE_HEADER_BYTES) {
|
||||||
// If nPos is less than STORAGE_HEADER_BYTES, we can't read the header that precedes the block data
|
// 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&
|
|||||||
}
|
}
|
||||||
|
|
||||||
block.resize(blk_size); // Zeroing of memory is intentional here
|
block.resize(blk_size); // Zeroing of memory is intentional here
|
||||||
filein.read(MakeWritableByteSpan(block));
|
filein.read(block);
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
LogError("Read from block file failed: %s for %s while reading raw block", e.what(), pos.ToString());
|
LogError("Read from block file failed: %s for %s while reading raw block", e.what(), pos.ToString());
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -413,7 +413,7 @@ public:
|
|||||||
/** Functions for disk access for blocks */
|
/** Functions for disk access for blocks */
|
||||||
bool ReadBlock(CBlock& block, const FlatFilePos& pos, const std::optional<uint256>& expected_hash = {}) const;
|
bool ReadBlock(CBlock& block, const FlatFilePos& pos, const std::optional<uint256>& expected_hash = {}) const;
|
||||||
bool ReadBlock(CBlock& block, const CBlockIndex& index) const;
|
bool ReadBlock(CBlock& block, const CBlockIndex& index) const;
|
||||||
bool ReadRawBlock(std::vector<uint8_t>& block, const FlatFilePos& pos) const;
|
bool ReadRawBlock(std::vector<std::byte>& block, const FlatFilePos& pos) const;
|
||||||
|
|
||||||
bool ReadBlockUndo(CBlockUndo& blockundo, const CBlockIndex& index) const;
|
bool ReadBlockUndo(CBlockUndo& blockundo, const CBlockIndex& index) const;
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -318,7 +318,7 @@ static bool rest_block(const std::any& context,
|
|||||||
pos = pblockindex->GetBlockPos();
|
pos = pblockindex->GetBlockPos();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<uint8_t> block_data{};
|
std::vector<std::byte> block_data{};
|
||||||
if (!chainman.m_blockman.ReadRawBlock(block_data, pos)) {
|
if (!chainman.m_blockman.ReadRawBlock(block_data, pos)) {
|
||||||
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
|
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
|
||||||
}
|
}
|
||||||
@@ -326,7 +326,7 @@ static bool rest_block(const std::any& context,
|
|||||||
switch (rf) {
|
switch (rf) {
|
||||||
case RESTResponseFormat::BINARY: {
|
case RESTResponseFormat::BINARY: {
|
||||||
req->WriteHeader("Content-Type", "application/octet-stream");
|
req->WriteHeader("Content-Type", "application/octet-stream");
|
||||||
req->WriteReply(HTTP_OK, std::as_bytes(std::span{block_data}));
|
req->WriteReply(HTTP_OK, block_data);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -660,9 +660,9 @@ static CBlock GetBlockChecked(BlockManager& blockman, const CBlockIndex& blockin
|
|||||||
return block;
|
return block;
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::vector<uint8_t> GetRawBlockChecked(BlockManager& blockman, const CBlockIndex& blockindex)
|
static std::vector<std::byte> GetRawBlockChecked(BlockManager& blockman, const CBlockIndex& blockindex)
|
||||||
{
|
{
|
||||||
std::vector<uint8_t> data{};
|
std::vector<std::byte> data{};
|
||||||
FlatFilePos pos{};
|
FlatFilePos pos{};
|
||||||
{
|
{
|
||||||
LOCK(cs_main);
|
LOCK(cs_main);
|
||||||
@@ -812,7 +812,7 @@ static RPCHelpMan getblock()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::vector<uint8_t> block_data{GetRawBlockChecked(chainman.m_blockman, *pblockindex)};
|
const std::vector<std::byte> block_data{GetRawBlockChecked(chainman.m_blockman, *pblockindex)};
|
||||||
|
|
||||||
if (verbosity <= 0) {
|
if (verbosity <= 0) {
|
||||||
return HexStr(block_data);
|
return HexStr(block_data);
|
||||||
|
|||||||
+3
-2
@@ -100,13 +100,14 @@ private:
|
|||||||
class SpanReader
|
class SpanReader
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
std::span<const unsigned char> m_data;
|
std::span<const std::byte> m_data;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @param[in] data Referenced byte vector to overwrite/append
|
* @param[in] data Referenced byte vector to overwrite/append
|
||||||
*/
|
*/
|
||||||
explicit SpanReader(std::span<const unsigned char> data) : m_data{data} {}
|
explicit SpanReader(std::span<const unsigned char> data) : m_data{std::as_bytes(data)} {}
|
||||||
|
explicit SpanReader(std::span<const std::byte> data) : m_data{data} {}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
SpanReader& operator>>(T&& obj)
|
SpanReader& operator>>(T&& obj)
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ std::list<const CZMQAbstractNotifier*> CZMQNotificationInterface::GetActiveNotif
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<CZMQNotificationInterface> CZMQNotificationInterface::Create(std::function<bool(std::vector<uint8_t>&, const CBlockIndex&)> get_block_by_index)
|
std::unique_ptr<CZMQNotificationInterface> CZMQNotificationInterface::Create(std::function<bool(std::vector<std::byte>&, const CBlockIndex&)> get_block_by_index)
|
||||||
{
|
{
|
||||||
std::map<std::string, CZMQNotifierFactory> factories;
|
std::map<std::string, CZMQNotifierFactory> factories;
|
||||||
factories["pubhashblock"] = CZMQAbstractNotifier::Create<CZMQPublishHashBlockNotifier>;
|
factories["pubhashblock"] = CZMQAbstractNotifier::Create<CZMQPublishHashBlockNotifier>;
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ public:
|
|||||||
|
|
||||||
std::list<const CZMQAbstractNotifier*> GetActiveNotifiers() const;
|
std::list<const CZMQAbstractNotifier*> GetActiveNotifiers() const;
|
||||||
|
|
||||||
static std::unique_ptr<CZMQNotificationInterface> Create(std::function<bool(std::vector<uint8_t>&, const CBlockIndex&)> get_block_by_index);
|
static std::unique_ptr<CZMQNotificationInterface> Create(std::function<bool(std::vector<std::byte>&, const CBlockIndex&)> get_block_by_index);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool Initialize();
|
bool Initialize();
|
||||||
|
|||||||
@@ -243,7 +243,7 @@ bool CZMQPublishRawBlockNotifier::NotifyBlock(const CBlockIndex *pindex)
|
|||||||
{
|
{
|
||||||
LogDebug(BCLog::ZMQ, "Publish rawblock %s to %s\n", pindex->GetBlockHash().GetHex(), this->address);
|
LogDebug(BCLog::ZMQ, "Publish rawblock %s to %s\n", pindex->GetBlockHash().GetHex(), this->address);
|
||||||
|
|
||||||
std::vector<uint8_t> block{};
|
std::vector<std::byte> block{};
|
||||||
if (!m_get_block_by_index(block, *pindex)) {
|
if (!m_get_block_by_index(block, *pindex)) {
|
||||||
zmqError("Can't read block from disk");
|
zmqError("Can't read block from disk");
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -49,10 +49,10 @@ public:
|
|||||||
class CZMQPublishRawBlockNotifier : public CZMQAbstractPublishNotifier
|
class CZMQPublishRawBlockNotifier : public CZMQAbstractPublishNotifier
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
const std::function<bool(std::vector<uint8_t>&, const CBlockIndex&)> m_get_block_by_index;
|
const std::function<bool(std::vector<std::byte>&, const CBlockIndex&)> m_get_block_by_index;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CZMQPublishRawBlockNotifier(std::function<bool(std::vector<uint8_t>&, const CBlockIndex&)> get_block_by_index)
|
CZMQPublishRawBlockNotifier(std::function<bool(std::vector<std::byte>&, const CBlockIndex&)> get_block_by_index)
|
||||||
: m_get_block_by_index{std::move(get_block_by_index)} {}
|
: m_get_block_by_index{std::move(get_block_by_index)} {}
|
||||||
bool NotifyBlock(const CBlockIndex *pindex) override;
|
bool NotifyBlock(const CBlockIndex *pindex) override;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in new issue
Block a user