refactor: Treat CDataStream bytes as uint8_t #20464

pull MarcoFalke wants to merge 5 commits into bitcoin:master from MarcoFalke:2011-dataStream changing 13 files +63 −122
  1. MarcoFalke commented at 7:08 pm on November 23, 2020: member

    Using uint8_t for raw bytes has a style benefit:

    • The signedness is clear from reading the code, as it does not depend on the architecture

    Other clean-ups in this pull include:

    • Remove unused methods
    • Constructor is simplified with Span
    • Remove Init() member in favor of C++11 member initialization
  2. Remove unused CDataStream methods faa96f841f
  3. MarcoFalke added the label Refactoring on Nov 23, 2020
  4. MarcoFalke added the label Utils/log/libs on Nov 23, 2020
  5. MarcoFalke force-pushed on Nov 23, 2020
  6. practicalswift commented at 7:44 pm on November 23, 2020: contributor
    Concept ACK: nice cleanup (+49 −109)!
  7. refactor: Drop CDataStream constructors in favor of one taking a Span of bytes fa8bdb048e
  8. Treat CDataStream bytes as uint8_t
    Also, rename CSerializeData to SerializeData
    fada14b948
  9. MarcoFalke force-pushed on Nov 23, 2020
  10. DrahtBot commented at 9:38 pm on November 23, 2020: member

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

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #14485 (Try to use posix_fadvise with CBufferedFile by luke-jr)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

  11. laanwj commented at 8:54 am on November 25, 2020: member
    Concept ACK!
  12. laanwj commented at 8:58 am on November 25, 2020: member

    There are some more unnecessary MakeUCharSpan that can be removed here, especially related to Base64 handling:

    0src/rpc/rawtransaction.cpp:    return EncodeBase64(MakeUCharSpan(ssTx));
    1src/rpc/rawtransaction.cpp:    return EncodeBase64(MakeUCharSpan(ssTx));
    2src/rpc/rawtransaction.cpp:    return EncodeBase64(MakeUCharSpan(ssTx));
    3src/rpc/rawtransaction.cpp:    return EncodeBase64(MakeUCharSpan(ssTx));
    4src/rpc/rawtransaction.cpp:    return EncodeBase64(MakeUCharSpan(ssTx));
    

    (possibly more but I could find these quickly)

  13. jonatack commented at 9:25 am on November 25, 2020: member
    Concept ACK
  14. in src/streams.h:241 in fa8dec9e89 outdated
    265-        Init(nTypeIn, nVersionIn);
    266         ::SerializeMany(*this, std::forward<Args>(args)...);
    267     }
    268 
    269-    void Init(int nTypeIn, int nVersionIn)
    270+    void Rewind()
    


    laanwj commented at 12:10 pm on December 19, 2020:
    There is already a bool Rewind(size_type n), overloading it here is confusing. We should think of a new name. I had hoped to think of a way to use the existing method in the only place it is used, but it appears there is no way to access nReadPos (or even derive it) from the outside.

    MarcoFalke commented at 12:17 pm on December 19, 2020:
    I think the name make sense. Rewind(size_t) rewinds only how much is passed. Rewind() rewinds all the way. Though, I am happy to rename this to anything else, if there are suggestions.

    theStack commented at 8:09 pm on December 30, 2020:

    It’s a pity that there is no defined invalid value for size_t, which could be passed as default value. Would Rewind(std::optional<size_t> n = std::nullopt) be a possibility here to unite the two methods? Like e.g.:

     0diff --git a/src/streams.h b/src/streams.h
     1index 2a921c2c5..a6e781b95 100644
     2--- a/src/streams.h
     3+++ b/src/streams.h
     4@@ -14,6 +14,7 @@
     5 #include <assert.h>
     6 #include <ios>
     7 #include <limits>
     8+#include <optional>
     9 #include <stdint.h>
    10 #include <stdio.h>
    11 #include <string.h>
    12@@ -238,11 +239,6 @@ public:
    13         ::SerializeMany(*this, std::forward<Args>(args)...);
    14     }
    15
    16-    void Rewind()
    17-    {
    18-        nReadPos = 0;
    19-    }
    20-
    21     std::string str() const
    22     {
    23         return (std::string(begin(), end()));
    24@@ -339,12 +335,17 @@ public:
    25         nReadPos = 0;
    26     }
    27
    28-    bool Rewind(size_type n)
    29+    bool Rewind(std::optional<size_type> n = std::nullopt)
    30     {
    31+        // Total rewind if no size is passed
    32+        if (!n) {
    33+            nReadPos = 0;
    34+            return true;
    35+        }
    36         // Rewind by n characters if the buffer hasn't been compacted yet
    37-        if (n > nReadPos)
    38+        if (*n > nReadPos)
    39             return false;
    40-        nReadPos -= n;
    41+        nReadPos -= *n;
    42         return true;
    43     }
    

    Probably overkill for methods that are only ever called in the benchmarks though…


    MarcoFalke commented at 8:10 am on December 31, 2020:
    Thanks, fixed

    laanwj commented at 2:05 pm on February 1, 2021:

    I think the name make sense. Rewind(size_t) rewinds only how much is passed.

    Right. My remark was triggered mostly by that the two implementations of rewind() were in different places in the method list and not together, the name makes some sense.

    But I like the new combined implementation.

  15. laanwj commented at 12:18 pm on December 19, 2020: member
    Code review ACK apart from the above comment. This PR is straightforward to review. The first commit can be seen separately.
  16. theStack commented at 8:19 pm on December 30, 2020: member
    Concept ACK – will review as soon as #20464 (review) is resolved
  17. Remove CDataStream::Init in favor of C++11 member initialization faf4aa2f47
  18. Remove redundant MakeUCharSpan wrappers fa29272459
  19. MarcoFalke force-pushed on Dec 31, 2020
  20. theStack approved
  21. theStack commented at 12:13 pm on December 31, 2020: member
    ACK fa292724598c273867bc6dbf311f1440fe2541ba 🍾
  22. laanwj commented at 2:06 pm on February 1, 2021: member
    code review ACK fa292724598c273867bc6dbf311f1440fe2541ba
  23. laanwj merged this on Feb 1, 2021
  24. laanwj closed this on Feb 1, 2021

  25. sidhujag referenced this in commit 6c6489cb41 on Feb 2, 2021
  26. MarcoFalke deleted the branch on Feb 3, 2021
  27. blockstreamsatellite referenced this in commit 6782514a85 on Oct 28, 2021
  28. blockstreamsatellite referenced this in commit dcbfe14615 on Nov 10, 2021
  29. DrahtBot locked this on Aug 16, 2022

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: 2024-12-18 15:12 UTC

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