Skip to content

Commit 8edfc17

Browse files
author
MarcoFalke
committed
Merge #19204: p2p: Reduce inv traffic during IBD
fa525e4 net: Avoid wasting inv traffic during IBD (MarcoFalke) fa06d7e refactor: block import implies IsInitialBlockDownload (MarcoFalke) faba65e Add ChainstateManager::ActiveChainstate (MarcoFalke) fabf3d6 test: Add FeeFilterRounder test (MarcoFalke) Pull request description: Tx-inv messages are ignored during IBD, so it would be nice if we told peers to not send them in the first place. Do that by sending two `feefilter` messages: One when the connection is made (and the node is in IBD), and another one when the node leaves IBD. ACKs for top commit: jamesob: ACK fa525e4 ([`jamesob/ackr/19204.1.MarcoFalke.p2p_reduce_inv_traffic_d`](https://github.com/jamesob/bitcoin/tree/ackr/19204.1.MarcoFalke.p2p_reduce_inv_traffic_d)) naumenkogs: utACK fa525e4 gzhao408: ACK bitcoin/bitcoin@fa525e4 jonatack: re-ACK fa525e4 checked diff `git range-diff 19612ca fa8a66c fa525e4`, re-reviewed, ran tests, ran a custom p2p IBD behavior test at jonatack/bitcoin@9321e0f. hebasto: re-ACK fa525e4, only rebased since the [previous](bitcoin/bitcoin#19204 (review)) review (verified with `git range-diff`). Tree-SHA512: 2c22a5def9822396fca45d808b165b636f1143c4bdb2eaa5c7e977f1f18e8b10c86d4c180da488def38416cf3076a26de15014dfd4d86b2a7e5af88c74afb8eb
2 parents 748178f + fa525e4 commit 8edfc17

File tree

6 files changed

+60
-11
lines changed

6 files changed

+60
-11
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ BITCOIN_TESTS =\
232232
test/net_tests.cpp \
233233
test/netbase_tests.cpp \
234234
test/pmt_tests.cpp \
235+
test/policy_fee_tests.cpp \
235236
test/policyestimator_tests.cpp \
236237
test/pow_tests.cpp \
237238
test/prevector_tests.cpp \

src/net_processing.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2576,7 +2576,7 @@ void ProcessMessage(
25762576
LogPrint(BCLog::NET, "transaction (%s) inv sent in violation of protocol, disconnecting peer=%d\n", inv.hash.ToString(), pfrom.GetId());
25772577
pfrom.fDisconnect = true;
25782578
return;
2579-
} else if (!fAlreadyHave && !fImporting && !fReindex && !::ChainstateActive().IsInitialBlockDownload()) {
2579+
} else if (!fAlreadyHave && !chainman.ActiveChainstate().IsInitialBlockDownload()) {
25802580
RequestTx(State(pfrom.GetId()), inv.hash, current_time);
25812581
}
25822582
}
@@ -4392,10 +4392,21 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
43924392
) {
43934393
CAmount currentFilter = m_mempool.GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFeePerK();
43944394
int64_t timeNow = GetTimeMicros();
4395+
static FeeFilterRounder g_filter_rounder{CFeeRate{DEFAULT_MIN_RELAY_TX_FEE}};
4396+
if (m_chainman.ActiveChainstate().IsInitialBlockDownload()) {
4397+
// Received tx-inv messages are discarded when the active
4398+
// chainstate is in IBD, so tell the peer to not send them.
4399+
currentFilter = MAX_MONEY;
4400+
} else {
4401+
static const CAmount MAX_FILTER{g_filter_rounder.round(MAX_MONEY)};
4402+
if (pto->m_tx_relay->lastSentFeeFilter == MAX_FILTER) {
4403+
// Send the current filter if we sent MAX_FILTER previously
4404+
// and made it out of IBD.
4405+
pto->m_tx_relay->nextSendTimeFeeFilter = timeNow - 1;
4406+
}
4407+
}
43954408
if (timeNow > pto->m_tx_relay->nextSendTimeFeeFilter) {
4396-
static CFeeRate default_feerate(DEFAULT_MIN_RELAY_TX_FEE);
4397-
static FeeFilterRounder filterRounder(default_feerate);
4398-
CAmount filterToSend = filterRounder.round(currentFilter);
4409+
CAmount filterToSend = g_filter_rounder.round(currentFilter);
43994410
// We always have a fee filter of at least minRelayTxFee
44004411
filterToSend = std::max(filterToSend, ::minRelayTxFee.GetFeePerK());
44014412
if (filterToSend != pto->m_tx_relay->lastSentFeeFilter) {

src/test/policy_fee_tests.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2020 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <amount.h>
6+
#include <policy/fees.h>
7+
8+
#include <test/util/setup_common.h>
9+
10+
#include <boost/test/unit_test.hpp>
11+
12+
BOOST_FIXTURE_TEST_SUITE(policy_fee_tests, BasicTestingSetup)
13+
14+
BOOST_AUTO_TEST_CASE(FeeRounder)
15+
{
16+
FeeFilterRounder fee_rounder{CFeeRate{1000}};
17+
18+
// check that 1000 rounds to 974 or 1071
19+
std::set<CAmount> results;
20+
while (results.size() < 2) {
21+
results.emplace(fee_rounder.round(1000));
22+
}
23+
BOOST_CHECK_EQUAL(*results.begin(), 974);
24+
BOOST_CHECK_EQUAL(*++results.begin(), 1071);
25+
26+
// check that negative amounts rounds to 0
27+
BOOST_CHECK_EQUAL(fee_rounder.round(-0), 0);
28+
BOOST_CHECK_EQUAL(fee_rounder.round(-1), 0);
29+
30+
// check that MAX_MONEY rounds to 9170997
31+
BOOST_CHECK_EQUAL(fee_rounder.round(MAX_MONEY), 9170997);
32+
}
33+
34+
BOOST_AUTO_TEST_SUITE_END()

src/validation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5251,10 +5251,10 @@ CChainState& ChainstateManager::InitializeChainstate(const uint256& snapshot_blo
52515251
return *to_modify;
52525252
}
52535253

5254-
CChain& ChainstateManager::ActiveChain() const
5254+
CChainState& ChainstateManager::ActiveChainstate() const
52555255
{
52565256
assert(m_active_chainstate);
5257-
return m_active_chainstate->m_chain;
5257+
return *m_active_chainstate;
52585258
}
52595259

52605260
bool ChainstateManager::IsSnapshotActive() const

src/validation.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,8 @@ class ChainstateManager
799799
std::vector<CChainState*> GetAll();
800800

801801
//! The most-work chain.
802-
CChain& ActiveChain() const;
802+
CChainState& ActiveChainstate() const;
803+
CChain& ActiveChain() const { return ActiveChainstate().m_chain; }
803804
int ActiveHeight() const { return ActiveChain().Height(); }
804805
CBlockIndex* ActiveTip() const { return ActiveChain().Tip(); }
805806

@@ -879,13 +880,13 @@ class ChainstateManager
879880
/** DEPRECATED! Please use node.chainman instead. May only be used in validation.cpp internally */
880881
extern ChainstateManager g_chainman GUARDED_BY(::cs_main);
881882

882-
/** @returns the most-work valid chainstate. */
883+
/** Please prefer the identical ChainstateManager::ActiveChainstate */
883884
CChainState& ChainstateActive();
884885

885-
/** @returns the most-work chain. */
886+
/** Please prefer the identical ChainstateManager::ActiveChain */
886887
CChain& ChainActive();
887888

888-
/** @returns the global block index map. */
889+
/** Please prefer the identical ChainstateManager::BlockIndex */
889890
BlockMap& BlockIndex();
890891

891892
/** Global variable that points to the active block tree (protected by cs_main) */

test/functional/rpc_net.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ class NetTest(BitcoinTestFramework):
4646
def set_test_params(self):
4747
self.setup_clean_chain = True
4848
self.num_nodes = 2
49-
self.extra_args = [["-minrelaytxfee=0.00001000"],["-minrelaytxfee=0.00000500"]]
49+
self.extra_args = [["-minrelaytxfee=0.00001000"], ["-minrelaytxfee=0.00000500"]]
5050
self.supports_cli = False
5151

5252
def run_test(self):
53+
self.log.info('Get out of IBD for the minfeefilter test')
54+
self.nodes[0].generate(1)
5355
self.log.info('Connect nodes both way')
5456
connect_nodes(self.nodes[0], 1)
5557
connect_nodes(self.nodes[1], 0)

0 commit comments

Comments
 (0)