Skip to content

Commit 7fabe0f

Browse files
committed
net: don't relay to the address' originator
For each address to be relayed we "randomly" pick 2 nodes to send the address to (in `RelayAddress()`). However we do not take into consideration that it does not make sense to relay the address back to its originator (`CNode::PushAddress()` will do nothing in that case). This means that if the originator is among the "randomly" picked nodes, then we will relay to one node less than intended. Fix this by skipping the originating node when choosing candidates to relay to.
1 parent bd60a9a commit 7fabe0f

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

src/net_processing.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,7 +1517,23 @@ void RelayTransaction(const uint256& txid, const uint256& wtxid, const CConnman&
15171517
});
15181518
}
15191519

1520-
static void RelayAddress(const CAddress& addr, bool fReachable, const CConnman& connman)
1520+
/**
1521+
* Relay (gossip) an address to a few randomly chosen nodes.
1522+
* We choose the same nodes within a given 24h window (if the list of connected
1523+
* nodes does not change) and we don't relay to nodes that already know an
1524+
* address. So within 24h we will likely relay a given address once. This is to
1525+
* prevent a peer from unjustly giving their address better propagation by sending
1526+
* it to us repeatedly.
1527+
* @param[in] originator The peer that sent us the address. We don't want to relay it back.
1528+
* @param[in] addr Address to relay.
1529+
* @param[in] fReachable Whether the address' network is reachable. We relay unreachable
1530+
* addresses less.
1531+
* @param[in] connman Connection manager to choose nodes to relay to.
1532+
*/
1533+
static void RelayAddress(const CNode& originator,
1534+
const CAddress& addr,
1535+
bool fReachable,
1536+
const CConnman& connman)
15211537
{
15221538
unsigned int nRelayNodes = fReachable ? 2 : 1; // limited relaying of addresses outside our network(s)
15231539

@@ -1531,8 +1547,8 @@ static void RelayAddress(const CAddress& addr, bool fReachable, const CConnman&
15311547
std::array<std::pair<uint64_t, CNode*>,2> best{{{0, nullptr}, {0, nullptr}}};
15321548
assert(nRelayNodes <= best.size());
15331549

1534-
auto sortfunc = [&best, &hasher, nRelayNodes](CNode* pnode) {
1535-
if (pnode->RelayAddrsWithConn()) {
1550+
auto sortfunc = [&best, &hasher, nRelayNodes, &originator](CNode* pnode) {
1551+
if (pnode->RelayAddrsWithConn() && pnode != &originator) {
15361552
uint64_t hashKey = CSipHasher(hasher).Write(pnode->GetId()).Finalize();
15371553
for (unsigned int i = 0; i < nRelayNodes; i++) {
15381554
if (hashKey > best[i].first) {
@@ -2637,7 +2653,7 @@ void PeerLogicValidation::ProcessMessage(CNode& pfrom, const std::string& msg_ty
26372653
if (addr.nTime > nSince && !pfrom.fGetAddr && vAddr.size() <= 10 && addr.IsRoutable())
26382654
{
26392655
// Relay to a limited number of other nodes
2640-
RelayAddress(addr, fReachable, m_connman);
2656+
RelayAddress(pfrom, addr, fReachable, m_connman);
26412657
}
26422658
// Do not store addresses outside our network
26432659
if (fReachable)

test/functional/p2p_addr_relay.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919
)
2020
import time
2121

22+
# Keep this with length <= 10. Addresses from larger messages are not relayed.
2223
ADDRS = []
23-
for i in range(10):
24+
num_ipv4_addrs = 10
25+
26+
for i in range(num_ipv4_addrs):
2427
addr = CAddress()
2528
addr.time = int(time.time()) + i
2629
addr.nServices = NODE_NETWORK | NODE_WITNESS
@@ -30,11 +33,15 @@
3033

3134

3235
class AddrReceiver(P2PInterface):
36+
num_ipv4_received = 0
37+
3338
def on_addr(self, message):
3439
for addr in message.addrs:
3540
assert_equal(addr.nServices, 9)
41+
if not 8333 <= addr.port < 8343:
42+
raise AssertionError("Invalid addr.port of {} (8333-8342 expected)".format(addr.port))
3643
assert addr.ip.startswith('123.123.123.')
37-
assert (8333 <= addr.port < 8343)
44+
self.num_ipv4_received += 1
3845

3946

4047
class AddrTest(BitcoinTestFramework):
@@ -48,21 +55,33 @@ def run_test(self):
4855
msg = msg_addr()
4956

5057
self.log.info('Send too-large addr message')
51-
msg.addrs = ADDRS * 101
58+
msg.addrs = ADDRS * 101 # more than 1000 addresses in one message
5259
with self.nodes[0].assert_debug_log(['addr message size = 1010']):
5360
addr_source.send_and_ping(msg)
5461

5562
self.log.info('Check that addr message content is relayed and added to addrman')
56-
addr_receiver = self.nodes[0].add_p2p_connection(AddrReceiver())
63+
num_receivers = 7
64+
receivers = []
65+
for _ in range(num_receivers):
66+
receivers.append(self.nodes[0].add_p2p_connection(AddrReceiver()))
5767
msg.addrs = ADDRS
58-
with self.nodes[0].assert_debug_log([
59-
'Added 10 addresses from 127.0.0.1: 0 tried',
68+
with self.nodes[0].assert_debug_log(
69+
[
70+
'Added {} addresses from 127.0.0.1: 0 tried'.format(num_ipv4_addrs),
6071
'received: addr (301 bytes) peer=0',
61-
'sending addr (301 bytes) peer=1',
62-
]):
72+
]
73+
):
6374
addr_source.send_and_ping(msg)
6475
self.nodes[0].setmocktime(int(time.time()) + 30 * 60)
65-
addr_receiver.sync_with_ping()
76+
for receiver in receivers:
77+
receiver.sync_with_ping()
78+
79+
total_ipv4_received = sum(r.num_ipv4_received for r in receivers)
80+
81+
# Every IPv4 address must be relayed to two peers, other than the
82+
# originating node (addr_source).
83+
ipv4_branching_factor = 2
84+
assert_equal(total_ipv4_received, num_ipv4_addrs * ipv4_branching_factor)
6685

6786

6887
if __name__ == '__main__':

0 commit comments

Comments
 (0)