Skip to content

Commit

Permalink
fix broken changes with runtime upgrade v1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
paulormart committed Apr 18, 2024
1 parent 04b8fbe commit aa36e6e
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 59 deletions.
Binary file modified metadata/kusama_metadata.scale
Binary file not shown.
1 change: 1 addition & 0 deletions src/records.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,7 @@ pub struct ValidatorProfileRecord {
pub mvr: Option<u64>,
// mvr_session contains the session from where the mvr was last updated
pub mvr_session: Option<EpochIndex>,
// TODO: DEPRECATE is_oversubscribed, after runtime v1.2 no longer needed
pub is_oversubscribed: bool,
pub is_active: bool,
pub is_chilled: bool,
Expand Down
12 changes: 6 additions & 6 deletions src/runtimes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ TODO: Improve the runtimes implementation without the need of replicating the sa
Download metadata from a substrate node, for use with `subxt` codegen.

```bash
subxt metadata --url wss://rpc.polkadot.io:443 -f bytes > polkadot_metadata.scale
subxt metadata --url wss://kusama-rpc.polkadot.io:443 -f bytes > kusama_metadata.scale
subxt metadata --url wss://westend-rpc.polkadot.io:443 -f bytes > westend_metadata.scale
subxt metadata --url wss://rpc.turboflakes.io:443/westend -f bytes > westend_metadata.scale
subxt metadata --url wss://rpc.turboflakes.io:443/kusama -f bytes > kusama_metadata.scale
subxt metadata --url wss://rpc.turboflakes.io:443/polkadot -f bytes > polkadot_metadata.scale
```

Generate runtime API client code from metadata.

```bash
subxt codegen --file polkadot_metadata.scale | rustfmt --edition=2018 --emit=stdout > polkadot_runtime.rs
subxt codegen --file kusama_metadata.scale | rustfmt --edition=2018 --emit=stdout > kusama_runtime.rs
subxt codegen --file westend_metadata.scale | rustfmt --edition=2018 --emit=stdout > westend_runtime.rs
subxt codegen --url wss://rpc.turboflakes.io:443/westend | rustfmt --edition=2018 --emit=stdout > westend_metadata.rs
subxt codegen --url wss://rpc.turboflakes.io:443/kusama | rustfmt --edition=2018 --emit=stdout > kusama_runtime.rs
subxt codegen --url wss://rpc.turboflakes.io:443/polkadot | rustfmt --edition=2018 --emit=stdout > polkadot_runtime.rs
```
151 changes: 98 additions & 53 deletions src/runtimes/kusama.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ use node_runtime::{
runtime_types::{
bounded_collections::bounded_vec::BoundedVec, pallet_identity::types::Data,
pallet_nomination_pools::PoolState, polkadot_parachain_primitives::primitives::Id,
polkadot_primitives::v5::CoreOccupied, polkadot_primitives::v5::DisputeStatement,
polkadot_primitives::v5::ValidatorIndex, polkadot_primitives::v5::ValidityAttestation,
polkadot_primitives::v6::DisputeStatement, polkadot_primitives::v6::ValidatorIndex,
polkadot_primitives::v6::ValidityAttestation,
polkadot_runtime_parachains::scheduler::common::Assignment,
polkadot_runtime_parachains::scheduler::pallet::CoreOccupied,
sp_arithmetic::per_things::Perbill, sp_consensus_babe::digests::PreDigest,
},
session::events::NewSession,
Expand Down Expand Up @@ -1287,7 +1289,43 @@ pub async fn track_records(
.fetch(&last_runtime_upgrade_addr)
.await?
{
if info.spec_version >= 1000000 {
// TODO: evaluate how history can also be imported with different metadata
//
// if info.spec_version >= 1000000 && info.spec_version < 1002000 {
// // Fetch availability_cores
// let availability_cores_addr = node_runtime::storage()
// .para_scheduler()
// .availability_cores();

// if let Some(availability_cores) = api
// .storage()
// .at(block_hash)
// .fetch(&availability_cores_addr)
// .await?
// {
// for (i, core_occupied) in availability_cores.iter().enumerate()
// {
// let core_idx = u32::try_from(i).unwrap();
// match &core_occupied {
// CoreOccupied::Free => records.update_core_free(
// core_idx,
// Some(backing_votes.session),
// ),
// CoreOccupied::Paras(paras_entry) => {
// // destructure para_id
// let Id(para_id) = paras_entry.assignment.para_id;
// records.update_core_by_para_id(
// para_id,
// core_idx,
// Some(backing_votes.session),
// );
// }
// }
// }
// }
// }

if info.spec_version >= 1002000 {
// Fetch availability_cores
let availability_cores_addr = node_runtime::storage()
.para_scheduler()
Expand All @@ -1308,13 +1346,26 @@ pub async fn track_records(
Some(backing_votes.session),
),
CoreOccupied::Paras(paras_entry) => {
// destructure para_id
let Id(para_id) = paras_entry.assignment.para_id;
records.update_core_by_para_id(
para_id,
core_idx,
Some(backing_votes.session),
);
match &paras_entry.assignment {
// ParasEntry::<u32>
Assignment::Pool {
para_id: Id(para_id),
core_index: _,
} => {
records.update_core_by_para_id(
para_id.clone(),
core_idx,
Some(backing_votes.session),
);
}
Assignment::Bulk(Id(para_id)) => {
records.update_core_by_para_id(
para_id.clone(),
core_idx,
Some(backing_votes.session),
);
}
}
}
}
}
Expand Down Expand Up @@ -1833,8 +1884,6 @@ pub async fn run_network_report(records: &Records) -> Result<(), OnetError> {
} else {
v.subset = Subset::TVP;
}
v.is_oversubscribed =
verify_oversubscribed(&onet, active_era_index, &stash, None).await?;
} else {
v.subset = Subset::C100;
}
Expand Down Expand Up @@ -2814,41 +2863,42 @@ async fn try_run_nomination(
))
}

async fn verify_oversubscribed(
onet: &Onet,
era_index: u32,
stash: &AccountId32,
block_hash: Option<H256>,
) -> Result<bool, OnetError> {
let api = onet.client().clone();

let max_addr = node_runtime::constants()
.staking()
.max_nominator_rewarded_per_validator();
let max: u32 = api.constants().at(&max_addr)?;

let block_hash = match block_hash {
Some(bh) => bh,
None => onet
.rpc()
.chain_get_block_hash(None)
.await?
.expect("didn't pass a block number; qed"),
};

let eras_stakers_addr = node_runtime::storage()
.staking()
.eras_stakers(&era_index, stash);
if let Some(exposure) = api
.storage()
.at(block_hash)
.fetch(&eras_stakers_addr)
.await?
{
return Ok(exposure.others.len() as u32 > max);
}
return Ok(false);
}
// TODO: DEPRECATE, no longer needed after runtime upgrade v1.2
// async fn verify_oversubscribed(
// onet: &Onet,
// era_index: u32,
// stash: &AccountId32,
// block_hash: Option<H256>,
// ) -> Result<bool, OnetError> {
// let api = onet.client().clone();

// let max_addr = node_runtime::constants()
// .staking()
// .max_nominator_rewarded_per_validator();
// let max: u32 = api.constants().at(&max_addr)?;

// let block_hash = match block_hash {
// Some(bh) => bh,
// None => onet
// .rpc()
// .chain_get_block_hash(None)
// .await?
// .expect("didn't pass a block number; qed"),
// };

// let eras_stakers_addr = node_runtime::storage()
// .staking()
// .eras_stakers(&era_index, stash);
// if let Some(exposure) = api
// .storage()
// .at(block_hash)
// .fetch(&eras_stakers_addr)
// .await?
// {
// return Ok(exposure.others.len() as u32 > max);
// }
// return Ok(false);
// }

async fn get_own_stake_via_controller(
onet: &Onet,
Expand Down Expand Up @@ -2902,7 +2952,7 @@ async fn get_identity(
.fetch(&identity_of_addr)
.await?
{
Some(identity) => {
Some((identity, _)) => {
debug!("identity {:?}", identity);
let parent = parse_identity_data(identity.info.display);
let identity = match sub_account_name {
Expand Down Expand Up @@ -3122,11 +3172,6 @@ pub async fn cache_session_stats_records(
Subset::C100
};

// check if is oversubscribed
v.is_oversubscribed =
verify_oversubscribed(&onet, era_index, &stash, Some(block.parent_hash))
.await?;

// check if is in active set
v.is_active = authorities.contains(&stash);

Expand Down

0 comments on commit aa36e6e

Please sign in to comment.