diff --git a/src/net.h b/src/net.h --- a/src/net.h +++ b/src/net.h @@ -667,6 +667,8 @@ bool fOneShot; bool m_manual_connection; bool fClient; + // after BIP159 + bool m_limited_node; const bool fInbound; std::atomic_bool fSuccessfullyConnected; std::atomic_bool fDisconnect; diff --git a/src/net.cpp b/src/net.cpp --- a/src/net.cpp +++ b/src/net.cpp @@ -2878,6 +2878,8 @@ m_manual_connection = false; // set by version message fClient = false; + // set by version message + m_limited_node = false; fFeeler = false; fSuccessfullyConnected = false; fDisconnect = false; diff --git a/src/net_processing.cpp b/src/net_processing.cpp --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -1074,6 +1074,7 @@ const int nNewHeight = pindexNew->nHeight; connman->SetBestHeight(nNewHeight); + SetServiceFlagsIBDCache(!fInitialDownload); if (!fInitialDownload) { // Find the hashes of all blocks that weren't previously in the best // chain. @@ -1912,7 +1913,17 @@ pfrom->cleanSubVer = cleanSubVer; } pfrom->nStartingHeight = nStartingHeight; - pfrom->fClient = !(nServices & NODE_NETWORK); + + // set nodes not relaying blocks and tx and not serving (parts) of the + // historical blockchain as "clients" + pfrom->fClient = (!(nServices & NODE_NETWORK) && + !(nServices & NODE_NETWORK_LIMITED)); + + // set nodes not capable of serving the complete blockchain history as + // "limited nodes" + pfrom->m_limited_node = + (!(nServices & NODE_NETWORK) && (nServices & NODE_NETWORK_LIMITED)); + { LOCK(pfrom->cs_filter); // set to true after we get the first filter* message @@ -2082,7 +2093,8 @@ // We only bother storing full nodes, though this may include things // which we would not make an outbound connection to, in part // because we may make feeler connections to them. - if (!MayHaveUsefulAddressDB(addr.nServices)) { + if (!MayHaveUsefulAddressDB(addr.nServices) && + !HasAllDesirableServiceFlags(addr.nServices)) { continue; } @@ -4204,7 +4216,8 @@ // Message: getdata (blocks) // std::vector vGetData; - if (!pto->fClient && (fFetch || !IsInitialBlockDownload()) && + if (!pto->fClient && + ((fFetch && !pto->m_limited_node) || !IsInitialBlockDownload()) && state.nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) { std::vector vToDownload; NodeId staller = -1; diff --git a/src/protocol.h b/src/protocol.h --- a/src/protocol.h +++ b/src/protocol.h @@ -346,9 +346,13 @@ * If the NODE_NONE return value is changed, contrib/seeds/makeseeds.py * should be updated appropriately to filter for the same nodes. */ -static ServiceFlags GetDesirableServiceFlags(ServiceFlags services) { - return ServiceFlags(NODE_NETWORK); -} +ServiceFlags GetDesirableServiceFlags(ServiceFlags services); + +/** + * Set the current IBD status in order to figure out the desirable service + * flags + */ +void SetServiceFlagsIBDCache(bool status); /** * A shortcut for (services & GetDesirableServiceFlags(services)) @@ -361,10 +365,10 @@ /** * Checks if a peer with the given service flags may be capable of having a - * robust address-storage DB. Currently an alias for checking NODE_NETWORK. + * robust address-storage DB. */ static inline bool MayHaveUsefulAddressDB(ServiceFlags services) { - return services & NODE_NETWORK; + return (services & NODE_NETWORK) || (services & NODE_NETWORK_LIMITED); } /** diff --git a/src/protocol.cpp b/src/protocol.cpp --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -13,6 +13,9 @@ #ifndef WIN32 #include #endif +#include + +static std::atomic g_initial_block_download_completed(false); namespace NetMsgType { const char *VERSION = "version"; @@ -181,6 +184,18 @@ return false; } +ServiceFlags GetDesirableServiceFlags(ServiceFlags services) { + if ((services & NODE_NETWORK_LIMITED) && + g_initial_block_download_completed) { + return ServiceFlags(NODE_NETWORK_LIMITED); + } + return ServiceFlags(NODE_NETWORK); +} + +void SetServiceFlagsIBDCache(bool state) { + g_initial_block_download_completed = state; +} + CAddress::CAddress() : CService() { Init(); } diff --git a/test/functional/p2p_node_network_limited.py b/test/functional/p2p_node_network_limited.py --- a/test/functional/p2p_node_network_limited.py +++ b/test/functional/p2p_node_network_limited.py @@ -8,17 +8,26 @@ and that it responds to getdata requests for blocks correctly: - send a block within 288 + 2 of the tip - disconnect peers who request blocks older than that.""" -from test_framework.messages import CInv, msg_getdata -from test_framework.mininode import NODE_BLOOM, NODE_NETWORK_LIMITED, NODE_BITCOIN_CASH, network_thread_start, P2PInterface +from test_framework.messages import CInv, msg_getdata, msg_verack +from test_framework.mininode import NODE_BLOOM, NODE_NETWORK_LIMITED, NODE_BITCOIN_CASH, P2PInterface, wait_until, mininode_lock, network_thread_start, network_thread_join from test_framework.test_framework import BitcoinTestFramework -from test_framework.util import assert_equal +from test_framework.util import assert_equal, disconnect_nodes, connect_nodes_bi, sync_blocks class P2PIgnoreInv(P2PInterface): + firstAddrnServices = 0 + def on_inv(self, message): # The node will send us invs for other blocks. Ignore them. pass + def on_addr(self, message): + self.firstAddrnServices = message.addrs[0].nServices + + def wait_for_addr(self, timeout=5): + def test_function(): return self.last_message.get("addr") + wait_until(test_function, timeout=timeout, lock=mininode_lock) + def send_getdata_for_block(self, blockhash): getdata_request = msg_getdata() getdata_request.inv.append(CInv(2, int(blockhash, 16))) @@ -28,8 +37,20 @@ class NodeNetworkLimitedTest(BitcoinTestFramework): def set_test_params(self): self.setup_clean_chain = True - self.num_nodes = 1 - self.extra_args = [['-prune=550']] + self.num_nodes = 3 + self.extra_args = [['-prune=550', '-addrmantest'], [], []] + + def disconnect_all(self): + disconnect_nodes(self.nodes[0], self.nodes[1]) + disconnect_nodes(self.nodes[1], self.nodes[0]) + disconnect_nodes(self.nodes[2], self.nodes[1]) + disconnect_nodes(self.nodes[2], self.nodes[0]) + disconnect_nodes(self.nodes[0], self.nodes[2]) + disconnect_nodes(self.nodes[1], self.nodes[2]) + + def setup_network(self): + super(NodeNetworkLimitedTest, self).setup_network() + self.disconnect_all() def run_test(self): node = self.nodes[0].add_p2p_connection(P2PIgnoreInv()) @@ -47,7 +68,9 @@ self.log.info( "Mine enough blocks to reach the NODE_NETWORK_LIMITED range.") - blocks = self.nodes[0].generate(292) + connect_nodes_bi(self.nodes[0], self.nodes[1]) + blocks = self.nodes[1].generate(292) + sync_blocks([self.nodes[0], self.nodes[1]]) self.log.info("Make sure we can max retrive block at tip-288.") # last block in valid range @@ -60,6 +83,50 @@ node.send_getdata_for_block(blocks[0]) node.wait_for_disconnect(5) + self.log.info("Check local address relay, do a fresh connection.") + self.nodes[0].disconnect_p2ps() + network_thread_join() + node1 = self.nodes[0].add_p2p_connection(P2PIgnoreInv()) + network_thread_start() + node1.wait_for_verack() + node1.send_message(msg_verack()) + + node1.wait_for_addr() + # must relay address with NODE_NETWORK_LIMITED + assert_equal(node1.firstAddrnServices, expected_services) + + self.nodes[0].disconnect_p2ps() + node1.wait_for_disconnect() + + # connect unsynced node 2 with pruned NODE_NETWORK_LIMITED peer + # because node 2 is in IBD and node 0 is a NODE_NETWORK_LIMITED peer, sync must not be possible + connect_nodes_bi(self.nodes[0], self.nodes[2]) + try: + sync_blocks([self.nodes[0], self.nodes[2]], timeout=5) + except: + pass + # node2 must remain at heigh 0 + assert_equal(self.nodes[2].getblockheader( + self.nodes[2].getbestblockhash())['height'], 0) + + # now connect also to node 1 (non pruned) + connect_nodes_bi(self.nodes[1], self.nodes[2]) + + # sync must be possible + sync_blocks(self.nodes) + + # disconnect all peers + self.disconnect_all() + + # mine 10 blocks on node 0 (pruned node) + self.nodes[0].generate(10) + + # connect node1 (non pruned) with node0 (pruned) and check if the can sync + connect_nodes_bi(self.nodes[0], self.nodes[1]) + + # sync must be possible, node 1 is no longer in IBD and should therefore connect to node 0 (NODE_NETWORK_LIMITED) + sync_blocks([self.nodes[0], self.nodes[1]]) + if __name__ == '__main__': NodeNetworkLimitedTest().main()