Skip to content

Commit

Permalink
Enabled RPC recconection
Browse files Browse the repository at this point in the history
  • Loading branch information
markopoloparadox committed Dec 2, 2024
1 parent 6574a27 commit 5c6ab41
Show file tree
Hide file tree
Showing 18 changed files with 219 additions and 169 deletions.
6 changes: 3 additions & 3 deletions avail-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ crate-type = ["cdylib", "rlib"]
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
derive_more = { version = "1", features = ["full"] }
kate-recovery = { git = "https://github.com/availproject/avail-core", tag = "core-node-4", features = ["serde"] }
subxt = { version = "0.38.0" }
subxt = { version = "0.38.0", features = ["reconnecting-rpc-client"] }
subxt-core = { version = "0.38.0" }
subxt-signer = { version = "0.38.0" }
tokio = { version = "1.21.2" }

[target.'cfg(target_arch = "wasm32")'.dependencies]
derive_more = { version = "1", default-features = false, features = ["from", "constructor"] }
kate-recovery = { git = "https://github.com/availproject/avail-core", default-features = false, tag = "core-node-4", features = ["serde"] }
subxt = { version = "0.38.0", default-features = false, features = ["web", "jsonrpsee"] }
subxt = { version = "0.38.0", default-features = false, features = ["web", "jsonrpsee", "reconnecting-rpc-client"] }
subxt-core = { version = "0.38.0", default-features = false }
subxt-signer = { version = "0.38.0", default-features = false, features = ["web", "sr25519", "subxt"] }
tokio = { version = "1.21.2", default-features = false }
sp-io = { version = "30", default-features = false, features = [ "disable_panic_handler" ] }

[dependencies]
serde = { version = "1.0.195", features = ["derive", ] }
serde_json = { version = "1.0.124" }
serde_json = { version = "1.0.124", features = ["raw_value"] }
codec = { package = "parity-scale-codec", version = "3", default-features = false, features = [
"derive",
"full",
Expand Down
1 change: 0 additions & 1 deletion avail-rust/docs/book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@
- [Events](./examples/events.md)
- [Transactions](./examples/transactions.md)
- [Validator](./examples/validator.md)
- [Insecure Connection](./examples/insecure_connection.md)
- [Batch](./examples/batch.md)

5 changes: 0 additions & 5 deletions avail-rust/docs/book/src/examples/insecure_connection.md

This file was deleted.

6 changes: 0 additions & 6 deletions avail-rust/docs/book/src/examples/insecure_connection.rs

This file was deleted.

2 changes: 0 additions & 2 deletions avail-rust/docs/book/src/examples/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
mod batch;
mod data_submission;
mod events;
mod insecure_connection;
mod transactions;
mod validator;

Expand All @@ -10,7 +9,6 @@ use avail_rust::error::ClientError;
pub async fn run() -> Result<(), ClientError> {
data_submission::run().await?;
events::run().await?;
insecure_connection::run().await?;
transactions::run().await?;
validator::run().await?;
batch::run().await?;
Expand Down
5 changes: 4 additions & 1 deletion avail-rust/src/account.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::str::FromStr;
use subxt::{backend::rpc::RpcClient, blocks::StaticExtrinsic, ext::scale_encode::EncodeAsFields};
use subxt::{
backend::rpc::reconnecting_rpc_client::RpcClient, blocks::StaticExtrinsic,
ext::scale_encode::EncodeAsFields,
};

use crate::{
avail,
Expand Down
14 changes: 7 additions & 7 deletions avail-rust/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
};

use primitive_types::H256;
use subxt::backend::rpc::RpcClient;
use subxt::backend::rpc::reconnecting_rpc_client::RpcClient;
use subxt::backend::StreamOfResults;
use subxt::blocks::StaticExtrinsic;
use subxt::storage::StorageKeyValuePair;
Expand All @@ -21,7 +21,7 @@ pub struct Block {
}

impl Block {
pub async fn new(client: &AOnlineClient, block_hash: H256) -> Result<Self, subxt::Error> {
pub async fn new(client: &AOnlineClient, block_hash: H256) -> Result<Self, ClientError> {
let (block, transactions) = fetch_transactions(client, block_hash).await?;
Ok(Self {
block,
Expand All @@ -32,15 +32,15 @@ impl Block {
pub async fn new_best_block(
online_client: &AOnlineClient,
rpc_client: &RpcClient,
) -> Result<Self, subxt::Error> {
) -> Result<Self, ClientError> {
let block_hash = Self::fetch_best_block_hash(rpc_client).await?;
Self::new(online_client, block_hash).await
}

pub async fn new_finalized_block(
online_client: &AOnlineClient,
rpc_client: &RpcClient,
) -> Result<Self, subxt::Error> {
) -> Result<Self, ClientError> {
let block_hash = Self::fetch_finalized_block_hash(rpc_client).await?;
Self::new(online_client, block_hash).await
}
Expand All @@ -57,7 +57,7 @@ impl Block {
online_client: &AOnlineClient,
rpc_client: &RpcClient,
block_number: u32,
) -> Result<Self, subxt::Error> {
) -> Result<Self, ClientError> {
let block_hash = rpcs::get_block_hash(rpc_client, Some(block_number)).await?;
Self::new(online_client, block_hash).await
}
Expand Down Expand Up @@ -176,11 +176,11 @@ impl Block {
self.block.storage().iter(address).await
}

pub async fn fetch_best_block_hash(client: &RpcClient) -> Result<H256, subxt::Error> {
pub async fn fetch_best_block_hash(client: &RpcClient) -> Result<H256, ClientError> {
rpcs::get_block_hash(client, None).await
}

pub async fn fetch_finalized_block_hash(client: &RpcClient) -> Result<H256, subxt::Error> {
pub async fn fetch_finalized_block_hash(client: &RpcClient) -> Result<H256, ClientError> {
rpcs::get_finalized_head(client).await
}
}
Expand Down
18 changes: 18 additions & 0 deletions avail-rust/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ use subxt_signer::SecretUriError;
use crate::transactions::TransactionFailed;
use crate::utils::TransactionExecutionError;

type RpcError = subxt::backend::rpc::reconnecting_rpc_client::Error;

#[derive(Debug)]
pub enum ClientError {
Custom(String),
TransactionExecution(TransactionExecutionError),
RpcError(RpcError),
SerdeJson(serde_json::Error),
Subxt(subxt::Error),
SubxtSigner(SecretUriError),
Sr25519(sr25519::Error),
Expand All @@ -19,6 +23,8 @@ impl ClientError {
match self {
ClientError::Custom(e) => e.clone(),
ClientError::TransactionExecution(e) => e.to_string(),
ClientError::RpcError(e) => e.to_string(),
ClientError::SerdeJson(e) => e.to_string(),
ClientError::Subxt(e) => e.to_string(),
ClientError::SubxtSigner(e) => e.to_string(),
ClientError::Sr25519(e) => e.to_string(),
Expand Down Expand Up @@ -67,3 +73,15 @@ impl From<TransactionFailed> for ClientError {
value.reason
}
}

impl From<RpcError> for ClientError {
fn from(value: RpcError) -> Self {
Self::RpcError(value)
}
}

impl From<serde_json::Error> for ClientError {
fn from(value: serde_json::Error) -> Self {
Self::SerdeJson(value)
}
}
Loading

0 comments on commit 5c6ab41

Please sign in to comment.