Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Use async/await instead of manual polling of NetworkWorker #13219

Merged
merged 43 commits into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
fb272a6
Convert `NetworkWorker::poll()` into async `next_action()`
dmitry-markin Jan 16, 2023
4b5d851
Use `NetworkWorker::next_action` instead of `poll` in `sc-network-test`
dmitry-markin Jan 18, 2023
1c87141
Revert "Use `NetworkWorker::next_action` instead of `poll` in `sc-net…
dmitry-markin Jan 23, 2023
dbd8ad9
Fix `sc-network-test` to poll `NetworkWorker::next_action`
dmitry-markin Jan 23, 2023
e8d0758
Fix `sc_network::service` tests to poll `NetworkWorker::next_action`
dmitry-markin Jan 23, 2023
3071b05
Fix docs
dmitry-markin Jan 23, 2023
431e550
Merge remote-tracking branch 'origin/master' into dm-network-service-…
dmitry-markin Jan 23, 2023
d7be660
kick CI
dmitry-markin Jan 23, 2023
78cce1a
Factor out `next_worker_message()` & `next_swarm_event()`
dmitry-markin Feb 3, 2023
c9f75f7
Error handling: replace `futures::pending!()` with `expect()`
dmitry-markin Feb 6, 2023
4a85d1f
Simplify stream polling in `select!`
dmitry-markin Feb 6, 2023
c165185
Replace `NetworkWorker::next_action()` with `run()`
dmitry-markin Feb 6, 2023
d827ae9
Merge remote-tracking branch 'origin/master' into dm-network-service-…
dmitry-markin Feb 6, 2023
019ec35
Apply suggestions from code review
dmitry-markin Feb 6, 2023
d38b2b7
minor: comment
dmitry-markin Feb 6, 2023
343e9e9
Merge remote-tracking branch 'origin/master' into dm-network-service-…
dmitry-markin Feb 6, 2023
dd4980d
Apply suggestions from code review
dmitry-markin Feb 7, 2023
403dded
Print debug log when network future is shut down
dmitry-markin Feb 7, 2023
5a7ec32
Merge remote-tracking branch 'origin/master' into dm-network-service-…
dmitry-markin Feb 7, 2023
73dfd4b
Merge remote-tracking branch 'origin/master' into dm-network-service-…
dmitry-markin Feb 8, 2023
5a63842
Evaluate `NetworkWorker::run()` future once before the loop
dmitry-markin Feb 10, 2023
913e21c
Merge remote-tracking branch 'origin/master' into dm-network-service-…
dmitry-markin Feb 10, 2023
8e1db2c
Fix client code to match new `NetworkService` interfaces
dmitry-markin Feb 11, 2023
faec2e9
Make clippy happy
dmitry-markin Feb 11, 2023
9fa646d
Apply suggestions from code review
dmitry-markin Feb 13, 2023
9c5b610
Apply suggestions from code review
dmitry-markin Feb 13, 2023
f324937
Revert "Apply suggestions from code review"
dmitry-markin Feb 13, 2023
9856cc4
Make `NetworkWorker::run()` consume `self`
dmitry-markin Feb 13, 2023
f86ec12
Terminate system RPC future if RPC rx stream has terminated.
dmitry-markin Feb 13, 2023
9557a42
Rewrite with let-else
dmitry-markin Feb 13, 2023
533cc04
Fix comments
dmitry-markin Feb 13, 2023
e669c6a
Get `best_seen_block` and call `on_block_finalized` via `ChainSync` i…
dmitry-markin Feb 13, 2023
6ed5d48
rustfmt
dmitry-markin Feb 13, 2023
ab1ced4
make clippy happy
dmitry-markin Feb 13, 2023
4be62c7
Tests: schedule wake if `next_action()` returned true
dmitry-markin Feb 14, 2023
5e90af0
minor: comment
dmitry-markin Feb 14, 2023
a9123a7
Merge remote-tracking branch 'origin/master' into dm-network-service-…
dmitry-markin Feb 14, 2023
113a888
minor: fix `NetworkWorker` rustdoc
dmitry-markin Feb 14, 2023
ac1452c
minor: amend the rustdoc
dmitry-markin Feb 14, 2023
5fc3831
Fix bug that caused `on_demand_beefy_justification_sync` test to hang
dmitry-markin Feb 15, 2023
6005109
Merge remote-tracking branch 'origin/master' into dm-network-service-…
dmitry-markin Feb 15, 2023
9df7d0e
rustfmt
dmitry-markin Feb 15, 2023
812c316
Apply review suggestions
dmitry-markin Feb 17, 2023
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
4 changes: 4 additions & 0 deletions client/authority-discovery/src/worker/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ impl NetworkStateInfo for TestNetwork {
fn external_addresses(&self) -> Vec<Multiaddr> {
self.external_addresses.clone()
}

fn listen_addresses(&self) -> Vec<Multiaddr> {
self.external_addresses.clone()
}
}

struct TestSigner<'a> {
Expand Down
4 changes: 2 additions & 2 deletions client/finality-grandpa/src/communication/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ const TELEMETRY_VOTERS_LIMIT: usize = 10;
/// well as the ability to set a fork sync request for a particular block.
pub trait Network<Block: BlockT>:
NetworkSyncForkRequest<Block::Hash, NumberFor<Block>>
+ NetworkBlock<Block::Hash, NumberFor<Block>>
+ NetworkBlock<Block::Hash, NumberFor<Block>, Block::Header>
+ GossipNetwork<Block>
+ Clone
+ Send
Expand All @@ -179,7 +179,7 @@ impl<Block, T> Network<Block> for T
where
Block: BlockT,
T: NetworkSyncForkRequest<Block::Hash, NumberFor<Block>>
+ NetworkBlock<Block::Hash, NumberFor<Block>>
+ NetworkBlock<Block::Hash, NumberFor<Block>, Block::Header>
+ GossipNetwork<Block>
+ Clone
+ Send
Expand Down
8 changes: 6 additions & 2 deletions client/finality-grandpa/src/communication/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use sc_network_common::{
},
};
use sc_network_gossip::Validator;
use sc_network_test::{Block, Hash};
use sc_network_test::{Block, Hash, Header};
use sc_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender};
use sp_finality_grandpa::AuthorityList;
use sp_keyring::Ed25519Keyring;
Expand Down Expand Up @@ -155,14 +155,18 @@ impl NetworkNotification for TestNetwork {
}
}

impl NetworkBlock<Hash, NumberFor<Block>> for TestNetwork {
impl NetworkBlock<Hash, NumberFor<Block>, Header> for TestNetwork {
fn announce_block(&self, hash: Hash, _data: Option<Vec<u8>>) {
let _ = self.sender.unbounded_send(Event::Announce(hash));
}

fn new_best_block_imported(&self, _hash: Hash, _number: NumberFor<Block>) {
unimplemented!();
}

fn on_block_finalized(&self, _hash: Hash, _header: Header) {
unimplemented!();
}
}

impl NetworkSyncForkRequest<Hash, NumberFor<Block>> for TestNetwork {
Expand Down
12 changes: 11 additions & 1 deletion client/network-gossip/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,9 @@ mod tests {
}
}

impl NetworkBlock<<Block as BlockT>::Hash, NumberFor<Block>> for TestNetwork {
impl NetworkBlock<<Block as BlockT>::Hash, NumberFor<Block>, <Block as BlockT>::Header>
for TestNetwork
{
fn announce_block(&self, _hash: <Block as BlockT>::Hash, _data: Option<Vec<u8>>) {
unimplemented!();
}
Expand All @@ -447,6 +449,14 @@ mod tests {
) {
unimplemented!();
}

fn on_block_finalized(
&self,
_hash: <Block as BlockT>::Hash,
_header: <Block as BlockT>::Header,
) {
unimplemented!();
}
}

struct AllowAll;
Expand Down
7 changes: 5 additions & 2 deletions client/network-gossip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ mod validator;

/// Abstraction over a network.
pub trait Network<B: BlockT>:
NetworkPeers + NetworkEventStream + NetworkNotification + NetworkBlock<B::Hash, NumberFor<B>>
NetworkPeers
+ NetworkEventStream
+ NetworkNotification
+ NetworkBlock<B::Hash, NumberFor<B>, B::Header>
{
fn add_set_reserved(&self, who: PeerId, protocol: ProtocolName) {
let addr =
Expand All @@ -97,6 +100,6 @@ impl<T, B: BlockT> Network<B> for T where
T: NetworkPeers
+ NetworkEventStream
+ NetworkNotification
+ NetworkBlock<B::Hash, NumberFor<B>>
+ NetworkBlock<B::Hash, NumberFor<B>, B::Header>
{
}
12 changes: 11 additions & 1 deletion client/network-gossip/src/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,9 @@ mod tests {
}
}

impl NetworkBlock<<Block as BlockT>::Hash, NumberFor<Block>> for NoOpNetwork {
impl NetworkBlock<<Block as BlockT>::Hash, NumberFor<Block>, <Block as BlockT>::Header>
for NoOpNetwork
{
fn announce_block(&self, _hash: <Block as BlockT>::Hash, _data: Option<Vec<u8>>) {
unimplemented!();
}
Expand All @@ -689,6 +691,14 @@ mod tests {
) {
unimplemented!();
}

fn on_block_finalized(
&self,
_hash: <Block as BlockT>::Hash,
_header: <Block as BlockT>::Header,
) {
unimplemented!();
}
}

#[test]
Expand Down
25 changes: 20 additions & 5 deletions client/network/common/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,13 @@ pub trait NetworkPeers {
/// purposes.
fn deny_unreserved_peers(&self);

/// Adds a `PeerId` and its `Multiaddr` as reserved.
/// Adds a `PeerId` and its `Multiaddr` as reserved for a sync protocol (default peer set).
///
/// Returns an `Err` if the given string is not a valid multiaddress
/// or contains an invalid peer ID (which includes the local peer ID).
fn add_reserved_peer(&self, peer: MultiaddrWithPeerId) -> Result<(), String>;

/// Removes a `PeerId` from the list of reserved peers.
/// Removes a `PeerId` from the list of reserved peers for a sync protocol (default peer set).
fn remove_reserved_peer(&self, peer_id: PeerId);

/// Sets the reserved set of a protocol to the given set of peers.
Expand Down Expand Up @@ -359,6 +359,9 @@ pub trait NetworkStateInfo {
/// Returns the local external addresses.
fn external_addresses(&self) -> Vec<Multiaddr>;

/// Returns the listening addresses (without trailing `/p2p/` with our `PeerId`).
fn listen_addresses(&self) -> Vec<Multiaddr>;

/// Returns the local Peer ID.
fn local_peer_id(&self) -> PeerId;
}
Expand All @@ -372,6 +375,10 @@ where
T::external_addresses(self)
}

fn listen_addresses(&self) -> Vec<Multiaddr> {
T::listen_addresses(self)
}

fn local_peer_id(&self) -> PeerId {
T::local_peer_id(self)
}
Expand Down Expand Up @@ -605,7 +612,7 @@ where
}

/// Provides ability to announce blocks to the network.
pub trait NetworkBlock<BlockHash, BlockNumber> {
pub trait NetworkBlock<BlockHash, BlockNumber, BlockHeader> {
/// Make sure an important block is propagated to peers.
///
/// In chain-based consensus, we often need to make sure non-best forks are
Expand All @@ -614,12 +621,16 @@ pub trait NetworkBlock<BlockHash, BlockNumber> {

/// Inform the network service about new best imported block.
fn new_best_block_imported(&self, hash: BlockHash, number: BlockNumber);

/// Notify the network service (and underlying `NetworkSync`) about a finalized block.
fn on_block_finalized(&self, hash: BlockHash, header: BlockHeader);
}

impl<T, BlockHash, BlockNumber> NetworkBlock<BlockHash, BlockNumber> for Arc<T>
impl<T, BlockHash, BlockNumber, BlockHeader> NetworkBlock<BlockHash, BlockNumber, BlockHeader>
for Arc<T>
where
T: ?Sized,
T: NetworkBlock<BlockHash, BlockNumber>,
T: NetworkBlock<BlockHash, BlockNumber, BlockHeader>,
{
fn announce_block(&self, hash: BlockHash, data: Option<Vec<u8>>) {
T::announce_block(self, hash, data)
Expand All @@ -628,4 +639,8 @@ where
fn new_best_block_imported(&self, hash: BlockHash, number: BlockNumber) {
T::new_best_block_imported(self, hash, number)
}

fn on_block_finalized(&self, hash: BlockHash, header: BlockHeader) {
T::on_block_finalized(self, hash, header)
}
}
Loading