hacky update jsonrpsee

This commit is contained in:
Niklas
2021-12-22 13:19:54 +01:00
parent 2bfc3898b7
commit bca14735ab
8 changed files with 95 additions and 81 deletions
+1 -2
View File
@@ -24,13 +24,12 @@ chameleon = "0.1.0"
scale-info = { version = "1.0.0", features = ["bit-vec"] } scale-info = { version = "1.0.0", features = ["bit-vec"] }
futures = "0.3.13" futures = "0.3.13"
hex = "0.4.3" hex = "0.4.3"
jsonrpsee = { git = "https://github.com/paritytech/jsonrpsee/", branch = "jsonrpsee-clients-expose-tls-feature", features = ["macros", "core-client", "client", "client-ws-transport"] } jsonrpsee = { git = "https://github.com/paritytech/jsonrpsee/", branch = "na-fix-core-client-feature", features = ["macros", "async-client", "client-ws-transport"] }
log = "0.4.14" log = "0.4.14"
num-traits = { version = "0.2.14", default-features = false } num-traits = { version = "0.2.14", default-features = false }
serde = { version = "1.0.124", features = ["derive"] } serde = { version = "1.0.124", features = ["derive"] }
serde_json = "1.0.64" serde_json = "1.0.64"
thiserror = "1.0.24" thiserror = "1.0.24"
url = "2.2.1"
subxt-macro = { version = "0.1.0", path = "macro" } subxt-macro = { version = "0.1.0", path = "macro" }
+1 -1
View File
@@ -17,7 +17,7 @@ keywords = ["parity", "substrate", "blockchain"]
[dependencies] [dependencies]
async-std = { version = "1.8.0", features = ["tokio1"] } async-std = { version = "1.8.0", features = ["tokio1"] }
futures = "0.3.9" futures = "0.3.9"
jsonrpsee = { git = "https://github.com/paritytech/jsonrpsee/", branch = "extract-async-client", features = ["client"] } jsonrpsee = { git = "https://github.com/paritytech/jsonrpsee/", branch = "na-fix-core-client-feature", features = ["async-client"] }
log = "0.4.13" log = "0.4.13"
thiserror = "1.0.23" thiserror = "1.0.23"
serde_json = "1" serde_json = "1"
+6 -8
View File
@@ -31,14 +31,12 @@ use futures::{
sink::SinkExt, sink::SinkExt,
stream::StreamExt, stream::StreamExt,
}; };
use jsonrpsee::{ use jsonrpsee::core::{
core_client::Client as JsonRpcClient, async_trait,
types::{ client::Client as JsonRpcClient,
async_trait, traits::{
traits::{ TransportReceiver,
TransportReceiver, TransportSender,
TransportSender,
},
}, },
}; };
use sc_network::config::TransportConfig; use sc_network::config::TransportConfig;
+4 -2
View File
@@ -61,7 +61,8 @@ async fn simple_transfer() -> Result<(), Box<dyn std::error::Error>> {
.sign_and_submit_then_watch(&signer) .sign_and_submit_then_watch(&signer)
.await? .await?
.wait_for_finalized_success() .wait_for_finalized_success()
.await.unwrap()?; .await
.unwrap()?;
let transfer_event = let transfer_event =
balance_transfer.find_first_event::<polkadot::balances::events::Transfer>()?; balance_transfer.find_first_event::<polkadot::balances::events::Transfer>()?;
@@ -93,7 +94,8 @@ async fn simple_transfer_separate_events() -> Result<(), Box<dyn std::error::Err
.sign_and_submit_then_watch(&signer) .sign_and_submit_then_watch(&signer)
.await? .await?
.wait_for_finalized() .wait_for_finalized()
.await.unwrap()?; .await
.unwrap()?;
// Now we know it's been finalized, we can get hold of a couple of // Now we know it's been finalized, we can get hold of a couple of
// details, including events. Calling `wait_for_finalized_success` is // details, including events. Calling `wait_for_finalized_success` is
+4 -1
View File
@@ -14,7 +14,10 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with subxt. If not, see <http://www.gnu.org/licenses/>. // along with subxt. If not, see <http://www.gnu.org/licenses/>.
use jsonrpsee::core::{DeserializeOwned, client::Subscription}; use jsonrpsee::core::{
client::Subscription,
DeserializeOwned,
};
use sp_core::{ use sp_core::{
storage::{ storage::{
StorageChangeSet, StorageChangeSet,
+75 -63
View File
@@ -64,51 +64,54 @@ impl<'client, T: Config> TransactionProgress<'client, T> {
// Return the next item otherwise: // Return the next item otherwise:
let res = sub.next().await?; let res = sub.next().await?;
Some(res.map(|status| { Some(
match status { res.map(|status| {
SubstrateTransactionStatus::Future => TransactionStatus::Future, match status {
SubstrateTransactionStatus::Ready => TransactionStatus::Ready, SubstrateTransactionStatus::Future => TransactionStatus::Future,
SubstrateTransactionStatus::Broadcast(peers) => { SubstrateTransactionStatus::Ready => TransactionStatus::Ready,
TransactionStatus::Broadcast(peers) SubstrateTransactionStatus::Broadcast(peers) => {
TransactionStatus::Broadcast(peers)
}
SubstrateTransactionStatus::InBlock(hash) => {
TransactionStatus::InBlock(TransactionInBlock {
block_hash: hash,
ext_hash: self.ext_hash,
client: self.client,
})
}
SubstrateTransactionStatus::Retracted(hash) => {
TransactionStatus::Retracted(hash)
}
SubstrateTransactionStatus::Usurped(hash) => {
TransactionStatus::Usurped(hash)
}
SubstrateTransactionStatus::Dropped => TransactionStatus::Dropped,
SubstrateTransactionStatus::Invalid => TransactionStatus::Invalid,
// Only the following statuses are actually considered "final" (see the substrate
// docs on `TransactionStatus`). Basically, either the transaction makes it into a
// block, or we eventually give up on waiting for it to make it into a block.
// Even `Dropped`/`Invalid`/`Usurped` transactions might make it into a block eventually.
//
// As an example, a transaction that is `Invalid` on one node due to having the wrong
// nonce might still be valid on some fork on another node which ends up being finalized.
// Equally, a transaction `Dropped` from one node may still be in the transaction pool,
// and make it into a block, on another node. Likewise with `Usurped`.
SubstrateTransactionStatus::FinalityTimeout(hash) => {
self.sub = None;
TransactionStatus::FinalityTimeout(hash)
}
SubstrateTransactionStatus::Finalized(hash) => {
self.sub = None;
TransactionStatus::Finalized(TransactionInBlock {
block_hash: hash,
ext_hash: self.ext_hash,
client: self.client,
})
}
} }
SubstrateTransactionStatus::InBlock(hash) => { })
TransactionStatus::InBlock(TransactionInBlock { .map_err(Into::into),
block_hash: hash, )
ext_hash: self.ext_hash,
client: self.client,
})
}
SubstrateTransactionStatus::Retracted(hash) => {
TransactionStatus::Retracted(hash)
}
SubstrateTransactionStatus::Usurped(hash) => {
TransactionStatus::Usurped(hash)
}
SubstrateTransactionStatus::Dropped => TransactionStatus::Dropped,
SubstrateTransactionStatus::Invalid => TransactionStatus::Invalid,
// Only the following statuses are actually considered "final" (see the substrate
// docs on `TransactionStatus`). Basically, either the transaction makes it into a
// block, or we eventually give up on waiting for it to make it into a block.
// Even `Dropped`/`Invalid`/`Usurped` transactions might make it into a block eventually.
//
// As an example, a transaction that is `Invalid` on one node due to having the wrong
// nonce might still be valid on some fork on another node which ends up being finalized.
// Equally, a transaction `Dropped` from one node may still be in the transaction pool,
// and make it into a block, on another node. Likewise with `Usurped`.
SubstrateTransactionStatus::FinalityTimeout(hash) => {
self.sub = None;
TransactionStatus::FinalityTimeout(hash)
}
SubstrateTransactionStatus::Finalized(hash) => {
self.sub = None;
TransactionStatus::Finalized(TransactionInBlock {
block_hash: hash,
ext_hash: self.ext_hash,
client: self.client,
})
}
}
}).map_err(Into::into))
} }
/// Wait for the transaction to be in a block (but not necessarily finalized), and return /// Wait for the transaction to be in a block (but not necessarily finalized), and return
@@ -127,17 +130,20 @@ impl<'client, T: Config> TransactionProgress<'client, T> {
) -> Option<Result<TransactionInBlock<'client, T>, Error>> { ) -> Option<Result<TransactionInBlock<'client, T>, Error>> {
loop { loop {
match self.next().await? { match self.next().await? {
Ok(status) => match status { Ok(status) => {
// Finalized or otherwise in a block! Return. match status {
TransactionStatus::InBlock(s) | TransactionStatus::Finalized(s) => { // Finalized or otherwise in a block! Return.
return Some(Ok(s)) TransactionStatus::InBlock(s)
| TransactionStatus::Finalized(s) => return Some(Ok(s)),
// Error scenarios; return the error.
TransactionStatus::FinalityTimeout(_) => {
return Some(Err(
TransactionError::FinalitySubscriptionTimeout.into(),
))
}
// Ignore anything else and wait for next status event:
_ => continue,
} }
// Error scenarios; return the error.
TransactionStatus::FinalityTimeout(_) => {
return Some(Err(TransactionError::FinalitySubscriptionTimeout.into()))
}
// Ignore anything else and wait for next status event:
_ => continue,
} }
Err(err) => return Some(Err(err)), Err(err) => return Some(Err(err)),
} }
@@ -159,15 +165,19 @@ impl<'client, T: Config> TransactionProgress<'client, T> {
) -> Option<Result<TransactionInBlock<'client, T>, Error>> { ) -> Option<Result<TransactionInBlock<'client, T>, Error>> {
loop { loop {
match self.next().await? { match self.next().await? {
Ok(status) => match status { Ok(status) => {
// finalized! return. match status {
TransactionStatus::Finalized(s) => return Some(Ok(s)), // finalized! return.
// error scenarios; return the error. TransactionStatus::Finalized(s) => return Some(Ok(s)),
TransactionStatus::FinalityTimeout(_) => { // error scenarios; return the error.
return Some(Err(TransactionError::FinalitySubscriptionTimeout.into())) TransactionStatus::FinalityTimeout(_) => {
return Some(Err(
TransactionError::FinalitySubscriptionTimeout.into(),
))
}
// ignore and wait for next status event:
_ => continue,
} }
// ignore and wait for next status event:
_ => continue,
} }
Err(err) => return Some(Err(err)), Err(err) => return Some(Err(err)),
} }
@@ -185,7 +195,9 @@ impl<'client, T: Config> TransactionProgress<'client, T> {
/// may well indicate with some probability that the transaction will not make it into a block, /// may well indicate with some probability that the transaction will not make it into a block,
/// there is no guarantee that this is true. Thus, we prefer to "play it safe" here. Use the lower /// there is no guarantee that this is true. Thus, we prefer to "play it safe" here. Use the lower
/// level [`TransactionProgress::next()`] API if you'd like to handle these statuses yourself. /// level [`TransactionProgress::next()`] API if you'd like to handle these statuses yourself.
pub async fn wait_for_finalized_success(self) -> Option<Result<TransactionEvents<T>, Error>> { pub async fn wait_for_finalized_success(
self,
) -> Option<Result<TransactionEvents<T>, Error>> {
let finalized = match self.wait_for_finalized().await? { let finalized = match self.wait_for_finalized().await? {
Ok(f) => f, Ok(f) => f,
Err(err) => return Some(Err(err)), Err(err) => return Some(Err(err)),
+1 -1
View File
@@ -11,6 +11,6 @@ codec = { package = "parity-scale-codec", version = "2", default-features = fals
[build-dependencies] [build-dependencies]
subxt = { path = ".." } subxt = { path = ".." }
sp-core = { package = "sp-core", git = "https://github.com/paritytech/substrate/", branch = "master" } sp-core = { package = "sp-core", git = "https://github.com/paritytech/substrate/", branch = "master" }
jsonrpsee-http-client = { git = "https://github.com/paritytech/jsonrpsee/", branch = "extract-async-client" } jsonrpsee = { git = "https://github.com/paritytech/jsonrpsee/", branch = "na-fix-core-client-feature", features = ["http-client"] }
async-std = { version = "1.9.0", features = ["attributes", "tokio1"] } async-std = { version = "1.9.0", features = ["attributes", "tokio1"] }
which = "4.2.2" which = "4.2.2"
+3 -3
View File
@@ -14,9 +14,9 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with subxt. If not, see <http://www.gnu.org/licenses/>. // along with subxt. If not, see <http://www.gnu.org/licenses/>.
use jsonrpsee_http_client::{ use jsonrpsee::{
types::traits::Client, core::client::ClientT,
HttpClientBuilder, http_client::HttpClientBuilder,
}; };
use std::{ use std::{
env, env,