Repository navigation
net/p2p: change nScore and nBestScore data types to int64_t - #24090
RandyMcMillan wants to merge 1 commit into
Conversation
shaavan
left a comment
There was a problem hiding this comment.
Concept ACK
It makes sense to convert nScore to int64_t from int to avoid UB. However, I shall take a further look in the codebase to check if doing so might not be causing a yet unforeseen issue.
a1071e9 to
81d20b8
Compare
|
81d20b8e2f0097867b234bdc02cb2d7aa95b4a51 Changed nBestScore type to int64_t per @mzumsande comment. |
The scope of nBestScore is limited to the function: which returns a boolean based on the evaluation: return nBestScore >= 0; |
w0xlt
left a comment
There was a problem hiding this comment.
crACK 81d20b8
Changing nScore and nBestScore to a fixed-width integer guarantees to be the same size on any architecture and avoids UB.
shaavan
left a comment
There was a problem hiding this comment.
Since the type of nScore is changed to int64_t, I think it’s logical to change the return type of GetnScore from int to int64_t.
Line 194 in src/net.cpp:
static int GetnScore(const CService& addr)
{
LOCK(cs_mapLocalHost);
const auto it = mapLocalHost.find(addr);
return (it != mapLocalHost.end()) ? it->second.nScore : 0;
}
81d20b8 to
1e0703b
Compare
|
yes - bravo to @shaavan for his thorough code review! |
|
An alternative would be to saturate the int on overflow |
|
agree - clamping the int is more prudent for memory usage. |
|
@w0xlt - I like the idea of saturating (clamping) this variable - open to suggestions/patches. Not sure which size is appropriate. |
faa7d8a util: Add SaturatingAdd helper (MarcoFalke) Pull request description: Seems good to have this in the repo, as it might be needed to write cleaner code. For example: * bitcoin/bitcoin#24090 (comment) * bitcoin/bitcoin#23418 (comment) * ... ACKs for top commit: MarcoFalke: Added a test. Should be trivial to re-ACK with `git range-diff bitcoin-core/master fa90189cbf faa7d8a` klementtan: reACK faa7d8a vasild: ACK faa7d8a Tree-SHA512: d0e6efdba7dfcbdd16ab4539a7f5e45a97d17792e42586c3c52caaae3fc70612dc9e364359658de5de5718fb8c2a765a59ceb2230098355394fa067a9732bc2a
|
Can rebase and use |
|
Saturating means once an IP reaches max, it won't ever get swapped out for a newer IP, right? Maybe instead, when we would increment beyond max, we should subtract the lowest (
Does this matter? |
|
@luke-jr - if you have an idea - post a patch - I will apply it. |
jonatack
left a comment
There was a problem hiding this comment.
ACK 1e0703b code review, rebased, debug build, ran unit tests.
That said, changing the one line that increments nScore in SeenLocal() to use SaturatingAdd (recently added in #24224) looks to me like a preferable and more minimal change.
An additional reason might be that if any math operations to nScore are added in the future, the C++ tendency to auto-convert to int might be a good reason to leave nScore as int here, if I'm not confused and lacking caffeine.
|
My thought is something like this: --- a/src/net.cpp
+++ b/src/net.cpp
@@ -349,6 +349,20 @@ bool SeenLocal(const CService& addr)
LOCK(g_maplocalhost_mutex);
const auto it = mapLocalHost.find(addr);
if (it == mapLocalHost.end()) return false;
+ if (it->second.nScore == std::numeric_limits<int>::max()) {
+ int lowest_inc_score = std::numeric_limits<int>::max();
+ for (auto& localinfo : mapLocalHost) {
+ if (localinfo.second.nScore > LOCAL_MAX && localinfo.second.nScore < lowest_inc_score) {
+ lowest_inc_score = localinfo.second.nScore;
+ }
+ }
+ lowest_inc_score -= LOCAL_MAX;
+ for (auto& localinfo : mapLocalHost) {
+ if (localinfo.second.nScore > LOCAL_MAX) {
+ localinfo.second.nScore -= lowest_inc_score;
+ }
+ }
+ }
++it->second.nScore;
return true;
}But I'm not 100% sure of it yet. |
Does the code inside the All of that, and it happens to be a breaking change. ++reinterpret_cast<unsigned&>(it->second.nScore);Keeps everything unchanged, gets rid of the undefined behavior making it (until c++20: implementation specified; since c++20: well-defined) behavior. What do you think? EDIT: Removed a paragraph since the problem I described in it couldn't happen without a data race, anyway. |
Changing nScore and nBestScore to a fixed-width integer guarantees to be the same size on any architecture and avoids UB.
Addresses issue: #24049