Skip to content

net/p2p: change nScore and nBestScore data types to int64_t - #24090

Closed
RandyMcMillan wants to merge 1 commit into
bitcoin:masterfrom
RandyMcMillan:1642450390-issue-24049
Closed

RandyMcMillan wants to merge 1 commit into
bitcoin:masterfrom
RandyMcMillan:1642450390-issue-24049

Conversation

@RandyMcMillan

@RandyMcMillan RandyMcMillan commented Jan 17, 2022 •

Copy link
Copy Markdown
Contributor

Changing nScore and nBestScore to a fixed-width integer guarantees to be the same size on any architecture and avoids UB.

Addresses issue: #24049

@DrahtBot DrahtBot added the P2P label Jan 17, 2022

@shaavan shaavan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@mzumsande mzumsande left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Concept ACK

Comment thread src/net.cpp Resolved
@RandyMcMillan
RandyMcMillan force-pushed the 1642450390-issue-24049 branch from a1071e9 to 81d20b8 Compare January 18, 2022 22:46
@RandyMcMillan

Copy link
Copy Markdown
Contributor Author

81d20b8e2f0097867b234bdc02cb2d7aa95b4a51

Changed nBestScore type to int64_t per @mzumsande comment.

@RandyMcMillan RandyMcMillan changed the title net/p2p: change nScore data type to int64_t net/p2p: change nScore and nBestScore data types to int64_t Jan 18, 2022
@RandyMcMillan

Copy link
Copy Markdown
Contributor Author

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.

The scope of nBestScore is limited to the function:

bool GetLocal(CService& addr, const CNetAddr *paddrPeer)

https://github.com/bitcoin/bitcoin/blob/81d20b8e2f0097867b234bdc02cb2d7aa95b4a51/src/net.cpp#L137

which returns a boolean based on the evaluation:

return nBestScore >= 0;

@w0xlt w0xlt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

crACK 81d20b8

Changing nScore and nBestScore to a fixed-width integer guarantees to be the same size on any architecture and avoids UB.

@shaavan shaavan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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;
}

@RandyMcMillan
RandyMcMillan force-pushed the 1642450390-issue-24049 branch from 81d20b8 to 1e0703b Compare January 19, 2022 16:03
@RandyMcMillan

Copy link
Copy Markdown
Contributor Author

1e0703b

updated function:

static int GetnScore(const CService& addr)

return type to int64_t per @shaavan comment.

@theStack theStack left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Concept and code-review ACK 1e0703b

@RandyMcMillan

Copy link
Copy Markdown
Contributor Author

yes - bravo to @shaavan for his thorough code review!

@w0xlt w0xlt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

reACK 1e0703b

@shaavan shaavan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ACK 1e0703b

Changes since my last review:

  • Changed the return type of GetnScore from int to int64_t to match the nScore's type.

@maflcko

maflcko commented Jan 24, 2022

Copy link
Copy Markdown
Member

An alternative would be to saturate the int on overflow

@RandyMcMillan

Copy link
Copy Markdown
Contributor Author

agree - clamping the int is more prudent for memory usage.

@RandyMcMillan
RandyMcMillan marked this pull request as draft January 30, 2022 22:27
@RandyMcMillan

Copy link
Copy Markdown
Contributor Author

@w0xlt - I like the idea of saturating (clamping) this variable - open to suggestions/patches. Not sure which size is appropriate.

maflcko pushed a commit to bitcoin-core/gui that referenced this pull request Feb 21, 2022
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
@maflcko

maflcko commented Feb 21, 2022

Copy link
Copy Markdown
Member

Can rebase and use SaturatingAdd?

@luke-jr

luke-jr commented Mar 10, 2022

Copy link
Copy Markdown
Contributor

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 (>LOCAL_MAX) current score from all mapLocalHost entries? I think that would preserve the current behaviour.

guarantees to be the same size

Does this matter?

@RandyMcMillan

Copy link
Copy Markdown
Contributor Author

@luke-jr - if you have an idea - post a patch - I will apply it.

@jonatack jonatack left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

@luke-jr

luke-jr commented Mar 10, 2022

Copy link
Copy Markdown
Contributor

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.

@ViralTaco

ViralTaco commented Nov 2, 2022 •

Copy link
Copy Markdown

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 if (INT_MAX == it->second.nscore) need to have linear ([or] is it binomial?) time complexity? You're updating EVERY node in a map because the nScore in ONE of the is equal to INT_MAX? If you only intend to keep the ordering: there are many ways to do it in constant, time.

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.

@bitcoin bitcoin locked and limited conversation to collaborators Nov 2, 2023
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.

10 participants