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

Avoid decoding storage by hand

parent 5a5552e1
Branches
Tags
No related merge requests found
This commit is part of merge request !291. Comments created here will be created in the context of that merge request.
...@@ -87,35 +87,34 @@ where ...@@ -87,35 +87,34 @@ 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 {
log::error!("🧙 [distance oracle] Cannot find our BABE owner key");
return Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(None));
};
if results.evaluators.contains(&local_account) { if results.evaluators.contains(&local_account) {
log::debug!("🧙 [distance oracle] Already published a result for this period"); log::debug!("🧙 [distance oracle] Already published a result for this period");
return Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(None)); return Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(None));
} }
} else {
log::error!("🧙 [distance oracle] Cannot find our BABE owner key");
return Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(None));
}
} }
// Read evaluation result from file, if it exists, then remove it // Read evaluation result from file, if it exists, then remove it
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment