Skip to content

Commit 03712dd

Browse files
committed
Expose HeadersSyncState::m_current_height in getpeerinfo()
1 parent 150a548 commit 03712dd

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

src/headerssync.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ class HeadersSyncState {
118118
/** Return the current state of our download */
119119
State GetState() const { return m_download_state; }
120120

121+
/** Return the height reached during the PRESYNC phase */
122+
int64_t GetPresyncHeight() const { return m_current_height; }
123+
121124
/** Construct a HeadersSyncState object representing a headers sync via this
122125
* download-twice mechanism).
123126
*

src/net_processing.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,6 +1566,12 @@ bool PeerManagerImpl::GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) c
15661566
stats.m_addr_processed = peer->m_addr_processed.load();
15671567
stats.m_addr_rate_limited = peer->m_addr_rate_limited.load();
15681568
stats.m_addr_relay_enabled = peer->m_addr_relay_enabled.load();
1569+
{
1570+
LOCK(peer->m_headers_sync_mutex);
1571+
if (peer->m_headers_sync) {
1572+
stats.presync_height = peer->m_headers_sync->GetPresyncHeight();
1573+
}
1574+
}
15691575

15701576
return true;
15711577
}

src/net_processing.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct CNodeStateStats {
3535
uint64_t m_addr_rate_limited = 0;
3636
bool m_addr_relay_enabled{false};
3737
ServiceFlags their_services;
38+
int64_t presync_height{-1};
3839
};
3940

4041
class PeerManager : public CValidationInterface, public NetEventsInterface

src/rpc/net.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ static RPCHelpMan getpeerinfo()
132132
{RPCResult::Type::BOOL, "bip152_hb_to", "Whether we selected peer as (compact blocks) high-bandwidth peer"},
133133
{RPCResult::Type::BOOL, "bip152_hb_from", "Whether peer selected us as (compact blocks) high-bandwidth peer"},
134134
{RPCResult::Type::NUM, "startingheight", /*optional=*/true, "The starting height (block) of the peer"},
135+
{RPCResult::Type::NUM, "presynced_headers", /*optional=*/true, "The current height of header pre-synchronization with this peer, or -1 if no low-work sync is in progress"},
135136
{RPCResult::Type::NUM, "synced_headers", /*optional=*/true, "The last header we have in common with this peer"},
136137
{RPCResult::Type::NUM, "synced_blocks", /*optional=*/true, "The last block we have in common with this peer"},
137138
{RPCResult::Type::ARR, "inflight", /*optional=*/true, "",
@@ -226,6 +227,7 @@ static RPCHelpMan getpeerinfo()
226227
obj.pushKV("bip152_hb_from", stats.m_bip152_highbandwidth_from);
227228
if (fStateStats) {
228229
obj.pushKV("startingheight", statestats.m_starting_height);
230+
obj.pushKV("presynced_headers", statestats.presync_height);
229231
obj.pushKV("synced_headers", statestats.nSyncHeight);
230232
obj.pushKV("synced_blocks", statestats.nCommonHeight);
231233
UniValue heights(UniValue::VARR);

test/functional/p2p_headers_sync_with_minchainwork.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@
66

77
from test_framework.test_framework import BitcoinTestFramework
88

9+
from test_framework.p2p import (
10+
P2PInterface,
11+
)
12+
13+
from test_framework.messages import (
14+
msg_headers,
15+
)
16+
17+
from test_framework.blocktools import (
18+
NORMAL_GBT_REQUEST_PARAMS,
19+
create_block,
20+
)
21+
22+
from test_framework.util import assert_equal
23+
924
NODE1_BLOCKS_REQUIRED = 15
1025
NODE2_BLOCKS_REQUIRED = 2047
1126

@@ -23,6 +38,10 @@ def setup_network(self):
2338
self.connect_nodes(0, 2)
2439
self.sync_all()
2540

41+
def disconnect_all(self):
42+
self.disconnect_nodes(0, 1)
43+
self.disconnect_nodes(0, 2)
44+
2645
def test_chains_sync_when_long_enough(self):
2746
self.log.info("Generate blocks on the node with no required chainwork, and verify nodes 1 and 2 have no new headers in their headers tree")
2847
with self.nodes[1].assert_debug_log(expected_msgs=["[net] Ignoring low-work chain (height=14)"]), self.nodes[2].assert_debug_log(expected_msgs=["[net] Ignoring low-work chain (height=14)"]):
@@ -58,9 +77,41 @@ def test_chains_sync_when_long_enough(self):
5877
self.log.info("Verify that node2 will sync the chain when it gets long enough")
5978
self.sync_blocks()
6079

80+
def test_peerinfo_includes_headers_presync_height(self):
81+
self.log.info("Test that getpeerinfo() includes headers presync height")
82+
83+
# Disconnect network, so that we can find our own peer connection more
84+
# easily
85+
self.disconnect_all()
86+
87+
p2p = self.nodes[0].add_p2p_connection(P2PInterface())
88+
node = self.nodes[0]
89+
90+
# Ensure we have a long chain already
91+
current_height = self.nodes[0].getblockcount()
92+
if (current_height < 3000):
93+
self.generate(node, 3000-current_height, sync_fun=self.no_op)
94+
95+
# Send a group of 2000 headers, forking from genesis.
96+
new_blocks = []
97+
hashPrevBlock = int(node.getblockhash(0), 16)
98+
for i in range(2000):
99+
block = create_block(hashprev = hashPrevBlock, tmpl=node.getblocktemplate(NORMAL_GBT_REQUEST_PARAMS))
100+
block.solve()
101+
new_blocks.append(block)
102+
hashPrevBlock = block.sha256
103+
104+
headers_message = msg_headers(headers=new_blocks)
105+
p2p.send_and_ping(headers_message)
106+
107+
# getpeerinfo should show a sync in progress
108+
assert_equal(node.getpeerinfo()[0]['presynced_headers'], 2000)
109+
61110
def run_test(self):
62111
self.test_chains_sync_when_long_enough()
63112

113+
self.test_peerinfo_includes_headers_presync_height()
114+
64115

65116
if __name__ == '__main__':
66117
RejectLowDifficultyHeadersTest().main()

0 commit comments

Comments
 (0)