- 
                Notifications
    
You must be signed in to change notification settings  - Fork 2.7k
 
Tipped Transaction Type. #2930
Tipped Transaction Type. #2930
Changes from all commits
ab34400
              d36dc9a
              a18be8c
              55d39cf
              a4e2816
              58ae208
              c7fe8bf
              e47bf96
              78dde21
              7326e5a
              a568a9b
              b3f69cb
              557f6c5
              8e713aa
              b110af6
              23df80d
              83721a4
              bd28f0d
              120de20
              0d1de9d
              6836f6b
              7191a7d
              0846065
              12ecfcc
              bf99e73
              4824776
              fb42e4e
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -19,25 +19,33 @@ | |
| 
     | 
||
| use crate::traits::{self, Member, SimpleArithmetic, MaybeDisplay}; | ||
| use crate::weights::{Weighable, Weight}; | ||
| use crate::generic::tip::{Tip, Tippable}; | ||
| 
     | 
||
| /// Definition of something that the external world might want to say; its | ||
| /// existence implies that it has been checked and is good, particularly with | ||
| /// regards to the signature. | ||
| #[derive(PartialEq, Eq, Clone)] | ||
| #[cfg_attr(feature = "std", derive(Debug))] | ||
| pub struct CheckedExtrinsic<AccountId, Index, Call> { | ||
| pub struct CheckedExtrinsic<AccountId, Index, Call, Balance> { | ||
| /// Who this purports to be from and the number of extrinsics have come before | ||
| /// from the same signer, if anyone (note this is not a signature). | ||
| pub signed: Option<(AccountId, Index)>, | ||
| /// The function that should be called. | ||
| pub function: Call, | ||
| /// An optional tip value that may or may not exist based on the underlying unchecked extrinsic. | ||
| /// | ||
| /// Most often this is: | ||
| /// - `None` if the unchecked extrinsic does not have a tip OR it is unsigned. | ||
| /// - `Some` if the opposite. | ||
| pub tip: Option<Tip<Balance>>, | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should just be an additional arg in  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could be moved there but you mean probably in   | 
||
| } | ||
| 
     | 
||
| impl<AccountId, Index, Call> traits::Applyable for CheckedExtrinsic<AccountId, Index, Call> | ||
| impl<AccountId, Index, Call, Balance> traits::Applyable for CheckedExtrinsic<AccountId, Index, Call, Balance> | ||
| where | ||
| AccountId: Member + MaybeDisplay, | ||
| Index: Member + MaybeDisplay + SimpleArithmetic, | ||
| Call: Member, | ||
| Balance: Member, | ||
| { | ||
| type Index = Index; | ||
| type AccountId = AccountId; | ||
| 
        
          
        
         | 
    @@ -56,11 +64,40 @@ where | |
| } | ||
| } | ||
| 
     | 
||
| impl<AccountId, Index, Call> Weighable for CheckedExtrinsic<AccountId, Index, Call> | ||
| impl<AccountId, Index, Call, Balance> Weighable for CheckedExtrinsic<AccountId, Index, Call, Balance> | ||
| where | ||
| Call: Weighable, | ||
| { | ||
| fn weight(&self, len: usize) -> Weight { | ||
| self.function.weight(len) | ||
| } | ||
| } | ||
| 
     | 
||
| /// `ExtBalance` is the balance type fed by the `check()` implementation of various unchecked | ||
| /// extrinsics. `NodeBalance` is the actual balance type used as a primitive type of the substrate | ||
| /// node. | ||
| /// | ||
| /// In practice, if they underlying unchecked transaction is tip-aware, they are the same. Otherwise, | ||
| /// the tip is always `None` and the type is of no importance. | ||
| impl<AccountId, Index, Call, ExtBalance, NodeBalance> Tippable<NodeBalance> | ||
| for CheckedExtrinsic<AccountId, Index, Call, ExtBalance> | ||
| where | ||
| ExtBalance: Clone + Copy, | ||
| NodeBalance: From<ExtBalance>, | ||
| { | ||
| fn tip(&self) -> Option<Tip<NodeBalance>> { | ||
| // This is a hacky way to prevent `unchecked_mortal[_compact]_extrinsic` types, which | ||
| // don't have a tip, become generic over a balance type. | ||
| // Basically, this CheckedExtrinsic is built either 1- from an | ||
| // `UncheckedMortalCompactTippedExtrinsic`, which is tip-aware and hence, the second arm | ||
| // will be trivially executed and the type conversion will be safe (the compiler is probably | ||
| // smart enough to remove it in fact). In this case, NodeBalance and ExtBalance are the same. | ||
| // Or 2- this is built from all other types of uncheckedextrinsic which do not have tip and | ||
| // hence are not tip-aware. These modules will naively place a u32 (can be `()` in practice) | ||
| // as the type and it does not matter since `None` is used in this case (first arm). | ||
| match &self.tip { | ||
| None => None, | ||
| Some(Tip::Sender(v)) => Some(Tip::Sender(NodeBalance::from(*v))), | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // Copyright 2019 Parity Technologies (UK) Ltd. | ||
| // This file is part of Substrate. | ||
| 
     | 
||
| // Substrate is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| 
     | 
||
| // Substrate is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| 
     | 
||
| // You should have received a copy of the GNU General Public License | ||
| // along with Substrate. If not, see <http://www.gnu.org/licenses/>. | ||
| 
     | 
||
| //! Tip structure for a transaction. | ||
| use crate::codec::{Encode, Decode}; | ||
| use crate::traits::Zero; | ||
| 
     | 
||
| /// A placeholder tip type that is used to fulfill the generic requirements of `checked_extrinsic` | ||
| /// when the underlying `unchecked_extrinsic` actually does not have a tip. | ||
| pub type NoTipBalance = u32; | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could not have it meet the type requirements of the generic type (has to be   | 
||
| 
     | 
||
| /// Representation of a transaction tip. | ||
| /// | ||
| /// Provided as an enum to support potential future use cases such as: | ||
| /// - Tipped by a third party (software or exchange). | ||
| /// - Unsigned tip. | ||
| #[cfg_attr(feature = "std", derive(Debug))] | ||
| #[derive(Clone, Copy, Eq, PartialEq, Encode, Decode)] | ||
| pub enum Tip<Balance> { | ||
                
      
                  kianenigma marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| /// The sender of the transaction has included some tip. | ||
| /// | ||
| /// this must be signed and included in the signature payload. | ||
| Sender(Balance), | ||
| } | ||
| 
     | 
||
| impl<Balance: Zero + Copy> Tip<Balance> { | ||
| /// Return the raw value of the tip (to be burned or consumed) regardless of any logic that the | ||
| /// Tip enum variant might embody. | ||
| pub fn value(&self) -> Balance { | ||
| match *self { | ||
| Tip::Sender(value) => value, | ||
| } | ||
| } | ||
| } | ||
| 
     | 
||
| /// A trait for a generic transaction that contains a tip. The tip itself might yield something | ||
| /// that translates to "no tip". | ||
| pub trait Tippable<Balance> { | ||
| /// Return the tip associated with this transaction. | ||
| fn tip(&self) -> Option<Tip<Balance>>; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it could theoretically be an option, but as long as it's compact encoded, then there's no point since zero compact encoded is a big as
Noneencoded.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just make sure
Tip<Balance>implementsDefault. For non-tipping chains,Tip<Balance>can be().There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if it is part of the signature then it is only visible in signed txs which is the point of the current option. Hence,
pub signed: Option<(AccountId, Index, Tip<Balance>)>,looks ok