Skip to content

Commit 753ed61

Browse files
committed
Fix duplicate initial headers sync
1 parent f585450 commit 753ed61

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

qa/rpc-tests/bip68-112-113-p2p.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ def __init__(self):
9898

9999
def setup_network(self):
100100
# Must set the blockversion for this test
101+
# Must also set '-maxtipage=600100' to allow syncing from very old blocks
101102
self.nodes = start_nodes(self.num_nodes, self.options.tmpdir,
102-
extra_args=[['-debug', '-whitelist=127.0.0.1', '-blockversion=4']],
103+
extra_args=[['-debug', '-whitelist=127.0.0.1', '-blockversion=4', '-maxtipage=600100']],
103104
binary=[self.options.testbinary])
104105

105106
def run_test(self):

qa/rpc-tests/maxuploadtarget.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def __init__(self):
9191
def setup_network(self):
9292
# Start a node with maxuploadtarget of 200 MB (/24h)
9393
self.nodes = []
94-
self.nodes.append(start_node(0, self.options.tmpdir, ["-debug", "-maxuploadtarget=200", "-blockmaxsize=999000"]))
94+
self.nodes.append(start_node(0, self.options.tmpdir, ["-debug", "-maxuploadtarget=200", "-blockmaxsize=999000", "-maxtipage="+str(2*60*60*24*7)]))
9595

9696
def run_test(self):
9797
# Advance all nodes 2 weeks in the future
@@ -205,7 +205,7 @@ def run_test(self):
205205
#stop and start node 0 with 1MB maxuploadtarget, whitelist 127.0.0.1
206206
print("Restarting nodes with -whitelist=127.0.0.1")
207207
stop_node(self.nodes[0], 0)
208-
self.nodes[0] = start_node(0, self.options.tmpdir, ["-debug", "-whitelist=127.0.0.1", "-maxuploadtarget=1", "-blockmaxsize=999000"])
208+
self.nodes[0] = start_node(0, self.options.tmpdir, ["-debug", "-whitelist=127.0.0.1", "-maxuploadtarget=1", "-blockmaxsize=999000", "-maxtipage="+str(2*60*60*24*7)])
209209

210210
#recreate/reconnect 3 test nodes
211211
test_nodes = []

src/net_processing.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,7 +1682,24 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
16821682

16831683
if (inv.type == MSG_BLOCK) {
16841684
UpdateBlockAvailability(pfrom->GetId(), inv.hash);
1685-
if (!fAlreadyHave && !fImporting && !fReindex && !mapBlocksInFlight.count(inv.hash)) {
1685+
1686+
if (fAlreadyHave || fImporting || fReindex || mapBlocksInFlight.count(inv.hash)) {
1687+
continue;
1688+
}
1689+
1690+
CNodeState *state = State(pfrom->id);
1691+
if (!state) {
1692+
continue;
1693+
}
1694+
1695+
// Download if this is a nice peer, or we have no nice peers and this one might do.
1696+
bool fFetch = state->fPreferredDownload || (nPreferredDownload == 0 && !pfrom->fOneShot);
1697+
// Only actively request headers from a single peer, unless we're close to end of initial download.
1698+
if ((nSyncStarted == 0 && fFetch) || pindexBestHeader->GetBlockTime() > GetAdjustedTime() - nMaxTipAge) {
1699+
// Make sure to mark this peer as the one we are currently syncing with etc.
1700+
state->fSyncStarted = true;
1701+
state->nHeadersSyncTimeout = GetTimeMicros() + HEADERS_DOWNLOAD_TIMEOUT_BASE + HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER * (GetAdjustedTime() - pindexBestHeader->GetBlockTime())/(chainparams.GetConsensus().nPowTargetSpacing);
1702+
nSyncStarted++;
16861703
// We used to request the full block here, but since headers-announcements are now the
16871704
// primary method of announcement on the network, and since, in the case that a node
16881705
// fell back to inv we probably have a reorg which we should get the headers for first,
@@ -3099,7 +3116,7 @@ bool SendMessages(CNode* pto, CConnman& connman, const std::atomic<bool>& interr
30993116
bool fFetch = state.fPreferredDownload || (nPreferredDownload == 0 && !pto->fClient && !pto->fOneShot); // Download if this is a nice peer, or we have no nice peers and this one might do.
31003117
if (!state.fSyncStarted && !pto->fClient && !fImporting && !fReindex) {
31013118
// Only actively request headers from a single peer, unless we're close to end of initial download.
3102-
if ((nSyncStarted == 0 && fFetch) || pindexBestHeader->GetBlockTime() > GetAdjustedTime() - 6 * 60 * 60) { // NOTE: was "close to today" and 24h in Bitcoin
3119+
if ((nSyncStarted == 0 && fFetch) || pindexBestHeader->GetBlockTime() > GetAdjustedTime() - nMaxTipAge) {
31033120
state.fSyncStarted = true;
31043121
state.nHeadersSyncTimeout = GetTimeMicros() + HEADERS_DOWNLOAD_TIMEOUT_BASE + HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER * (GetAdjustedTime() - pindexBestHeader->GetBlockTime())/(consensusParams.nPowTargetSpacing);
31053122
nSyncStarted++;
@@ -3418,7 +3435,7 @@ bool SendMessages(CNode* pto, CConnman& connman, const std::atomic<bool>& interr
34183435
// Check for headers sync timeouts
34193436
if (state.fSyncStarted && state.nHeadersSyncTimeout < std::numeric_limits<int64_t>::max()) {
34203437
// Detect whether this is a stalling initial-headers-sync peer
3421-
if (pindexBestHeader->GetBlockTime() <= GetAdjustedTime() - 6*60*60) { // was 24*60*60 in bitcoin
3438+
if (pindexBestHeader->GetBlockTime() <= GetAdjustedTime() - nMaxTipAge) {
34223439
if (nNow > state.nHeadersSyncTimeout && nSyncStarted == 1 && (nPreferredDownload - state.fPreferredDownload >= 1)) {
34233440
// Disconnect a (non-whitelisted) peer if it is our only sync peer,
34243441
// and we have others we could be using instead.

0 commit comments

Comments
 (0)