From bf1bef4b297e14bd1fb27ac1d6f0e034c59138ec Mon Sep 17 00:00:00 2001
From: bgallois <benjamin@gallois.cc>
Date: Mon, 20 Jan 2025 12:38:25 +0100
Subject: [PATCH] update txextensions

---
 pallets/duniter-account/src/lib.rs         | 10 +++
 pallets/oneshot-account/src/check_nonce.rs | 79 +++++++++++++++-------
 pallets/oneshot-account/src/lib.rs         | 16 ++++-
 pallets/upgrade-origin/src/lib.rs          |  2 +-
 runtime/g1/src/lib.rs                      |  6 +-
 runtime/gdev/src/lib.rs                    |  6 +-
 runtime/gtest/src/lib.rs                   |  6 +-
 7 files changed, 87 insertions(+), 38 deletions(-)

diff --git a/pallets/duniter-account/src/lib.rs b/pallets/duniter-account/src/lib.rs
index ddd56e0ad..03eec0d4e 100644
--- a/pallets/duniter-account/src/lib.rs
+++ b/pallets/duniter-account/src/lib.rs
@@ -320,6 +320,16 @@ where
     type Balance = BalanceOf<T>;
     type LiquidityInfo = Option<Credit<T::AccountId, T::Currency>>;
 
+    fn can_withdraw_fee(
+        who: &T::AccountId,
+        call: &T::RuntimeCall,
+        dispatch_info: &DispatchInfoOf<T::RuntimeCall>,
+        fee: Self::Balance,
+        tip: Self::Balance,
+    ) -> Result<(), TransactionValidityError> {
+        T::InnerOnChargeTransaction::can_withdraw_fee(who, call, dispatch_info, fee, tip)
+    }
+
     fn withdraw_fee(
         who: &T::AccountId,
         call: &T::RuntimeCall,
diff --git a/pallets/oneshot-account/src/check_nonce.rs b/pallets/oneshot-account/src/check_nonce.rs
index 3d792e53b..63968eddb 100644
--- a/pallets/oneshot-account/src/check_nonce.rs
+++ b/pallets/oneshot-account/src/check_nonce.rs
@@ -17,15 +17,19 @@
 use crate::Config;
 
 use codec::{Decode, Encode};
-use frame_support::{dispatch::DispatchInfo, traits::IsSubType};
+use frame_support::{dispatch::DispatchInfo, pallet_prelude::Weight, traits::IsSubType};
 //use frame_system::Config;
 use scale_info::{
     prelude::fmt::{Debug, Formatter},
     TypeInfo,
 };
 use sp_runtime::{
-    traits::{DispatchInfoOf, Dispatchable, SignedExtension},
-    transaction_validity::{TransactionValidity, TransactionValidityError},
+    traits::{
+        AsSystemOriginSigner, DispatchInfoOf, Dispatchable, PostDispatchInfoOf,
+        TransactionExtension, ValidateResult,
+    },
+    transaction_validity::{TransactionSource, TransactionValidityError},
+    DispatchResult,
 };
 
 /// Wrapper around `frame_system::CheckNonce<T>`.
@@ -51,46 +55,71 @@ impl<T: Config> Debug for CheckNonce<T> {
     }
 }
 
-impl<T: Config + TypeInfo> SignedExtension for CheckNonce<T>
+impl<T: Config + TypeInfo> TransactionExtension<T::RuntimeCall> for CheckNonce<T>
 where
-    T::RuntimeCall: Dispatchable<Info = DispatchInfo> + IsSubType<crate::Call<T>>,
+    T::RuntimeCall: Dispatchable<Info = DispatchInfo>,
+    <T::RuntimeCall as Dispatchable>::RuntimeOrigin: AsSystemOriginSigner<T::AccountId> + Clone,
+    T::RuntimeCall: IsSubType<crate::Call<T>>,
 {
-    type AccountId = <T as frame_system::Config>::AccountId;
-    type AdditionalSigned = ();
-    type Call = <T as frame_system::Config>::RuntimeCall;
-    type Pre = ();
+    type Implicit = ();
+    type Pre = <frame_system::CheckNonce<T> as TransactionExtension<T::RuntimeCall>>::Pre;
+    type Val = <frame_system::CheckNonce<T> as TransactionExtension<T::RuntimeCall>>::Val;
 
     const IDENTIFIER: &'static str = "CheckNonce";
 
-    fn additional_signed(&self) -> Result<(), TransactionValidityError> {
-        self.0.additional_signed()
+    fn validate(
+        &self,
+        origin: <T as frame_system::Config>::RuntimeOrigin,
+        call: &T::RuntimeCall,
+        info: &DispatchInfoOf<T::RuntimeCall>,
+        len: usize,
+        self_implicit: Self::Implicit,
+        inherited_implication: &impl Encode,
+        source: TransactionSource,
+    ) -> ValidateResult<Self::Val, T::RuntimeCall> {
+        self.0.validate(
+            origin,
+            call,
+            info,
+            len,
+            self_implicit,
+            inherited_implication,
+            source,
+        )
     }
 
-    fn pre_dispatch(
+    fn weight(&self, origin: &T::RuntimeCall) -> Weight {
+        self.0.weight(origin)
+    }
+
+    fn prepare(
         self,
-        who: &Self::AccountId,
-        call: &Self::Call,
-        info: &DispatchInfoOf<Self::Call>,
+        val: Self::Val,
+        origin: &T::RuntimeOrigin,
+        call: &T::RuntimeCall,
+        info: &DispatchInfoOf<T::RuntimeCall>,
         len: usize,
-    ) -> Result<(), TransactionValidityError> {
+    ) -> Result<Self::Pre, TransactionValidityError> {
         if let Some(
             crate::Call::consume_oneshot_account { .. }
             | crate::Call::consume_oneshot_account_with_remaining { .. },
         ) = call.is_sub_type()
         {
-            Ok(())
+            Ok(Self::Pre::NonceChecked)
         } else {
-            self.0.pre_dispatch(who, call, info, len)
+            self.0.prepare(val, origin, call, info, len)
         }
     }
 
-    fn validate(
-        &self,
-        who: &Self::AccountId,
-        call: &Self::Call,
-        info: &DispatchInfoOf<Self::Call>,
+    fn post_dispatch_details(
+        pre: Self::Pre,
+        info: &DispatchInfo,
+        post_info: &PostDispatchInfoOf<T::RuntimeCall>,
         len: usize,
-    ) -> TransactionValidity {
-        self.0.validate(who, call, info, len)
+        result: &DispatchResult,
+    ) -> Result<Weight, TransactionValidityError> {
+        <frame_system::CheckNonce<T> as TransactionExtension<T::RuntimeCall>>::post_dispatch_details(
+            pre, info, post_info, len, result,
+        )
     }
 }
diff --git a/pallets/oneshot-account/src/lib.rs b/pallets/oneshot-account/src/lib.rs
index 42adac55a..dc54c7637 100644
--- a/pallets/oneshot-account/src/lib.rs
+++ b/pallets/oneshot-account/src/lib.rs
@@ -136,7 +136,7 @@ pub mod pallet {
         ///
         /// Origin account is kept alive.
         #[pallet::call_index(0)]
-        #[pallet::weight(T::WeightInfo::create_oneshot_account())]
+        #[pallet::weight(<T as pallet::Config>::WeightInfo::create_oneshot_account())]
         pub fn create_oneshot_account(
             origin: OriginFor<T>,
             dest: <T::Lookup as StaticLookup>::Source,
@@ -177,7 +177,7 @@ pub mod pallet {
         /// - `dest`: The destination account.
         /// - `dest_is_oneshot`: If set to `true`, then a oneshot account is created at `dest`. Else, `dest` has to be an existing account.
         #[pallet::call_index(1)]
-        #[pallet::weight(T::WeightInfo::consume_oneshot_account())]
+        #[pallet::weight(<T as pallet::Config>::WeightInfo::consume_oneshot_account())]
         pub fn consume_oneshot_account(
             origin: OriginFor<T>,
             block_height: BlockNumberFor<T>,
@@ -237,7 +237,7 @@ pub mod pallet {
         /// - `dest2_is_oneshot`: If set to `true`, then a oneshot account is created at `dest2`. Else, `dest2` has to be an existing account.
         /// - `balance1`: The amount transfered to `dest`, the leftover being transfered to `dest2`.
         #[pallet::call_index(2)]
-        #[pallet::weight(T::WeightInfo::consume_oneshot_account_with_remaining())]
+        #[pallet::weight(<T as pallet::Config>::WeightInfo::consume_oneshot_account_with_remaining())]
         pub fn consume_oneshot_account_with_remaining(
             origin: OriginFor<T>,
             block_height: BlockNumberFor<T>,
@@ -339,6 +339,16 @@ where
     type Balance = BalanceOf<T>;
     type LiquidityInfo = Option<Credit<T::AccountId, T::Currency>>;
 
+    fn can_withdraw_fee(
+        who: &T::AccountId,
+        call: &T::RuntimeCall,
+        dispatch_info: &DispatchInfoOf<T::RuntimeCall>,
+        fee: Self::Balance,
+        tip: Self::Balance,
+    ) -> Result<(), TransactionValidityError> {
+        T::InnerOnChargeTransaction::can_withdraw_fee(who, call, dispatch_info, fee, tip)
+    }
+
     fn withdraw_fee(
         who: &T::AccountId,
         call: &T::RuntimeCall,
diff --git a/pallets/upgrade-origin/src/lib.rs b/pallets/upgrade-origin/src/lib.rs
index d98c21f84..b9d0e0479 100644
--- a/pallets/upgrade-origin/src/lib.rs
+++ b/pallets/upgrade-origin/src/lib.rs
@@ -86,7 +86,7 @@ pub mod pallet {
 			let dispatch_info = call.get_dispatch_info();
 			(
 				T::WeightInfo::dispatch_as_root()
-					.saturating_add(dispatch_info.weight),
+					.saturating_add(dispatch_info.call_weight).saturating_add(dispatch_info.extension_weight),
 				dispatch_info.class,
 			)
 		})]
diff --git a/runtime/g1/src/lib.rs b/runtime/g1/src/lib.rs
index 8dccda2f1..7f8314c50 100644
--- a/runtime/g1/src/lib.rs
+++ b/runtime/g1/src/lib.rs
@@ -113,9 +113,9 @@ pub fn native_version() -> NativeVersion {
 pub type Block = sp_runtime::generic::Block<Header, UncheckedExtrinsic>;
 /// Unchecked extrinsic type as expected by this runtime.
 pub type UncheckedExtrinsic =
-    generic::UncheckedExtrinsic<Address, RuntimeCall, Signature, SignedExtra>;
-/// The SignedExtension to the basic transaction logic.
-pub type SignedExtra = (
+    generic::UncheckedExtrinsic<Address, RuntimeCall, Signature, TxExtension>;
+/// The `TransactionExtension` to the basic transaction logic.
+pub type TxExtension = (
     frame_system::CheckNonZeroSender<Runtime>,
     frame_system::CheckSpecVersion<Runtime>,
     frame_system::CheckTxVersion<Runtime>,
diff --git a/runtime/gdev/src/lib.rs b/runtime/gdev/src/lib.rs
index 4b7d04278..0ab65024f 100644
--- a/runtime/gdev/src/lib.rs
+++ b/runtime/gdev/src/lib.rs
@@ -113,9 +113,9 @@ pub fn native_version() -> NativeVersion {
 pub type Block = sp_runtime::generic::Block<Header, UncheckedExtrinsic>;
 /// Unchecked extrinsic type as expected by this runtime.
 pub type UncheckedExtrinsic =
-    generic::UncheckedExtrinsic<Address, RuntimeCall, Signature, SignedExtra>;
-/// The SignedExtension to the basic transaction logic.
-pub type SignedExtra = (
+    generic::UncheckedExtrinsic<Address, RuntimeCall, Signature, TxExtension>;
+/// The `TransactionExtension` to the basic transaction logic.
+pub type TxExtension = (
     frame_system::CheckNonZeroSender<Runtime>,
     frame_system::CheckSpecVersion<Runtime>,
     frame_system::CheckTxVersion<Runtime>,
diff --git a/runtime/gtest/src/lib.rs b/runtime/gtest/src/lib.rs
index 842fc6a38..0a0c4f099 100644
--- a/runtime/gtest/src/lib.rs
+++ b/runtime/gtest/src/lib.rs
@@ -112,9 +112,9 @@ pub fn native_version() -> NativeVersion {
 pub type Block = sp_runtime::generic::Block<Header, UncheckedExtrinsic>;
 /// Unchecked extrinsic type as expected by this runtime.
 pub type UncheckedExtrinsic =
-    generic::UncheckedExtrinsic<Address, RuntimeCall, Signature, SignedExtra>;
-/// The SignedExtension to the basic transaction logic.
-pub type SignedExtra = (
+    generic::UncheckedExtrinsic<Address, RuntimeCall, Signature, TxExtension>;
+/// The `TransactionExtension` to the basic transaction logic.
+pub type TxExtension = (
     frame_system::CheckNonZeroSender<Runtime>,
     frame_system::CheckSpecVersion<Runtime>,
     frame_system::CheckTxVersion<Runtime>,
-- 
GitLab