Skip to content

Commit 42882fc

Browse files
committed
[net processing] Only accept sendcmpct with version=2
Subsequent commits will remove support for other versions of compact blocks. Add a test that a received `sendcmpct` message with version = 1 is ignored.
1 parent 16730b6 commit 42882fc

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

src/net_processing.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2883,22 +2883,24 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
28832883
bool fAnnounceUsingCMPCTBLOCK = false;
28842884
uint64_t nCMPCTBLOCKVersion = 0;
28852885
vRecv >> fAnnounceUsingCMPCTBLOCK >> nCMPCTBLOCKVersion;
2886-
if (nCMPCTBLOCKVersion == 1 || nCMPCTBLOCKVersion == 2) {
2887-
LOCK(cs_main);
2888-
// fProvidesHeaderAndIDs is used to "lock in" version of compact blocks we send (fWantsCmpctWitness)
2889-
if (!State(pfrom.GetId())->fProvidesHeaderAndIDs) {
2890-
State(pfrom.GetId())->fProvidesHeaderAndIDs = true;
2891-
State(pfrom.GetId())->fWantsCmpctWitness = nCMPCTBLOCKVersion == 2;
2892-
}
2893-
if (State(pfrom.GetId())->fWantsCmpctWitness == (nCMPCTBLOCKVersion == 2)) { // ignore later version announces
2894-
State(pfrom.GetId())->fPreferHeaderAndIDs = fAnnounceUsingCMPCTBLOCK;
2895-
// save whether peer selects us as BIP152 high-bandwidth peer
2896-
// (receiving sendcmpct(1) signals high-bandwidth, sendcmpct(0) low-bandwidth)
2897-
pfrom.m_bip152_highbandwidth_from = fAnnounceUsingCMPCTBLOCK;
2898-
}
2899-
if (!State(pfrom.GetId())->fSupportsDesiredCmpctVersion) {
2900-
State(pfrom.GetId())->fSupportsDesiredCmpctVersion = (nCMPCTBLOCKVersion == 2);
2901-
}
2886+
2887+
// Only support compact block relay with witnesses
2888+
if (nCMPCTBLOCKVersion != CMPCTBLOCKS_VERSION) return;
2889+
2890+
LOCK(cs_main);
2891+
// fProvidesHeaderAndIDs is used to "lock in" version of compact blocks we send (fWantsCmpctWitness)
2892+
if (!State(pfrom.GetId())->fProvidesHeaderAndIDs) {
2893+
State(pfrom.GetId())->fProvidesHeaderAndIDs = true;
2894+
State(pfrom.GetId())->fWantsCmpctWitness = nCMPCTBLOCKVersion == 2;
2895+
}
2896+
if (State(pfrom.GetId())->fWantsCmpctWitness == (nCMPCTBLOCKVersion == 2)) { // ignore later version announces
2897+
State(pfrom.GetId())->fPreferHeaderAndIDs = fAnnounceUsingCMPCTBLOCK;
2898+
// save whether peer selects us as BIP152 high-bandwidth peer
2899+
// (receiving sendcmpct(1) signals high-bandwidth, sendcmpct(0) low-bandwidth)
2900+
pfrom.m_bip152_highbandwidth_from = fAnnounceUsingCMPCTBLOCK;
2901+
}
2902+
if (!State(pfrom.GetId())->fSupportsDesiredCmpctVersion) {
2903+
State(pfrom.GetId())->fSupportsDesiredCmpctVersion = (nCMPCTBLOCKVersion == 2);
29022904
}
29032905
return;
29042906
}

test/functional/p2p_compactblocks.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ def make_utxos(self):
179179

180180
# Test "sendcmpct" (between peers preferring the same version):
181181
# - No compact block announcements unless sendcmpct is sent.
182+
# - If sendcmpct is sent with version = 1, the message is ignored.
182183
# - If sendcmpct is sent with version > 2, the message is ignored.
183184
# - If sendcmpct is sent with boolean 0, then block announcements are not
184185
# made with compact blocks.
@@ -221,6 +222,13 @@ def check_announcement_of_new_block(node, peer, predicate):
221222
# Before each test, sync the headers chain.
222223
test_node.request_headers_and_sync(locator=[tip])
223224

225+
# Now try a SENDCMPCT message with too-low version
226+
test_node.send_and_ping(msg_sendcmpct(announce=True, version=1))
227+
check_announcement_of_new_block(node, test_node, lambda p: "cmpctblock" not in p.last_message)
228+
229+
# Headers sync before next test.
230+
test_node.request_headers_and_sync(locator=[tip])
231+
224232
# Now try a SENDCMPCT message with too-high version
225233
test_node.send_and_ping(msg_sendcmpct(announce=True, version=3))
226234
check_announcement_of_new_block(node, test_node, lambda p: "cmpctblock" not in p.last_message)

0 commit comments

Comments
 (0)