Skip to content
Snippets Groups Projects
Commit b270ecb9 authored by bgallois's avatar bgallois Committed by Hugo Trentesaux
Browse files

inherent data provider cannot fail

parent f4f23dfd
No related branches found
No related tags found
1 merge request!294Fix #264: Distance inherent data provider should not fail
Pipeline #39248 skipped
...@@ -59,37 +59,35 @@ pub fn create_distance_inherent_data_provider<B, C, Backend>( ...@@ -59,37 +59,35 @@ pub fn create_distance_inherent_data_provider<B, C, Backend>(
parent: B::Hash, parent: B::Hash,
distance_dir: PathBuf, distance_dir: PathBuf,
owner_keys: &[sp_core::sr25519::Public], owner_keys: &[sp_core::sr25519::Public],
) -> Result<sp_distance::InherentDataProvider<IdtyIndex>, sc_client_api::blockchain::Error> ) -> sp_distance::InherentDataProvider<IdtyIndex>
where where
B: BlockT, B: BlockT,
C: ProvideUncles<B> + StorageProvider<B, Backend>, C: ProvideUncles<B> + StorageProvider<B, Backend>,
Backend: sc_client_api::Backend<B>, Backend: sc_client_api::Backend<B>,
IdtyIndex: Decode + Encode + PartialEq + TypeInfo, IdtyIndex: Decode + Encode + PartialEq + TypeInfo,
{ {
// Retrieve the period_index from storage. If storage is inaccessible or the data is corrupted, // Retrieve the period_index from storage.
// return the appropriate error.
let period_index = client let period_index = client
.storage( .storage(
parent, parent,
&StorageKey( &StorageKey(
frame_support::storage::storage_prefix(b"Distance", b"CurrentPeriodIndex").to_vec(), frame_support::storage::storage_prefix(b"Distance", b"CurrentPeriodIndex").to_vec(),
), ),
)? )
.map_or_else( .ok()
|| { .flatten()
Err(sc_client_api::blockchain::Error::Storage( .and_then(|raw| u32::decode(&mut &raw.0[..]).ok());
"CurrentPeriodIndex value not found".to_string(),
)) // Return early if the storage is inaccessible or the data is corrupted.
}, let period_index = match period_index {
|raw| { Some(index) => index,
u32::decode(&mut &raw.0[..]) None => {
.map_err(|e| sc_client_api::blockchain::Error::from_state(Box::new(e))) log::error!("🧙 [distance inherent] PeriodIndex decoding failed.");
}, return sp_distance::InherentDataProvider::<IdtyIndex>::new(None);
)?; }
};
// Retrieve the published_results from storage. // Retrieve the published_results from storage.
// Return an error if the storage is inaccessible.
// If accessible, continue execution. If None, it means there are no published_results at this block.
let published_results = client let published_results = client
.storage( .storage(
parent, parent,
...@@ -105,13 +103,22 @@ where ...@@ -105,13 +103,22 @@ where
) )
.to_vec(), .to_vec(),
), ),
)? )
.ok()
.flatten()
.and_then(|raw| { .and_then(|raw| {
pallet_distance::EvaluationPool::<AccountId32, IdtyIndex>::decode(&mut &raw.0[..]).ok() pallet_distance::EvaluationPool::<AccountId32, IdtyIndex>::decode(&mut &raw.0[..]).ok()
}); });
// Have we already published a result for this period? // Return early if the storage is inaccessible or the data is corrupted.
if let Some(results) = published_results { let published_results = match published_results {
Some(published_results) => published_results,
None => {
log::info!("🧙 [distance inherent] No published result at this block.");
return sp_distance::InherentDataProvider::<IdtyIndex>::new(None);
}
};
// 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 mut local_account = None; let mut local_account = None;
for key in owner_keys { for key in owner_keys {
...@@ -123,29 +130,34 @@ where ...@@ -123,29 +130,34 @@ where
storage_key.extend_from_slice(&sp_core::twox_64(&item_key)); storage_key.extend_from_slice(&sp_core::twox_64(&item_key));
storage_key.extend_from_slice(&item_key); storage_key.extend_from_slice(&item_key);
if let Some(raw_data) = client.storage(parent, &StorageKey(storage_key))? { if let Some(raw_data) = client
.storage(parent, &StorageKey(storage_key))
.ok()
.flatten()
{
if let Ok(key_owner) = AccountId32::decode(&mut &raw_data.0[..]) { if let Ok(key_owner) = AccountId32::decode(&mut &raw_data.0[..]) {
local_account = Some(key_owner); local_account = Some(key_owner);
break; break;
} else { } else {
log::warn!("🧙 [distance oracle] Cannot decode key owner value"); log::warn!("🧙 [distance inherent] Cannot decode key owner value");
} }
} }
} }
// Have we already published a result for this period?
if let Some(local_account) = local_account { if let Some(local_account) = local_account {
if results.evaluators.contains(&local_account) { if published_results.evaluators.contains(&local_account) {
log::debug!("🧙 [distance oracle] Already published a result for this period"); log::debug!("🧙 [distance inherent] Already published a result for this period");
return Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(None)); return sp_distance::InherentDataProvider::<IdtyIndex>::new(None);
} }
} else { } else {
log::error!("🧙 [distance oracle] Cannot find our BABE owner key"); log::error!("🧙 [distance inherent] Cannot find our BABE owner key");
return Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(None)); return sp_distance::InherentDataProvider::<IdtyIndex>::new(None);
}
} }
// Read evaluation result from file, if it exists // Read evaluation result from file, if it exists
log::debug!( log::debug!(
"🧙 [distance oracle] Reading evaluation result from file {:?}", "🧙 [distance inherent] Reading evaluation result from file {:?}",
distance_dir.clone().join(period_index.to_string()) distance_dir.clone().join(period_index.to_string())
); );
let evaluation_result = match std::fs::read( let evaluation_result = match std::fs::read(
...@@ -155,20 +167,20 @@ where ...@@ -155,20 +167,20 @@ where
Err(e) => { Err(e) => {
match e.kind() { match e.kind() {
std::io::ErrorKind::NotFound => { std::io::ErrorKind::NotFound => {
log::debug!("🧙 [distance oracle] Evaluation result file not found. Please ensure that the oracle version matches {}", VERSION_PREFIX); log::debug!("🧙 [distance inherent] Evaluation result file not found. Please ensure that the oracle version matches {}", VERSION_PREFIX);
} }
_ => { _ => {
log::error!( log::error!(
"🧙 [distance oracle] Cannot read distance evaluation result file: {e:?}" "🧙 [distance inherent] Cannot read distance evaluation result file: {e:?}"
); );
} }
} }
return Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(None)); return sp_distance::InherentDataProvider::<IdtyIndex>::new(None);
} }
}; };
log::info!("🧙 [distance oracle] Providing evaluation result"); log::info!("🧙 [distance inherent] Providing evaluation result");
Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(Some( sp_distance::InherentDataProvider::<IdtyIndex>::new(Some(
sp_distance::ComputationResult::decode(&mut evaluation_result.as_slice()).unwrap(), sp_distance::ComputationResult::decode(&mut evaluation_result.as_slice()).unwrap(),
))) ))
} }
...@@ -503,7 +503,7 @@ where ...@@ -503,7 +503,7 @@ where
FullBackend, FullBackend,
>( >(
&*client, parent, distance_dir, &babe_owner_keys.clone() &*client, parent, distance_dir, &babe_owner_keys.clone()
)?; );
Ok((timestamp, babe, distance)) Ok((timestamp, babe, distance))
} }
}, },
...@@ -549,7 +549,7 @@ where ...@@ -549,7 +549,7 @@ where
FullBackend, FullBackend,
>( >(
&*client, parent, distance_dir, &babe_owner_keys.clone() &*client, parent, distance_dir, &babe_owner_keys.clone()
)?; );
Ok((slot, timestamp, storage_proof, distance)) Ok((slot, timestamp, storage_proof, distance))
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment