|
6 | 6 | #define BITCOIN_TEST_UTIL_NET_H
|
7 | 7 |
|
8 | 8 | #include <compat/compat.h>
|
| 9 | +#include <netmessagemaker.h> |
9 | 10 | #include <net.h>
|
10 | 11 | #include <net_permissions.h>
|
11 | 12 | #include <net_processing.h>
|
|
19 | 20 | #include <array>
|
20 | 21 | #include <cassert>
|
21 | 22 | #include <chrono>
|
| 23 | +#include <condition_variable> |
22 | 24 | #include <cstdint>
|
23 | 25 | #include <cstring>
|
24 | 26 | #include <memory>
|
| 27 | +#include <optional> |
25 | 28 | #include <string>
|
26 | 29 | #include <unordered_map>
|
27 | 30 | #include <vector>
|
@@ -204,6 +207,157 @@ class StaticContentsSock : public ZeroSock
|
204 | 207 | mutable size_t m_consumed{0};
|
205 | 208 | };
|
206 | 209 |
|
| 210 | +/** |
| 211 | + * A mocked Sock alternative that allows providing the data to be returned by Recv() |
| 212 | + * and inspecting the data that has been supplied to Send(). |
| 213 | + */ |
| 214 | +class DynSock : public ZeroSock |
| 215 | +{ |
| 216 | +public: |
| 217 | + /** |
| 218 | + * Unidirectional bytes or CNetMessage queue (FIFO). |
| 219 | + */ |
| 220 | + class Pipe |
| 221 | + { |
| 222 | + public: |
| 223 | + /** |
| 224 | + * Get bytes and remove them from the pipe. |
| 225 | + * @param[in] buf Destination to write bytes to. |
| 226 | + * @param[in] len Write up to this number of bytes. |
| 227 | + * @param[in] flags Same as the flags of `recv(2)`. Just `MSG_PEEK` is honored. |
| 228 | + * @return The number of bytes written to `buf`. `0` if `Eof()` has been called. |
| 229 | + * If no bytes are available then `-1` is returned and `errno` is set to `EAGAIN`. |
| 230 | + */ |
| 231 | + ssize_t GetBytes(void* buf, size_t len, int flags = 0) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex); |
| 232 | + |
| 233 | + /** |
| 234 | + * Deserialize a `CNetMessage` and remove it from the pipe. |
| 235 | + * If not enough bytes are available then the function will wait. If parsing fails |
| 236 | + * or EOF is signaled to the pipe, then `std::nullopt` is returned. |
| 237 | + */ |
| 238 | + std::optional<CNetMessage> GetNetMsg() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex); |
| 239 | + |
| 240 | + /** |
| 241 | + * Push bytes to the pipe. |
| 242 | + */ |
| 243 | + void PushBytes(const void* buf, size_t len) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex); |
| 244 | + |
| 245 | + /** |
| 246 | + * Construct and push CNetMessage to the pipe. |
| 247 | + */ |
| 248 | + template <typename... Args> |
| 249 | + void PushNetMsg(const std::string& type, Args&&... payload) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex); |
| 250 | + |
| 251 | + /** |
| 252 | + * Signal end-of-file on the receiving end (`GetBytes()` or `GetNetMsg()`). |
| 253 | + */ |
| 254 | + void Eof() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex); |
| 255 | + |
| 256 | + private: |
| 257 | + /** |
| 258 | + * Return when there is some data to read or EOF has been signaled. |
| 259 | + * @param[in,out] lock Unique lock that must have been derived from `m_mutex` by `WAIT_LOCK(m_mutex, lock)`. |
| 260 | + */ |
| 261 | + void WaitForDataOrEof(UniqueLock<Mutex>& lock) EXCLUSIVE_LOCKS_REQUIRED(m_mutex); |
| 262 | + |
| 263 | + Mutex m_mutex; |
| 264 | + std::condition_variable m_cond; |
| 265 | + std::vector<uint8_t> m_data GUARDED_BY(m_mutex); |
| 266 | + bool m_eof GUARDED_BY(m_mutex){false}; |
| 267 | + }; |
| 268 | + |
| 269 | + struct Pipes { |
| 270 | + Pipe recv; |
| 271 | + Pipe send; |
| 272 | + }; |
| 273 | + |
| 274 | + /** |
| 275 | + * A basic thread-safe queue, used for queuing sockets to be returned by Accept(). |
| 276 | + */ |
| 277 | + class Queue |
| 278 | + { |
| 279 | + public: |
| 280 | + using S = std::unique_ptr<DynSock>; |
| 281 | + |
| 282 | + void Push(S s) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex) |
| 283 | + { |
| 284 | + LOCK(m_mutex); |
| 285 | + m_queue.push(std::move(s)); |
| 286 | + } |
| 287 | + |
| 288 | + std::optional<S> Pop() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex) |
| 289 | + { |
| 290 | + LOCK(m_mutex); |
| 291 | + if (m_queue.empty()) { |
| 292 | + return std::nullopt; |
| 293 | + } |
| 294 | + S front{std::move(m_queue.front())}; |
| 295 | + m_queue.pop(); |
| 296 | + return front; |
| 297 | + } |
| 298 | + |
| 299 | + bool Empty() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex) |
| 300 | + { |
| 301 | + LOCK(m_mutex); |
| 302 | + return m_queue.empty(); |
| 303 | + } |
| 304 | + |
| 305 | + private: |
| 306 | + mutable Mutex m_mutex; |
| 307 | + std::queue<S> m_queue GUARDED_BY(m_mutex); |
| 308 | + }; |
| 309 | + |
| 310 | + /** |
| 311 | + * Create a new mocked sock. |
| 312 | + * @param[in] pipes Send/recv pipes used by the Send() and Recv() methods. |
| 313 | + * @param[in] accept_sockets Sockets to return by the Accept() method. |
| 314 | + */ |
| 315 | + explicit DynSock(std::shared_ptr<Pipes> pipes, std::shared_ptr<Queue> accept_sockets); |
| 316 | + |
| 317 | + ~DynSock(); |
| 318 | + |
| 319 | + ssize_t Recv(void* buf, size_t len, int flags) const override; |
| 320 | + |
| 321 | + ssize_t Send(const void* buf, size_t len, int) const override; |
| 322 | + |
| 323 | + std::unique_ptr<Sock> Accept(sockaddr* addr, socklen_t* addr_len) const override; |
| 324 | + |
| 325 | + bool Wait(std::chrono::milliseconds timeout, |
| 326 | + Event requested, |
| 327 | + Event* occurred = nullptr) const override; |
| 328 | + |
| 329 | + bool WaitMany(std::chrono::milliseconds timeout, EventsPerSock& events_per_sock) const override; |
| 330 | + |
| 331 | +private: |
| 332 | + DynSock& operator=(Sock&&) override; |
| 333 | + |
| 334 | + std::shared_ptr<Pipes> m_pipes; |
| 335 | + std::shared_ptr<Queue> m_accept_sockets; |
| 336 | +}; |
| 337 | + |
| 338 | +template <typename... Args> |
| 339 | +void DynSock::Pipe::PushNetMsg(const std::string& type, Args&&... payload) |
| 340 | +{ |
| 341 | + auto msg = NetMsg::Make(type, std::forward<Args>(payload)...); |
| 342 | + V1Transport transport{NodeId{0}}; |
| 343 | + |
| 344 | + const bool queued{transport.SetMessageToSend(msg)}; |
| 345 | + assert(queued); |
| 346 | + |
| 347 | + LOCK(m_mutex); |
| 348 | + |
| 349 | + for (;;) { |
| 350 | + const auto& [bytes, _more, _msg_type] = transport.GetBytesToSend(/*have_next_message=*/true); |
| 351 | + if (bytes.empty()) { |
| 352 | + break; |
| 353 | + } |
| 354 | + m_data.insert(m_data.end(), bytes.begin(), bytes.end()); |
| 355 | + transport.MarkBytesSent(bytes.size()); |
| 356 | + } |
| 357 | + |
| 358 | + m_cond.notify_all(); |
| 359 | +} |
| 360 | + |
207 | 361 | std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext& random_context);
|
208 | 362 |
|
209 | 363 | #endif // BITCOIN_TEST_UTIL_NET_H
|
0 commit comments