Skip to content

Commit

Permalink
Only send transactions to peers with open substream (#2717)
Browse files Browse the repository at this point in the history
Fix #2680 

The code in `network_service` was wrong and was attempting to send a
transaction to every peer we have a connection with, instead of every
peer we have opened a transactions notifications substream with.
  • Loading branch information
tomaka authored Sep 7, 2022
1 parent 5e3e724 commit 9ce1d5f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
7 changes: 6 additions & 1 deletion bin/light-base/src/network_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,12 @@ impl<TPlat: Platform> NetworkService<TPlat> {
let mut guarded = self.shared.guarded.lock().await;

// TODO: collecting in a Vec :-/
for peer in guarded.network.peers_list().cloned().collect::<Vec<_>>() {
for peer in guarded
.network
.opened_transactions_substream(chain_index)
.cloned()
.collect::<Vec<_>>()
{
if guarded
.network
.announce_transaction(&peer, chain_index, transaction)
Expand Down
1 change: 1 addition & 0 deletions bin/wasm-node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Fix occasional panic when connecting to a parachain with forks and/or missed slots. ([#2703](https://github.com/paritytech/smoldot/pull/2703))
- Fix parachain initialization unnecessarily waiting for its corresponding relay chain initialization to be finished. ([#2705](https://github.com/paritytech/smoldot/pull/2705))
- Fix panic when broadcasting a transaction to a peer while its connection is shutting down. ([#2717](https://github.com/paritytech/smoldot/pull/2717))
- Fix crash when receiving a Yamux GoAway frame. ([#2708](https://github.com/paritytech/smoldot/pull/2708))

## 0.6.31 - 2022-08-30
Expand Down
14 changes: 14 additions & 0 deletions src/libp2p/peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,20 @@ where
)
}

/// Returns the list of peers for which we have a fully established notifications protocol of
/// the given protocol.
pub fn opened_out_notifications(
&'_ self,
notifications_protocol_index: usize,
) -> impl Iterator<Item = &'_ PeerId> + '_ {
// TODO: this is O(n)
self.peers_notifications_out
.iter()
.filter(move |((_, idx), _)| *idx == notifications_protocol_index)
.filter(|(_, state)| matches!(state.open, NotificationsOutOpenState::Open(_)))
.map(|((peer_idx, _), _)| &self.peers[*peer_idx].peer_id)
}

/// Returns the state of the given connection.
///
/// # Panic
Expand Down
10 changes: 10 additions & 0 deletions src/network/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,16 @@ where
)
}

/// Returns the list of peers for which we have a fully established notifications protocol of
/// the given protocol.
pub fn opened_transactions_substream(
&'_ self,
chain_index: usize,
) -> impl Iterator<Item = &'_ PeerId> + '_ {
self.inner
.opened_out_notifications(chain_index * NOTIFICATIONS_PROTOCOLS_PER_CHAIN + 1)
}

///
///
/// Must be passed the SCALE-encoded transaction.
Expand Down

0 comments on commit 9ce1d5f

Please sign in to comment.