Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#21349: Refactor long functions in entrynodes.c #173

Closed
wants to merge 11 commits into from

Conversation

Labels
None yet
Projects
None yet
4 participants
@rl1987
Copy link
Contributor

@rl1987 rl1987 commented Jun 22, 2018

https://trac.torproject.org/projects/tor/ticket/21349

@coveralls
Copy link

@coveralls coveralls commented Jun 22, 2018

Coverage Status

Coverage increased (+0.006%) to 59.897% when pulling 91de9c0 on rl1987:ticket21349 into eb784aa on torproject:master.

@@ -142,6 +142,9 @@
#include "or/node_st.h"
#include "or/origin_circuit_st.h"

#define REMOVE_UNLISTED_GUARDS_AFTER \
Copy link
Member

@asn-d6 asn-d6 Jun 27, 2018

Hmm, not sure if I like this macro doing multiplications and casting to time_t. Suggestion: Let's modify get_remove_unlisted_guards_after_days() to return exactly what we want, and let's do the multiplication in the log of sampled_guards_prune_obsolete_entries(). What do you think?

Copy link
Contributor Author

@rl1987 rl1987 Jul 2, 2018

In 64685c3 I made a function that returns the exact number we need here.

* some guards as listed or unlisted, and removing expired guards. */
STATIC void
sampled_guards_update_from_consensus(guard_selection_t *gs)
/** Enumerate <b>sampled_entry_guards</b> smartlist in <b>gs</b>.
Copy link
Member

@asn-d6 asn-d6 Jun 27, 2018

Not exactly the right function documentation style. For multi-line comments perhaps leave the /** line empty. See how other functions do it.

Copy link
Contributor Author

@rl1987 rl1987 Jul 2, 2018

Fixed in 0977049.

Copy link
Member

@asn-d6 asn-d6 Jul 2, 2018

LGTM.

}
log_info(LD_GUARD, "Updating sampled guard status based on received "
"consensus.");
tor_assert(n_changes);
Copy link
Member

@asn-d6 asn-d6 Jun 27, 2018

Suggestion: Instead of taking an n_changes pointer argument, let's switch this function (and the other one) from void to int, and have it return the value of n_changes. I think that will be cleaner, since there won't be a need to do pointer operations.

Copy link
Contributor Author

@rl1987 rl1987 Jul 2, 2018

Did so (with size_t) in eea7c81.

Copy link
Member

@asn-d6 asn-d6 Jul 2, 2018

LGTM.

int n_changes = 0;

/* First: Update listed/unlisted. */
sampled_guards_update_listedness(gs, &n_changes);
Copy link
Member

@asn-d6 asn-d6 Jun 27, 2018

Suggestion: Rename sampled_guards_update_listedness() to sampled_guards_update_consensus_presence(), since 'listedness' is not actually a word. Let me know if you like this, or you prefer something else, or prefer to leave it as is.

Copy link
Contributor Author

@rl1987 rl1987 Jul 2, 2018

Renamed in 72da8b4.

Copy link
Member

@asn-d6 asn-d6 Jul 2, 2018

LGTM.

/* First: Update listed/unlisted. */
sampled_guards_update_listedness(gs, &n_changes);

const time_t remove_if_unlisted_since =
Copy link
Member

@asn-d6 asn-d6 Jun 27, 2018

Perhaps we can do these computations inside sampled_guards_prune_obsolete_entries()? Or you like them here?

Copy link
Contributor Author

@rl1987 rl1987 Jul 2, 2018

I prefer sampled_guards_prune_obsolete_entries() to be parametrised by threshold values - it's more testable that way when we change our code.

Copy link
Member

@asn-d6 asn-d6 Jul 2, 2018

Sounds good!

@@ -93,6 +93,24 @@ static inline void smartlist_swap(smartlist_t *sl, int idx1, int idx2)
}
}

/** Return true if there is shallow equality between smartlists -
Copy link
Member

@asn-d6 asn-d6 Jun 27, 2018

Perhaps we should put this in container.c? That's where the other _eq() functions are. I'm not sure if I like functions in .h files, but it seems to be the case in container.h, so if you like it like this feel free to leave it as is.

Copy link
Contributor Author

@rl1987 rl1987 Jul 2, 2018

Moved to container.c in cda8adc.

Copy link
Member

@asn-d6 asn-d6 Jul 2, 2018

LGTM

*/
static inline int smartlist_shallow_eq(const smartlist_t *s1,
const smartlist_t *s2)
{
Copy link
Member

@asn-d6 asn-d6 Jun 27, 2018

Since we opted for a generic function here we should also check that s1 and s2 are non-NULL and return 0 if any of them are NULL.

Copy link
Contributor Author

@rl1987 rl1987 Jul 2, 2018

Introduced some extra checks in cda8adc.

Copy link
Member

@asn-d6 asn-d6 Jul 2, 2018

LGTM

* i.e. all indices correspond to exactly same object (pointer
* values are matching). Otherwise, return false.
*/
static inline int smartlist_shallow_eq(const smartlist_t *s1,
Copy link
Member

@asn-d6 asn-d6 Jun 27, 2018

Optional suggestion: We can make this function return bool instead of int.

Copy link
Contributor Author

@rl1987 rl1987 Jul 2, 2018

We generally don't use bool in the codebase, so I'll rather use int, like we do pretty much always.

Copy link
Member

@asn-d6 asn-d6 Jul 2, 2018

stdbool was only recently added to the codebase that's why it's not yet used extensively. In any case, I'm fine with int as well.

SAMPLE_EXCLUDE_PENDING |
flags);
if (chosen_guard == NULL) {
log_info(LD_GUARD, "Absolutely no sampled guards were available. "
Copy link
Member

@asn-d6 asn-d6 Jun 27, 2018

Suggestion: Perhaps we can pull this mark all guards as maybe reachable logic out of this function, and into the end of select_entry_guard_for_circuit() to make it clear that this is the end of the algorithm. We could do this in a separate commit in the end, if you like the idea.

Copy link
Contributor Author

@rl1987 rl1987 Jul 2, 2018

Did so in d02b2f1.

Copy link
Member

@asn-d6 asn-d6 Jul 2, 2018

LGTM!

@@ -0,0 +1,6 @@
o Code simplification and refactoring:
Copy link
Member

@asn-d6 asn-d6 Jun 27, 2018

General comment: This branch needs to be rebased to the latest master, which contains the various modularization structure improvements. Let me know if you don't want to do this, and I will do it myself.

Copy link
Contributor Author

@rl1987 rl1987 Jul 2, 2018

I will do this once I address all the things you mentioned.

@tlyu
Copy link
Contributor

@tlyu tlyu commented Jul 25, 2018

Superseded by #246.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment