Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update types for metadata v5 #608

Merged
merged 3 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions essentials/src/api/api_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl<T: OnlineClientT<PolkadotConfig>> ApiClientT for ApiClient<T> {
block_hash: H256,
para_id: u32,
) -> Result<BTreeMap<u32, SubxtHrmpChannel>, subxt::Error> {
use polkadot::runtime_types::polkadot_parachain::primitives::{HrmpChannelId, Id};
use polkadot::runtime_types::polkadot_parachain_primitives::primitives::{HrmpChannelId, Id};
let addr = polkadot::storage().hrmp().hrmp_ingress_channels_index(&Id(para_id));
let hrmp_channels = self.storage().at(block_hash).fetch(&addr).await?.unwrap_or_default();
let mut channels_configuration: BTreeMap<u32, SubxtHrmpChannel> = BTreeMap::new();
Expand All @@ -164,7 +164,7 @@ impl<T: OnlineClientT<PolkadotConfig>> ApiClientT for ApiClient<T> {
block_hash: H256,
para_id: u32,
) -> Result<BTreeMap<u32, SubxtHrmpChannel>, subxt::Error> {
use polkadot::runtime_types::polkadot_parachain::primitives::{HrmpChannelId, Id};
use polkadot::runtime_types::polkadot_parachain_primitives::primitives::{HrmpChannelId, Id};

let addr = polkadot::storage().hrmp().hrmp_egress_channels_index(&Id(para_id));
let hrmp_channels = self.storage().at(block_hash).fetch(&addr).await?.unwrap_or_default();
Expand Down
27 changes: 2 additions & 25 deletions essentials/src/api/dynamic.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
use super::subxt_wrapper::SubxtWrapperError::{self, DecodeDynamicError};
use crate::{
metadata::{
polkadot::runtime_types::{
polkadot_parachain::primitives::Id, polkadot_runtime_parachains::scheduler::AssignmentKind,
},
polkadot_primitives::{CoreIndex, ValidatorIndex},
},
types::{Assignment, BlockNumber, ClaimQueue, CoreAssignment, CoreOccupied, OnDemandOrder, ParasEntry},
metadata::polkadot_primitives::ValidatorIndex,
types::{Assignment, BlockNumber, ClaimQueue, CoreOccupied, OnDemandOrder, ParasEntry},
};
use log::error;
use std::collections::{BTreeMap, VecDeque};
Expand Down Expand Up @@ -58,24 +53,6 @@ pub(crate) fn decode_availability_cores(raw_cores: &Value<u32>) -> Result<Vec<Co
Ok(cores)
}

pub(crate) fn decode_scheduled_paras(raw_paras: &Value<u32>) -> Result<Vec<CoreAssignment>, SubxtWrapperError> {
let decoded_paras = decode_unnamed_composite(raw_paras)?;
let mut paras = Vec::with_capacity(decoded_paras.len());
for para in decoded_paras.iter() {
let core = CoreIndex(decode_composite_u128_value(value_at("core", para)?)? as u32);
let para_id = Id(decode_composite_u128_value(value_at("para_id", para)?)? as u32);
let kind = match decode_variant(value_at("kind", para)?)?.name.as_str() {
"Parachain" => AssignmentKind::Parachain,
name => todo!("Add support for {name}"),
};
let assignment = CoreAssignment { core, para_id, kind };

paras.push(assignment)
}

Ok(paras)
}

pub(crate) fn decode_claim_queue(raw: &Value<u32>) -> Result<ClaimQueue, SubxtWrapperError> {
let decoded_btree_map = decode_unnamed_composite(raw)?;
let decoded_btree_map_inner = decoded_btree_map
Expand Down
15 changes: 0 additions & 15 deletions essentials/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,6 @@ mod tests {
.expect("Inherent data must be present");
}

#[tokio::test]
async fn get_scheduled_paras() {
let api = ApiService::<H256>::new_with_storage(
RecordsStorageConfig { max_blocks: 1 },
ApiClientMode::RPC,
RetryOptions::default(),
);
let mut subxt = api.subxt();

let head = subxt.get_block_head(rpc_node_url(), None).await.unwrap().unwrap();
let paras = subxt.get_scheduled_paras(rpc_node_url(), head.hash()).await;

assert!(paras.is_ok());
}

#[tokio::test]
async fn get_occupied_cores() {
let api = ApiService::<H256>::new_with_storage(
Expand Down
29 changes: 3 additions & 26 deletions essentials/src/api/subxt_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
use crate::{
api::{
api_client::{build_light_client, build_online_client, ApiClientT, HeaderStream},
dynamic::{decode_availability_cores, decode_claim_queue, decode_scheduled_paras, decode_validator_groups},
dynamic::{decode_availability_cores, decode_claim_queue, decode_validator_groups},
},
metadata::polkadot_primitives,
types::{
AccountId32, BlockNumber, ClaimQueue, CoreAssignment, CoreOccupied, InherentData, SessionKeys,
SubxtHrmpChannel, Timestamp, H256,
AccountId32, BlockNumber, ClaimQueue, CoreOccupied, InherentData, SessionKeys, SubxtHrmpChannel, Timestamp,
H256,
},
utils::{Retry, RetryOptions},
};
Expand Down Expand Up @@ -54,8 +54,6 @@ pub enum RequestType {
GetEvents(<PolkadotConfig as subxt::Config>::Hash),
/// Extract the `ParaInherentData` from a given block.
ExtractParaInherent(Option<<PolkadotConfig as subxt::Config>::Hash>),
/// Get the availability core scheduling information at a given block.
GetScheduledParas(<PolkadotConfig as subxt::Config>::Hash),
/// Get the claim queue scheduling information at a given block.
GetClaimQueue(<PolkadotConfig as subxt::Config>::Hash),
/// Get occupied core information at a given block.
Expand Down Expand Up @@ -102,9 +100,6 @@ impl Debug for RequestType {
RequestType::ExtractParaInherent(h) => {
format!("get inherent for block: {:?}", h)
},
RequestType::GetScheduledParas(h) => {
format!("get scheduled paras: {:?}", h)
},
RequestType::GetClaimQueue(h) => {
format!("get claim queue: {:?}", h)
},
Expand Down Expand Up @@ -151,8 +146,6 @@ pub enum Response {
MaybeEvents(Option<subxt::events::Events<PolkadotConfig>>),
/// `ParaInherent` data.
ParaInherentData(InherentData),
/// Availability core assignments for parachains.
ScheduledParas(Vec<CoreAssignment>),
/// Claim queue for parachains.
ClaimQueue(ClaimQueue),
/// List of the occupied availability cores.
Expand Down Expand Up @@ -236,7 +229,6 @@ impl RequestExecutor {
RequestType::GetBlockHash(maybe_block_number) => subxt_get_block_hash(api, maybe_block_number).await,
RequestType::GetEvents(hash) => subxt_get_events(api, hash).await,
RequestType::ExtractParaInherent(maybe_hash) => subxt_extract_parainherent(api, maybe_hash).await,
RequestType::GetScheduledParas(hash) => subxt_get_sheduled_paras(api, hash).await,
RequestType::GetClaimQueue(hash) => subxt_get_claim_queue(api, hash).await,
RequestType::GetOccupiedCores(hash) => subxt_get_occupied_cores(api, hash).await,
RequestType::GetBackingGroups(hash) => subxt_get_validator_groups(api, hash).await,
Expand Down Expand Up @@ -321,14 +313,6 @@ impl RequestExecutor {
wrap_subxt_call!(self, ExtractParaInherent, ParaInherentData, url, maybe_hash)
}

pub async fn get_scheduled_paras(
&mut self,
url: &str,
block_hash: <PolkadotConfig as subxt::Config>::Hash,
) -> std::result::Result<Vec<CoreAssignment>, SubxtWrapperError> {
wrap_subxt_call!(self, GetScheduledParas, ScheduledParas, url, block_hash)
}

pub async fn get_claim_queue(
&mut self,
url: &str,
Expand Down Expand Up @@ -501,13 +485,6 @@ async fn fetch_dynamic_storage(
.ok_or(SubxtWrapperError::EmptyResponseFromDynamicStorage(format!("{pallet_name}.{entry_name}")))
}

async fn subxt_get_sheduled_paras(api: Box<dyn ApiClientT>, block_hash: H256) -> Result {
let value = fetch_dynamic_storage(api, Some(block_hash), "ParaScheduler", "Scheduled").await?;
let paras = decode_scheduled_paras(&value)?;

Ok(Response::ScheduledParas(paras))
}

async fn subxt_get_claim_queue(api: Box<dyn ApiClientT>, block_hash: H256) -> Result {
let value = fetch_dynamic_storage(api, Some(block_hash), "ParaScheduler", "ClaimQueue").await?;
let queue = decode_claim_queue(&value)?;
Expand Down
34 changes: 8 additions & 26 deletions essentials/src/collector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,41 +671,23 @@ impl Collector {
block_number: u32,
ts: Timestamp,
) -> color_eyre::Result<(), CollectorError> {
// After adding On-demand Parachains, `ParaScheduler.Scheduled` API call will be removed
let mut assignments = self.core_assignments_via_scheduled_paras(block_hash).await;
// `ParaScheduler,Scheduled` not found, try to fetch `ParaScheduler.ClaimQueue`
if let Err(SubxtWrapperError::SubxtError(subxt::error::Error::Metadata(
subxt::error::MetadataError::StorageEntryNotFound(_),
))) = assignments
{
assignments = self.core_assignments_via_claim_queue(block_hash).await;
}
if let Err(SubxtWrapperError::EmptyResponseFromDynamicStorage(reason)) = assignments {
info!("{}. Nothing to process, used empty value", reason);
assignments = Ok(BTreeMap::default());
}

let assignments = match self.core_assignments_via_claim_queue(block_hash).await {
Err(SubxtWrapperError::EmptyResponseFromDynamicStorage(reason)) => {
info!("{}. Nothing to process, used empty value", reason);
BTreeMap::default()
},
v => v?,
};
self.storage_write_prefixed(
CollectorPrefixType::CoreAssignments,
block_hash,
StorageEntry::new_onchain(RecordTime::with_ts(block_number, Duration::from_secs(ts)), assignments?),
StorageEntry::new_onchain(RecordTime::with_ts(block_number, Duration::from_secs(ts)), assignments),
)
.await?;

Ok(())
}

async fn core_assignments_via_scheduled_paras(
&mut self,
block_hash: H256,
) -> color_eyre::Result<BTreeMap<u32, Vec<u32>>, SubxtWrapperError> {
let core_assignments = self.executor.get_scheduled_paras(self.endpoint.as_str(), block_hash).await?;
Ok(core_assignments
.iter()
.map(|v| (v.core.0, vec![v.para_id.0]))
.collect::<BTreeMap<_, _>>())
}

async fn core_assignments_via_claim_queue(
&mut self,
block_hash: H256,
Expand Down
2 changes: 1 addition & 1 deletion essentials/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
#[subxt::subxt(runtime_metadata_path = "assets/polkadot_metadata.scale")]
pub mod polkadot {}

pub use polkadot::runtime_types::polkadot_primitives::v4 as polkadot_primitives;
pub use polkadot::runtime_types::polkadot_primitives::v5 as polkadot_primitives;
27 changes: 3 additions & 24 deletions essentials/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@
// along with polkadot-introspector. If not, see <http://www.gnu.org/licenses/>.
//

use crate::metadata::{
polkadot::{
runtime_types as subxt_runtime_types,
runtime_types::{polkadot_parachain::primitives::Id, polkadot_runtime_parachains::scheduler::AssignmentKind},
},
polkadot_primitives,
};
use crate::metadata::{polkadot::runtime_types as subxt_runtime_types, polkadot_primitives};
use parity_scale_codec::{Decode, Encode};
use std::collections::{BTreeMap, VecDeque};
use subxt::{
Expand All @@ -40,12 +34,8 @@ pub type SubxtCall = runtime::RuntimeCall;
pub type ClaimQueue = BTreeMap<u32, VecDeque<Option<ParasEntry>>>;

/// The `InherentData` constructed with the subxt API.
pub type InherentData = polkadot_primitives::InherentData<
subxt_runtime_types::sp_runtime::generic::header::Header<
::core::primitive::u32,
subxt_runtime_types::sp_runtime::traits::BlakeTwo256,
>,
>;
pub type InherentData =
polkadot_primitives::InherentData<subxt_runtime_types::sp_runtime::generic::header::Header<::core::primitive::u32>>;

/// A wrapper over subxt HRMP channel configuration
#[derive(Debug, Clone, Default)]
Expand Down Expand Up @@ -75,17 +65,6 @@ impl From<subxt_runtime_types::polkadot_runtime_parachains::hrmp::HrmpChannel> f
}
}

// TODO: Take it from runtime types v5
/// How a free core is scheduled to be assigned.
pub struct CoreAssignment {
/// The core that is assigned.
pub core: polkadot_primitives::CoreIndex,
/// The unique ID of the para that is assigned to the core.
pub para_id: Id,
/// The kind of the assignment.
pub kind: AssignmentKind,
}

// TODO: Take it from runtime types v5
/// Polkadot v5 ParasEntry type
#[derive(Debug)]
Expand Down
10 changes: 3 additions & 7 deletions parachain-tracer/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ use polkadot_introspector_essentials::{
polkadot::runtime_types::{
bounded_collections::bounded_vec::BoundedVec,
polkadot_core_primitives::CandidateHash,
polkadot_parachain::primitives::{HeadData, Id, ValidationCodeHash},
polkadot_parachain_primitives::primitives::{HeadData, Id, ValidationCodeHash},
sp_core::sr25519::{Public, Signature},
sp_runtime::{
generic::{digest::Digest, header::Header},
traits::BlakeTwo256,
},
sp_runtime::generic::{digest::Digest, header::Header},
},
polkadot_primitives::{
collator_app, signed::UncheckedSigned, validator_app, AvailabilityBitfield, BackedCandidate,
Expand Down Expand Up @@ -106,7 +103,7 @@ pub fn create_dispute_statement_set() -> DisputeStatementSet {
}
}

pub fn create_inherent_data(para_id: u32) -> InherentData<Header<u32, BlakeTwo256>> {
pub fn create_inherent_data(para_id: u32) -> InherentData<Header<u32>> {
InherentData {
bitfields: vec![UncheckedSigned {
payload: AvailabilityBitfield(DecodedBits::from_iter([true])),
Expand All @@ -122,7 +119,6 @@ pub fn create_inherent_data(para_id: u32) -> InherentData<Header<u32, BlakeTwo25
state_root: Default::default(),
extrinsics_root: Default::default(),
digest: Digest { logs: Default::default() },
__subxt_unused_type_params: Default::default(),
},
}
}
Expand Down