Skip to content
Snippets Groups Projects
Commit 407b1d15 authored by Pascal Engélibert's avatar Pascal Engélibert :bicyclist:
Browse files

Avoid decoding storage by hand

parent 4f0f3825
No related branches found
No related tags found
1 merge request!291Fix distance evaluation client
Pipeline #38679 waiting for manual action
...@@ -87,34 +87,33 @@ where ...@@ -87,34 +87,33 @@ where
// Have we already published a result for this period? // Have we already published a result for this period?
if let Some(results) = published_results { if let Some(results) = published_results {
// Find the account associated with the BABE key that is in our owner keys. // Find the account associated with the BABE key that is in our owner keys.
let Some((_key, local_account)) = client let mut local_account = None;
.storage_pairs( for key in owner_keys {
parent, // Session::KeyOwner is StorageMap<_, Twox64Concat, (KeyTypeId, Vec<u8>), AccountId32, OptionQuery>
Some(&StorageKey( // Slices (variable length) and array (fixed length) are encoded differently, so the `.as_slice()` is needed
frame_support::storage::storage_prefix(b"Session", b"KeyOwner").to_vec(), let item_key = (sp_runtime::KeyTypeId(*b"babe"), key.0.as_slice()).encode();
)), let mut storage_key =
None, frame_support::storage::storage_prefix(b"Session", b"KeyOwner").to_vec();
)? storage_key.extend_from_slice(&sp_core::twox_64(&item_key));
.filter_map(|(raw_key, raw_data)| { storage_key.extend_from_slice(&item_key);
if &raw_key.0[40..44] == b"babe" {
Some(( if let Some(raw_data) = client.storage(parent, &StorageKey(storage_key))? {
sp_core::sr25519::Public::from_raw( if let Ok(key_owner) = AccountId32::decode(&mut &raw_data.0[..]) {
raw_key.0[45..45 + 32].try_into().unwrap(), local_account = Some(key_owner);
), break;
AccountId32::decode(&mut &raw_data.0[..]).ok()?,
))
} else { } else {
None log::warn!("🧙 [distance oracle] Cannot decode key owner value");
} }
}) }
.find(|(key, _data)| owner_keys.contains(key)) }
else { if let Some(local_account) = local_account {
if results.evaluators.contains(&local_account) {
log::debug!("🧙 [distance oracle] Already published a result for this period");
return Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(None));
}
} else {
log::error!("🧙 [distance oracle] Cannot find our BABE owner key"); log::error!("🧙 [distance oracle] Cannot find our BABE owner key");
return Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(None)); return Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(None));
};
if results.evaluators.contains(&local_account) {
log::debug!("🧙 [distance oracle] Already published a result for this period");
return Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(None));
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment