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

net: avoid overriding non-virtual ToString() in CService and use better naming #25619

Merged
merged 5 commits into from Feb 17, 2023

Conversation

vasild
Copy link
Contributor

@vasild vasild commented Jul 15, 2022

Before this PR we had the somewhat confusing combination of methods:

CNetAddr::ToStringIP()
CNetAddr::ToString() (duplicate of the above)
CService::ToStringIPPort()
CService::ToString() (duplicate of the above, overrides a non-virtual method from CNetAddr)
CService::ToStringPort()

Avoid overriding non-virtual methods.

"IP" stands for "Internet Protocol" and while sometimes "IP addresses" are called just "IPs", it is incorrect to call Tor or I2P addresses "IPs". Thus use "Addr" instead of "IP".

Change the above to:

CNetAddr::ToStringAddr()
CService::ToStringAddrPort()

The changes touch a lot of files, but are mostly mechanical.

@fanquake fanquake added the P2P label Jul 15, 2022
@maflcko
Copy link
Member

maflcko commented Jul 15, 2022

ToStringPort should probably be removed. The only place where it is used in the gui should probably use ToStringIPPort to properly display IPV6, no?

@vasild
Copy link
Contributor Author

vasild commented Jul 15, 2022

5b3c6bc788..7f4aa41772: simplify OptionsDialog::updateDefaultProxyNets() to not use CService::ToStringPort() and drop the latter.

ToStringPort should probably be removed.

Done! :)

The only place where it is used in the gui should probably use ToStringIPPort to properly display IPV6, no?

To be honest, I stared at that code in the GUI for a few minutes before opening this PR, trying to figure out what is going on and "how is it possible that it displays ipv6addr:port without [ ] !?", but it only used that (non-standard) representation to compare with another string internally, never displayed it. It could have used any other separator, e.g. "ipv6addr-port". Anyway, we have CService::operator==() for that, so I changed it.

@jonatack
Copy link
Contributor

Concept ACK

@jonatack
Copy link
Contributor

Light ACK, skimmed commits, debug build and unit tests. Will review more thoroughly.

@DrahtBot
Copy link
Contributor

DrahtBot commented Jul 16, 2022

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK sipa, jonatack, LarryRuane, achow101
Stale ACK Empact

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #27071 (Handle CJDNS from LookupSubNet() by vasild)
  • #26621 (refactor: Continue moving application data from CNode to Peer by dergoegge)
  • #26312 (Remove Sock::Get() and Sock::Sock() by vasild)
  • #26261 (p2p: cleanup LookupIntern, Lookup and LookupHost by brunoerg)
  • #25390 (sync: introduce a thread-safe generic container and use it to remove a bunch of "GlobalMutex"es by vasild)
  • #24545 (BIP324: Enable v2 P2P encrypted transport by dhruv)
  • #21878 (Make all networking code mockable by vasild)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@luke-jr
Copy link
Member

luke-jr commented Jul 16, 2022

nit: "host" seems preferable to "addr"

@maflcko
Copy link
Member

maflcko commented Jul 18, 2022

Maybe the scripted-diff can be made first to avoid repeatedly changing the same line of code several times?

@vasild
Copy link
Contributor Author

vasild commented Jul 18, 2022

7f4aa41772...b9ca6d0af3: do the scripted-diff first

Cumulative diff of the PR is unchanged before and after this force push:
before: git diff 85b601e043..7f4aa41772
after: git diff d806407173..b9ca6d0af3
(identical)

Maybe the scripted-diff can be made first to avoid repeatedly changing the same line of code several times?

I actually made it this way originally but then decided it is better to have the scripted-diff at the end so that it is optional and if it is contentious, then just drop the last commit (the scripted-diff). Changed now, since there are two more commits on top. For reviewing it shouldn't matter much since, I guess, reviewers are not staring carefully at the entire diff of scripted-diffs. It would matter for history chasing - to reduce the number of hops ("git blame"s) one has to do.

nit: "host" seems preferable to "addr"

Could be, can you elaborate a bit why? I did not change it yet because I am somewhat in favor of "addr" because "host" may be misunderstood to return a host (aka hostname): e.g. ToStringHost() could be misunderstood to return bar.foo.com instead of 34.206.39.153. I am fine either way, what do others think?

@vasild
Copy link
Contributor Author

vasild commented Jul 19, 2022

b9ca6d0af3...9854d3a820: rebase due to conflicts

@vasild
Copy link
Contributor Author

vasild commented Jul 27, 2022

9854d3a820...0a798d56c2: rebase due to conflicts

@vasild
Copy link
Contributor Author

vasild commented Jul 27, 2022

0a798d56c2...cbfa859028: rebase due to conflicts

@Empact
Copy link
Member

Empact commented Oct 13, 2022

Code Review ACK e5d1916

@vasild
Copy link
Contributor Author

vasild commented Oct 14, 2022

e5d1916425...caeec22664: rebase due to conflicts

Invalidates ACKs from @jonatack, @Empact

"IP" stands for "Internet Protocol".

"IP address" is sometimes shortened to just "IP" or "address".

However, Tor or I2P addresses are not "IP addresses", nor "IPs".

Thus, use "Addr" instead of "IP" for addresses that could be IP, Tor or
I2P addresses:

`CService::ToStringIPPort()` -> `CService::ToStringAddrPort()`
`CNetAddr::ToStringIP()` -> `CNetAddr::ToStringAddr()`

-BEGIN VERIFY SCRIPT-
sed -i 's/ToStringIPPort/ToStringAddrPort/g' -- $(git grep -l ToStringIPPort src)
sed -i 's/ToStringIP/ToStringAddr/g' -- $(git grep -l ToStringIP src)
-END VERIFY SCRIPT-
Both methods do the same thing, so simplify to having just one.

Further, `CService` inherits `CNetAddr` and `CService::ToString()`
overrides `CNetAddr::ToString()` but the latter is not virtual which
may be confusing. Avoid such a confusion by not having non-virtual
methods with the same names in inheritance.
Both methods do the same thing, so simplify to having just one.

`ToString()` is too generic in this case and it is unclear what it does,
given that there are similar methods:
`ToStringAddr()` (inherited from `CNetAddr`),
`ToStringPort()` and
`ToStringAddrPort()`.
Do not create strings and compare them to check if one `addr:port`
equals another. Use `CService::operator==()` instead.

`strDefaultProxyGUI` was assigned the same value 3 times. Instead save
it in `const CService ui_proxy` at the beginning of the function.
It is used only internally in `CService::ToStringAddrPort()`.
@vasild
Copy link
Contributor Author

vasild commented Dec 12, 2022

caeec22664...c9d548c91f: rebase due to conflicts

Previously invalidated ACKs from @jonatack, @Empact

@sipa
Copy link
Member

sipa commented Feb 13, 2023

utACK c9d548c

Copy link
Contributor

@jonatack jonatack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-ACK c9d548c only change since my previous reviews is rebase, but as a sanity check rebased to current master and at each commit quickly re-reviewed and re-verified clean build and green unit tests

Copy link
Contributor

@LarryRuane LarryRuane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK c9d548c
rebased to master, built, ran tests, ran bitcoind mainnet, code reviewed except I didn't completely understand the qt changes.

@achow101
Copy link
Member

ACK c9d548c

@achow101 achow101 merged commit 35fbc97 into bitcoin:master Feb 17, 2023
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Feb 17, 2023
@vasild vasild deleted the CNetAddr_ToString branch February 20, 2023 09:48
@bitcoin bitcoin locked and limited conversation to collaborators Feb 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants