-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into A0-3390-kozoun
- Loading branch information
Showing
7 changed files
with
151 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
use std::{ | ||
fmt::{Display, Formatter}, | ||
marker::PhantomData, | ||
sync::Arc, | ||
}; | ||
|
||
use aleph_runtime::SessionKeys; | ||
use parity_scale_codec::{Decode, DecodeAll, Error as DecodeError}; | ||
use sc_client_api::Backend; | ||
use sp_application_crypto::key_types::AURA; | ||
use sp_core::twox_128; | ||
use sp_runtime::traits::{Block, OpaqueKeys}; | ||
|
||
use crate::{ | ||
aleph_primitives::{AccountId, AlephSessionApi, AuraId}, | ||
BlockHash, ClientForAleph, | ||
}; | ||
|
||
/// Trait handling connection between host code and runtime storage | ||
pub trait RuntimeApi { | ||
type Error: Display; | ||
/// Returns aura authorities for the next session using state from block `at` | ||
fn next_aura_authorities(&self, at: BlockHash) -> Result<Vec<AuraId>, Self::Error>; | ||
} | ||
|
||
type QueuedKeys = Vec<(AccountId, SessionKeys)>; | ||
|
||
#[derive(Clone)] | ||
pub struct RuntimeApiImpl<C, B, BE> | ||
where | ||
C: ClientForAleph<B, BE> + Send + Sync + 'static, | ||
C::Api: AlephSessionApi<B>, | ||
B: Block<Hash = BlockHash>, | ||
BE: Backend<B> + 'static, | ||
{ | ||
client: Arc<C>, | ||
_phantom: PhantomData<(B, BE)>, | ||
} | ||
|
||
impl<C, B, BE> RuntimeApiImpl<C, B, BE> | ||
where | ||
C: ClientForAleph<B, BE> + Send + Sync + 'static, | ||
C::Api: AlephSessionApi<B>, | ||
B: Block<Hash = BlockHash>, | ||
BE: Backend<B> + 'static, | ||
{ | ||
pub fn new(client: Arc<C>) -> Self { | ||
Self { | ||
client, | ||
_phantom: PhantomData, | ||
} | ||
} | ||
|
||
fn read_storage<D: Decode>( | ||
&self, | ||
pallet: &str, | ||
item: &str, | ||
at_block: BlockHash, | ||
) -> Result<D, ApiError> { | ||
let storage_key = [twox_128(pallet.as_bytes()), twox_128(item.as_bytes())].concat(); | ||
|
||
let encoded = match self | ||
.client | ||
.storage(at_block, &sc_client_api::StorageKey(storage_key)) | ||
{ | ||
Ok(Some(e)) => e, | ||
_ => return Err(ApiError::NoStorage(pallet.to_string(), item.to_string())), | ||
}; | ||
|
||
D::decode_all(&mut encoded.0.as_ref()).map_err(ApiError::DecodeError) | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub enum ApiError { | ||
NoStorage(String, String), | ||
DecodeError(DecodeError), | ||
} | ||
|
||
impl Display for ApiError { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { | ||
match self { | ||
ApiError::NoStorage(pallet, item) => write!(f, "no storage under {}.{}", pallet, item), | ||
ApiError::DecodeError(error) => write!(f, "decode error: {:?}", error), | ||
} | ||
} | ||
} | ||
|
||
impl<C, B, BE> RuntimeApi for RuntimeApiImpl<C, B, BE> | ||
where | ||
C: ClientForAleph<B, BE> + Send + Sync + 'static, | ||
C::Api: AlephSessionApi<B>, | ||
B: Block<Hash = BlockHash>, | ||
BE: Backend<B> + 'static, | ||
{ | ||
type Error = ApiError; | ||
|
||
fn next_aura_authorities(&self, at: BlockHash) -> Result<Vec<AuraId>, Self::Error> { | ||
if let Ok(authorities) = self.client.runtime_api().next_session_aura_authorities(at) { | ||
return Ok(authorities); | ||
} | ||
|
||
let queued_keys: QueuedKeys = self.read_storage("Session", "QueuedKeys", at)?; | ||
|
||
Ok(queued_keys | ||
.into_iter() | ||
.filter_map(|(_, keys)| keys.get(AURA)) | ||
.collect()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters