pallet_subspace/
extensions.rs

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//! Extensions for unsigned general extrinsics

#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarking;
pub mod weights;

use crate::extensions::weights::WeightInfo;
use crate::pallet::Call as SubspaceCall;
use crate::{Config, Pallet as Subspace};
use codec::{Decode, Encode};
use frame_support::pallet_prelude::{PhantomData, TypeInfo, Weight};
use frame_support::RuntimeDebugNoBound;
use frame_system::pallet_prelude::{BlockNumberFor, RuntimeCallFor};
use scale_info::prelude::fmt;
use sp_consensus_subspace::SignedVote;
use sp_runtime::traits::{
    AsSystemOriginSigner, DispatchInfoOf, DispatchOriginOf, Dispatchable, Implication,
    PostDispatchInfoOf, TransactionExtension, ValidateResult,
};
use sp_runtime::transaction_validity::{
    InvalidTransaction, TransactionSource, TransactionValidityError, ValidTransaction,
};
use sp_runtime::DispatchResult;

/// Trait to convert Runtime call to possible Subspace call.
pub trait MaybeSubspaceCall<Runtime>
where
    Runtime: Config,
{
    fn maybe_subspace_call(&self) -> Option<&SubspaceCall<Runtime>>;
}

/// Weight info used by this extension
#[derive(RuntimeDebugNoBound)]
pub enum ExtensionWeightData {
    /// Represents the validated call's used weight
    Validated(Weight),
    /// Skipped validation
    Skipped,
}

/// Extensions for pallet-subspace unsigned extrinsics.
#[derive(Encode, Decode, Clone, Eq, PartialEq, TypeInfo)]
pub struct SubspaceExtension<Runtime>(PhantomData<Runtime>);

impl<Runtime> SubspaceExtension<Runtime> {
    pub fn new() -> Self {
        Self(PhantomData)
    }
}

impl<Runtime> Default for SubspaceExtension<Runtime> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: Config> fmt::Debug for SubspaceExtension<T> {
    #[cfg(feature = "std")]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "SubspaceExtension",)
    }

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

impl<Runtime> SubspaceExtension<Runtime>
where
    Runtime: Config + scale_info::TypeInfo + fmt::Debug + Send + Sync,
{
    fn do_check_vote(
        signed_vote: &SignedVote<BlockNumberFor<Runtime>, Runtime::Hash, Runtime::AccountId>,
        source: TransactionSource,
    ) -> Result<(ValidTransaction, Weight), TransactionValidityError> {
        let pre_dispatch = source == TransactionSource::InBlock;
        if pre_dispatch {
            Subspace::<Runtime>::pre_dispatch_vote(signed_vote)
                .map(|weight| (ValidTransaction::default(), weight))
        } else {
            Subspace::<Runtime>::validate_vote(signed_vote)
        }
    }

    fn max_weight() -> Weight {
        <Runtime as Config>::ExtensionWeightInfo::vote()
            .max(<Runtime as Config>::ExtensionWeightInfo::vote_with_equivocation())
    }
}

impl<Runtime> TransactionExtension<RuntimeCallFor<Runtime>> for SubspaceExtension<Runtime>
where
    Runtime: Config + scale_info::TypeInfo + fmt::Debug + Send + Sync,
    <RuntimeCallFor<Runtime> as Dispatchable>::RuntimeOrigin:
        AsSystemOriginSigner<<Runtime as frame_system::Config>::AccountId> + Clone,
    RuntimeCallFor<Runtime>: MaybeSubspaceCall<Runtime>,
{
    const IDENTIFIER: &'static str = "SubspaceExtension";
    type Implicit = ();
    type Val = ExtensionWeightData;
    type Pre = ExtensionWeightData;

    fn weight(&self, call: &RuntimeCallFor<Runtime>) -> Weight {
        match call.maybe_subspace_call() {
            Some(SubspaceCall::vote { .. }) => Self::max_weight(),
            _ => Weight::zero(),
        }
    }

    fn validate(
        &self,
        origin: DispatchOriginOf<RuntimeCallFor<Runtime>>,
        call: &RuntimeCallFor<Runtime>,
        _info: &DispatchInfoOf<RuntimeCallFor<Runtime>>,
        _len: usize,
        _self_implicit: Self::Implicit,
        _inherited_implication: &impl Implication,
        source: TransactionSource,
    ) -> ValidateResult<Self::Val, RuntimeCallFor<Runtime>> {
        // we only care about unsigned calls
        if origin.as_system_origin_signer().is_some() {
            return Ok((
                ValidTransaction::default(),
                ExtensionWeightData::Skipped,
                origin,
            ));
        };

        let subspace_call = match call.maybe_subspace_call() {
            Some(subspace_call) => subspace_call,
            None => {
                return Ok((
                    ValidTransaction::default(),
                    ExtensionWeightData::Skipped,
                    origin,
                ))
            }
        };

        let (validity, weight_used) = match subspace_call {
            SubspaceCall::vote { signed_vote } => Self::do_check_vote(signed_vote, source)?,
            _ => return Err(InvalidTransaction::Call.into()),
        };

        Ok((
            validity,
            ExtensionWeightData::Validated(weight_used),
            origin,
        ))
    }

    fn prepare(
        self,
        val: Self::Val,
        _origin: &DispatchOriginOf<RuntimeCallFor<Runtime>>,
        _call: &RuntimeCallFor<Runtime>,
        _info: &DispatchInfoOf<RuntimeCallFor<Runtime>>,
        _len: usize,
    ) -> Result<Self::Pre, TransactionValidityError> {
        Ok(val)
    }

    fn post_dispatch_details(
        pre: Self::Pre,
        _info: &DispatchInfoOf<RuntimeCallFor<Runtime>>,
        _post_info: &PostDispatchInfoOf<RuntimeCallFor<Runtime>>,
        _len: usize,
        _result: &DispatchResult,
    ) -> Result<Weight, TransactionValidityError> {
        match pre {
            // return the unused weight for a validated call.
            ExtensionWeightData::Validated(used_weight) => {
                Ok(Self::max_weight().saturating_sub(used_weight))
            }
            // return no weight since this call is not validated and took no weight.
            ExtensionWeightData::Skipped => Ok(Weight::zero()),
        }
    }
}