Merge #10387: Eventually connect to NODE_NETWORK_LIMITED peers

eb91835 Add setter for g_initial_block_download_completed (Jonas Schnelli)
3f56df5 [QA] add NODE_NETWORK_LIMITED address relay and sync test (Jonas Schnelli)
158e1a6 [QA] fix mininode CAddress ser/deser (Jonas Schnelli)
fa999af [QA] Allow addrman loopback tests (add debug option -addrmantest) (Jonas Schnelli)
6fe57bd Connect to peers signaling NODE_NETWORK_LIMITED when out-of-IBD (Jonas Schnelli)
31c45a9 Accept addresses with NODE_NETWORK_LIMITED flag (Jonas Schnelli)

Pull request description:

  Eventually connect to peers signalling NODE_NETWORK_LIMITED if we are out of IBD.
  Accept and relay NODE_NETWORK_LIMITED peers in addrman.

Tree-SHA512: 8a238fc97f767f81cae1866d6cc061390f23a72af4a711d2f7158c77f876017986abb371d213d1c84019eef7be4ca951e8e6f83fda36769c4e1a1d763f787037
This commit is contained in:
Wladimir J. van der Laan 2018-03-01 15:14:09 +01:00
commit 5c2aff8d95
No known key found for this signature in database
GPG Key ID: 1E4AED62986CD25D
8 changed files with 119 additions and 23 deletions

View File

@ -448,6 +448,7 @@ std::string HelpMessage(HelpMessageMode mode)
strUsage += HelpMessageOpt("-limitdescendantcount=<n>", strprintf("Do not accept transactions if any ancestor would have <n> or more in-mempool descendants (default: %u)", DEFAULT_DESCENDANT_LIMIT)); strUsage += HelpMessageOpt("-limitdescendantcount=<n>", strprintf("Do not accept transactions if any ancestor would have <n> or more in-mempool descendants (default: %u)", DEFAULT_DESCENDANT_LIMIT));
strUsage += HelpMessageOpt("-limitdescendantsize=<n>", strprintf("Do not accept transactions if any ancestor would have more than <n> kilobytes of in-mempool descendants (default: %u).", DEFAULT_DESCENDANT_SIZE_LIMIT)); strUsage += HelpMessageOpt("-limitdescendantsize=<n>", strprintf("Do not accept transactions if any ancestor would have more than <n> kilobytes of in-mempool descendants (default: %u).", DEFAULT_DESCENDANT_SIZE_LIMIT));
strUsage += HelpMessageOpt("-vbparams=deployment:start:end", "Use given start/end times for specified version bits deployment (regtest-only)"); strUsage += HelpMessageOpt("-vbparams=deployment:start:end", "Use given start/end times for specified version bits deployment (regtest-only)");
strUsage += HelpMessageOpt("-addrmantest", "Allows to test address relay on localhost");
} }
strUsage += HelpMessageOpt("-debug=<category>", strprintf(_("Output debugging information (default: %u, supplying <category> is optional)"), 0) + ". " + strUsage += HelpMessageOpt("-debug=<category>", strprintf(_("Output debugging information (default: %u, supplying <category> is optional)"), 0) + ". " +
_("If <category> is not supplied or if <category> = 1, output all debugging information.") + " " + _("<category> can be:") + " " + ListLogCategories() + "."); _("If <category> is not supplied or if <category> = 1, output all debugging information.") + " " + _("<category> can be:") + " " + ListLogCategories() + ".");

View File

@ -181,6 +181,10 @@ void AdvertiseLocal(CNode *pnode)
if (fListen && pnode->fSuccessfullyConnected) if (fListen && pnode->fSuccessfullyConnected)
{ {
CAddress addrLocal = GetLocalAddress(&pnode->addr, pnode->GetLocalServices()); CAddress addrLocal = GetLocalAddress(&pnode->addr, pnode->GetLocalServices());
if (gArgs.GetBoolArg("-addrmantest", false)) {
// use IPv4 loopback during addrmantest
addrLocal = CAddress(CService(LookupNumeric("127.0.0.1", GetListenPort())), pnode->GetLocalServices());
}
// If discovery is enabled, sometimes give our peer the address it // If discovery is enabled, sometimes give our peer the address it
// tells us that it sees us as in case it has a better idea of our // tells us that it sees us as in case it has a better idea of our
// address than we do. // address than we do.
@ -189,7 +193,7 @@ void AdvertiseLocal(CNode *pnode)
{ {
addrLocal.SetIP(pnode->GetAddrLocal()); addrLocal.SetIP(pnode->GetAddrLocal());
} }
if (addrLocal.IsRoutable()) if (addrLocal.IsRoutable() || gArgs.GetBoolArg("-addrmantest", false))
{ {
LogPrint(BCLog::NET, "AdvertiseLocal: advertising address %s\n", addrLocal.ToString()); LogPrint(BCLog::NET, "AdvertiseLocal: advertising address %s\n", addrLocal.ToString());
FastRandomContext insecure_rand; FastRandomContext insecure_rand;
@ -2718,6 +2722,7 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
fOneShot = false; fOneShot = false;
m_manual_connection = false; m_manual_connection = false;
fClient = false; // set by version message fClient = false; // set by version message
m_limited_node = false; // set by version message
fFeeler = false; fFeeler = false;
fSuccessfullyConnected = false; fSuccessfullyConnected = false;
fDisconnect = false; fDisconnect = false;

View File

@ -641,6 +641,7 @@ public:
bool fOneShot; bool fOneShot;
bool m_manual_connection; bool m_manual_connection;
bool fClient; bool fClient;
bool m_limited_node; //after BIP159
const bool fInbound; const bool fInbound;
std::atomic_bool fSuccessfullyConnected; std::atomic_bool fSuccessfullyConnected;
std::atomic_bool fDisconnect; std::atomic_bool fDisconnect;

View File

@ -892,6 +892,7 @@ void PeerLogicValidation::UpdatedBlockTip(const CBlockIndex *pindexNew, const CB
const int nNewHeight = pindexNew->nHeight; const int nNewHeight = pindexNew->nHeight;
connman->SetBestHeight(nNewHeight); connman->SetBestHeight(nNewHeight);
SetServiceFlagsIBDCache(!fInitialDownload);
if (!fInitialDownload) { if (!fInitialDownload) {
// Find the hashes of all blocks that weren't previously in the best chain. // Find the hashes of all blocks that weren't previously in the best chain.
std::vector<uint256> vHashes; std::vector<uint256> vHashes;
@ -1642,7 +1643,13 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
pfrom->cleanSubVer = cleanSubVer; pfrom->cleanSubVer = cleanSubVer;
} }
pfrom->nStartingHeight = nStartingHeight; 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); LOCK(pfrom->cs_filter);
pfrom->fRelayTxes = fRelay; // set to true after we get the first filter* message pfrom->fRelayTxes = fRelay; // set to true after we get the first filter* message
@ -1801,7 +1808,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
// We only bother storing full nodes, though this may include // We only bother storing full nodes, though this may include
// things which we would not make an outbound connection to, in // things which we would not make an outbound connection to, in
// part because we may make feeler connections to them. // part because we may make feeler connections to them.
if (!MayHaveUsefulAddressDB(addr.nServices)) if (!MayHaveUsefulAddressDB(addr.nServices) && !HasAllDesirableServiceFlags(addr.nServices))
continue; continue;
if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60) if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60)
@ -3611,7 +3618,7 @@ bool PeerLogicValidation::SendMessages(CNode* pto, std::atomic<bool>& interruptM
// Message: getdata (blocks) // Message: getdata (blocks)
// //
std::vector<CInv> vGetData; std::vector<CInv> vGetData;
if (!pto->fClient && (fFetch || !IsInitialBlockDownload()) && state.nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) { if (!pto->fClient && ((fFetch && !pto->m_limited_node) || !IsInitialBlockDownload()) && state.nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
std::vector<const CBlockIndex*> vToDownload; std::vector<const CBlockIndex*> vToDownload;
NodeId staller = -1; NodeId staller = -1;
FindNextBlocksToDownload(pto->GetId(), MAX_BLOCKS_IN_TRANSIT_PER_PEER - state.nBlocksInFlight, vToDownload, staller, consensusParams); FindNextBlocksToDownload(pto->GetId(), MAX_BLOCKS_IN_TRANSIT_PER_PEER - state.nBlocksInFlight, vToDownload, staller, consensusParams);

View File

@ -12,6 +12,8 @@
# include <arpa/inet.h> # include <arpa/inet.h>
#endif #endif
static std::atomic<bool> g_initial_block_download_completed(false);
namespace NetMsgType { namespace NetMsgType {
const char *VERSION="version"; const char *VERSION="version";
const char *VERACK="verack"; const char *VERACK="verack";
@ -127,6 +129,17 @@ bool CMessageHeader::IsValid(const MessageStartChars& pchMessageStartIn) const
} }
ServiceFlags GetDesirableServiceFlags(ServiceFlags services) {
if ((services & NODE_NETWORK_LIMITED) && g_initial_block_download_completed) {
return ServiceFlags(NODE_NETWORK_LIMITED | NODE_WITNESS);
}
return ServiceFlags(NODE_NETWORK | NODE_WITNESS);
}
void SetServiceFlagsIBDCache(bool state) {
g_initial_block_download_completed = state;
}
CAddress::CAddress() : CService() CAddress::CAddress() : CService()
{ {

View File

@ -15,6 +15,7 @@
#include <uint256.h> #include <uint256.h>
#include <version.h> #include <version.h>
#include <atomic>
#include <stdint.h> #include <stdint.h>
#include <string> #include <string>
@ -301,9 +302,10 @@ enum ServiceFlags : uint64_t {
* If the NODE_NONE return value is changed, contrib/seeds/makeseeds.py * If the NODE_NONE return value is changed, contrib/seeds/makeseeds.py
* should be updated appropriately to filter for the same nodes. * should be updated appropriately to filter for the same nodes.
*/ */
static ServiceFlags GetDesirableServiceFlags(ServiceFlags services) { ServiceFlags GetDesirableServiceFlags(ServiceFlags services);
return ServiceFlags(NODE_NETWORK | NODE_WITNESS);
} /** Set the current IBD status in order to figure out the desirable service flags */
void SetServiceFlagsIBDCache(bool status);
/** /**
* A shortcut for (services & GetDesirableServiceFlags(services)) * A shortcut for (services & GetDesirableServiceFlags(services))
@ -316,10 +318,10 @@ static inline bool HasAllDesirableServiceFlags(ServiceFlags services) {
/** /**
* Checks if a peer with the given service flags may be capable of having a * 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) { static inline bool MayHaveUsefulAddressDB(ServiceFlags services) {
return services & NODE_NETWORK; return (services & NODE_NETWORK) || (services & NODE_NETWORK_LIMITED);
} }
/** A CService with information about it as peer */ /** A CService with information about it as peer */

View File

@ -8,16 +8,21 @@ Tests that a node configured with -prune=550 signals NODE_NETWORK_LIMITED correc
and that it responds to getdata requests for blocks correctly: and that it responds to getdata requests for blocks correctly:
- send a block within 288 + 2 of the tip - send a block within 288 + 2 of the tip
- disconnect peers who request blocks older than that.""" - disconnect peers who request blocks older than that."""
from test_framework.messages import CInv, msg_getdata from test_framework.messages import CInv, msg_getdata, msg_verack
from test_framework.mininode import NODE_BLOOM, NODE_NETWORK_LIMITED, NODE_WITNESS, NetworkThread, P2PInterface from test_framework.mininode import NODE_BLOOM, NODE_NETWORK_LIMITED, NODE_WITNESS, P2PInterface, wait_until, mininode_lock, network_thread_start, network_thread_join
from test_framework.test_framework import BitcoinTestFramework 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): class P2PIgnoreInv(P2PInterface):
firstAddrnServices = 0
def on_inv(self, message): def on_inv(self, message):
# The node will send us invs for other blocks. Ignore them. # The node will send us invs for other blocks. Ignore them.
pass pass
def on_addr(self, message):
self.firstAddrnServices = message.addrs[0].nServices
def wait_for_addr(self, timeout=5):
test_function = lambda: self.last_message.get("addr")
wait_until(test_function, timeout=timeout, lock=mininode_lock)
def send_getdata_for_block(self, blockhash): def send_getdata_for_block(self, blockhash):
getdata_request = msg_getdata() getdata_request = msg_getdata()
getdata_request.inv.append(CInv(2, int(blockhash, 16))) getdata_request.inv.append(CInv(2, int(blockhash, 16)))
@ -26,12 +31,24 @@ class P2PIgnoreInv(P2PInterface):
class NodeNetworkLimitedTest(BitcoinTestFramework): class NodeNetworkLimitedTest(BitcoinTestFramework):
def set_test_params(self): def set_test_params(self):
self.setup_clean_chain = True self.setup_clean_chain = True
self.num_nodes = 1 self.num_nodes = 3
self.extra_args = [['-prune=550']] self.extra_args = [['-prune=550', '-addrmantest'], [], []]
def disconnect_all(self):
disconnect_nodes(self.nodes[0], 1)
disconnect_nodes(self.nodes[1], 0)
disconnect_nodes(self.nodes[2], 1)
disconnect_nodes(self.nodes[2], 0)
disconnect_nodes(self.nodes[0], 2)
disconnect_nodes(self.nodes[1], 2)
def setup_network(self):
super(NodeNetworkLimitedTest, self).setup_network()
self.disconnect_all()
def run_test(self): def run_test(self):
node = self.nodes[0].add_p2p_connection(P2PIgnoreInv()) node = self.nodes[0].add_p2p_connection(P2PIgnoreInv())
NetworkThread().start() network_thread_start()
node.wait_for_verack() node.wait_for_verack()
expected_services = NODE_BLOOM | NODE_WITNESS | NODE_NETWORK_LIMITED expected_services = NODE_BLOOM | NODE_WITNESS | NODE_NETWORK_LIMITED
@ -43,7 +60,9 @@ class NodeNetworkLimitedTest(BitcoinTestFramework):
assert_equal(int(self.nodes[0].getnetworkinfo()['localservices'], 16), expected_services) assert_equal(int(self.nodes[0].getnetworkinfo()['localservices'], 16), expected_services)
self.log.info("Mine enough blocks to reach the NODE_NETWORK_LIMITED range.") 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, 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.") self.log.info("Make sure we can max retrive block at tip-288.")
node.send_getdata_for_block(blocks[1]) # last block in valid range node.send_getdata_for_block(blocks[1]) # last block in valid range
@ -53,5 +72,48 @@ class NodeNetworkLimitedTest(BitcoinTestFramework):
node.send_getdata_for_block(blocks[0]) # first block outside of the 288+2 limit node.send_getdata_for_block(blocks[0]) # first block outside of the 288+2 limit
node.wait_for_disconnect(5) 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, 1036)
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, 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, 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, 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__': if __name__ == '__main__':
NodeNetworkLimitedTest().main() NodeNetworkLimitedTest().main()

17
test/functional/test_framework/messages.py Normal file → Executable file
View File

@ -186,19 +186,24 @@ def ToHex(obj):
class CAddress(): class CAddress():
def __init__(self): def __init__(self):
self.time = 0
self.nServices = 1 self.nServices = 1
self.pchReserved = b"\x00" * 10 + b"\xff" * 2 self.pchReserved = b"\x00" * 10 + b"\xff" * 2
self.ip = "0.0.0.0" self.ip = "0.0.0.0"
self.port = 0 self.port = 0
def deserialize(self, f): def deserialize(self, f, with_time=True):
if with_time:
self.time = struct.unpack("<i", f.read(4))[0]
self.nServices = struct.unpack("<Q", f.read(8))[0] self.nServices = struct.unpack("<Q", f.read(8))[0]
self.pchReserved = f.read(12) self.pchReserved = f.read(12)
self.ip = socket.inet_ntoa(f.read(4)) self.ip = socket.inet_ntoa(f.read(4))
self.port = struct.unpack(">H", f.read(2))[0] self.port = struct.unpack(">H", f.read(2))[0]
def serialize(self): def serialize(self, with_time=True):
r = b"" r = b""
if with_time:
r += struct.pack("<i", self.time)
r += struct.pack("<Q", self.nServices) r += struct.pack("<Q", self.nServices)
r += self.pchReserved r += self.pchReserved
r += socket.inet_aton(self.ip) r += socket.inet_aton(self.ip)
@ -856,11 +861,11 @@ class msg_version():
self.nServices = struct.unpack("<Q", f.read(8))[0] self.nServices = struct.unpack("<Q", f.read(8))[0]
self.nTime = struct.unpack("<q", f.read(8))[0] self.nTime = struct.unpack("<q", f.read(8))[0]
self.addrTo = CAddress() self.addrTo = CAddress()
self.addrTo.deserialize(f) self.addrTo.deserialize(f, False)
if self.nVersion >= 106: if self.nVersion >= 106:
self.addrFrom = CAddress() self.addrFrom = CAddress()
self.addrFrom.deserialize(f) self.addrFrom.deserialize(f, False)
self.nNonce = struct.unpack("<Q", f.read(8))[0] self.nNonce = struct.unpack("<Q", f.read(8))[0]
self.strSubVer = deser_string(f) self.strSubVer = deser_string(f)
else: else:
@ -888,8 +893,8 @@ class msg_version():
r += struct.pack("<i", self.nVersion) r += struct.pack("<i", self.nVersion)
r += struct.pack("<Q", self.nServices) r += struct.pack("<Q", self.nServices)
r += struct.pack("<q", self.nTime) r += struct.pack("<q", self.nTime)
r += self.addrTo.serialize() r += self.addrTo.serialize(False)
r += self.addrFrom.serialize() r += self.addrFrom.serialize(False)
r += struct.pack("<Q", self.nNonce) r += struct.pack("<Q", self.nNonce)
r += ser_string(self.strSubVer) r += ser_string(self.strSubVer)
r += struct.pack("<i", self.nStartingHeight) r += struct.pack("<i", self.nStartingHeight)