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

* Convert `NetworkWorker::poll()` into async `next_action()`

* Use `NetworkWorker::next_action` instead of `poll` in `sc-network-test`

* Revert "Use `NetworkWorker::next_action` instead of `poll` in `sc-network-test`"

This reverts commit 4b5d851ec864f78f9d083a18a618fbe117c896d2.

* Fix `sc-network-test` to poll `NetworkWorker::next_action`

* Fix `sc_network::service` tests to poll `NetworkWorker::next_action`

* Fix docs

* kick CI

* Factor out `next_worker_message()` & `next_swarm_event()`

* Error handling: replace `futures::pending!()` with `expect()`

* Simplify stream polling in `select!`

* Replace `NetworkWorker::next_action()` with `run()`

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <git@kchr.de>

* minor: comment

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <git@kchr.de>

* Print debug log when network future is shut down

* Evaluate `NetworkWorker::run()` future once before the loop

* Fix client code to match new `NetworkService` interfaces

* Make clippy happy

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <git@kchr.de>

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <git@kchr.de>

* Revert "Apply suggestions from code review"

This reverts commit 9fa646d0ed613e5f8623d3d37d1d59ec0a535850.

* Make `NetworkWorker::run()` consume `self`

* Terminate system RPC future if RPC rx stream has terminated.

* Rewrite with let-else

* Fix comments

* Get `best_seen_block` and call `on_block_finalized` via `ChainSync` instead of `NetworkService`

* rustfmt

* make clippy happy

* Tests: schedule wake if `next_action()` returned true

* minor: comment

* minor: fix `NetworkWorker` rustdoc

* minor: amend the rustdoc

* Fix bug that caused `on_demand_beefy_justification_sync` test to hang

* rustfmt

* Apply review suggestions

---------

Co-authored-by: Bastian Köcher <git@kchr.de>
This commit is contained in:
Dmitry Markin
2023-02-20 15:08:02 +03:00
committed by GitHub
parent fdd5203add
commit 8d033b6dfb
12 changed files with 861 additions and 747 deletions
File diff suppressed because it is too large Load Diff
@@ -75,12 +75,8 @@ async fn normal_network_poll_no_peers() {
.with_chain_sync((chain_sync, chain_sync_service))
.build();
// poll the network once
futures::future::poll_fn(|cx| {
let _ = network.network().poll_unpin(cx);
Poll::Ready(())
})
.await;
// perform one action on network
let _ = network.network().next_action().await;
}
#[tokio::test]
@@ -110,11 +106,8 @@ async fn request_justification() {
// send "request justifiction" message and poll the network
network.service().request_justification(&hash, number);
futures::future::poll_fn(|cx| {
let _ = network.network().poll_unpin(cx);
Poll::Ready(())
})
.await;
// perform one action on network
let _ = network.network().next_action().await;
}
#[tokio::test]
@@ -141,11 +134,8 @@ async fn clear_justification_requests() {
// send "request justifiction" message and poll the network
network.service().clear_justification_requests();
futures::future::poll_fn(|cx| {
let _ = network.network().poll_unpin(cx);
Poll::Ready(())
})
.await;
// perform one action on network
let _ = network.network().next_action().await;
}
#[tokio::test]
@@ -180,11 +170,8 @@ async fn set_sync_fork_request() {
// send "set sync fork request" message and poll the network
network.service().set_sync_fork_request(copy_peers, hash, number);
futures::future::poll_fn(|cx| {
let _ = network.network().poll_unpin(cx);
Poll::Ready(())
})
.await;
// perform one action on network
let _ = network.network().next_action().await;
}
#[tokio::test]
@@ -225,11 +212,8 @@ async fn on_block_finalized() {
// send "set sync fork request" message and poll the network
network.network().on_block_finalized(hash, header);
futures::future::poll_fn(|cx| {
let _ = network.network().poll_unpin(cx);
Poll::Ready(())
})
.await;
// perform one action on network
let _ = network.network().next_action().await;
}
// report from mock import queue that importing a justification was not successful
@@ -80,10 +80,7 @@ impl TestNetwork {
let service = worker.service().clone();
let event_stream = service.event_stream("test");
tokio::spawn(async move {
futures::pin_mut!(worker);
let _ = worker.await;
});
tokio::spawn(worker.run());
(service, event_stream)
}