diff --git a/pallets/services-payment/src/benchmarks.rs b/pallets/services-payment/src/benchmarks.rs index c304dfc2d..5655a4f10 100644 --- a/pallets/services-payment/src/benchmarks.rs +++ b/pallets/services-payment/src/benchmarks.rs @@ -27,6 +27,7 @@ use { frame_system::RawOrigin, sp_runtime::Saturating, sp_std::prelude::*, + tp_traits::AuthorNotingHook, }; // Build genesis storage according to the mock runtime. @@ -121,5 +122,28 @@ mod benchmarks { assert!(crate::GivenFreeCredits::::get(¶_id).is_some()); } + #[benchmark] + fn on_container_author_noted() { + let para_id = 1001u32; + let block_cost = T::ProvideBlockProductionCost::block_cost(¶_id.into()).0; + let max_credit_stored = T::MaxCreditsStored::get(); + let balance_to_purchase = block_cost.saturating_mul(max_credit_stored.into()); + let caller = create_funded_user::("caller", 1, 1_000_000_000u32); + let existential_deposit = ::minimum_balance(); + assert_ok!(Pallet::::purchase_credits( + RawOrigin::Signed(caller.clone()).into(), + para_id.into(), + balance_to_purchase + existential_deposit + )); + #[block] + { + as AuthorNotingHook>::on_container_author_noted( + &caller, + 0, + para_id.into(), + ); + } + } + impl_benchmark_test_suite!(Pallet, crate::benchmarks::new_test_ext(), crate::mock::Test); } diff --git a/pallets/services-payment/src/lib.rs b/pallets/services-payment/src/lib.rs index 31ea16ebf..0273eec9e 100644 --- a/pallets/services-payment/src/lib.rs +++ b/pallets/services-payment/src/lib.rs @@ -290,10 +290,8 @@ impl AuthorNotingHook for Pallet { _block_number: BlockNumber, para_id: ParaId, ) -> Weight { - let total_weight = T::DbWeight::get().reads_writes(1, 1); - if Pallet::::burn_credit_for_para(¶_id).is_err() { - let (amount_to_charge, weight) = T::ProvideBlockProductionCost::block_cost(¶_id); + let (amount_to_charge, _weight) = T::ProvideBlockProductionCost::block_cost(¶_id); match T::Currency::withdraw( &Self::parachain_tank(para_id), amount_to_charge, @@ -309,17 +307,16 @@ impl AuthorNotingHook for Pallet { T::OnChargeForBlock::on_unbalanced(imbalance); } } - total_weight.saturating_add(weight); } - total_weight + T::WeightInfo::on_container_author_noted() } } impl Pallet { /// Derive a derivative account ID from the paraId. pub fn parachain_tank(para_id: ParaId) -> T::AccountId { - let entropy = (b"modlpy/serpayment", para_id).using_encoded(blake2_256); + let entropy = code..using_encoded(blake2_256); Decode::decode(&mut TrailingZeroInput::new(entropy.as_ref())) .expect("infinite length input; no invalid inputs for type; qed") } diff --git a/pallets/services-payment/src/tests.rs b/pallets/services-payment/src/tests.rs index 8cded6483..f41857b32 100644 --- a/pallets/services-payment/src/tests.rs +++ b/pallets/services-payment/src/tests.rs @@ -264,5 +264,9 @@ fn not_having_enough_tokens_in_tank_should_not_error() { Balances::balance(&crate::Pallet::::parachain_tank(1.into())), 1u128 ); + println!( + "address is {:?}", + crate::Pallet::::parachain_tank(2000.into()) + ) }); } diff --git a/pallets/services-payment/src/weights.rs b/pallets/services-payment/src/weights.rs index 9323b9822..d28639959 100644 --- a/pallets/services-payment/src/weights.rs +++ b/pallets/services-payment/src/weights.rs @@ -55,6 +55,7 @@ pub trait WeightInfo { fn purchase_credits() -> Weight; fn set_credits() -> Weight; fn set_given_free_credits() -> Weight; + fn on_container_author_noted() -> Weight; } /// Weights for pallet_services_payment using the Substrate node and recommended hardware. @@ -93,6 +94,20 @@ impl WeightInfo for SubstrateWeight { Weight::from_parts(2_691_000, 0) .saturating_add(T::DbWeight::get().writes(1_u64)) } + + /// Storage: `ServicesPayment::BlockProductionCredits` (r:1 w:0) + /// Proof: `ServicesPayment::BlockProductionCredits` (`max_values`: None, `max_size`: Some(24), added: 2499, mode: `MaxEncodedLen`) + /// Storage: `System::Account` (r:1 w:1) + /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`) + fn on_container_author_noted() -> Weight { + // Proof Size summary in bytes: + // Measured: `224` + // Estimated: `3593` + // Minimum execution time: 19_043_000 picoseconds. + Weight::from_parts(19_499_000, 3593) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } } // For backwards compatibility and tests @@ -130,4 +145,17 @@ impl WeightInfo for () { Weight::from_parts(2_691_000, 0) .saturating_add(RocksDbWeight::get().writes(1_u64)) } + /// Storage: `ServicesPayment::BlockProductionCredits` (r:1 w:0) + /// Proof: `ServicesPayment::BlockProductionCredits` (`max_values`: None, `max_size`: Some(24), added: 2499, mode: `MaxEncodedLen`) + /// Storage: `System::Account` (r:1 w:1) + /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`) + fn on_container_author_noted() -> Weight { + // Proof Size summary in bytes: + // Measured: `224` + // Estimated: `3593` + // Minimum execution time: 19_043_000 picoseconds. + Weight::from_parts(19_499_000, 3593) + .saturating_add(RocksDbWeight::get().reads(2_u64)) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + } }