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 15 commits
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
1 change: 1 addition & 0 deletions core/sr-api-macros/tests/trybuild.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#[test]
#[ignore]
fn ui() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/ui/*.rs");
Expand Down
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()
}
}
6 changes: 3 additions & 3 deletions core/sr-primitives/src/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ mod checked_extrinsic;
mod header;
mod block;
mod digest;
mod tip;
#[cfg(test)]
mod tests;

Expand All @@ -36,9 +37,8 @@ pub use self::era::{Era, Phase};
pub use self::checked_extrinsic::CheckedExtrinsic;
pub use self::header::Header;
pub use self::block::{Block, SignedBlock, BlockId};
pub use self::digest::{
Digest, DigestItem, DigestItemRef, OpaqueDigestItemId
};
pub use self::digest::{Digest, DigestItem, DigestItemRef, OpaqueDigestItemId};
pub use self::tip::{Tip, Tippable};

use crate::codec::Encode;
use rstd::prelude::*;
Expand Down
47 changes: 47 additions & 0 deletions core/sr-primitives/src/generic/tip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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};

/// Representation of a transaction tip.
///
/// Upon decoding, all transaction types try and decode this from the end of the encoded byte
/// stream.
/// If non-existent, the default implementation will be used.
#[cfg_attr(feature = "std", derive(Debug))]
#[derive(Clone, Copy, Eq, PartialEq, Encode, Decode)]
pub enum Tip<Balance> {
/// This transaction does not include any tips.
None,
/// The sender of the transaction has included some tip.
Sender(Balance),
}

/// Default implementation for tip.
impl<Balance> Default for Tip<Balance> {
fn default() -> Self {
Tip::None
}
}

/// A trait for a generic transaction that contains a tip. The tip itself might yield something
/// that translates to "no tip" but this trait must always be implemented for `UncheckedExtrinsic`.
pub trait Tippable<Balance> {
/// Return the tip associated with this transaction.
fn tip(&self) -> Tip<Balance>;
}
90 changes: 62 additions & 28 deletions core/sr-primitives/src/generic/unchecked_extrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

//! Generic implementation of an unchecked (pre-verification) extrinsic.

#[cfg(feature = "std")]
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;
use super::{CheckedExtrinsic, Tip};

#[derive(PartialEq, Eq, Clone, Encode, Decode)]
pub struct SignatureContent<Address, Index, Signature>
Expand All @@ -39,7 +38,7 @@ where
/// A extrinsic right from the external world. This is unchecked and so
/// can contain a signature.
#[derive(PartialEq, Eq, Clone)]
pub struct UncheckedExtrinsic<Address, Index, Call, Signature>
pub struct UncheckedExtrinsic<Address, Index, Call, Signature, Balance>
where
Address: Codec,
Index: HasCompact + Codec,
Expand All @@ -50,19 +49,27 @@ where
pub signature: Option<SignatureContent<Address, Index, Signature>>,
/// The function that should be called.
pub function: Call,
/// The tip for this transaction
pub tip: Tip<Balance>,
}

impl<Address, Index, Signature, Call> UncheckedExtrinsic<Address, Index, Call, Signature>
impl<Address, Index, Signature, Call, Balance> UncheckedExtrinsic<Address, Index, Call, Signature, Balance>
where
Address: Codec,
Index: HasCompact + Codec,
Signature: Codec,
{
/// New instance of a signed extrinsic aka "transaction".
pub fn new_signed(index: Index, function: Call, signed: Address, signature: Signature) -> Self {
pub fn new_signed(index: Index,
function: Call,
signed: Address,
signature: Signature,
tip: Tip<Balance>,
) -> Self {
UncheckedExtrinsic {
signature: Some(SignatureContent{signed, signature, index}),
function,
tip,
}
}

Expand All @@ -71,38 +78,44 @@ where
UncheckedExtrinsic {
signature: None,
function,
tip: Tip::None,
}
}
}

impl<Address, Index, Signature, Call, AccountId, Context> traits::Checkable<Context>
for UncheckedExtrinsic<Address, Index, Call, Signature>
impl<Address, Index, Signature, Call, AccountId, Context, Balance> traits::Checkable<Context>
for UncheckedExtrinsic<Address, Index, Call, Signature, Balance>
where
Address: Member + MaybeDisplay + Codec,
Index: Member + MaybeDisplay + SimpleArithmetic + Codec,
Call: Encode + Member,
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,
},
})
}
Expand All @@ -113,15 +126,20 @@ impl<
Index: HasCompact + Codec,
Signature: Codec,
Call,
> Extrinsic for UncheckedExtrinsic<Address, Index, Call, Signature> {
Balance,
> Extrinsic for UncheckedExtrinsic<Address, Index, Call, Signature, Balance> {
fn is_signed(&self) -> Option<bool> {
Some(self.signature.is_some())
}
}

impl<Address: Codec, Index: HasCompact + Codec, Signature: Codec, Call: Decode> Decode
for UncheckedExtrinsic<Address, Index, Call, Signature>
{
impl<
Address: Codec,
Index: HasCompact + Codec,
Signature: Codec,
Call: Decode,
Balance: Decode
> Decode for UncheckedExtrinsic<Address, Index, Call, Signature, Balance> {
fn decode<I: Input>(input: &mut I) -> Option<Self> {
// This is a little more complicated than usual since the binary format must be compatible
// with substrate's generic `Vec<u8>` type. Basically this just means accepting that there
Expand All @@ -132,52 +150,70 @@ impl<Address: Codec, Index: HasCompact + Codec, Signature: Codec, Call: Decode>
Some(UncheckedExtrinsic {
signature: Decode::decode(input)?,
function: Decode::decode(input)?,
tip: Decode::decode(input).unwrap_or_default(),
})
}
}

impl<Address: Codec, Index: HasCompact + Codec, Signature: Codec, Call: Encode> Encode
for UncheckedExtrinsic<Address, Index, Call, Signature>
{
impl<
Address: Codec,
Index: HasCompact + Codec,
Signature: Codec,
Call: Encode,
Balance: Encode
> Encode for UncheckedExtrinsic<Address, Index, Call, Signature, Balance> {
fn encode(&self) -> Vec<u8> {
super::encode_with_vec_prefix::<Self, _>(|v| {
self.signature.encode_to(v);
self.function.encode_to(v);
self.tip.encode_to(v);
})
}
}

#[cfg(feature = "std")]
impl<Address: Codec, Index: HasCompact + Codec, Signature: Codec, Call: Encode> serde::Serialize
for UncheckedExtrinsic<Address, Index, Call, Signature>
{
impl<
Address: Codec,
Index: HasCompact + Codec,
Signature: Codec,
Call: Encode,
Balance: Codec
> serde::Serialize for UncheckedExtrinsic<Address, Index, Call, Signature, Balance> {
fn serialize<S>(&self, seq: S) -> Result<S::Ok, S::Error> where S: ::serde::Serializer {
self.using_encoded(|bytes| ::substrate_primitives::bytes::serialize(bytes, seq))
}
}

#[cfg(feature = "std")]
impl<Address, Index, Signature, Call> fmt::Debug
for UncheckedExtrinsic<Address, Index, Call, Signature>
impl<Address, Index, Signature, Call, Balance> fmt::Debug
for UncheckedExtrinsic<Address, Index, Call, Signature, Balance>
where
Address: fmt::Debug + Codec,
Index: fmt::Debug + HasCompact + Codec,
Signature: Codec,
Call: fmt::Debug,
Balance: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "UncheckedExtrinsic({:?}, {:?})", self.signature.as_ref().map(|x| (&x.signed, &x.index)), self.function)
write!(
f,
"UncheckedExtrinsic({:?}, {:?}, {:?})",
self.signature.as_ref().map(|x| (&x.signed, &x.index)),
self.function,
self.tip,
)
}
}

#[cfg(test)]
mod test {
use crate::codec::{Decode, Encode};
use super::UncheckedExtrinsic;
use super::{UncheckedExtrinsic};
type Extrinsic = UncheckedExtrinsic<u32, u32, u32, u32, u32>;


#[test]
fn encoding_matches_vec() {
type Extrinsic = UncheckedExtrinsic<u32, u32, u32, u32>;
let ex = Extrinsic::new_unsigned(42);
let encoded = ex.encode();
let decoded = Extrinsic::decode(&mut encoded.as_slice()).unwrap();
Expand All @@ -186,13 +222,11 @@ mod test {
assert_eq!(as_vec.encode(), encoded);
}


#[test]
#[cfg(feature = "std")]
fn serialization_of_unchecked_extrinsics() {
type Extrinsic = UncheckedExtrinsic<u32, u32, u32, u32>;
type Extrinsic = UncheckedExtrinsic<u32, u32, u32, u32, u32>;
let ex = Extrinsic::new_unsigned(42);

assert_eq!(serde_json::to_string(&ex).unwrap(), "\"0x14002a000000\"");
assert_eq!(serde_json::to_string(&ex).unwrap(), "\"0x18002a00000000\"");
}
}
Loading