diff --git a/doc/release-notes-7755.md b/doc/release-notes-7755.md new file mode 100644 index 000000000000..006d1ca899ca --- /dev/null +++ b/doc/release-notes-7755.md @@ -0,0 +1,7 @@ +Proof of Service +---------------- + +DKG members now also vote a member bad if none of their masternode connections to it advertise: + +* transaction relay (`relay=1` in the `version` message); +* compact block filters (`NODE_COMPACT_FILTERS`). diff --git a/src/active/dkgsession.cpp b/src/active/dkgsession.cpp index 313da0ff30bd..b50bd966f194 100644 --- a/src/active/dkgsession.cpp +++ b/src/active/dkgsession.cpp @@ -233,13 +233,27 @@ void ActiveDKGSession::VerifyConnectionAndMinProtoVersions(CConnman& connman) co CDKGLogger logger(*this, __func__, __LINE__); - Uint256HashMap protoMap; + struct PeerInfo { + int version{0}; + // Only masternode connections are judged: block-relay-only connections always advertise + // relay=0, and one connection that provides a service is enough. + bool has_mn_connection{false}; + bool relays_txs{false}; + bool serves_compact_filters{false}; + }; + Uint256HashMap peerInfo; connman.ForEachNode([&](const CNode* pnode) { auto verifiedProRegTxHash = pnode->GetVerifiedProRegTxHash(); if (verifiedProRegTxHash.IsNull()) { return; } - protoMap.emplace(verifiedProRegTxHash, pnode->nVersion); + auto [it, inserted] = peerInfo.try_emplace(verifiedProRegTxHash); + if (inserted) it->second.version = pnode->nVersion; + if (pnode->m_masternode_connection) { + it->second.has_mn_connection = true; + it->second.relays_txs |= pnode->m_version_relay_txs; + it->second.serves_compact_filters |= (pnode->m_version_services & NODE_COMPACT_FILTERS) != 0; + } }); bool fShouldAllMembersBeConnected = IsAllMembersConnectedEnabled(params.type, m_sporkman); @@ -247,14 +261,22 @@ void ActiveDKGSession::VerifyConnectionAndMinProtoVersions(CConnman& connman) co if (m->dmn->proTxHash == myProTxHash) { continue; } - if (auto it = protoMap.find(m->dmn->proTxHash); it == protoMap.end()) { + if (auto it = peerInfo.find(m->dmn->proTxHash); it == peerInfo.end()) { m->badConnection = fShouldAllMembersBeConnected; if (m->badConnection) { logger.Batch("%s is not connected to us, badConnection=1", m->dmn->proTxHash.ToString()); } - } else if (it->second < MIN_MASTERNODE_PROTO_VERSION) { + } else if (it->second.version < MIN_MASTERNODE_PROTO_VERSION) { + m->badConnection = true; + logger.Batch("%s does not have min proto version %d (has %d)", m->dmn->proTxHash.ToString(), + MIN_MASTERNODE_PROTO_VERSION, it->second.version); + } else if (it->second.has_mn_connection && !it->second.relays_txs) { + // A masternode signs InstantSend locks only for transactions in its own mempool + m->badConnection = true; + logger.Batch("%s does not relay transactions", m->dmn->proTxHash.ToString()); + } else if (it->second.has_mn_connection && !it->second.serves_compact_filters) { m->badConnection = true; - logger.Batch("%s does not have min proto version %d (has %d)", m->dmn->proTxHash.ToString(), MIN_MASTERNODE_PROTO_VERSION, it->second); + logger.Batch("%s does not serve compact block filters", m->dmn->proTxHash.ToString()); } if (m_mn_metaman.OutboundFailedTooManyTimes(m->dmn->proTxHash)) { m->badConnection = true; diff --git a/src/net.h b/src/net.h index 6493128c58c4..3b1ded7de0fb 100644 --- a/src/net.h +++ b/src/net.h @@ -817,6 +817,10 @@ class CNode std::atomic m_masternode_probe_connection{false}; // If 'true', we identified it as an intra-quorum relay connection std::atomic m_masternode_iqr_connection{false}; + // Whether the peer asked for transaction relay in its VERSION message (fRelay) + std::atomic m_version_relay_txs{false}; + // Service flags the peer advertised in its VERSION message + std::atomic m_version_services{NODE_NONE}; CSemaphoreGrant grantOutbound; std::atomic nRefCount{0}; diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 6c6b9ea593ab..53e4834bdbcf 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -4057,6 +4057,8 @@ void PeerManagerImpl::ProcessMessage( } pfrom.m_has_all_wanted_services = HasAllDesirableServiceFlags(nServices); + pfrom.m_version_services = nServices; + pfrom.m_version_relay_txs = fRelay; peer->m_their_services = nServices; pfrom.SetAddrLocal(addrMe); { diff --git a/test/functional/feature_llmq_simplepose.py b/test/functional/feature_llmq_simplepose.py index 3cf92f82c138..4ad57c34c7e3 100755 --- a/test/functional/feature_llmq_simplepose.py +++ b/test/functional/feature_llmq_simplepose.py @@ -13,6 +13,8 @@ import time from test_framework.masternodes import check_banned, check_punished +from test_framework.messages import NODE_COMPACT_FILTERS +from test_framework.p2p import P2P_SERVICES, P2PInterface from test_framework.test_framework import ( DashTestFramework, MasternodeInfo, @@ -22,6 +24,19 @@ # See version.h MIN_MASTERNODE_PROTO_VERSION = 70242 + +class MasternodePeer(P2PInterface): + """Connects to a masternode the way another masternode does, advertising the given relay flag""" + def __init__(self, relay): + super().__init__() + self.version_relay = relay + + def peer_connect_send_version(self, services): + super().peer_connect_send_version(services) + self.on_connection_send_msg.relay = self.version_relay + self.on_connection_send_msg.other_masternode = True + + class LLMQSimplePoSeTest(DashTestFramework): def set_test_params(self): # rotating quorums add instability for this functional tests @@ -84,6 +99,15 @@ def run_test(self): # With PoSe off there should be no punishing for outdated nodes self.test_no_banning(self.force_old_mn_proto, 3) + self.repair_masternodes(True) + self.reset_probe_timeouts() + + for relay, services, reason in [(0, P2P_SERVICES | NODE_COMPACT_FILTERS, "does not relay transactions"), + (1, P2P_SERVICES, "does not serve compact block filters")]: + self.test_no_service(relay, services, reason, banned=not self.options.disable_spork23) + self.repair_masternodes(True) + self.reset_probe_timeouts() + def isolate_mn(self, mn: MasternodeInfo): mn.get_node(self).setnetworkactive(False) self.wait_until(lambda: mn.get_node(self).getconnectioncount() == 0) @@ -108,6 +132,108 @@ def force_old_mn_proto(self, mn: MasternodeInfo): self.reset_probe_timeouts() return False, True + def test_no_service(self, relay, services, reason, banned): + # A member is judged by what our masternode connections to it advertise in their version message. A stock + # masternode always advertises both services, so a P2P peer stands in for the connections to one member. + mn = self.mninfo[0] + node = mn.get_node(self) + others = [m for m in self.mninfo if m is not mn] + expected_complaints = len(others) if banned else 0 + for i in range(2): + self.log.info(f"Testing PoSe {'banning' if banned else 'no banning'} of a masternode that {reason} {i + 1}/2") + self.reset_probe_timeouts() + with others[0].get_node(self).assert_debug_log([reason] if banned else [], unexpected_msgs=[] if banned else [reason]): + self.mine_quorum_no_service(mn, others, relay, services, expected_complaints) + for other in others: + other.get_node(self).disconnect_p2ps() + node.setnetworkactive(True) + force_finish_mnsync(node) + self.connect_nodes(mn.nodeIdx, 0) + self.sync_blocks() + if check_banned(self.nodes[0], mn): + break + assert_equal(check_banned(self.nodes[0], mn), banned) + if not banned: + assert not check_punished(self.nodes[0], mn) + + def mine_quorum_no_service(self, mn, others, relay, services, expected_complaints): + # Like mine_quorum, except that mn goes offline once every member holds its contribution and a peer stands + # in for it on each other member with a masternode connection that lacks the service, so that the complain + # phase judges mn by that connection alone. + self.log.info(f"Mining quorum with a stand-in for {mn.proTxHash}: expected_complaints={expected_complaints}") + nodes = [self.nodes[0]] + [m.get_node(self) for m in others] + spork23_active = self.nodes[0].spork('show')['SPORK_23_QUORUM_POSE'] <= 1 + + # move forward to next DKG + skip_count = 24 - (self.nodes[0].getblockcount() % 24) + self.bump_mocktime(1) + self.generate(self.nodes[0], skip_count) + + q = self.nodes[0].getbestblockhash() + self.log.info("Expected quorum_hash:"+str(q)) + self.log.info("Waiting for phase 1 (init)") + self.wait_for_quorum_phase(q, 1, len(others), None, 0, others) + self.wait_for_quorum_connections(q, self.llmq_size - 1, others, wait_proc=lambda: self.bump_mocktime(1)) + if spork23_active: + self.wait_for_masternode_probes(q, others, wait_proc=lambda: self.bump_mocktime(1)) + + self.move_blocks(self.nodes, 2) + + self.log.info("Waiting for phase 2 (contribute)") + self.wait_for_quorum_phase(q, 2, len(others), "receivedContributions", self.llmq_size, others) + + self.log.info("Replacing the connections to the masternode with stand-ins") + mn.get_node(self).setnetworkactive(False) + for other in others: + other_node = other.get_node(self) + self.wait_until(lambda: all(p.get("verified_proregtx_hash") != mn.proTxHash for p in other_node.getpeerinfo())) + peer = other_node.add_p2p_connection(MasternodePeer(relay), services=services) + peer_ids = [p["id"] for p in other_node.getpeerinfo() if p["subver"] == peer.strSubVer] + assert_equal(len(peer_ids), 1) + assert other_node.mnauth(peer_ids[0], mn.proTxHash, mn.pubKeyOperator) + + self.move_blocks(nodes, 2) + + self.log.info("Waiting for phase 3 (complain)") + self.wait_for_quorum_phase(q, 3, len(others), "receivedComplaints", expected_complaints, others) + + self.move_blocks(nodes, 2) + + self.log.info("Waiting for phase 4 (justify)") + self.wait_for_quorum_phase(q, 4, len(others), "receivedJustifications", 0, others) + + self.move_blocks(nodes, 2) + + self.log.info("Waiting for phase 5 (commit)") + self.wait_for_quorum_phase(q, 5, len(others), "receivedPrematureCommitments", len(others), others) + + self.move_blocks(nodes, 2) + + self.log.info("Waiting for phase 6 (mining)") + self.wait_for_quorum_phase(q, 6, len(others), None, 0, others) + + self.log.info("Waiting final commitment") + self.wait_for_quorum_commitment(q, others) + + self.log.info("Waiting final commitments on mining node") + self.wait_for_quorum_commitments_on_miner(q, others) + + self.log.info("Mining final commitment") + self.bump_mocktime(1) + self.nodes[0].getblocktemplate() # this calls CreateNewBlock + self.generate(self.nodes[0], 1, sync_fun=lambda: self.sync_blocks(nodes)) + + self.log.info("Waiting for quorum to appear in the list") + self.wait_for_quorum_list(q, nodes) + assert_equal(q, self.nodes[0].quorum("list", 1)["llmq_test"][0]) + + # Mine 8 (SIGN_HEIGHT_OFFSET) more blocks to make sure that the new quorum gets eligible for signing sessions + self.generate(self.nodes[0], 8, sync_fun=lambda: self.sync_blocks(nodes)) + + for m in others: + assert not check_punished(self.nodes[0], m) + assert not check_banned(self.nodes[0], m) + def test_no_banning(self, invalidate_proc, expected_connections=None): [_, instant_ban] = invalidate_proc(self.mninfo[0]) iters = 2 if instant_ban else 6 diff --git a/test/functional/test_framework/messages.py b/test/functional/test_framework/messages.py index 690028c61803..eedaf604ea0d 100755 --- a/test/functional/test_framework/messages.py +++ b/test/functional/test_framework/messages.py @@ -1633,7 +1633,7 @@ def serialize(self): # Objects that correspond to messages on the wire class msg_version: - __slots__ = ("addrFrom", "addrTo", "nNonce", "relay", "nServices", + __slots__ = ("addrFrom", "addrTo", "mnauth_challenge", "nNonce", "other_masternode", "relay", "nServices", "nStartingHeight", "nTime", "nVersion", "strSubVer") msgtype = b"version" @@ -1647,6 +1647,8 @@ def __init__(self): self.strSubVer = '' self.nStartingHeight = -1 self.relay = 0 + self.mnauth_challenge = 0 + self.other_masternode = False def deserialize(self, f): self.nVersion = struct.unpack("