Switch the client to new futures (#3103)

* Switch the client to new futures

* No need for compat in the client

* Fix client tests

* Address review
This commit is contained in:
Pierre Krieger
2019-07-11 16:58:30 +02:00
committed by Bastian Köcher
parent f5e921281e
commit bf2551a854
28 changed files with 249 additions and 112 deletions
+1 -1
View File
@@ -21,7 +21,7 @@ use std::{
panic::UnwindSafe, result, cell::RefCell, rc::Rc,
};
use crate::error::Error;
use futures::sync::mpsc;
use futures::channel::mpsc;
use parking_lot::{Mutex, RwLock};
use primitives::NativeOrEncoded;
use runtime_primitives::{
+1 -1
View File
@@ -58,7 +58,7 @@ pub use crate::client::{
new_with_backend,
new_in_mem,
BlockBody, BlockStatus, ImportNotifications, FinalityNotifications, BlockchainEvents,
BlockImportNotification, Client, ClientInfo, ExecutionStrategies,
BlockImportNotification, Client, ClientInfo, ExecutionStrategies, FinalityNotification,
LongestChain,
};
#[cfg(feature = "std")]
+9 -9
View File
@@ -19,7 +19,6 @@
use std::collections::HashMap;
use std::sync::{Arc, Weak};
use futures::{Future, IntoFuture};
use parking_lot::{RwLock, Mutex};
use runtime_primitives::{generic::BlockId, Justification, StorageOverlay, ChildrenStorageOverlay};
@@ -359,14 +358,15 @@ where
*self.cached_header.write() = Some(cached_header);
}
self.fetcher.upgrade().ok_or(ClientError::NotAvailableOnLightClient)?
.remote_read(RemoteReadRequest {
block: self.block,
header: header.expect("if block above guarantees that header is_some(); qed"),
key: key.to_vec(),
retry_count: None,
})
.into_future().wait()
futures::executor::block_on(
self.fetcher.upgrade().ok_or(ClientError::NotAvailableOnLightClient)?
.remote_read(RemoteReadRequest {
block: self.block,
header: header.expect("if block above guarantees that header is_some(); qed"),
key: key.to_vec(),
retry_count: None,
})
)
}
fn child_storage(&self, _storage_key: &[u8], _key: &[u8]) -> ClientResult<Option<Vec<u8>>> {
+15 -15
View File
@@ -18,7 +18,6 @@
//! blocks. CHT roots are stored for headers of ancient blocks.
use std::{sync::{Weak, Arc}, collections::HashMap};
use futures::{Future, IntoFuture};
use parking_lot::Mutex;
use runtime_primitives::{Justification, generic::BlockId};
@@ -122,14 +121,15 @@ impl<S, F, Block> BlockchainHeaderBackend<Block> for Blockchain<S, F> where Bloc
return Ok(None);
}
self.fetcher().upgrade().ok_or(ClientError::NotAvailableOnLightClient)?
.remote_header(RemoteHeaderRequest {
cht_root: self.storage.header_cht_root(cht::size(), number)?,
block: number,
retry_count: None,
futures::executor::block_on(
self.fetcher().upgrade()
.ok_or(ClientError::NotAvailableOnLightClient)?
.remote_header(RemoteHeaderRequest {
cht_root: self.storage.header_cht_root(cht::size(), number)?,
block: number,
retry_count: None,
})
.into_future().wait()
.map(Some)
).map(Some)
}
}
}
@@ -158,13 +158,13 @@ impl<S, F, Block> BlockchainBackend<Block> for Blockchain<S, F> where Block: Blo
None => return Ok(None),
};
self.fetcher().upgrade().ok_or(ClientError::NotAvailableOnLightClient)?
.remote_body(RemoteBodyRequest {
header,
retry_count: None,
})
.into_future().wait()
.map(Some)
futures::executor::block_on(
self.fetcher().upgrade().ok_or(ClientError::NotAvailableOnLightClient)?
.remote_body(RemoteBodyRequest {
header,
retry_count: None,
})
).map(Some)
}
fn justification(&self, _id: BlockId<Block>) -> ClientResult<Option<Justification>> {
@@ -21,7 +21,6 @@ use std::{
collections::HashSet, sync::Arc, panic::UnwindSafe, result,
marker::PhantomData, cell::RefCell, rc::Rc,
};
use futures::{IntoFuture, Future};
use parity_codec::{Encode, Decode};
use primitives::{offchain, H256, Blake2Hasher, convert_hash, NativeOrEncoded};
@@ -100,13 +99,13 @@ where
let block_hash = self.blockchain.expect_block_hash_from_id(id)?;
let block_header = self.blockchain.expect_header(id.clone())?;
self.fetcher.remote_call(RemoteCallRequest {
futures::executor::block_on(self.fetcher.remote_call(RemoteCallRequest {
block: block_hash,
header: block_header,
method: method.into(),
call_data: call_data.to_vec(),
retry_count: None,
}).into_future().wait()
}))
}
fn contextual_call<
+15 -15
View File
@@ -19,7 +19,7 @@
use std::sync::Arc;
use std::collections::BTreeMap;
use std::marker::PhantomData;
use futures::IntoFuture;
use std::future::Future;
use hash_db::{HashDB, Hasher};
use parity_codec::{Decode, Encode};
@@ -141,15 +141,15 @@ pub struct RemoteBodyRequest<Header: HeaderT> {
/// is correct (see FetchedDataChecker) and return already checked data.
pub trait Fetcher<Block: BlockT>: Send + Sync {
/// Remote header future.
type RemoteHeaderResult: IntoFuture<Item = Block::Header, Error = ClientError>;
type RemoteHeaderResult: Future<Output = Result<Block::Header, ClientError>>;
/// Remote storage read future.
type RemoteReadResult: IntoFuture<Item = Option<Vec<u8>>, Error = ClientError>;
type RemoteReadResult: Future<Output = Result<Option<Vec<u8>>, ClientError>>;
/// Remote call result future.
type RemoteCallResult: IntoFuture<Item = Vec<u8>, Error = ClientError>;
type RemoteCallResult: Future<Output = Result<Vec<u8>, ClientError>>;
/// Remote changes result future.
type RemoteChangesResult: IntoFuture<Item = Vec<(NumberFor<Block>, u32)>, Error = ClientError>;
type RemoteChangesResult: Future<Output = Result<Vec<(NumberFor<Block>, u32)>, ClientError>>;
/// Remote block body result future.
type RemoteBodyResult: IntoFuture<Item = Vec<Block::Extrinsic>, Error = ClientError>;
type RemoteBodyResult: Future<Output = Result<Vec<Block::Extrinsic>, ClientError>>;
/// Fetch remote header.
fn remote_header(&self, request: RemoteHeaderRequest<Block::Header>) -> Self::RemoteHeaderResult;
@@ -484,7 +484,7 @@ impl<'a, H, Number, Hash> ChangesTrieRootsStorage<H, Number> for RootsStorage<'a
#[cfg(test)]
pub mod tests {
use futures::future::{ok, err, FutureResult};
use futures::future::Ready;
use parking_lot::Mutex;
use parity_codec::Decode;
use crate::client::tests::prepare_client_with_key_changes;
@@ -508,19 +508,19 @@ pub mod tests {
pub type OkCallFetcher = Mutex<Vec<u8>>;
fn not_implemented_in_tests<T, E>() -> FutureResult<T, E>
fn not_implemented_in_tests<T, E>() -> Ready<Result<T, E>>
where
E: std::convert::From<&'static str>,
{
err("Not implemented on test node".into())
futures::future::ready(Err("Not implemented on test node".into()))
}
impl Fetcher<Block> for OkCallFetcher {
type RemoteHeaderResult = FutureResult<Header, ClientError>;
type RemoteReadResult = FutureResult<Option<Vec<u8>>, ClientError>;
type RemoteCallResult = FutureResult<Vec<u8>, ClientError>;
type RemoteChangesResult = FutureResult<Vec<(NumberFor<Block>, u32)>, ClientError>;
type RemoteBodyResult = FutureResult<Vec<Extrinsic>, ClientError>;
type RemoteHeaderResult = Ready<Result<Header, ClientError>>;
type RemoteReadResult = Ready<Result<Option<Vec<u8>>, ClientError>>;
type RemoteCallResult = Ready<Result<Vec<u8>, ClientError>>;
type RemoteChangesResult = Ready<Result<Vec<(NumberFor<Block>, u32)>, ClientError>>;
type RemoteBodyResult = Ready<Result<Vec<Extrinsic>, ClientError>>;
fn remote_header(&self, _request: RemoteHeaderRequest<Header>) -> Self::RemoteHeaderResult {
not_implemented_in_tests()
@@ -535,7 +535,7 @@ pub mod tests {
}
fn remote_call(&self, _request: RemoteCallRequest<Header>) -> Self::RemoteCallResult {
ok((*self.lock()).clone())
futures::future::ready(Ok((*self.lock()).clone()))
}
fn remote_changes(&self, _request: RemoteChangesRequest<Header>) -> Self::RemoteChangesResult {
+34 -19
View File
@@ -22,7 +22,7 @@ use std::{
};
use fnv::{FnvHashSet, FnvHashMap};
use futures::sync::mpsc;
use futures::channel::mpsc;
use primitives::storage::{StorageKey, StorageData};
use runtime_primitives::traits::Block as BlockT;
@@ -309,7 +309,6 @@ impl<Block: BlockT> StorageNotifications<Block> {
mod tests {
use runtime_primitives::testing::{H256 as Hash, Block as RawBlock, ExtrinsicWrapper};
use super::*;
use futures::Stream;
use std::iter::{empty, Empty};
type TestChangeSet = (
@@ -348,7 +347,9 @@ mod tests {
// given
let mut notifications = StorageNotifications::<Block>::default();
let child_filter = [(StorageKey(vec![4]), None)];
let mut recv = notifications.listen(None, Some(&child_filter[..])).wait();
let mut recv = futures::executor::block_on_stream(
notifications.listen(None, Some(&child_filter[..]))
);
// when
let changeset = vec![
@@ -367,13 +368,13 @@ mod tests {
);
// then
assert_eq!(recv.next().unwrap(), Ok((Hash::from_low_u64_be(1), (vec![
assert_eq!(recv.next().unwrap(), (Hash::from_low_u64_be(1), (vec![
(StorageKey(vec![2]), Some(StorageData(vec![3]))),
(StorageKey(vec![3]), None),
], vec![(StorageKey(vec![4]), vec![
(StorageKey(vec![5]), Some(StorageData(vec![4]))),
(StorageKey(vec![6]), None),
])]).into())));
])]).into()));
}
#[test]
@@ -381,9 +382,15 @@ mod tests {
// given
let mut notifications = StorageNotifications::<Block>::default();
let child_filter = [(StorageKey(vec![4]), Some(vec![StorageKey(vec![5])]))];
let mut recv1 = notifications.listen(Some(&[StorageKey(vec![1])]), None).wait();
let mut recv2 = notifications.listen(Some(&[StorageKey(vec![2])]), None).wait();
let mut recv3 = notifications.listen(Some(&[]), Some(&child_filter)).wait();
let mut recv1 = futures::executor::block_on_stream(
notifications.listen(Some(&[StorageKey(vec![1])]), None)
);
let mut recv2 = futures::executor::block_on_stream(
notifications.listen(Some(&[StorageKey(vec![2])]), None)
);
let mut recv3 = futures::executor::block_on_stream(
notifications.listen(Some(&[]), Some(&child_filter))
);
// when
let changeset = vec![
@@ -403,16 +410,16 @@ mod tests {
);
// then
assert_eq!(recv1.next().unwrap(), Ok((Hash::from_low_u64_be(1), (vec![
assert_eq!(recv1.next().unwrap(), (Hash::from_low_u64_be(1), (vec![
(StorageKey(vec![1]), None),
], vec![]).into())));
assert_eq!(recv2.next().unwrap(), Ok((Hash::from_low_u64_be(1), (vec![
], vec![]).into()));
assert_eq!(recv2.next().unwrap(), (Hash::from_low_u64_be(1), (vec![
(StorageKey(vec![2]), Some(StorageData(vec![3]))),
], vec![]).into())));
assert_eq!(recv3.next().unwrap(), Ok((Hash::from_low_u64_be(1), (vec![],
], vec![]).into()));
assert_eq!(recv3.next().unwrap(), (Hash::from_low_u64_be(1), (vec![],
vec![
(StorageKey(vec![4]), vec![(StorageKey(vec![5]), Some(StorageData(vec![4])))]),
]).into())));
]).into()));
}
@@ -422,10 +429,18 @@ mod tests {
let mut notifications = StorageNotifications::<Block>::default();
{
let child_filter = [(StorageKey(vec![4]), Some(vec![StorageKey(vec![5])]))];
let _recv1 = notifications.listen(Some(&[StorageKey(vec![1])]), None).wait();
let _recv2 = notifications.listen(Some(&[StorageKey(vec![2])]), None).wait();
let _recv3 = notifications.listen(None, None).wait();
let _recv4 = notifications.listen(None, Some(&child_filter)).wait();
let _recv1 = futures::executor::block_on_stream(
notifications.listen(Some(&[StorageKey(vec![1])]), None)
);
let _recv2 = futures::executor::block_on_stream(
notifications.listen(Some(&[StorageKey(vec![2])]), None)
);
let _recv3 = futures::executor::block_on_stream(
notifications.listen(None, None)
);
let _recv4 = futures::executor::block_on_stream(
notifications.listen(None, Some(&child_filter))
);
assert_eq!(notifications.listeners.len(), 2);
assert_eq!(notifications.wildcard_listeners.len(), 2);
assert_eq!(notifications.child_listeners.len(), 1);
@@ -450,7 +465,7 @@ mod tests {
// given
let mut recv = {
let mut notifications = StorageNotifications::<Block>::default();
let recv = notifications.listen(None, None).wait();
let recv = futures::executor::block_on_stream(notifications.listen(None, None));
// when
let changeset = vec![];