Skip to content

Commit f2ddb3c

Browse files
committed
feat: Migrated to TransactionExtension
1 parent 6306d00 commit f2ddb3c

File tree

1 file changed

+114
-6
lines changed

1 file changed

+114
-6
lines changed

demo/runtime/src/lib.rs

Lines changed: 114 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ use sp_core::{OpaqueMetadata, crypto::KeyTypeId};
4747
use sp_governed_map::MainChainScriptsV1;
4848
use sp_inherents::InherentIdentifier;
4949
use sp_runtime::{
50-
ApplyExtrinsicResult, MultiSignature, Perbill, generic, impl_opaque_keys,
50+
ApplyExtrinsicResult, MultiSignature, Perbill, SaturatedConversion, generic, impl_opaque_keys,
5151
traits::{
5252
AccountIdLookup, BlakeTwo256, Block as BlockT, IdentifyAccount, NumberFor, One, OpaqueKeys,
53-
Verify,
53+
StaticLookup, Verify,
5454
},
5555
transaction_validity::{TransactionSource, TransactionValidity},
5656
};
@@ -712,8 +712,9 @@ pub type Address = sp_runtime::MultiAddress<AccountId, ()>;
712712
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
713713
/// Block type as expected by this runtime.
714714
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
715-
/// The SignedExtension to the basic transaction logic.
716-
pub type SignedExtra = (
715+
/// The TransactionExtension to the basic transaction logic.
716+
pub type TxExtension = (
717+
frame_system::AuthorizeCall<Runtime>,
717718
frame_system::CheckNonZeroSender<Runtime>,
718719
frame_system::CheckSpecVersion<Runtime>,
719720
frame_system::CheckTxVersion<Runtime>,
@@ -722,13 +723,18 @@ pub type SignedExtra = (
722723
frame_system::CheckNonce<Runtime>,
723724
frame_system::CheckWeight<Runtime>,
724725
pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
726+
frame_system::WeightReclaim<Runtime>,
725727
);
726728

727729
/// Unchecked extrinsic type as expected by this runtime.
728730
pub type UncheckedExtrinsic =
729-
generic::UncheckedExtrinsic<Address, RuntimeCall, Signature, SignedExtra>;
731+
generic::UncheckedExtrinsic<Address, RuntimeCall, Signature, TxExtension>;
732+
pub type UncheckedSignaturePayload =
733+
generic::UncheckedSignaturePayload<Address, Signature, TxExtension>;
730734
/// The payload being signed in transactions.
731-
pub type SignedPayload = generic::SignedPayload<RuntimeCall, SignedExtra>;
735+
pub type SignedPayload = generic::SignedPayload<RuntimeCall, TxExtension>;
736+
/// Extrinsic type that has already been checked.
737+
pub type CheckedExtrinsic = generic::CheckedExtrinsic<AccountId, RuntimeCall, TxExtension>;
732738
pub type Migrations = (
733739
pallet_session_validator_management::migrations::v1::LegacyToV1Migration<Runtime>,
734740
// More migrations can be added here
@@ -743,6 +749,108 @@ pub type Executive = frame_executive::Executive<
743749
Migrations,
744750
>;
745751

752+
impl<LocalCall> frame_system::offchain::CreateTransaction<LocalCall> for Runtime
753+
where
754+
RuntimeCall: From<LocalCall>,
755+
{
756+
type Extension = TxExtension;
757+
758+
fn create_transaction(call: RuntimeCall, extension: TxExtension) -> UncheckedExtrinsic {
759+
generic::UncheckedExtrinsic::new_transaction(call, extension).into()
760+
}
761+
}
762+
763+
impl<LocalCall> frame_system::offchain::CreateBare<LocalCall> for Runtime
764+
where
765+
RuntimeCall: From<LocalCall>,
766+
{
767+
fn create_bare(call: RuntimeCall) -> UncheckedExtrinsic {
768+
generic::UncheckedExtrinsic::new_bare(call).into()
769+
}
770+
}
771+
772+
impl frame_system::offchain::SigningTypes for Runtime {
773+
type Public = <Signature as sp_runtime::traits::Verify>::Signer;
774+
type Signature = Signature;
775+
}
776+
777+
impl<C> frame_system::offchain::CreateTransactionBase<C> for Runtime
778+
where
779+
RuntimeCall: From<C>,
780+
{
781+
type Extrinsic = UncheckedExtrinsic;
782+
type RuntimeCall = RuntimeCall;
783+
}
784+
785+
impl<C> frame_system::offchain::CreateAuthorizedTransaction<C> for Runtime
786+
where
787+
RuntimeCall: From<C>,
788+
{
789+
fn create_extension() -> Self::Extension {
790+
(
791+
frame_system::AuthorizeCall::<Runtime>::new(),
792+
frame_system::CheckNonZeroSender::<Runtime>::new(),
793+
frame_system::CheckSpecVersion::<Runtime>::new(),
794+
frame_system::CheckTxVersion::<Runtime>::new(),
795+
frame_system::CheckGenesis::<Runtime>::new(),
796+
frame_system::CheckEra::<Runtime>::from(generic::Era::Immortal),
797+
frame_system::CheckNonce::<Runtime>::from(0),
798+
frame_system::CheckWeight::<Runtime>::new(),
799+
pallet_transaction_payment::ChargeTransactionPayment::<Runtime>::from(0),
800+
frame_system::WeightReclaim::<Runtime>::new(),
801+
)
802+
}
803+
}
804+
805+
impl<LocalCall> frame_system::offchain::CreateSignedTransaction<LocalCall> for Runtime
806+
where
807+
RuntimeCall: From<LocalCall>,
808+
{
809+
fn create_signed_transaction<
810+
C: frame_system::offchain::AppCrypto<Self::Public, Self::Signature>,
811+
>(
812+
call: RuntimeCall,
813+
public: <Signature as sp_runtime::traits::Verify>::Signer,
814+
account: AccountId,
815+
nonce: Nonce,
816+
) -> Option<UncheckedExtrinsic> {
817+
// take the biggest period possible.
818+
let period =
819+
BlockHashCount::get().checked_next_power_of_two().map(|c| c / 2).unwrap_or(2) as u64;
820+
let current_block = System::block_number()
821+
.saturated_into::<u64>()
822+
// The `System::block_number` is initialized with `n+1`,
823+
// so the actual block number is `n`.
824+
.saturating_sub(1);
825+
let era = generic::Era::mortal(period, current_block);
826+
let tip = 0;
827+
let tx_ext: TxExtension = (
828+
frame_system::AuthorizeCall::<Runtime>::new(),
829+
frame_system::CheckNonZeroSender::<Runtime>::new(),
830+
frame_system::CheckSpecVersion::<Runtime>::new(),
831+
frame_system::CheckTxVersion::<Runtime>::new(),
832+
frame_system::CheckGenesis::<Runtime>::new(),
833+
frame_system::CheckEra::<Runtime>::from(era),
834+
frame_system::CheckNonce::<Runtime>::from(nonce),
835+
frame_system::CheckWeight::<Runtime>::new(),
836+
pallet_transaction_payment::ChargeTransactionPayment::<Runtime>::from(tip),
837+
frame_system::WeightReclaim::<Runtime>::new(),
838+
);
839+
840+
let raw_payload = SignedPayload::new(call, tx_ext)
841+
.map_err(|e| {
842+
log::warn!("Unable to create signed payload: {:?}", e);
843+
})
844+
.ok()?;
845+
let signature = raw_payload.using_encoded(|payload| C::sign(payload, public))?;
846+
let address = <Runtime as frame_system::Config>::Lookup::unlookup(account);
847+
let (call, tx_ext, _) = raw_payload.deconstruct();
848+
let transaction =
849+
generic::UncheckedExtrinsic::new_signed(call, address, signature, tx_ext).into();
850+
Some(transaction)
851+
}
852+
}
853+
746854
#[cfg(feature = "runtime-benchmarks")]
747855
mod benches {
748856
define_benchmarks!(

0 commit comments

Comments
 (0)