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 fa70b03
Show file tree
Hide file tree
Showing 19 changed files with 234 additions and 222 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)

68 changes: 15 additions & 53 deletions avail-rust/docs/book/src/examples/data_submission.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use std::str::FromStr;

use avail_rust::{
avail,
block::{Block, DataSubmission},
error::ClientError,
Keypair,
Nonce::BestBlockAndTxPool,
Options, SDK,
Options, SecretUri, SDK,
};

type DataSubmissionCall = avail::data_availability::calls::types::SubmitData;
Expand All @@ -13,60 +16,19 @@ pub async fn run() -> Result<(), ClientError> {
let sdk = SDK::new(SDK::local_endpoint()).await?;
let online_client = &sdk.online_client;

let account = SDK::alice()?;

// Application Key Creation
let key = String::from("My Key").into_bytes();
let options = Some(Options::new().nonce(BestBlockAndTxPool));
let tx = sdk.tx.data_availability.create_application_key(key);
let res = tx.execute_wait_for_inclusion(&account, options).await?;

let Some(event) = res.find_first_event::<ApplicationKeyCreatedEvent>() else {
return Ok(());
};
let app_id = event.id.0;
let secret_uri = SecretUri::from_str("//Alice")?;
let account = Keypair::from_uri(&secret_uri)?;

// Data Submission
let data = String::from("My Data").into_bytes();
let options = Some(Options::new().nonce(BestBlockAndTxPool).app_id(app_id));
let tx = sdk.tx.data_availability.submit_data(data);
let res = tx.execute_wait_for_inclusion(&account, options).await?;

println!(
"Block Hash: {:?}, Block Number: {}, Tx Hash: {:?}, Tx Index: {}",
res.block_hash, res.block_number, res.tx_hash, res.tx_index
);

let Some(call_data) = res.get_data::<DataSubmissionCall>(online_client).await else {
return Ok(());
};
println!("Call data: {:?}", call_data.data);

// Getting Data Submission from Block #1
let block = Block::new(online_client, res.block_hash).await?;

// data_submissions_by_signer, data_submissions_by_index, data_submissions_by_hash, data_submissions_by_app_id
let data_submissions = block.data_submissions_all();
for ds in data_submissions {
println!(
"Tx Hash: {:?}, Tx Index: {}, Data {:?}, Tx Signer: {:?}, App Id: {}",
ds.tx_hash, ds.tx_index, ds.data, ds.tx_signer, ds.app_id
);

println!("Ascii data: {}", ds.to_ascii().expect("qed"));
}

// Getting Data Submission from Block #2
for tx in block.transaction_all_static::<DataSubmissionCall>() {
println!("Call data: {:?}", tx.value.data);

let ds = DataSubmission::from_static(tx);
println!(
"Tx Hash: {:?}, Tx Index: {}, Data {:?}, Tx Signer: {:?}, App Id: {}",
ds.tx_hash, ds.tx_index, ds.data, ds.tx_signer, ds.app_id
);

println!("Ascii data: {}", ds.to_ascii().expect("qed"));
let options = Some(Options::new().nonce(BestBlockAndTxPool).app_id(1));

loop {
let block = Block::new_best_block(&sdk.online_client, &sdk.rpc_client).await?;
let current_block_number = block.block.number();
let data = std::format!("Marko1 Inclusion {}", current_block_number);
let tx = sdk.tx.data_availability.submit_data(data.into_bytes());
_ = tx.execute_wait_for_inclusion(&account, options).await?;
println!("")
}

Ok(())
Expand Down
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 fa70b03

Please sign in to comment.