Support serialization of std::vector<bool> #16730

pull sipa wants to merge 1 commits into bitcoin:master from sipa:201908_servecbool changing 2 files +21 −0
  1. sipa commented at 6:39 PM on August 26, 2019: member

    This adds support for serialization of std::vector<bool>, as a prerequisite of #16702.

  2. Support serialization of std::vector<bool> 92528c260e
  3. MarcoFalke commented at 6:45 PM on August 26, 2019: member

    ACK 92528c260e9bd5d36da4b017098a49fda5051154 (only looked at the diff on GitHub)

  4. practicalswift commented at 6:56 PM on August 26, 2019: contributor

    ACK 92528c260e9bd5d36da4b017098a49fda5051154 -- diff looks correct

    Related: #16572 (comment)

    std::vector<bool> gotcha illustrated:

        std::vector<char> v1;
        for (const auto& x : v1) {
          // const char& x (as expected)
        }
    
        std::vector<int> v2;
        for (const auto& x : v2) {
          // const int& x (as expected)
        }
    
        std::vector<bool> v3;
        for (const auto& x : v3) {
          // const std::_Bit_reference& x (🎉)
        }
    
  5. DrahtBot added the label Tests on Aug 26, 2019
  6. jamesob commented at 7:17 PM on August 26, 2019: member
  7. MarcoFalke removed the label Tests on Aug 26, 2019
  8. MarcoFalke added the label Utils/log/libs on Aug 26, 2019
  9. fanquake referenced this in commit efe1ee0d8d on Aug 26, 2019
  10. fanquake merged this on Aug 26, 2019
  11. fanquake closed this on Aug 26, 2019

  12. Empact commented at 12:08 AM on August 27, 2019: member

    ACK https://github.com/bitcoin/bitcoin/pull/16730/commits/92528c260e9bd5d36da4b017098a49fda5051154

    • diff looks correct
    • confirmed the included test doesn't build without the source change
    In file included from test/serialize_tests.cpp:5:
    ./serialize.h:602:7: error: no member named 'Serialize' in 'std::__1::__bit_const_reference<std::__1::vector<bool, std::__1::allocator<bool> > >'
        a.Serialize(os);
        ~ ^
    ./serialize.h:721:11: note: in instantiation of function template specialization 'Serialize<CHashWriter, std::__1::__bit_const_reference<std::__1::vector<bool, std::__1::allocator<bool> > > >' requested here
            ::Serialize(os, (*vi));
              ^
    ./serialize.h:727:5: note: in instantiation of function template specialization 'Serialize_impl<CHashWriter, bool, std::__1::allocator<bool>, bool>' requested here
        Serialize_impl(os, v, T());
        ^
    ./hash.h:154:11: note: in instantiation of function template specialization 'Serialize<CHashWriter, bool, std::__1::allocator<bool> >' requested here
            ::Serialize(*this, obj);
              ^
    ./hash.h:199:8: note: in instantiation of function template specialization 'CHashWriter::operator<<<std::__1::vector<bool, std::__1::allocator<bool> > >' requested here
        ss << obj;
           ^
    test/serialize_tests.cpp:267:40: note: in instantiation of function template specialization 'SerializeHash<std::__1::vector<bool, std::__1::allocator<bool> > >' requested here
        BOOST_CHECK(SerializeHash(vec1) == SerializeHash(vec2));
                                           ^
    1 error generated.
    
  13. JeremyRubin commented at 4:12 AM on August 27, 2019: contributor

    We don't want to define serialization to be a bit vector?

    We're not using a std::vector<bool> serialized as bytes right now are we?

  14. sipa commented at 5:53 AM on August 27, 2019: member

    @JeremyRubin It's just for hashing, it doesn't really matter.

  15. sipa commented at 11:52 PM on August 27, 2019: member

    @JeremyRubin Longer answer: I think the expected serialization of std::vector<bool> is that it's serialized as any other vector would be serialized (compact length + serialization of each of the individual elements). If we want something that's serialized as a bitpacked array, we can introduce a BitPacker wrapper to serialize.h or so.

  16. JeremyRubin commented at 10:44 PM on August 29, 2019: contributor

    I would think the reason to worry about the serialization for hashing with a bitpacked array is that the incoming message can be hashed and it will be equivalent to the hash of the deserializaed struct.

    Agree the best way to do this is by having a different type for a BitPacked serialization.

  17. jasonbcox referenced this in commit 4b12d4863d on Oct 14, 2020
  18. DeckerSU referenced this in commit 49d4ea2f95 on Feb 7, 2021
  19. DeckerSU referenced this in commit 4e949d584e on Feb 25, 2021
  20. kittywhiskers referenced this in commit ed647db1cb on Mar 9, 2021
  21. kittywhiskers referenced this in commit 688985b2f4 on Mar 9, 2021
  22. kittywhiskers referenced this in commit be03114ed9 on Apr 21, 2021
  23. kittywhiskers referenced this in commit 05d2cfb86f on Apr 23, 2021
  24. kittywhiskers referenced this in commit 87419c96c0 on Apr 23, 2021
  25. kittywhiskers referenced this in commit e3dbb6f347 on Apr 23, 2021
  26. kittywhiskers referenced this in commit abc1c3c365 on Apr 23, 2021
  27. kittywhiskers referenced this in commit a509acab98 on May 11, 2021
  28. kittywhiskers referenced this in commit 53aaa4ba04 on May 11, 2021
  29. UdjinM6 referenced this in commit a97079890b on May 15, 2021
  30. kittywhiskers referenced this in commit 2f8618486b on May 18, 2021
  31. kittywhiskers referenced this in commit 6a25151bbd on May 18, 2021
  32. UdjinM6 referenced this in commit 0b0e8c57d3 on May 19, 2021
  33. PastaPastaPasta referenced this in commit 2f555f87d1 on May 19, 2021
  34. kittywhiskers referenced this in commit 40429731be on May 20, 2021
  35. DeckerSU referenced this in commit 6844acfd62 on Jun 9, 2021
  36. furszy referenced this in commit 2b0e8fa137 on Nov 16, 2021
  37. DrahtBot locked this on Dec 16, 2021

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: 2026-04-13 15:14 UTC

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