Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Update substrate for new on_initialize syntax and update on_initializ…
Browse files Browse the repository at this point in the history
…e weight (#937)

* update new on_initialize syntax

* update substrate

* update spec_version
  • Loading branch information
gui1117 authored Mar 25, 2020
1 parent c837f9d commit 5f37995
Show file tree
Hide file tree
Showing 8 changed files with 1,419 additions and 1,288 deletions.
2,662 changes: 1,390 additions & 1,272 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions runtime/common/src/crowdfund.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,15 +570,18 @@ mod tests {
use super::*;

use std::{collections::HashMap, cell::RefCell};
use frame_support::{impl_outer_origin, assert_ok, assert_noop, parameter_types};
use frame_support::{
impl_outer_origin, assert_ok, assert_noop, parameter_types,
traits::{OnInitialize, OnFinalize},
};
use frame_support::traits::Contains;
use sp_core::H256;
use primitives::parachain::{Info as ParaInfo, Id as ParaId};
// The testing primitives are very useful for avoiding having to work with signatures
// or public keys. `u64` is used as the `AccountId` and no `Signature`s are requried.
use sp_runtime::{
Perbill, Permill, Percent, testing::Header, DispatchResult,
traits::{BlakeTwo256, OnInitialize, OnFinalize, IdentityLookup},
traits::{BlakeTwo256, IdentityLookup},
};
use crate::registrar::Registrar;

Expand Down
9 changes: 6 additions & 3 deletions runtime/common/src/parachains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use sp_staking::{
use frame_support::{
traits::KeyOwnerProofSystem,
dispatch::{IsSubType},
weights::{DispatchInfo, SimpleDispatchInfo},
weights::{DispatchInfo, SimpleDispatchInfo, Weight, WeighData},
};
use primitives::{
Balance,
Expand Down Expand Up @@ -516,7 +516,7 @@ decl_module! {
Ok(())
}

fn on_initialize() {
fn on_initialize() -> Weight {
<Self as Store>::DidUpdate::kill();

let current_session = <session::Module<T>>::current_index();
Expand All @@ -538,6 +538,8 @@ decl_module! {
}
}
<ParentToSessionIndex<T>>::insert(parent_hash, current_session);

SimpleDispatchInfo::default().weigh_data(())
}

fn on_finalize() {
Expand Down Expand Up @@ -1204,7 +1206,7 @@ mod tests {
impl_opaque_keys,
Perbill, curve::PiecewiseLinear, testing::{Header},
traits::{
BlakeTwo256, IdentityLookup, OnInitialize, OnFinalize, SaturatedConversion,
BlakeTwo256, IdentityLookup, SaturatedConversion,
OpaqueKeys,
},
};
Expand All @@ -1218,6 +1220,7 @@ mod tests {
use keyring::Sr25519Keyring;
use frame_support::{
impl_outer_origin, impl_outer_dispatch, assert_ok, assert_err, parameter_types,
traits::{OnInitialize, OnFinalize},
};
use crate::parachains;
use crate::registrar;
Expand Down
10 changes: 6 additions & 4 deletions runtime/common/src/registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use sp_runtime::{
use frame_support::{
decl_storage, decl_module, decl_event, decl_error, ensure,
dispatch::{DispatchResult, IsSubType}, traits::{Get, Currency, ReservableCurrency},
weights::{SimpleDispatchInfo, DispatchInfo},
weights::{SimpleDispatchInfo, DispatchInfo, Weight, WeighData},
};
use system::{self, ensure_root, ensure_signed};
use primitives::parachain::{
Expand Down Expand Up @@ -413,7 +413,7 @@ decl_module! {
}

/// Block initializer. Clears SelectedThreads and constructs/replaces Active.
fn on_initialize() {
fn on_initialize() -> Weight {
let next_up = SelectedThreads::mutate(|t| {
let r = if t.len() >= T::QueueSize::get() {
// Take the first set of parathreads in queue
Expand Down Expand Up @@ -453,6 +453,8 @@ decl_module! {
paras.sort_by_key(|&(ref id, _)| *id);

Active::put(paras);

SimpleDispatchInfo::default().weigh_data(())
}

fn on_finalize() {
Expand Down Expand Up @@ -644,7 +646,7 @@ mod tests {
use sp_core::{H256, Pair};
use sp_runtime::{
traits::{
BlakeTwo256, IdentityLookup, OnInitialize, OnFinalize, Dispatchable,
BlakeTwo256, IdentityLookup, Dispatchable,
AccountIdConversion,
}, testing::{UintAuthorityId, Header}, KeyTypeId, Perbill, curve::PiecewiseLinear,
};
Expand All @@ -657,7 +659,7 @@ mod tests {
Balance, BlockNumber,
};
use frame_support::{
traits::KeyOwnerProofSystem,
traits::{KeyOwnerProofSystem, OnInitialize, OnFinalize},
impl_outer_origin, impl_outer_dispatch, assert_ok, parameter_types, assert_noop,
};
use keyring::Sr25519Keyring;
Expand Down
13 changes: 9 additions & 4 deletions runtime/common/src/slots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ use sp_std::{prelude::*, mem::swap, convert::TryInto};
use sp_runtime::traits::{
CheckedSub, StaticLookup, Zero, One, CheckedConversion, Hash, AccountIdConversion,
};
use frame_support::weights::SimpleDispatchInfo;
use codec::{Encode, Decode, Codec};
use frame_support::{
decl_module, decl_storage, decl_event, decl_error, ensure, dispatch::DispatchResult,
traits::{Currency, ReservableCurrency, WithdrawReason, ExistenceRequirement, Get, Randomness},
weights::{SimpleDispatchInfo, WeighData, Weight},
};
use primitives::parachain::{
SwapAux, PARACHAIN_INFO, Id as ParaId
Expand Down Expand Up @@ -267,7 +267,7 @@ decl_module! {

fn deposit_event() = default;

fn on_initialize(n: T::BlockNumber) {
fn on_initialize(n: T::BlockNumber) -> Weight {
let lease_period = T::LeasePeriod::get();
let lease_period_index: LeasePeriodOf<T> = (n / lease_period).into();

Expand All @@ -285,6 +285,8 @@ decl_module! {
if (n % lease_period).is_zero() {
Self::manage_lease_period_start(lease_period_index);
}

SimpleDispatchInfo::default().weigh_data(())
}

fn on_finalize(now: T::BlockNumber) {
Expand Down Expand Up @@ -877,9 +879,12 @@ mod tests {
use sp_core::H256;
use sp_runtime::{
Perbill, testing::Header,
traits::{BlakeTwo256, Hash, IdentityLookup, OnInitialize, OnFinalize},
traits::{BlakeTwo256, Hash, IdentityLookup},
};
use frame_support::{
impl_outer_origin, parameter_types, assert_ok, assert_noop,
traits::{OnInitialize, OnFinalize}
};
use frame_support::{impl_outer_origin, parameter_types, assert_ok, assert_noop};
use balances;
use primitives::parachain::{Id as ParaId, Info as ParaInfo};

Expand Down
2 changes: 1 addition & 1 deletion runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("kusama"),
impl_name: create_runtime_str!("parity-kusama"),
authoring_version: 2,
spec_version: 1056,
spec_version: 1057,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
};
Expand Down
2 changes: 1 addition & 1 deletion runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("polkadot"),
impl_name: create_runtime_str!("parity-polkadot"),
authoring_version: 2,
spec_version: 1005,
spec_version: 1006,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
};
Expand Down
2 changes: 1 addition & 1 deletion runtime/test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("polkadot-test-runtime"),
impl_name: create_runtime_str!("parity-polkadot-test-runtime"),
authoring_version: 2,
spec_version: 1048,
spec_version: 1049,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
};
Expand Down

0 comments on commit 5f37995

Please sign in to comment.