Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
ab34400
Initial draft of tip enum
Jun 19, 2019
d36dc9a
Fix local tests
Jun 19, 2019
a18be8c
Builds.
kianenigma Jun 22, 2019
55d39cf
Fix test build.
kianenigma Jun 22, 2019
a4e2816
Tratify tip.
kianenigma Jun 23, 2019
58ae208
Cleanup of checkedExt.
kianenigma Jun 23, 2019
c7fe8bf
More cleanup.
kianenigma Jun 23, 2019
e47bf96
Master.into()
kianenigma Jun 23, 2019
78dde21
Checked Tip + other fixes.
kianenigma Jun 27, 2019
7326e5a
Line width
kianenigma Jun 27, 2019
a568a9b
Update core/sr-primitives/src/generic/tip.rs
4meta5 Jun 28, 2019
b3f69cb
Fix build.
kianenigma Jun 28, 2019
557f6c5
Merge branch 'master' of github.com:paritytech/substrate into kiz-tx-tip
kianenigma Jun 28, 2019
8e713aa
Bump.
kianenigma Jun 28, 2019
b110af6
Merge branch 'kiz-tx-tip' of github.com:paritytech/substrate into kiz…
kianenigma Jun 28, 2019
23df80d
Some cleanup (+should pass the tests).
kianenigma Jun 30, 2019
83721a4
Fix sync test payload.
kianenigma Jun 30, 2019
bd28f0d
Fix subkey and factory sig
kianenigma Jun 30, 2019
120de20
revert back old unchecked ext type to NOT use tip.
kianenigma Jul 9, 2019
0d1de9d
Make balance associated type of payment.
kianenigma Jul 10, 2019
6836f6b
Optionize tip.
kianenigma Jul 10, 2019
7191a7d
Further cleanup.
kianenigma Jul 10, 2019
0846065
Further cleanup.
kianenigma Jul 10, 2019
12ecfcc
Master.into()
kianenigma Jul 10, 2019
bf99e73
Fix tests.
kianenigma Jul 10, 2019
4824776
remove balance as generic.
kianenigma Jul 10, 2019
fb42e4e
Update doc.
kianenigma Jul 10, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Checked Tip + other fixes.
  • Loading branch information
kianenigma committed Jun 27, 2019
commit 78dde219fe5773b585f4832501df58702501de96
20 changes: 17 additions & 3 deletions core/sr-primitives/src/generic/checked_extrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,29 @@

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)>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub signed: Option<(AccountId, Index)>,
pub signed: Option<(AccountId, Index, Tip<Balance>)>,

Copy link
Member

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 None encoded.

Copy link
Member

@gavofyork gavofyork Jul 10, 2019

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> implements Default. For non-tipping chains, Tip<Balance> can be ().

Copy link
Contributor Author

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

/// The function that should be called.
pub function: Call,
/// The validated tip value for this transaction.
pub tip: Tip<Balance>,
}

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;
Expand All @@ -56,11 +60,21 @@ 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)
}
}

impl<AccountId, Index, Call, Balance> Tippable<Balance>
for CheckedExtrinsic<AccountId, Index, Call, Balance>
where
Balance: Clone,
{
fn tip(&self) -> Tip<Balance> {
self.tip.clone()
}
}
48 changes: 13 additions & 35 deletions core/sr-primitives/src/generic/unchecked_extrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::fmt;
use rstd::prelude::*;
use crate::codec::{Decode, Encode, Codec, Input, HasCompact};
use crate::traits::{self, Member, SimpleArithmetic, MaybeDisplay, Lookup, Extrinsic};
use super::{CheckedExtrinsic, Tip, Tippable};
use super::{CheckedExtrinsic, Tip};

#[derive(PartialEq, Eq, Clone, Encode, Decode)]
pub struct SignatureContent<Address, Index, Signature>
Expand Down Expand Up @@ -74,11 +74,11 @@ where
}

/// New instance of an unsigned extrinsic aka "inherent".
pub fn new_unsigned(function: Call, tip: Tip<Balance>) -> Self {
pub fn new_unsigned(function: Call) -> Self {
UncheckedExtrinsic {
signature: None,
function,
tip,
tip: Tip::None,
}
}
}
Expand All @@ -92,43 +92,35 @@ where
Signature: Member + traits::Verify<Signer=AccountId> + Codec,
AccountId: Member + MaybeDisplay,
Context: Lookup<Source=Address, Target=AccountId>,
Balance: Member + Encode,
{
type Checked = CheckedExtrinsic<AccountId, Index, Call>;
type Checked = CheckedExtrinsic<AccountId, Index, Call, Balance>;

fn check(self, context: &Context) -> Result<Self::Checked, &'static str> {
Ok(match self.signature {
Some(SignatureContent{signed, signature, index}) => {
let payload = (index, self.function);
let payload = (index, self.function, self.tip);
let signed = context.lookup(signed)?;
if !crate::verify_encoded_lazy(&signature, &payload, &signed) {
return Err(crate::BAD_SIGNATURE)
}
// TODO: maybe add tip to checked as well? hmm..
CheckedExtrinsic {
signed: Some((signed, payload.0)),
function: payload.1,
tip: payload.2,
}
}
None => CheckedExtrinsic {
signed: None,
function: self.function,
// an unsigned transaction cannot have tips.
tip: Tip::None,
},
})
}
}

impl<Address, Index, Signature, Call, Balance> Tippable<Balance>
for UncheckedExtrinsic<Address, Index, Call, Signature, Balance>
where
Index: HasCompact + Codec,
Signature: Codec,
Address: Codec,
Balance: Clone,
{
fn tip(&self) -> Tip<Balance> {
self.tip.clone()
}
}

impl<
Address: Codec,
Index: HasCompact + Codec,
Expand Down Expand Up @@ -216,39 +208,25 @@ where
#[cfg(test)]
mod test {
use crate::codec::{Decode, Encode};
use super::{UncheckedExtrinsic, Tip};
use super::{UncheckedExtrinsic};
type Extrinsic = UncheckedExtrinsic<u32, u32, u32, u32, u32>;


#[test]
fn encoding_matches_vec() {
let ex = Extrinsic::new_unsigned(42, Tip::Sender(10_u32));
let ex = Extrinsic::new_unsigned(42);
let encoded = ex.encode();
let decoded = Extrinsic::decode(&mut encoded.as_slice()).unwrap();
assert_eq!(decoded, ex);
let as_vec: Vec<u8> = Decode::decode(&mut encoded.as_slice()).unwrap();
assert_eq!(as_vec.encode(), encoded);
}

#[test]
fn unprovided_tip_will_not_fail() {
// without tip
// len sig f(u32).....
let bytes: Vec<u8> = vec![24, 0, 42, 0, 0, 0];
let decoded: Extrinsic = Decode::decode(&mut bytes.as_slice()).unwrap();
assert_eq!(decoded, Extrinsic::new_unsigned(42, Tip::default()));
// with tip
let bytes: Vec<u8> = vec![40, 0, 42, 0, 0, 0, 1, 10, 0, 0, 0];
let decoded: Extrinsic = Decode::decode(&mut bytes.as_slice()).unwrap();
assert_eq!(decoded, Extrinsic::new_unsigned(42, Tip::Sender(10)));

}

#[test]
#[cfg(feature = "std")]
fn serialization_of_unchecked_extrinsics() {
type Extrinsic = UncheckedExtrinsic<u32, u32, u32, u32, u32>;
let ex = Extrinsic::new_unsigned(42, Tip::None);
let ex = Extrinsic::new_unsigned(42);
assert_eq!(serde_json::to_string(&ex).unwrap(), "\"0x18002a00000000\"");
}
}
Loading