test: add mocked Sock that can read/write custom data and/or CNetMessages #30205

pull vasild wants to merge 3 commits into bitcoin:master from vasild:DynSock changing 2 files +462 −70
  1. vasild commented at 3:49 pm on May 30, 2024: contributor

    Put the generic parts from StaticContentsSock into a separate class ZeroSock so that they can be reused in other mocked Sock implementations.

    Add a new DynSock whose Recv() and Send() methods can be controlled after the object is created. To achieve that, the caller/creator of DynSock provides to its constructor two pipes (FIFOs) - recv-pipe and send-pipe. Whatever data is written to recv-pipe is later received by DynSock::Recv() method and whatever data is written to the socket using DynSock::Send() can later be found in the send-pipe. For convenience there are also two methods to send and receive CNetMessages.


    This is used in #26812 (first two commits from that PR). Extracting as a separate PR suggested here: #30043 (review).

  2. DrahtBot commented at 3:49 pm on May 30, 2024: 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/30205.

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK Sjors, jonatack, pinheadmz

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

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #26812 (test: add end-to-end tests for CConnman and PeerManager by vasild)

    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.

  3. DrahtBot added the label Tests on May 30, 2024
  4. Sjors commented at 4:31 pm on May 30, 2024: member
    This seems very useful. I’ll try to use it for the (currently very brittle) Sv2Transport tests in #29432, and review it along the way.
  5. in src/test/util/net.h:238 in 06b21ab6cb outdated
    230+        /**
    231+         * Deserialize a `CNetMessage` and remove it from the pipe.
    232+         * If not enough bytes are available then the function will wait. If parsing fails
    233+         * or EOF is signaled to the pipe, then `std::nullopt` is returned.
    234+         */
    235+        std::optional<CNetMessage> GetNetMsg() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
    


    Sjors commented at 1:24 pm on July 1, 2024:

    06b21ab6cb868f6b1e41e36a1f3a4d4c9c51558b: is this still useful now that you have GetBytes? It seems it’s the job for Transport to turn bytes into a CNetMessage.

    Same question for PushNetMsg below.

    See earlier discussion: #26812 (review)

  6. Sjors commented at 1:37 pm on July 1, 2024: member

    I’m still a bit confused on how to use this, e.g. in #30332.

    The first step is to override CreateSocket() in sv2_template_provider_tests.cpp, so that when the TemplateProvider calls it we get a mock socket.

    But how? Something like:

    0CreateSock = [](int, int, int) {
    1    return std::make_unique<DynSock>(...);
    2};
    

    The test contains a TPTester helper which has std::unique_ptr<DynSock> m_peer_socket; to represent the other side of the connection (a Sv2Transport instance that does a handshake and then sends a hardcoded protocol message SetupConnectionMsg).

    This is inialitzed with:

    0m_peer_socket = ConnectDirectly(*tp, /*manual_connection=*/true);
    

    Instead of making a real TCP/IP connection this should be making a mock connection.

  7. vasild commented at 1:37 pm on July 3, 2024: contributor
    @Sjors, I changed the test sv2_connman_tests/client_tests from #30332 to use mocked sockets instead of real ones from the operating system. See the top commit from here: https://github.com/vasild/bitcoin/commits/sv2_mocksock/, that compiles, but the test fails at some point. I will try to get it to pass, just sharing this early wip with you.
  8. Sjors commented at 7:27 am on July 4, 2024: member
    @vasild thanks! At first glance these changes make sense, not sure why it’s broken. I left some comments on your commit f0dc62f8ab773ef7cbf44ba7119d08af66506428.
  9. Sjors commented at 11:37 am on July 4, 2024: member

    So the Sv2Connman is called, but start() isn’t, so the mock BindListenPort doesn’t happen.


    The last commit here gets all the way through the handshake, but then it hangs up with Num bytes received from client id=1: -1. https://github.com/Sjors/bitcoin/commits/2024/07/mockscok/


    The disconnect seems to happen because has_received_data = socket_it->second.occurred & Sock::RECV; is true when sending a message.


    I think we need to override ZeroSock::WaitMany to set occured based on what’s in m_data (DynSock::WaitMany is not called).


    Not sure how to fix this. My guess is ListenSock needs its own WaitMany which picks the right entry from m_pipes and then calls DynSock::WaitMany?

  10. vasild commented at 12:52 pm on July 16, 2024: contributor

    @Sjors, thanks for your suggestions at https://github.com/vasild/bitcoin/commit/f0dc62f8ab773ef7cbf44ba7119d08af66506428. I extended DynSock to be able to Accept() new connections, returning a socket that has been provided by the tests.

    Now the test sv2_connman_tests/client_tests uses mocked sockets and passes!

    See https://github.com/vasild/bitcoin/commits/sv2_mocksock/ which is:

    • This PR, modified to handle Accept(). If you like that I will push the mods to this PR.
    • Plus #30332
    • Plus a minor fixup from #30332 (review)
    • Plus modifications to sv2_connman_tests/client_tests to use mocked sockets and some simplifications because of no real sockets are used now, e.g. the ProcessOurResponse() method is not needed anymore.
  11. Sjors commented at 7:22 am on July 17, 2024: member

    Works like a charm! I’ll see if I can add them to the tests in #30332 as well.


    It worked for the Template Provider as well and was easy to integrate. Thanks again! I’ll review this PR soon(tm).

  12. vasild force-pushed on Aug 2, 2024
  13. vasild commented at 9:34 am on August 2, 2024: contributor

    06b21ab6cb...3769f89a78: Extend DynSock to be able to Accept() new connections, returning a socket that has been provided by the tests.

    If you like that I will push the mods to this PR.

    Done.

  14. vasild force-pushed on Sep 3, 2024
  15. vasild commented at 10:36 am on September 3, 2024: contributor
    3769f89a78...e59097a0a5: rebase to pick CMake
  16. in src/test/util/net.h:240 in 187ba684a9 outdated
    277+
    278     const std::string m_contents;
    279     mutable size_t m_consumed{0};
    280 };
    281 
    282-std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext& random_context);
    


    Sjors commented at 1:01 pm on September 5, 2024:
    187ba684a9d5e63d09b43511b7128cce9edbedf3 any particular reason for moving this?

    vasild commented at 8:25 am on September 6, 2024:
    Just stylistic - I though it would be better to have it before the classes declarations. Should I move it at the bottom, where it was?

    Sjors commented at 8:47 am on September 6, 2024:
    Not a big deal, but diff was already quite large.

    jonatack commented at 2:26 pm on November 26, 2024:
    I also wondered why this was moved. Not moving it would be preferable to save reviewers’ time.

    vasild commented at 9:46 am on December 24, 2024:
    Dropped the move.
  17. in src/test/util/net.h:145 in 187ba684a9 outdated
    145-class StaticContentsSock : public Sock
    146+class ZeroSock : public Sock
    147 {
    148 public:
    149-    explicit StaticContentsSock(const std::string& contents)
    150-        : Sock{INVALID_SOCKET},
    


    Sjors commented at 1:13 pm on September 5, 2024:
    187ba684a9d5e63d09b43511b7128cce9edbedf3: shouldn’t Sock{INVALID_SOCKET} be preserved in the new ZeroSock constructor?

    vasild commented at 7:50 am on September 6, 2024:

    No, the new ZeroSock constructor’s body is now in src/test/util/net.cpp and it does

    0ZeroSock::ZeroSock() : Sock{g_mocked_sock_fd++} {}
    

    Because if all of the ZeroSock instances have m_socket == INVALID_SOCKET, then they would compare equal by EqualSharedPtrSock.

  18. Sjors approved
  19. Sjors commented at 3:18 pm on September 5, 2024: member

    ACK e59097a0a56ecaffdb38aaa94e232f5b45881897

    You can see it in action in sv2_connman_tests in https://github.com/Sjors/bitcoin/pull/50.

    I didn’t test the CNetMessage functionality and Eof() method.

    Maybe split 187ba684a9d5e63d09b43511b7128cce9edbedf3 into one commit that moves the implementation and another that splits the class.

  20. vasild force-pushed on Sep 6, 2024
  21. vasild commented at 8:23 am on September 6, 2024: contributor

    Maybe split 187ba68 into one commit that moves the implementation and another that splits the class.

    e59097a0a5...e995ffa5c3: do that ^

    The cumulative diff before and after this push is identical:

    0$ diff -u <(git diff e59097a0a5~2..e59097a0a5) <(git diff e995ffa5c~3..e995ffa5c)
    1$
    
  22. Sjors commented at 8:52 am on September 6, 2024: member
    @vasild it’s probably easier to reverse those two commits: first move the implementation out, and then split the class.
  23. vasild force-pushed on Sep 6, 2024
  24. vasild commented at 9:24 am on September 6, 2024: contributor

    @vasild it’s probably easier to reverse those two commits: first move the implementation out, and then split the class.

    e995ffa5c3...e7cf8e8fc6: done, same as before, the cumulative diff is identical before and after:

    0$ diff -u <(git diff e59097a0a5~2..e59097a0a5) <(git diff e995ffa5c~3..e995ffa5c)
    1$ diff -u <(git diff e59097a0a5~2..e59097a0a5) <(git diff e7cf8e8fc~3..e7cf8e8fc)
    2$ 
    
  25. Sjors commented at 1:58 pm on September 6, 2024: member

    re-ACK e7cf8e8fc658e4559fcc64279c7bd854773ad6c2

    This makes my dimmed zebra very happy for the first commit, and the second one is now much easier to read.

  26. jonatack commented at 1:39 pm on October 11, 2024: member
    Concept ACK
  27. DrahtBot added the label CI failed on Oct 23, 2024
  28. DrahtBot removed the label CI failed on Oct 25, 2024
  29. test: move the implementation of StaticContentsSock to .cpp
    Move the implementation (method definitions) from `test/util/net.h` to
    `test/util/net.cpp` to make the header easier to follow.
    4b58d55878
  30. vasild force-pushed on Nov 18, 2024
  31. vasild commented at 12:31 pm on November 18, 2024: contributor
    e7cf8e8fc6...5766bbefa9: rebase and make sure DynSock::Accept()’s output argument is initialized properly: https://github.com/Sjors/bitcoin/pull/50#issuecomment-2478468037
  32. in src/test/util/net.h:139 in 5766bbefa9 outdated
    135@@ -133,110 +136,228 @@ constexpr auto ALL_NETWORKS = std::array{
    136     Network::NET_INTERNAL,
    137 };
    138 
    139+std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext& random_context);
    


    jonatack commented at 2:31 pm on November 26, 2024:
    2f6ce54212926f7f8642dfa5021f4ff089a4de61 Suggest not needlessly moving this to save reviewer time.

    vasild commented at 9:47 am on December 24, 2024:
    done, also suggested in #30205 (review)
  33. jonatack commented at 2:45 pm on November 26, 2024: member

    Light ACK 5766bbefa94d40f8d37639c2752961617d7a262a

    Similar change to the larger one I reviewed in #26812.

  34. DrahtBot requested review from Sjors on Nov 26, 2024
  35. pinheadmz commented at 4:17 pm on December 13, 2024: member

    Concept ACK

    Using this in my http rewrite branch and will leave a few review comments next week

  36. test: put the generic parts from StaticContentsSock into a separate class
    This allows reusing them in other mocked implementations.
    f1864148c4
  37. test: add a mocked Sock that allows inspecting what has been Send() to it
    And also allows gradually providing the data to be returned by `Recv()`
    and sending and receiving net messages (`CNetMessage`).
    b448b01494
  38. vasild force-pushed on Dec 24, 2024
  39. vasild commented at 9:43 am on December 24, 2024: contributor
    5766bbefa9...b448b01494: avoid moving GetRandomNodeEvictionCandidates() as suggested in #30205 (review) and #30205 (review)
  40. Sjors commented at 1:35 pm on January 13, 2025: member
    re-ACK b448b014947093cd217dbde47c8fb9e6c2bc8ba3
  41. DrahtBot requested review from jonatack on Jan 13, 2025
  42. DrahtBot requested review from pinheadmz on Jan 13, 2025
  43. jonatack commented at 9:27 pm on January 15, 2025: member
    re-ACK b448b014947093cd217dbde47c8fb9e6c2bc8ba3
  44. in src/test/util/net.h:225 in b448b01494
    239-        return consume_bytes;
    240-    }
    241+    public:
    242+        /**
    243+         * Get bytes and remove them from the pipe.
    244+         * @param[in] buf Destination to write bytes to.
    


    vasild commented at 12:04 pm on January 28, 2025:

    nit: this is an out parameter, pointed by @pinheadmz in https://github.com/Sjors/bitcoin/pull/64#discussion_r1878686775 Diff that I applied locally and will be in next retouch (if there is next retouch):

    0    -+         * [@param](/bitcoin-bitcoin/contributor/param/)[in] buf Destination to write bytes to.
    1    ++         * [@param](/bitcoin-bitcoin/contributor/param/)[out] buf Destination to write bytes to.
    
  45. in src/test/util/net.cpp:209 in 4b58d55878 outdated
    204+
    205+bool StaticContentsSock::IsSelectable() const { return true; }
    206+
    207+bool StaticContentsSock::Wait(std::chrono::milliseconds timeout,
    208+          Event requested,
    209+          Event* occurred) const
    


    pinheadmz commented at 8:17 pm on January 31, 2025:

    4b58d55878db55372d1b09de49c6caf363fe3c06

    just noticing in the code-move the default argument was lost:

    Event* occurred = nullptr


    vasild commented at 11:02 am on February 10, 2025:
    It is still there in the .h file.
  46. in src/test/util/net.cpp:241 in f1864148c4 outdated
    238+}
    239+
    240+StaticContentsSock& StaticContentsSock::operator=(Sock&& other)
    241+{
    242+    assert(false && "Move of Sock into StaticContentsSock not allowed.");
    243+    return *this;
    


    pinheadmz commented at 8:24 pm on January 31, 2025:

    f1864148c4a091afd63be75bc1ff14ae93383523

    unreachable right? you still need a return statement to satisfy the compiler?


    vasild commented at 10:54 am on February 10, 2025:
    Yes. Flow will not go after the assert.
  47. pinheadmz approved
  48. pinheadmz commented at 8:39 pm on January 31, 2025: member

    ACK b448b014947093cd217dbde47c8fb9e6c2bc8ba3

    This adds a useful network testing utility without touching production code. I use both StaticContentsSocket and DynSock in testing an internal HTTP server in this branch and the API is clear and easy to use and produces expected results.

    reviewed each commit, built and ran unit and functional tests on macos/arm

     0-----BEGIN PGP SIGNED MESSAGE-----
     1Hash: SHA256
     2
     3ACK b448b014947093cd217dbde47c8fb9e6c2bc8ba3
     4-----BEGIN PGP SIGNATURE-----
     5
     6iQIzBAEBCAAdFiEE5hdzzW4BBA4vG9eM5+KYS2KJyToFAmedNCwACgkQ5+KYS2KJ
     7yTrV6hAAkb5EP5RajBWNWW/LD/EXEFVZPqoz4Uc4c7Gt5oQVVMYpZLUFaH0TpXdq
     8PGZn1g4ietO+tB0tqXDHRE9Agivu9zSBQkygcKPutJXEspWZqWO9h5Ipl5YbfbWf
     9QMOcwD18ffDD9ufMgjWYQWfuSmRAbG6HRk3rDRtL7PCrAOVb7VuxgnqkKuWHoIgZ
    10Hw2e82XkLdoAyvt3nPdLeizuD+KIGTh1LoUz0t3nl7mxMHbJIXoB7Cw74DW7dH3O
    11Pllieg52bMXlsPoXDWNSivQnBl3nKQu1cf5kU2bt/me6bOpfyELxLDU6dWm2glYH
    12BLSDyJnjUC9o5Qry2w35DqptRDWTSVaWSUiYbrpsoL0w41dhnKHVaii/SEFVHmw8
    13q9nPTeu3ndm0dFkaVa2/ApvDlqdctOWeMKzCiWOgIQ33EwUbKe61dWWnzbUyvSZQ
    14oF99vTZHWMbtKxivFTU1g2iYF7V11xhbiUKnnZvQX0P8HVXWNsEzYfH997PZarFi
    15GpIjCb8BtpXGIK7QSC4R0LZ00rZTP7Qx4Tzm3UvqVNL5EwRACGTtO6I+uuoLMnNs
    165N+ffxZ1pCv0pJoUUzFNhLidZWaQe38fniRI9wXC6qPb2zYg0bsHUPkaa0BFQNcR
    17wPnbepCdm36AGzcxR/l7H9NKMiUfs4LsWTHsku6OWcgecdj/9DQ=
    18=c0ag
    19-----END PGP SIGNATURE-----
    

    pinheadmz’s public key is on keybase

  49. ryanofsky assigned ryanofsky on Feb 10, 2025
  50. ryanofsky merged this on Feb 10, 2025
  51. ryanofsky closed this on Feb 10, 2025

  52. ryanofsky commented at 1:51 pm on February 10, 2025: contributor
    Merged this, and one thing I was curious about looking at the code was if it makes sense to replace void* buf, size_t len which is used many places with std::span<std::byte> here? That seems like could make it safer or easier to use
  53. vasild deleted the branch on Feb 11, 2025
  54. vasild commented at 12:29 pm on February 11, 2025: contributor

    That would be DynSock::Pipe::GetBytes() and DynSock::Pipe::PushBytes(). Sock::Send() and Sock::Recv() also use raw pointer + length, but they were not introduced in this PR and, more importantly, are supposed to mimic 1:1 the send(2) and recv(2) syscalls, in return value, arguments and semantic.

    For GetBytes() and PushBytes() the change would be:

      0diff --git i/src/test/util/net.h w/src/test/util/net.h
      1index 3e717341d8..a28c442035 100644
      2--- i/src/test/util/net.h
      3+++ w/src/test/util/net.h
      4@@ -17,18 +17,20 @@
      5 #include <sync.h>
      6 #include <util/sock.h>
      7 
      8 #include <algorithm>
      9 #include <array>
     10 #include <cassert>
     11+#include <cstddef>
     12 #include <chrono>
     13 #include <condition_variable>
     14 #include <cstdint>
     15 #include <cstring>
     16 #include <memory>
     17 #include <optional>
     18+#include <span>
     19 #include <string>
     20 #include <unordered_map>
     21 #include <vector>
     22 
     23 class FastRandomContext;
     24 
     25@@ -218,30 +220,29 @@ public:
     26     class Pipe
     27     {
     28     public:
     29         /**
     30          * Get bytes and remove them from the pipe.
     31          * [@param](/bitcoin-bitcoin/contributor/param/)[in] buf Destination to write bytes to.
     32-         * [@param](/bitcoin-bitcoin/contributor/param/)[in] len Write up to this number of bytes.
     33          * [@param](/bitcoin-bitcoin/contributor/param/)[in] flags Same as the flags of `recv(2)`. Just `MSG_PEEK` is honored.
     34          * [@return](/bitcoin-bitcoin/contributor/return/) The number of bytes written to `buf`. `0` if `Eof()` has been called.
     35          * If no bytes are available then `-1` is returned and `errno` is set to `EAGAIN`.
     36          */
     37-        ssize_t GetBytes(void* buf, size_t len, int flags = 0) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
     38+        ssize_t GetBytes(std::span<std::byte> buf, int flags = 0) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
     39 
     40         /**
     41          * Deserialize a `CNetMessage` and remove it from the pipe.
     42          * If not enough bytes are available then the function will wait. If parsing fails
     43          * or EOF is signaled to the pipe, then `std::nullopt` is returned.
     44          */
     45         std::optional<CNetMessage> GetNetMsg() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
     46 
     47         /**
     48          * Push bytes to the pipe.
     49          */
     50-        void PushBytes(const void* buf, size_t len) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
     51+        void PushBytes(std::span<const std::byte> buf) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
     52 
     53         /**
     54          * Construct and push CNetMessage to the pipe.
     55          */
     56         template <typename... Args>
     57         void PushNetMsg(const std::string& type, Args&&... payload) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
     58diff --git i/src/test/util/net.cpp w/src/test/util/net.cpp
     59index ddd96a5064..9f30f6413d 100644
     60--- i/src/test/util/net.cpp
     61+++ w/src/test/util/net.cpp
     62@@ -241,27 +241,27 @@ ssize_t StaticContentsSock::Recv(void* buf, size_t len, int flags) const
     63 StaticContentsSock& StaticContentsSock::operator=(Sock&& other)
     64 {
     65     assert(false && "Move of Sock into StaticContentsSock not allowed.");
     66     return *this;
     67 }
     68 
     69-ssize_t DynSock::Pipe::GetBytes(void* buf, size_t len, int flags)
     70+ssize_t DynSock::Pipe::GetBytes(std::span<std::byte> buf, int flags)
     71 {
     72     WAIT_LOCK(m_mutex, lock);
     73 
     74     if (m_data.empty()) {
     75         if (m_eof) {
     76             return 0;
     77         }
     78         errno = EAGAIN; // Same as recv(2) on a non-blocking socket.
     79         return -1;
     80     }
     81 
     82-    const size_t read_bytes{std::min(len, m_data.size())};
     83+    const size_t read_bytes{std::min(buf.size(), m_data.size())};
     84 
     85-    std::memcpy(buf, m_data.data(), read_bytes);
     86+    std::memcpy(buf.data(), m_data.data(), read_bytes);
     87     if ((flags & MSG_PEEK) == 0) {
     88         m_data.erase(m_data.begin(), m_data.begin() + read_bytes);
     89     }
     90 
     91     return read_bytes;
     92 }
     93@@ -301,17 +301,17 @@ std::optional<CNetMessage> DynSock::Pipe::GetNetMsg()
     94     if (reject) {
     95         return std::nullopt;
     96     }
     97     return std::make_optional<CNetMessage>(std::move(msg));
     98 }
     99 
    100-void DynSock::Pipe::PushBytes(const void* buf, size_t len)
    101+void DynSock::Pipe::PushBytes(std::span<const std::byte> buf)
    102 {
    103     LOCK(m_mutex);
    104-    const uint8_t* b = static_cast<const uint8_t*>(buf);
    105-    m_data.insert(m_data.end(), b, b + len);
    106+    const uint8_t* b = reinterpret_cast<const uint8_t*>(buf.data());
    107+    m_data.insert(m_data.end(), b, b + buf.size());
    108     m_cond.notify_all();
    109 }
    110 
    111 void DynSock::Pipe::Eof()
    112 {
    113     LOCK(m_mutex);
    114@@ -338,18 +338,18 @@ DynSock::~DynSock()
    115 {
    116     m_pipes->send.Eof();
    117 }
    118 
    119 ssize_t DynSock::Recv(void* buf, size_t len, int flags) const
    120 {
    121-    return m_pipes->recv.GetBytes(buf, len, flags);
    122+    return m_pipes->recv.GetBytes({reinterpret_cast<std::byte*>(buf), len}, flags);
    123 }
    124 
    125 ssize_t DynSock::Send(const void* buf, size_t len, int) const
    126 {
    127-    m_pipes->send.PushBytes(buf, len);
    128+    m_pipes->send.PushBytes({reinterpret_cast<const std::byte*>(buf), len});
    129     return len;
    130 }
    131 
    132 std::unique_ptr<Sock> DynSock::Accept(sockaddr* addr, socklen_t* addr_len) const
    133 {
    134     ZeroSock::Accept(addr, addr_len);
    135@@ -382,14 +382,14 @@ bool DynSock::WaitMany(std::chrono::milliseconds timeout, EventsPerSock& events_
    136                 events.occurred |= Sock::SEND;
    137                 at_least_one_event_occurred = true;
    138             }
    139 
    140             if ((events.requested & Sock::RECV) != 0) {
    141                 auto dyn_sock = reinterpret_cast<const DynSock*>(sock.get());
    142-                uint8_t b;
    143-                if (dyn_sock->m_pipes->recv.GetBytes(&b, 1, MSG_PEEK) == 1 || !dyn_sock->m_accept_sockets->Empty()) {
    144+                std::byte b[1];
    145+                if (dyn_sock->m_pipes->recv.GetBytes(b, MSG_PEEK) == 1 || !dyn_sock->m_accept_sockets->Empty()) {
    146                     events.occurred |= Sock::RECV;
    147                     at_least_one_event_occurred = true;
    148                 }
    149             }
    150         }
    

    What do you think? PR? Leave it alone?

  55. ryanofsky commented at 1:13 pm on February 11, 2025: contributor

    re: What do you think? PR? Leave it alone?

    Whichever you prefer, I’d happily review it. I wanted to make suggestion more as an idea to keep in mind when updating this code or writing new code, because of benefits described in #31272

    Also ideally the change would go further IMO: replace std::vector<uint8_t> with std::vector<std::byte>, replace memcpy with std::vector::assign, replace Recv/Send arguments with span instead of void*. I don’t think it is good to mimic mimic void pointers in recv/send calls, better to remove all direct recv/send calls and replace them them with simple Recv/Send wrappers that take spans.


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-02-23 00:12 UTC

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