Repository navigation
p2p: Headers-sync followups - #25960
Conversation
|
@ajtowns @instagibbs These should address the remaining review items that came up in #25717 (comment) |
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. |
1206e2d to
2ad515a
Compare
2ad515a to
94af3e4
Compare
| // anti-DoS logic (this saves bandwidth when we connect to a trusted peer | ||
| // on startup). | ||
| if (pfrom.HasPermission(NetPermissionFlags::NoBan)) { | ||
| already_validated_work = true; |
There was a problem hiding this comment.
nit: after this changetrustworthy_work or something could be more accurate, although I'm not sure it actually benefits the readers.
|
tACK 94af3e4 I checked that The |
For what it's worth, I interpret |
| #endif | ||
| UTIL = (1 << 25), | ||
| BLOCKSTORE = (1 << 26), | ||
| HEADERSSYNC = (1 << 27), |
There was a problem hiding this comment.
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-
|
We should probably document what we mean by these permissions :-) |
They're documented in and in |
|
Ok, in that case I tend to agree |
|
ACK 94af3e4 |
1 similar comment
|
ACK 94af3e4 |
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, this pull request conflicts with the following ones:
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. |
|
ACK 94af3e4 |
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.