mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 18:41:05 +00:00
Extensible transactions (and tips) (#3102)
* Make extrinsics extensible. Also Remove old extrinsic types. * Rest of mockup. Add tips. * Fix some build issues * Runtiem builds :) * Substrate builds. * Fix a doc test * Compact encoding * Extract out the era logic into an extension * Weight Check signed extension. (#3115) * Weight signed extension. * Revert a bit + test for check era. * Update Cargo.toml * Update node/cli/src/factory_impl.rs * Update node/executor/src/lib.rs * Update node/executor/src/lib.rs * Don't use len for weight - use data. * Operational Transaction; second attempt (#3138) * working poc added. * some fixes. * Update doc. * Fix all tests + final logic. * more refactoring. * nits. * System block limit in bytes. * Silent the storage macro warnings. * More logic more tests. * Fix import. * Refactor names. * Fix build. * Update srml/balances/src/lib.rs * Final refactor. * Bump transaction version * Fix weight mult test. * Fix more tests and improve doc. * Bump. * Make some tests work again. * Fix subkey. * Remove todos + bump. * Ignore expensive test. * Bump.
This commit is contained in:
@@ -17,50 +17,83 @@
|
||||
//! Generic implementation of an extrinsic that has passed the verification
|
||||
//! stage.
|
||||
|
||||
use crate::traits::{self, Member, SimpleArithmetic, MaybeDisplay};
|
||||
use crate::weights::{Weighable, Weight};
|
||||
use rstd::result::Result;
|
||||
use crate::traits::{
|
||||
self, Member, MaybeDisplay, SignedExtension, DispatchError, Dispatchable, DispatchResult,
|
||||
ValidateUnsigned
|
||||
};
|
||||
use crate::weights::{GetDispatchInfo, DispatchInfo};
|
||||
use crate::transaction_validity::TransactionValidity;
|
||||
|
||||
/// 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, Call, Extra> {
|
||||
/// 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)>,
|
||||
pub signed: Option<(AccountId, Extra)>,
|
||||
|
||||
/// The function that should be called.
|
||||
pub function: Call,
|
||||
}
|
||||
|
||||
impl<AccountId, Index, Call> traits::Applyable for CheckedExtrinsic<AccountId, Index, Call>
|
||||
impl<AccountId, Call, Extra, Origin> traits::Applyable
|
||||
for
|
||||
CheckedExtrinsic<AccountId, Call, Extra>
|
||||
where
|
||||
AccountId: Member + MaybeDisplay,
|
||||
Index: Member + MaybeDisplay + SimpleArithmetic,
|
||||
Call: Member,
|
||||
Call: Member + Dispatchable<Origin=Origin>,
|
||||
Extra: SignedExtension<AccountId=AccountId>,
|
||||
Origin: From<Option<AccountId>>,
|
||||
{
|
||||
type Index = Index;
|
||||
type AccountId = AccountId;
|
||||
type Call = Call;
|
||||
|
||||
fn index(&self) -> Option<&Self::Index> {
|
||||
self.signed.as_ref().map(|x| &x.1)
|
||||
}
|
||||
type Call = Call;
|
||||
|
||||
fn sender(&self) -> Option<&Self::AccountId> {
|
||||
self.signed.as_ref().map(|x| &x.0)
|
||||
}
|
||||
|
||||
fn deconstruct(self) -> (Self::Call, Option<Self::AccountId>) {
|
||||
(self.function, self.signed.map(|x| x.0))
|
||||
fn validate<U: ValidateUnsigned<Call=Self::Call>>(&self,
|
||||
info: DispatchInfo,
|
||||
len: usize,
|
||||
) -> TransactionValidity {
|
||||
if let Some((ref id, ref extra)) = self.signed {
|
||||
Extra::validate(extra, id, info, len).into()
|
||||
} else {
|
||||
match Extra::validate_unsigned(info, len) {
|
||||
Ok(extra) => match U::validate_unsigned(&self.function) {
|
||||
TransactionValidity::Valid(v) =>
|
||||
TransactionValidity::Valid(v.combine_with(extra)),
|
||||
x => x,
|
||||
},
|
||||
x => x.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn dispatch(self,
|
||||
info: DispatchInfo,
|
||||
len: usize,
|
||||
) -> Result<DispatchResult, DispatchError> {
|
||||
let maybe_who = if let Some((id, extra)) = self.signed {
|
||||
Extra::pre_dispatch(extra, &id, info, len)?;
|
||||
Some(id)
|
||||
} else {
|
||||
Extra::pre_dispatch_unsigned(info, len)?;
|
||||
None
|
||||
};
|
||||
Ok(self.function.dispatch(Origin::from(maybe_who)))
|
||||
}
|
||||
}
|
||||
|
||||
impl<AccountId, Index, Call> Weighable for CheckedExtrinsic<AccountId, Index, Call>
|
||||
impl<AccountId, Call, Extra> GetDispatchInfo for CheckedExtrinsic<AccountId, Call, Extra>
|
||||
where
|
||||
Call: Weighable,
|
||||
Call: GetDispatchInfo,
|
||||
{
|
||||
fn weight(&self, len: usize) -> Weight {
|
||||
self.function.weight(len)
|
||||
fn get_dispatch_info(&self) -> DispatchInfo {
|
||||
self.function.get_dispatch_info()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,8 +19,6 @@
|
||||
// end::description[]
|
||||
|
||||
mod unchecked_extrinsic;
|
||||
mod unchecked_mortal_extrinsic;
|
||||
mod unchecked_mortal_compact_extrinsic;
|
||||
mod era;
|
||||
mod checked_extrinsic;
|
||||
mod header;
|
||||
@@ -30,8 +28,6 @@ mod digest;
|
||||
mod tests;
|
||||
|
||||
pub use self::unchecked_extrinsic::UncheckedExtrinsic;
|
||||
pub use self::unchecked_mortal_extrinsic::UncheckedMortalExtrinsic;
|
||||
pub use self::unchecked_mortal_compact_extrinsic::UncheckedMortalCompactExtrinsic;
|
||||
pub use self::era::{Era, Phase};
|
||||
pub use self::checked_extrinsic::CheckedExtrinsic;
|
||||
pub use self::header::Header;
|
||||
|
||||
@@ -20,48 +20,40 @@
|
||||
use std::fmt;
|
||||
|
||||
use rstd::prelude::*;
|
||||
use crate::codec::{Decode, Encode, Codec, Input, HasCompact};
|
||||
use crate::traits::{self, Member, SimpleArithmetic, MaybeDisplay, Lookup, Extrinsic};
|
||||
use runtime_io::blake2_256;
|
||||
use crate::codec::{Decode, Encode, Input};
|
||||
use crate::traits::{self, Member, MaybeDisplay, SignedExtension, Checkable, Extrinsic};
|
||||
use super::CheckedExtrinsic;
|
||||
|
||||
#[derive(PartialEq, Eq, Clone, Encode, Decode)]
|
||||
pub struct SignatureContent<Address, Index, Signature>
|
||||
where
|
||||
Address: Codec,
|
||||
Index: HasCompact + Codec,
|
||||
Signature: Codec,
|
||||
{
|
||||
signed: Address,
|
||||
signature: Signature,
|
||||
index: Index,
|
||||
}
|
||||
const TRANSACTION_VERSION: u8 = 2;
|
||||
|
||||
/// 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, Call, Signature, Extra>
|
||||
where
|
||||
Address: Codec,
|
||||
Index: HasCompact + Codec,
|
||||
Signature: Codec,
|
||||
Extra: SignedExtension
|
||||
{
|
||||
/// The signature, address and number of extrinsics have come before from
|
||||
/// the same signer, if this is a signed extrinsic.
|
||||
pub signature: Option<SignatureContent<Address, Index, Signature>>,
|
||||
/// The signature, address, number of extrinsics have come before from
|
||||
/// the same signer and an era describing the longevity of this transaction,
|
||||
/// if this is a signed extrinsic.
|
||||
pub signature: Option<(Address, Signature, Extra)>,
|
||||
/// The function that should be called.
|
||||
pub function: Call,
|
||||
}
|
||||
|
||||
impl<Address, Index, Signature, Call> UncheckedExtrinsic<Address, Index, Call, Signature>
|
||||
where
|
||||
Address: Codec,
|
||||
Index: HasCompact + Codec,
|
||||
Signature: Codec,
|
||||
impl<Address, Call, Signature, Extra: SignedExtension>
|
||||
UncheckedExtrinsic<Address, Call, Signature, Extra>
|
||||
{
|
||||
/// 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(
|
||||
function: Call,
|
||||
signed: Address,
|
||||
signature: Signature,
|
||||
extra: Extra
|
||||
) -> Self {
|
||||
UncheckedExtrinsic {
|
||||
signature: Some(SignatureContent{signed, signature, index}),
|
||||
signature: Some((signed, signature, extra)),
|
||||
function,
|
||||
}
|
||||
}
|
||||
@@ -75,29 +67,52 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<Address, Index, Signature, Call, AccountId, Context> traits::Checkable<Context>
|
||||
for UncheckedExtrinsic<Address, Index, Call, Signature>
|
||||
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>,
|
||||
impl<Address, Call, Signature, Extra: SignedExtension> Extrinsic
|
||||
for UncheckedExtrinsic<Address, Call, Signature, Extra>
|
||||
{
|
||||
type Checked = CheckedExtrinsic<AccountId, Index, Call>;
|
||||
type Call = Call;
|
||||
|
||||
fn check(self, context: &Context) -> Result<Self::Checked, &'static str> {
|
||||
fn is_signed(&self) -> Option<bool> {
|
||||
Some(self.signature.is_some())
|
||||
}
|
||||
|
||||
fn new_unsigned(function: Call) -> Option<Self> {
|
||||
Some(UncheckedExtrinsic::new_unsigned(function))
|
||||
}
|
||||
}
|
||||
|
||||
impl<Address, AccountId, Call, Signature, Extra, Lookup>
|
||||
Checkable<Lookup>
|
||||
for
|
||||
UncheckedExtrinsic<Address, Call, Signature, Extra>
|
||||
where
|
||||
Address: Member + MaybeDisplay,
|
||||
Call: Encode + Member,
|
||||
Signature: Member + traits::Verify<Signer=AccountId>,
|
||||
Extra: SignedExtension<AccountId=AccountId>,
|
||||
AccountId: Member + MaybeDisplay,
|
||||
Lookup: traits::Lookup<Source=Address, Target=AccountId>
|
||||
{
|
||||
type Checked = CheckedExtrinsic<AccountId, Call, Extra>;
|
||||
|
||||
fn check(self, lookup: &Lookup) -> Result<Self::Checked, &'static str> {
|
||||
Ok(match self.signature {
|
||||
Some(SignatureContent{signed, signature, index}) => {
|
||||
let payload = (index, self.function);
|
||||
let signed = context.lookup(signed)?;
|
||||
if !crate::verify_encoded_lazy(&signature, &payload, &signed) {
|
||||
Some((signed, signature, extra)) => {
|
||||
let additional_signed = extra.additional_signed()?;
|
||||
let raw_payload = (self.function, extra, additional_signed);
|
||||
let signed = lookup.lookup(signed)?;
|
||||
if !raw_payload.using_encoded(|payload| {
|
||||
if payload.len() > 256 {
|
||||
signature.verify(&blake2_256(payload)[..], &signed)
|
||||
} else {
|
||||
signature.verify(payload, &signed)
|
||||
}
|
||||
}) {
|
||||
return Err(crate::BAD_SIGNATURE)
|
||||
}
|
||||
CheckedExtrinsic {
|
||||
signed: Some((signed, payload.0)),
|
||||
function: payload.1,
|
||||
signed: Some((signed, raw_payload.1)),
|
||||
function: raw_payload.0,
|
||||
}
|
||||
}
|
||||
None => CheckedExtrinsic {
|
||||
@@ -108,25 +123,13 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
Address: Codec,
|
||||
Index: HasCompact + Codec,
|
||||
Signature: Codec,
|
||||
Call,
|
||||
> Extrinsic for UncheckedExtrinsic<Address, Index, Call, Signature> {
|
||||
type Call = Call;
|
||||
|
||||
fn is_signed(&self) -> Option<bool> {
|
||||
Some(self.signature.is_some())
|
||||
}
|
||||
|
||||
fn new_unsigned(call: Self::Call) -> Option<Self> {
|
||||
Some(UncheckedExtrinsic::new_unsigned(call))
|
||||
}
|
||||
}
|
||||
|
||||
impl<Address: Codec, Index: HasCompact + Codec, Signature: Codec, Call: Decode> Decode
|
||||
for UncheckedExtrinsic<Address, Index, Call, Signature>
|
||||
impl<Address, Call, Signature, Extra> Decode
|
||||
for UncheckedExtrinsic<Address, Call, Signature, Extra>
|
||||
where
|
||||
Address: Decode,
|
||||
Signature: Decode,
|
||||
Call: Decode,
|
||||
Extra: SignedExtension,
|
||||
{
|
||||
fn decode<I: Input>(input: &mut I) -> Option<Self> {
|
||||
// This is a little more complicated than usual since the binary format must be compatible
|
||||
@@ -135,70 +138,191 @@ impl<Address: Codec, Index: HasCompact + Codec, Signature: Codec, Call: Decode>
|
||||
// to use this).
|
||||
let _length_do_not_remove_me_see_above: Vec<()> = Decode::decode(input)?;
|
||||
|
||||
let version = input.read_byte()?;
|
||||
|
||||
let is_signed = version & 0b1000_0000 != 0;
|
||||
let version = version & 0b0111_1111;
|
||||
if version != TRANSACTION_VERSION {
|
||||
return None
|
||||
}
|
||||
|
||||
Some(UncheckedExtrinsic {
|
||||
signature: Decode::decode(input)?,
|
||||
signature: if is_signed { Some(Decode::decode(input)?) } else { None },
|
||||
function: Decode::decode(input)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<Address: Codec, Index: HasCompact + Codec, Signature: Codec, Call: Encode> Encode
|
||||
for UncheckedExtrinsic<Address, Index, Call, Signature>
|
||||
impl<Address, Call, Signature, Extra> Encode
|
||||
for UncheckedExtrinsic<Address, Call, Signature, Extra>
|
||||
where
|
||||
Address: Encode,
|
||||
Signature: Encode,
|
||||
Call: Encode,
|
||||
Extra: SignedExtension,
|
||||
{
|
||||
fn encode(&self) -> Vec<u8> {
|
||||
super::encode_with_vec_prefix::<Self, _>(|v| {
|
||||
self.signature.encode_to(v);
|
||||
// 1 byte version id.
|
||||
match self.signature.as_ref() {
|
||||
Some(s) => {
|
||||
v.push(TRANSACTION_VERSION | 0b1000_0000);
|
||||
s.encode_to(v);
|
||||
}
|
||||
None => {
|
||||
v.push(TRANSACTION_VERSION & 0b0111_1111);
|
||||
}
|
||||
}
|
||||
self.function.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: Encode, Signature: Encode, Call: Encode, Extra: SignedExtension> serde::Serialize
|
||||
for UncheckedExtrinsic<Address, Call, Signature, Extra>
|
||||
{
|
||||
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))
|
||||
self.using_encoded(|bytes| seq.serialize_bytes(bytes))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<Address, Index, Signature, Call> fmt::Debug
|
||||
for UncheckedExtrinsic<Address, Index, Call, Signature>
|
||||
impl<Address, Call, Signature, Extra> fmt::Debug
|
||||
for UncheckedExtrinsic<Address, Call, Signature, Extra>
|
||||
where
|
||||
Address: fmt::Debug + Codec,
|
||||
Index: fmt::Debug + HasCompact + Codec,
|
||||
Signature: Codec,
|
||||
Address: fmt::Debug,
|
||||
Call: fmt::Debug,
|
||||
Extra: SignedExtension,
|
||||
{
|
||||
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.0, &x.2)), self.function)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::codec::{Decode, Encode};
|
||||
use super::UncheckedExtrinsic;
|
||||
mod tests {
|
||||
use super::*;
|
||||
use runtime_io::blake2_256;
|
||||
use crate::codec::{Encode, Decode};
|
||||
use crate::traits::{SignedExtension, BlockNumberToHash, Lookup, CurrentHeight};
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
struct TestContext;
|
||||
impl Lookup for TestContext {
|
||||
type Source = u64;
|
||||
type Target = u64;
|
||||
fn lookup(&self, s: u64) -> Result<u64, &'static str> { Ok(s) }
|
||||
}
|
||||
impl CurrentHeight for TestContext {
|
||||
type BlockNumber = u64;
|
||||
fn current_height(&self) -> u64 { 42 }
|
||||
}
|
||||
impl BlockNumberToHash for TestContext {
|
||||
type BlockNumber = u64;
|
||||
type Hash = u64;
|
||||
fn block_number_to_hash(&self, n: u64) -> Option<u64> { Some(n) }
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq, Clone, Debug, Serialize, Deserialize, Encode, Decode)]
|
||||
struct TestSig(u64, Vec<u8>);
|
||||
impl traits::Verify for TestSig {
|
||||
type Signer = u64;
|
||||
fn verify<L: traits::Lazy<[u8]>>(&self, mut msg: L, signer: &Self::Signer) -> bool {
|
||||
*signer == self.0 && msg.get() == &self.1[..]
|
||||
}
|
||||
}
|
||||
|
||||
type TestAccountId = u64;
|
||||
type TestCall = Vec<u8>;
|
||||
|
||||
const TEST_ACCOUNT: TestAccountId = 0;
|
||||
|
||||
// NOTE: this is demonstration. One can simply use `()` for testing.
|
||||
#[derive(Debug, Encode, Decode, Clone, Eq, PartialEq, Ord, PartialOrd)]
|
||||
struct TestExtra;
|
||||
impl SignedExtension for TestExtra {
|
||||
type AccountId = u64;
|
||||
type AdditionalSigned = ();
|
||||
fn additional_signed(&self) -> rstd::result::Result<(), &'static str> { Ok(()) }
|
||||
}
|
||||
|
||||
type Ex = UncheckedExtrinsic<TestAccountId, TestCall, TestSig, TestExtra>;
|
||||
type CEx = CheckedExtrinsic<TestAccountId, TestCall, TestExtra>;
|
||||
|
||||
#[test]
|
||||
fn unsigned_codec_should_work() {
|
||||
let ux = Ex::new_unsigned(vec![0u8; 0]);
|
||||
let encoded = ux.encode();
|
||||
assert_eq!(Ex::decode(&mut &encoded[..]), Some(ux));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn signed_codec_should_work() {
|
||||
let ux = Ex::new_signed(
|
||||
vec![0u8; 0],
|
||||
TEST_ACCOUNT,
|
||||
TestSig(TEST_ACCOUNT, (vec![0u8; 0], TestExtra).encode()),
|
||||
TestExtra
|
||||
);
|
||||
let encoded = ux.encode();
|
||||
assert_eq!(Ex::decode(&mut &encoded[..]), Some(ux));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn large_signed_codec_should_work() {
|
||||
let ux = Ex::new_signed(
|
||||
vec![0u8; 0],
|
||||
TEST_ACCOUNT,
|
||||
TestSig(TEST_ACCOUNT, (vec![0u8; 257], TestExtra)
|
||||
.using_encoded(blake2_256)[..].to_owned()),
|
||||
TestExtra
|
||||
);
|
||||
let encoded = ux.encode();
|
||||
assert_eq!(Ex::decode(&mut &encoded[..]), Some(ux));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsigned_check_should_work() {
|
||||
let ux = Ex::new_unsigned(vec![0u8; 0]);
|
||||
assert!(!ux.is_signed().unwrap_or(false));
|
||||
assert!(<Ex as Checkable<TestContext>>::check(ux, &TestContext).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn badly_signed_check_should_fail() {
|
||||
let ux = Ex::new_signed(
|
||||
vec![0u8; 0],
|
||||
TEST_ACCOUNT,
|
||||
TestSig(TEST_ACCOUNT, vec![0u8; 0]),
|
||||
TestExtra
|
||||
);
|
||||
assert!(ux.is_signed().unwrap_or(false));
|
||||
assert_eq!(<Ex as Checkable<TestContext>>::check(ux, &TestContext), Err(crate::BAD_SIGNATURE));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn signed_check_should_work() {
|
||||
let ux = Ex::new_signed(
|
||||
vec![0u8; 0],
|
||||
TEST_ACCOUNT,
|
||||
TestSig(TEST_ACCOUNT, (vec![0u8; 0], TestExtra).encode()),
|
||||
TestExtra
|
||||
);
|
||||
assert!(ux.is_signed().unwrap_or(false));
|
||||
assert_eq!(
|
||||
<Ex as Checkable<TestContext>>::check(ux, &TestContext),
|
||||
Ok(CEx { signed: Some((TEST_ACCOUNT, TestExtra)), function: vec![0u8; 0] })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encoding_matches_vec() {
|
||||
type Extrinsic = UncheckedExtrinsic<u32, u32, u32, u32>;
|
||||
let ex = Extrinsic::new_unsigned(42);
|
||||
let ex = Ex::new_unsigned(vec![0u8; 0]);
|
||||
let encoded = ex.encode();
|
||||
let decoded = Extrinsic::decode(&mut encoded.as_slice()).unwrap();
|
||||
let decoded = Ex::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]
|
||||
#[cfg(feature = "std")]
|
||||
fn serialization_of_unchecked_extrinsics() {
|
||||
type Extrinsic = UncheckedExtrinsic<u32, u32, u32, u32>;
|
||||
let ex = Extrinsic::new_unsigned(42);
|
||||
|
||||
assert_eq!(serde_json::to_string(&ex).unwrap(), "\"0x14002a000000\"");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,312 +0,0 @@
|
||||
// Copyright 2017-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/>.
|
||||
|
||||
//! Generic implementation of an unchecked (pre-verification) extrinsic.
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use std::fmt;
|
||||
|
||||
use rstd::prelude::*;
|
||||
use runtime_io::blake2_256;
|
||||
use crate::codec::{Decode, Encode, Input, Compact};
|
||||
use crate::traits::{self, Member, SimpleArithmetic, MaybeDisplay, CurrentHeight, BlockNumberToHash,
|
||||
Lookup, Checkable, Extrinsic, SaturatedConversion};
|
||||
use super::{CheckedExtrinsic, Era};
|
||||
|
||||
const TRANSACTION_VERSION: u8 = 1;
|
||||
|
||||
/// A extrinsic right from the external world. This is unchecked and so
|
||||
/// can contain a signature.
|
||||
#[derive(PartialEq, Eq, Clone)]
|
||||
pub struct UncheckedMortalCompactExtrinsic<Address, Index, Call, Signature> {
|
||||
/// The signature, address, number of extrinsics have come before from
|
||||
/// the same signer and an era describing the longevity of this transaction,
|
||||
/// if this is a signed extrinsic.
|
||||
pub signature: Option<(Address, Signature, Compact<Index>, Era)>,
|
||||
/// The function that should be called.
|
||||
pub function: Call,
|
||||
}
|
||||
|
||||
impl<Address, Index, Call, Signature> UncheckedMortalCompactExtrinsic<Address, Index, Call, Signature> {
|
||||
/// New instance of a signed extrinsic aka "transaction".
|
||||
pub fn new_signed(index: Index, function: Call, signed: Address, signature: Signature, era: Era) -> Self {
|
||||
UncheckedMortalCompactExtrinsic {
|
||||
signature: Some((signed, signature, index.into(), era)),
|
||||
function,
|
||||
}
|
||||
}
|
||||
|
||||
/// New instance of an unsigned extrinsic aka "inherent".
|
||||
pub fn new_unsigned(function: Call) -> Self {
|
||||
UncheckedMortalCompactExtrinsic {
|
||||
signature: None,
|
||||
function,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Address: Encode, Index: Encode, Call: Encode, Signature: Encode> Extrinsic for UncheckedMortalCompactExtrinsic<Address, Index, Call, Signature> {
|
||||
type Call = Call;
|
||||
|
||||
fn is_signed(&self) -> Option<bool> {
|
||||
Some(self.signature.is_some())
|
||||
}
|
||||
|
||||
fn new_unsigned(call: Self::Call) -> Option<Self> {
|
||||
Some(UncheckedMortalCompactExtrinsic::new_unsigned(call))
|
||||
}
|
||||
}
|
||||
|
||||
impl<Address, AccountId, Index, Call, Signature, Context, Hash, BlockNumber> Checkable<Context>
|
||||
for UncheckedMortalCompactExtrinsic<Address, Index, Call, Signature>
|
||||
where
|
||||
Address: Member + MaybeDisplay,
|
||||
Index: Member + MaybeDisplay + SimpleArithmetic,
|
||||
Compact<Index>: Encode,
|
||||
Call: Encode + Member,
|
||||
Signature: Member + traits::Verify<Signer=AccountId>,
|
||||
AccountId: Member + MaybeDisplay,
|
||||
BlockNumber: SimpleArithmetic,
|
||||
Hash: Encode,
|
||||
Context: Lookup<Source=Address, Target=AccountId>
|
||||
+ CurrentHeight<BlockNumber=BlockNumber>
|
||||
+ BlockNumberToHash<BlockNumber=BlockNumber, Hash=Hash>,
|
||||
{
|
||||
type Checked = CheckedExtrinsic<AccountId, Index, Call>;
|
||||
|
||||
fn check(self, context: &Context) -> Result<Self::Checked, &'static str> {
|
||||
Ok(match self.signature {
|
||||
Some((signed, signature, index, era)) => {
|
||||
let current_u64 = context.current_height().saturated_into::<u64>();
|
||||
let h = context.block_number_to_hash(era.birth(current_u64).saturated_into())
|
||||
.ok_or("transaction birth block ancient")?;
|
||||
let signed = context.lookup(signed)?;
|
||||
let raw_payload = (index, self.function, era, h);
|
||||
if !raw_payload.using_encoded(|payload| {
|
||||
if payload.len() > 256 {
|
||||
signature.verify(&blake2_256(payload)[..], &signed)
|
||||
} else {
|
||||
signature.verify(payload, &signed)
|
||||
}
|
||||
}) {
|
||||
return Err(crate::BAD_SIGNATURE)
|
||||
}
|
||||
CheckedExtrinsic {
|
||||
signed: Some((signed, (raw_payload.0).0)),
|
||||
function: raw_payload.1,
|
||||
}
|
||||
}
|
||||
None => CheckedExtrinsic {
|
||||
signed: None,
|
||||
function: self.function,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<Address, Index, Call, Signature> Decode
|
||||
for UncheckedMortalCompactExtrinsic<Address, Index, Call, Signature>
|
||||
where
|
||||
Address: Decode,
|
||||
Signature: Decode,
|
||||
Compact<Index>: Decode,
|
||||
Call: Decode,
|
||||
{
|
||||
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
|
||||
// will be a prefix of vector length (we don't need
|
||||
// to use this).
|
||||
let _length_do_not_remove_me_see_above: Vec<()> = Decode::decode(input)?;
|
||||
|
||||
let version = input.read_byte()?;
|
||||
|
||||
let is_signed = version & 0b1000_0000 != 0;
|
||||
let version = version & 0b0111_1111;
|
||||
if version != TRANSACTION_VERSION {
|
||||
return None
|
||||
}
|
||||
|
||||
Some(UncheckedMortalCompactExtrinsic {
|
||||
signature: if is_signed { Some(Decode::decode(input)?) } else { None },
|
||||
function: Decode::decode(input)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<Address, Index, Call, Signature> Encode
|
||||
for UncheckedMortalCompactExtrinsic<Address, Index, Call, Signature>
|
||||
where
|
||||
Address: Encode,
|
||||
Signature: Encode,
|
||||
Compact<Index>: Encode,
|
||||
Call: Encode,
|
||||
{
|
||||
fn encode(&self) -> Vec<u8> {
|
||||
super::encode_with_vec_prefix::<Self, _>(|v| {
|
||||
// 1 byte version id.
|
||||
match self.signature.as_ref() {
|
||||
Some(s) => {
|
||||
v.push(TRANSACTION_VERSION | 0b1000_0000);
|
||||
s.encode_to(v);
|
||||
}
|
||||
None => {
|
||||
v.push(TRANSACTION_VERSION & 0b0111_1111);
|
||||
}
|
||||
}
|
||||
self.function.encode_to(v);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<Address: Encode, Index, Signature: Encode, Call: Encode> serde::Serialize
|
||||
for UncheckedMortalCompactExtrinsic<Address, Index, Call, Signature>
|
||||
where Compact<Index>: Encode
|
||||
{
|
||||
fn serialize<S>(&self, seq: S) -> Result<S::Ok, S::Error> where S: ::serde::Serializer {
|
||||
self.using_encoded(|bytes| seq.serialize_bytes(bytes))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<Address, Index, Call, Signature> fmt::Debug for UncheckedMortalCompactExtrinsic<Address, Index, Call, Signature> where
|
||||
Address: fmt::Debug,
|
||||
Index: fmt::Debug,
|
||||
Call: fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "UncheckedMortalCompactExtrinsic({:?}, {:?})", self.signature.as_ref().map(|x| (&x.0, &x.2)), self.function)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use runtime_io::blake2_256;
|
||||
use crate::codec::{Encode, Decode};
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
struct TestContext;
|
||||
impl Lookup for TestContext {
|
||||
type Source = u64;
|
||||
type Target = u64;
|
||||
fn lookup(&self, s: u64) -> Result<u64, &'static str> { Ok(s) }
|
||||
}
|
||||
impl CurrentHeight for TestContext {
|
||||
type BlockNumber = u64;
|
||||
fn current_height(&self) -> u64 { 42 }
|
||||
}
|
||||
impl BlockNumberToHash for TestContext {
|
||||
type BlockNumber = u64;
|
||||
type Hash = u64;
|
||||
fn block_number_to_hash(&self, n: u64) -> Option<u64> { Some(n) }
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq, Clone, Debug, Serialize, Deserialize, Encode, Decode)]
|
||||
struct TestSig(u64, Vec<u8>);
|
||||
impl traits::Verify for TestSig {
|
||||
type Signer = u64;
|
||||
fn verify<L: traits::Lazy<[u8]>>(&self, mut msg: L, signer: &Self::Signer) -> bool {
|
||||
*signer == self.0 && msg.get() == &self.1[..]
|
||||
}
|
||||
}
|
||||
|
||||
const DUMMY_ACCOUNTID: u64 = 0;
|
||||
|
||||
type Ex = UncheckedMortalCompactExtrinsic<u64, u64, Vec<u8>, TestSig>;
|
||||
type CEx = CheckedExtrinsic<u64, u64, Vec<u8>>;
|
||||
|
||||
#[test]
|
||||
fn unsigned_codec_should_work() {
|
||||
let ux = Ex::new_unsigned(vec![0u8;0]);
|
||||
let encoded = ux.encode();
|
||||
assert_eq!(Ex::decode(&mut &encoded[..]), Some(ux));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn signed_codec_should_work() {
|
||||
let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (DUMMY_ACCOUNTID, vec![0u8;0], Era::immortal(), 0u64).encode()), Era::immortal());
|
||||
let encoded = ux.encode();
|
||||
assert_eq!(Ex::decode(&mut &encoded[..]), Some(ux));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn large_signed_codec_should_work() {
|
||||
let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (DUMMY_ACCOUNTID, vec![0u8; 257], Era::immortal(), 0u64).using_encoded(blake2_256)[..].to_owned()), Era::immortal());
|
||||
let encoded = ux.encode();
|
||||
assert_eq!(Ex::decode(&mut &encoded[..]), Some(ux));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsigned_check_should_work() {
|
||||
let ux = Ex::new_unsigned(vec![0u8;0]);
|
||||
assert!(!ux.is_signed().unwrap_or(false));
|
||||
assert!(<Ex as Checkable<TestContext>>::check(ux, &TestContext).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn badly_signed_check_should_fail() {
|
||||
let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, vec![0u8]), Era::immortal());
|
||||
assert!(ux.is_signed().unwrap_or(false));
|
||||
assert_eq!(<Ex as Checkable<TestContext>>::check(ux, &TestContext), Err(crate::BAD_SIGNATURE));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn immortal_signed_check_should_work() {
|
||||
let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (Compact::from(DUMMY_ACCOUNTID), vec![0u8;0], Era::immortal(), 0u64).encode()), Era::immortal());
|
||||
assert!(ux.is_signed().unwrap_or(false));
|
||||
assert_eq!(<Ex as Checkable<TestContext>>::check(ux, &TestContext), Ok(CEx { signed: Some((DUMMY_ACCOUNTID, 0)), function: vec![0u8;0] }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mortal_signed_check_should_work() {
|
||||
let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (Compact::from(DUMMY_ACCOUNTID), vec![0u8;0], Era::mortal(32, 42), 42u64).encode()), Era::mortal(32, 42));
|
||||
assert!(ux.is_signed().unwrap_or(false));
|
||||
assert_eq!(<Ex as Checkable<TestContext>>::check(ux, &TestContext), Ok(CEx { signed: Some((DUMMY_ACCOUNTID, 0)), function: vec![0u8;0] }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn later_mortal_signed_check_should_work() {
|
||||
let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (Compact::from(DUMMY_ACCOUNTID), vec![0u8;0], Era::mortal(32, 11), 11u64).encode()), Era::mortal(32, 11));
|
||||
assert!(ux.is_signed().unwrap_or(false));
|
||||
assert_eq!(<Ex as Checkable<TestContext>>::check(ux, &TestContext), Ok(CEx { signed: Some((DUMMY_ACCOUNTID, 0)), function: vec![0u8;0] }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn too_late_mortal_signed_check_should_fail() {
|
||||
let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (DUMMY_ACCOUNTID, vec![0u8;0], Era::mortal(32, 10), 10u64).encode()), Era::mortal(32, 10));
|
||||
assert!(ux.is_signed().unwrap_or(false));
|
||||
assert_eq!(<Ex as Checkable<TestContext>>::check(ux, &TestContext), Err(crate::BAD_SIGNATURE));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn too_early_mortal_signed_check_should_fail() {
|
||||
let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (DUMMY_ACCOUNTID, vec![0u8;0], Era::mortal(32, 43), 43u64).encode()), Era::mortal(32, 43));
|
||||
assert!(ux.is_signed().unwrap_or(false));
|
||||
assert_eq!(<Ex as Checkable<TestContext>>::check(ux, &TestContext), Err(crate::BAD_SIGNATURE));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encoding_matches_vec() {
|
||||
let ex = Ex::new_unsigned(vec![0u8;0]);
|
||||
let encoded = ex.encode();
|
||||
let decoded = Ex::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);
|
||||
}
|
||||
}
|
||||
@@ -1,313 +0,0 @@
|
||||
// Copyright 2017-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/>.
|
||||
|
||||
//! Generic implementation of an unchecked (pre-verification) extrinsic.
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use std::fmt;
|
||||
|
||||
use rstd::prelude::*;
|
||||
use runtime_io::blake2_256;
|
||||
use crate::codec::{Decode, Encode, Input};
|
||||
use crate::traits::{
|
||||
self, Member, SimpleArithmetic, MaybeDisplay, CurrentHeight, BlockNumberToHash,
|
||||
Lookup, Checkable, Extrinsic, SaturatedConversion
|
||||
};
|
||||
use super::{CheckedExtrinsic, Era};
|
||||
|
||||
const TRANSACTION_VERSION: u8 = 1;
|
||||
|
||||
/// A extrinsic right from the external world. This is unchecked and so
|
||||
/// can contain a signature.
|
||||
#[derive(PartialEq, Eq, Clone)]
|
||||
pub struct UncheckedMortalExtrinsic<Address, Index, Call, Signature> {
|
||||
/// The signature, address, number of extrinsics have come before from
|
||||
/// the same signer and an era describing the longevity of this transaction,
|
||||
/// if this is a signed extrinsic.
|
||||
pub signature: Option<(Address, Signature, Index, Era)>,
|
||||
/// The function that should be called.
|
||||
pub function: Call,
|
||||
}
|
||||
|
||||
impl<Address, Index, Call, Signature> UncheckedMortalExtrinsic<Address, Index, Call, Signature> {
|
||||
/// New instance of a signed extrinsic aka "transaction".
|
||||
pub fn new_signed(index: Index, function: Call, signed: Address, signature: Signature, era: Era) -> Self {
|
||||
UncheckedMortalExtrinsic {
|
||||
signature: Some((signed, signature, index, era)),
|
||||
function,
|
||||
}
|
||||
}
|
||||
|
||||
/// New instance of an unsigned extrinsic aka "inherent".
|
||||
pub fn new_unsigned(function: Call) -> Self {
|
||||
UncheckedMortalExtrinsic {
|
||||
signature: None,
|
||||
function,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Address: Encode, Index: Encode, Call: Encode, Signature: Encode> Extrinsic for UncheckedMortalExtrinsic<Address, Index, Call, Signature> {
|
||||
type Call = Call;
|
||||
|
||||
fn is_signed(&self) -> Option<bool> {
|
||||
Some(self.signature.is_some())
|
||||
}
|
||||
|
||||
fn new_unsigned(call: Self::Call) -> Option<Self> {
|
||||
Some(UncheckedMortalExtrinsic::new_unsigned(call))
|
||||
}
|
||||
}
|
||||
|
||||
impl<Address, AccountId, Index, Call, Signature, Context, Hash, BlockNumber> Checkable<Context>
|
||||
for UncheckedMortalExtrinsic<Address, Index, Call, Signature>
|
||||
where
|
||||
Address: Member + MaybeDisplay,
|
||||
Index: Encode + Member + MaybeDisplay + SimpleArithmetic,
|
||||
Call: Encode + Member,
|
||||
Signature: Member + traits::Verify<Signer=AccountId>,
|
||||
AccountId: Member + MaybeDisplay,
|
||||
BlockNumber: SimpleArithmetic,
|
||||
Hash: Encode,
|
||||
Context: Lookup<Source=Address, Target=AccountId>
|
||||
+ CurrentHeight<BlockNumber=BlockNumber>
|
||||
+ BlockNumberToHash<BlockNumber=BlockNumber, Hash=Hash>,
|
||||
{
|
||||
type Checked = CheckedExtrinsic<AccountId, Index, Call>;
|
||||
|
||||
fn check(self, context: &Context) -> Result<Self::Checked, &'static str> {
|
||||
Ok(match self.signature {
|
||||
Some((signed, signature, index, era)) => {
|
||||
let current_u64 = context.current_height().saturated_into::<u64>();
|
||||
let h = context.block_number_to_hash(era.birth(current_u64).saturated_into())
|
||||
.ok_or("transaction birth block ancient")?;
|
||||
let signed = context.lookup(signed)?;
|
||||
let raw_payload = (index, self.function, era, h);
|
||||
|
||||
if !raw_payload.using_encoded(|payload| {
|
||||
if payload.len() > 256 {
|
||||
signature.verify(&blake2_256(payload)[..], &signed)
|
||||
} else {
|
||||
signature.verify(payload, &signed)
|
||||
}
|
||||
}) {
|
||||
return Err(crate::BAD_SIGNATURE)
|
||||
}
|
||||
CheckedExtrinsic {
|
||||
signed: Some((signed, raw_payload.0)),
|
||||
function: raw_payload.1,
|
||||
}
|
||||
}
|
||||
None => CheckedExtrinsic {
|
||||
signed: None,
|
||||
function: self.function,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<Address, Index, Call, Signature> Decode
|
||||
for UncheckedMortalExtrinsic<Address, Index, Call, Signature>
|
||||
where
|
||||
Address: Decode,
|
||||
Signature: Decode,
|
||||
Index: Decode,
|
||||
Call: Decode,
|
||||
{
|
||||
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
|
||||
// will be a prefix of vector length (we don't need
|
||||
// to use this).
|
||||
let _length_do_not_remove_me_see_above: Vec<()> = Decode::decode(input)?;
|
||||
|
||||
let version = input.read_byte()?;
|
||||
|
||||
let is_signed = version & 0b1000_0000 != 0;
|
||||
let version = version & 0b0111_1111;
|
||||
if version != TRANSACTION_VERSION {
|
||||
return None
|
||||
}
|
||||
|
||||
Some(UncheckedMortalExtrinsic {
|
||||
signature: if is_signed { Some(Decode::decode(input)?) } else { None },
|
||||
function: Decode::decode(input)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<Address, Index, Call, Signature> Encode
|
||||
for UncheckedMortalExtrinsic<Address, Index, Call, Signature>
|
||||
where
|
||||
Address: Encode,
|
||||
Signature: Encode,
|
||||
Index: Encode,
|
||||
Call: Encode,
|
||||
{
|
||||
fn encode(&self) -> Vec<u8> {
|
||||
super::encode_with_vec_prefix::<Self, _>(|v| {
|
||||
// 1 byte version id.
|
||||
match self.signature.as_ref() {
|
||||
Some(s) => {
|
||||
v.push(TRANSACTION_VERSION | 0b1000_0000);
|
||||
s.encode_to(v);
|
||||
}
|
||||
None => {
|
||||
v.push(TRANSACTION_VERSION & 0b0111_1111);
|
||||
}
|
||||
}
|
||||
self.function.encode_to(v);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<Address: Encode, Index: Encode, Signature: Encode, Call: Encode> serde::Serialize
|
||||
for UncheckedMortalExtrinsic<Address, Index, Call, Signature>
|
||||
{
|
||||
fn serialize<S>(&self, seq: S) -> Result<S::Ok, S::Error> where S: ::serde::Serializer {
|
||||
self.using_encoded(|bytes| seq.serialize_bytes(bytes))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<Address, Index, Call, Signature> fmt::Debug for UncheckedMortalExtrinsic<Address, Index, Call, Signature> where
|
||||
Address: fmt::Debug,
|
||||
Index: fmt::Debug,
|
||||
Call: fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "UncheckedMortalExtrinsic({:?}, {:?})", self.signature.as_ref().map(|x| (&x.0, &x.2)), self.function)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use runtime_io::blake2_256;
|
||||
use crate::codec::{Encode, Decode};
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
struct TestContext;
|
||||
impl Lookup for TestContext {
|
||||
type Source = u64;
|
||||
type Target = u64;
|
||||
fn lookup(&self, s: u64) -> Result<u64, &'static str> { Ok(s) }
|
||||
}
|
||||
impl CurrentHeight for TestContext {
|
||||
type BlockNumber = u64;
|
||||
fn current_height(&self) -> u64 { 42 }
|
||||
}
|
||||
impl BlockNumberToHash for TestContext {
|
||||
type BlockNumber = u64;
|
||||
type Hash = u64;
|
||||
fn block_number_to_hash(&self, n: u64) -> Option<u64> { Some(n) }
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq, Clone, Debug, Serialize, Deserialize, Encode, Decode)]
|
||||
struct TestSig(u64, Vec<u8>);
|
||||
impl traits::Verify for TestSig {
|
||||
type Signer = u64;
|
||||
fn verify<L: traits::Lazy<[u8]>>(&self, mut msg: L, signer: &Self::Signer) -> bool {
|
||||
*signer == self.0 && msg.get() == &self.1[..]
|
||||
}
|
||||
}
|
||||
|
||||
const DUMMY_ACCOUNTID: u64 = 0;
|
||||
|
||||
type Ex = UncheckedMortalExtrinsic<u64, u64, Vec<u8>, TestSig>;
|
||||
type CEx = CheckedExtrinsic<u64, u64, Vec<u8>>;
|
||||
|
||||
#[test]
|
||||
fn unsigned_codec_should_work() {
|
||||
let ux = Ex::new_unsigned(vec![0u8;0]);
|
||||
let encoded = ux.encode();
|
||||
assert_eq!(Ex::decode(&mut &encoded[..]), Some(ux));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn signed_codec_should_work() {
|
||||
let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (DUMMY_ACCOUNTID, vec![0u8;0], Era::immortal(), 0u64).encode()), Era::immortal());
|
||||
let encoded = ux.encode();
|
||||
assert_eq!(Ex::decode(&mut &encoded[..]), Some(ux));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn large_signed_codec_should_work() {
|
||||
let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (DUMMY_ACCOUNTID, vec![0u8; 257], Era::immortal(), 0u64).using_encoded(blake2_256)[..].to_owned()), Era::immortal());
|
||||
let encoded = ux.encode();
|
||||
assert_eq!(Ex::decode(&mut &encoded[..]), Some(ux));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsigned_check_should_work() {
|
||||
let ux = Ex::new_unsigned(vec![0u8;0]);
|
||||
assert!(!ux.is_signed().unwrap_or(false));
|
||||
assert!(<Ex as Checkable<TestContext>>::check(ux, &TestContext).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn badly_signed_check_should_fail() {
|
||||
let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, vec![0u8]), Era::immortal());
|
||||
assert!(ux.is_signed().unwrap_or(false));
|
||||
assert_eq!(<Ex as Checkable<TestContext>>::check(ux, &TestContext), Err(crate::BAD_SIGNATURE));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn immortal_signed_check_should_work() {
|
||||
let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (DUMMY_ACCOUNTID, vec![0u8;0], Era::immortal(), 0u64).encode()), Era::immortal());
|
||||
assert!(ux.is_signed().unwrap_or(false));
|
||||
assert_eq!(<Ex as Checkable<TestContext>>::check(ux, &TestContext), Ok(CEx { signed: Some((DUMMY_ACCOUNTID, 0)), function: vec![0u8;0] }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mortal_signed_check_should_work() {
|
||||
let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (DUMMY_ACCOUNTID, vec![0u8;0], Era::mortal(32, 42), 42u64).encode()), Era::mortal(32, 42));
|
||||
assert!(ux.is_signed().unwrap_or(false));
|
||||
assert_eq!(<Ex as Checkable<TestContext>>::check(ux, &TestContext), Ok(CEx { signed: Some((DUMMY_ACCOUNTID, 0)), function: vec![0u8;0] }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn later_mortal_signed_check_should_work() {
|
||||
let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (DUMMY_ACCOUNTID, vec![0u8;0], Era::mortal(32, 11), 11u64).encode()), Era::mortal(32, 11));
|
||||
assert!(ux.is_signed().unwrap_or(false));
|
||||
assert_eq!(<Ex as Checkable<TestContext>>::check(ux, &TestContext), Ok(CEx { signed: Some((DUMMY_ACCOUNTID, 0)), function: vec![0u8;0] }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn too_late_mortal_signed_check_should_fail() {
|
||||
let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (DUMMY_ACCOUNTID, vec![0u8;0], Era::mortal(32, 10), 10u64).encode()), Era::mortal(32, 10));
|
||||
assert!(ux.is_signed().unwrap_or(false));
|
||||
assert_eq!(<Ex as Checkable<TestContext>>::check(ux, &TestContext), Err(crate::BAD_SIGNATURE));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn too_early_mortal_signed_check_should_fail() {
|
||||
let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (DUMMY_ACCOUNTID, vec![0u8;0], Era::mortal(32, 43), 43u64).encode()), Era::mortal(32, 43));
|
||||
assert!(ux.is_signed().unwrap_or(false));
|
||||
assert_eq!(<Ex as Checkable<TestContext>>::check(ux, &TestContext), Err(crate::BAD_SIGNATURE));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encoding_matches_vec() {
|
||||
let ex = Ex::new_unsigned(vec![0u8;0]);
|
||||
let encoded = ex.encode();
|
||||
let decoded = Ex::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);
|
||||
}
|
||||
}
|
||||
@@ -19,12 +19,16 @@
|
||||
use serde::{Serialize, Serializer, Deserialize, de::Error as DeError, Deserializer};
|
||||
use std::{fmt::Debug, ops::Deref, fmt};
|
||||
use crate::codec::{Codec, Encode, Decode};
|
||||
use crate::traits::{self, Checkable, Applyable, BlakeTwo256, OpaqueKeys, TypedKey};
|
||||
use crate::traits::{
|
||||
self, Checkable, Applyable, BlakeTwo256, OpaqueKeys, TypedKey, DispatchError, DispatchResult,
|
||||
ValidateUnsigned, SignedExtension, Dispatchable,
|
||||
};
|
||||
use crate::{generic, KeyTypeId};
|
||||
use crate::weights::{Weighable, Weight};
|
||||
use crate::weights::{GetDispatchInfo, DispatchInfo};
|
||||
pub use substrate_primitives::H256;
|
||||
use substrate_primitives::U256;
|
||||
use substrate_primitives::ed25519::{Public as AuthorityId};
|
||||
use crate::transaction_validity::TransactionValidity;
|
||||
|
||||
/// Authority Id
|
||||
#[derive(Default, PartialEq, Eq, Clone, Encode, Decode, Debug)]
|
||||
@@ -204,52 +208,82 @@ impl<'a, Xt> Deserialize<'a> for Block<Xt> where Block<Xt>: Decode {
|
||||
}
|
||||
}
|
||||
|
||||
/// Test transaction, tuple of (sender, index, call)
|
||||
/// Test transaction, tuple of (sender, call, signed_extra)
|
||||
/// with index only used if sender is some.
|
||||
///
|
||||
/// If sender is some then the transaction is signed otherwise it is unsigned.
|
||||
#[derive(PartialEq, Eq, Clone, Encode, Decode)]
|
||||
pub struct TestXt<Call>(pub Option<u64>, pub u64, pub Call);
|
||||
pub struct TestXt<Call, Extra>(pub Option<(u64, Extra)>, pub Call);
|
||||
|
||||
impl<Call> Serialize for TestXt<Call> where TestXt<Call>: Encode
|
||||
{
|
||||
impl<Call, Extra> Serialize for TestXt<Call, Extra> where TestXt<Call, Extra>: Encode {
|
||||
fn serialize<S>(&self, seq: S) -> Result<S::Ok, S::Error> where S: Serializer {
|
||||
self.using_encoded(|bytes| seq.serialize_bytes(bytes))
|
||||
}
|
||||
}
|
||||
|
||||
impl<Call> Debug for TestXt<Call> {
|
||||
impl<Call, Extra> Debug for TestXt<Call, Extra> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "TestXt({:?}, {:?})", self.0, self.1)
|
||||
write!(f, "TestXt({:?}, ...)", self.0.as_ref().map(|x| &x.0))
|
||||
}
|
||||
}
|
||||
|
||||
impl<Call: Codec + Sync + Send, Context> Checkable<Context> for TestXt<Call> {
|
||||
impl<Call: Codec + Sync + Send, Context, Extra> Checkable<Context> for TestXt<Call, Extra> {
|
||||
type Checked = Self;
|
||||
fn check(self, _: &Context) -> Result<Self::Checked, &'static str> { Ok(self) }
|
||||
}
|
||||
impl<Call: Codec + Sync + Send> traits::Extrinsic for TestXt<Call> {
|
||||
impl<Call: Codec + Sync + Send, Extra> traits::Extrinsic for TestXt<Call, Extra> {
|
||||
type Call = Call;
|
||||
|
||||
fn is_signed(&self) -> Option<bool> {
|
||||
Some(self.0.is_some())
|
||||
}
|
||||
|
||||
fn new_unsigned(_c: Call) -> Option<Self> {
|
||||
None
|
||||
}
|
||||
}
|
||||
impl<Call> Applyable for TestXt<Call> where
|
||||
Call: 'static + Sized + Send + Sync + Clone + Eq + Codec + Debug,
|
||||
|
||||
impl<Origin, Call, Extra> Applyable for TestXt<Call, Extra> where
|
||||
Call: 'static + Sized + Send + Sync + Clone + Eq + Codec + Debug + Dispatchable<Origin=Origin>,
|
||||
Extra: SignedExtension<AccountId=u64>,
|
||||
Origin: From<Option<u64>>
|
||||
{
|
||||
type AccountId = u64;
|
||||
type Index = u64;
|
||||
type Call = Call;
|
||||
fn sender(&self) -> Option<&u64> { self.0.as_ref() }
|
||||
fn index(&self) -> Option<&u64> { self.0.as_ref().map(|_| &self.1) }
|
||||
fn deconstruct(self) -> (Self::Call, Option<Self::AccountId>) {
|
||||
(self.2, self.0)
|
||||
|
||||
fn sender(&self) -> Option<&u64> { self.0.as_ref().map(|x| &x.0) }
|
||||
|
||||
/// Checks to see if this is a valid *transaction*. It returns information on it if so.
|
||||
fn validate<U: ValidateUnsigned<Call=Self::Call>>(&self,
|
||||
_info: DispatchInfo,
|
||||
_len: usize,
|
||||
) -> TransactionValidity {
|
||||
TransactionValidity::Valid(Default::default())
|
||||
}
|
||||
|
||||
/// Executes all necessary logic needed prior to dispatch and deconstructs into function call,
|
||||
/// index and sender.
|
||||
fn dispatch(self,
|
||||
info: DispatchInfo,
|
||||
len: usize,
|
||||
) -> Result<DispatchResult, DispatchError> {
|
||||
let maybe_who = if let Some((who, extra)) = self.0 {
|
||||
Extra::pre_dispatch(extra, &who, info, len)?;
|
||||
Some(who)
|
||||
} else {
|
||||
Extra::pre_dispatch_unsigned(info, len)?;
|
||||
None
|
||||
};
|
||||
Ok(self.1.dispatch(maybe_who.into()))
|
||||
}
|
||||
}
|
||||
impl<Call> Weighable for TestXt<Call> {
|
||||
fn weight(&self, len: usize) -> Weight {
|
||||
|
||||
impl<Call: Encode, Extra: Encode> GetDispatchInfo for TestXt<Call, Extra> {
|
||||
fn get_dispatch_info(&self) -> DispatchInfo {
|
||||
// for testing: weight == size.
|
||||
len as Weight
|
||||
DispatchInfo {
|
||||
weight: self.encode().len() as u32,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,8 +23,9 @@ use runtime_io;
|
||||
#[cfg(feature = "std")] use serde::{Serialize, Deserialize, de::DeserializeOwned};
|
||||
use substrate_primitives::{self, Hasher, Blake2Hasher};
|
||||
use crate::codec::{Codec, Encode, Decode, HasCompact};
|
||||
use crate::transaction_validity::TransactionValidity;
|
||||
use crate::transaction_validity::{ValidTransaction, TransactionValidity};
|
||||
use crate::generic::{Digest, DigestItem};
|
||||
use crate::weights::DispatchInfo;
|
||||
pub use substrate_primitives::crypto::TypedKey;
|
||||
pub use integer_sqrt::IntegerSquareRoot;
|
||||
pub use num_traits::{
|
||||
@@ -716,7 +717,8 @@ pub trait Extrinsic: Sized {
|
||||
/// If no information are available about signed/unsigned, `None` should be returned.
|
||||
fn is_signed(&self) -> Option<bool> { None }
|
||||
|
||||
/// New instance of an unsigned extrinsic aka "inherent".
|
||||
/// New instance of an unsigned extrinsic aka "inherent". `None` if this is an opaque
|
||||
/// extrinsic type.
|
||||
fn new_unsigned(_call: Self::Call) -> Option<Self> { None }
|
||||
}
|
||||
|
||||
@@ -761,6 +763,184 @@ impl<T: BlindCheckable, Context> Checkable<Context> for T {
|
||||
}
|
||||
}
|
||||
|
||||
/// An abstract error concerning an attempt to verify, check or dispatch the transaction. This
|
||||
/// cannot be more concrete because it's designed to work reasonably well over a broad range of
|
||||
/// possible transaction types.
|
||||
#[cfg_attr(feature = "std", derive(Debug))]
|
||||
pub enum DispatchError {
|
||||
/// General error to do with the inability to pay some fees (e.g. account balance too low).
|
||||
Payment,
|
||||
|
||||
/// General error to do with the permissions of the sender.
|
||||
NoPermission,
|
||||
|
||||
/// General error to do with the state of the system in general.
|
||||
BadState,
|
||||
|
||||
/// General error to do with the transaction being outdated (e.g. nonce too low).
|
||||
Stale,
|
||||
|
||||
/// General error to do with the transaction not yet being valid (e.g. nonce too high).
|
||||
Future,
|
||||
|
||||
/// General error to do with the transaction's proofs (e.g. signature).
|
||||
BadProof,
|
||||
|
||||
/* /// General error to do with actually executing the dispatched logic.
|
||||
User(&'static str),*/
|
||||
}
|
||||
|
||||
impl From<DispatchError> for i8 {
|
||||
fn from(e: DispatchError) -> i8 {
|
||||
match e {
|
||||
DispatchError::Payment => -64,
|
||||
DispatchError::NoPermission => -65,
|
||||
DispatchError::BadState => -66,
|
||||
DispatchError::Stale => -67,
|
||||
DispatchError::Future => -68,
|
||||
DispatchError::BadProof => -69,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Result of a module function call; either nothing (functions are only called for "side effects")
|
||||
/// or an error message.
|
||||
pub type DispatchResult = result::Result<(), &'static str>;
|
||||
|
||||
/// A lazy call (module function and argument values) that can be executed via its `dispatch`
|
||||
/// method.
|
||||
pub trait Dispatchable {
|
||||
/// Every function call from your runtime has an origin, which specifies where the extrinsic was
|
||||
/// generated from. In the case of a signed extrinsic (transaction), the origin contains an
|
||||
/// identifier for the caller. The origin can be empty in the case of an inherent extrinsic.
|
||||
type Origin;
|
||||
/// ...
|
||||
type Trait;
|
||||
/// Actually dispatch this call and result the result of it.
|
||||
fn dispatch(self, origin: Self::Origin) -> DispatchResult;
|
||||
}
|
||||
|
||||
/// Means by which a transaction may be extended. This type embodies both the data and the logic
|
||||
/// that should be additionally associated with the transaction. It should be plain old data.
|
||||
pub trait SignedExtension:
|
||||
Codec + MaybeDebug + Sync + Send + Clone + Eq + PartialEq
|
||||
{
|
||||
/// The type which encodes the sender identity.
|
||||
type AccountId;
|
||||
|
||||
/// Any additional data that will go into the signed payload. This may be created dynamically
|
||||
/// from the transaction using the `additional_signed` function.
|
||||
type AdditionalSigned: Encode;
|
||||
|
||||
/// Construct any additional data that should be in the signed payload of the transaction. Can
|
||||
/// also perform any pre-signature-verification checks and return an error if needed.
|
||||
fn additional_signed(&self) -> Result<Self::AdditionalSigned, &'static str>;
|
||||
|
||||
/// Validate a signed transaction for the transaction queue.
|
||||
fn validate(
|
||||
&self,
|
||||
_who: &Self::AccountId,
|
||||
_info: DispatchInfo,
|
||||
_len: usize,
|
||||
) -> Result<ValidTransaction, DispatchError> { Ok(Default::default()) }
|
||||
|
||||
/// Do any pre-flight stuff for a signed transaction.
|
||||
fn pre_dispatch(
|
||||
self,
|
||||
who: &Self::AccountId,
|
||||
info: DispatchInfo,
|
||||
len: usize,
|
||||
) -> Result<(), DispatchError> { self.validate(who, info, len).map(|_| ()) }
|
||||
|
||||
/// Validate an unsigned transaction for the transaction queue. Normally the default
|
||||
/// implementation is fine since `ValidateUnsigned` is a better way of recognising and
|
||||
/// validating unsigned transactions.
|
||||
fn validate_unsigned(
|
||||
_info: DispatchInfo,
|
||||
_len: usize,
|
||||
) -> Result<ValidTransaction, DispatchError> { Ok(Default::default()) }
|
||||
|
||||
/// Do any pre-flight stuff for a unsigned transaction.
|
||||
fn pre_dispatch_unsigned(
|
||||
info: DispatchInfo,
|
||||
len: usize,
|
||||
) -> Result<(), DispatchError> { Self::validate_unsigned(info, len).map(|_| ()) }
|
||||
}
|
||||
|
||||
macro_rules! tuple_impl_indexed {
|
||||
($first:ident, $($rest:ident,)+ ; $first_index:tt, $($rest_index:tt,)+) => {
|
||||
tuple_impl_indexed!([$first] [$($rest)+] ; [$first_index,] [$($rest_index,)+]);
|
||||
};
|
||||
([$($direct:ident)+] ; [$($index:tt,)+]) => {
|
||||
impl<
|
||||
AccountId,
|
||||
$($direct: SignedExtension<AccountId=AccountId>),+
|
||||
> SignedExtension for ($($direct),+,) {
|
||||
type AccountId = AccountId;
|
||||
type AdditionalSigned = ($($direct::AdditionalSigned,)+);
|
||||
fn additional_signed(&self) -> Result<Self::AdditionalSigned, &'static str> {
|
||||
Ok(( $(self.$index.additional_signed()?,)+ ))
|
||||
}
|
||||
fn validate(
|
||||
&self,
|
||||
who: &Self::AccountId,
|
||||
info: DispatchInfo,
|
||||
len: usize,
|
||||
) -> Result<ValidTransaction, DispatchError> {
|
||||
let aggregator = vec![$(<$direct as SignedExtension>::validate(&self.$index, who, info, len)?),+];
|
||||
Ok(aggregator.into_iter().fold(ValidTransaction::default(), |acc, a| acc.combine_with(a)))
|
||||
}
|
||||
fn pre_dispatch(
|
||||
self,
|
||||
who: &Self::AccountId,
|
||||
info: DispatchInfo,
|
||||
len: usize,
|
||||
) -> Result<(), DispatchError> {
|
||||
$(self.$index.pre_dispatch(who, info, len)?;)+
|
||||
Ok(())
|
||||
}
|
||||
fn validate_unsigned(
|
||||
info: DispatchInfo,
|
||||
len: usize,
|
||||
) -> Result<ValidTransaction, DispatchError> {
|
||||
let aggregator = vec![$($direct::validate_unsigned(info, len)?),+];
|
||||
Ok(aggregator.into_iter().fold(ValidTransaction::default(), |acc, a| acc.combine_with(a)))
|
||||
}
|
||||
fn pre_dispatch_unsigned(
|
||||
info: DispatchInfo,
|
||||
len: usize,
|
||||
) -> Result<(), DispatchError> {
|
||||
$($direct::pre_dispatch_unsigned(info, len)?;)+
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
([$($direct:ident)+] [] ; [$($index:tt,)+] []) => {
|
||||
tuple_impl_indexed!([$($direct)+] ; [$($index,)+]);
|
||||
};
|
||||
(
|
||||
[$($direct:ident)+] [$first:ident $($rest:ident)*]
|
||||
;
|
||||
[$($index:tt,)+] [$first_index:tt, $($rest_index:tt,)*]
|
||||
) => {
|
||||
tuple_impl_indexed!([$($direct)+] ; [$($index,)+]);
|
||||
tuple_impl_indexed!([$($direct)+ $first] [$($rest)*] ; [$($index,)+ $first_index,] [$($rest_index,)*]);
|
||||
};
|
||||
}
|
||||
|
||||
// TODO: merge this into `tuple_impl` once codec supports `trait Codec` for longer tuple lengths. #3152
|
||||
#[allow(non_snake_case)]
|
||||
tuple_impl_indexed!(A, B, C, D, E, F, G, H, I, J, ; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,);
|
||||
|
||||
/// Only for base bone testing when you don't care about signed extensions at all.\
|
||||
#[cfg(feature = "std")]
|
||||
impl SignedExtension for () {
|
||||
type AccountId = u64;
|
||||
type AdditionalSigned = ();
|
||||
fn additional_signed(&self) -> rstd::result::Result<(), &'static str> { Ok(()) }
|
||||
}
|
||||
|
||||
/// An "executable" piece of information, used by the standard Substrate Executive in order to
|
||||
/// enact a piece of extrinsic information by marshalling and dispatching to a named function
|
||||
/// call.
|
||||
@@ -770,16 +950,25 @@ impl<T: BlindCheckable, Context> Checkable<Context> for T {
|
||||
pub trait Applyable: Sized + Send + Sync {
|
||||
/// Id of the account that is responsible for this piece of information (sender).
|
||||
type AccountId: Member + MaybeDisplay;
|
||||
/// Index allowing to disambiguate other `Applyable`s from the same `AccountId`.
|
||||
type Index: Member + MaybeDisplay + SimpleArithmetic;
|
||||
/// Function call.
|
||||
type Call: Member;
|
||||
/// Returns a reference to the index if any.
|
||||
fn index(&self) -> Option<&Self::Index>;
|
||||
|
||||
/// Type by which we can dispatch. Restricts the UnsignedValidator type.
|
||||
type Call;
|
||||
|
||||
/// Returns a reference to the sender if any.
|
||||
fn sender(&self) -> Option<&Self::AccountId>;
|
||||
/// Deconstructs into function call and sender.
|
||||
fn deconstruct(self) -> (Self::Call, Option<Self::AccountId>);
|
||||
|
||||
/// Checks to see if this is a valid *transaction*. It returns information on it if so.
|
||||
fn validate<V: ValidateUnsigned<Call=Self::Call>>(&self,
|
||||
info: DispatchInfo,
|
||||
len: usize,
|
||||
) -> TransactionValidity;
|
||||
|
||||
/// Executes all necessary logic needed prior to dispatch and deconstructs into function call,
|
||||
/// index and sender.
|
||||
fn dispatch(self,
|
||||
info: DispatchInfo,
|
||||
len: usize,
|
||||
) -> Result<DispatchResult, DispatchError>;
|
||||
}
|
||||
|
||||
/// Auxiliary wrapper that holds an api instance and binds it to the given lifetime.
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
|
||||
use rstd::prelude::*;
|
||||
use crate::codec::{Encode, Decode};
|
||||
use crate::traits::DispatchError;
|
||||
|
||||
/// Priority for a transaction. Additive. Higher is better.
|
||||
pub type TransactionPriority = u64;
|
||||
@@ -36,40 +37,81 @@ pub enum TransactionValidity {
|
||||
/// Transaction is invalid. Details are described by the error code.
|
||||
Invalid(i8),
|
||||
/// Transaction is valid.
|
||||
Valid {
|
||||
/// Priority of the transaction.
|
||||
///
|
||||
/// Priority determines the ordering of two transactions that have all
|
||||
/// their dependencies (required tags) satisfied.
|
||||
priority: TransactionPriority,
|
||||
/// Transaction dependencies
|
||||
///
|
||||
/// A non-empty list signifies that some other transactions which provide
|
||||
/// given tags are required to be included before that one.
|
||||
requires: Vec<TransactionTag>,
|
||||
/// Provided tags
|
||||
///
|
||||
/// A list of tags this transaction provides. Successfully importing the transaction
|
||||
/// will enable other transactions that depend on (require) those tags to be included as well.
|
||||
/// Provided and required tags allow Substrate to build a dependency graph of transactions
|
||||
/// and import them in the right (linear) order.
|
||||
provides: Vec<TransactionTag>,
|
||||
/// Transaction longevity
|
||||
///
|
||||
/// Longevity describes minimum number of blocks the validity is correct.
|
||||
/// After this period transaction should be removed from the pool or revalidated.
|
||||
longevity: TransactionLongevity,
|
||||
/// A flag indicating if the transaction should be propagated to other peers.
|
||||
///
|
||||
/// By setting `false` here the transaction will still be considered for
|
||||
/// including in blocks that are authored on the current node, but will
|
||||
/// never be sent to other peers.
|
||||
propagate: bool,
|
||||
},
|
||||
Valid(ValidTransaction),
|
||||
/// Transaction validity can't be determined.
|
||||
Unknown(i8),
|
||||
}
|
||||
|
||||
impl From<Result<ValidTransaction, DispatchError>> for TransactionValidity {
|
||||
fn from(r: Result<ValidTransaction, DispatchError>) -> Self {
|
||||
match r {
|
||||
Ok(v) => TransactionValidity::Valid(v),
|
||||
Err(e) => TransactionValidity::Invalid(e.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Information concerning a valid transaction.
|
||||
#[derive(Clone, PartialEq, Eq, Encode)]
|
||||
#[cfg_attr(feature = "std", derive(Debug))]
|
||||
pub struct ValidTransaction {
|
||||
/// Priority of the transaction.
|
||||
///
|
||||
/// Priority determines the ordering of two transactions that have all
|
||||
/// their dependencies (required tags) satisfied.
|
||||
pub priority: TransactionPriority,
|
||||
/// Transaction dependencies
|
||||
///
|
||||
/// A non-empty list signifies that some other transactions which provide
|
||||
/// given tags are required to be included before that one.
|
||||
pub requires: Vec<TransactionTag>,
|
||||
/// Provided tags
|
||||
///
|
||||
/// A list of tags this transaction provides. Successfully importing the transaction
|
||||
/// will enable other transactions that depend on (require) those tags to be included as well.
|
||||
/// Provided and required tags allow Substrate to build a dependency graph of transactions
|
||||
/// and import them in the right (linear) order.
|
||||
pub provides: Vec<TransactionTag>,
|
||||
/// Transaction longevity
|
||||
///
|
||||
/// Longevity describes minimum number of blocks the validity is correct.
|
||||
/// After this period transaction should be removed from the pool or revalidated.
|
||||
pub longevity: TransactionLongevity,
|
||||
/// A flag indicating if the transaction should be propagated to other peers.
|
||||
///
|
||||
/// By setting `false` here the transaction will still be considered for
|
||||
/// including in blocks that are authored on the current node, but will
|
||||
/// never be sent to other peers.
|
||||
pub propagate: bool,
|
||||
}
|
||||
|
||||
impl Default for ValidTransaction {
|
||||
fn default() -> Self {
|
||||
ValidTransaction {
|
||||
priority: 0,
|
||||
requires: vec![],
|
||||
provides: vec![],
|
||||
longevity: TransactionLongevity::max_value(),
|
||||
propagate: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ValidTransaction {
|
||||
/// Combine two instances into one, as a best effort. This will take the superset of each of the
|
||||
/// `provides` and `requires` tags, it will sum the priorities, take the minimum longevity and
|
||||
/// the logic *And* of the propagate flags.
|
||||
pub fn combine_with(mut self, mut other: ValidTransaction) -> Self {
|
||||
ValidTransaction {
|
||||
priority: self.priority.saturating_add(other.priority),
|
||||
requires: { self.requires.append(&mut other.requires); self.requires },
|
||||
provides: { self.provides.append(&mut other.provides); self.provides },
|
||||
longevity: self.longevity.min(other.longevity),
|
||||
propagate: self.propagate && other.propagate,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode for TransactionValidity {
|
||||
fn decode<I: crate::codec::Input>(value: &mut I) -> Option<Self> {
|
||||
match value.read_byte()? {
|
||||
@@ -81,9 +123,9 @@ impl Decode for TransactionValidity {
|
||||
let longevity = TransactionLongevity::decode(value)?;
|
||||
let propagate = bool::decode(value).unwrap_or(true);
|
||||
|
||||
Some(TransactionValidity::Valid {
|
||||
Some(TransactionValidity::Valid(ValidTransaction {
|
||||
priority, requires, provides, longevity, propagate,
|
||||
})
|
||||
}))
|
||||
},
|
||||
2 => Some(TransactionValidity::Unknown(i8::decode(value)?)),
|
||||
_ => None,
|
||||
@@ -101,24 +143,24 @@ mod tests {
|
||||
1, 5, 0, 0, 0, 0, 0, 0, 0, 4, 16, 1, 2, 3, 4, 4, 12, 4, 5, 6, 42, 0, 0, 0, 0, 0, 0, 0
|
||||
];
|
||||
|
||||
assert_eq!(TransactionValidity::decode(&mut &*old_encoding), Some(TransactionValidity::Valid {
|
||||
assert_eq!(TransactionValidity::decode(&mut &*old_encoding), Some(TransactionValidity::Valid(ValidTransaction {
|
||||
priority: 5,
|
||||
requires: vec![vec![1, 2, 3, 4]],
|
||||
provides: vec![vec![4, 5, 6]],
|
||||
longevity: 42,
|
||||
propagate: true,
|
||||
}));
|
||||
})));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_encode_and_decode() {
|
||||
let v = TransactionValidity::Valid {
|
||||
let v = TransactionValidity::Valid(ValidTransaction {
|
||||
priority: 5,
|
||||
requires: vec![vec![1, 2, 3, 4]],
|
||||
provides: vec![vec![4, 5, 6]],
|
||||
longevity: 42,
|
||||
propagate: false,
|
||||
};
|
||||
});
|
||||
|
||||
let encoded = v.encode();
|
||||
assert_eq!(
|
||||
|
||||
@@ -17,8 +17,8 @@
|
||||
//! Primitives for transaction weighting.
|
||||
//!
|
||||
//! Each dispatch function within `decl_module!` can have an optional `#[weight = $x]` attribute.
|
||||
//! $x can be any object that implements the `Weighable` trait. By default, All transactions are
|
||||
//! annotated by `#[weight = TransactionWeight::default()]`.
|
||||
//! `$x` can be any type that implements the `ClassifyDispatch<T>` and `WeighData<T>` traits. By
|
||||
//! default, All transactions are annotated with `#[weight = SimpleDispatchInfo::default()]`.
|
||||
//!
|
||||
//! Note that the decl_module macro _cannot_ enforce this and will simply fail if an invalid struct
|
||||
//! (something that does not implement `Weighable`) is passed in.
|
||||
@@ -26,59 +26,147 @@
|
||||
use crate::{Fixed64, traits::Saturating};
|
||||
use crate::codec::{Encode, Decode};
|
||||
|
||||
/// The final type that each `#[weight = $x:expr]`'s
|
||||
/// expression must evaluate to.
|
||||
pub use crate::transaction_validity::TransactionPriority;
|
||||
use crate::traits::Bounded;
|
||||
|
||||
/// Numeric range of a transaction weight.
|
||||
pub type Weight = u32;
|
||||
|
||||
/// Maximum block saturation: 4mb
|
||||
pub const MAX_TRANSACTIONS_WEIGHT: u32 = 4 * 1024 * 1024;
|
||||
/// Target block saturation: 25% of max block saturation = 1mb
|
||||
pub const IDEAL_TRANSACTIONS_WEIGHT: u32 = MAX_TRANSACTIONS_WEIGHT / 4;
|
||||
|
||||
/// A `Call` enum (aka transaction) that can be weighted using the custom weight attribute of
|
||||
/// its dispatchable functions. Is implemented by default in the `decl_module!`.
|
||||
///
|
||||
/// Both the outer Call enum and the per-module individual ones will implement this.
|
||||
/// The outer enum simply calls the inner ones based on call type.
|
||||
pub trait Weighable {
|
||||
/// Return the weight of this call.
|
||||
/// The `len` argument is the encoded length of the transaction/call.
|
||||
fn weight(&self, len: usize) -> Weight;
|
||||
/// A generalized group of dispatch types. This is only distinguishing normal, user-triggered transactions
|
||||
/// (`Normal`) and anything beyond which serves a higher purpose to the system (`Operational`).
|
||||
#[cfg_attr(feature = "std", derive(Debug))]
|
||||
#[derive(PartialEq, Eq, Clone, Copy)]
|
||||
pub enum DispatchClass {
|
||||
/// A normal dispatch.
|
||||
Normal,
|
||||
/// An operational dispatch.
|
||||
Operational,
|
||||
}
|
||||
|
||||
/// Default type used as the weight representative in a `#[weight = x]` attribute.
|
||||
///
|
||||
/// A user may pass in any other type that implements [`Weighable`]. If not, the `Default`
|
||||
/// implementation of [`TransactionWeight`] is used.
|
||||
pub enum TransactionWeight {
|
||||
/// Basic weight (base, byte).
|
||||
/// The values contained are the base weight and byte weight respectively.
|
||||
Basic(Weight, Weight),
|
||||
/// Maximum fee. This implies that this transaction _might_ get included but
|
||||
/// no more transaction can be added. This can be done by setting the
|
||||
/// implementation to _maximum block weight_.
|
||||
Max,
|
||||
/// Free. The transaction does not increase the total weight
|
||||
/// (i.e. is not included in weight calculation).
|
||||
Free,
|
||||
impl Default for DispatchClass {
|
||||
fn default() -> Self {
|
||||
DispatchClass::Normal
|
||||
}
|
||||
}
|
||||
|
||||
impl Weighable for TransactionWeight {
|
||||
fn weight(&self, len: usize) -> Weight {
|
||||
match self {
|
||||
TransactionWeight::Basic(base, byte) => base + byte * len as Weight,
|
||||
TransactionWeight::Max => 3 * 1024 * 1024,
|
||||
TransactionWeight::Free => 0,
|
||||
impl From<SimpleDispatchInfo> for DispatchClass {
|
||||
fn from(tx: SimpleDispatchInfo) -> Self {
|
||||
match tx {
|
||||
SimpleDispatchInfo::FixedOperational(_) => DispatchClass::Operational,
|
||||
SimpleDispatchInfo::MaxOperational => DispatchClass::Operational,
|
||||
SimpleDispatchInfo::FreeOperational => DispatchClass::Operational,
|
||||
|
||||
SimpleDispatchInfo::FixedNormal(_) => DispatchClass::Normal,
|
||||
SimpleDispatchInfo::MaxNormal => DispatchClass::Normal,
|
||||
SimpleDispatchInfo::FreeNormal => DispatchClass::Normal,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for TransactionWeight {
|
||||
/// A bundle of static information collected from the `#[weight = $x]` attributes.
|
||||
#[cfg_attr(feature = "std", derive(PartialEq, Eq, Debug))]
|
||||
#[derive(Clone, Copy, Default)]
|
||||
pub struct DispatchInfo {
|
||||
/// Weight of this transaction.
|
||||
pub weight: Weight,
|
||||
/// Class of this transaction.
|
||||
pub class: DispatchClass,
|
||||
}
|
||||
|
||||
impl DispatchInfo {
|
||||
/// Determine if this dispatch should pay the base length-related fee or not.
|
||||
pub fn pay_length_fee(&self) -> bool {
|
||||
match self.class {
|
||||
DispatchClass::Normal => true,
|
||||
// For now we assume all operational transactions don't pay the length fee.
|
||||
DispatchClass::Operational => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A `Dispatchable` function (aka transaction) that can carry some static information along with it, using the
|
||||
/// `#[weight]` attribute.
|
||||
pub trait GetDispatchInfo {
|
||||
/// Return a `DispatchInfo`, containing relevant information of this dispatch.
|
||||
///
|
||||
/// This is done independently of its encoded size.
|
||||
fn get_dispatch_info(&self) -> DispatchInfo;
|
||||
}
|
||||
|
||||
/// Means of weighing some particular kind of data (`T`).
|
||||
pub trait WeighData<T> {
|
||||
/// Weigh the data `T` given by `target`.
|
||||
fn weigh_data(&self, target: T) -> Weight;
|
||||
}
|
||||
|
||||
/// Means of classifying a dispatchable function.
|
||||
pub trait ClassifyDispatch<T> {
|
||||
/// Classify the dispatch function based on input data `target` of type `T`.
|
||||
fn classify_dispatch(&self, target: T) -> DispatchClass;
|
||||
}
|
||||
|
||||
/// Default type used with the `#[weight = x]` attribute in a substrate chain.
|
||||
///
|
||||
/// A user may pass in any other type that implements the correct traits. If not, the `Default`
|
||||
/// implementation of [`SimpleDispatchInfo`] is used.
|
||||
///
|
||||
/// For each generalized group (`Normal` and `Operation`):
|
||||
/// - A `Fixed` variant means weight fee is charged normally and the weight is the number
|
||||
/// specified in the inner value of the variant.
|
||||
/// - A `Free` variant is equal to `::Fixed(0)`. Note that this does not guarantee inclusion.
|
||||
/// - A `Max` variant is equal to `::Fixed(Weight::max_value())`.
|
||||
///
|
||||
/// Based on the final weight value, based on the above variants:
|
||||
/// - A _weight-fee_ is deducted.
|
||||
/// - The block weight is consumed proportionally.
|
||||
///
|
||||
/// As for the generalized groups themselves:
|
||||
/// - `Normal` variants will be assigned a priority proportional to their weight. They can only
|
||||
/// consume a portion (1/4) of the maximum block resource limits.
|
||||
/// - `Operational` variants will be assigned the maximum priority. They can potentially consume
|
||||
/// the entire block resource limit.
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum SimpleDispatchInfo {
|
||||
/// A normal dispatch with fixed weight.
|
||||
FixedNormal(Weight),
|
||||
/// A normal dispatch with the maximum weight.
|
||||
MaxNormal,
|
||||
/// A normal dispatch with no weight.
|
||||
FreeNormal,
|
||||
/// An operational dispatch with fixed weight.
|
||||
FixedOperational(Weight),
|
||||
/// An operational dispatch with the maximum weight.
|
||||
MaxOperational,
|
||||
/// An operational dispatch with no weight.
|
||||
FreeOperational,
|
||||
}
|
||||
|
||||
impl<T> WeighData<T> for SimpleDispatchInfo {
|
||||
fn weigh_data(&self, _: T) -> Weight {
|
||||
match self {
|
||||
SimpleDispatchInfo::FixedNormal(w) => *w,
|
||||
SimpleDispatchInfo::MaxNormal => Bounded::max_value(),
|
||||
SimpleDispatchInfo::FreeNormal => Bounded::min_value(),
|
||||
|
||||
SimpleDispatchInfo::FixedOperational(w) => *w,
|
||||
SimpleDispatchInfo::MaxOperational => Bounded::max_value(),
|
||||
SimpleDispatchInfo::FreeOperational => Bounded::min_value(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ClassifyDispatch<T> for SimpleDispatchInfo {
|
||||
fn classify_dispatch(&self, _: T) -> DispatchClass {
|
||||
DispatchClass::from(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for SimpleDispatchInfo {
|
||||
fn default() -> Self {
|
||||
// This implies that the weight is currently equal to tx-size, nothing more
|
||||
// This implies that the weight is currently equal to 100, nothing more
|
||||
// for all substrate transactions that do NOT explicitly annotate weight.
|
||||
// TODO #2431 needs to be updated with proper max values.
|
||||
TransactionWeight::Basic(0, 1)
|
||||
SimpleDispatchInfo::FixedNormal(100)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user