MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / Interact

Method Interact

src/test/net_tests.cpp:1100–1146  ·  view source on GitHub ↗

Send/receive scheduled/available bytes and messages. * * This is the only function that interacts with the transport being tested; everything else is * scheduling things done by Interact(), or processing things learned by it. */

Source from the content-addressed store, hash-verified

1098 * scheduling things done by Interact(), or processing things learned by it.
1099 */
1100 InteractResult Interact()
1101 {
1102 std::vector<std::optional<CNetMessage>> ret;
1103 while (true) {
1104 bool progress{false};
1105 // Send bytes from m_to_send to the transport.
1106 if (!m_to_send.empty()) {
1107 std::span<const uint8_t> to_send = std::span{m_to_send}.first(1 + m_rng.randrange(m_to_send.size()));
1108 size_t old_len = to_send.size();
1109 if (!m_transport.ReceivedBytes(to_send)) {
1110 return std::nullopt; // transport error occurred
1111 }
1112 if (old_len != to_send.size()) {
1113 progress = true;
1114 m_to_send.erase(m_to_send.begin(), m_to_send.begin() + (old_len - to_send.size()));
1115 }
1116 }
1117 // Retrieve messages received by the transport.
1118 if (m_transport.ReceivedMessageComplete() && (!progress || m_rng.randbool())) {
1119 bool reject{false};
1120 auto msg = m_transport.GetReceivedMessage({}, reject);
1121 if (reject) {
1122 ret.emplace_back(std::nullopt);
1123 } else {
1124 ret.emplace_back(std::move(msg));
1125 }
1126 progress = true;
1127 }
1128 // Enqueue a message to be sent by the transport to us.
1129 if (!m_msg_to_send.empty() && (!progress || m_rng.randbool())) {
1130 if (m_transport.SetMessageToSend(m_msg_to_send.front())) {
1131 m_msg_to_send.pop_front();
1132 progress = true;
1133 }
1134 }
1135 // Receive bytes from the transport.
1136 const auto& [recv_bytes, _more, _msg_type] = m_transport.GetBytesToSend(!m_msg_to_send.empty());
1137 if (!recv_bytes.empty() && (!progress || m_rng.randbool())) {
1138 size_t to_receive = 1 + m_rng.randrange(recv_bytes.size());
1139 m_received.insert(m_received.end(), recv_bytes.begin(), recv_bytes.begin() + to_receive);
1140 progress = true;
1141 m_transport.MarkBytesSent(to_receive);
1142 }
1143 if (!progress) break;
1144 }
1145 return ret;
1146 }
1147
1148 /** Expose the cipher. */
1149 BIP324Cipher& GetCipher() { return m_cipher; }

Callers 1

BOOST_AUTO_TEST_CASEFunction · 0.80

Calls 15

randrangeMethod · 0.80
ReceivedBytesMethod · 0.80
randboolMethod · 0.80
GetReceivedMessageMethod · 0.80
SetMessageToSendMethod · 0.80
GetBytesToSendMethod · 0.80
MarkBytesSentMethod · 0.80
emptyMethod · 0.45
sizeMethod · 0.45
eraseMethod · 0.45
beginMethod · 0.45

Tested by

no test coverage detected