Skip to content

Conversation

jnewbery
Copy link
Contributor

@jnewbery jnewbery commented Mar 31, 2021

  • Use default initialization of PeerManagerImpl members where possible
  • Remove unique_ptr indirection where it's not needed

@DrahtBot DrahtBot added the P2P label Mar 31, 2021
@fanquake
Copy link
Member

fanquake commented Apr 1, 2021

Cirrus failures:

In file included from init.cpp:55:
./txorphanage.h:50:28: error: reading variable 'm_orphans' requires holding mutex 'g_cs_orphans' [-Werror,-Wthread-safety-analysis]
    size_t Size() { return m_orphans.size(); };
                           ^
1 error generated.
make[2]: *** [libbitcoin_server_a-init.o] Error 1
make[2]: *** Waiting for unfinished jobs....
  CXX      libbitcoin_server_a-net_processing.o
In file included from net_processing.cpp:29:
./txorphanage.h:50:28: error: reading variable 'm_orphans' requires holding mutex 'g_cs_orphans' [-Werror,-Wthread-safety-analysis]
    size_t Size() { return m_orphans.size(); };
                           ^
net_processing.cpp:410:59: error: expected ';' at end of declaration list
    CRollingBloomFilter m_recent_rejects{120000, 0.000001} GUARDED_BY(cs_main);
                                                          ^
                                                          ;
net_processing.cpp:429:73: error: expected ';' at end of declaration list
    CRollingBloomFilter m_recent_confirmed_transactions{48000, 0.000001} GUARDED_BY(m_recent_confirmed_transactions_mutex);
                                                                        ^
                                                                        ;
3 errors generated.

@DrahtBot
Copy link
Contributor

DrahtBot commented Apr 1, 2021

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

Conflicts

Reviewers, 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.

@jnewbery jnewbery force-pushed the 2021-03-peer-manager-impl-ctor-dtor branch from 94b5fea to b458400 Compare April 1, 2021 08:25
@practicalswift
Copy link
Contributor

Strong Concept ACK

I find the suggested code easier to reason about from a safety and correctness perspective.

@jnewbery jnewbery force-pushed the 2021-03-peer-manager-impl-ctor-dtor branch from b458400 to 44c05f7 Compare April 1, 2021 10:33
@jnewbery jnewbery marked this pull request as ready for review April 1, 2021 13:49
@jnewbery jnewbery closed this Apr 1, 2021
@jnewbery jnewbery reopened this Apr 1, 2021
@maflcko
Copy link
Member

maflcko commented Apr 1, 2021

The build failure is because Assume breaks static lock analysis. One way to fix is to assign the assumed-value to a bool first.

@jnewbery jnewbery force-pushed the 2021-03-peer-manager-impl-ctor-dtor branch from 44c05f7 to a1f2f65 Compare April 1, 2021 15:27
@jnewbery
Copy link
Contributor Author

jnewbery commented Apr 1, 2021

Thanks Marco! I've just reverted these checks to assert() for now.

@hebasto
Copy link
Member

hebasto commented Apr 2, 2021

@jnewbery Help me to understand the motivation for "[net processing] Move consistency checks to PeerManagerImpl dtor" commit please.

My current understanding is following. A consistency is an invariant that the PeerManagerImpl class must hold. It's ctor's job to establish class's invariants, and member functions' job to preserve invariants. Delegating such a task to dtor seems unusual for me.

@jnewbery
Copy link
Contributor Author

jnewbery commented Apr 6, 2021

Help me to understand the motivation for "[net processing] Move consistency checks to PeerManagerImpl dtor" commit please.

My current understanding is following. A consistency is an invariant that the PeerManagerImpl class must hold. It's ctor's job to establish class's invariants, and member functions' job to preserve invariants. Delegating such a task to dtor seems unusual for me.

I think that's fair. My reasoning was that these invariant checks are all test/development asserts. We'd hope that they'd never be hit in the wild. They currently run when the peer count drops to zero, but if there's an inconsistency then, we'd expect those invariants to also be false when the PeerManagerImpl is destructed on shutdown.

My motivation is that I want to pull some of these members out from being guarded by cs_main. The global counts like m_outbound_peers_with_protect_from_disconnect and m_wtxid_relay_peers don't actually need to be atomically updated with the correspondingCNodeState member variables m_chain_sync.m_protect and m_wtxid_relay. We could pull those things out of cs_main and just make them separate atomics and that'd be safe, but not obviously so. FinalizeNode() is called by the same thread that increments the counters, so there's no way that they can be non-zero whilst the final peer is being removed. However, if other threads were able to increment the counters, these invariants would no longer be true. That's not the case if we move the invariant checks to the destructor. Inside the dtor, we know that no other thread can be mutating the members, so the counters must be at zero.

PeerManagerImpl::~PeerManagerImpl()
{
// Do consistency checks on shutdown
LOCK(cs_main);
Copy link
Member

Choose a reason for hiding this comment

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

Only one thread has an access to the object while a dtor is called. Why lock?

@DrahtBot
Copy link
Contributor

DrahtBot commented May 3, 2021

🕵️ @sipa has been requested to review this pull request as specified in the REVIEWERS file.

@jnewbery jnewbery force-pushed the 2021-03-peer-manager-impl-ctor-dtor branch from a1f2f65 to b26a5ad Compare May 3, 2021 11:18
@jnewbery jnewbery changed the title [net processing] Various tidying up of PeerManagerImpl ctor and dtor [net processing] Various tidying up of PeerManagerImpl ctor May 3, 2021
@jnewbery
Copy link
Contributor Author

jnewbery commented May 3, 2021

@hebasto - there are various changes to net_processing logging happening in 21527, so I've removed the changes to the destructor here until those land. Let me know if you think the remaining changes to the constructor in this PR are worth keeping.

Also rebased on current master.

@Crypt-iQ
Copy link
Contributor

Crypt-iQ commented May 9, 2021

Code review ACK b26a5ad

I was confused by the asserts prior to this PR. Also there are two typos in the commit messages "contetx" should be "context"?

@jnewbery jnewbery force-pushed the 2021-03-peer-manager-impl-ctor-dtor branch from b26a5ad to d6cef4d Compare May 10, 2021 06:42
@jnewbery
Copy link
Contributor Author

Thanks @Crypt-iQ. I've corrected the typos in the commit logs.

I was confused by the asserts prior to this PR.

These were to ensure that recentRejects wasn't null before dereferencing it. No need for that now that m_recent_rejects is not a pointer.

@Crypt-iQ
Copy link
Contributor

Code review ACK d6cef4d

Only the commit messages were fixed from b26a5ad, so should be good.

@jnewbery jnewbery force-pushed the 2021-03-peer-manager-impl-ctor-dtor branch from d6cef4d to 93e881f Compare June 2, 2021 15:12
@jnewbery
Copy link
Contributor Author

jnewbery commented Jun 2, 2021

Rebased on master to fix fuzz CI timeout.

@Crypt-iQ
Copy link
Contributor

Crypt-iQ commented Jun 5, 2021

Code review ACK 93e881f

Commit message 9e6bf78 has a typo: rejectRejects should be recentRejects

@jnewbery
Copy link
Contributor Author

Rebased.

Commit message 9e6bf78 has a typo: rejectRejects should be recentRejects

Fixed. Thank you!

@jnewbery jnewbery force-pushed the 2021-03-peer-manager-impl-ctor-dtor branch from 93e881f to d5dd3fa Compare June 14, 2021 09:23
@maflcko
Copy link
Member

maflcko commented Jun 14, 2021

review ACK d5dd3fa 🔠

Show signature and timestamp

Signature:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

review ACK d5dd3fa1de2f96a7ddfd8b300829c68b93bd4607 🔠
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUhpNwwAjxKP8RmX2H5ReC3tV5MQ/WvFMI9x+WHwIiNcpkRQ8wk4kErsemjnoroT
gEZg/nq3IfgMQL3LvaU270gGOdH3J45TFJtoGCtq+nogV/nU8TwjlCzP0vUsRzfP
3hiarnsn3GIJs9r01uC31GII/TrTSZPDjsKxd9X5aLKAtjsMlB9C5mZdQtQ5CYul
+3QGhnGGZpTIqE9V24dY0sOUrO54UtSW87qaxRXx88MkgVz+LTF1TDjSJy9We6Hj
ehe8OTC9MoHOYJ5rv7aiLsrJ9H28Ntal6w84Q9lts1laTEyMlazegtjIvoUogrP4
eswz53RfJg1APyIDZPn62+xNp3XYIG7ib29L3A/lsjjbw6d862EDOmyjVVloTMcM
Tb7ruvFaZdr8k9TBeKtZ1P/ecEPszUWldCIJ5RXo9iIIGaks7f8OKJNyTmgxjfVj
HOVgn1gwuE2sTQHb6pSuW45JBOslXCrYaCOQNWrCpX29X0iOpT4bJM31WVX1lFzH
TtiamXSG
=v+4O
-----END PGP SIGNATURE-----

Timestamp of file with hash cb3d358ad6eccc7781819f9917bb2f9a6f7b802e636f39fe7a8d779c985d5dfc -

Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

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

ACK d5dd3fa

While touching the net_processing.cpp, also a typo which was introduced in #22141 could be fixed:

* - the block has been recieved from a peer

recieved ==> received

@practicalswift
Copy link
Contributor

cr ACK d5dd3fa

@jnewbery
Copy link
Contributor Author

Thanks for the reviews @MarcoFalke, @hebasto, @practicalswift. I've addressed all of the review comments from @hebasto so this is now ready for re-review.

While touching the net_processing.cpp, also a typo which was introduced in #22141 could be fixed:

I was too slow. It's been fixed in #22323.

Copy link
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

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

Code-review ACK 214e91a

one stylistic/readability-improvement nit: could also use the digit separators (') for the floating point numeral for the CRollingBloomFilter (valid since C++14 according to https://en.cppreference.com/w/cpp/language/floating_literal):

diff --git a/src/net_processing.cpp b/src/net_processing.cpp
index 25fbd33b5..772ea2ced 100644
--- a/src/net_processing.cpp
+++ b/src/net_processing.cpp
@@ -453,7 +453,7 @@ private:
      *
      * Memory used: 1.3 MB
      */
-    CRollingBloomFilter m_recent_rejects GUARDED_BY(::cs_main){120'000, 0.000001};
+    CRollingBloomFilter m_recent_rejects GUARDED_BY(::cs_main){120'000, 0.000'001};
     uint256 hashRecentRejectsChainTip GUARDED_BY(cs_main);

     /*
@@ -472,7 +472,7 @@ private:
      * same probability that we have in the reject filter).
      */
     Mutex m_recent_confirmed_transactions_mutex;
-    CRollingBloomFilter m_recent_confirmed_transactions GUARDED_BY(m_recent_confirmed_transactions_mutex){48'000, 0.000001};
+    CRollingBloomFilter m_recent_confirmed_transactions GUARDED_BY(m_recent_confirmed_transactions_mutex){48'000, 0.000'001};

     /** Have we requested this block from a peer */
     bool IsBlockRequested(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);

jnewbery added 5 commits July 20, 2021 13:12
When removing the final peer, assert that m_tx_orphanage is empty.
Now that recentRejects is owned by PeerManagerImpl, and
PeerManagerImpl's lifetime is managed by the node context, we can just
default initialize recentRejects during object initialization. We can
also remove the unique_ptr indirection.
-BEGIN VERIFY SCRIPT-
ren() { sed -i "s:\<$1\>:$2:g" $(git grep -l "\<$1\>" ./src ./test); }

ren recentRejects m_recent_rejects
-END VERIFY SCRIPT-
Now that m_recent_confirmed_transactions is owned by PeerManagerImpl,
and PeerManagerImpl's lifetime is managed by the node context, we can
just default initialize m_recent_confirmed_transactions during object
initialization. We can also remove the unique_ptr indirection.
@jnewbery jnewbery force-pushed the 2021-03-peer-manager-impl-ctor-dtor branch from 214e91a to fde1bf4 Compare July 20, 2021 12:31
@jnewbery
Copy link
Contributor Author

Thanks for the review @theStack. I didn't realize that the digit separator was permitted after the decimal point. I've taken your suggestion and rebased on master.

Copy link
Contributor

@theStack theStack 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 fde1bf4

checked via git range-diff 214e91a3...fde1bf4f that since my previous ACK, the only non-rebase-related change is using the digit separator also for floating points now, as suggested.

@maflcko
Copy link
Member

maflcko commented Jul 28, 2021

ACK fde1bf4 👞

Show signature and timestamp

Signature:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

ACK fde1bf4f6136638e84cdf9806eedaae08e841bbf 👞
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUgIaAv+PZATwaBhcsf9/tfSEfMxRLP+BpK9c5vKV+URqEVG8wL+rV+vi/4Sdv5K
C7aNNt2AXAYZgqnIAvmsg1uL8G3uFZHHAo54ziHVjS7MlrG0+SSg5jKOerW/QjwA
Qb1BJqMP/ra4Jm7PHVcbGTjQg6Z0t3kJ+11jzqpVCRJ21NTPNwajBrrcpNKkdwHP
7sIKPVi5LLvK8V43gwGoAbEl9Dtwj/SfOIkaINpeZv8URYeKLYey3s3yjiO06wH1
9eXj+Sglww9GiPETOBuFMEMKcc9hCktK2oEHMv3U3d5cC12F9Dtjp1NazPNmBnvj
FwNAVbP//HwTIv0oZ+7dsZ/SKNIgKlgXR8fAsKH0ZiD85vC1ZalobhVqkmrRMX86
vW9ZP2Fj8X36Ez6vD+xRvy0cFMs+oQPPE4VLBnysmB5qT8uzcLSvSLTdwklW7FWK
T8XSw4GuNv/l/R7lEARBrkzajpMxtxRxNzvI5Zb7mu5CQWTZxogQFbCji+Vnk7ok
R+vr6Dyk
=9Rfq
-----END PGP SIGNATURE-----

Timestamp of file with hash d2de22e790c8247f5ed87c9b394b8c69ce8ccefa133cb79fea5a41603e328f92 -

@maflcko maflcko merged commit 67b9416 into bitcoin:master Jul 28, 2021
@jnewbery jnewbery deleted the 2021-03-peer-manager-impl-ctor-dtor branch July 28, 2021 14:52
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jul 28, 2021
…erImpl ctor

fde1bf4 [net processing] Default initialize m_recent_confirmed_transactions (John Newbery)
37dcd12 scripted-diff: Rename recentRejects (John Newbery)
cd9902a [net processing] Default initialize recentRejects (John Newbery)
a28bfd1 [net processing] Default initialize m_stale_tip_check_time (John Newbery)
9190b01 [net processing] Add Orphanage empty consistency check (John Newbery)

Pull request description:

  - Use default initialization of PeerManagerImpl members where possible
  - Remove unique_ptr indirection where it's not needed

ACKs for top commit:
  MarcoFalke:
    ACK fde1bf4 👞
  theStack:
    re-ACK fde1bf4

Tree-SHA512: 7ddedcc972df8e933e1fbe5c88b8ea17df89e1e58fc769518512c5540e49dc8eddb3f47e78d1329a6fc5644d2c1d11c981f681fd633f5218bfa4b3e6a86f3d7b
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 18, 2022
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.

8 participants