Upgrade jsonrpc to 0.18.0 (#9547)

* Upgrade jsonrpc to 0.18.0

I think this says all :P

* 🤦

* Fmt etc

* Fix tests

* Fix tests again...

* Better impl

* Revert "Tell dependabot to ignore jsonrpc-* updates (#9518)"

This reverts commit 6e0cd5587d.
This commit is contained in:
Bastian Köcher
2021-08-13 08:46:07 +02:00
committed by GitHub
parent 199b2883af
commit c44aba89e6
65 changed files with 1422 additions and 1291 deletions
+49 -46
View File
@@ -28,12 +28,10 @@ use sp_blockchain::HeaderBackend;
use codec::{Decode, Encode};
use futures::{
compat::Compat,
future::{ready, FutureExt, TryFutureExt},
StreamExt as _,
future::{FutureExt, TryFutureExt},
SinkExt, StreamExt as _,
};
use jsonrpc_pubsub::{manager::SubscriptionManager, typed::Subscriber, SubscriptionId};
use rpc::futures::{future::result, Future, Sink};
use sc_rpc_api::DenyUnsafe;
use sc_transaction_pool_api::{
error::IntoPoolError, BlockHash, InPoolTransaction, TransactionFor, TransactionPool,
@@ -42,7 +40,7 @@ use sc_transaction_pool_api::{
use sp_api::ProvideRuntimeApi;
use sp_core::Bytes;
use sp_keystore::{SyncCryptoStore, SyncCryptoStorePtr};
use sp_runtime::generic;
use sp_runtime::{generic, traits::Block as BlockT};
use sp_session::SessionKeys;
use self::error::{Error, FutureResult, Result};
@@ -88,6 +86,8 @@ where
P: TransactionPool + Sync + Send + 'static,
Client: HeaderBackend<P::Block> + ProvideRuntimeApi<P::Block> + Send + Sync + 'static,
Client::Api: SessionKeys<P::Block>,
P::Hash: Unpin,
<P::Block as BlockT>::Hash: Unpin,
{
type Metadata = crate::Metadata;
@@ -135,19 +135,18 @@ where
fn submit_extrinsic(&self, ext: Bytes) -> FutureResult<TxHash<P>> {
let xt = match Decode::decode(&mut &ext[..]) {
Ok(xt) => xt,
Err(err) => return Box::new(result(Err(err.into()))),
Err(err) => return async move { Err(err.into()) }.boxed(),
};
let best_block_hash = self.client.info().best_hash;
Box::new(
self.pool
.submit_one(&generic::BlockId::hash(best_block_hash), TX_SOURCE, xt)
.compat()
.map_err(|e| {
e.into_pool_error()
.map(Into::into)
.unwrap_or_else(|e| error::Error::Verification(Box::new(e)).into())
}),
)
self.pool
.submit_one(&generic::BlockId::hash(best_block_hash), TX_SOURCE, xt)
.map_err(|e| {
e.into_pool_error()
.map(Into::into)
.unwrap_or_else(|e| error::Error::Verification(Box::new(e)).into())
})
.boxed()
}
fn pending_extrinsics(&self) -> Result<Vec<Bytes>> {
@@ -185,46 +184,50 @@ where
subscriber: Subscriber<TransactionStatus<TxHash<P>, BlockHash<P>>>,
xt: Bytes,
) {
let submit = || -> Result<_> {
let best_block_hash = self.client.info().best_hash;
let dxt = TransactionFor::<P>::decode(&mut &xt[..]).map_err(error::Error::from)?;
Ok(self
.pool
.submit_and_watch(&generic::BlockId::hash(best_block_hash), TX_SOURCE, dxt)
.map_err(|e| {
e.into_pool_error()
.map(error::Error::from)
.unwrap_or_else(|e| error::Error::Verification(Box::new(e)).into())
}))
let best_block_hash = self.client.info().best_hash;
let dxt = match TransactionFor::<P>::decode(&mut &xt[..]).map_err(error::Error::from) {
Ok(tx) => tx,
Err(err) => {
warn!("Failed to submit extrinsic: {}", err);
// reject the subscriber (ignore errors - we don't care if subscriber is no longer
// there).
let _ = subscriber.reject(err.into());
return
},
};
let submit = self
.pool
.submit_and_watch(&generic::BlockId::hash(best_block_hash), TX_SOURCE, dxt)
.map_err(|e| {
e.into_pool_error()
.map(error::Error::from)
.unwrap_or_else(|e| error::Error::Verification(Box::new(e)).into())
});
let subscriptions = self.subscriptions.clone();
let future = ready(submit())
.and_then(|res| res)
// convert the watcher into a `Stream`
.map(|res| res.map(|stream| stream.map(|v| Ok::<_, ()>(Ok(v)))))
// now handle the import result,
// start a new subscrition
.map(move |result| match result {
Ok(watcher) => {
subscriptions.add(subscriber, move |sink| {
sink.sink_map_err(|e| log::debug!("Subscription sink failed: {:?}", e))
.send_all(Compat::new(watcher))
.map(|_| ())
});
},
let future = async move {
let tx_stream = match submit.await {
Ok(s) => s,
Err(err) => {
warn!("Failed to submit extrinsic: {}", err);
// reject the subscriber (ignore errors - we don't care if subscriber is no
// longer there).
let _ = subscriber.reject(err.into());
return
},
});
};
let res = self
.subscriptions
.executor()
.execute(Box::new(Compat::new(future.map(|_| Ok(())))));
subscriptions.add(subscriber, move |sink| {
tx_stream
.map(|v| Ok(Ok(v)))
.forward(sink.sink_map_err(|e| warn!("Error sending notifications: {:?}", e)))
.map(drop)
});
};
let res = self.subscriptions.executor().spawn_obj(future.boxed().into());
if res.is_err() {
warn!("Error spawning subscription RPC task.");
}
+14 -15
View File
@@ -20,8 +20,7 @@ use super::*;
use assert_matches::assert_matches;
use codec::Encode;
use futures::{compat::Future01CompatExt, executor};
use rpc::futures::Stream as _;
use futures::executor;
use sc_transaction_pool::{BasicPool, FullChainApi};
use sp_core::{
blake2_256,
@@ -86,10 +85,10 @@ fn submit_transaction_should_not_cause_error() {
let h: H256 = blake2_256(&xt).into();
assert_matches!(
AuthorApi::submit_extrinsic(&p, xt.clone().into()).wait(),
executor::block_on(AuthorApi::submit_extrinsic(&p, xt.clone().into())),
Ok(h2) if h == h2
);
assert!(AuthorApi::submit_extrinsic(&p, xt.into()).wait().is_err());
assert!(executor::block_on(AuthorApi::submit_extrinsic(&p, xt.into())).is_err());
}
#[test]
@@ -99,10 +98,10 @@ fn submit_rich_transaction_should_not_cause_error() {
let h: H256 = blake2_256(&xt).into();
assert_matches!(
AuthorApi::submit_extrinsic(&p, xt.clone().into()).wait(),
executor::block_on(AuthorApi::submit_extrinsic(&p, xt.clone().into())),
Ok(h2) if h == h2
);
assert!(AuthorApi::submit_extrinsic(&p, xt.into()).wait().is_err());
assert!(executor::block_on(AuthorApi::submit_extrinsic(&p, xt.into())).is_err());
}
#[test]
@@ -120,7 +119,7 @@ fn should_watch_extrinsic() {
uxt(AccountKeyring::Alice, 0).encode().into(),
);
let id = executor::block_on(id_rx.compat()).unwrap().unwrap();
let id = executor::block_on(id_rx).unwrap().unwrap();
assert_matches!(id, SubscriptionId::String(_));
let id = match id {
@@ -138,8 +137,8 @@ fn should_watch_extrinsic() {
};
tx.into_signed_tx()
};
AuthorApi::submit_extrinsic(&p, replacement.encode().into()).wait().unwrap();
let (res, data) = executor::block_on(data.into_future().compat()).unwrap();
executor::block_on(AuthorApi::submit_extrinsic(&p, replacement.encode().into())).unwrap();
let (res, data) = executor::block_on(data.into_future());
let expected = Some(format!(
r#"{{"jsonrpc":"2.0","method":"test","params":{{"result":"ready","subscription":"{}"}}}}"#,
@@ -154,7 +153,7 @@ fn should_watch_extrinsic() {
id,
));
let res = executor::block_on(data.into_future().compat()).unwrap().0;
let res = executor::block_on(data.into_future()).0;
assert_eq!(res, expected);
}
@@ -174,7 +173,7 @@ fn should_return_watch_validation_error() {
);
// then
let res = executor::block_on(id_rx.compat()).unwrap();
let res = executor::block_on(id_rx).unwrap();
assert!(res.is_err(), "Expected the transaction to be rejected as invalid.");
}
@@ -183,7 +182,7 @@ fn should_return_pending_extrinsics() {
let p = TestSetup::default().author();
let ex = uxt(AccountKeyring::Alice, 0);
AuthorApi::submit_extrinsic(&p, ex.encode().into()).wait().unwrap();
executor::block_on(AuthorApi::submit_extrinsic(&p, ex.encode().into())).unwrap();
assert_matches!(
p.pending_extrinsics(),
Ok(ref expected) if *expected == vec![Bytes(ex.encode())]
@@ -196,11 +195,11 @@ fn should_remove_extrinsics() {
let p = setup.author();
let ex1 = uxt(AccountKeyring::Alice, 0);
p.submit_extrinsic(ex1.encode().into()).wait().unwrap();
executor::block_on(p.submit_extrinsic(ex1.encode().into())).unwrap();
let ex2 = uxt(AccountKeyring::Alice, 1);
p.submit_extrinsic(ex2.encode().into()).wait().unwrap();
executor::block_on(p.submit_extrinsic(ex2.encode().into())).unwrap();
let ex3 = uxt(AccountKeyring::Bob, 0);
let hash3 = p.submit_extrinsic(ex3.encode().into()).wait().unwrap();
let hash3 = executor::block_on(p.submit_extrinsic(ex3.encode().into())).unwrap();
assert_eq!(setup.pool.status().ready, 3);
// now remove all 3