diff --git a/Cargo.lock b/Cargo.lock index 0a631959e..00d595772 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12662,9 +12662,9 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" [[package]] name = "tokio" -version = "1.21.1" +version = "1.21.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0020c875007ad96677dcc890298f4b942882c5d4eb7cc8f439fc3bf813dc9c95" +checksum = "a9e03c497dc955702ba729190dc4aac6f2a0ce97f913e5b1b5912fc5039d9099" dependencies = [ "autocfg", "bytes", @@ -12672,7 +12672,6 @@ dependencies = [ "memchr", "mio", "num_cpus", - "once_cell", "parking_lot 0.12.1", "pin-project-lite 0.2.9", "signal-hook-registry", diff --git a/pallets/crowdloans/src/benchmarking.rs b/pallets/crowdloans/src/benchmarking.rs index 7c5bcd8f9..023ba84f9 100644 --- a/pallets/crowdloans/src/benchmarking.rs +++ b/pallets/crowdloans/src/benchmarking.rs @@ -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, diff --git a/pallets/crowdloans/src/lib.rs b/pallets/crowdloans/src/lib.rs index 042458e04..1a998a023 100644 --- a/pallets/crowdloans/src/lib.rs +++ b/pallets/crowdloans/src/lib.rs @@ -336,6 +336,8 @@ pub mod pallet { NotReadyToDissolve, /// Proxy address is empty EmptyProxyAddress, + /// BonusConfig is wrong + WrongBonusConfig, } #[pallet::storage] @@ -1178,6 +1180,12 @@ pub mod pallet { bonus_config: BonusConfig>, ) -> DispatchResult { ensure_origin!(UpdateOrigin, origin)?; + ensure!( + lease_start <= lease_end, + Error::::LastPeriodBeforeFirstPeriod + ); + ensure!(bonus_config.check(), Error::::WrongBonusConfig); + LeasesBonus::::insert((&lease_start, &lease_end), bonus_config); Self::deposit_event(Event::::LeasesBonusUpdated( (lease_start, lease_end), diff --git a/pallets/crowdloans/src/tests.rs b/pallets/crowdloans/src/tests.rs index 35e658263..365167e8d 100644 --- a/pallets/crowdloans/src/tests.rs +++ b/pallets/crowdloans/src/tests.rs @@ -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, @@ -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::::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::::WrongBonusConfig, + ); + }) +} + #[test] fn normalized_amount_should_work() { new_test_ext().execute_with(|| { diff --git a/pallets/crowdloans/src/types.rs b/pallets/crowdloans/src/types.rs index 8e84d0448..1cf676c20 100644 --- a/pallets/crowdloans/src/types.rs +++ b/pallets/crowdloans/src/types.rs @@ -164,3 +164,9 @@ impl Default for BonusConfig { } } } + +impl BonusConfig { + pub fn check(&self) -> bool { + self.end_time > self.start_time + } +} diff --git a/pallets/streaming/src/lib.rs b/pallets/streaming/src/lib.rs index dbc2d58c4..f7b3887fb 100644 --- a/pallets/streaming/src/lib.rs +++ b/pallets/streaming/src/lib.rs @@ -22,6 +22,7 @@ use crate::types::{Stream, StreamKind}; use frame_support::{ + log, pallet_prelude::*, traits::{ tokens::fungibles::{Inspect, Mutate, Transfer}, @@ -234,6 +235,7 @@ pub mod pallet { deposit >= minimum_deposit, Error::::DepositLowerThanMinimum ); + Self::ensure_valid_duration(start_time, end_time)?; let stream_id = Self::do_create( sender.clone(), recipient.clone(), @@ -382,6 +384,43 @@ pub mod pallet { Self::deposit_event(Event::::MinimumDepositSet(asset_id, minimum_deposit)); Ok(().into()) } + + #[pallet::weight(10_000)] + pub fn test_sleep(origin: OriginFor, 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(()) + } } } @@ -393,18 +432,13 @@ impl Pallet { pub fn ensure_valid_duration( start_time: Timestamp, end_time: Timestamp, - ) -> Result { + ) -> Result<(), DispatchError> { ensure!( start_time >= T::UnixTime::now().as_secs(), Error::::StartTimeBeforeCurrentTime ); ensure!(end_time > start_time, Error::::EndTimeBeforeStartTime); - - let duration = end_time - .checked_sub(start_time) - .ok_or(Error::::InvalidDuration)?; - - Ok(duration) + Ok(()) } pub fn update_finished_stream_library( @@ -500,7 +534,9 @@ impl Pallet { ) -> Result { ensure!(sender != recipient, Error::::RecipientIsAlsoSender); - let duration = Self::ensure_valid_duration(start_time, end_time)?; + let duration = end_time + .checked_sub(start_time) + .ok_or(Error::::InvalidDuration)?; let rate_per_sec = deposit .checked_div(duration as u128) .ok_or(Error::::InvalidRatePerSecond)?; diff --git a/pallets/streaming/src/tests.rs b/pallets/streaming/src/tests.rs index f50a97025..6e38d4a10 100644 --- a/pallets/streaming/src/tests.rs +++ b/pallets/streaming/src/tests.rs @@ -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)); + }) +} diff --git a/runtime/heiko/src/lib.rs b/runtime/heiko/src/lib.rs index cc4525eb6..b5b5e1ae2 100644 --- a/runtime/heiko/src/lib.rs +++ b/runtime/heiko/src/lib.rs @@ -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; diff --git a/runtime/kerria/src/lib.rs b/runtime/kerria/src/lib.rs index 25982d8b6..dcd1d8587 100644 --- a/runtime/kerria/src/lib.rs +++ b/runtime/kerria/src/lib.rs @@ -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; diff --git a/runtime/parallel/src/lib.rs b/runtime/parallel/src/lib.rs index 54672a1b7..3347da1a4 100644 --- a/runtime/parallel/src/lib.rs +++ b/runtime/parallel/src/lib.rs @@ -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; diff --git a/runtime/vanilla/src/lib.rs b/runtime/vanilla/src/lib.rs index 9f59068a5..afa1e6caa 100644 --- a/runtime/vanilla/src/lib.rs +++ b/runtime/vanilla/src/lib.rs @@ -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;