1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! A `SignedExtension` module. Adds a reinit request for the signer of each trx

#![cfg_attr(not(feature = "std"), no_std)]

use super::{Module, Trait};
use frame_support::codec::{Decode, Encode};
use frame_support::dispatch::fmt::Debug;
use frame_support::dispatch::DispatchInfo;
use sp_runtime::traits::{DispatchInfoOf, Dispatchable, SignedExtension};
use sp_runtime::transaction_validity::TransactionValidityError;
use sp_std::marker::PhantomData;

/// Account reinit request
#[derive(Encode, Decode, Clone, Eq, PartialEq)]
pub struct ReinitAccount<T: Trait + Send + Sync>(PhantomData<T>);

impl<T: Trait + Send + Sync> Debug for ReinitAccount<T> {
    #[cfg(feature = "std")]
    fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
        write!(f, "ReinitAccount")
    }

    #[cfg(not(feature = "std"))]
    fn fmt(&self, _: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
        Ok(())
    }
}

impl<T: Trait + Send + Sync> ReinitAccount<T> {
    pub fn new() -> Self {
        Self(PhantomData)
    }
}

impl<T: Trait + Send + Sync> SignedExtension for ReinitAccount<T>
where
    T::Call: Dispatchable<Info = DispatchInfo>,
{
    type AccountId = T::AccountId;
    type Call = T::Call;
    type AdditionalSigned = ();
    type Pre = ();
    const IDENTIFIER: &'static str = "ReinitAccount";

    fn additional_signed(&self) -> sp_std::result::Result<(), TransactionValidityError> {
        Ok(())
    }

    #[allow(unused_must_use)]
    fn pre_dispatch(
        self,
        who: &Self::AccountId,
        _call: &Self::Call,
        _info: &DispatchInfoOf<Self::Call>,
        _len: usize,
    ) -> Result<(), TransactionValidityError> {
        <Module<T>>::_reinit(who);

        Ok(())
    }
}