Remove tokio dependency from serai-coordinator-p2p

Re-implements tokio::mpsc::oneshot with a thin wrapper around async-channel.

Also replaces futures-util with futures-lite.
This commit is contained in:
Luke Parker
2025-01-09 02:16:05 -05:00
parent 9833911e06
commit 201a444e89
8 changed files with 73 additions and 22 deletions

41
Cargo.lock generated
View File

@@ -840,6 +840,18 @@ dependencies = [
"futures-core", "futures-core",
] ]
[[package]]
name = "async-channel"
version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89b47800b0be77592da0afd425cc03468052844aff33b84e33cc696f64e77b6a"
dependencies = [
"concurrent-queue",
"event-listener-strategy",
"futures-core",
"pin-project-lite",
]
[[package]] [[package]]
name = "async-io" name = "async-io"
version = "2.4.0" version = "2.4.0"
@@ -2528,7 +2540,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d"
dependencies = [ dependencies = [
"libc", "libc",
"windows-sys 0.52.0", "windows-sys 0.59.0",
] ]
[[package]] [[package]]
@@ -3047,7 +3059,10 @@ version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cef40d21ae2c515b51041df9ed313ed21e572df340ea58a922a0aefe7e8891a1" checksum = "cef40d21ae2c515b51041df9ed313ed21e572df340ea58a922a0aefe7e8891a1"
dependencies = [ dependencies = [
"fastrand",
"futures-core", "futures-core",
"futures-io",
"parking",
"pin-project-lite", "pin-project-lite",
] ]
@@ -3548,7 +3563,7 @@ dependencies = [
"httpdate", "httpdate",
"itoa", "itoa",
"pin-project-lite", "pin-project-lite",
"socket2 0.4.10", "socket2 0.5.8",
"tokio", "tokio",
"tower-service", "tower-service",
"tracing", "tracing",
@@ -4112,7 +4127,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc2f4eb4bc735547cfed7c0a4922cbd04a4655978c09b54f1f7b228750664c34" checksum = "fc2f4eb4bc735547cfed7c0a4922cbd04a4655978c09b54f1f7b228750664c34"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"windows-targets 0.48.5", "windows-targets 0.52.6",
] ]
[[package]] [[package]]
@@ -6907,7 +6922,7 @@ dependencies = [
"errno", "errno",
"libc", "libc",
"linux-raw-sys", "linux-raw-sys",
"windows-sys 0.52.0", "windows-sys 0.59.0",
] ]
[[package]] [[package]]
@@ -7450,7 +7465,7 @@ version = "0.10.0-dev"
source = "git+https://github.com/serai-dex/substrate#6e3f07bf5c98a6a3ec15f2b1a46148aa8c7d737a" source = "git+https://github.com/serai-dex/substrate#6e3f07bf5c98a6a3ec15f2b1a46148aa8c7d737a"
dependencies = [ dependencies = [
"array-bytes", "array-bytes",
"async-channel", "async-channel 1.9.0",
"async-trait", "async-trait",
"asynchronous-codec", "asynchronous-codec",
"bytes", "bytes",
@@ -7491,7 +7506,7 @@ name = "sc-network-bitswap"
version = "0.10.0-dev" version = "0.10.0-dev"
source = "git+https://github.com/serai-dex/substrate#6e3f07bf5c98a6a3ec15f2b1a46148aa8c7d737a" source = "git+https://github.com/serai-dex/substrate#6e3f07bf5c98a6a3ec15f2b1a46148aa8c7d737a"
dependencies = [ dependencies = [
"async-channel", "async-channel 1.9.0",
"cid", "cid",
"futures", "futures",
"libp2p-identity", "libp2p-identity",
@@ -7548,7 +7563,7 @@ version = "0.10.0-dev"
source = "git+https://github.com/serai-dex/substrate#6e3f07bf5c98a6a3ec15f2b1a46148aa8c7d737a" source = "git+https://github.com/serai-dex/substrate#6e3f07bf5c98a6a3ec15f2b1a46148aa8c7d737a"
dependencies = [ dependencies = [
"array-bytes", "array-bytes",
"async-channel", "async-channel 1.9.0",
"futures", "futures",
"libp2p-identity", "libp2p-identity",
"log", "log",
@@ -7569,7 +7584,7 @@ version = "0.10.0-dev"
source = "git+https://github.com/serai-dex/substrate#6e3f07bf5c98a6a3ec15f2b1a46148aa8c7d737a" source = "git+https://github.com/serai-dex/substrate#6e3f07bf5c98a6a3ec15f2b1a46148aa8c7d737a"
dependencies = [ dependencies = [
"array-bytes", "array-bytes",
"async-channel", "async-channel 1.9.0",
"async-trait", "async-trait",
"fork-tree", "fork-tree",
"futures", "futures",
@@ -7943,7 +7958,7 @@ name = "sc-utils"
version = "4.0.0-dev" version = "4.0.0-dev"
source = "git+https://github.com/serai-dex/substrate#6e3f07bf5c98a6a3ec15f2b1a46148aa8c7d737a" source = "git+https://github.com/serai-dex/substrate#6e3f07bf5c98a6a3ec15f2b1a46148aa8c7d737a"
dependencies = [ dependencies = [
"async-channel", "async-channel 1.9.0",
"futures", "futures",
"futures-timer", "futures-timer",
"lazy_static", "lazy_static",
@@ -8369,14 +8384,14 @@ dependencies = [
name = "serai-coordinator-p2p" name = "serai-coordinator-p2p"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"async-channel 2.3.1",
"borsh", "borsh",
"futures-util", "futures-lite",
"log", "log",
"serai-client", "serai-client",
"serai-cosign", "serai-cosign",
"serai-db", "serai-db",
"serai-task", "serai-task",
"tokio",
"tributary-chain", "tributary-chain",
] ]
@@ -10528,7 +10543,7 @@ dependencies = [
"getrandom", "getrandom",
"once_cell", "once_cell",
"rustix", "rustix",
"windows-sys 0.52.0", "windows-sys 0.59.0",
] ]
[[package]] [[package]]
@@ -11752,7 +11767,7 @@ version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
dependencies = [ dependencies = [
"windows-sys 0.48.0", "windows-sys 0.59.0",
] ]
[[package]] [[package]]

View File

@@ -26,8 +26,8 @@ serai-client = { path = "../../substrate/client", default-features = false, feat
serai-cosign = { path = "../cosign" } serai-cosign = { path = "../cosign" }
tributary = { package = "tributary-chain", path = "../tributary" } tributary = { package = "tributary-chain", path = "../tributary" }
futures-util = { version = "0.3", default-features = false, features = ["std"] } async-channel = { version = "2", default-features = false, features = ["std"] }
tokio = { version = "1", default-features = false, features = ["sync"] } futures-lite = { version = "2", default-features = false, features = ["std"] }
log = { version = "0.4", default-features = false, features = ["std"] } log = { version = "0.4", default-features = false, features = ["std"] }
serai-task = { path = "../../common/task", version = "0.1" } serai-task = { path = "../../common/task", version = "0.1" }

View File

@@ -19,7 +19,7 @@ use serai_client::{
Serai, Serai,
}; };
use tokio::sync::{mpsc, oneshot, Mutex, RwLock}; use tokio::sync::{mpsc, Mutex, RwLock};
use serai_task::{Task, ContinuallyRan}; use serai_task::{Task, ContinuallyRan};
@@ -35,7 +35,7 @@ use libp2p::{
SwarmBuilder, SwarmBuilder,
}; };
use serai_coordinator_p2p::{Heartbeat, TributaryBlockWithCommit}; use serai_coordinator_p2p::{oneshot, Heartbeat, TributaryBlockWithCommit};
/// A struct to sync the validators from the Serai node in order to keep track of them. /// A struct to sync the validators from the Serai node in order to keep track of them.
mod validators; mod validators;

View File

@@ -8,7 +8,7 @@ use borsh::BorshDeserialize;
use serai_client::validator_sets::primitives::ValidatorSet; use serai_client::validator_sets::primitives::ValidatorSet;
use tokio::sync::{mpsc, oneshot, RwLock}; use tokio::sync::{mpsc, RwLock};
use serai_task::TaskHandle; use serai_task::TaskHandle;
@@ -21,7 +21,7 @@ use libp2p::{
swarm::{dial_opts::DialOpts, SwarmEvent, Swarm}, swarm::{dial_opts::DialOpts, SwarmEvent, Swarm},
}; };
use serai_coordinator_p2p::Heartbeat; use serai_coordinator_p2p::{oneshot, Heartbeat};
use crate::{ use crate::{
Peers, BehaviorEvent, Behavior, Peers, BehaviorEvent, Behavior,

View File

@@ -3,7 +3,7 @@ use std::time::{Duration, SystemTime};
use serai_client::validator_sets::primitives::ValidatorSet; use serai_client::validator_sets::primitives::ValidatorSet;
use futures_util::FutureExt; use futures_lite::FutureExt;
use tributary::{ReadWrite, TransactionTrait, Block, Tributary, TributaryReader}; use tributary::{ReadWrite, TransactionTrait, Block, Tributary, TributaryReader};

View File

@@ -8,10 +8,11 @@ use borsh::{BorshSerialize, BorshDeserialize};
use serai_client::{primitives::NetworkId, validator_sets::primitives::ValidatorSet}; use serai_client::{primitives::NetworkId, validator_sets::primitives::ValidatorSet};
use tokio::sync::oneshot;
use serai_cosign::SignedCosign; use serai_cosign::SignedCosign;
/// A oneshot channel.
pub mod oneshot;
/// The heartbeat task, effecting sync of Tributaries /// The heartbeat task, effecting sync of Tributaries
pub mod heartbeat; pub mod heartbeat;

View File

@@ -0,0 +1,35 @@
use core::{
pin::Pin,
task::{Poll, Context},
future::Future,
};
pub use async_channel::{SendError, RecvError};
/// The sender for a oneshot channel.
pub struct Sender<T: Send>(async_channel::Sender<T>);
impl<T: Send> Sender<T> {
/// Send a value down the channel.
///
/// Returns an error if the channel's receiver was dropped.
pub fn send(self, msg: T) -> Result<(), SendError<T>> {
self.0.send_blocking(msg)
}
}
/// The receiver for a oneshot channel.
pub struct Receiver<T: Send>(async_channel::Receiver<T>);
impl<T: Send> Future for Receiver<T> {
type Output = Result<T, RecvError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let recv = self.0.recv();
futures_lite::pin!(recv);
recv.poll(cx)
}
}
/// Create a new oneshot channel.
pub fn channel<T: Send>() -> (Sender<T>, Receiver<T>) {
let (send, recv) = async_channel::bounded(1);
(Sender(send), Receiver(recv))
}