mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-02-09 02:59:31 +08:00
chore: use std::vector<std::byte> for BlockManager::ReadRawBlock()
This commit is contained in:
@@ -55,7 +55,7 @@ static void ReadRawBlockBench(benchmark::Bench& bench)
|
||||
const auto testing_setup{MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN)};
|
||||
auto& blockman{testing_setup->m_node.chainman->m_blockman};
|
||||
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
|
||||
bench.run([&] {
|
||||
const auto success{blockman.ReadRawBlock(block_data, pos)};
|
||||
|
||||
@@ -1677,7 +1677,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
||||
|
||||
#ifdef ENABLE_ZMQ
|
||||
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);
|
||||
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()) {
|
||||
// Fast-path: in this case it is possible to serve the block directly from 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 (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));
|
||||
|
||||
@@ -995,7 +995,7 @@ bool BlockManager::ReadBlock(CBlock& block, const FlatFilePos& pos, const std::o
|
||||
block.SetNull();
|
||||
|
||||
// Open history file to read
|
||||
std::vector<uint8_t> block_data;
|
||||
std::vector<std::byte> block_data;
|
||||
if (!ReadRawBlock(block_data, pos)) {
|
||||
return false;
|
||||
}
|
||||
@@ -1037,7 +1037,7 @@ bool BlockManager::ReadBlock(CBlock& block, const CBlockIndex& index) const
|
||||
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 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
|
||||
filein.read(MakeWritableByteSpan(block));
|
||||
filein.read(block);
|
||||
} catch (const std::exception& e) {
|
||||
LogError("Read from block file failed: %s for %s while reading raw block", e.what(), pos.ToString());
|
||||
return false;
|
||||
|
||||
@@ -413,7 +413,7 @@ public:
|
||||
/** 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 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;
|
||||
|
||||
|
||||
@@ -318,7 +318,7 @@ static bool rest_block(const std::any& context,
|
||||
pos = pblockindex->GetBlockPos();
|
||||
}
|
||||
|
||||
std::vector<uint8_t> block_data{};
|
||||
std::vector<std::byte> block_data{};
|
||||
if (!chainman.m_blockman.ReadRawBlock(block_data, pos)) {
|
||||
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
|
||||
}
|
||||
@@ -326,7 +326,7 @@ static bool rest_block(const std::any& context,
|
||||
switch (rf) {
|
||||
case RESTResponseFormat::BINARY: {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -660,9 +660,9 @@ static CBlock GetBlockChecked(BlockManager& blockman, const CBlockIndex& blockin
|
||||
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{};
|
||||
{
|
||||
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) {
|
||||
return HexStr(block_data);
|
||||
|
||||
@@ -100,13 +100,14 @@ private:
|
||||
class SpanReader
|
||||
{
|
||||
private:
|
||||
std::span<const unsigned char> m_data;
|
||||
std::span<const std::byte> m_data;
|
||||
|
||||
public:
|
||||
/**
|
||||
* @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>
|
||||
SpanReader& operator>>(T&& obj)
|
||||
|
||||
@@ -40,7 +40,7 @@ std::list<const CZMQAbstractNotifier*> CZMQNotificationInterface::GetActiveNotif
|
||||
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;
|
||||
factories["pubhashblock"] = CZMQAbstractNotifier::Create<CZMQPublishHashBlockNotifier>;
|
||||
|
||||
@@ -26,7 +26,7 @@ public:
|
||||
|
||||
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:
|
||||
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);
|
||||
|
||||
std::vector<uint8_t> block{};
|
||||
std::vector<std::byte> block{};
|
||||
if (!m_get_block_by_index(block, *pindex)) {
|
||||
zmqError("Can't read block from disk");
|
||||
return false;
|
||||
|
||||
@@ -49,10 +49,10 @@ public:
|
||||
class CZMQPublishRawBlockNotifier : public CZMQAbstractPublishNotifier
|
||||
{
|
||||
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:
|
||||
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)} {}
|
||||
bool NotifyBlock(const CBlockIndex *pindex) override;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user