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
No related branches found
No related tags found
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
// Have we already published a result for this period?
if let Some(results) = published_results {
// Find the account associated with the BABE key that is in our owner keys.
let Some((_key, local_account)) = client
.storage_pairs(
parent,
Some(&StorageKey(
frame_support::storage::storage_prefix(b"Session", b"KeyOwner").to_vec(),
)),
None,
)?
.filter_map(|(raw_key, raw_data)| {
if &raw_key.0[40..44] == b"babe" {
Some((
sp_core::sr25519::Public::from_raw(
raw_key.0[45..45 + 32].try_into().unwrap(),
),
AccountId32::decode(&mut &raw_data.0[..]).ok()?,
))
let mut local_account = None;
for key in owner_keys {
// Session::KeyOwner is StorageMap<_, Twox64Concat, (KeyTypeId, Vec<u8>), AccountId32, OptionQuery>
// Slices (variable length) and array (fixed length) are encoded differently, so the `.as_slice()` is needed
let item_key = (sp_runtime::KeyTypeId(*b"babe"), key.0.as_slice()).encode();
let mut storage_key =
frame_support::storage::storage_prefix(b"Session", b"KeyOwner").to_vec();
storage_key.extend_from_slice(&sp_core::twox_64(&item_key));
storage_key.extend_from_slice(&item_key);
if let Some(raw_data) = client.storage(parent, &StorageKey(storage_key))? {
if let Ok(key_owner) = AccountId32::decode(&mut &raw_data.0[..]) {
local_account = Some(key_owner);
break;
} else {
None
log::warn!("🧙 [distance oracle] Cannot decode key owner value");
}
})
.find(|(key, _data)| owner_keys.contains(key))
else {
log::error!("🧙 [distance oracle] Cannot find our BABE owner key");
return Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(None));
};
}
}
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");
return Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(None));
}
}
// 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