refactor: use std::vector<std::byte> for BlockManager::ReadRawBlock() #32743

pull romanz wants to merge 1 commits into bitcoin:master from romanz:read-raw-bytes changing 12 files +20 −19
  1. romanz commented at 5:04 am on June 13, 2025: contributor

    Following this comment, this PR changes BlockManager::ReadRawBlock() to accept a std::vector<std::byte> instead of std::vector<uint8_t>, in order to avoid casts during its invocations.

    It also adds a new SpanReader constructor to allow reading from a span of std::bytes (in addition to span of uint8_t).

  2. DrahtBot commented at 5:04 am on June 13, 2025: contributor

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/32743.

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK l0rinc
    Stale ACK maflcko, TheCharlatan, hebasto, BrandonOdiwuor

    If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

  3. maflcko commented at 5:34 am on June 13, 2025: member

    lgtm ACK ddaa27e83dfa13e961a4be3293fb647ed0fe3a1d

    This changes the underlying type to std::byte to get rid of casts in calling code and also allows SpanReader to deal with std::byte now.

  4. DrahtBot renamed this:
    chore: use `std::vector<std::byte>` for `BlockManager::ReadRawBlock()`
    refactor: use `std::vector<std::byte>` for `BlockManager::ReadRawBlock()`
    on Jun 13, 2025
  5. DrahtBot added the label Refactoring on Jun 13, 2025
  6. TheCharlatan approved
  7. TheCharlatan commented at 10:28 am on June 13, 2025: contributor
    ACK ddaa27e83dfa13e961a4be3293fb647ed0fe3a1d
  8. hebasto approved
  9. hebasto commented at 10:40 am on June 13, 2025: member
    ACK ddaa27e83dfa13e961a4be3293fb647ed0fe3a1d, I have reviewed the code and it looks OK.
  10. fanquake commented at 10:49 am on June 13, 2025: member
    Killed and re-ran the never ending CIs: https://cirrus-ci.com/build/4760419941744640.
  11. in src/node/blockstorage.cpp:1040 in ddaa27e83d outdated
    1036@@ -1037,7 +1037,7 @@ bool BlockManager::ReadBlock(CBlock& block, const CBlockIndex& index) const
    1037     return ReadBlock(block, block_pos, index.GetBlockHash());
    1038 }
    1039 
    1040-bool BlockManager::ReadRawBlock(std::vector<uint8_t>& block, const FlatFilePos& pos) const
    1041+bool BlockManager::ReadRawBlock(std::vector<std::byte>& block, const FlatFilePos& pos) const
    


    l0rinc commented at 11:13 am on June 13, 2025:

    It seems to me we don’t need filein.read(MakeWritableByteSpan(block)); after this change anymore:

    0        filein.read(block);
    

    romanz commented at 4:21 pm on June 13, 2025:
    Thanks! Fixed in 6ecb9fc65f.
  12. in src/streams.h:110 in ddaa27e83d outdated
    106@@ -107,6 +107,7 @@ class SpanReader
    107      * @param[in]  data Referenced byte vector to overwrite/append
    108      */
    109     explicit SpanReader(std::span<const unsigned char> data) : m_data{data} {}
    110+    explicit SpanReader(std::span<const std::byte> data) : m_data{MakeUCharSpan(data)} {}
    


    l0rinc commented at 11:18 am on June 13, 2025:

    👍 for adding an alternative constructor - this could be a good opportunity to migrate the internals of SpanReader to std::byte as well, given that read already copies std::byte into m_data:

    0    std::span<const std::byte> m_data;
    1
    2public:
    3    /**
    4     * [@param](/bitcoin-bitcoin/contributor/param/)[in]  data Referenced byte vector to overwrite/append
    5     */
    6    explicit SpanReader(const std::span<const unsigned char> data) : m_data{std::as_bytes(data)} {}
    7    explicit SpanReader(const std::span<const std::byte> data) : m_data{data} {}
    

    romanz commented at 4:21 pm on June 13, 2025:
    Sounds good - changed in 6ecb9fc65f.
  13. l0rinc approved
  14. l0rinc commented at 11:53 am on June 13, 2025: contributor

    Concept ACK, good to get rid of a few more unsigned-char-to-byte conversions! Could you please provide a more descriptive PR description?

    I also have a few non-blocking suggestions I’d like you to consider:

     0diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp
     1index 3fc1e0fb16..5816baa20e 100644
     2--- a/src/node/blockstorage.cpp
     3+++ b/src/node/blockstorage.cpp
     4@@ -1071,7 +1071,7 @@ bool BlockManager::ReadRawBlock(std::vector<std::byte>& block, const FlatFilePos
     5         }
     6
     7         block.resize(blk_size); // Zeroing of memory is intentional here
     8-        filein.read(MakeWritableByteSpan(block));
     9+        filein.read(block);
    10     } catch (const std::exception& e) {
    11         LogError("Read from block file failed: %s for %s while reading raw block", e.what(), pos.ToString());
    12         return false;
    13diff --git a/src/streams.h b/src/streams.h
    14index 682f4d4b33..8dde5a9af9 100644
    15--- a/src/streams.h
    16+++ b/src/streams.h
    17@@ -99,15 +99,14 @@ private:
    18  */
    19 class SpanReader
    20 {
    21-private:
    22-    std::span<const unsigned char> m_data;
    23+    std::span<const std::byte> m_data;
    24
    25 public:
    26     /**
    27      * [@param](/bitcoin-bitcoin/contributor/param/)[in]  data Referenced byte vector to overwrite/append
    28      */
    29-    explicit SpanReader(std::span<const unsigned char> data) : m_data{data} {}
    30-    explicit SpanReader(std::span<const std::byte> data) : m_data{MakeUCharSpan(data)} {}
    31+    explicit SpanReader(const std::span<const unsigned char> data) : m_data{std::as_bytes(data)} {}
    32+    explicit SpanReader(const std::span<const std::byte> data) : m_data{data} {}
    33
    34     template<typename T>
    35     SpanReader& operator>>(T&& obj)
    
  15. BrandonOdiwuor commented at 12:17 pm on June 13, 2025: contributor
    Code Review ACK ddaa27e83dfa13e961a4be3293fb647ed0fe3a1d using std::vector<std::byte> instead of std::vector<uint8_t> for BlockManager::ReadRawBlock()
  16. DrahtBot requested review from l0rinc on Jun 13, 2025
  17. chore: use `std::vector<std::byte>` for `BlockManager::ReadRawBlock()` 6ecb9fc65f
  18. romanz force-pushed on Jun 13, 2025
  19. l0rinc commented at 5:36 pm on June 13, 2025: contributor
    ACK 6ecb9fc65f9a5654663304757880e50d73cf4a84
  20. DrahtBot requested review from hebasto on Jun 13, 2025
  21. DrahtBot requested review from TheCharlatan on Jun 13, 2025
  22. DrahtBot requested review from BrandonOdiwuor on Jun 13, 2025
  23. DrahtBot requested review from maflcko on Jun 13, 2025

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2025-06-15 06:13 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me