mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-09 19:37:27 -03:00
net: make CaptureMessage() mockable
Rename `CaptureMessage()` to `CaptureMessageToFile()` and introduce a `std::function` variable called `CaptureMessage` whose value can be changed by unit tests, should they need to inspect message contents.
This commit is contained in:
parent
43868ba416
commit
3cb9d9c861
3 changed files with 23 additions and 3 deletions
11
src/net.cpp
11
src/net.cpp
|
@ -3085,7 +3085,10 @@ uint64_t CConnman::CalculateKeyedNetGroup(const CAddress& ad) const
|
|||
return GetDeterministicRandomizer(RANDOMIZER_ID_NETGROUP).Write(vchNetGroup.data(), vchNetGroup.size()).Finalize();
|
||||
}
|
||||
|
||||
void CaptureMessage(const CAddress& addr, const std::string& msg_type, const Span<const unsigned char>& data, bool is_incoming)
|
||||
void CaptureMessageToFile(const CAddress& addr,
|
||||
const std::string& msg_type,
|
||||
const Span<const unsigned char>& data,
|
||||
bool is_incoming)
|
||||
{
|
||||
// Note: This function captures the message at the time of processing,
|
||||
// not at socket receive/send time.
|
||||
|
@ -3112,3 +3115,9 @@ void CaptureMessage(const CAddress& addr, const std::string& msg_type, const Spa
|
|||
ser_writedata32(f, size);
|
||||
f.write(AsBytes(data));
|
||||
}
|
||||
|
||||
std::function<void(const CAddress& addr,
|
||||
const std::string& msg_type,
|
||||
const Span<const unsigned char>& data,
|
||||
bool is_incoming)>
|
||||
CaptureMessage = CaptureMessageToFile;
|
||||
|
|
13
src/net.h
13
src/net.h
|
@ -31,6 +31,7 @@
|
|||
#include <condition_variable>
|
||||
#include <cstdint>
|
||||
#include <deque>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
@ -1272,7 +1273,17 @@ private:
|
|||
};
|
||||
|
||||
/** Dump binary message to file, with timestamp */
|
||||
void CaptureMessage(const CAddress& addr, const std::string& msg_type, const Span<const unsigned char>& data, bool is_incoming);
|
||||
void CaptureMessageToFile(const CAddress& addr,
|
||||
const std::string& msg_type,
|
||||
const Span<const unsigned char>& data,
|
||||
bool is_incoming);
|
||||
|
||||
/** Defaults to `CaptureMessageToFile()`, but can be overridden by unit tests. */
|
||||
extern std::function<void(const CAddress& addr,
|
||||
const std::string& msg_type,
|
||||
const Span<const unsigned char>& data,
|
||||
bool is_incoming)>
|
||||
CaptureMessage;
|
||||
|
||||
struct NodeEvictionCandidate
|
||||
{
|
||||
|
|
|
@ -20,7 +20,7 @@ LENGTH_SIZE = 4
|
|||
MSGTYPE_SIZE = 12
|
||||
|
||||
def mini_parser(dat_file):
|
||||
"""Parse a data file created by CaptureMessage.
|
||||
"""Parse a data file created by CaptureMessageToFile.
|
||||
|
||||
From the data file we'll only check the structure.
|
||||
|
||||
|
|
Loading…
Reference in a new issue