feat: initialize Kurdistan SDK - independent fork of Polkadot SDK
This commit is contained in:
@@ -0,0 +1,196 @@
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// This file is part of Pezkuwi.
|
||||
|
||||
// Pezkuwi is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Pezkuwi is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
use super::*;
|
||||
use codec::{Decode, Encode};
|
||||
use frame_support::{
|
||||
pallet_prelude::ValueQuery, traits::UncheckedOnRuntimeUpgrade, weights::Weight,
|
||||
};
|
||||
|
||||
#[cfg(feature = "try-runtime")]
|
||||
const LOG_TARGET: &str = "runtime::shared";
|
||||
|
||||
pub mod v0 {
|
||||
use super::*;
|
||||
use alloc::collections::vec_deque::VecDeque;
|
||||
|
||||
use frame_support::storage_alias;
|
||||
|
||||
/// All allowed relay-parents storage at version 0.
|
||||
#[storage_alias]
|
||||
pub(crate) type AllowedRelayParents<T: Config> = StorageValue<
|
||||
Pallet<T>,
|
||||
super::v0::AllowedRelayParentsTracker<<T as frame_system::Config>::Hash, BlockNumberFor<T>>,
|
||||
ValueQuery,
|
||||
>;
|
||||
|
||||
#[derive(Encode, Decode, Default, TypeInfo)]
|
||||
pub struct AllowedRelayParentsTracker<Hash, BlockNumber> {
|
||||
// The past relay parents, paired with state roots, that are viable to build upon.
|
||||
//
|
||||
// They are in ascending chronologic order, so the newest relay parents are at
|
||||
// the back of the deque.
|
||||
//
|
||||
// (relay_parent, state_root)
|
||||
pub buffer: VecDeque<(Hash, Hash)>,
|
||||
|
||||
// The number of the most recent relay-parent, if any.
|
||||
// If the buffer is empty, this value has no meaning and may
|
||||
// be nonsensical.
|
||||
pub latest_number: BlockNumber,
|
||||
}
|
||||
|
||||
// Required to workaround #64.
|
||||
impl<Hash: PartialEq + Copy, BlockNumber: AtLeast32BitUnsigned + Copy>
|
||||
AllowedRelayParentsTracker<Hash, BlockNumber>
|
||||
{
|
||||
/// Returns block number of the earliest block the buffer would contain if
|
||||
/// `now` is pushed into it.
|
||||
pub(crate) fn hypothetical_earliest_block_number(
|
||||
&self,
|
||||
now: BlockNumber,
|
||||
max_ancestry_len: u32,
|
||||
) -> BlockNumber {
|
||||
let allowed_ancestry_len = max_ancestry_len.min(self.buffer.len() as u32);
|
||||
|
||||
now - allowed_ancestry_len.into()
|
||||
}
|
||||
}
|
||||
|
||||
impl<Hash, BlockNumber> From<AllowedRelayParentsTracker<Hash, BlockNumber>>
|
||||
for super::AllowedRelayParentsTracker<Hash, BlockNumber>
|
||||
{
|
||||
fn from(value: AllowedRelayParentsTracker<Hash, BlockNumber>) -> Self {
|
||||
Self {
|
||||
latest_number: value.latest_number,
|
||||
buffer: value
|
||||
.buffer
|
||||
.into_iter()
|
||||
.map(|(relay_parent, state_root)| super::RelayParentInfo {
|
||||
relay_parent,
|
||||
state_root,
|
||||
claim_queue: Default::default(),
|
||||
})
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod v1 {
|
||||
use super::*;
|
||||
|
||||
#[cfg(feature = "try-runtime")]
|
||||
use frame_support::{
|
||||
ensure,
|
||||
traits::{GetStorageVersion, StorageVersion},
|
||||
};
|
||||
|
||||
pub struct VersionUncheckedMigrateToV1<T>(core::marker::PhantomData<T>);
|
||||
|
||||
impl<T: Config> UncheckedOnRuntimeUpgrade for VersionUncheckedMigrateToV1<T> {
|
||||
#[cfg(feature = "try-runtime")]
|
||||
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
|
||||
log::trace!(target: LOG_TARGET, "Running pre_upgrade() for shared MigrateToV1");
|
||||
let bytes = u32::to_ne_bytes(v0::AllowedRelayParents::<T>::get().buffer.len() as u32);
|
||||
|
||||
Ok(bytes.to_vec())
|
||||
}
|
||||
|
||||
fn on_runtime_upgrade() -> Weight {
|
||||
let mut weight: Weight = Weight::zero();
|
||||
|
||||
// Read old storage.
|
||||
let old_rp_tracker = v0::AllowedRelayParents::<T>::take();
|
||||
|
||||
super::AllowedRelayParents::<T>::set(old_rp_tracker.into());
|
||||
|
||||
weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
|
||||
|
||||
weight
|
||||
}
|
||||
|
||||
#[cfg(feature = "try-runtime")]
|
||||
fn post_upgrade(state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
|
||||
log::trace!(target: LOG_TARGET, "Running post_upgrade() for shared MigrateToV1");
|
||||
ensure!(
|
||||
Pallet::<T>::on_chain_storage_version() >= StorageVersion::new(1),
|
||||
"Storage version should be >= 1 after the migration"
|
||||
);
|
||||
|
||||
let relay_parent_count = u32::from_ne_bytes(
|
||||
state
|
||||
.try_into()
|
||||
.expect("u32::from_ne_bytes(to_ne_bytes(u32)) always works; qed"),
|
||||
);
|
||||
|
||||
let rp_tracker = AllowedRelayParents::<T>::get();
|
||||
|
||||
ensure!(
|
||||
relay_parent_count as usize == rp_tracker.buffer.len(),
|
||||
"Number of allowed relay parents should be the same as the one before the upgrade."
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Migrate shared module storage to v1.
|
||||
pub type MigrateToV1<T> = frame_support::migrations::VersionedMigration<
|
||||
0,
|
||||
1,
|
||||
v1::VersionUncheckedMigrateToV1<T>,
|
||||
Pallet<T>,
|
||||
<T as frame_system::Config>::DbWeight,
|
||||
>;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{v1::VersionUncheckedMigrateToV1, *};
|
||||
use crate::mock::{new_test_ext, MockGenesisConfig, Test};
|
||||
use frame_support::traits::UncheckedOnRuntimeUpgrade;
|
||||
use pezkuwi_primitives::Hash;
|
||||
|
||||
#[test]
|
||||
fn migrate_to_v1() {
|
||||
new_test_ext(MockGenesisConfig::default()).execute_with(|| {
|
||||
let rp_tracker = v0::AllowedRelayParentsTracker {
|
||||
latest_number: 9,
|
||||
buffer: (0..10u64)
|
||||
.into_iter()
|
||||
.map(|idx| (Hash::from_low_u64_ne(idx), Hash::from_low_u64_ne(2 * idx)))
|
||||
.collect::<VecDeque<_>>(),
|
||||
};
|
||||
|
||||
v0::AllowedRelayParents::<Test>::put(rp_tracker);
|
||||
|
||||
<VersionUncheckedMigrateToV1<Test> as UncheckedOnRuntimeUpgrade>::on_runtime_upgrade();
|
||||
|
||||
let rp_tracker = AllowedRelayParents::<Test>::get();
|
||||
|
||||
assert_eq!(rp_tracker.buffer.len(), 10);
|
||||
|
||||
for idx in 0..10u64 {
|
||||
let relay_parent = Hash::from_low_u64_ne(idx);
|
||||
let state_root = Hash::from_low_u64_ne(2 * idx);
|
||||
let (info, block_num) = rp_tracker.acquire_info(relay_parent, None).unwrap();
|
||||
|
||||
assert!(info.claim_queue.is_empty());
|
||||
assert_eq!(info.relay_parent, relay_parent);
|
||||
assert_eq!(info.state_root, state_root);
|
||||
assert_eq!(block_num as u64, idx);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,235 @@
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// This file is part of Pezkuwi.
|
||||
|
||||
// Pezkuwi is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Pezkuwi is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use super::*;
|
||||
use crate::{
|
||||
configuration::HostConfiguration,
|
||||
mock::{new_test_ext, MockGenesisConfig, ParasShared, Test},
|
||||
shared,
|
||||
};
|
||||
use assert_matches::assert_matches;
|
||||
use pezkuwi_primitives::Hash;
|
||||
use pezkuwi_primitives_test_helpers::validator_pubkeys;
|
||||
use sp_keyring::Sr25519Keyring;
|
||||
|
||||
#[test]
|
||||
fn tracker_earliest_block_number() {
|
||||
let mut tracker = AllowedRelayParentsTracker::default();
|
||||
|
||||
// Test it on an empty tracker.
|
||||
let now: u32 = 1;
|
||||
let max_ancestry_len = 5;
|
||||
assert_eq!(tracker.hypothetical_earliest_block_number(now, max_ancestry_len), now);
|
||||
|
||||
// Push a single block into the tracker, suppose max capacity is 1.
|
||||
let max_ancestry_len = 0;
|
||||
tracker.update(Hash::zero(), Hash::zero(), Default::default(), 0, max_ancestry_len + 1);
|
||||
assert_eq!(tracker.hypothetical_earliest_block_number(now, max_ancestry_len as _), now);
|
||||
|
||||
// Test a greater capacity.
|
||||
let max_ancestry_len = 4;
|
||||
let now = 4;
|
||||
for i in 1..now {
|
||||
tracker.update(
|
||||
Hash::from([i as u8; 32]),
|
||||
Hash::zero(),
|
||||
Default::default(),
|
||||
i,
|
||||
max_ancestry_len + 1,
|
||||
);
|
||||
assert_eq!(tracker.hypothetical_earliest_block_number(i + 1, max_ancestry_len as _), 0);
|
||||
}
|
||||
|
||||
// Capacity exceeded.
|
||||
tracker.update(Hash::zero(), Hash::zero(), Default::default(), now, max_ancestry_len);
|
||||
assert_eq!(tracker.hypothetical_earliest_block_number(now + 1, max_ancestry_len as _), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tracker_claim_queue_transpose() {
|
||||
let mut tracker = AllowedRelayParentsTracker::<Hash, u32>::default();
|
||||
|
||||
let mut claim_queue = BTreeMap::new();
|
||||
claim_queue.insert(CoreIndex(0), vec![Id::from(0), Id::from(1), Id::from(2)].into());
|
||||
claim_queue.insert(CoreIndex(1), vec![Id::from(0), Id::from(0), Id::from(100)].into());
|
||||
claim_queue.insert(CoreIndex(2), vec![Id::from(1), Id::from(2), Id::from(100)].into());
|
||||
|
||||
tracker.update(Hash::zero(), Hash::zero(), claim_queue, 1u32, 4);
|
||||
|
||||
let (info, _block_num) = tracker.acquire_info(Hash::zero(), None).unwrap();
|
||||
assert_eq!(
|
||||
info.claim_queue.get(&Id::from(0)).unwrap()[&0],
|
||||
vec![CoreIndex(0), CoreIndex(1)].into_iter().collect::<BTreeSet<_>>()
|
||||
);
|
||||
assert_eq!(
|
||||
info.claim_queue.get(&Id::from(1)).unwrap()[&0],
|
||||
vec![CoreIndex(2)].into_iter().collect::<BTreeSet<_>>()
|
||||
);
|
||||
assert_eq!(info.claim_queue.get(&Id::from(2)).unwrap().get(&0), None);
|
||||
assert_eq!(info.claim_queue.get(&Id::from(100)).unwrap().get(&0), None);
|
||||
|
||||
assert_eq!(
|
||||
info.claim_queue.get(&Id::from(0)).unwrap()[&1],
|
||||
vec![CoreIndex(1)].into_iter().collect::<BTreeSet<_>>()
|
||||
);
|
||||
assert_eq!(
|
||||
info.claim_queue.get(&Id::from(1)).unwrap()[&1],
|
||||
vec![CoreIndex(0)].into_iter().collect::<BTreeSet<_>>()
|
||||
);
|
||||
assert_eq!(
|
||||
info.claim_queue.get(&Id::from(2)).unwrap()[&1],
|
||||
vec![CoreIndex(2)].into_iter().collect::<BTreeSet<_>>()
|
||||
);
|
||||
assert_eq!(info.claim_queue.get(&Id::from(100)).unwrap().get(&1), None);
|
||||
|
||||
assert_eq!(info.claim_queue.get(&Id::from(0)).unwrap().get(&2), None);
|
||||
assert_eq!(info.claim_queue.get(&Id::from(1)).unwrap().get(&2), None);
|
||||
assert_eq!(
|
||||
info.claim_queue.get(&Id::from(2)).unwrap()[&2],
|
||||
vec![CoreIndex(0)].into_iter().collect::<BTreeSet<_>>()
|
||||
);
|
||||
assert_eq!(
|
||||
info.claim_queue.get(&Id::from(100)).unwrap()[&2],
|
||||
vec![CoreIndex(1), CoreIndex(2)].into_iter().collect::<BTreeSet<_>>()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tracker_acquire_info() {
|
||||
let mut tracker = AllowedRelayParentsTracker::<Hash, u32>::default();
|
||||
let max_ancestry_len = 2;
|
||||
|
||||
// (relay_parent, state_root) pairs.
|
||||
let blocks = &[
|
||||
(Hash::repeat_byte(0), Hash::repeat_byte(10)),
|
||||
(Hash::repeat_byte(1), Hash::repeat_byte(11)),
|
||||
(Hash::repeat_byte(2), Hash::repeat_byte(12)),
|
||||
];
|
||||
|
||||
let (relay_parent, state_root) = blocks[0];
|
||||
tracker.update(relay_parent, state_root, Default::default(), 0, max_ancestry_len + 1);
|
||||
assert_matches!(
|
||||
tracker.acquire_info(relay_parent, None),
|
||||
Some((s, b)) if s.state_root == state_root && b == 0
|
||||
);
|
||||
|
||||
// Try to push a duplicate. Should be ignored.
|
||||
tracker.update(
|
||||
relay_parent,
|
||||
Hash::repeat_byte(13),
|
||||
Default::default(),
|
||||
0,
|
||||
max_ancestry_len + 1,
|
||||
);
|
||||
assert_eq!(tracker.buffer.len(), 1);
|
||||
assert_matches!(
|
||||
tracker.acquire_info(relay_parent, None),
|
||||
Some((s, b)) if s.state_root == state_root && b == 0
|
||||
);
|
||||
|
||||
let (relay_parent, state_root) = blocks[1];
|
||||
tracker.update(relay_parent, state_root, Default::default(), 1u32, max_ancestry_len + 1);
|
||||
let (relay_parent, state_root) = blocks[2];
|
||||
tracker.update(relay_parent, state_root, Default::default(), 2u32, max_ancestry_len + 1);
|
||||
for (block_num, (rp, state_root)) in blocks.iter().enumerate().take(2) {
|
||||
assert_matches!(
|
||||
tracker.acquire_info(*rp, None),
|
||||
Some((s, b)) if &s.state_root == state_root && b == block_num as u32
|
||||
);
|
||||
|
||||
assert!(tracker.acquire_info(*rp, Some(2)).is_none());
|
||||
}
|
||||
|
||||
for (block_num, (rp, state_root)) in blocks.iter().enumerate().skip(1) {
|
||||
assert_matches!(
|
||||
tracker.acquire_info(*rp, Some(block_num as u32 - 1)),
|
||||
Some((s, b)) if &s.state_root == state_root && b == block_num as u32
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sets_and_shuffles_validators() {
|
||||
let validators = vec![
|
||||
Sr25519Keyring::Alice,
|
||||
Sr25519Keyring::Bob,
|
||||
Sr25519Keyring::Charlie,
|
||||
Sr25519Keyring::Dave,
|
||||
Sr25519Keyring::Ferdie,
|
||||
];
|
||||
|
||||
let mut config = HostConfiguration::default();
|
||||
config.max_validators = None;
|
||||
|
||||
let pubkeys = validator_pubkeys(&validators);
|
||||
|
||||
new_test_ext(MockGenesisConfig::default()).execute_with(|| {
|
||||
let validators = ParasShared::initializer_on_new_session(1, [1; 32], &config, pubkeys);
|
||||
|
||||
assert_eq!(
|
||||
validators,
|
||||
validator_pubkeys(&[
|
||||
Sr25519Keyring::Ferdie,
|
||||
Sr25519Keyring::Bob,
|
||||
Sr25519Keyring::Charlie,
|
||||
Sr25519Keyring::Dave,
|
||||
Sr25519Keyring::Alice,
|
||||
])
|
||||
);
|
||||
|
||||
assert_eq!(shared::ActiveValidatorKeys::<Test>::get(), validators);
|
||||
|
||||
assert_eq!(
|
||||
shared::ActiveValidatorIndices::<Test>::get(),
|
||||
vec![
|
||||
ValidatorIndex(4),
|
||||
ValidatorIndex(1),
|
||||
ValidatorIndex(2),
|
||||
ValidatorIndex(3),
|
||||
ValidatorIndex(0),
|
||||
]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sets_truncates_and_shuffles_validators() {
|
||||
let validators = vec![
|
||||
Sr25519Keyring::Alice,
|
||||
Sr25519Keyring::Bob,
|
||||
Sr25519Keyring::Charlie,
|
||||
Sr25519Keyring::Dave,
|
||||
Sr25519Keyring::Ferdie,
|
||||
];
|
||||
|
||||
let mut config = HostConfiguration::default();
|
||||
config.max_validators = Some(2);
|
||||
|
||||
let pubkeys = validator_pubkeys(&validators);
|
||||
|
||||
new_test_ext(MockGenesisConfig::default()).execute_with(|| {
|
||||
let validators = ParasShared::initializer_on_new_session(1, [1; 32], &config, pubkeys);
|
||||
|
||||
assert_eq!(validators, validator_pubkeys(&[Sr25519Keyring::Ferdie, Sr25519Keyring::Bob,]));
|
||||
|
||||
assert_eq!(shared::ActiveValidatorKeys::<Test>::get(), validators);
|
||||
|
||||
assert_eq!(
|
||||
shared::ActiveValidatorIndices::<Test>::get(),
|
||||
vec![ValidatorIndex(4), ValidatorIndex(1),]
|
||||
);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user