Skip to content

Commit

Permalink
Merge branch 'stable2407' into Magnet-Stack-toints
Browse files Browse the repository at this point in the history
  • Loading branch information
sulijia authored Sep 20, 2024
2 parents 62cc00b + 7300a76 commit a30bd62
Show file tree
Hide file tree
Showing 66 changed files with 7,952 additions and 6,342 deletions.
7,472 changes: 4,043 additions & 3,429 deletions Cargo.lock

Large diffs are not rendered by default.

325 changes: 159 additions & 166 deletions Cargo.toml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion client/coretime/bulk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ version = "0.1.0"

[dependencies]
async-trait = { workspace = true }
codec = { package = "parity-scale-codec", workspace = true, features = [ "derive" ] }
codec = { workspace = true }
futures = { workspace = true }
tracing = { workspace = true }
schnellru = { workspace = true }
Expand Down
52 changes: 41 additions & 11 deletions client/coretime/bulk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ use futures::{lock::Mutex, select, FutureExt};
use mc_coretime_common::is_parathread;
use mp_coretime_bulk::{
self, well_known_keys::broker_regions, BulkMemRecord, BulkMemRecordItem, BulkRuntimeApi,
BulkStatus,
BulkStatus, RegionRecord, RegionRecordV0,
};
use mp_coretime_common::{
chain_state_snapshot::GenericStateProof, well_known_keys::SYSTEM_BLOCKHASH_GENESIS,
};
use pallet_broker::{CoreMask, RegionId, RegionRecord};
use pallet_broker::{CoreMask, RegionId};
use polkadot_primitives::{AccountId, Balance};
use sc_client_api::UsageProvider;
use sc_service::TaskManager;
Expand Down Expand Up @@ -78,9 +78,15 @@ where

let rpc_url = std::str::from_utf8(&url)?;

let api = OnlineClient::<PolkadotConfig>::from_url(rpc_url).await?;
let api_client = OnlineClient::<PolkadotConfig>::from_url(rpc_url).await;

let mut blocks_sub = api.blocks().subscribe_best().await?;
let api = if let Ok(ok_api) = api_client {
ok_api
} else {
return Ok(());
};

let mut blocks_sub = api.blocks().subscribe_finalized().await?;

// For each block, print a bunch of information about it:
while let Some(block) = blocks_sub.next().await {
Expand All @@ -93,9 +99,18 @@ where
continue;
}
let block_number = block.header().number;
let block_hash = block.hash();

let mut bulk_record_local = bulk_record.lock().await;
bulk_record_local.coretime_para_height = block_number;
let record_index = parachain.runtime_api().record_index(hash)?;
// Remove useless item.
let record_items = &mut bulk_record_local.items;
if let Some(pos) = record_items.iter().position(|item| {
(item.status == BulkStatus::CoreAssigned) && (item.record_index + 1 == record_index)
}) {
record_items.remove(pos);
}
let events = block.events().await?;
for event in events.iter() {
let event = event?;
Expand Down Expand Up @@ -136,10 +151,8 @@ where
relevant_keys.push(region_key.as_slice());
relevant_keys.push(block_hash_key);

let proof = rpc
.state_get_read_proof(relevant_keys, Some(events.block_hash()))
.await
.unwrap();
let proof =
rpc.state_get_read_proof(relevant_keys, Some(block_hash)).await?;
let storage_proof =
StorageProof::new(proof.proof.into_iter().map(|bytes| bytes.to_vec()));

Expand All @@ -155,8 +168,23 @@ where
None,
)
.ok();
let mut proof_gen = false;
// Check proof is ok.
if head_data.is_some() {
proof_gen = true;
} else {
// decode to lower version
let head_data = relay_storage_rooted_proof
.read_entry::<RegionRecordV0<AccountId, Balance>>(
region_key.as_slice(),
None,
)
.ok();
if head_data.is_some() {
proof_gen = true;
}
}
if proof_gen {
// Record some data.
let record_item = BulkMemRecordItem {
storage_proof,
Expand All @@ -166,6 +194,7 @@ where
status: BulkStatus::Assigned,
start_relaychain_height: 0,
end_relaychain_height: 0,
record_index,
};
bulk_record_local.items.push(record_item);
}
Expand All @@ -176,7 +205,6 @@ where

// Query CoreAssigned event.
let ev_core_assigned = event.as_event::<metadata::CoreAssigned>();

if let Ok(core_assigned_event) = ev_core_assigned {
if let Some(ev) = core_assigned_event {
log::info!(
Expand Down Expand Up @@ -220,7 +248,6 @@ where
}
}
}

Ok(())
}
pub async fn run_coretime_bulk_task<P, R, Block>(
Expand All @@ -236,9 +263,12 @@ pub async fn run_coretime_bulk_task<P, R, Block>(
{
let bulk_task = async move {
loop {
let _ =
let result =
coretime_bulk_task(&*parachain, relay_chain.clone(), para_id, bulk_record.clone())
.await;
if let Err(err) = result {
log::info!("==============run_coretime_bulk_task error:{:?}", err);
}
}
};
select! {
Expand Down
74 changes: 37 additions & 37 deletions client/coretime/bulk/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
//! The data type here is generated by the subxt tool and represents the event-related data of the coretime parachain.
//!
#[derive(
:: subxt :: ext :: codec :: Decode,
:: subxt :: ext :: codec :: Encode,
:: subxt :: ext :: scale_decode :: DecodeAsType,
:: subxt :: ext :: scale_encode :: EncodeAsType,
:: subxt :: ext :: subxt_core :: ext :: codec :: Decode,
:: subxt :: ext :: subxt_core :: ext :: codec :: Encode,
:: subxt :: ext :: subxt_core :: ext :: scale_decode :: DecodeAsType,
:: subxt :: ext :: subxt_core :: ext :: scale_encode :: EncodeAsType,
Debug,
)]
# [codec (crate = :: subxt :: ext :: codec)]
#[decode_as_type(crate_path = ":: subxt :: ext :: scale_decode")]
#[encode_as_type(crate_path = ":: subxt :: ext :: scale_encode")]
# [codec (crate = :: subxt :: ext :: subxt_core :: ext :: codec)]
#[decode_as_type(crate_path = ":: subxt :: ext :: subxt_core :: ext :: scale_decode")]
#[encode_as_type(crate_path = ":: subxt :: ext :: subxt_core :: ext :: scale_encode")]
pub enum CoreAssignment {
#[codec(index = 0)]
Idle,
Expand All @@ -38,71 +38,71 @@ pub enum CoreAssignment {
}

#[derive(
:: subxt :: ext :: codec :: Decode,
:: subxt :: ext :: codec :: Encode,
:: subxt :: ext :: scale_decode :: DecodeAsType,
:: subxt :: ext :: scale_encode :: EncodeAsType,
:: subxt :: ext :: subxt_core :: ext :: codec :: Decode,
:: subxt :: ext :: subxt_core :: ext :: codec :: Encode,
:: subxt :: ext :: subxt_core :: ext :: scale_decode :: DecodeAsType,
:: subxt :: ext :: subxt_core :: ext :: scale_encode :: EncodeAsType,
Debug,
)]
# [codec (crate = :: subxt :: ext :: codec)]
#[decode_as_type(crate_path = ":: subxt :: ext :: scale_decode")]
#[encode_as_type(crate_path = ":: subxt :: ext :: scale_encode")]
# [codec (crate = :: subxt :: ext :: subxt_core :: ext :: codec)]
#[decode_as_type(crate_path = ":: subxt :: ext :: subxt_core :: ext :: scale_decode")]
#[encode_as_type(crate_path = ":: subxt :: ext :: subxt_core :: ext :: scale_encode")]
pub struct CoreAssigned {
pub core: u16,
pub when: u32,
pub assignment: Vec<(CoreAssignment, u16)>,
}

impl subxt::events::StaticEvent for CoreAssigned {
impl ::subxt::ext::subxt_core::events::StaticEvent for CoreAssigned {
const PALLET: &'static str = "Broker";
const EVENT: &'static str = "CoreAssigned";
}

#[derive(
:: subxt :: ext :: codec :: Decode,
:: subxt :: ext :: codec :: Encode,
:: subxt :: ext :: scale_decode :: DecodeAsType,
:: subxt :: ext :: scale_encode :: EncodeAsType,
:: subxt :: ext :: subxt_core :: ext :: codec :: Decode,
:: subxt :: ext :: subxt_core :: ext :: codec :: Encode,
:: subxt :: ext :: subxt_core :: ext :: scale_decode :: DecodeAsType,
:: subxt :: ext :: subxt_core :: ext :: scale_encode :: EncodeAsType,
Debug,
)]
# [codec (crate = :: subxt :: ext :: codec)]
#[decode_as_type(crate_path = ":: subxt :: ext :: scale_decode")]
#[encode_as_type(crate_path = ":: subxt :: ext :: scale_encode")]
# [codec (crate = :: subxt :: ext :: subxt_core :: ext :: codec)]
#[decode_as_type(crate_path = ":: subxt :: ext :: subxt_core :: ext :: scale_decode")]
#[encode_as_type(crate_path = ":: subxt :: ext :: subxt_core :: ext :: scale_encode")]
pub struct CoreMask(pub [u8; 10usize]);

#[derive(
:: subxt :: ext :: codec :: Decode,
:: subxt :: ext :: codec :: Encode,
:: subxt :: ext :: scale_decode :: DecodeAsType,
:: subxt :: ext :: scale_encode :: EncodeAsType,
:: subxt :: ext :: subxt_core :: ext :: codec :: Decode,
:: subxt :: ext :: subxt_core :: ext :: codec :: Encode,
:: subxt :: ext :: subxt_core :: ext :: scale_decode :: DecodeAsType,
:: subxt :: ext :: subxt_core :: ext :: scale_encode :: EncodeAsType,
Debug,
)]
# [codec (crate = :: subxt :: ext :: codec)]
#[decode_as_type(crate_path = ":: subxt :: ext :: scale_decode")]
#[encode_as_type(crate_path = ":: subxt :: ext :: scale_encode")]
# [codec (crate = :: subxt :: ext :: subxt_core :: ext :: codec)]
#[decode_as_type(crate_path = ":: subxt :: ext :: subxt_core :: ext :: scale_decode")]
#[encode_as_type(crate_path = ":: subxt :: ext :: subxt_core :: ext :: scale_encode")]
pub struct RegionId {
pub begin: u32,
pub core: u16,
pub mask: CoreMask,
}

#[derive(
:: subxt :: ext :: codec :: Decode,
:: subxt :: ext :: codec :: Encode,
:: subxt :: ext :: scale_decode :: DecodeAsType,
:: subxt :: ext :: scale_encode :: EncodeAsType,
:: subxt :: ext :: subxt_core :: ext :: codec :: Decode,
:: subxt :: ext :: subxt_core :: ext :: codec :: Encode,
:: subxt :: ext :: subxt_core :: ext :: scale_decode :: DecodeAsType,
:: subxt :: ext :: subxt_core :: ext :: scale_encode :: EncodeAsType,
Debug,
)]
# [codec (crate = :: subxt :: ext :: codec)]
#[decode_as_type(crate_path = ":: subxt :: ext :: scale_decode")]
#[encode_as_type(crate_path = ":: subxt :: ext :: scale_encode")]
# [codec (crate = :: subxt :: ext :: subxt_core :: ext :: codec)]
#[decode_as_type(crate_path = ":: subxt :: ext :: subxt_core :: ext :: scale_decode")]
#[encode_as_type(crate_path = ":: subxt :: ext :: subxt_core :: ext :: scale_encode")]
pub struct Assigned {
pub region_id: RegionId,
pub duration: u32,
pub task: u32,
}

impl ::subxt::events::StaticEvent for Assigned {
impl ::subxt::ext::subxt_core::events::StaticEvent for Assigned {
const PALLET: &'static str = "Broker";
const EVENT: &'static str = "Assigned";
}
4 changes: 3 additions & 1 deletion client/coretime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ version = "0.1.0"

[dependencies]
async-trait = { workspace = true }
codec = { package = "parity-scale-codec", workspace = true, features = [ "derive" ] }
codec = {workspace = true}
futures = { workspace = true }
tracing = { workspace = true }
schnellru = { workspace = true }
log = { workspace = true }
subxt = { workspace = true, features = ["substrate-compat"]}
scale-info = { workspace = true, features = ["derive", "serde"] }

mp-coretime-common = { path = "../../../primitives/coretime/common"}

Expand All @@ -42,6 +43,7 @@ substrate-prometheus-endpoint = { workspace = true }
sc-service = { workspace = true }
pallet-broker = { workspace = true}
sp-storage = { workspace = true }
frame-support = { workspace = true }

# Cumulus
cumulus-client-consensus-common = { workspace = true }
Expand Down
Loading

0 comments on commit a30bd62

Please sign in to comment.