Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
a83059a
Completely rework dispatch mechanism into something modular.
gavofyork Mar 13, 2018
82a178d
Council vote tests.
gavofyork Mar 13, 2018
1d28d9a
Fix tests.
gavofyork Mar 13, 2018
69c88c3
whitespace.
gavofyork Mar 13, 2018
b12a708
Fix demo runtime tests.
gavofyork Mar 14, 2018
e48e86d
Merge branch 'gav-demo' into gav-dispatch
gavofyork Mar 14, 2018
8e3cc51
Fix up tests.
gavofyork Mar 14, 2018
53e2fdf
Merge branch 'gav-demo' into gav-dispatch
gavofyork Mar 14, 2018
27ecd6f
Merge branch 'master' into gav-dispatch
gavofyork Mar 14, 2018
5eca74a
Remove dead code.
gavofyork Mar 14, 2018
7cece3b
Merge branch 'master' into gav-dispatch
gavofyork Mar 14, 2018
8c2396d
Timestamp uses new storage API.
gavofyork Mar 14, 2018
6b6c240
Move over system module to new API.
gavofyork Mar 14, 2018
a79dab2
Much nicer storage API, moved over staking module.
gavofyork Mar 15, 2018
1fd6b3e
More refactoring.
gavofyork Mar 15, 2018
51b4a8c
Democracy uses new storage API.
gavofyork Mar 15, 2018
8ada9f7
Council uses new RPC.
gavofyork Mar 16, 2018
c4f5f42
Fix more tests.
gavofyork Mar 16, 2018
d11f5ca
Use match for Id
gavofyork Mar 16, 2018
faa0a44
Use match for Id
gavofyork Mar 16, 2018
7912de1
Make PrivPass better protected.
gavofyork Mar 19, 2018
aac3899
Address other grumbles.
gavofyork Mar 19, 2018
26519c6
Give PrivPass a private member.
gavofyork Mar 19, 2018
9f32ea7
Testing PrivPass.
gavofyork Mar 19, 2018
8966951
Add docs.
gavofyork Mar 19, 2018
91d5e75
Merge branch 'gav-dispatch' into gav-storage-revamp
gavofyork Mar 19, 2018
7ee81e6
Recompile binaries after merge.
gavofyork Mar 19, 2018
33215e4
Merge branch 'master' into gav-storage-revamp
gavofyork Mar 19, 2018
cf9659e
Remove duplicated code.
gavofyork Mar 19, 2018
b8c0832
New binaries.
gavofyork Mar 19, 2018
57a9a79
Docs
gavofyork Mar 19, 2018
59c7a1a
Docs
gavofyork Mar 19, 2018
6f8f456
avoid use of (arguably) confusing terminology.
gavofyork Mar 19, 2018
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
Prev Previous commit
Next Next commit
Fix tests.
  • Loading branch information
gavofyork committed Mar 13, 2018
commit 1d28d9a97af09661f9e5c09fbab3d2c2067c7a86
9 changes: 6 additions & 3 deletions demo/runtime/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,9 @@ impl_meta_dispatch! {
pub mod public;
path public;
trait staking::PublicPass;
Staking(mod staking) = 0;
Timestamp(mod timestamp) = 1;
Session(mod session) = 1;
Staking(mod staking) = 2;
Timestamp(mod timestamp) = 3;
Democracy(mod democracy) = 5;
Council(mod council) = 6;
CouncilVote(mod council) = 7;
Expand All @@ -235,7 +236,9 @@ impl_meta_dispatch! {
pub mod privileged;
path privileged;
trait system::PrivPass;
Staking(mod staking) = 0;
System(mod system) = 0;
Session(mod session) = 1;
Staking(mod staking) = 2;
Democracy(mod democracy) = 5;
Council(mod council) = 6;
CouncilVote(mod council) = 7;
Expand Down
73 changes: 41 additions & 32 deletions demo/runtime/src/runtime/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use codec::KeyedVec;
use runtime_support::{storage, StorageVec};
use demo_primitives::{AccountId, SessionKey, BlockNumber};
use runtime::{system, staking, consensus};
use runtime::system::PrivPass;
use runtime::staking::PublicPass;

pub const SESSION_LENGTH: &[u8] = b"ses:len";
pub const CURRENT_INDEX: &[u8] = b"ses:ind";
Expand Down Expand Up @@ -62,28 +64,35 @@ pub fn last_length_change() -> BlockNumber {
storage::get_or(LAST_LENGTH_CHANGE, 0)
}

pub mod public {
use super::*;
impl_dispatch! {
pub mod public;
fn set_key(key: SessionKey) = 0;
}

impl<'a> public::Dispatch for PublicPass<'a> {
/// Sets the session key of `_validator` to `_key`. This doesn't take effect until the next
/// session.
pub fn set_key(validator: &AccountId, key: &SessionKey) {
fn set_key(self, key: SessionKey) {
// set new value for next session
storage::put(&validator.to_keyed_vec(NEXT_KEY_FOR), key);
storage::put(&self.to_keyed_vec(NEXT_KEY_FOR), &key);
}
}

pub mod privileged {
use super::*;
impl_dispatch! {
pub mod privileged;
fn set_length(new: BlockNumber) = 0;
fn force_new_session() = 1;
}

impl privileged::Dispatch for PrivPass {
/// Set a new era length. Won't kick in until the next era change (at current length).
pub fn set_length(new: BlockNumber) {
fn set_length(self, new: BlockNumber) {
storage::put(NEXT_SESSION_LENGTH, &new);
}

/// Forces a new session.
pub fn force_new_session() {
rotate_session();
fn force_new_session(self) {
internal::rotate_session();
}
}

Expand All @@ -110,27 +119,27 @@ pub mod internal {
rotate_session();
}
}
}

/// Move onto next session: register the new authority set.
fn rotate_session() {
// Increment current session index.
storage::put(CURRENT_INDEX, &(current_index() + 1));
/// Move onto next session: register the new authority set.
pub fn rotate_session() {
// Increment current session index.
storage::put(CURRENT_INDEX, &(current_index() + 1));

// Enact era length change.
if let Some(next_len) = storage::get::<u64>(NEXT_SESSION_LENGTH) {
storage::put(SESSION_LENGTH, &next_len);
storage::put(LAST_LENGTH_CHANGE, &system::block_number());
storage::kill(NEXT_SESSION_LENGTH);
}

// Update any changes in session keys.
validators().iter().enumerate().for_each(|(i, v)| {
let k = v.to_keyed_vec(NEXT_KEY_FOR);
if let Some(n) = storage::take(&k) {
consensus::internal::set_authority(i as u32, &n);
// Enact era length change.
if let Some(next_len) = storage::get::<u64>(NEXT_SESSION_LENGTH) {
storage::put(SESSION_LENGTH, &next_len);
storage::put(LAST_LENGTH_CHANGE, &system::block_number());
storage::kill(NEXT_SESSION_LENGTH);
}
});

// Update any changes in session keys.
validators().iter().enumerate().for_each(|(i, v)| {
let k = v.to_keyed_vec(NEXT_KEY_FOR);
if let Some(n) = storage::take(&k) {
consensus::internal::set_authority(i as u32, &n);
}
});
}
}

#[cfg(any(feature = "std", test))]
Expand Down Expand Up @@ -161,7 +170,7 @@ pub mod testing {
mod tests {
use super::*;
use super::public::*;
use super::privileged::*;
use super::privileged::Dispatch as PrivDispatch;
use super::internal::*;
use runtime_io::{with_externalities, twox_128, TestExternalities};
use codec::{KeyedVec, Joiner};
Expand Down Expand Up @@ -200,14 +209,14 @@ mod tests {
with_externalities(&mut t, || {
// Block 1: Change to length 3; no visible change.
with_env(|e| e.block_number = 1);
set_length(3);
PrivPass.set_length(3);
check_rotate_session();
assert_eq!(length(), 2);
assert_eq!(current_index(), 0);

// Block 2: Length now changed to 3. Index incremented.
with_env(|e| e.block_number = 2);
set_length(3);
PrivPass.set_length(3);
check_rotate_session();
assert_eq!(length(), 3);
assert_eq!(current_index(), 1);
Expand All @@ -220,7 +229,7 @@ mod tests {

// Block 4: Change to length 2; no visible change.
with_env(|e| e.block_number = 4);
set_length(2);
PrivPass.set_length(2);
check_rotate_session();
assert_eq!(length(), 3);
assert_eq!(current_index(), 1);
Expand Down Expand Up @@ -261,7 +270,7 @@ mod tests {

// Block 3: Set new key for validator 2; no visible change.
with_env(|e| e.block_number = 3);
set_key(&[20; 32], &[22; 32]);
PublicPass::test(&[20; 32]).set_key([22; 32]);
assert_eq!(consensus::authorities(), vec![[11u8; 32], [21u8; 32]]);

check_rotate_session();
Expand Down
2 changes: 1 addition & 1 deletion demo/runtime/src/runtime/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl privileged::Dispatch for system::PrivPass {
/// Force there to be a new era. This also forces a new session immediately after.
fn force_new_era(self) {
new_era();
session::privileged::force_new_session();
session::internal::rotate_session();
}
}

Expand Down
11 changes: 7 additions & 4 deletions demo/runtime/src/runtime/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ pub fn block_hash(number: BlockNumber) -> Hash {

pub struct PrivPass;

pub mod privileged {
use super::*;
impl_dispatch! {
pub mod privileged;
fn set_code(new: Vec<u8>) = 0;
}

impl privileged::Dispatch for PrivPass {
/// Set the new code.
pub fn set_code(new: &[u8]) {
storage::unhashed::put_raw(b":code", new);
fn set_code(self, new: Vec<u8>) {
storage::unhashed::put_raw(b":code", &new);
}
}

Expand Down