Skip to content

Commit 99b9e5f

Browse files
committed
p2p: always set nTime for self-advertisements
If we self-advertised to an inbound peer with the address they gave us, nTime was left default-initialized, so that our peer wouldn't relay it any further along.
1 parent 455780b commit 99b9e5f

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

src/net.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ std::optional<CAddress> GetLocalAddrForPeer(CNode *pnode)
250250
if (pnode->IsInboundConn()) {
251251
// For inbound connections, assume both the address and the port
252252
// as seen from the peer.
253-
addrLocal = CAddress{pnode->GetAddrLocal(), addrLocal.nServices};
253+
addrLocal = CAddress{pnode->GetAddrLocal(), addrLocal.nServices, addrLocal.nTime};
254254
} else {
255255
// For outbound connections, assume just the address as seen from
256256
// the peer and leave the port in `addrLocal` as returned by

src/test/net_tests.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -675,10 +675,13 @@ BOOST_AUTO_TEST_CASE(get_local_addr_for_peer_port)
675675
const uint16_t bind_port = 20001;
676676
m_node.args->ForceSetArg("-bind", strprintf("3.4.5.6:%u", bind_port));
677677

678+
const uint32_t current_time = static_cast<uint32_t>(GetAdjustedTime());
679+
SetMockTime(current_time);
680+
678681
// Our address:port as seen from the peer, completely different from the above.
679682
in_addr peer_us_addr;
680683
peer_us_addr.s_addr = htonl(0x02030405);
681-
const CAddress peer_us{CService{peer_us_addr, 20002}, NODE_NETWORK};
684+
const CAddress peer_us{CService{peer_us_addr, 20002}, NODE_NETWORK, current_time};
682685

683686
// Create a peer with a routable IPv4 address (outbound).
684687
in_addr peer_out_in_addr;
@@ -699,7 +702,7 @@ BOOST_AUTO_TEST_CASE(get_local_addr_for_peer_port)
699702
// Without the fix peer_us:8333 is chosen instead of the proper peer_us:bind_port.
700703
auto chosen_local_addr = GetLocalAddrForPeer(&peer_out);
701704
BOOST_REQUIRE(chosen_local_addr);
702-
const CService expected{peer_us_addr, bind_port};
705+
const CAddress expected{CService{peer_us_addr, bind_port}, NODE_NETWORK, current_time};
703706
BOOST_CHECK(*chosen_local_addr == expected);
704707

705708
// Create a peer with a routable IPv4 address (inbound).

0 commit comments

Comments
 (0)