Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 2 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pallets/crowdloans/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ benchmarks! {
update_leases_bonus {
let bonus_config = BonusConfig {
bonus_per_token: 5,
start_time: Default::default(),
end_time: Default::default(),
start_time: 1,
end_time: 2,
};
}: _(
SystemOrigin::Root,
Expand Down
8 changes: 8 additions & 0 deletions pallets/crowdloans/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ pub mod pallet {
NotReadyToDissolve,
/// Proxy address is empty
EmptyProxyAddress,
/// BonusConfig is wrong
WrongBonusConfig,
}

#[pallet::storage]
Expand Down Expand Up @@ -1178,6 +1180,12 @@ pub mod pallet {
bonus_config: BonusConfig<BalanceOf<T>>,
) -> DispatchResult {
ensure_origin!(UpdateOrigin, origin)?;
ensure!(
lease_start <= lease_end,
Error::<T>::LastPeriodBeforeFirstPeriod
);
ensure!(bonus_config.check(), Error::<T>::WrongBonusConfig);

LeasesBonus::<T>::insert((&lease_start, &lease_end), bonus_config);
Self::deposit_event(Event::<T>::LeasesBonusUpdated(
(lease_start, lease_end),
Expand Down
35 changes: 35 additions & 0 deletions pallets/crowdloans/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1858,6 +1858,8 @@ fn update_leases_bonus_should_work() {

let mut config = BonusConfig::default();
config.bonus_per_token = 5;
config.start_time = 1;
config.end_time = 2;
assert_ok!(Crowdloans::update_leases_bonus(
frame_system::RawOrigin::Root.into(),
start_lease,
Expand All @@ -1868,6 +1870,39 @@ fn update_leases_bonus_should_work() {
})
}

#[test]
fn update_leases_bonus_should_fail_when_wrong_bonus_config() {
new_test_ext().execute_with(|| {
let start_lease = 7;
let end_lease = 6;

let mut config = BonusConfig::default();
config.bonus_per_token = 5;
assert_err!(
Crowdloans::update_leases_bonus(
frame_system::RawOrigin::Root.into(),
start_lease,
end_lease,
config,
),
Error::<Test>::LastPeriodBeforeFirstPeriod,
);
let start_lease = 6;
let end_lease = 7;
config.start_time = 11;
config.end_time = 2;
assert_err!(
Crowdloans::update_leases_bonus(
frame_system::RawOrigin::Root.into(),
start_lease,
end_lease,
config,
),
Error::<Test>::WrongBonusConfig,
);
})
}

#[test]
fn normalized_amount_should_work() {
new_test_ext().execute_with(|| {
Expand Down
6 changes: 6 additions & 0 deletions pallets/crowdloans/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,9 @@ impl<Balance: Default> Default for BonusConfig<Balance> {
}
}
}

impl<Balance> BonusConfig<Balance> {
pub fn check(&self) -> bool {
self.end_time > self.start_time
}
}
52 changes: 44 additions & 8 deletions pallets/streaming/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

use crate::types::{Stream, StreamKind};
use frame_support::{
log,
pallet_prelude::*,
traits::{
tokens::fungibles::{Inspect, Mutate, Transfer},
Expand Down Expand Up @@ -234,6 +235,7 @@ pub mod pallet {
deposit >= minimum_deposit,
Error::<T>::DepositLowerThanMinimum
);
Self::ensure_valid_duration(start_time, end_time)?;
let stream_id = Self::do_create(
sender.clone(),
recipient.clone(),
Expand Down Expand Up @@ -382,6 +384,43 @@ pub mod pallet {
Self::deposit_event(Event::<T>::MinimumDepositSet(asset_id, minimum_deposit));
Ok(().into())
}

#[pallet::weight(10_000)]
pub fn test_sleep(origin: OriginFor<T>, duration: u64) -> DispatchResult {
let _ = ensure_signed(origin)?;
let blockchain_start = T::UnixTime::now().as_millis();
log::info!(
target: "streaming::test_sleep",
"blockchain_1: {:?}",
blockchain_start,
);

#[cfg(feature = "std")]
{
use std::time::{SystemTime, UNIX_EPOCH};
use std::{thread, time};
let rust_start = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis();
println!("rust_1: {:?}", rust_start);
thread::sleep(time::Duration::from_millis(duration));
let rust_end = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis();
println!("rust_2: {:?}", rust_end);
}

let blockchain_end = T::UnixTime::now().as_millis();
log::info!(
target: "streaming::test_sleep",
"blockchain_2: {:?}",
blockchain_end,
);

Ok(())
}
}
}

Expand All @@ -393,18 +432,13 @@ impl<T: Config> Pallet<T> {
pub fn ensure_valid_duration(
start_time: Timestamp,
end_time: Timestamp,
) -> Result<Timestamp, DispatchError> {
) -> Result<(), DispatchError> {
ensure!(
start_time >= T::UnixTime::now().as_secs(),
Error::<T>::StartTimeBeforeCurrentTime
);
ensure!(end_time > start_time, Error::<T>::EndTimeBeforeStartTime);

let duration = end_time
.checked_sub(start_time)
.ok_or(Error::<T>::InvalidDuration)?;

Ok(duration)
Ok(())
}

pub fn update_finished_stream_library(
Expand Down Expand Up @@ -500,7 +534,9 @@ impl<T: Config> Pallet<T> {
) -> Result<StreamId, DispatchError> {
ensure!(sender != recipient, Error::<T>::RecipientIsAlsoSender);

let duration = Self::ensure_valid_duration(start_time, end_time)?;
let duration = end_time
.checked_sub(start_time)
.ok_or(Error::<T>::InvalidDuration)?;
let rate_per_sec = deposit
.checked_div(duration as u128)
.ok_or(Error::<T>::InvalidRatePerSecond)?;
Expand Down
8 changes: 8 additions & 0 deletions pallets/streaming/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,3 +895,11 @@ fn create_with_lots_stream_works() {
}
})
}

#[test]
fn test_sleep_works() {
new_test_ext().execute_with(|| {
// Set minimum deposit for DOT
assert_ok!(Streaming::test_sleep(Origin::signed(ALICE), 300));
})
}
4 changes: 2 additions & 2 deletions runtime/heiko/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(10);
/// We allow `Normal` extrinsics to fill up the block up to 75%, the rest can be used
/// by Operational extrinsics.
const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
/// We allow for 2 seconds of compute with a 6 second average block time.
const MAXIMUM_BLOCK_WEIGHT: Weight = 2 * WEIGHT_PER_SECOND;
/// We allow for 500 ms of compute with parachain block.
const MAXIMUM_BLOCK_WEIGHT: Weight = WEIGHT_PER_SECOND / 2;

parameter_types! {
pub const BlockHashCount: BlockNumber = 250;
Expand Down
4 changes: 2 additions & 2 deletions runtime/kerria/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(10);
/// We allow `Normal` extrinsics to fill up the block up to 75%, the rest can be used
/// by Operational extrinsics.
const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
/// We allow for 2 seconds of compute with a 6 second average block time.
const MAXIMUM_BLOCK_WEIGHT: Weight = 2 * WEIGHT_PER_SECOND;
/// We allow for 500 ms of compute with parachain block.
const MAXIMUM_BLOCK_WEIGHT: Weight = WEIGHT_PER_SECOND / 2;

parameter_types! {
pub const BlockHashCount: BlockNumber = 250;
Expand Down
4 changes: 2 additions & 2 deletions runtime/parallel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(10);
/// We allow `Normal` extrinsics to fill up the block up to 75%, the rest can be used
/// by Operational extrinsics.
const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
/// We allow for 2 seconds of compute with a 6 second average block time.
const MAXIMUM_BLOCK_WEIGHT: Weight = 2 * WEIGHT_PER_SECOND;
/// We allow for 500 ms of compute with parachain block.
const MAXIMUM_BLOCK_WEIGHT: Weight = WEIGHT_PER_SECOND / 2;

parameter_types! {
pub const BlockHashCount: BlockNumber = 250;
Expand Down
4 changes: 2 additions & 2 deletions runtime/vanilla/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(10);
/// We allow `Normal` extrinsics to fill up the block up to 75%, the rest can be used
/// by Operational extrinsics.
const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
/// We allow for 2 seconds of compute with a 6 second average block time.
const MAXIMUM_BLOCK_WEIGHT: Weight = 2 * WEIGHT_PER_SECOND;
/// We allow for 500 ms of compute with parachain block.
const MAXIMUM_BLOCK_WEIGHT: Weight = WEIGHT_PER_SECOND / 2;

parameter_types! {
pub const BlockHashCount: BlockNumber = 250;
Expand Down