Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net processing: Reduce resource usage for inbound block-relay-only connections #22778

Merged
merged 4 commits into from
May 19, 2022

Conversation

jnewbery
Copy link
Contributor

@jnewbery jnewbery commented Aug 23, 2021

block-relay-only connections are additional outbound connections that bitcoind makes since v0.19. They participate in block relay, but do not propagate transactions or addresses. They were introduced in #15759.

When creating an outbound block-relay-only connection, since we know that we're never going to announce transactions over that connection, we can save on memory usage by not a TxRelay data structure for that connection. When receiving an inbound connection, we don't know whether the connection was opened by the peer as block-relay-only or not, and therefore we always construct a TxRelay data structure for inbound connections.

However, it is possible to tell whether an inbound connection will ever request that we start announcing transactions to it. The fRelay field in the version message may be set to 0 to indicate that the peer does not wish to receive transaction announcements. The peer may later request that we start announcing transactions to it by sending a filterload or filterclear message, but only if we have offered NODE_BLOOM services to that peer. NODE_BLOOM services are disabled by default, and it has been recommended for some time that users not enable NODE_BLOOM services on public connections, for privacy and anti-DoS reasons.

Therefore, if we have not offered NODE_BLOOM to the peer and it has set fRelay to 0, then we know that it will never request transaction announcements, and that we can save resources by not initializing the TxRelay data structure.

Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

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

Concept ACK

src/net_processing.cpp Show resolved Hide resolved
@DrahtBot
Copy link
Contributor

DrahtBot commented Aug 23, 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:

  • #25156 (refactor: Introduce PeerManagerImpl::RejectIncomingTxs by MarcoFalke)
  • #25144 (refactor: Pass Peer& to Misbehaving() by MarcoFalke)
  • #24474 (Additional thread safety annotations for CNode/Peer by ajtowns)
  • #21527 (net_processing: lock clean up by ajtowns)
  • #21515 (Erlay: bandwidth-efficient transaction relay protocol by naumenkogs)
  • #20726 (p2p: Add DISABLETX message for negotiating block-relay-only connections by sdaftuar)

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
Copy link
Contributor Author

I've pushed a new branch that simplifies the implementation a bit. I'd previously wanted to amalgamate the various mutexes and atomics in Peer::TxRelay as part of this PR, but it's easier to wait until #21527 to do that, otherwise there are all kinds of lock ordering/inversion issues to work around.

@jnewbery jnewbery force-pushed the 2021-02-tx-relay-init branch from cb2c083 to bc91085 Compare August 27, 2021 08:43
@jnewbery jnewbery force-pushed the 2021-02-tx-relay-init branch 2 times, most recently from 8653f7d to 4ee20ce Compare August 27, 2021 09:59
@jnewbery
Copy link
Contributor Author

rebased

@naumenkogs
Copy link
Member

Concept ACK

@jnewbery
Copy link
Contributor Author

jnewbery commented Sep 24, 2021

Rebased on the latest #21160

@vasild
Copy link
Contributor

vasild commented Sep 30, 2021

Some rough estimates on how much this will save:

(gdb) p sizeof(CNode::TxRelay)
$1 = 152

Assuming 50 incoming connections and 20% of them being block-only, that makes 10 peers will benefit from this. 152 bytes each, so 1520 bytes will be saved.

@jnewbery
Copy link
Contributor Author

Some rough estimates on how much this will save...

@vasild This seems completely wrong. TxRelay contains a CRollingBloomFilter parametrized by {50000, 0.000001}. According to this code comment:

bitcoin/src/bloom.h

Lines 94 to 98 in 419afa9

* It needs around 1.8 bytes per element per factor 0.1 of false positive rate.
* For example, if we want 1000 elements, we'd need:
* - ~1800 bytes for a false positive rate of 0.1
* - ~3600 bytes for a false positive rate of 0.01
* - ~5400 bytes for a false positive rate of 0.001

the storage requirement for just that rolling bloom filter is ~1.8 * 50,000 * 6 = 540kB

@jnewbery
Copy link
Contributor Author

Thanks for the review @MarcoFalke. All review comments have been addressed.

@maflcko
Copy link
Member

maflcko commented May 18, 2022

You did the rebase on the wrong machine. The typo from #22778 (comment) is back again.

review ACK edf2944 💎

Show signature

Signature:

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

review ACK edf29446658ee9105b6451573687dc74a92604b9 💎
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUiqcQv+NNN0jFiVXfsQikTdVkZEyp8Re02fU5bsk1AYZlB45koiyFQehpyC87wS
e9vTQpGGy3lu3JoYNjuIeJSC07MPTy8anqunZ2tHfe0xz9XCksqqml4DiFWHhPIr
tLnRxuamaBUy3VzDZN1hulLsG2JI/NB4TuxTEkZOjesiPqiAep79ugKCvm7828At
EYyS/NVO7uQq2x0li/1KFtzRbC9wG/32AkNYMa7je+dlMkrE8InHcLqhCpQp2c89
8dhmUfKMYcxock8yTaQQH6iqEH9vxSBYccLHd9rOpDCrxYcQW39u/EPom4Esqvna
O48dF3aV+lS/ztEB0qH5TtE9p9AdTPZaUw2lHwTzcZ4VgKgD2IyJzemA6a4Wnao/
ymBpWk9RJoToBpKUelqMrkrLhXneIDAtAO28aiAAi0SuNsBxIbLLgweYUemom11L
P/78PSLAEe3QvVpubDPULGXR2JMueWkcHYy09/TT4s44J4DnD/bUF4mpy6vz3fI9
ZuJzQdNA
=3z05
-----END PGP SIGNATURE-----

src/net_processing.cpp Outdated Show resolved Hide resolved
jnewbery added 4 commits May 18, 2022 17:01
…er_sent

Move m_next_send_feefilter and m_fee_filter_sent out of the `TxRelay`
data structure. All of the other members of `TxRelay` are related to
sending transactions _to_ the peer, whereas m_fee_filter_sent and
m_next_send_feefilter are both related to receiving transactions _from_
the peer. A node's tx relay behaviour is not always symmetrical (eg a
blocksonly node will ignore incoming transactions, but may still send
out its own transactions), so it doesn't make sense to group the
feefilter sending data with the TxRelay data in a single structure.

This does not change behaviour, since IsBlockOnlyConn() is always equal
to !peer.m_tx_relay. We still don't send feefilter messages to outbound
block-relay-only peers (tested in p2p_feefilter.py).
This fully comments all the TxRelay members. The only significant change
is to the comment for m_relay_txs. Previously the comment stated that
one of the purposes of the field was that "We don't relay tx invs before
receiving the peer's version message". However, even without the
m_relay_txs flag, we would not send transactions to the peer before
receiving the `version` message, since SendMessages() returns
immediately if fSuccessfullyConnected is not set to true, which only
happens once a `version` and `verack` message have been received.
Delay initializing the TxRelay data structure for a peer until we receive
a version message from that peer. At that point we'll know whether it
will ever relay transactions. We only initialize the m_tx_relay
data structure if:

- this isn't an outbound block-relay-only connection; AND
- fRelay=true OR we're offering NODE_BLOOM to this peer
  (NODE_BLOOM means that the peer may turn on tx relay later)
@jnewbery
Copy link
Contributor Author

You did the rebase on the wrong machine. The typo from #22778 (comment) is back again.

🤦‍♂️ Thanks. Fixed.

@jnewbery jnewbery force-pushed the 2021-02-tx-relay-init branch from edf2944 to 9db82f1 Compare May 18, 2022 16:32
@maflcko
Copy link
Member

maflcko commented May 18, 2022

review ACK 9db82f1 🖖

Show signature

Signature:

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

review ACK 9db82f1bca0bb51c2180ca4a4ad63ba490e79da4 🖖
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUia8gv/Y52Be4OmX6OF+MVJkvO5yvf1pgfk+5UrMwxwRHD+JPU/j09OSlRh9Iqh
VQfCIISSQgLbmxXuOODYgIPxK3WDkTBSH1zTNZWxfcODcXr539kTsJMq0Uh40TbI
hPIR+BrUJ+NTW1zQOJtMJeUHPsWTuomqbqYJ6iX360WnUE5D/mi/m2rBaOaq97De
XDb059Ww4NgYmQVJJk3xbqiNskVG/sZkW78W0676+Gum+awt0+XK5aJTH/rFHAio
uDmpAPPGMfnxcNnOi3rj0d/nI/lEBYX1+/0hlnhOZI6y2ordKokRe7ePDSok1MgK
piRGIPbmHzptjnVWvBFybTN7k+zlMBmouG498mmwS2HAw/EaNE+WV0gK1JHycnj5
538RWYKG/qaKc8dPHFjvCnV2QXhmfcoADYnPGmvqVqdS40aAT77upwDd3KB3PYm5
0Jm22Y+CbqT1y6NdUIV0YPVKrjylNrls6FMN/RnBcVNna/iWvR+UBI/sF3pMQiz+
i/LQZkTg
=/JFT
-----END PGP SIGNATURE-----

@dergoegge
Copy link
Member

dergoegge commented May 18, 2022

Code review ACK 9db82f1

Only documentation changed since I last reviewed.

@LarryRuane
Copy link
Contributor

I was just speaking with @glozow, and I'd like to host review club for this PR on June 8, if it's not already merged by then, or maybe even if it is.

@naumenkogs
Copy link
Member

ACK 9db82f1

@fanquake fanquake merged commit 986bae8 into bitcoin:master May 19, 2022
@jnewbery jnewbery deleted the 2021-02-tx-relay-init branch May 19, 2022 09:48
@jnewbery
Copy link
Contributor Author

I was just speaking with @glozow, and I'd like to host review club for this PR on June 8, if it's not already merged by then, or maybe even if it is.

That's great! I'm very happy to review notes & questions, or have a call to discuss. Feel free to message me.

maflcko pushed a commit to bitcoin-core/gui that referenced this pull request May 20, 2022
… by propagating some negative capabilities

2b3373c refactor: Propagate negative `!m_tx_relay_mutex` capability (Hennadii Stepanov)
5a6e3c1 refactor: Propagate negative `!m_most_recent_block_mutex` capability (Hennadii Stepanov)

Pull request description:

  This PR is a follow-up for bitcoin/bitcoin#22778 and bitcoin/bitcoin#24062, and it seems [required](bitcoin/bitcoin#24931 (comment)) for bitcoin/bitcoin#24931.

  See details in the commit messages.

ACKs for top commit:
  ajtowns:
    ACK 2b3373c
  w0xlt:
    ACK bitcoin/bitcoin@2b3373c

Tree-SHA512: 8a4bb9641af8d79824ec12e2d6bfce0e09524094b298a2edcdb2ab115fbaa21215b9c97a6b059f8aa023551071fd5798eca66ab8d262a3f97246a91d960850d0
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request May 28, 2022
@LarryRuane
Copy link
Contributor

Post-merge ACK
I'd appreciate it if a maintainer could add the review club label.

dergoegge added a commit to dergoegge/bitcoin that referenced this pull request Jun 22, 2022
fanquake added a commit to bitcoin-core/gui that referenced this pull request Jun 23, 2022
…eceiving BIP37 filters

e7a9133 [net processing] Set CNode::m_relays_txs=true when receiving BIP37 filters (dergoegge)

Pull request description:

  This line was accidentally removed in bitcoin/bitcoin#22778.

  Receiving a `filterload` message implies that we should relay txs to the sender (`CNode::m_relays_txs = true`). `CNode::m_relays_txs` is only used for the inbound eviction logic, so removing the line might have slightly changed the eviction behaviour but nothing else.

ACKs for top commit:
  laanwj:
    Code review ACK e7a9133
  vasild:
    ACK e7a9133

Tree-SHA512: 19c5df0f579f707c6c7900d12a6b71ac69e802be64f7d2fdcc40ac714c918dc4c17def164592f8836cc105a03daefefca3ca5e10423145eca8db4348c27c9cfc
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jun 27, 2022
… BIP37 filters

e7a9133 [net processing] Set CNode::m_relays_txs=true when receiving BIP37 filters (dergoegge)

Pull request description:

  This line was accidentally removed in bitcoin#22778.

  Receiving a `filterload` message implies that we should relay txs to the sender (`CNode::m_relays_txs = true`). `CNode::m_relays_txs` is only used for the inbound eviction logic, so removing the line might have slightly changed the eviction behaviour but nothing else.

ACKs for top commit:
  laanwj:
    Code review ACK e7a9133
  vasild:
    ACK e7a9133

Tree-SHA512: 19c5df0f579f707c6c7900d12a6b71ac69e802be64f7d2fdcc40ac714c918dc4c17def164592f8836cc105a03daefefca3ca5e10423145eca8db4348c27c9cfc
@bitcoin bitcoin locked and limited conversation to collaborators Jun 9, 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.

9 participants