Skip to content

Commit 8f56fd4

Browse files
authored
Merge pull request #6406 from jcnelson/fix/6381
Fix/6381
2 parents b73f838 + 1cd4c90 commit 8f56fd4

File tree

15 files changed

+315
-121
lines changed

15 files changed

+315
-121
lines changed

stackslib/src/net/connection.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1318,7 +1318,9 @@ impl<P: ProtocolFamily> ConnectionOutbox<P> {
13181318
);
13191319

13201320
if total_sent == 0 && disconnected && !blocked {
1321-
return Err(net_error::PeerNotConnected);
1321+
return Err(net_error::PeerNotConnected(format!(
1322+
"Failed to send bytes: total_sent = {total_sent}, disconnected = {disconnected}, blocked = {blocked}"
1323+
)));
13221324
}
13231325
update_outbound_bandwidth(total_sent as i64);
13241326
Ok(total_sent)

stackslib/src/net/db.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ pub struct LocalPeer {
9696

9797
impl fmt::Display for LocalPeer {
9898
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
99-
write!(f, "local::{}", self.port)?;
99+
write!(f, "local")?;
100100
match &self.public_ip_address {
101-
None => Ok(()),
102-
Some((addr, port)) => write!(f, "::pub={}", addr.to_socketaddr(*port)),
101+
None => write!(f, "::UNKNOWN"),
102+
Some((addr, port)) => write!(f, "::{}", addr.to_socketaddr(*port)),
103103
}
104104
}
105105
}

stackslib/src/net/download/nakamoto/tenure_downloader.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,10 @@ impl NakamotoTenureDownloader {
710710
return Ok(true);
711711
}
712712
if neighbor_rpc.is_dead_or_broken(network, &self.naddr) {
713-
return Err(NetError::PeerNotConnected);
713+
return Err(NetError::PeerNotConnected(format!(
714+
"Failed to send next download request to {:?}: connection is dead or broken",
715+
&self.naddr
716+
)));
714717
}
715718

716719
let Some(peerhost) = NeighborRPC::get_peer_host(network, &self.naddr) else {
@@ -721,7 +724,10 @@ impl NakamotoTenureDownloader {
721724
DropReason::DeadConnection("No authenticated connection open".into()),
722725
DropSource::NakamotoTenureDownloader,
723726
);
724-
return Err(NetError::PeerNotConnected);
727+
return Err(NetError::PeerNotConnected(format!(
728+
"No authenticated connection open to {:?} for tenure download",
729+
&self.naddr
730+
)));
725731
};
726732

727733
let request = match self.make_next_download_request(peerhost) {

stackslib/src/net/download/nakamoto/tenure_downloader_unconfirmed.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ impl NakamotoUnconfirmedTenureDownloader {
810810
return Ok(());
811811
}
812812
if neighbor_rpc.is_dead_or_broken(network, &self.naddr) {
813-
return Err(NetError::PeerNotConnected);
813+
return Err(NetError::PeerNotConnected(format!("Failed to send next unconfirmed download request to {:?}: connection is dead or broken", &self.naddr)));
814814
}
815815

816816
let Some(peerhost) = NeighborRPC::get_peer_host(network, &self.naddr) else {
@@ -821,7 +821,10 @@ impl NakamotoUnconfirmedTenureDownloader {
821821
DropReason::DeadConnection("No authenticated connection open".into()),
822822
DropSource::NakamotoUnconfirmedTenureDownloader,
823823
);
824-
return Err(NetError::PeerNotConnected);
824+
return Err(NetError::PeerNotConnected(format!(
825+
"No authenticated connection open to {:?} for unconfirmed tenure download",
826+
&self.naddr
827+
)));
825828
};
826829

827830
let Some(request) = self.make_next_download_request(peerhost) else {

stackslib/src/net/httpcore.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,8 +1752,10 @@ impl PeerNetwork {
17521752
HttpPeer::saturate_http_socket(socket, convo)?;
17531753
Ok(event_id)
17541754
} else {
1755-
debug!("HTTP failed to connect to {:?}, {:?}", &data_url, &addr);
1756-
Err(NetError::PeerNotConnected)
1755+
debug!("HTTP failed to connect to {data_url}, {addr:?}");
1756+
Err(NetError::PeerNotConnected(format!(
1757+
"HTTP failed to connect to {data_url}, {addr:?}",
1758+
)))
17571759
}
17581760
}
17591761
Err(e) => {

stackslib/src/net/inv/epoch2x.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,7 +2346,7 @@ impl PeerNetwork {
23462346
inv_state.hint_learned_data = true;
23472347
inv_state.hint_learned_data_height = u64::MAX;
23482348
}
2349-
Err(net_error::PeerNotConnected) | Err(net_error::SendError(..)) => {
2349+
Err(net_error::PeerNotConnected(..)) | Err(net_error::SendError(..)) => {
23502350
stats.status = NodeStatus::Dead;
23512351
}
23522352
Err(e) => {
@@ -2599,7 +2599,9 @@ impl PeerNetwork {
25992599
if let Some(nstats) = inv_state.block_stats.get_mut(nk) {
26002600
Ok(func(network, nstats))
26012601
} else {
2602-
Err(net_error::PeerNotConnected)
2602+
Err(net_error::PeerNotConnected(format!(
2603+
"No inventory stats for neighbor {nk}",
2604+
)))
26032605
}
26042606
}) {
26052607
Ok(Ok(x)) => Ok(x),

stackslib/src/net/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ pub enum Error {
186186
/// server is not bound to a socket
187187
NotConnected,
188188
/// Remote peer is not connected
189-
PeerNotConnected,
189+
PeerNotConnected(String),
190190
/// Too many peers
191191
TooManyPeers,
192192
/// Peer already connected
@@ -336,7 +336,9 @@ impl fmt::Display for Error {
336336
Error::RegisterError => write!(f, "Failed to register socket with poller"),
337337
Error::SocketError => write!(f, "Socket error"),
338338
Error::NotConnected => write!(f, "Not connected to peer network"),
339-
Error::PeerNotConnected => write!(f, "Remote peer is not connected to us"),
339+
Error::PeerNotConnected(ref msg) => {
340+
write!(f, "Remote peer is not connected to us: {}", msg)
341+
}
340342
Error::TooManyPeers => write!(f, "Too many peer connections open"),
341343
Error::AlreadyConnected(ref _id, ref _nk) => write!(f, "Peer already connected"),
342344
Error::InProgress => write!(f, "Message already in progress"),
@@ -446,7 +448,7 @@ impl error::Error for Error {
446448
Error::RegisterError => None,
447449
Error::SocketError => None,
448450
Error::NotConnected => None,
449-
Error::PeerNotConnected => None,
451+
Error::PeerNotConnected(..) => None,
450452
Error::TooManyPeers => None,
451453
Error::AlreadyConnected(ref _id, ref _nk) => None,
452454
Error::InProgress => None,

stackslib/src/net/neighbors/comms.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ pub trait NeighborComms {
137137
DropReason::DeadConnection(format!("Not connected: {e}")),
138138
DropSource::NeighborCommsHandshake,
139139
);
140-
net_error::PeerNotConnected
140+
net_error::PeerNotConnected(
141+
format!("Failed to send neighbor message to {nk}: {e}",),
142+
)
141143
})
142144
}
143145

@@ -162,7 +164,9 @@ pub trait NeighborComms {
162164
if !network.is_connecting(event_id) {
163165
debug!("{:?}: Failed to connect to {:?} (event {} no longer connecting; assumed timed out)", network.get_local_peer(), event_id, &nk);
164166
self.remove_connecting_error(network, &nk);
165-
return Err(net_error::PeerNotConnected);
167+
return Err(net_error::PeerNotConnected(format!(
168+
"Failed to connect to {nk} (event {event_id} no longer connecting; assumed timed out)",
169+
)));
166170
}
167171

168172
// still connecting
@@ -177,14 +181,14 @@ pub trait NeighborComms {
177181

178182
match network.can_register_peer(&nk, true) {
179183
Ok(_) => {
180-
let event_id = network.connect_peer(&nk).map_err(|_e| {
184+
let event_id = network.connect_peer(&nk).map_err(|e| {
181185
debug!(
182186
"{:?}: Failed to connect to {:?}: {:?}",
183187
network.get_local_peer(),
184188
&nk,
185-
&_e
189+
&e
186190
);
187-
net_error::PeerNotConnected
191+
net_error::PeerNotConnected(format!("Failed to connect to {nk}: {e}"))
188192
})?;
189193

190194
// remember this in the walk result
@@ -271,7 +275,9 @@ pub trait NeighborComms {
271275
"AlreadyConnected error on event {} has no conversation",
272276
event_id
273277
);
274-
return Err(net_error::PeerNotConnected);
278+
return Err(net_error::PeerNotConnected(format!(
279+
"Already connected to {nk} as {handshake_nk} on event {event_id}",
280+
)));
275281
}
276282
Err(e) => {
277283
test_debug!(
@@ -403,7 +409,10 @@ pub trait NeighborComms {
403409
Err(e)
404410
}
405411
},
406-
None => Err(net_error::PeerNotConnected),
412+
None => Err(net_error::PeerNotConnected(format!(
413+
"Failed to receive message from {:?}: no handle",
414+
req_nk.to_neighbor_key(network)
415+
))),
407416
}
408417
}
409418

stackslib/src/net/neighbors/rpc.rs

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -126,33 +126,39 @@ impl NeighborRPC {
126126
let mut dead = vec![];
127127
let mut ret = vec![];
128128
for (naddr, (event_id, mut request_opt)) in self.state.drain() {
129-
let response = match NeighborRPC::poll_next_reply(network, event_id, &mut request_opt) {
130-
Ok(Some(response)) => response,
131-
Ok(None) => {
132-
// keep trying
133-
debug!("Still waiting for next reply from {}", &naddr);
134-
inflight.insert(naddr, (event_id, request_opt));
135-
continue;
136-
}
137-
Err(NetError::WaitingForDNS) => {
138-
// keep trying
139-
debug!(
140-
"Could not yet poll next reply from {}: waiting for DNS",
141-
&naddr
129+
let response =
130+
match NeighborRPC::poll_next_reply(network, &naddr, event_id, &mut request_opt) {
131+
Ok(Some(response)) => response,
132+
Ok(None) => {
133+
// keep trying
134+
debug!("Still waiting for next reply from {naddr}");
135+
inflight.insert(naddr, (event_id, request_opt));
136+
continue;
137+
}
138+
Err(NetError::WaitingForDNS) => {
139+
// keep trying
140+
debug!("Could not yet poll next reply from {naddr}: waiting for DNS",);
141+
inflight.insert(naddr, (event_id, request_opt));
142+
continue;
143+
}
144+
Err(NetError::InProgress) => {
145+
// keep trying
146+
debug!(
147+
"Could not yet poll next reply from {naddr}: request already in progress",
142148
);
143-
inflight.insert(naddr, (event_id, request_opt));
144-
continue;
145-
}
146-
Err(e) => {
147-
// declare this neighbor as dead by default
148-
debug!("Failed to poll next reply from {}: {:?}", &naddr, &e);
149-
dead.push((
150-
naddr,
151-
DropReason::DeadConnection(format!("Failed to poll next reply: {e}")),
152-
));
153-
continue;
154-
}
155-
};
149+
inflight.insert(naddr, (event_id, request_opt));
150+
continue;
151+
}
152+
Err(e) => {
153+
// declare this neighbor as dead by default
154+
debug!("Failed to poll next reply from {naddr}: {e}");
155+
dead.push((
156+
naddr,
157+
DropReason::DeadConnection(format!("Failed to poll next reply: {e}")),
158+
));
159+
continue;
160+
}
161+
};
156162

157163
ret.push((naddr, response));
158164
}
@@ -183,6 +189,7 @@ impl NeighborRPC {
183189
}
184190

185191
/// Send an HTTP request to the given neighbor's HTTP endpoint.
192+
/// The peer must already be connected and authenticated via the p2p network.
186193
/// Returns Ok(()) if we successfully queue the request.
187194
/// Returns Err(..) if we fail to connect to the remote peer for some reason.
188195
pub fn send_request(
@@ -194,7 +201,9 @@ impl NeighborRPC {
194201
let nk = naddr.to_neighbor_key(network);
195202
let convo = network
196203
.get_neighbor_convo(&nk)
197-
.ok_or(NetError::PeerNotConnected)?;
204+
.ok_or(NetError::PeerNotConnected(format!(
205+
"No authenticated conversation open to {nk} -- cannot perform HTTP request",
206+
)))?;
198207
let data_url = convo.data_url.clone();
199208
let data_addr = if let Some(ip) = convo.data_ip {
200209
ip.clone()
@@ -213,7 +222,9 @@ impl NeighborRPC {
213222
&convo,
214223
&data_url
215224
);
216-
return Err(NetError::PeerNotConnected);
225+
return Err(NetError::PeerNotConnected(format!(
226+
"Have not resolved {nk} data URL {data_url} yet, and not waiting for DNS",
227+
)));
217228
};
218229

219230
let event_id =
@@ -248,6 +259,7 @@ impl NeighborRPC {
248259
/// Returns Err(..) if we fail to connect, or if we are unable to receive a reply.
249260
fn poll_next_reply(
250261
network: &mut PeerNetwork,
262+
naddr: &NeighborAddress,
251263
event_id: usize,
252264
request_opt: &mut Option<StacksHttpRequest>,
253265
) -> Result<Option<StacksHttpResponse>, NetError> {
@@ -265,13 +277,17 @@ impl NeighborRPC {
265277
} else {
266278
// conversation died
267279
debug!("{:?}: HTTP event {} hung up", &network.local_peer, event_id);
268-
return Err(NetError::PeerNotConnected);
280+
return Err(NetError::PeerNotConnected(format!("HTTP connection to {naddr} (event {event_id}) hung up -- no connection established and not connecting")));
269281
}
270282
};
271283

272284
// drive socket I/O
273285
if let Some(request) = request_opt.take() {
274-
convo.send_request(request)?;
286+
let res = convo.send_request(request.clone());
287+
if let Err(NetError::InProgress) = res {
288+
// try again -- another state machine is using this conversation
289+
request_opt.replace(request);
290+
}
275291
};
276292
HttpPeer::saturate_http_socket(socket, convo)?;
277293

stackslib/src/net/neighbors/walk.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ impl<DB: NeighborWalkDB, NC: NeighborComms> NeighborWalk<DB, NC> {
711711
&neighbor_from_handshake.addr,
712712
&self.cur_neighbor.addr);
713713

714-
return Err(net_error::PeerNotConnected);
714+
return Err(net_error::PeerNotConnected(format!("Got unsolicited (or bootstrapping) HandshakeAccept from outbound {:?} (expected {:?})", &neighbor_from_handshake.addr, &self.cur_neighbor.addr)));
715715
};
716716

717717
debug!(
@@ -744,7 +744,10 @@ impl<DB: NeighborWalkDB, NC: NeighborComms> NeighborWalk<DB, NC> {
744744
if self.comms.count_inflight() == 0 {
745745
// can't proceed
746746
debug!("{:?}: No messages inflight", network.get_local_peer());
747-
return Err(net_error::PeerNotConnected);
747+
return Err(net_error::PeerNotConnected(format!(
748+
"No Handshake messages inflight to {:?}; expected at least one",
749+
&self.cur_neighbor
750+
)));
748751
}
749752

750753
let message = if let Some((_, message)) = self.comms.collect_replies(network).pop() {
@@ -761,12 +764,18 @@ impl<DB: NeighborWalkDB, NC: NeighborComms> NeighborWalk<DB, NC> {
761764
}
762765
StacksMessageType::HandshakeReject => {
763766
// told to bugger off
764-
return Err(net_error::PeerNotConnected);
767+
return Err(net_error::PeerNotConnected(format!(
768+
"Neighbor {:?} replied HandshakeReject",
769+
&self.cur_neighbor
770+
)));
765771
}
766-
StacksMessageType::Nack(_) => {
772+
StacksMessageType::Nack(ref nack) => {
767773
// something's wrong on our end (we're using a new key that they don't yet
768774
// know about, or something)
769-
return Err(net_error::PeerNotConnected);
775+
return Err(net_error::PeerNotConnected(format!(
776+
"Neighbor {:?} replied a NACK {:?}",
777+
&self.cur_neighbor, nack
778+
)));
770779
}
771780
_ => {
772781
// invalid message
@@ -843,7 +852,10 @@ impl<DB: NeighborWalkDB, NC: NeighborComms> NeighborWalk<DB, NC> {
843852
if self.comms.count_inflight() == 0 {
844853
// can't proceed
845854
debug!("{:?}: No messages inflight", network.get_local_peer());
846-
return Err(net_error::PeerNotConnected);
855+
return Err(net_error::PeerNotConnected(format!(
856+
"No GetNeighbors messages inflight to {:?}; expected at least one",
857+
&self.cur_neighbor
858+
)));
847859
}
848860

849861
let message = if let Some((_, message)) = self.comms.collect_replies(network).pop() {

0 commit comments

Comments
 (0)