difftreelog
fix scheduler takes into account call len
in: master
1 file changed
runtime/common/scheduler.rsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use frame_support::{18 traits::NamedReservableCurrency,19 weights::{GetDispatchInfo, PostDispatchInfo, DispatchInfo},20};21use sp_runtime::{22 traits::{Dispatchable, Applyable, Member},23 generic::Era,24 transaction_validity::TransactionValidityError,25 DispatchErrorWithPostInfo, DispatchError,26};27use crate::{Runtime, Call, Origin, Balances};28use up_common::types::{AccountId, Balance};29use fp_self_contained::SelfContainedCall;30use pallet_unique_scheduler::DispatchCall;31use pallet_transaction_payment::ChargeTransactionPayment;3233type SponsorshipChargeTransactionPayment = pallet_charge_transaction::ChargeTransactionPayment<Runtime>;3435/// The SignedExtension to the basic transaction logic.36pub type SignedExtraScheduler = (37 frame_system::CheckSpecVersion<Runtime>,38 frame_system::CheckGenesis<Runtime>,39 frame_system::CheckEra<Runtime>,40 frame_system::CheckNonce<Runtime>,41 frame_system::CheckWeight<Runtime>,42 ChargeTransactionPayment::<Runtime>,43);4445fn get_signed_extras(from: <Runtime as frame_system::Config>::AccountId) -> SignedExtraScheduler {46 (47 frame_system::CheckSpecVersion::<Runtime>::new(),48 frame_system::CheckGenesis::<Runtime>::new(),49 frame_system::CheckEra::<Runtime>::from(Era::Immortal),50 frame_system::CheckNonce::<Runtime>::from(frame_system::Pallet::<Runtime>::account_nonce(51 from,52 )),53 frame_system::CheckWeight::<Runtime>::new(),54 ChargeTransactionPayment::<Runtime>::from(0),55 )56}5758pub struct SchedulerPaymentExecutor;5960impl<T: frame_system::Config + pallet_unique_scheduler::Config, SelfContainedSignedInfo>61 DispatchCall<T, SelfContainedSignedInfo> for SchedulerPaymentExecutor62where63 <T as frame_system::Config>::Call: Member64 + Dispatchable<Origin = Origin, Info = DispatchInfo>65 + SelfContainedCall<SignedInfo = SelfContainedSignedInfo>66 + GetDispatchInfo67 + From<frame_system::Call<Runtime>>,68 SelfContainedSignedInfo: Send + Sync + 'static,69 Call: From<<T as frame_system::Config>::Call>70 + From<<T as pallet_unique_scheduler::Config>::Call>71 + SelfContainedCall<SignedInfo = SelfContainedSignedInfo>,72 sp_runtime::AccountId32: From<<T as frame_system::Config>::AccountId>,73{74 fn dispatch_call(75 signer: <T as frame_system::Config>::AccountId,76 call: <T as pallet_unique_scheduler::Config>::Call,77 ) -> Result<78 Result<PostDispatchInfo, DispatchErrorWithPostInfo<PostDispatchInfo>>,79 TransactionValidityError,80 > {81 let dispatch_info = call.get_dispatch_info();82 let extrinsic = fp_self_contained::CheckedExtrinsic::<83 AccountId,84 Call,85 SignedExtraScheduler,86 SelfContainedSignedInfo,87 > {88 signed: fp_self_contained::CheckedSignature::<89 AccountId,90 SignedExtraScheduler,91 SelfContainedSignedInfo,92 >::Signed(signer.clone().into(), get_signed_extras(signer.into())),93 function: call.into(),94 };9596 extrinsic.apply::<Runtime>(&dispatch_info, 0)97 }9899 fn reserve_balance(100 id: [u8; 16],101 sponsor: <T as frame_system::Config>::AccountId,102 call: <T as pallet_unique_scheduler::Config>::Call,103 count: u32,104 ) -> Result<(), DispatchError> {105 let dispatch_info = call.get_dispatch_info();106 let weight: Balance = SponsorshipChargeTransactionPayment::traditional_fee(0, &dispatch_info, 0)107 .saturating_mul(count.into());108109 <Balances as NamedReservableCurrency<AccountId>>::reserve_named(110 &id,111 &(sponsor.into()),112 weight,113 )114 }115116 fn pay_for_call(117 id: [u8; 16],118 sponsor: <T as frame_system::Config>::AccountId,119 call: <T as pallet_unique_scheduler::Config>::Call,120 ) -> Result<u128, DispatchError> {121 let dispatch_info = call.get_dispatch_info();122 let weight: Balance = SponsorshipChargeTransactionPayment::traditional_fee(0, &dispatch_info, 0);123 Ok(124 <Balances as NamedReservableCurrency<AccountId>>::unreserve_named(125 &id,126 &(sponsor.into()),127 weight,128 ),129 )130 }131132 fn cancel_reserve(133 id: [u8; 16],134 sponsor: <T as frame_system::Config>::AccountId,135 ) -> Result<u128, DispatchError> {136 Ok(137 <Balances as NamedReservableCurrency<AccountId>>::unreserve_named(138 &id,139 &(sponsor.into()),140 u128::MAX,141 ),142 )143 }144}1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use frame_support::{18 traits::NamedReservableCurrency,19 weights::{GetDispatchInfo, PostDispatchInfo, DispatchInfo},20};21use sp_runtime::{22 traits::{Dispatchable, Applyable, Member},23 generic::Era,24 transaction_validity::TransactionValidityError,25 DispatchErrorWithPostInfo, DispatchError,26};27use codec::Encode;28use crate::{Runtime, Call, Origin, Balances};29use up_common::types::{AccountId, Balance};30use fp_self_contained::SelfContainedCall;31use pallet_unique_scheduler::DispatchCall;32use pallet_transaction_payment::ChargeTransactionPayment;3334type SponsorshipChargeTransactionPayment = pallet_charge_transaction::ChargeTransactionPayment<Runtime>;3536/// The SignedExtension to the basic transaction logic.37pub type SignedExtraScheduler = (38 frame_system::CheckSpecVersion<Runtime>,39 frame_system::CheckGenesis<Runtime>,40 frame_system::CheckEra<Runtime>,41 frame_system::CheckNonce<Runtime>,42 frame_system::CheckWeight<Runtime>,43 ChargeTransactionPayment::<Runtime>,44);4546fn get_signed_extras(from: <Runtime as frame_system::Config>::AccountId) -> SignedExtraScheduler {47 (48 frame_system::CheckSpecVersion::<Runtime>::new(),49 frame_system::CheckGenesis::<Runtime>::new(),50 frame_system::CheckEra::<Runtime>::from(Era::Immortal),51 frame_system::CheckNonce::<Runtime>::from(frame_system::Pallet::<Runtime>::account_nonce(52 from,53 )),54 frame_system::CheckWeight::<Runtime>::new(),55 ChargeTransactionPayment::<Runtime>::from(0),56 )57}5859pub struct SchedulerPaymentExecutor;6061impl<T: frame_system::Config + pallet_unique_scheduler::Config, SelfContainedSignedInfo>62 DispatchCall<T, SelfContainedSignedInfo> for SchedulerPaymentExecutor63where64 <T as frame_system::Config>::Call: Member65 + Dispatchable<Origin = Origin, Info = DispatchInfo>66 + SelfContainedCall<SignedInfo = SelfContainedSignedInfo>67 + GetDispatchInfo68 + From<frame_system::Call<Runtime>>,69 SelfContainedSignedInfo: Send + Sync + 'static,70 Call: From<<T as frame_system::Config>::Call>71 + From<<T as pallet_unique_scheduler::Config>::Call>72 + SelfContainedCall<SignedInfo = SelfContainedSignedInfo>,73 sp_runtime::AccountId32: From<<T as frame_system::Config>::AccountId>,74{75 fn dispatch_call(76 signer: <T as frame_system::Config>::AccountId,77 call: <T as pallet_unique_scheduler::Config>::Call,78 ) -> Result<79 Result<PostDispatchInfo, DispatchErrorWithPostInfo<PostDispatchInfo>>,80 TransactionValidityError,81 > {82 let dispatch_info = call.get_dispatch_info();83 let len = call.encoded_size();84 let extrinsic = fp_self_contained::CheckedExtrinsic::<85 AccountId,86 Call,87 SignedExtraScheduler,88 SelfContainedSignedInfo,89 > {90 signed: fp_self_contained::CheckedSignature::<91 AccountId,92 SignedExtraScheduler,93 SelfContainedSignedInfo,94 >::Signed(signer.clone().into(), get_signed_extras(signer.into())),95 function: call.into(),96 };9798 extrinsic.apply::<Runtime>(&dispatch_info, len)99 }100101 fn reserve_balance(102 id: [u8; 16],103 sponsor: <T as frame_system::Config>::AccountId,104 call: <T as pallet_unique_scheduler::Config>::Call,105 count: u32,106 ) -> Result<(), DispatchError> {107 let dispatch_info = call.get_dispatch_info();108 let weight: Balance = SponsorshipChargeTransactionPayment::traditional_fee(0, &dispatch_info, 0)109 .saturating_mul(count.into());110111 <Balances as NamedReservableCurrency<AccountId>>::reserve_named(112 &id,113 &(sponsor.into()),114 weight,115 )116 }117118 fn pay_for_call(119 id: [u8; 16],120 sponsor: <T as frame_system::Config>::AccountId,121 call: <T as pallet_unique_scheduler::Config>::Call,122 ) -> Result<u128, DispatchError> {123 let dispatch_info = call.get_dispatch_info();124 let weight: Balance = SponsorshipChargeTransactionPayment::traditional_fee(0, &dispatch_info, 0);125 Ok(126 <Balances as NamedReservableCurrency<AccountId>>::unreserve_named(127 &id,128 &(sponsor.into()),129 weight,130 ),131 )132 }133134 fn cancel_reserve(135 id: [u8; 16],136 sponsor: <T as frame_system::Config>::AccountId,137 ) -> Result<u128, DispatchError> {138 Ok(139 <Balances as NamedReservableCurrency<AccountId>>::unreserve_named(140 &id,141 &(sponsor.into()),142 u128::MAX,143 ),144 )145 }146}