Skip to content

Conversation

sipa
Copy link
Member

@sipa sipa commented Aug 26, 2019

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

@maflcko
Copy link
Member

maflcko commented Aug 26, 2019

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

@practicalswift
Copy link
Contributor

ACK 92528c2 -- 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 (🎉)
    }

@DrahtBot DrahtBot added the Tests label Aug 26, 2019
@jamesob
Copy link
Contributor

jamesob commented Aug 26, 2019

ACK 92528c2

Low-risk change, reviewed on Github.

fanquake added a commit that referenced this pull request Aug 26, 2019
92528c2 Support serialization of std::vector<bool> (Pieter Wuille)

Pull request description:

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

ACKs for top commit:
  MarcoFalke:
    ACK 92528c2 (only looked at the diff on GitHub)
  practicalswift:
    ACK 92528c2 -- diff looks correct
  jamesob:
    ACK 92528c2

Tree-SHA512: 068246a55a889137098f817bf72d99fc3218a560d62a97c59cccc0392b110f6778843cee4e89d56f271ac64e03a0f64dc5e2cc22daa833fbbbe9234c5f42d7b9
@fanquake fanquake merged commit 92528c2 into bitcoin:master Aug 26, 2019
@Empact
Copy link
Contributor

Empact commented Aug 27, 2019

ACK 92528c2

  • 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.

@JeremyRubin
Copy link
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?

@sipa
Copy link
Member Author

sipa commented Aug 27, 2019

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

@sipa
Copy link
Member Author

sipa commented Aug 27, 2019

@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.

@JeremyRubin
Copy link
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.

jasonbcox pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Oct 14, 2020
Summary:
PR description:
> This adds support for serialization of std::vector<bool>, as a prerequisite of [[bitcoin/bitcoin#16702 | PR16702]].

Backport of Core [[bitcoin/bitcoin#16730 | PR16730]].

Test Plan: `ninja && ninja check`

Reviewers: O1 Bitcoin ABC, #bitcoin_abc, deadalnix

Reviewed By: O1 Bitcoin ABC, #bitcoin_abc, deadalnix

Subscribers: deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D7907
DeckerSU added a commit to DeckerSU/komodo that referenced this pull request Feb 7, 2021
bitcoin/bitcoin#16730

Without this fix we got a following error:
no member named 'Serialize' in 'std::__1::__bit_const_reference<std::__1::vector<bool, std::__1::allocator<bool> > >'
during clang build.
DeckerSU added a commit to DeckerSU/KomodoOcean that referenced this pull request Feb 25, 2021
bitcoin/bitcoin#16730

Without this fix we got a following error:
no member named 'Serialize' in 'std::__1::__bit_const_reference<std::__1::vector<bool, std::__1::allocator<bool> > >'
during clang build.
kwvg added a commit to kwvg/dash that referenced this pull request Mar 9, 2021
kwvg added a commit to kwvg/dash that referenced this pull request Mar 9, 2021
kwvg added a commit to kwvg/dash that referenced this pull request Apr 21, 2021
kwvg added a commit to kwvg/dash that referenced this pull request Apr 23, 2021
kwvg added a commit to kwvg/dash that referenced this pull request Apr 23, 2021
kwvg added a commit to kwvg/dash that referenced this pull request Apr 23, 2021
kwvg added a commit to kwvg/dash that referenced this pull request Apr 23, 2021
kwvg pushed a commit to kwvg/dash that referenced this pull request May 11, 2021
kwvg pushed a commit to kwvg/dash that referenced this pull request May 11, 2021
UdjinM6 pushed a commit to UdjinM6/dash that referenced this pull request May 15, 2021
kwvg pushed a commit to kwvg/dash that referenced this pull request May 18, 2021
kwvg added a commit to kwvg/dash that referenced this pull request May 18, 2021
UdjinM6 pushed a commit to UdjinM6/dash that referenced this pull request May 19, 2021
PastaPastaPasta added a commit to dashpay/dash that referenced this pull request May 19, 2021
merge bitcoin#17812, bitcoin#16702, bitcoin#16730, bitcoin#18023: supplying and using asmap to improve IP bucketing in addrman
kwvg added a commit to kwvg/dash that referenced this pull request May 20, 2021
DeckerSU added a commit to DeckerSU/komodo that referenced this pull request Jun 9, 2021
bitcoin/bitcoin#16730

Without this fix we got a following error:
no member named 'Serialize' in 'std::__1::__bit_const_reference<std::__1::vector<bool, std::__1::allocator<bool> > >'
during clang build.
furszy added a commit to PIVX-Project/PIVX that referenced this pull request Nov 16, 2021
bbd14ce Support serialization of std::vector<bool> (Pieter Wuille)

Pull request description:

  Backport of bitcoin#16730, fixing the following clang warning:

  ```
  ./serialize.h:631:44: warning: loop variable 'elem' is always a copy because the range of type 'const
        std::vector<bool, std::allocator<bool> >' does not return a reference [-Wrange-loop-analysis]
          for (const typename V::value_type& elem : v) {
                                             ^
  ./serialize.h:484:77: note: in instantiation of function template specialization
        'VectorFormatter<DefaultFormatter>::Ser<CHashWriter, std::vector<bool, std::allocator<bool> > >'
        requested here
      template<typename Stream> void Serialize(Stream &s) const { Formatter().Ser(s, m_object); }
                                                                              ^
  ./serialize.h:806:7: note: in instantiation of function template specialization
        'Wrapper<VectorFormatter<DefaultFormatter>, const std::vector<bool, std::allocator<bool> >
        &>::Serialize<CHashWriter>' requested here
      a.Serialize(os);
        ^
  ./serialize.h:956:5: note: in instantiation of function template specialization 'Serialize<CHashWriter,
        Wrapper<VectorFormatter<DefaultFormatter>, const std::vector<bool, std::allocator<bool> > &> >'
        requested here
      Serialize(os, Using<VectorFormatter<DefaultFormatter>>(v));
      ^
  ./serialize.h:962:5: note: in instantiation of function template specialization
        'Serialize_impl<CHashWriter, bool, std::allocator<bool>, bool>' requested here
      Serialize_impl(os, v, T());
      ^
  ./hash.h:247:11: note: in instantiation of function template specialization 'Serialize<CHashWriter, bool,
        std::allocator<bool> >' requested here
          ::Serialize(*this, obj);
            ^
  ./hash.h:292:8: note: in instantiation of function template specialization
        'CHashWriter::operator<<<std::vector<bool, std::allocator<bool> > >' requested here
      ss << obj;
         ^
  init.cpp:1504:39: note: in instantiation of function template specialization
        'SerializeHash<std::vector<bool, std::allocator<bool> > >' requested here
          const uint256 asmap_version = SerializeHash(asmap);
  ```

ACKs for top commit:
  furszy:
    good finding, utACK bbd14ce

Tree-SHA512: ff26242545f727e6f6925e2f4f63f2616d89306c0bcc42bdf778eae95b85673ae5062637601fb5a27b49cb793de2ec144abcb2af63f62c18a0edbac5e07aea68
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Dec 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants