diff --git a/.github/workflows/common-tests.yml b/.github/workflows/common-tests.yml index f0545f0b..117b5858 100644 --- a/.github/workflows/common-tests.yml +++ b/.github/workflows/common-tests.yml @@ -27,6 +27,7 @@ jobs: GITHUB_CI=true RUST_BACKTRACE=1 cargo test --all-features \ -p std-shims \ -p zalloc \ + -p patchable-async-sleep \ -p serai-db \ -p serai-env \ -p simple-request diff --git a/Cargo.lock b/Cargo.lock index 96d01250..3251b7df 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5720,6 +5720,13 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" +[[package]] +name = "patchable-async-sleep" +version = "0.1.0" +dependencies = [ + "tokio", +] + [[package]] name = "pbkdf2" version = "0.11.0" @@ -9843,6 +9850,7 @@ dependencies = [ "hex", "log", "parity-scale-codec", + "patchable-async-sleep", "serai-db", "thiserror", "tokio", diff --git a/Cargo.toml b/Cargo.toml index ebd877f5..82357cf4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ members = [ "common/std-shims", "common/zalloc", + "common/patchable-async-sleep", "common/db", "common/env", "common/request", diff --git a/common/patchable-async-sleep/Cargo.toml b/common/patchable-async-sleep/Cargo.toml new file mode 100644 index 00000000..e2d1e9cf --- /dev/null +++ b/common/patchable-async-sleep/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "patchable-async-sleep" +version = "0.1.0" +description = "An async sleep function, patchable to the preferred runtime" +license = "MIT" +repository = "https://github.com/serai-dex/serai/tree/develop/common/patchable-async-sleep" +authors = ["Luke Parker "] +keywords = ["async", "sleep", "tokio", "smol", "async-std"] +edition = "2021" + +[package.metadata.docs.rs] +all-features = true +rustdoc-args = ["--cfg", "docsrs"] + +[lints] +workspace = true + +[dependencies] +tokio = { version = "1", default-features = false, features = [ "time"] } diff --git a/common/patchable-async-sleep/LICENSE b/common/patchable-async-sleep/LICENSE new file mode 100644 index 00000000..659881f1 --- /dev/null +++ b/common/patchable-async-sleep/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Luke Parker + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/common/patchable-async-sleep/README.md b/common/patchable-async-sleep/README.md new file mode 100644 index 00000000..ec5e4f9b --- /dev/null +++ b/common/patchable-async-sleep/README.md @@ -0,0 +1,7 @@ +# Patchable Async Sleep + +An async sleep function, patchable to the preferred runtime. + +This crate is `tokio`-backed. Applications which don't want to use `tokio` +should patch this crate to one which works witht heir preferred runtime. The +point of it is to have a minimal API surface to trivially facilitate such work. diff --git a/common/patchable-async-sleep/src/lib.rs b/common/patchable-async-sleep/src/lib.rs new file mode 100644 index 00000000..780fd0df --- /dev/null +++ b/common/patchable-async-sleep/src/lib.rs @@ -0,0 +1,10 @@ +#![cfg_attr(docsrs, feature(doc_auto_cfg))] +#![doc = include_str!("../README.md")] +#![deny(missing_docs)] + +use core::time::Duration; + +/// Sleep for the specified duration. +pub fn sleep(duration: Duration) -> impl core::future::Future { + tokio::time::sleep(duration) +} diff --git a/coordinator/tributary/tendermint/Cargo.toml b/coordinator/tributary/tendermint/Cargo.toml index 5a290590..ac6becfa 100644 --- a/coordinator/tributary/tendermint/Cargo.toml +++ b/coordinator/tributary/tendermint/Cargo.toml @@ -25,7 +25,7 @@ parity-scale-codec = { version = "3", default-features = false, features = ["std futures-util = { version = "0.3", default-features = false, features = ["std", "async-await-macro", "sink", "channel"] } futures-channel = { version = "0.3", default-features = false, features = ["std", "sink"] } -tokio = { version = "1", default-features = false, features = ["time"] } +patchable-async-sleep = { version = "0.1", path = "../../../common/patchable-async-sleep", default-features = false } serai-db = { path = "../../../common/db", version = "0.1", default-features = false } diff --git a/coordinator/tributary/tendermint/src/lib.rs b/coordinator/tributary/tendermint/src/lib.rs index d9ecd253..10f1fccf 100644 --- a/coordinator/tributary/tendermint/src/lib.rs +++ b/coordinator/tributary/tendermint/src/lib.rs @@ -13,7 +13,7 @@ use futures_util::{ FutureExt, StreamExt, SinkExt, future::{self, Fuse}, }; -use tokio::time::sleep; +use patchable_async_sleep::sleep; use serai_db::{Get, DbTxn, Db}; diff --git a/coordinator/tributary/tendermint/src/round.rs b/coordinator/tributary/tendermint/src/round.rs index a97e3ed1..42aa3212 100644 --- a/coordinator/tributary/tendermint/src/round.rs +++ b/coordinator/tributary/tendermint/src/round.rs @@ -5,7 +5,7 @@ use std::{ }; use futures_util::{FutureExt, future}; -use tokio::time::sleep; +use patchable_async_sleep::sleep; use crate::{ time::CanonicalInstant,