Update to 2024 edition (#2001)

* Update to 2024 edition

* Update to 2024 edition; fmt, use<> and remove refs

* async functions
This commit is contained in:
James Wilson
2025-05-09 16:12:18 +01:00
committed by GitHub
parent 98c1d153b6
commit 23c62f3d5d
120 changed files with 399 additions and 322 deletions
@@ -218,8 +218,8 @@ impl<Hash> Stream for FollowStream<Hash> {
pub(super) mod test_utils {
use super::*;
use crate::config::substrate::H256;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use subxt_rpcs::methods::chain_head::{BestBlockChanged, Finalized, Initialized, NewBlock};
/// Given some events, returns a follow stream getter that we can use in
@@ -313,7 +313,7 @@ pub mod test {
});
let s = FollowStream::new(stream_getter);
let out: Vec<_> = s.filter_map(|e| async move { e.ok() }).collect().await;
let out: Vec<_> = s.filter_map(async |e| e.ok()).collect().await;
// The expected response, given the above.
assert_eq!(
@@ -2,8 +2,8 @@
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
use super::follow_stream::FollowStream;
use super::ChainHeadRpcMethods;
use super::follow_stream::FollowStream;
use crate::config::{Config, Hash, HashFor};
use crate::error::Error;
use futures::stream::{FuturesUnordered, Stream, StreamExt};
@@ -468,7 +468,7 @@ impl<H: Hash> Drop for BlockRef<H> {
#[cfg(test)]
pub(super) mod test_utils {
use super::super::follow_stream::{test_utils::test_stream_getter, FollowStream};
use super::super::follow_stream::{FollowStream, test_utils::test_stream_getter};
use super::*;
use crate::config::substrate::H256;
@@ -573,10 +573,7 @@ mod test {
10,
);
let out: Vec<_> = follow_unpin
.filter_map(|e| async move { e.ok() })
.collect()
.await;
let out: Vec<_> = follow_unpin.filter_map(async |e| e.ok()).collect().await;
assert_eq!(
out,
+14 -8
View File
@@ -18,8 +18,8 @@ mod storage_items;
use self::follow_stream_driver::FollowStreamFinalizedHeads;
use crate::backend::{
utils::retry, Backend, BlockRef, BlockRefT, RuntimeVersion, StorageResponse, StreamOf,
StreamOfResults, TransactionStatus,
Backend, BlockRef, BlockRefT, RuntimeVersion, StorageResponse, StreamOf, StreamOfResults,
TransactionStatus, utils::retry,
};
use crate::config::{Config, Hash, HashFor};
use crate::error::{Error, RpcError};
@@ -30,10 +30,10 @@ use futures::{Stream, StreamExt};
use std::collections::HashMap;
use std::task::Poll;
use storage_items::StorageItems;
use subxt_rpcs::RpcClient;
use subxt_rpcs::methods::chain_head::{
FollowEvent, MethodResponse, RuntimeEvent, StorageQuery, StorageQueryType, StorageResultType,
};
use subxt_rpcs::RpcClient;
/// Re-export RPC types and methods from [`subxt_rpcs::methods::chain_head`].
pub mod rpc_methods {
@@ -301,7 +301,7 @@ impl<T: Config + Send + Sync + 'static> Backend<T> for ChainHeadBackend<T> {
StorageItems::from_methods(queries, at, &self.follow_handle, self.methods.clone())
.await?;
let stream = storage_items.filter_map(|val| async move {
let stream = storage_items.filter_map(async |val| {
let val = match val {
Ok(val) => val,
Err(e) => return Some(Err(e)),
@@ -366,7 +366,7 @@ impl<T: Config + Send + Sync + 'static> Backend<T> for ChainHeadBackend<T> {
)
.await?;
let storage_result_stream = storage_items.filter_map(|val| async move {
let storage_result_stream = storage_items.filter_map(async |val| {
let val = match val {
Ok(val) => val,
Err(e) => return Some(Err(e)),
@@ -686,7 +686,7 @@ impl<T: Config + Send + Sync + 'static> Backend<T> for ChainHeadBackend<T> {
Poll::Ready(None) => {
return Poll::Ready(err_other(
"chainHead_follow stream ended unexpectedly",
))
));
}
Poll::Ready(Some(follow_ev)) => Poll::Ready(follow_ev),
Poll::Pending => Poll::Pending,
@@ -722,7 +722,9 @@ impl<T: Config + Send + Sync + 'static> Backend<T> for ChainHeadBackend<T> {
// in which we may lose the finalized block that the TX is in. For now, just error if
// this happens, to prevent the case in which we never see a finalized block and wait
// forever.
return Poll::Ready(err_other("chainHead_follow emitted 'stop' event during transaction submission"));
return Poll::Ready(err_other(
"chainHead_follow emitted 'stop' event during transaction submission",
));
}
_ => {}
}
@@ -756,7 +758,11 @@ impl<T: Config + Send + Sync + 'static> Backend<T> for ChainHeadBackend<T> {
// If we don't have a finalized block yet, we keep polling for tx progress events.
let tx_progress_ev = match tx_progress.poll_next_unpin(cx) {
Poll::Pending => return Poll::Pending,
Poll::Ready(None) => return Poll::Ready(err_other("No more transaction progress events, but we haven't seen a Finalized one yet")),
Poll::Ready(None) => {
return Poll::Ready(err_other(
"No more transaction progress events, but we haven't seen a Finalized one yet",
));
}
Poll::Ready(Some(Err(e))) => return Poll::Ready(Some(Err(e.into()))),
Poll::Ready(Some(Ok(ev))) => ev,
};
+3 -3
View File
@@ -12,12 +12,12 @@ use crate::backend::{
TransactionStatus,
};
use crate::{
config::{Config, HashFor, Header},
Error,
config::{Config, HashFor, Header},
};
use async_trait::async_trait;
use futures::TryStreamExt;
use futures::{future, future::Either, stream, Future, FutureExt, Stream, StreamExt};
use futures::{Future, FutureExt, Stream, StreamExt, future, future::Either, stream};
use std::collections::VecDeque;
use std::pin::Pin;
use std::task::{Context, Poll};
@@ -473,7 +473,7 @@ where
Ok::<_, Error>(header)
}
})
.filter_map(|h| async { h.transpose() });
.filter_map(async |h| h.transpose());
// On the next iteration, we'll get details starting just after this end block.
last_block_num = Some(end_block_num);
+22 -20
View File
@@ -388,10 +388,10 @@ mod test {
use primitive_types::H256;
use rpc::RpcClientT;
use std::collections::{HashMap, VecDeque};
use subxt_core::{config::DefaultExtrinsicParams, Config};
use subxt_core::{Config, config::DefaultExtrinsicParams};
use subxt_rpcs::client::{
mock_rpc_client::{Json, MockRpcClientBuilder},
MockRpcClient,
mock_rpc_client::{Json, MockRpcClientBuilder},
};
fn random_hash() -> H256 {
@@ -427,7 +427,7 @@ mod test {
mod legacy {
use super::*;
use crate::{
backend::legacy::{rpc_methods::RuntimeVersion, LegacyBackend},
backend::legacy::{LegacyBackend, rpc_methods::RuntimeVersion},
error::RpcError,
};
@@ -511,11 +511,11 @@ mod test {
#[tokio::test]
async fn storage_fetch_value() {
let rpc_client = MockRpcClient::builder()
.method_handler_once("state_getStorage", move |_params| async move {
.method_handler_once("state_getStorage", async move |_params| {
// Return "disconnected" error on first call
Err::<Infallible, _>(disconnected_will_reconnect())
})
.method_handler_once("state_getStorage", move |_param| async move {
.method_handler_once("state_getStorage", async move |_param| {
// Return some hex encoded storage value on the next one
Json(hex::encode("Data1"))
})
@@ -550,11 +550,11 @@ mod test {
async fn simple_fetch() {
let hash = random_hash();
let rpc_client = MockRpcClient::builder()
.method_handler_once("chain_getBlockHash", move |_params| async move {
.method_handler_once("chain_getBlockHash", async move |_params| {
// Return "disconnected" error on first call
Err::<Infallible, _>(disconnected_will_reconnect())
})
.method_handler_once("chain_getBlockHash", move |_params| async move {
.method_handler_once("chain_getBlockHash", async move |_params| {
// Return the blockhash on next call
Json(hash)
})
@@ -854,11 +854,13 @@ mod test {
.await
.unwrap();
assert!(response
.next()
.await
.unwrap()
.is_err_and(|e| matches!(e, Error::Other(e) if e == "error")));
assert!(
response
.next()
.await
.unwrap()
.is_err_and(|e| matches!(e, Error::Other(e) if e == "error"))
);
assert!(response.next().await.is_none());
}
@@ -868,11 +870,11 @@ mod test {
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
let rpc_client = mock_client_builder(rx)
.method_handler_once("chainHead_v1_storage", move |_params| async move {
.method_handler_once("chainHead_v1_storage", async move |_params| {
// First call; return DisconnectedWillReconnect
Err::<Infallible, _>(disconnected_will_reconnect())
})
.method_handler_once("chainHead_v1_storage", move |_params| async move {
.method_handler_once("chainHead_v1_storage", async move |_params| {
// Otherwise, return that we'll start sending a response, and spawn
// task to send the relevant response via chainHead_follow.
tokio::spawn(async move {
@@ -925,11 +927,11 @@ mod test {
let tx2 = tx.clone();
let rpc_client = mock_client_builder(rx)
.method_handler_once("chainHead_v1_storage", move |_params| async move {
.method_handler_once("chainHead_v1_storage", async move |_params| {
// First call; return DisconnectedWillReconnect
Err::<Infallible, _>(disconnected_will_reconnect())
})
.method_handler_once("chainHead_v1_storage", move |_params| async move {
.method_handler_once("chainHead_v1_storage", async move |_params| {
// Next call, return a storage item and then a "waiting for continue".
tokio::spawn(async move {
tx.send(storage_items("Id1", &[storage_result("ID1", "Data1")]))
@@ -938,11 +940,11 @@ mod test {
});
Ok(Json(response_started("Id1")))
})
.method_handler_once("chainHead_v1_continue", move |_params| async move {
.method_handler_once("chainHead_v1_continue", async move |_params| {
// First call; return DisconnectedWillReconnect
Err::<Infallible, _>(disconnected_will_reconnect())
})
.method_handler_once("chainHead_v1_continue", move |_params| async move {
.method_handler_once("chainHead_v1_continue", async move |_params| {
// Next call; acknowledge the "continue" and return reamining storage items.
tokio::spawn(async move {
tx2.send(storage_items("Id1", &[storage_result("ID2", "Data2")]))
@@ -986,11 +988,11 @@ mod test {
let hash = random_hash();
let (_tx, rx) = tokio::sync::mpsc::unbounded_channel();
let rpc_client = mock_client_builder(rx)
.method_handler_once("chainSpec_v1_genesisHash", move |_params| async move {
.method_handler_once("chainSpec_v1_genesisHash", async move |_params| {
// First call, return disconnected error.
Err::<Infallible, _>(disconnected_will_reconnect())
})
.method_handler_once("chainSpec_v1_genesisHash", move |_params| async move {
.method_handler_once("chainSpec_v1_genesisHash", async move |_params| {
// Next call, return the hash.
Ok(Json(hash))
})
+2 -2
View File
@@ -246,7 +246,7 @@ mod tests {
#[tokio::test]
async fn retry_sub_err_terminates_stream() {
let stream = futures::stream::iter([Ok(1)]);
let resubscribe = Box::new(move || async move { Err(custom_err()) }.boxed());
let resubscribe = Box::new(|| async move { Err(custom_err()) }.boxed());
let retry_stream = RetrySubscription {
state: Some(PendingOrStream::Stream(StreamOf::new(Box::pin(stream)))),
@@ -259,7 +259,7 @@ mod tests {
#[tokio::test]
async fn retry_sub_resubscribe_err() {
let stream = futures::stream::iter([Ok(1), Err(disconnect_err())]);
let resubscribe = Box::new(move || async move { Err(custom_err()) }.boxed());
let resubscribe = Box::new(|| async move { Err(custom_err()) }.boxed());
let retry_stream = RetrySubscription {
state: Some(PendingOrStream::Stream(StreamOf::new(Box::pin(stream)))),