pop-fork 0.13.0

Library for forking live Substrate chains.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
// SPDX-License-Identifier: GPL-3.0

//! Legacy state_* RPC methods.
//!
//! These methods provide state-related operations for polkadot.js compatibility.

use crate::{
	Blockchain, BlockchainEvent,
	rpc_server::{
		RpcServerError, parse_block_hash, parse_hex_bytes,
		types::{HexString, RuntimeVersion, StorageChangeSet},
	},
	strings::rpc_server::{runtime_api, storage},
};
use jsonrpsee::{
	PendingSubscriptionSink,
	core::{RpcResult, SubscriptionResult},
	proc_macros::rpc,
};
use log::{debug, warn};
use scale::Decode;
use std::sync::Arc;
use tokio::sync::broadcast;
use tokio_util::sync::CancellationToken;

/// Legacy state RPC methods.
#[rpc(server, namespace = "state")]
pub trait StateApi {
	/// Get storage value at a key.
	///
	/// Returns the hex-encoded storage value at the given key, or null if no value exists.
	#[method(name = "getStorage")]
	async fn get_storage(&self, key: String, at: Option<String>) -> RpcResult<Option<String>>;

	/// Get the runtime metadata.
	///
	/// Returns the hex-encoded runtime metadata.
	#[method(name = "getMetadata")]
	async fn get_metadata(&self, at: Option<String>) -> RpcResult<String>;

	/// Get the runtime version.
	#[method(name = "getRuntimeVersion")]
	async fn get_runtime_version(&self, at: Option<String>) -> RpcResult<RuntimeVersion>;

	/// Get storage keys with pagination.
	#[method(name = "getKeysPaged")]
	async fn get_keys_paged(
		&self,
		prefix: Option<String>,
		count: u32,
		start_key: Option<String>,
		at: Option<String>,
	) -> RpcResult<Vec<String>>;

	/// Call a runtime API method.
	///
	/// Returns the hex-encoded result of the runtime call.
	#[method(name = "call")]
	async fn call(&self, method: String, data: String, at: Option<String>) -> RpcResult<String>;

	/// Subscribe to runtime version changes.
	#[subscription(name = "subscribeRuntimeVersion" => "runtimeVersion", unsubscribe = "unsubscribeRuntimeVersion", item = RuntimeVersion)]
	async fn subscribe_runtime_version(&self) -> SubscriptionResult;

	/// Subscribe to storage changes.
	#[subscription(name = "subscribeStorage" => "storage", unsubscribe = "unsubscribeStorage", item = StorageChangeSet)]
	async fn subscribe_storage(&self, keys: Option<Vec<String>>) -> SubscriptionResult;

	/// Query storage values at a specific block.
	///
	/// Returns a list of storage change sets with the values for each key.
	#[method(name = "queryStorageAt")]
	async fn query_storage_at(
		&self,
		keys: Vec<String>,
		at: Option<String>,
	) -> RpcResult<Vec<StorageChangeSet>>;
}

/// Implementation of legacy state RPC methods.
pub struct StateApi {
	blockchain: Arc<Blockchain>,
	shutdown_token: CancellationToken,
}

impl StateApi {
	/// Create a new StateApi instance.
	pub fn new(blockchain: Arc<Blockchain>, shutdown_token: CancellationToken) -> Self {
		Self { blockchain, shutdown_token }
	}
}

#[async_trait::async_trait]
impl StateApiServer for StateApi {
	async fn get_storage(&self, key: String, at: Option<String>) -> RpcResult<Option<String>> {
		let block_number = match at {
			Some(hash) => {
				let block_hash = parse_block_hash(&hash)?;
				self.blockchain
					.block_number_by_hash(block_hash)
					.await
					.map_err(|_| {
						RpcServerError::InvalidParam(format!("Invalid block hash: {}", hash))
					})?
					.ok_or_else(|| {
						RpcServerError::InvalidParam(format!("Invalid block hash: {}", hash))
					})?
			},
			None => self.blockchain.head_number().await,
		};
		let key_bytes = parse_hex_bytes(&key, "key")?;

		// Query storage from blockchain
		match self.blockchain.storage_at(block_number, &key_bytes).await {
			Ok(Some(value)) => Ok(Some(HexString::from_bytes(&value).into())),
			Ok(None) => Ok(None),
			Err(e) => Err(RpcServerError::Storage(e.to_string()).into()),
		}
	}

	async fn get_metadata(&self, at: Option<String>) -> RpcResult<String> {
		let block_hash = match at {
			Some(hash) => parse_block_hash(&hash)?,
			None => self.blockchain.head().await.hash,
		};
		// Fetch real metadata via runtime call
		// The runtime returns SCALE-encoded Vec<u8>, so we need to decode it
		// to strip the compact length prefix
		match self.blockchain.call_at_block(block_hash, runtime_api::METADATA, &[]).await {
			Ok(Some(result)) => {
				// Decode SCALE Vec<u8> to get raw metadata bytes
				let metadata_bytes: Vec<u8> = Decode::decode(&mut &result[..]).map_err(|e| {
					RpcServerError::Internal(format!("Failed to decode metadata: {}", e))
				})?;
				Ok(HexString::from_bytes(&metadata_bytes).into())
			},
			_ => Err(RpcServerError::Internal("Failed to get metadata".to_string()).into()),
		}
	}

	async fn get_runtime_version(&self, at: Option<String>) -> RpcResult<RuntimeVersion> {
		let (block_number, block_hash) = match at {
			Some(ref hash) => {
				let parsed = parse_block_hash(hash)?;
				let num = self
					.blockchain
					.block_number_by_hash(parsed)
					.await
					.map_err(|_| {
						RpcServerError::InvalidParam(format!("Invalid block hash: {hash}"))
					})?
					.ok_or_else(|| {
						RpcServerError::InvalidParam(format!("Block not found: {hash}"))
					})?;
				(num, parsed)
			},
			None => {
				let head = self.blockchain.head().await;
				(head.number, head.hash)
			},
		};

		// For blocks at or before the fork point, proxy Core_version to the upstream
		// node. Its JIT-compiled runtime is orders of magnitude faster than the local
		// WASM interpreter.
		let result = if block_number <= self.blockchain.fork_point_number() {
			self.blockchain
				.proxy_state_call(runtime_api::CORE_VERSION, &[], block_hash)
				.await
				.ok()
		} else {
			None
		};

		// Fall back to local WASM execution for fork-local blocks or proxy failure.
		let result = match result {
			Some(r) => r,
			None => self
				.blockchain
				.call_at_block(block_hash, runtime_api::CORE_VERSION, &[])
				.await
				.map_err(|e| {
					RpcServerError::Internal(format!("Failed to get runtime version: {e}"))
				})?
				.ok_or_else(|| {
					RpcServerError::Internal("Failed to get runtime version".to_string())
				})?,
		};

		// Decode the SCALE-encoded RuntimeVersion.
		#[derive(Decode)]
		struct ScaleRuntimeVersion {
			spec_name: String,
			impl_name: String,
			authoring_version: u32,
			spec_version: u32,
			impl_version: u32,
			apis: Vec<([u8; 8], u32)>,
			transaction_version: u32,
			state_version: u8,
		}
		let version = ScaleRuntimeVersion::decode(&mut result.as_slice()).map_err(|e| {
			RpcServerError::Internal(format!("Failed to decode runtime version: {e}"))
		})?;

		Ok(RuntimeVersion {
			spec_name: version.spec_name,
			impl_name: version.impl_name,
			authoring_version: version.authoring_version,
			spec_version: version.spec_version,
			impl_version: version.impl_version,
			transaction_version: version.transaction_version,
			state_version: version.state_version,
			apis: version
				.apis
				.into_iter()
				.map(|(id, ver)| (HexString::from_bytes(&id).into(), ver))
				.collect(),
		})
	}

	async fn get_keys_paged(
		&self,
		prefix: Option<String>,
		count: u32,
		start_key: Option<String>,
		at: Option<String>,
	) -> RpcResult<Vec<String>> {
		let prefix_bytes = match prefix {
			Some(ref p) => parse_hex_bytes(p, "prefix")?,
			None => vec![],
		};
		let start_key_bytes = match start_key {
			Some(ref k) => Some(parse_hex_bytes(k, "start_key")?),
			None => None,
		};
		let block_hash = match at {
			Some(ref hash) => Some(parse_block_hash(hash)?),
			None => None,
		};

		jsonrpsee::tracing::debug!(
			prefix = ?prefix,
			count = count,
			start_key = ?start_key,
			at = ?at,
			"state_getKeysPaged: querying storage keys"
		);

		let keys = self
			.blockchain
			.storage_keys_paged(&prefix_bytes, count, start_key_bytes.as_deref(), block_hash)
			.await
			.map_err(|e| RpcServerError::Storage(e.to_string()))?;

		jsonrpsee::tracing::debug!(
			keys_returned = keys.len(),
			"state_getKeysPaged: returning keys"
		);

		Ok(keys.into_iter().map(|k| HexString::from_bytes(&k).into()).collect())
	}

	async fn call(&self, method: String, data: String, at: Option<String>) -> RpcResult<String> {
		let params = parse_hex_bytes(&data, "data")?;

		let (block_number, block_hash) = match at {
			Some(ref hash) => {
				let parsed = parse_block_hash(hash)?;
				let num = self
					.blockchain
					.block_number_by_hash(parsed)
					.await
					.map_err(|_| {
						RpcServerError::InvalidParam(format!("Invalid block hash: {hash}"))
					})?
					.ok_or_else(|| {
						RpcServerError::InvalidParam(format!("Block not found: {hash}"))
					})?;
				(num, parsed)
			},
			None => {
				let head = self.blockchain.head().await;
				(head.number, head.hash)
			},
		};

		// Proxy metadata runtime API calls to the upstream RPC for performance,
		// but only for blocks at or before the fork point where the runtime is
		// guaranteed to match the upstream. Fork-local blocks may have a different
		// runtime due to upgrades.
		if method.starts_with(runtime_api::METADATA_PREFIX) &&
			block_number <= self.blockchain.fork_point_number()
		{
			match self.blockchain.proxy_state_call(&method, &params, block_hash).await {
				Ok(result) => return Ok(HexString::from_bytes(&result).into()),
				Err(e) => {
					jsonrpsee::tracing::debug!(
						"Upstream proxy failed for {method}, falling back to local execution: {e}"
					);
				},
			}
		}

		match self.blockchain.call_at_block(block_hash, &method, &params).await {
			Ok(Some(result)) => Ok(HexString::from_bytes(&result).into()),
			Ok(None) => Err(RpcServerError::Internal("Call returned no result".to_string()).into()),
			Err(e) => Err(RpcServerError::Internal(format!("Runtime call failed: {}", e)).into()),
		}
	}

	async fn query_storage_at(
		&self,
		keys: Vec<String>,
		at: Option<String>,
	) -> RpcResult<Vec<StorageChangeSet>> {
		// Resolve block
		let (block_number, block_hash) = match at {
			Some(ref hash) => {
				let parsed = parse_block_hash(hash)?;
				let num = self
					.blockchain
					.block_number_by_hash(parsed)
					.await
					.map_err(|_| {
						RpcServerError::InvalidParam(format!("Invalid block hash: {}", hash))
					})?
					.ok_or_else(|| {
						RpcServerError::InvalidParam(format!("Block not found: {}", hash))
					})?;
				(num, parsed)
			},
			None => {
				let head = self.blockchain.head().await;
				(head.number, head.hash)
			},
		};

		jsonrpsee::tracing::debug!(
			num_keys = keys.len(),
			block_number = block_number,
			"state_queryStorageAt: fetching {} keys in parallel",
			keys.len()
		);

		// Parse all keys upfront
		let parsed_keys: Vec<(String, Vec<u8>)> = keys
			.into_iter()
			.map(|key| {
				let bytes = parse_hex_bytes(&key, "key")?;
				Ok((key, bytes))
			})
			.collect::<Result<Vec<_>, jsonrpsee::types::ErrorObjectOwned>>()?;

		// For blocks at or before the fork point, batch-fetch from the upstream in a
		// single RPC call. This is orders of magnitude faster than per-key fetching.
		let changes: Vec<_> = if block_number <= self.blockchain.fork_point_number() {
			let key_refs: Vec<&[u8]> = parsed_keys.iter().map(|(_, k)| k.as_slice()).collect();
			let values = self
				.blockchain
				.storage_batch(block_hash, &key_refs)
				.await
				.map_err(|e| RpcServerError::Storage(e.to_string()))?;

			parsed_keys
				.into_iter()
				.zip(values)
				.map(|((key, _), value)| (key, value.map(|v| HexString::from_bytes(&v).into())))
				.collect()
		} else {
			// For fork-local blocks, query each key through the local storage layer
			let futures: Vec<_> = parsed_keys
				.iter()
				.map(|(_, key_bytes)| self.blockchain.storage_at(block_number, key_bytes))
				.collect();
			let results = futures::future::join_all(futures).await;

			parsed_keys
				.into_iter()
				.zip(results)
				.map(|((key, _), result)| {
					let value = match result {
						Ok(Some(v)) => Some(HexString::from_bytes(&v).into()),
						_ => None,
					};
					(key, value)
				})
				.collect()
		};

		// Return as single change set for the queried block
		Ok(vec![StorageChangeSet {
			block: HexString::from_bytes(block_hash.as_bytes()).into(),
			changes,
		}])
	}

	async fn subscribe_runtime_version(
		&self,
		pending: PendingSubscriptionSink,
	) -> SubscriptionResult {
		let sink = pending.accept().await?;
		let blockchain = Arc::clone(&self.blockchain);
		let token = self.shutdown_token.clone();

		// Get current runtime version and send it immediately
		let current_version = self.get_runtime_version(None).await.ok();
		if let Some(ref version) = current_version {
			let _ = sink.send(jsonrpsee::SubscriptionMessage::from_json(version)?).await;
		}

		// Subscribe to blockchain events to detect runtime upgrades
		let mut receiver = blockchain.subscribe_events();

		// The `:code` storage key indicates a runtime upgrade
		let code_key = storage::RUNTIME_CODE_KEY.to_vec();

		tokio::spawn(async move {
			let mut last_spec_version = current_version.map(|v| v.spec_version);

			loop {
				tokio::select! {
					biased;

					_ = token.cancelled() => break,

					_ = sink.closed() => break,

					event = receiver.recv() => {
						match event {
							Ok(BlockchainEvent::NewBlock { modified_keys, hash, .. }) => {
								// Check if :code was modified (runtime upgrade)
								let runtime_changed = modified_keys.contains(&code_key);

								if runtime_changed {
									// Fetch new runtime version
									if let Ok(Some(result)) = blockchain.call_at_block(hash, runtime_api::CORE_VERSION, &[]).await {
										#[derive(Decode)]
										struct ScaleRuntimeVersion {
											spec_name: String,
											impl_name: String,
											authoring_version: u32,
											spec_version: u32,
											impl_version: u32,
											apis: Vec<([u8; 8], u32)>,
											transaction_version: u32,
											state_version: u8,
										}

										if let Ok(version) = ScaleRuntimeVersion::decode(&mut result.as_slice()) {
											// Only notify if spec_version changed
											if last_spec_version != Some(version.spec_version) {
												last_spec_version = Some(version.spec_version);

												let rt_version = RuntimeVersion {
													spec_name: version.spec_name,
													impl_name: version.impl_name,
													authoring_version: version.authoring_version,
													spec_version: version.spec_version,
													impl_version: version.impl_version,
													transaction_version: version.transaction_version,
													state_version: version.state_version,
													apis: version.apis.into_iter()
														.map(|(id, ver)| (format!("0x{}", hex::encode(id)), ver))
														.collect(),
												};

												let msg = match jsonrpsee::SubscriptionMessage::from_json(&rt_version) {
													Ok(m) => m,
													Err(_) => continue,
												};
												if sink.send(msg).await.is_err() {
													break;
												}
											}
										}
									}
								}
							}
							Err(broadcast::error::RecvError::Lagged(_)) => continue,
							Err(broadcast::error::RecvError::Closed) => break,
						}
					}
				}
			}
		});

		Ok(())
	}

	async fn subscribe_storage(
		&self,
		pending: PendingSubscriptionSink,
		keys: Option<Vec<String>>,
	) -> SubscriptionResult {
		let sink = pending.accept().await?;
		let blockchain = Arc::clone(&self.blockchain);

		// Parse subscribed keys from hex to bytes
		let subscribed_keys: Vec<Vec<u8>> = keys
			.clone()
			.unwrap_or_default()
			.iter()
			.filter_map(|k| hex::decode(k.trim_start_matches("0x")).ok())
			.collect();

		// Also keep original hex keys for response formatting
		let subscribed_keys_hex: Vec<String> = keys.unwrap_or_default();

		debug!("[state] Storage subscription accepted for {} keys", subscribed_keys.len());

		// Send initial values
		let head_hash = blockchain.head_hash().await;
		let block_hex = format!("0x{}", hex::encode(head_hash.as_bytes()));

		let mut changes = Vec::new();
		for (i, key_bytes) in subscribed_keys.iter().enumerate() {
			let value = blockchain
				.storage(key_bytes)
				.await
				.ok()
				.flatten()
				.map(|v| format!("0x{}", hex::encode(v)));
			let key_hex = subscribed_keys_hex.get(i).cloned().unwrap_or_default();
			changes.push((key_hex, value));
		}

		let change_set = StorageChangeSet { block: block_hex, changes };
		let _ = sink.send(jsonrpsee::SubscriptionMessage::from_json(&change_set)?).await;

		// If no keys to watch, just wait for close
		if subscribed_keys.is_empty() {
			sink.closed().await;
			return Ok(());
		}

		// Subscribe to blockchain events
		let mut receiver = blockchain.subscribe_events();
		let token = self.shutdown_token.clone();

		tokio::spawn(async move {
			loop {
				tokio::select! {
					biased;

					_ = token.cancelled() => break,

					_ = sink.closed() => {
						debug!("[state] Storage subscriber disconnected");
						break;
					},

					event = receiver.recv() => {
						match event {
							Ok(BlockchainEvent::NewBlock { number, hash, modified_keys, .. }) => {
								// Filter to keys we're watching that were modified
								let affected_indices: Vec<usize> = subscribed_keys.iter()
									.enumerate()
									.filter(|(_, k)| modified_keys.iter().any(|mk| mk == *k))
									.map(|(i, _)| i)
									.collect();

								debug!(
									"[state] Block #{number}: {}/{} subscribed keys affected ({} modified total)",
									affected_indices.len(),
									subscribed_keys.len(),
									modified_keys.len()
								);

								if affected_indices.is_empty() {
									continue;
								}

								// Fetch new values for affected keys
								let block_hex = format!("0x{}", hex::encode(hash.as_bytes()));
								let mut changes = Vec::new();

								for i in affected_indices {
									let key_hex = subscribed_keys_hex.get(i).cloned().unwrap_or_default();
									let key_bytes = &subscribed_keys[i];
									let value = blockchain.storage(key_bytes).await.ok().flatten()
										.map(|v| format!("0x{}", hex::encode(v)));
									changes.push((key_hex, value));
								}

								let change_set = StorageChangeSet { block: block_hex, changes };
								let msg = match jsonrpsee::SubscriptionMessage::from_json(&change_set) {
									Ok(m) => m,
									Err(e) => {
										warn!("[state] Failed to serialize storage change for #{number}: {e}");
										continue;
									},
								};
								if sink.send(msg).await.is_err() {
									debug!("[state] Storage subscriber disconnected during send");
									break;
								}
							}
							Err(broadcast::error::RecvError::Lagged(n)) => {
								warn!("[state] Storage subscriber lagged, skipped {n} events");
								continue;
							}
							Err(broadcast::error::RecvError::Closed) => {
								debug!("[state] Broadcast channel closed");
								break;
							}
						}
					}
				}
			}
		});

		Ok(())
	}
}

#[cfg(test)]
mod tests {

	use crate::{
		rpc_server::types::RuntimeVersion,
		testing::{TestContext, accounts::ALICE, helpers::account_storage_key},
	};
	use jsonrpsee::{core::client::ClientT, rpc_params, ws_client::WsClientBuilder};

	#[tokio::test(flavor = "multi_thread")]
	async fn state_get_storage_returns_value() {
		let ctx = TestContext::for_rpc_server().await;
		let client = WsClientBuilder::default()
			.build(&ctx.ws_url())
			.await
			.expect("Failed to connect");

		// Query Alice's account storage (should exist on dev chain)
		let key = account_storage_key(&ALICE);
		let key_hex = format!("0x{}", hex::encode(&key));

		let result: Option<String> = client
			.request("state_getStorage", rpc_params![key_hex])
			.await
			.expect("RPC call failed");

		assert!(result.is_some(), "Alice's account should exist");
		let value = result.unwrap();
		assert!(value.starts_with("0x"), "Value should be hex encoded");
		assert!(value.len() > 2, "Value should not be empty");
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn state_get_storage_at_block_hash() {
		let ctx = TestContext::for_rpc_server().await;
		let client = WsClientBuilder::default()
			.build(&ctx.ws_url())
			.await
			.expect("Failed to connect");

		let block = ctx.blockchain().build_empty_block().await.unwrap();
		ctx.blockchain().build_empty_block().await.unwrap();

		// Get current head hash
		let block_hash_hex = format!("0x{}", hex::encode(block.hash.as_bytes()));

		// Query Alice's account storage at specific block
		let key = account_storage_key(&ALICE);
		let key_hex = format!("0x{}", hex::encode(&key));

		let result: Option<String> = client
			.request("state_getStorage", rpc_params![key_hex, block_hash_hex])
			.await
			.expect("RPC call failed");

		assert!(result.is_some(), "Alice's account should exist at block");
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn state_get_storage_returns_none_for_nonexistent_key() {
		let ctx = TestContext::for_rpc_server().await;
		let client = WsClientBuilder::default()
			.build(&ctx.ws_url())
			.await
			.expect("Failed to connect");

		// Query a nonexistent storage key
		let fake_key = "0x0000000000000000000000000000000000000000000000000000000000000000";

		let result: Option<String> = client
			.request("state_getStorage", rpc_params![fake_key])
			.await
			.expect("RPC call failed");

		assert!(result.is_none(), "Nonexistent key should return None");
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn state_get_metadata_returns_metadata() {
		let ctx = TestContext::for_rpc_server().await;
		let client = WsClientBuilder::default()
			.request_timeout(std::time::Duration::from_secs(120))
			.build(&ctx.ws_url())
			.await
			.expect("Failed to connect");

		let result: String = client
			.request("state_getMetadata", rpc_params![])
			.await
			.expect("RPC call failed");

		assert!(result.starts_with("0x"), "Metadata should be hex encoded");
		// Metadata is large, just check it's substantial
		assert!(result.len() > 1000, "Metadata should be substantial in size");
		// Verify metadata magic number "meta" (0x6d657461) is at the start
		assert!(
			result.starts_with("0x6d657461"),
			"Metadata should start with magic number 'meta' (0x6d657461), got: {}",
			&result[..20.min(result.len())]
		);
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn state_get_metadata_at_block_hash() {
		let ctx = TestContext::for_rpc_server().await;
		let client = WsClientBuilder::default()
			.request_timeout(std::time::Duration::from_secs(120))
			.build(&ctx.ws_url())
			.await
			.expect("Failed to connect");

		// Get current head hash
		let head_hash = ctx.blockchain().head_hash().await;
		let head_hash_hex = format!("0x{}", hex::encode(head_hash.as_bytes()));

		let result: String = client
			.request("state_getMetadata", rpc_params![head_hash_hex])
			.await
			.expect("RPC call failed");

		assert!(result.starts_with("0x"), "Metadata should be hex encoded");
		assert!(result.len() > 1000, "Metadata should be substantial in size");
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn state_get_runtime_version_returns_version() {
		let ctx = TestContext::for_rpc_server().await;
		let client = WsClientBuilder::default()
			.build(&ctx.ws_url())
			.await
			.expect("Failed to connect");

		let result: RuntimeVersion = client
			.request("state_getRuntimeVersion", rpc_params![])
			.await
			.expect("RPC call failed");

		// Verify we got a valid runtime version
		assert!(!result.spec_name.is_empty(), "Spec name should not be empty");
		assert!(!result.impl_name.is_empty(), "Impl name should not be empty");
		assert!(result.spec_version > 0, "Spec version should be positive");
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn state_get_runtime_version_at_block_hash() {
		let ctx = TestContext::for_rpc_server().await;
		let client = WsClientBuilder::default()
			.build(&ctx.ws_url())
			.await
			.expect("Failed to connect");

		// Get current head hash
		let head_hash = ctx.blockchain().head_hash().await;
		let head_hash_hex = format!("0x{}", hex::encode(head_hash.as_bytes()));

		let result: RuntimeVersion = client
			.request("state_getRuntimeVersion", rpc_params![head_hash_hex])
			.await
			.expect("RPC call failed");

		assert!(!result.spec_name.is_empty(), "Spec name should not be empty");
		assert!(result.spec_version > 0, "Spec version should be positive");
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn state_get_storage_invalid_hex_returns_error() {
		let ctx = TestContext::for_rpc_server().await;
		let client = WsClientBuilder::default()
			.build(&ctx.ws_url())
			.await
			.expect("Failed to connect");

		let result: Result<Option<String>, _> =
			client.request("state_getStorage", rpc_params!["not_valid_hex"]).await;

		assert!(result.is_err(), "Should fail with invalid hex key");
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn state_get_storage_invalid_block_hash_returns_error() {
		let ctx = TestContext::for_rpc_server().await;
		let client = WsClientBuilder::default()
			.build(&ctx.ws_url())
			.await
			.expect("Failed to connect");

		let key = account_storage_key(&ALICE);
		let key_hex = format!("0x{}", hex::encode(&key));
		let invalid_hash = "0x0000000000000000000000000000000000000000000000000000000000000000";

		let result: Result<Option<String>, _> =
			client.request("state_getStorage", rpc_params![key_hex, invalid_hash]).await;

		assert!(result.is_err(), "Should fail with invalid block hash");
	}
}