Skip to content

p2p: Headers-sync followups - #25960

Merged
fanquake merged 3 commits into
bitcoin:masterfrom
sdaftuar:2022-08-headers-sync-followups
Sep 1, 2022
Merged

fanquake merged 3 commits into
bitcoin:masterfrom
sdaftuar:2022-08-headers-sync-followups

Conversation

@sdaftuar

@sdaftuar sdaftuar commented Aug 30, 2022 •

Copy link
Copy Markdown
Member

Remove BCLog::HEADERSSYNC and move all headerssync logging to BCLog::NET.

Bypass headers anti-DoS checks for NoBan peers

Also fix a typo that was introduced in PR25717.

@sdaftuar

sdaftuar commented Aug 30, 2022 •

Copy link
Copy Markdown
Member Author

@ajtowns @instagibbs These should address the remaining review items that came up in #25717 (comment)

@fanquake fanquake added the P2P label Aug 30, 2022
@jonatack

jonatack commented Aug 30, 2022 •

Copy link
Copy Markdown
Member
Some initial WIP follow-up suggestions on first look-over of #25717.

diff --git a/src/net_processing.cpp b/src/net_processing.cpp
index c4a1b5fa15..1158b6c06a 100644
--- a/src/net_processing.cpp
+++ b/src/net_processing.cpp
@@ -634,23 +634,23 @@ private:
      *              acceptance by the caller).
      */
     bool IsContinuationOfLowWorkHeadersSync(Peer& peer, CNode& pfrom,
-            std::vector<CBlockHeader>& headers)
+                                            std::vector<CBlockHeader>& headers)
         EXCLUSIVE_LOCKS_REQUIRED(peer.m_headers_sync_mutex, !m_headers_presync_mutex);
     /** Check work on a headers chain to be processed, and if insufficient,
      * initiate our anti-DoS headers sync mechanism.
      *
-     * @param[in]   peer                The peer whose headers we're processing.
-     * @param[in]   pfrom               CNode of the peer
-     * @param[in]   chain_start_header  Where these headers connect in our index.
-     * @param[in,out]   headers             The headers to be processed.
+     * @param[in]     peer                The peer whose headers we're processing.
+     * @param[in]     pfrom               CNode of the peer
+     * @param[in]     chain_start_header  Where these headers connect in our index.
+     * @param[in,out] headers             The headers to be processed.
      *
      * @return      True if chain was low work and a headers sync was
      *              initiated (and headers will be empty after calling); false
      *              otherwise.
      */
     bool TryLowWorkHeadersSync(Peer& peer, CNode& pfrom,
-                                  const CBlockIndex* chain_start_header,
-                                  std::vector<CBlockHeader>& headers)
+                               const CBlockIndex* chain_start_header,
+                               std::vector<CBlockHeader>& headers)
         EXCLUSIVE_LOCKS_REQUIRED(!peer.m_headers_sync_mutex, !m_peer_mutex, !m_headers_presync_mutex);
 
     /** Return true if the given header is an ancestor of
@@ -1482,7 +1482,7 @@ void PeerManagerImpl::ReattemptInitialBroadcast(CScheduler& scheduler)
 
 void PeerManagerImpl::FinalizeNode(const CNode& node)
 {
+    AssertLockNotHeld(m_headers_presync_mutex);
     NodeId nodeid = node.GetId();
     int misbehavior{0};
     {
@@ -2481,6 +2481,9 @@ bool PeerManagerImpl::CheckHeadersAreContinuous(const std::vector<CBlockHeader>&
 
 bool PeerManagerImpl::IsContinuationOfLowWorkHeadersSync(Peer& peer, CNode& pfrom, std::vector<CBlockHeader>& headers)
 {
+    AssertLockHeld(peer.m_headers_sync_mutex);
+    AssertLockNotHeld(m_headers_presync_mutex);
     if (peer.m_headers_sync) {
         auto result = peer.m_headers_sync->ProcessNextHeaders(headers, headers.size() == MAX_HEADERS_RESULTS);
         if (result.request_more) {
@@ -2494,13 +2497,12 @@ bool PeerManagerImpl::IsContinuationOfLowWorkHeadersSync(Peer& peer, CNode& pfro
                 // it may be possible to bypass this via compactblock
                 // processing, so check the result before logging just to be
                 // safe.
-                bool sent_getheaders = MaybeSendGetHeaders(pfrom, locator, peer);
-                if (sent_getheaders) {
+                if (MaybeSendGetHeaders(pfrom, locator, peer)) {
                     LogPrint(BCLog::NET, "more getheaders (from %s) to peer=%d\n",
-                            locator.vHave.front().ToString(), pfrom.GetId());
+                             locator.vHave.front().ToString(), pfrom.GetId());
                 } else {
                     LogPrint(BCLog::NET, "error sending next getheaders (from %s) to continue sync with peer=%d\n",
-                            locator.vHave.front().ToString(), pfrom.GetId());
+                             locator.vHave.front().ToString(), pfrom.GetId());
                 }
             }
         }
@@ -2567,6 +2569,9 @@ bool PeerManagerImpl::IsContinuationOfLowWorkHeadersSync(Peer& peer, CNode& pfro
 
 bool PeerManagerImpl::TryLowWorkHeadersSync(Peer& peer, CNode& pfrom, const CBlockIndex* chain_start_header, std::vector<CBlockHeader>& headers)
 {
+    AssertLockNotHeld(peer.m_headers_sync_mutex);
+    AssertLockNotHeld(m_peer_mutex);
+    AssertLockNotHeld(m_headers_presync_mutex);
     // Calculate the total work on this chain.
     arith_uint256 total_work = chain_start_header->nChainWork + CalculateHeadersWork(headers);
 
@@ -2609,6 +2614,7 @@ bool PeerManagerImpl::TryLowWorkHeadersSync(Peer& peer, CNode& pfrom, const CBlo
 
 bool PeerManagerImpl::IsAncestorOfBestHeaderOrTip(const CBlockIndex* header)
 {
+    AssertLockHeld(cs_main);
     if (header == nullptr) {
         return false;
     } else if (m_chainman.m_best_header != nullptr && header == m_chainman.m_best_header->GetAncestor(header->nHeight)) {
@@ -2767,7 +2773,7 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer,
                                             std::vector<CBlockHeader>&& headers,
                                             bool via_compact_block)
 {
+    AssertLockNotHeld(m_headers_presync_mutex);
     size_t nCount = headers.size();
 
     if (nCount == 0) {
@@ -3166,9 +3172,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
                                      const std::chrono::microseconds time_received,
                                      const std::atomic<bool>& interruptMsgProc)
 {
+    AssertLockNotHeld(m_headers_presync_mutex);
 
     LogPrint(BCLog::NET, "received: %s (%u bytes) peer=%d\n", SanitizeString(msg_type), vRecv.size(), pfrom.GetId());

Feel free to ignore. I also have a branch ready at master...jonatack:bitcoin:net-add-missing-thread-safety-runtime-assertions for all the net and net_processing code and mention above only the runtime lock assertions directly related to changes in #25717.

Edit: updated with additional missing lock assertions.

@sdaftuar
sdaftuar force-pushed the 2022-08-headers-sync-followups branch from 1206e2d to 2ad515a Compare August 30, 2022 17:41
@sdaftuar
sdaftuar force-pushed the 2022-08-headers-sync-followups branch from 2ad515a to 94af3e4 Compare August 30, 2022 18:13
Comment thread src/net_processing.cpp
// anti-DoS logic (this saves bandwidth when we connect to a trusted peer
// on startup).
if (pfrom.HasPermission(NetPermissionFlags::NoBan)) {
already_validated_work = true;

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.

nit: after this changetrustworthy_work or something could be more accurate, although I'm not sure it actually benefits the readers.

@Sjors

Sjors commented Aug 31, 2022 •

Copy link
Copy Markdown
Member

tACK 94af3e4

I checked that NoBan peers indeed sync in a single pass now.

The download permission might be more suitable for this (and it's implied by noban). In any case it's not a very useful optimisation, but I'm not opposed to it.

@ajtowns

ajtowns commented Aug 31, 2022

Copy link
Copy Markdown
Contributor

The download permission might be more suitable for this

For what it's worth, I interpret download as being more in the other direction -- "this peer can download from me, even if I'm in IBD" rather than "I feel safe downloading from this peer", so it seemed less suitable to me. But 🤷

Comment thread src/logging.h
#endif
UTIL = (1 << 25),
BLOCKSTORE = (1 << 26),
HEADERSSYNC = (1 << 27),

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.

You could make this a scripted diff:

-BEGIN VERIFY SCRIPT-
sed -i '/headerssync/Id' src/logging.h src/logging.cpp
sed -i 's/BCLog::HEADERSSYNC/BCLog::NET/g' $(git grep -l BCLog::HEADERSSYNC)
-END VERIFY SCRIPT-

@Sjors

Sjors commented Aug 31, 2022

Copy link
Copy Markdown
Member

We should probably document what we mean by these permissions :-)

@ajtowns

ajtowns commented Aug 31, 2022

Copy link
Copy Markdown
Contributor

We should probably document what we mean by these permissions :-)

They're documented in bitcoin-cli help getpeerinfo:

    "permissions" : [                     (json array) Any special permissions that have been granted to this peer
      "str",                              (string) bloomfilter (allow requesting BIP37 filtered blocks and transactions),
                                          noban (do not ban for misbehavior; implies download),
                                          forcerelay (relay transactions that are already in the mempool; implies relay),
                                          relay (relay even in -blocksonly mode, and unlimited transaction announcements),
                                          mempool (allow requesting BIP35 mempool contents),
                                          download (allow getheaders during IBD, no disconnect after maxuploadtarget limit),
                                          addr (responses to GETADDR avoid hitting the cache and contain random records with the most up-to-date info).
                                          
      ...
    ],

and in bitcoind -help under -whitebind.

@Sjors

Sjors commented Aug 31, 2022 •

Copy link
Copy Markdown
Member

Ok, in that case I tend to agree noban is better. I guess download should have been called upload, but that depends on the perspective :-)

@ajtowns

ajtowns commented Aug 31, 2022

Copy link
Copy Markdown
Contributor

ACK 94af3e4

1 similar comment
@sipa

sipa commented Aug 31, 2022

Copy link
Copy Markdown
Member

ACK 94af3e4

@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.

ACK 94af3e4

@fanquake fanquake added this to the 24.0 milestone Aug 31, 2022
@DrahtBot

Copy link
Copy Markdown
Contributor

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #23443 (p2p: Erlay support signaling by naumenkogs)

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.

@naumenkogs

Copy link
Copy Markdown
Contributor

ACK 94af3e4

@fanquake
fanquake merged commit 6ab8470 into bitcoin:master Sep 1, 2022
@bitcoin bitcoin locked and limited conversation to collaborators Sep 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants