Merge Notifier and TransactionsPoolNotifier (#10591)
* Merge `Notifier` and `TransactionsPoolNotifier` * fix tests
This commit is contained in:
@@ -20,7 +20,7 @@ use std::sync::{Arc, Weak};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use jsonrpc_core::{BoxFuture, Result, Error};
|
||||
use jsonrpc_core::futures::{self, Future, IntoFuture, Stream};
|
||||
use jsonrpc_core::futures::{self, Future, IntoFuture, Stream, sync::mpsc};
|
||||
use jsonrpc_pubsub::typed::{Sink, Subscriber};
|
||||
use jsonrpc_pubsub::SubscriptionId;
|
||||
|
||||
@@ -80,23 +80,39 @@ impl<C> EthPubSubClient<C>
|
||||
}
|
||||
}
|
||||
|
||||
impl<C> EthPubSubClient<C> {
|
||||
impl<C> EthPubSubClient<C>
|
||||
where
|
||||
C: 'static + Send + Sync {
|
||||
|
||||
/// Creates new `EthPubSubClient`.
|
||||
pub fn new(client: Arc<C>, executor: Executor) -> Self {
|
||||
pub fn new(client: Arc<C>, executor: Executor, pool_receiver: mpsc::UnboundedReceiver<Arc<Vec<H256>>>) -> Self {
|
||||
let heads_subscribers = Arc::new(RwLock::new(Subscribers::default()));
|
||||
let logs_subscribers = Arc::new(RwLock::new(Subscribers::default()));
|
||||
let transactions_subscribers = Arc::new(RwLock::new(Subscribers::default()));
|
||||
let sync_subscribers = Arc::new(RwLock::new(Subscribers::default()));
|
||||
|
||||
let handler = Arc::new(ChainNotificationHandler {
|
||||
client,
|
||||
executor,
|
||||
heads_subscribers: heads_subscribers.clone(),
|
||||
logs_subscribers: logs_subscribers.clone(),
|
||||
transactions_subscribers: transactions_subscribers.clone(),
|
||||
sync_subscribers: sync_subscribers.clone(),
|
||||
});
|
||||
let handler2 = Arc::downgrade(&handler);
|
||||
|
||||
handler.executor.spawn(pool_receiver
|
||||
.for_each(move |hashes| {
|
||||
if let Some(handler2) = handler2.upgrade() {
|
||||
handler2.notify_new_transactions(&hashes.to_vec());
|
||||
return Ok(())
|
||||
}
|
||||
Err(())
|
||||
})
|
||||
);
|
||||
|
||||
EthPubSubClient {
|
||||
handler: Arc::new(ChainNotificationHandler {
|
||||
client,
|
||||
executor,
|
||||
heads_subscribers: heads_subscribers.clone(),
|
||||
logs_subscribers: logs_subscribers.clone(),
|
||||
transactions_subscribers: transactions_subscribers.clone(),
|
||||
sync_subscribers: sync_subscribers.clone(),
|
||||
}),
|
||||
handler,
|
||||
sync_subscribers,
|
||||
heads_subscribers,
|
||||
logs_subscribers,
|
||||
@@ -123,6 +139,7 @@ where
|
||||
cache: Arc<Mutex<Cache>>,
|
||||
executor: Executor,
|
||||
gas_price_percentile: usize,
|
||||
pool_receiver: mpsc::UnboundedReceiver<Arc<Vec<H256>>>
|
||||
) -> Self {
|
||||
let fetch = LightFetch {
|
||||
client,
|
||||
@@ -131,7 +148,7 @@ where
|
||||
cache,
|
||||
gas_price_percentile,
|
||||
};
|
||||
EthPubSubClient::new(Arc::new(fetch), executor)
|
||||
EthPubSubClient::new(Arc::new(fetch), executor, pool_receiver)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,7 +222,7 @@ impl<C> ChainNotificationHandler<C> {
|
||||
}
|
||||
|
||||
/// Notify all subscribers about new transaction hashes.
|
||||
pub fn notify_new_transactions(&self, hashes: &[H256]) {
|
||||
fn notify_new_transactions(&self, hashes: &[H256]) {
|
||||
for subscriber in self.transactions_subscribers.read().values() {
|
||||
for hash in hashes {
|
||||
Self::notify(&self.executor, subscriber, pubsub::Result::TransactionHash(*hash));
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use jsonrpc_core::MetaIoHandler;
|
||||
use jsonrpc_core::futures::{self, Stream, Future};
|
||||
use jsonrpc_core::futures::{self, Stream, Future, sync::mpsc};
|
||||
use jsonrpc_pubsub::Session;
|
||||
|
||||
use std::time::Duration;
|
||||
@@ -40,7 +40,9 @@ fn should_subscribe_to_new_heads() {
|
||||
let h2 = client.block_hash_delta_minus(2);
|
||||
let h1 = client.block_hash_delta_minus(3);
|
||||
|
||||
let pubsub = EthPubSubClient::new(Arc::new(client), el.executor());
|
||||
let (_, pool_receiver) = mpsc::unbounded();
|
||||
|
||||
let pubsub = EthPubSubClient::new(Arc::new(client), el.executor(), pool_receiver);
|
||||
let handler = pubsub.handler().upgrade().unwrap();
|
||||
let pubsub = pubsub.to_delegate();
|
||||
|
||||
@@ -112,7 +114,9 @@ fn should_subscribe_to_logs() {
|
||||
}
|
||||
]);
|
||||
|
||||
let pubsub = EthPubSubClient::new(Arc::new(client), el.executor());
|
||||
let (_, pool_receiver) = mpsc::unbounded();
|
||||
|
||||
let pubsub = EthPubSubClient::new(Arc::new(client), el.executor(), pool_receiver);
|
||||
let handler = pubsub.handler().upgrade().unwrap();
|
||||
let pubsub = pubsub.to_delegate();
|
||||
|
||||
@@ -159,8 +163,9 @@ fn should_subscribe_to_pending_transactions() {
|
||||
let el = Runtime::with_thread_count(1);
|
||||
let client = TestBlockChainClient::new();
|
||||
|
||||
let pubsub = EthPubSubClient::new(Arc::new(client), el.executor());
|
||||
let handler = pubsub.handler().upgrade().unwrap();
|
||||
let (pool_sender, pool_receiver) = mpsc::unbounded();
|
||||
|
||||
let pubsub = EthPubSubClient::new(Arc::new(client), el.executor(), pool_receiver);
|
||||
let pubsub = pubsub.to_delegate();
|
||||
|
||||
let mut io = MetaIoHandler::default();
|
||||
@@ -181,7 +186,7 @@ fn should_subscribe_to_pending_transactions() {
|
||||
assert_eq!(io.handle_request_sync(request, metadata.clone()), Some(response.to_owned()));
|
||||
|
||||
// Send new transactions
|
||||
handler.notify_new_transactions(&[H256::from_low_u64_be(5), H256::from_low_u64_be(7)]);
|
||||
pool_sender.unbounded_send(Arc::new(vec![H256::from_low_u64_be(5), H256::from_low_u64_be(7)])).unwrap();
|
||||
|
||||
let (res, receiver) = receiver.into_future().wait().unwrap();
|
||||
let response = r#"{"jsonrpc":"2.0","method":"eth_subscription","params":{"result":"0x0000000000000000000000000000000000000000000000000000000000000005","subscription":"0x43ca64edf03768e1"}}"#;
|
||||
@@ -205,7 +210,8 @@ fn eth_subscribe_syncing() {
|
||||
// given
|
||||
let el = Runtime::with_thread_count(1);
|
||||
let client = TestBlockChainClient::new();
|
||||
let pubsub = EthPubSubClient::new(Arc::new(client), el.executor());
|
||||
let (_, pool_receiver) = mpsc::unbounded();
|
||||
let pubsub = EthPubSubClient::new(Arc::new(client), el.executor(), pool_receiver);
|
||||
let pubsub = pubsub.to_delegate();
|
||||
|
||||
let mut io = MetaIoHandler::default();
|
||||
|
||||
Reference in New Issue
Block a user