diff --git a/subxt/src/storage/storage_type.rs b/subxt/src/storage/storage_type.rs index d072c1e40b..fc8caeef0c 100644 --- a/subxt/src/storage/storage_type.rs +++ b/subxt/src/storage/storage_type.rs @@ -255,7 +255,7 @@ where let key_bytes = kv.key; let cursor = &mut &key_bytes[..]; - strip_storage_addess_root_bytes(cursor)?; + strip_storage_address_root_bytes(cursor)?; let keys = ::decode_storage_key( cursor, @@ -314,10 +314,10 @@ where } } -/// Strips the first 16 bytes (8 for the pallet hash, 8 for the entry hash) off some storage address bytes. -fn strip_storage_addess_root_bytes(address_bytes: &mut &[u8]) -> Result<(), StorageAddressError> { - if address_bytes.len() >= 16 { - *address_bytes = &address_bytes[16..]; +/// Strips the first 32 bytes (16 for the pallet hash, 16 for the entry hash) off some storage address bytes. +fn strip_storage_address_root_bytes(address_bytes: &mut &[u8]) -> Result<(), StorageAddressError> { + if address_bytes.len() >= 32 { + *address_bytes = &address_bytes[32..]; Ok(()) } else { Err(StorageAddressError::UnexpectedAddressBytes) diff --git a/testing/integration-tests/src/full_client/storage/mod.rs b/testing/integration-tests/src/full_client/storage/mod.rs index 1d7dcd50d4..f10b5a6e48 100644 --- a/testing/integration-tests/src/full_client/storage/mod.rs +++ b/testing/integration-tests/src/full_client/storage/mod.rs @@ -228,3 +228,54 @@ async fn storage_pallet_storage_version() -> Result<(), subxt::Error> { .await?; Ok(()) } + +#[subxt_test] +async fn storage_iter_decode_keys() -> Result<(), subxt::Error> { + use futures::StreamExt; + + let ctx = test_context().await; + let api = ctx.client(); + + let storage_static = node_runtime::storage().system().account_iter(); + let results_static = api + .storage() + .at_latest() + .await? + .iter(storage_static) + .await?; + + let storage_dynamic = subxt::dynamic::storage("System", "Account", vec![]); + let results_dynamic = api + .storage() + .at_latest() + .await? + .iter(storage_dynamic) + .await?; + + // Even the testing node should have more than 3 accounts registered. + let results_static = results_static.take(3).collect::>().await; + let results_dynamic = results_dynamic.take(3).collect::>().await; + + assert_eq!(results_static.len(), 3); + assert_eq!(results_dynamic.len(), 3); + + let twox_system = sp_core::twox_128("System".as_bytes()); + let twox_account = sp_core::twox_128("Account".as_bytes()); + + for (static_kv, dynamic_kv) in results_static.into_iter().zip(results_dynamic.into_iter()) { + let static_kv = static_kv?; + let dynamic_kv = dynamic_kv?; + + // We only care about the underlying key bytes. + assert_eq!(static_kv.key_bytes, dynamic_kv.key_bytes); + + let bytes = static_kv.key_bytes; + assert!(bytes.len() > 32); + + // The first 16 bytes should be the twox hash of "System" and the next 16 bytes should be the twox hash of "Account". + assert_eq!(&bytes[..16], &twox_system[..]); + assert_eq!(&bytes[16..32], &twox_account[..]); + } + + Ok(()) +}