Skip to content

Commit

Permalink
fix: update runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
nakul1010 committed Aug 8, 2023
1 parent 1646b7f commit 79a25f5
Show file tree
Hide file tree
Showing 52 changed files with 1,070 additions and 988 deletions.
723 changes: 372 additions & 351 deletions Cargo.lock

Large diffs are not rendered by default.

524 changes: 262 additions & 262 deletions Cargo.toml

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions crates/annuity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use frame_support::{
weights::Weight,
PalletId,
};
use frame_system::pallet_prelude::BlockNumberFor;
use sp_runtime::traits::{AccountIdConversion, CheckedDiv, Convert, Saturating};
use sp_std::cmp::min;

Expand Down Expand Up @@ -55,11 +56,11 @@ pub mod pallet {
type BlockRewardProvider: BlockRewardProvider<Self::AccountId, Currency = Self::Currency>;

/// Convert the block number into a balance.
type BlockNumberToBalance: Convert<Self::BlockNumber, BalanceOf<Self, I>>;
type BlockNumberToBalance: Convert<BlockNumberFor<Self>, BalanceOf<Self, I>>;

/// The emission period for block rewards.
#[pallet::constant]
type EmissionPeriod: Get<Self::BlockNumber>;
type EmissionPeriod: Get<BlockNumberFor<Self>>;

/// The total amount of the wrapped asset.
type TotalWrapped: Get<BalanceOf<Self, I>>;
Expand All @@ -79,8 +80,8 @@ pub mod pallet {
pub enum Error<T, I = ()> {}

#[pallet::hooks]
impl<T: Config<I>, I: 'static> Hooks<T::BlockNumber> for Pallet<T, I> {
fn on_initialize(n: T::BlockNumber) -> Weight {
impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {
fn on_initialize(n: BlockNumberFor<T>) -> Weight {
if let Err(e) = Self::begin_block(n) {
sp_runtime::print(e);
}
Expand Down Expand Up @@ -154,7 +155,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
}
}

pub(crate) fn begin_block(_height: T::BlockNumber) -> DispatchResult {
pub(crate) fn begin_block(_height: BlockNumberFor<T>) -> DispatchResult {
let reward_per_block = Self::min_reward_per_block();
Self::deposit_event(Event::<T, I>::BlockReward(reward_per_block));
T::BlockRewardProvider::distribute_block_reward(&Self::account_id(), reward_per_block)
Expand Down
9 changes: 5 additions & 4 deletions crates/btc-relay/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ use mocktopus::macros::mockable;
#[cfg_attr(test, mockable)]
pub(crate) mod security {
use frame_support::dispatch::DispatchError;
use frame_system::pallet_prelude::BlockNumberFor;

#[cfg(feature = "runtime-benchmarks")]
pub fn set_active_block_number<T: crate::Config>(n: T::BlockNumber) {
pub fn set_active_block_number<T: crate::Config>(n: BlockNumberFor<T>) {
<security::Pallet<T>>::set_active_block_number(n)
}

pub fn active_block_number<T: crate::Config>() -> T::BlockNumber {
pub fn active_block_number<T: crate::Config>() -> BlockNumberFor<T> {
<security::Pallet<T>>::active_block_number()
}

pub fn parachain_block_expired<T: crate::Config>(
opentime: T::BlockNumber,
period: T::BlockNumber,
opentime: BlockNumberFor<T>,
period: BlockNumberFor<T>,
) -> Result<bool, DispatchError> {
<security::Pallet<T>>::parachain_block_expired(opentime, period)
}
Expand Down
39 changes: 20 additions & 19 deletions crates/btc-relay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ pub mod pallet {
type WeightInfo: WeightInfo;

#[pallet::constant]
type ParachainBlocksPerBitcoinBlock: Get<<Self as frame_system::Config>::BlockNumber>;
type ParachainBlocksPerBitcoinBlock: Get<BlockNumberFor<Self>>;
}

#[pallet::hooks]
impl<T: Config> Hooks<T::BlockNumber> for Pallet<T> {}
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}

#[pallet::call]
impl<T: Config> Pallet<T> {
Expand Down Expand Up @@ -323,7 +323,7 @@ pub mod pallet {
/// Store Bitcoin block headers
#[pallet::storage]
pub(super) type BlockHeaders<T: Config> =
StorageMap<_, Blake2_128Concat, H256Le, RichBlockHeader<T::BlockNumber>, ValueQuery>;
StorageMap<_, Blake2_128Concat, H256Le, RichBlockHeader<BlockNumberFor<T>>, ValueQuery>;

/// Priority queue of BlockChain elements, ordered by the maximum height (descending).
/// The first index into this mapping (0) is considered to be the longest chain. The value
Expand Down Expand Up @@ -366,7 +366,7 @@ pub mod pallet {
/// Global security parameter k for stable Parachain transactions
#[pallet::storage]
#[pallet::getter(fn parachain_confirmations)]
pub(super) type StableParachainConfirmations<T: Config> = StorageValue<_, T::BlockNumber, ValueQuery>;
pub(super) type StableParachainConfirmations<T: Config> = StorageValue<_, BlockNumberFor<T>, ValueQuery>;

/// Whether the module should perform difficulty checks.
#[pallet::storage]
Expand All @@ -384,15 +384,15 @@ pub mod pallet {
/// Global security parameter k for stable Bitcoin transactions
pub bitcoin_confirmations: u32,
/// Global security parameter k for stable Parachain transactions
pub parachain_confirmations: T::BlockNumber,
pub parachain_confirmations: BlockNumberFor<T>,
/// Whether the module should perform difficulty checks.
pub disable_difficulty_check: bool,
/// Whether the module should perform inclusion checks.
pub disable_inclusion_check: bool,
}

#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {
StableBitcoinConfirmations::<T>::put(self.bitcoin_confirmations);
StableParachainConfirmations::<T>::put(self.parachain_confirmations);
Expand Down Expand Up @@ -424,6 +424,7 @@ pub const UNROUNDED_MAX_TARGET: U256 = U256([

/// Main chain id
pub const MAIN_CHAIN_ID: u32 = 0;
use frame_system::pallet_prelude::BlockNumberFor;

#[cfg_attr(test, mockable)]
impl<T: Config> Pallet<T> {
Expand Down Expand Up @@ -695,20 +696,20 @@ impl<T: Config> Pallet<T> {
}

pub fn has_request_expired(
opentime: T::BlockNumber,
opentime: BlockNumberFor<T>,
btc_open_height: u32,
period: T::BlockNumber,
period: BlockNumberFor<T>,
) -> Result<bool, DispatchError> {
Ok(ext::security::parachain_block_expired::<T>(opentime, period)?
&& Self::bitcoin_block_expired(btc_open_height, period)?)
}

pub fn bitcoin_expiry_height(btc_open_height: u32, period: T::BlockNumber) -> Result<u32, DispatchError> {
pub fn bitcoin_expiry_height(btc_open_height: u32, period: BlockNumberFor<T>) -> Result<u32, DispatchError> {
// calculate num_bitcoin_blocks as ceil(period / ParachainBlocksPerBitcoinBlock)
let num_bitcoin_blocks: u32 = period
.checked_add(&T::ParachainBlocksPerBitcoinBlock::get())
.ok_or(Error::<T>::ArithmeticOverflow)?
.checked_sub(&T::BlockNumber::one())
.checked_sub(&BlockNumberFor::<T>::one())
.ok_or(Error::<T>::ArithmeticUnderflow)?
.checked_div(&T::ParachainBlocksPerBitcoinBlock::get())
.ok_or(Error::<T>::ArithmeticUnderflow)?
Expand All @@ -720,7 +721,7 @@ impl<T: Config> Pallet<T> {
.ok_or(Error::<T>::ArithmeticOverflow)?)
}

pub fn bitcoin_block_expired(btc_open_height: u32, period: T::BlockNumber) -> Result<bool, DispatchError> {
pub fn bitcoin_block_expired(btc_open_height: u32, period: BlockNumberFor<T>) -> Result<bool, DispatchError> {
let expiration_height = Self::bitcoin_expiry_height(btc_open_height, period)?;

// Note that we check stictly greater than. This ensures that at least
Expand Down Expand Up @@ -786,7 +787,7 @@ impl<T: Config> Pallet<T> {
}

/// Get a block header from its hash
fn get_block_header_from_hash(block_hash: H256Le) -> Result<RichBlockHeader<T::BlockNumber>, DispatchError> {
fn get_block_header_from_hash(block_hash: H256Le) -> Result<RichBlockHeader<BlockNumberFor<T>>, DispatchError> {
BlockHeaders::<T>::try_get(block_hash).or(Err(Error::<T>::BlockNotFound.into()))
}

Expand All @@ -799,7 +800,7 @@ impl<T: Config> Pallet<T> {
fn get_block_header_from_height(
blockchain: &BlockChain,
block_height: u32,
) -> Result<RichBlockHeader<T::BlockNumber>, DispatchError> {
) -> Result<RichBlockHeader<BlockNumberFor<T>>, DispatchError> {
let block_hash = Self::get_block_hash(blockchain.chain_id, block_height)?;
Self::get_block_header_from_hash(block_hash)
}
Expand Down Expand Up @@ -827,7 +828,7 @@ impl<T: Config> Pallet<T> {
}

/// Set a new block header
fn set_block_header_from_hash(hash: H256Le, header: &RichBlockHeader<T::BlockNumber>) {
fn set_block_header_from_hash(hash: H256Le, header: &RichBlockHeader<BlockNumberFor<T>>) {
BlockHeaders::<T>::insert(hash, header);
}

Expand Down Expand Up @@ -941,7 +942,7 @@ impl<T: Config> Pallet<T> {
fn verify_block_header(
block_header: &BlockHeader,
block_height: u32,
prev_block_header: RichBlockHeader<T::BlockNumber>,
prev_block_header: RichBlockHeader<BlockNumberFor<T>>,
) -> Result<(), DispatchError> {
// Check that the block header is not yet stored in BTC-Relay
ensure!(
Expand Down Expand Up @@ -975,7 +976,7 @@ impl<T: Config> Pallet<T> {
/// * `prev_block_header`: previous block header
/// * `block_height` : block height of new target
fn compute_new_target(
prev_block_header: &RichBlockHeader<T::BlockNumber>,
prev_block_header: &RichBlockHeader<BlockNumberFor<T>>,
block_height: u32,
) -> Result<U256, DispatchError> {
// time of last retarget (first block in current difficulty period)
Expand Down Expand Up @@ -1099,7 +1100,7 @@ impl<T: Config> Pallet<T> {
/// Transfers the given block to the main chain. If this would overwrite a block already in the
/// main chain, then the overwritten block is moved to to `chain_id_for_old_main_blocks`.
fn swap_block_to_mainchain(
block: RichBlockHeader<T::BlockNumber>,
block: RichBlockHeader<BlockNumberFor<T>>,
chain_id_for_old_main_blocks: u32,
) -> Result<(), DispatchError> {
let block_height = block.block_height;
Expand Down Expand Up @@ -1128,7 +1129,7 @@ impl<T: Config> Pallet<T> {
// returns (child, parent)
fn enumerate_chain_links(
start: H256Le,
) -> impl Iterator<Item = Result<(RichBlockHeader<T::BlockNumber>, RichBlockHeader<T::BlockNumber>), DispatchError>>
) -> impl Iterator<Item = Result<(RichBlockHeader<BlockNumberFor<T>>, RichBlockHeader<BlockNumberFor<T>>), DispatchError>>
{
let child = Self::get_block_header_from_hash(start);

Expand Down Expand Up @@ -1306,7 +1307,7 @@ impl<T: Config> Pallet<T> {
/// # Arguments
///
/// * `para_height` - height of the parachain when the block was stored
pub fn check_parachain_confirmations(para_height: T::BlockNumber) -> Result<(), DispatchError> {
pub fn check_parachain_confirmations(para_height: BlockNumberFor<T>) -> Result<(), DispatchError> {
let current_height = ext::security::active_block_number::<T>();

ensure!(
Expand Down
7 changes: 4 additions & 3 deletions crates/collator-selection/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ fn register_candidates<T: Config>(count: u32) {
)]
pub mod benchmarks {
use super::*;
use frame_system::pallet_prelude::BlockNumberFor;

#[benchmark]
fn set_invulnerables(b: Linear<1, 100>) {
Expand Down Expand Up @@ -202,7 +203,7 @@ pub mod benchmarks {
<CandidacyBond<T>>::put(BalanceOf::<T>::max_value() / 4u32.into());
T::RewardsCurrency::make_free_balance_be(&<CollatorSelection<T>>::account_id(), 2000u32.into());
let author = account("author", 0, SEED);
let new_block: T::BlockNumber = 10u32.into();
let new_block: BlockNumberFor<T> = 10u32.into();

frame_system::Pallet::<T>::set_block_number(new_block);
assert!(T::RewardsCurrency::free_balance(&author) == 0u32.into());
Expand All @@ -226,8 +227,8 @@ pub mod benchmarks {
register_validators::<T>(c);
register_candidates::<T>(c);

let new_block: T::BlockNumber = 1800u32.into();
let zero_block: T::BlockNumber = 0u32.into();
let new_block: BlockNumberFor<T> = 1800u32.into();
let zero_block: BlockNumberFor<T> = 0u32.into();
let candidates = <Candidates<T>>::get();

let non_removals = c.saturating_sub(r);
Expand Down
8 changes: 4 additions & 4 deletions crates/collator-selection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub mod pallet {
type MaxInvulnerables: Get<u32>;

// Will be kicked if block is not produced in threshold.
type KickThreshold: Get<Self::BlockNumber>;
type KickThreshold: Get<BlockNumberFor<Self>>;

/// A stable ID for a validator.
type ValidatorId: Member + Parameter;
Expand Down Expand Up @@ -160,7 +160,7 @@ pub mod pallet {
/// Last block authored by collator.
#[pallet::storage]
#[pallet::getter(fn last_authored_block)]
pub type LastAuthoredBlock<T: Config> = StorageMap<_, Twox64Concat, T::AccountId, T::BlockNumber, ValueQuery>;
pub type LastAuthoredBlock<T: Config> = StorageMap<_, Twox64Concat, T::AccountId, BlockNumberFor<T>, ValueQuery>;

/// Desired number of candidates.
///
Expand All @@ -185,7 +185,7 @@ pub mod pallet {
}

#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {
let duplicate_invulnerables = self
.invulnerables
Expand Down Expand Up @@ -458,7 +458,7 @@ pub mod pallet {

/// Keep track of number of authored blocks per authority, uncles are counted as well since
/// they're a valid proof of being online.
impl<T: Config + pallet_authorship::Config> pallet_authorship::EventHandler<T::AccountId, T::BlockNumber>
impl<T: Config + pallet_authorship::Config> pallet_authorship::EventHandler<T::AccountId, BlockNumberFor<T>>
for Pallet<T>
{
fn note_author(author: T::AccountId) {
Expand Down
Loading

0 comments on commit 79a25f5

Please sign in to comment.