Skip to content

Commit

Permalink
Revert "Revert 1.9.0 update."
Browse files Browse the repository at this point in the history
This reverts commit 641b301.
  • Loading branch information
gautamdhameja committed Mar 25, 2024
1 parent 641b301 commit adb5ead
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 58 deletions.
30 changes: 15 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,79 +8,79 @@ repository = 'https://github.com/gautamdhameja/substrate-validator-set'

[dev-dependencies.serde]
features = ['derive']
version = '1.0.197'
version = '1.0.188'

[dev-dependencies.sp-state-machine]
default-features = false
git = 'https://github.com/paritytech/polkadot-sdk.git'
tag = 'polkadot-v1.9.0'
tag = 'polkadot-v1.8.0'

[dependencies.sp-core]
default-features = false
git = 'https://github.com/paritytech/polkadot-sdk.git'
tag = 'polkadot-v1.9.0'
tag = 'polkadot-v1.8.0'

[dependencies.sp-io]
default-features = false
git = 'https://github.com/paritytech/polkadot-sdk.git'
tag = 'polkadot-v1.9.0'
tag = 'polkadot-v1.8.0'

[dependencies.sp-runtime]
default-features = false
git = 'https://github.com/paritytech/polkadot-sdk.git'
tag = 'polkadot-v1.9.0'
tag = 'polkadot-v1.8.0'

[dependencies.sp-std]
default-features = false
git = 'https://github.com/paritytech/polkadot-sdk.git'
tag = 'polkadot-v1.9.0'
tag = 'polkadot-v1.8.0'

[dependencies.sp-staking]
default-features = false
git = 'https://github.com/paritytech/polkadot-sdk.git'
tag = 'polkadot-v1.9.0'
tag = 'polkadot-v1.8.0'

[dependencies.log]
default-features = false
version = '0.4.20'
version = '0.4.17'

[dependencies.codec]
default-features = false
features = ['derive']
package = 'parity-scale-codec'
version = '3.6.1'
version = '3.0.0'

[dependencies.frame-benchmarking]
default-features = false
git = 'https://github.com/paritytech/polkadot-sdk.git'
optional = true
tag = 'polkadot-v1.9.0'
tag = 'polkadot-v1.8.0'

[dependencies.frame-support]
default-features = false
git = 'https://github.com/paritytech/polkadot-sdk.git'
tag = 'polkadot-v1.9.0'
tag = 'polkadot-v1.8.0'

[dependencies.frame-system]
default-features = false
git = 'https://github.com/paritytech/polkadot-sdk.git'
tag = 'polkadot-v1.9.0'
tag = 'polkadot-v1.8.0'

[dependencies.pallet-session]
default-features = false
features = ['historical']
git = 'https://github.com/paritytech/polkadot-sdk.git'
tag = 'polkadot-v1.9.0'
tag = 'polkadot-v1.8.0'

[dependencies.scale-info]
default-features = false
features = ['derive']
version = '2.10.0'
version = '2.1.1'

[dependencies.sp-weights]
default-features = false
git = 'https://github.com/paritytech/polkadot-sdk.git'
tag = 'polkadot-v1.9.0'
tag = 'polkadot-v1.8.0'

[features]
default = ['std']
Expand Down
5 changes: 3 additions & 2 deletions docs/im-online-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
[dependencies.pallet-im-online]
default-features = false
git = 'https://github.com/paritytech/polkadot-sdk.git'
tag = 'polkadot-v1.9.0'
tag = 'polkadot-v1.8.0'
```

```toml
Expand Down Expand Up @@ -116,6 +116,7 @@ impl pallet_im_online::Config for Runtime {
type WeightInfo = pallet_im_online::weights::SubstrateWeight<Runtime>;
type MaxKeys = MaxKeys;
type MaxPeerInHeartbeats = MaxPeerInHeartbeats;
type MaxPeerDataEncodingSize = MaxPeerDataEncodingSize;
}
```

Expand Down Expand Up @@ -149,7 +150,7 @@ construct_runtime!(
[dependencies.pallet-im-online]
default-features = false
git = 'https://github.com/paritytech/polkadot-sdk.git'
tag = 'polkadot-v1.9.0'
tag = 'polkadot-v1.8.0'
```

* Import `ImOnlineId` in the `chain_spec.rs`.
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

A [Substrate](https://github.com/paritytech/polkadot-sdk/tree/master/substrate#substrate) pallet to add/remove authorities/validators in PoA networks.

**Note: Current master is compatible with Substrate [polkadot-v1.9.0](https://github.com/paritytech/polkadot-sdk/tree/polkadot-v1.9.0) branch. For older versions, please see releases/tags.**
**Note: Current master is compatible with Substrate [polkadot-v1.8.0](https://github.com/paritytech/polkadot-sdk/tree/polkadot-v1.8.0) branch. For older versions, please see releases/tags.**

## Setup with Substrate Node Template

Expand All @@ -22,7 +22,7 @@ version = '1.1.0'
[dependencies.pallet-session]
default-features = false
git = 'https://github.com/paritytech/polkadot-sdk.git'
tag = 'polkadot-v1.9.0'
tag = 'polkadot-v1.8.0'
```

```toml
Expand Down
100 changes: 61 additions & 39 deletions src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

use super::*;
use crate as validator_set;
use std::collections::BTreeMap;
use frame_support::{derive_impl, parameter_types};
use frame_system::EnsureRoot;
use pallet_session::*;
use sp_core::crypto::key_types::DUMMY;
use sp_core::{crypto::key_types::DUMMY, H256};
use sp_runtime::{
impl_opaque_keys,
testing::UintAuthorityId,
traits::OpaqueKeys,
traits::{BlakeTwo256, IdentityLookup, OpaqueKeys},
BuildStorage, KeyTypeId, RuntimeAppPublic,
};
use sp_state_machine::BasicExternalities;
use std::cell::RefCell;

impl_opaque_keys! {
pub struct MockSessionKeys {
Expand Down Expand Up @@ -57,27 +57,23 @@ impl OpaqueKeys for PreUpgradeMockSessionKeys {
type Block = frame_system::mocking::MockBlock<Test>;

frame_support::construct_runtime!(
pub enum Test
{
pub struct Test {
System: frame_system,
ValidatorSet: validator_set,
Session: pallet_session,
}
);

parameter_types! {
pub static Validators: Vec<u64> = vec![1, 2, 3];
pub static NextValidators: Vec<u64> = vec![1, 2, 3];
pub static Authorities: Vec<UintAuthorityId> =
vec![UintAuthorityId(1), UintAuthorityId(2), UintAuthorityId(3)];
pub static ForceSessionEnd: bool = false;
pub static SessionLength: u64 = 2;
pub static SessionChanged: bool = false;
pub static TestSessionChanged: bool = false;
pub static Disabled: bool = false;
// Stores if `on_before_session_end` was called
pub static BeforeSessionEndCalled: bool = false;
pub static ValidatorAccounts: BTreeMap<u64, u64> = BTreeMap::new();
thread_local! {
pub static VALIDATORS: RefCell<Vec<u64>> = RefCell::new(vec![1, 2, 3]);
pub static NEXT_VALIDATORS: RefCell<Vec<u64>> = RefCell::new(vec![1, 2, 3]);
pub static AUTHORITIES: RefCell<Vec<UintAuthorityId>> =
RefCell::new(vec![UintAuthorityId(1), UintAuthorityId(2), UintAuthorityId(3)]);
pub static FORCE_SESSION_END: RefCell<bool> = RefCell::new(false);
pub static SESSION_LENGTH: RefCell<u64> = RefCell::new(2);
pub static SESSION_CHANGED: RefCell<bool> = RefCell::new(false);
pub static DISABLED: RefCell<bool> = RefCell::new(false);
pub static BEFORE_SESSION_END_CALLED: RefCell<bool> = RefCell::new(false);
}

pub struct TestSessionHandler;
Expand All @@ -89,66 +85,92 @@ impl SessionHandler<u64> for TestSessionHandler {
validators: &[(u64, T)],
_queued_validators: &[(u64, T)],
) {
SessionChanged::mutate(|l| *l = changed);
Authorities::mutate(|l| {
*l = validators
SESSION_CHANGED.with(|l| *l.borrow_mut() = changed);
AUTHORITIES.with(|l| {
*l.borrow_mut() = validators
.iter()
.map(|(_, id)| id.get::<UintAuthorityId>(DUMMY).unwrap_or_default())
.collect()
});
}
fn on_disabled(_validator_index: u32) {
Disabled::mutate(|l| *l = true)
DISABLED.with(|l| *l.borrow_mut() = true)
}
fn on_before_session_ending() {
BeforeSessionEndCalled::mutate(|b| *b = true);
BEFORE_SESSION_END_CALLED.with(|b| *b.borrow_mut() = true);
}
}

pub struct TestShouldEndSession;
impl ShouldEndSession<u64> for TestShouldEndSession {
fn should_end_session(now: u64) -> bool {
let l = SessionLength::get();
let l = SESSION_LENGTH.with(|l| *l.borrow());
now % l == 0 ||
ForceSessionEnd::mutate(|l| {
let r = *l;
*l = false;
FORCE_SESSION_END.with(|l| {
let r = *l.borrow();
*l.borrow_mut() = false;
r
})
}
}

pub fn authorities() -> Vec<UintAuthorityId> {
Authorities::get().to_vec()
AUTHORITIES.with(|l| l.borrow().to_vec())
}

pub fn new_test_ext() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
let keys: Vec<_> = NextValidators::get()
.iter()
.cloned()
.map(|i| (i, i, UintAuthorityId(i).into()))
.collect();
let keys: Vec<_> = NEXT_VALIDATORS
.with(|l| l.borrow().iter().cloned().map(|i| (i, i, UintAuthorityId(i).into())).collect());
BasicExternalities::execute_with_storage(&mut t, || {
for (ref k, ..) in &keys {
frame_system::Pallet::<Test>::inc_providers(k);
}
frame_system::Pallet::<Test>::inc_providers(&4);
// An additional identity that we use.
frame_system::Pallet::<Test>::inc_providers(&69);
});
pallet_session::GenesisConfig::<Test> { keys }
validator_set::GenesisConfig::<Test> {
initial_validators: keys.iter().map(|x| x.0).collect::<Vec<_>>(),
}
.assimilate_storage(&mut t)
.unwrap();
pallet_session::GenesisConfig::<Test> { keys: keys.clone() }
.assimilate_storage(&mut t)
.unwrap();

let v = NextValidators::get().iter().map(|&i| (i, i)).collect();
ValidatorAccounts::mutate(|m| *m = v);
sp_io::TestExternalities::new(t)
}

parameter_types! {
pub const BlockHashCount: u64 = 250;
pub BlockWeights: frame_system::limits::BlockWeights =
frame_system::limits::BlockWeights::simple_max(frame_support::weights::Weight::from_parts(1024, 0));
}

#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type RuntimeOrigin = RuntimeOrigin;
type Nonce = u64;
type RuntimeCall = RuntimeCall;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Block = Block;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}

parameter_types! {
Expand All @@ -163,7 +185,7 @@ impl validator_set::Config for Test {
}

impl pallet_session::Config for Test {
type ValidatorId = u64;
type ValidatorId = <Self as frame_system::Config>::AccountId;
type ValidatorIdOf = validator_set::ValidatorOf<Self>;
type ShouldEndSession = TestShouldEndSession;
type NextSessionRotation = ();
Expand Down

0 comments on commit adb5ead

Please sign in to comment.