storage: Fix partial key storage iteration (#1298)

* storage/fix: Use partial key instead of root key for iter

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* storage: Allow partial key construction

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* storage: Allow less provided types than num of hashes

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* storage: Error on more fields than types

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* testing: Check partial key iteration

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* storage: Rename variable wrt partial address bytes

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* storage: Identical storage key to root key if no keys are provided

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
This commit is contained in:
Alexandru Vasile
2023-12-01 15:33:33 +02:00
committed by GitHub
parent ea8735f863
commit b03679fcd9
4 changed files with 89 additions and 6 deletions
+10 -3
View File
@@ -172,6 +172,12 @@ where
.resolve(*key_ty)
.ok_or(MetadataError::TypeNotFound(*key_ty))?;
// If the provided keys are empty, the storage address must be
// equal to the storage root address.
if self.storage_entry_keys.is_empty() {
return Ok(());
}
// If the key is a tuple, we encode each value to the corresponding tuple type.
// If the key is not a tuple, encode a single value to the key type.
let type_ids = match &ty.type_def {
@@ -181,7 +187,8 @@ where
_other => either::Either::Right(std::iter::once(*key_ty)),
};
if type_ids.len() != self.storage_entry_keys.len() {
if type_ids.len() < self.storage_entry_keys.len() {
// Provided more keys than fields.
return Err(StorageAddressError::WrongNumberOfKeys {
expected: type_ids.len(),
actual: self.storage_entry_keys.len(),
@@ -198,7 +205,7 @@ where
}
hash_bytes(&input, &hashers[0], bytes);
Ok(())
} else if hashers.len() == type_ids.len() {
} else if hashers.len() >= type_ids.len() {
let iter = self.storage_entry_keys.iter().zip(type_ids).zip(hashers);
// A hasher per field; encode and hash each field independently.
for ((key, type_id), hasher) in iter {
@@ -208,7 +215,7 @@ where
}
Ok(())
} else {
// Mismatch; wrong number of hashers/fields.
// Provided more fields than hashers.
Err(StorageAddressError::WrongNumberOfHashers {
hashers: hashers.len(),
fields: type_ids.len(),
+3 -3
View File
@@ -228,12 +228,12 @@ where
// in the iterator.
let return_type_id = return_type_from_storage_entry_type(entry.entry_type());
// The root pallet/entry bytes for this storage entry:
let address_root_bytes = super::utils::storage_address_root_bytes(&address);
// The address bytes of this entry:
let address_bytes = super::utils::storage_address_bytes(&address, &metadata)?;
let s = client
.backend()
.storage_fetch_descendant_values(address_root_bytes, block_ref.hash())
.storage_fetch_descendant_values(address_bytes, block_ref.hash())
.await?
.map(move |kv| {
let kv = match kv {