refactor: Minor refactoring of get_close_nodes functions.

Avoiding passing down the entire DHT struct pointer to the inner
functions makes it possible in the future to write unit tests without
having to construct a full DHT object.
This commit is contained in:
iphydf 2024-01-10 12:57:30 +00:00
parent ebc9643862
commit c66e10fb7a
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9
4 changed files with 47 additions and 25 deletions

View File

@ -1 +1 @@
a8e6d6d075090f4e6d27f59dd2e859a152948b3fac7f0b073386172339ec5d8d /usr/local/bin/tox-bootstrapd
7e86d4f1c4aadce01a03153f2101ac1486f6de65f824b7b0cccbbfbf832c180a /usr/local/bin/tox-bootstrapd

View File

@ -781,10 +781,11 @@ bool add_to_list(
* helper for `get_close_nodes()`. argument list is a monster :D
*/
non_null()
static void get_close_nodes_inner(uint64_t cur_time, const uint8_t *public_key, Node_format *nodes_list,
Family sa_family, const Client_data *client_list, uint32_t client_list_length,
uint32_t *num_nodes_ptr, bool is_lan,
bool want_announce)
static void get_close_nodes_inner(
uint64_t cur_time, const uint8_t *public_key,
Node_format *nodes_list, uint32_t *num_nodes_ptr,
Family sa_family, const Client_data *client_list, uint32_t client_list_length,
bool is_lan, bool want_announce)
{
if (!net_family_is_ipv4(sa_family) && !net_family_is_ipv6(sa_family) && !net_family_is_unspec(sa_family)) {
return;
@ -851,28 +852,44 @@ static void get_close_nodes_inner(uint64_t cur_time, const uint8_t *public_key,
* want_announce: return only nodes which implement the dht announcements protocol.
*/
non_null()
static int get_somewhat_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list,
Family sa_family, bool is_lan, bool want_announce)
static int get_somewhat_close_nodes(
uint64_t cur_time, const uint8_t *public_key, Node_format *nodes_list,
Family sa_family, const Client_data *close_clientlist,
const DHT_Friend *friends_list, uint16_t friends_list_size,
bool is_lan, bool want_announce)
{
uint32_t num_nodes = 0;
get_close_nodes_inner(dht->cur_time, public_key, nodes_list, sa_family,
dht->close_clientlist, LCLIENT_LIST, &num_nodes, is_lan, want_announce);
memset(nodes_list, 0, MAX_SENT_NODES * sizeof(Node_format));
for (uint32_t i = 0; i < dht->num_friends; ++i) {
get_close_nodes_inner(dht->cur_time, public_key, nodes_list, sa_family,
dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS,
&num_nodes, is_lan, want_announce);
uint32_t num_nodes = 0;
get_close_nodes_inner(
cur_time, public_key,
nodes_list, &num_nodes,
sa_family, close_clientlist, LCLIENT_LIST,
is_lan, want_announce);
for (uint16_t i = 0; i < friends_list_size; ++i) {
const DHT_Friend *dht_friend = &friends_list[i];
get_close_nodes_inner(
cur_time, public_key,
nodes_list, &num_nodes,
sa_family, dht_friend->client_list, MAX_FRIEND_CLIENTS,
is_lan, want_announce);
}
return num_nodes;
}
int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, Family sa_family,
bool is_lan, bool want_announce)
int get_close_nodes(
const DHT *dht, const uint8_t *public_key,
Node_format *nodes_list, Family sa_family,
bool is_lan, bool want_announce)
{
memset(nodes_list, 0, MAX_SENT_NODES * sizeof(Node_format));
return get_somewhat_close_nodes(dht, public_key, nodes_list, sa_family,
is_lan, want_announce);
return get_somewhat_close_nodes(
dht->cur_time, public_key, nodes_list,
sa_family, dht->close_clientlist,
dht->friends_list, dht->num_friends,
is_lan, want_announce);
}
typedef struct DHT_Cmp_Data {

View File

@ -384,18 +384,20 @@ void set_announce_node(DHT *dht, const uint8_t *public_key);
#endif
/**
* Get the (maximum MAX_SENT_NODES) closest nodes to public_key we know
* @brief Get the (maximum MAX_SENT_NODES) closest nodes to public_key we know
* and put them in nodes_list (must be MAX_SENT_NODES big).
*
* sa_family = family (IPv4 or IPv6) (0 if we don't care)?
* is_LAN = return some LAN ips (true or false)
* want_announce: return only nodes which implement the dht announcements protocol.
* @param sa_family family (IPv4 or IPv6) (0 if we don't care)?
* @param is_lan return some LAN ips (true or false).
* @param want_announce return only nodes which implement the dht announcements protocol.
*
* @return the number of nodes returned.
*/
non_null()
int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, Family sa_family,
bool is_lan, bool want_announce);
int get_close_nodes(
const DHT *dht, const uint8_t *public_key,
Node_format *nodes_list, Family sa_family,
bool is_lan, bool want_announce);
/** @brief Put up to max_num nodes in nodes from the random friends.

View File

@ -92,6 +92,9 @@ struct TCP_Server {
BS_List accepted_key_list;
};
static_assert(sizeof(TCP_Server) < 7 * 1024 * 1024,
"TCP_Server struct should not grow more; it's already 6MB");
const uint8_t *tcp_server_public_key(const TCP_Server *tcp_server)
{
return tcp_server->public_key;