Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some p2p issues #1306

Merged
merged 14 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions p2p/backend-test-suite/src/ban.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ where
};
messaging_handle_2
.send_message(peer, SyncMessage::HeaderList(HeaderList::new(Vec::new())))
.await
.unwrap();
messaging_handle_2
.send_message(
peer,
SyncMessage::HeaderList(HeaderList::new(vec![block.header().clone()])),
)
.await
.unwrap();
});

Expand Down
3 changes: 3 additions & 0 deletions p2p/backend-test-suite/src/block_announcement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ where
peer2.peer_id,
SyncMessage::HeaderList(HeaderList::new(vec![block.header().clone()])),
)
.await
.unwrap();

let mut sync_msg_rx_2 = match sync2.poll_next().await.unwrap() {
Expand Down Expand Up @@ -130,6 +131,7 @@ where
peer1.peer_id,
SyncMessage::HeaderList(HeaderList::new(vec![block.header().clone()])),
)
.await
.unwrap();

let mut sync_msg_rx_1 = match sync1.poll_next().await.unwrap() {
Expand Down Expand Up @@ -236,6 +238,7 @@ where
peer2.peer_id,
SyncMessage::HeaderList(HeaderList::new(vec![block.header().clone()])),
)
.await
.unwrap();

shutdown.store(true);
Expand Down
14 changes: 13 additions & 1 deletion p2p/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,15 @@ pub enum ProtocolError {
DuplicatedBlockRequest(Id<Block>),
#[error("Headers aren't connected")]
DisconnectedHeaders,
#[error("Received a message ({0}) that wasn't expected")]
#[error("Peer sent a message ({0}) that wasn't expected")]
UnexpectedMessage(String),
#[error("Peer sent a block ({0}) that wasn't requested")]
UnsolicitedBlockReceived(Id<Block>),
#[error("Peer sent block {expected_block_id} while it was expected to send {actual_block_id}")]
BlocksReceivedInWrongOrder {
expected_block_id: Id<Block>,
actual_block_id: Id<Block>,
},
#[error("Empty block list requested")]
ZeroBlocksInRequest,
#[error("Handshake expected")]
Expand Down Expand Up @@ -227,6 +234,11 @@ impl BanScore for ProtocolError {
ProtocolError::DuplicatedBlockRequest(_) => 20,
ProtocolError::DisconnectedHeaders => 20,
ProtocolError::UnexpectedMessage(_) => 20,
ProtocolError::UnsolicitedBlockReceived(_) => 20,
ProtocolError::BlocksReceivedInWrongOrder {
expected_block_id: _,
actual_block_id: _,
} => 20,
ProtocolError::ZeroBlocksInRequest => 20,
ProtocolError::HandshakeExpected => 100,
ProtocolError::AddressListLimitExceeded => 100,
Expand Down
4 changes: 3 additions & 1 deletion p2p/src/net/default_backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,10 @@ where
}
}

#[async_trait]
impl MessagingService for MessagingHandle {
fn send_message(&mut self, peer_id: PeerId, message: SyncMessage) -> crate::Result<()> {
async fn send_message(&mut self, peer_id: PeerId, message: SyncMessage) -> crate::Result<()> {
// Note: send_message was made async to be able to use a bounded channel in tests.
Ok(self.command_sender.send(types::Command::SendMessage {
peer_id,
message: message.into(),
Expand Down
3 changes: 2 additions & 1 deletion p2p/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ where
}

/// An interface for sending messages and announcements to peers.
#[async_trait]
pub trait MessagingService: Clone {
/// Sends a message to the peer.
fn send_message(&mut self, peer: PeerId, message: SyncMessage) -> crate::Result<()>;
async fn send_message(&mut self, peer: PeerId, message: SyncMessage) -> crate::Result<()>;
}

#[async_trait]
Expand Down
Loading
Loading