Skip to main content

quantus_cli/chain/
client.rs

1//! Common client utilities to eliminate code duplication
2//!
3//! This module provides shared functionality for creating and managing clients
4//! across all CLI modules.
5
6use crate::{error::QuantusError, log_verbose};
7use jsonrpsee::ws_client::{WsClient, WsClientBuilder};
8use qp_dilithium_crypto::types::DilithiumSignatureScheme;
9use qp_poseidon::PoseidonHasher;
10use sp_core::{crypto::AccountId32, ByteArray};
11use sp_runtime::{traits::IdentifyAccount, MultiAddress};
12use std::{sync::Arc, time::Duration};
13use subxt::{
14	backend::rpc::RpcClient,
15	config::{substrate::SubstrateHeader, DefaultExtrinsicParams},
16	Config, OnlineClient,
17};
18use subxt_metadata::Metadata as SubxtMetadata;
19
20#[derive(Debug, Clone, Copy)]
21pub struct SubxtPoseidonHasher;
22
23impl subxt::config::Hasher for SubxtPoseidonHasher {
24	type Output = sp_core::H256;
25
26	fn new(_metadata: &SubxtMetadata) -> Self {
27		SubxtPoseidonHasher
28	}
29
30	fn hash(&self, bytes: &[u8]) -> Self::Output {
31		<PoseidonHasher as sp_runtime::traits::Hash>::hash(bytes)
32	}
33}
34
35/// Configuration of the chain
36pub enum ChainConfig {}
37impl Config for ChainConfig {
38	type AccountId = AccountId32;
39	type Address = MultiAddress<Self::AccountId, ()>;
40	type Signature = DilithiumSignatureScheme;
41	type Hasher = SubxtPoseidonHasher;
42	type Header = SubstrateHeader<u32, SubxtPoseidonHasher>;
43	type AssetId = u32;
44	type ExtrinsicParams = DefaultExtrinsicParams<Self>;
45}
46
47/// Wrapper around OnlineClient that also stores the node URL and RPC client
48#[derive(Clone)]
49pub struct QuantusClient {
50	client: OnlineClient<ChainConfig>,
51	rpc_client: Arc<WsClient>,
52	node_url: String,
53}
54
55impl QuantusClient {
56	/// Create a new QuantusClient by connecting to the specified node URL
57	pub async fn new(node_url: &str) -> crate::error::Result<Self> {
58		log_verbose!("🔗 Connecting to Quantus node: {}", node_url);
59
60		// Validate URL format and provide helpful error messages
61		if !node_url.starts_with("ws://") && !node_url.starts_with("wss://") {
62			return Err(QuantusError::NetworkError(format!(
63                "Invalid WebSocket URL: '{node_url}'. URL must start with 'ws://' (unsecured) or 'wss://' (secured)"
64            )));
65		}
66
67		// Create WS client with custom timeouts
68		let ws_client = WsClientBuilder::default()
69            // TODO: Make these configurable in a separate change
70            // These timeouts should be configurable via CLI or config file
71            .connection_timeout(Duration::from_secs(30))
72            .request_timeout(Duration::from_secs(30))
73            .build(node_url)
74            .await
75            .map_err(|e| {
76                // Provide more helpful error messages for common issues
77                let error_str = format!("{e:?}");
78                let error_msg = if error_str.contains("TimedOut") || error_str.contains("timed out") {
79                    if node_url.starts_with("ws://") {
80                        format!(
81                            "Connection timed out. Try using 'wss://{}' instead of '{}'",
82                            node_url.strip_prefix("ws://").unwrap_or(node_url),
83                            node_url
84                        )
85                    } else {
86                        format!("Connection timed out. Please check if the node is running and accessible at: {node_url}")
87                    }
88                } else if error_str.contains("HTTP") {
89                    format!("HTTP error: {error_str}. This might indicate the node doesn't support WebSocket connections")
90                } else {
91                    format!("Failed to create RPC client: {error_str}")
92                };
93                QuantusError::NetworkError(error_msg)
94            })?;
95
96		// Wrap WS client in Arc for sharing
97		let ws_client = Arc::new(ws_client);
98
99		// Create RPC client wrapper for subxt
100		let rpc_client = RpcClient::new(ws_client.clone());
101
102		// Create SubXT client using the configured RPC client
103		let client = OnlineClient::<ChainConfig>::from_rpc_client(rpc_client).await?;
104
105		log_verbose!("✅ Connected to Quantus node successfully!");
106
107		Ok(QuantusClient { client, rpc_client: ws_client, node_url: node_url.to_string() })
108	}
109
110	/// Get reference to the underlying SubXT client
111	pub fn client(&self) -> &OnlineClient<ChainConfig> {
112		&self.client
113	}
114
115	/// Get the node URL
116	pub fn node_url(&self) -> &str {
117		&self.node_url
118	}
119
120	/// Get reference to the RPC client
121	pub fn rpc_client(&self) -> &WsClient {
122		&self.rpc_client
123	}
124
125	/// Get the latest block (best block) using RPC call
126	/// This bypasses SubXT's default behavior of using finalized blocks
127	pub async fn get_latest_block(&self) -> crate::error::Result<subxt::utils::H256> {
128		log_verbose!("🔍 Fetching latest block hash via RPC...");
129
130		// Use RPC call to get the latest block hash
131		use jsonrpsee::core::client::ClientT;
132		let latest_hash: subxt::utils::H256 = self
133			.rpc_client
134			.request::<subxt::utils::H256, [(); 0]>("chain_getBlockHash", [])
135			.await
136			.map_err(|e| {
137				crate::error::QuantusError::NetworkError(format!(
138					"Failed to fetch latest block hash: {e:?}"
139				))
140			})?;
141
142		log_verbose!("📦 Latest block hash: {:?}", latest_hash);
143		Ok(latest_hash)
144	}
145
146	/// Get account nonce from the best block (latest) using direct RPC call
147	/// This bypasses SubXT's default behavior of using finalized blocks
148	pub async fn get_account_nonce_from_best_block(
149		&self,
150		account_id: &AccountId32,
151	) -> crate::error::Result<u64> {
152		log_verbose!("🔍 Fetching account nonce from best block via RPC...");
153
154		// Get latest block hash first
155		let latest_block_hash = self.get_latest_block().await?;
156		log_verbose!("📦 Latest block hash for nonce query: {:?}", latest_block_hash);
157
158		// Convert sp_core::AccountId32 to subxt::utils::AccountId32
159		let account_bytes: [u8; 32] = *account_id.as_ref();
160		let subxt_account_id = subxt::utils::AccountId32::from(account_bytes);
161
162		// Use SubXT's storage API to query nonce at the best block
163		use crate::chain::quantus_subxt::api;
164		let storage_addr = api::storage().system().account(subxt_account_id);
165
166		let storage_at = self.client.storage().at(latest_block_hash);
167
168		let account_info = storage_at.fetch_or_default(&storage_addr).await?;
169
170		log_verbose!("✅ Nonce from best block: {}", account_info.nonce);
171		Ok(account_info.nonce as u64)
172	}
173
174	/// Get genesis hash using RPC call
175	pub async fn get_genesis_hash(&self) -> crate::error::Result<subxt::utils::H256> {
176		log_verbose!("🔍 Fetching genesis hash via RPC...");
177
178		use jsonrpsee::core::client::ClientT;
179		let genesis_hash: subxt::utils::H256 = self
180			.rpc_client
181			.request::<subxt::utils::H256, [u32; 1]>("chain_getBlockHash", [0u32])
182			.await
183			.map_err(|e| {
184				crate::error::QuantusError::NetworkError(format!(
185					"Failed to fetch genesis hash: {e:?}"
186				))
187			})?;
188
189		log_verbose!("🧬 Genesis hash: {:?}", genesis_hash);
190		Ok(genesis_hash)
191	}
192
193	/// Get runtime version using RPC call
194	pub async fn get_runtime_version(&self) -> crate::error::Result<(u32, u32)> {
195		log_verbose!("🔍 Fetching runtime version via RPC...");
196
197		use jsonrpsee::core::client::ClientT;
198		let runtime_version: serde_json::Value = self
199			.rpc_client
200			.request::<serde_json::Value, [(); 0]>("state_getRuntimeVersion", [])
201			.await
202			.map_err(|e| {
203				crate::error::QuantusError::NetworkError(format!(
204					"Failed to fetch runtime version: {e:?}"
205				))
206			})?;
207
208		let spec_version = runtime_version["specVersion"].as_u64().ok_or_else(|| {
209			crate::error::QuantusError::NetworkError("Failed to parse spec version".to_string())
210		})? as u32;
211
212		let transaction_version =
213			runtime_version["transactionVersion"].as_u64().ok_or_else(|| {
214				crate::error::QuantusError::NetworkError(
215					"Failed to parse transaction version".to_string(),
216				)
217			})? as u32;
218
219		log_verbose!("🔧 Runtime version: spec={}, tx={}", spec_version, transaction_version);
220		Ok((spec_version, transaction_version))
221	}
222
223	/// Get runtime hash using RPC call (if available)
224	pub async fn get_runtime_hash(&self) -> crate::error::Result<Option<String>> {
225		log_verbose!("🔍 Fetching runtime hash via RPC...");
226
227		use jsonrpsee::core::client::ClientT;
228
229		// Try different possible RPC calls for runtime hash
230		let possible_calls = ["state_getRuntimeHash", "state_getRuntime", "chain_getRuntimeHash"];
231
232		for call_name in &possible_calls {
233			match self.rpc_client.request::<serde_json::Value, [(); 0]>(call_name, []).await {
234				Ok(result) => {
235					log_verbose!("✅ Found runtime hash via {}", call_name);
236					if let Some(hash) = result.as_str() {
237						return Ok(Some(hash.to_string()));
238					} else if let Some(hash_obj) = result.get("hash") {
239						if let Some(hash) = hash_obj.as_str() {
240							return Ok(Some(hash.to_string()));
241						}
242					}
243				},
244				Err(_e) => {
245					log_verbose!("❌ {} failed: {:?}", call_name, _e);
246				},
247			}
248		}
249
250		log_verbose!("⚠️  No runtime hash RPC call available");
251		Ok(None)
252	}
253}
254
255// Implement subxt::tx::Signer for ResonancePair
256impl subxt::tx::Signer<ChainConfig> for qp_dilithium_crypto::types::DilithiumPair {
257	fn account_id(&self) -> <ChainConfig as Config>::AccountId {
258		let resonance_public =
259			qp_dilithium_crypto::types::DilithiumPublic::from_slice(self.public.as_slice())
260				.expect("Invalid public key");
261		<qp_dilithium_crypto::types::DilithiumPublic as IdentifyAccount>::into_account(
262			resonance_public,
263		)
264	}
265
266	fn sign(&self, signer_payload: &[u8]) -> <ChainConfig as Config>::Signature {
267		// Use the sign method from the trait implemented for ResonancePair
268		// sp_core::Pair::sign returns ResonanceSignatureWithPublic, which we need to wrap in
269		// ResonanceSignatureScheme
270		let signature_with_public =
271			<qp_dilithium_crypto::types::DilithiumPair as sp_core::Pair>::sign(
272				self,
273				signer_payload,
274			);
275		qp_dilithium_crypto::types::DilithiumSignatureScheme::Dilithium(signature_with_public)
276	}
277}