Skip to content

Commit

Permalink
add authorities and min_slot_freq fn to interface (#25)
Browse files Browse the repository at this point in the history
* add authorities and min_slot_freq fn to interface

* fix tests + clippy

* fmt
  • Loading branch information
nanocryk committed Jul 2, 2024
1 parent d18c151 commit 73dc2e3
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 80 deletions.
44 changes: 41 additions & 3 deletions client/orchestrator-chain-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
//!
//! prove_read: generates a storage proof of a given set of keys at a given Orchestrator parent
pub use dp_core::{Hash as PHash, Header as PHeader};
use {
core::pin::Pin, futures::Stream, polkadot_overseer::Handle, sc_client_api::StorageProof,
sp_api::ApiError, sp_state_machine::StorageValue, std::sync::Arc,
core::pin::Pin, dp_core::ParaId, futures::Stream, polkadot_overseer::Handle,
sc_client_api::StorageProof, sp_api::ApiError, sp_state_machine::StorageValue, std::sync::Arc,
};
pub use {
cumulus_primitives_core::relay_chain::Slot,
dp_core::{Hash as PHash, Header as PHeader},
};

#[derive(thiserror::Error, Debug)]
Expand Down Expand Up @@ -87,6 +90,8 @@ pub type OrchestratorChainResult<T> = Result<T, OrchestratorChainError>;
/// Trait that provides all necessary methods for interaction between collator and orchestrator chain.
#[async_trait::async_trait]
pub trait OrchestratorChainInterface: Send + Sync {
type AuthorityId;

/// Fetch a storage item by key.
async fn get_storage_by_key(
&self,
Expand Down Expand Up @@ -118,13 +123,30 @@ pub trait OrchestratorChainInterface: Send + Sync {
async fn finality_notification_stream(
&self,
) -> OrchestratorChainResult<Pin<Box<dyn Stream<Item = PHeader> + Send>>>;

/// Return the set of authorities assigned to the paraId where
/// the first eligible key from the keystore is collating
async fn authorities(
&self,
orchestrator_parent: PHash,
para_id: ParaId,
) -> OrchestratorChainResult<Option<Vec<Self::AuthorityId>>>;

/// Returns the minimum slot frequency for this para id.
async fn min_slot_freq(
&self,
orchestrator_parent: PHash,
para_id: ParaId,
) -> OrchestratorChainResult<Option<Slot>>;
}

#[async_trait::async_trait]
impl<T> OrchestratorChainInterface for Arc<T>
where
T: OrchestratorChainInterface + ?Sized,
{
type AuthorityId = T::AuthorityId;

fn overseer_handle(&self) -> OrchestratorChainResult<Handle> {
(**self).overseer_handle()
}
Expand Down Expand Up @@ -164,4 +186,20 @@ where
) -> OrchestratorChainResult<Pin<Box<dyn Stream<Item = PHeader> + Send>>> {
(**self).finality_notification_stream().await
}

async fn authorities(
&self,
orchestrator_parent: PHash,
para_id: ParaId,
) -> OrchestratorChainResult<Option<Vec<Self::AuthorityId>>> {
(**self).authorities(orchestrator_parent, para_id).await
}

async fn min_slot_freq(
&self,
orchestrator_parent: PHash,
para_id: ParaId,
) -> OrchestratorChainResult<Option<Slot>> {
(**self).min_slot_freq(orchestrator_parent, para_id).await
}
}
49 changes: 40 additions & 9 deletions client/orchestrator-chain-rpc-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ mod ws_client;

use {
async_trait::async_trait,
core::pin::Pin,
core::{marker::PhantomData, pin::Pin},
dc_orchestrator_chain_interface::{
OrchestratorChainError, OrchestratorChainInterface, OrchestratorChainResult, PHash, PHeader,
OrchestratorChainError, OrchestratorChainInterface, OrchestratorChainResult, PHash,
PHeader, Slot,
},
dp_core::ParaId,
futures::{Stream, StreamExt},
jsonrpsee::{core::params::ArrayParams, rpc_params},
sc_client_api::{StorageData, StorageProof},
Expand Down Expand Up @@ -60,11 +62,11 @@ fn url_to_string_with_port(url: Url) -> Option<String> {
))
}

pub async fn create_client_and_start_worker(
pub async fn create_client_and_start_worker<T>(
urls: Vec<Url>,
task_manager: &mut TaskManager,
overseer_handle: Option<polkadot_overseer::Handle>,
) -> OrchestratorChainResult<OrchestratorChainRpcClient> {
) -> OrchestratorChainResult<OrchestratorChainRpcClient<T>> {
let urls: Vec<_> = urls
.into_iter()
.filter_map(url_to_string_with_port)
Expand All @@ -84,18 +86,20 @@ pub async fn create_client_and_start_worker(
let client = OrchestratorChainRpcClient {
request_sender,
overseer_handle,
_phantom: PhantomData,
};

Ok(client)
}

#[derive(Clone)]
pub struct OrchestratorChainRpcClient {
pub struct OrchestratorChainRpcClient<T> {
request_sender: mpsc::Sender<WsClientRequest>,
overseer_handle: Option<polkadot_overseer::Handle>,
_phantom: PhantomData<T>,
}

impl OrchestratorChainRpcClient {
impl<T> OrchestratorChainRpcClient<T> {
/// Call a call to `state_call` rpc method.
pub async fn call_remote_runtime_function<R: Decode>(
&self,
Expand Down Expand Up @@ -216,7 +220,9 @@ impl OrchestratorChainRpcClient {
}

#[async_trait]
impl OrchestratorChainInterface for OrchestratorChainRpcClient {
impl<T: Sync + Send + Decode> OrchestratorChainInterface for OrchestratorChainRpcClient<T> {
type AuthorityId = T;

/// Fetch a storage item by key.
async fn get_storage_by_key(
&self,
Expand Down Expand Up @@ -245,8 +251,8 @@ impl OrchestratorChainInterface for OrchestratorChainRpcClient {
relevant_keys: &[Vec<u8>],
) -> OrchestratorChainResult<StorageProof> {
let mut cloned = Vec::new();
cloned.extend_from_slice(&relevant_keys);
let storage_keys: Vec<StorageKey> = cloned.into_iter().map(|key| StorageKey(key)).collect();
cloned.extend_from_slice(relevant_keys);
let storage_keys: Vec<StorageKey> = cloned.into_iter().map(StorageKey).collect();

self.state_get_read_proof(storage_keys, Some(orchestrator_parent))
.await
Expand Down Expand Up @@ -281,4 +287,29 @@ impl OrchestratorChainInterface for OrchestratorChainRpcClient {
let stream = tokio_stream::wrappers::ReceiverStream::new(rx);
Ok(stream.boxed())
}

/// Return the set of authorities assigned to the paraId where
/// the first eligible key from the keystore is collating
async fn authorities(
&self,
orchestrator_parent: PHash,
para_id: ParaId,
) -> OrchestratorChainResult<Option<Vec<Self::AuthorityId>>> {
self.call_remote_runtime_function("para_id_authorities", orchestrator_parent, Some(para_id))
.await
}

/// Returns the minimum slot frequency for this para id.
async fn min_slot_freq(
&self,
orchestrator_parent: PHash,
para_id: ParaId,
) -> OrchestratorChainResult<Option<Slot>> {
self.call_remote_runtime_function(
"parathread_slot_frequency",
orchestrator_parent,
Some(para_id),
)
.await
}
}
4 changes: 2 additions & 2 deletions container-chain-pallets/authorities-noting/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ mod test_sproof {
benchmarks! {
set_latest_authorities_data {
// TODO: this could measure the proof size
let sproof_builder_relay = test_sproof::ParaHeaderSproofBuilder::default();
let sproof_builder_orchestrator = test_sproof::AuthorityAssignmentSproofBuilder::default();
let sproof_builder_relay = test_sproof::ParaHeaderSproofBuilder;
let sproof_builder_orchestrator = test_sproof::AuthorityAssignmentSproofBuilder;

let (relay_root, relay_proof) = sproof_builder_relay.into_state_root_and_proof();
let (orchestrator_root, orchestrator_proof) = sproof_builder_orchestrator.into_state_root_and_proof();
Expand Down
2 changes: 1 addition & 1 deletion container-chain-pallets/authorities-noting/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn genesis_config_orchestrator_para_id() {
fn genesis_config_orchestrator_para_id_storage_update() {
new_test_ext().execute_with(|| {
let new_para_id = ParaId::new(2000);
OrchestratorParaId::<Test>::put(&new_para_id);
OrchestratorParaId::<Test>::put(new_para_id);
assert_eq!(OrchestratorParaId::<Test>::get(), new_para_id);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use {
InboundDownwardMessage, InboundHrmpMessage, ParaId, PersistedValidationData,
},
cumulus_relay_chain_interface::{PHash, PHeader, RelayChainInterface, RelayChainResult},
dc_orchestrator_chain_interface::{OrchestratorChainInterface, OrchestratorChainResult},
dc_orchestrator_chain_interface::{OrchestratorChainInterface, OrchestratorChainResult, Slot},
dp_core::{well_known_keys, Header as OrchestratorHeader},
futures::Stream,
polkadot_overseer::Handle,
Expand Down Expand Up @@ -84,6 +84,8 @@ impl DummyRelayChainInterface {

#[async_trait]
impl OrchestratorChainInterface for DummyOrchestratorChainInterface {
type AuthorityId = ();

fn overseer_handle(&self) -> OrchestratorChainResult<Handle> {
unimplemented!("Not needed for test")
}
Expand All @@ -94,7 +96,7 @@ impl OrchestratorChainInterface for DummyOrchestratorChainInterface {
key: &[u8],
) -> OrchestratorChainResult<Option<StorageValue>> {
self.orchestrator_client
.storage(hash.into(), &StorageKey(key.to_vec()))
.storage(hash, &StorageKey(key.to_vec()))
.map(|a| a.map(|b| b.0))
.map_err(|e| e.into())
}
Expand Down Expand Up @@ -128,6 +130,22 @@ impl OrchestratorChainInterface for DummyOrchestratorChainInterface {
) -> OrchestratorChainResult<Pin<Box<dyn Stream<Item = PHeader> + Send>>> {
unimplemented!("Not needed for test")
}

async fn authorities(
&self,
_orchestrator_parent: PHash,
_para_id: ParaId,
) -> OrchestratorChainResult<Option<Vec<Self::AuthorityId>>> {
unimplemented!("Not needed for test")
}

async fn min_slot_freq(
&self,
_orchestrator_parent: PHash,
_para_id: ParaId,
) -> OrchestratorChainResult<Option<Slot>> {
unimplemented!("Not needed for test")
}
}

#[async_trait]
Expand Down Expand Up @@ -218,7 +236,7 @@ impl RelayChainInterface for DummyRelayChainInterface {
) -> RelayChainResult<Option<StorageValue>> {
Ok(self
.relay_client
.storage(hash.into(), &StorageKey(key.to_vec()))
.storage(hash, &StorageKey(key.to_vec()))
.map(|a| a.map(|b| b.0))
.unwrap())
}
Expand Down
6 changes: 5 additions & 1 deletion pallets/xcm-executor-utils/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,9 @@ mod benchmarks {
Ok(())
}

impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test);
impl_benchmark_test_suite!(
Pallet,
crate::mock::mock_never::new_test_ext(),
crate::mock::mock_never::TestNever
);
}
60 changes: 24 additions & 36 deletions pallets/xcm-executor-utils/src/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,12 @@ mod test {
fun: Fungible(1_000),
};

assert_eq!(
apply_policy::<TestAllNative>(
&grandparent_asset,
&parent_location,
None,
<TestAllNative as Config>::ReserveDefaultTrustPolicy::get(),
),
false
);
assert!(!apply_policy::<TestAllNative>(
&grandparent_asset,
&parent_location,
None,
<TestAllNative as Config>::ReserveDefaultTrustPolicy::get(),
));
}

#[test]
Expand All @@ -193,15 +190,12 @@ mod test {
fun: Fungible(1_000),
};

assert_eq!(
apply_policy::<TestNever>(
&parent_asset,
&parent_location,
None,
<TestNever as Config>::ReserveDefaultTrustPolicy::get(),
),
false
);
assert!(!apply_policy::<TestNever>(
&parent_asset,
&parent_location,
None,
<TestNever as Config>::ReserveDefaultTrustPolicy::get(),
));
}

#[test]
Expand Down Expand Up @@ -260,15 +254,12 @@ mod test {
DefaultTrustPolicy::AllNative,
));

assert_eq!(
apply_policy::<TestNever>(
&grandparent_asset,
&parent_location,
origin_policy,
default_policy
),
false
);
assert!(!apply_policy::<TestNever>(
&grandparent_asset,
&parent_location,
origin_policy,
default_policy
));
}

#[test]
Expand Down Expand Up @@ -314,14 +305,11 @@ mod test {
));

// parent_asset should be rejected
assert_eq!(
apply_policy::<TestNever>(
&parent_asset,
&parent_location,
origin_policy,
default_policy
),
false
);
assert!(!apply_policy::<TestNever>(
&parent_asset,
&parent_location,
origin_policy,
default_policy
));
}
}
Loading

0 comments on commit 73dc2e3

Please sign in to comment.