-
Notifications
You must be signed in to change notification settings - Fork 284
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
DHT refactoring #498
DHT refactoring #498
Conversation
I have splitted on 4 commits to make review easier |
Do we want to expose |
@nurupo It will be fixed in another PR. I have started this PR because during update AF_INET I found a lot of not good (ok, sometimes it's bad 😄) code in DHT and decide to make refactor it in separate RP |
Just make sure you don't change the behaviour of DHT code. |
AFAIK this is the logic of |
Review status: 0 of 2 files reviewed at latest revision, 9 unresolved discussions. toxcore/DHT.c, line 527 at r1 (raw file):
This replaces use of public_key_cmp in the original with use of id_equal. This is good. Why not replace the others too? toxcore/DHT.c, line 538 at r1 (raw file):
Using macros to define functions makes it hard to look up the definition. Maybe only use a macro for the body, or keep it as is but add explicit function declarations? toxcore/DHT.c, line 618 at r1 (raw file):
The original code handled the case that an ip_port occurs multiple times in the list, updating the public key for each occurence, while the revised version does not. The same goes for repeated public keys. It is actually possible to have duplicate ip_ports, because the ip_port of an existing pk could be replaced by an ip_port which is already in the list, associated to a different pk. toxcore/DHT.c, line 1141 at r1 (raw file):
Why did AF_INET become TOX_AF_INET? toxcore/DHT.c, line 1148 at r1 (raw file):
Why not use unix_time() directly here? toxcore/DHT.c, line 1170 at r1 (raw file):
Could define i here, i.e. "for (uint32_t i = 0;". toxcore/DHT.c, line 1263 at r1 (raw file):
Here crypto_size includes CRYPTO_MAC_SIZE, but in earlier definitions it didn't. Better to be consistent? toxcore/DHT.c, line 1278 at r1 (raw file):
Why not include the second public key in crypto_size? toxcore/DHT.c, line 1552 at r1 (raw file):
Need to check that this index isn't -1! Return 0 if it is. Comments from Reviewable |
Above issues aside, this refactoring makes quite a bit of code substantially cleaner. |
Review status: 0 of 2 files reviewed at latest revision, 9 unresolved discussions. toxcore/DHT.c, line 527 at r1 (raw file): Previously, zugz (zugz) wrote…
Done. toxcore/DHT.c, line 538 at r1 (raw file): Previously, zugz (zugz) wrote…
Yes, I thought about it. But I'm nooby in C and don't know how to do it right. Done toxcore/DHT.c, line 618 at r1 (raw file): Previously, zugz (zugz) wrote…
Hm... As far as I understand, in old code, both toxcore/DHT.c, line 1141 at r1 (raw file): Previously, zugz (zugz) wrote…
Accidentally copied from another PR. Reverted toxcore/DHT.c, line 1148 at r1 (raw file): Previously, zugz (zugz) wrote…
I thought about it. For some reason it was calculated in the start of the function (maybe to avoid including toxcore/DHT.c, line 1170 at r1 (raw file): Previously, zugz (zugz) wrote…
IDK, which version of the C standard toxcore are using, but in old version you can't define variable in for init block toxcore/DHT.c, line 1263 at r1 (raw file): Previously, zugz (zugz) wrote…
toxcore/DHT.c, line 1278 at r1 (raw file): Previously, zugz (zugz) wrote…
Did not see first. Thank you 👍 toxcore/DHT.c, line 1552 at r1 (raw file): Previously, zugz (zugz) wrote…
Done. Comments from Reviewable |
Review status: 0 of 2 files reviewed at latest revision, 9 unresolved discussions. toxcore/DHT.c, line 1170 at r1 (raw file):
Done Comments from Reviewable |
Review status: 0 of 2 files reviewed at latest revision, 15 unresolved discussions. toxcore/DHT.c, line 618 at r1 (raw file): Previously, Diadlo (Polshakov Dmitry) wrote…
Sorry yes, you're quite right./IPP/ toxcore/DHT.c, line 519 at r2 (raw file):
You can drop the argument now (also in INDEX_OF_PORT_IP below) toxcore/DHT.c, line 1583 at r2 (raw file):
This is cute, but is it really a good idea? At least a comment in the Client_data struct would be in order, warning that assoc4 and assoc6 must be kept next to each other. toxcore/DHT.c, line 1854 at r2 (raw file):
You've inverted the loops. Unlikely to matter, but this is meant to be purely a refactoring. toxcore/DHT.c, line 1898 at r2 (raw file):
As above. toxcore/DHT.c, line 2071 at r2 (raw file):
Can make it "i < MAX_PUNCHING_PORTS". toxcore/DHT.c, line 2077 at r2 (raw file):
Problems with integer types here... -1 for the uint8_t sign ends up being used as 255. Comments from Reviewable |
Review status: 0 of 2 files reviewed at latest revision, 15 unresolved discussions, some commit checks failed. toxcore/DHT.c, line 519 at r2 (raw file): Previously, zugz (zugz) wrote…
Done. toxcore/DHT.c, line 1583 at r2 (raw file): Previously, zugz (zugz) wrote…
Comment with warning added. Is it ok? toxcore/DHT.c, line 1854 at r2 (raw file): Previously, zugz (zugz) wrote…
Done. toxcore/DHT.c, line 1898 at r2 (raw file): Previously, zugz (zugz) wrote…
Done. toxcore/DHT.c, line 2071 at r2 (raw file): Previously, zugz (zugz) wrote…
Done. toxcore/DHT.c, line 2077 at r2 (raw file): Previously, zugz (zugz) wrote…
Done. Comments from Reviewable |
Review status: 0 of 2 files reviewed at latest revision, 15 unresolved discussions, some commit checks failed. toxcore/DHT.c, line 1148 at r1 (raw file): Previously, Diadlo (Polshakov Dmitry) wrote…
I'm not sure what you mean. There's nothing in between the definition of temp_time and its use which could call update_unix_time(). toxcore/DHT.c, line 1583 at r2 (raw file): Previously, Diadlo (Polshakov Dmitry) wrote…
I meant a warning in the definition of the struct, in DHT.h. toxcore/DHT.c, line 1854 at r2 (raw file): Previously, Diadlo (Polshakov Dmitry) wrote…
Not pushed? toxcore/DHT.c, line 2077 at r2 (raw file): Previously, Diadlo (Polshakov Dmitry) wrote…
This still doesn't work. If you also make each uint32_t into uint16_t, then it should nearly agree with the original formula, except that delta should be Comments from Reviewable |
I can't get reviewables to let me comment on it, but there's
a whitespace error:
…--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -1517,7 +1513,7 @@ void tox_self_get_dht_id(const Tox *tox, uint8_t *dht_id)
{
if (dht_id) {
const Messenger *m = tox;
- memcpy(dht_id, m->dht->self_public_key, CRYPTO_PUBLIC_KEY_SIZE);
+ memcpy(dht_id , m->dht->self_public_key, CRYPTO_PUBLIC_KEY_SIZE);
}
}
|
@zugz About whitespace. It's not from my PR |
* Thursday, 2017-03-09 at 10:51 -0800 - Polshakov Dmitry <[email protected]>:
@zugz About whitespace. It's not from my PR
>From header: "Files changed 2". It's DHT.h and DHT.c
Oh sorry. Nevermind.
|
Review status: 0 of 2 files reviewed at latest revision, 15 unresolved discussions. toxcore/DHT.c, line 1148 at r1 (raw file): Previously, zugz (zugz) wrote…
toxcore/DHT.c, line 1583 at r2 (raw file): Previously, zugz (zugz) wrote…
Done. toxcore/DHT.c, line 1854 at r2 (raw file): Previously, zugz (zugz) wrote…
Done. toxcore/DHT.c, line 2077 at r2 (raw file): Previously, zugz (zugz) wrote…
Comments from Reviewable |
* Thursday, 2017-03-09 at 12:23 -0800 - Polshakov Dmitry <[email protected]>:
*[toxcore/DHT.c, line 1148 at r1](https://reviewable.io:443/reviews/toktok/c-toxcore/498#-KeeC_pWEama28QsixR-:-KeoPuNwkKnRVaxrUcZZ:b-tkj6cn) ([raw file](https://github.com/toktok/c-toxcore/blob/ff039d7f2ca9135337c6ab20820ed07c3ca92e57/toxcore/DHT.c#L1148)):*
<details><summary><i>Previously, zugz (zugz) wrote…</i></summary><blockquote>
I'm not sure what you mean. There's nothing in between the definition of temp_time and its use which could call update_unix_time().
</blockquote></details>
`index_of_client_pk` between
OK. That isn't a problem.
---
*[toxcore/DHT.c, line 2077 at r2](https://reviewable.io:443/reviews/toktok/c-toxcore/498#-KejnkTuVmNaV1oBnuk7:-KeoR5PdEOsCGf3OGTdm:b-ku0nwo) ([raw file](https://github.com/toktok/c-toxcore/blob/e89e1a3089652e3fd1297ca78933dfda1834d4bb/toxcore/DHT.c#L2077)):*
<details><summary><i>Previously, zugz (zugz) wrote…</i></summary><blockquote>
This still doesn't work. If you also make each uint32_t into uint16_t, then it should nearly agree with the original formula, except that delta should be
uint16_t delta = sign * (it / (2 * numports));
Arithmetic in C is tricky!
</blockquote></details>
`punching_index` is `uint32_t`, so it should be `uint32_t` too.
I think, that `uint32_t / (2 * uint16_t)` is `uint32_t`. Isn't it?
And why here should be extra braces?
The extra paretheses are needed because * and / have equal precedence
and are parsed left-to-right. Actually that should be all that's needed,
it's then fine to use uint32.
|
But |
* Thursday, 2017-03-09 at 13:16 -0800 - Polshakov Dmitry <[email protected]>:
But `abs(sign) == 1` so we can use `*` with direct order
With unsigned integers, it makes a difference.
Consider ((-1)*2)/2 vs (-1)*(2/2),
remembering that (-1)*2 == (UINT_MAX+1) - 2.
|
@zugz Thanks for explanation. I change formulas a bit |
What is the status of this PR? |
@iphydf Waiting for review? |
* Sunday, 2017-04-09 at 06:36 -0700 - Polshakov Dmitry <[email protected]>:
@iphydf Waiting for review?
The formulae in punch_holes seem to still need revising, as discussed
above.
|
@zugz Sorry, reviewable misplaced your comment and I didn't see it. Fixed |
<img class="emoji" title=":lgtm_strong:" alt=":lgtm_strong:" align="absmiddle" src="https://reviewable.io/lgtm_strong.png" height="20" width="61"/>
But given the potential for introducing nasty bugs, and my relative
unfamiliarity with C, I'd feel happier if someone else could look over
it too.
|
Should I split this PR into smaller ones?
|
Is there anything else? |
Review status: 0 of 2 files reviewed at latest revision, 16 unresolved discussions, some commit checks failed. toxcore/DHT.c, line 1029 at r6 (raw file):
Comments from Reviewable |
Review status: 0 of 2 files reviewed at latest revision, 16 unresolved discussions, some commit checks failed. toxcore/DHT.c, line 1029 at r6 (raw file): Previously, iphydf wrote…
Done. Comments from Reviewable |
@Diadlo can you squash the commits? |
@iphydf All in one? |
I can, but if some of changes will provide regression, it will be very hard to bisect it |
Having commits called 1, 2, 3, 4, is not useful. Either squash them into a single commit with a good description, or several commits, also with good descriptions. |
Ok, if they are separately useful commits then do keep them separate, but rename them from 1, 2, 3 to something that explains what that commit does. |
@iphydf Ok. It will take some time |
@iphydf Is it better now? |
- Replace 'for' with long body - Replace friend_number function on index_of_friend_pk - Replace client_in_nodelist on index_of_node_pk
Return value of 'addto_list' function is 'used' counter, which is uint32_t. It will be better to use the same return type.
To make 'client_or_ip_port_in_list' code easy to read
Review status: 0 of 2 files reviewed at latest revision, 16 unresolved discussions. Comments from Reviewable |
This change is