Browse Source

runtime: Move out code from the lib.rs to the integration module

Shamil Gadelshin 4 years ago
parent
commit
45657b9d9c
3 changed files with 77 additions and 49 deletions
  1. 1 0
      runtime/src/integration/mod.rs
  2. 71 0
      runtime/src/integration/transactions.rs
  3. 5 49
      runtime/src/lib.rs

+ 1 - 0
runtime/src/integration/mod.rs

@@ -2,5 +2,6 @@ pub mod content_working_group;
 pub mod forum;
 pub mod proposals;
 pub mod storage;
+pub mod transactions;
 pub mod versioned_store_permissions;
 pub mod working_group;

+ 71 - 0
runtime/src/integration/transactions.rs

@@ -0,0 +1,71 @@
+use codec::Encode;
+use frame_support::debug;
+use frame_support::weights::{WeightToFeeCoefficients, WeightToFeePolynomial};
+use sp_runtime::generic;
+use sp_runtime::generic::SignedPayload;
+use sp_runtime::traits::StaticLookup;
+use sp_runtime::SaturatedConversion;
+
+use crate::{AccountId, Balance, BlockHashCount, Index, SignedExtra, UncheckedExtrinsic};
+use crate::{Call, Indices, Runtime, System};
+
+/// Stub for zero transaction weights.
+pub struct NoWeights;
+impl WeightToFeePolynomial for NoWeights {
+    type Balance = Balance;
+
+    fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
+        Default::default()
+    }
+
+    fn calc(_weight: &u64) -> Self::Balance {
+        Default::default()
+    }
+}
+
+/// 'Create transaction' default implementation.
+pub(crate) fn create_transaction<
+    C: system::offchain::AppCrypto<
+        <Runtime as system::offchain::SigningTypes>::Public,
+        <Runtime as system::offchain::SigningTypes>::Signature,
+    >,
+>(
+    call: Call,
+    public: <<Runtime as system::offchain::SigningTypes>::Signature as sp_runtime::traits::Verify>::Signer,
+    account: AccountId,
+    nonce: Index,
+) -> Option<(
+    Call,
+    <UncheckedExtrinsic as sp_runtime::traits::Extrinsic>::SignaturePayload,
+)> {
+    // take the biggest period possible.
+    let period = BlockHashCount::get()
+        .checked_next_power_of_two()
+        .map(|c| c / 2)
+        .unwrap_or(2) as u64;
+    let current_block = System::block_number()
+        .saturated_into::<u64>()
+        // The `System::block_number` is initialized with `n+1`,
+        // so the actual block number is `n`.
+        .saturating_sub(1);
+    let tip = 0;
+    let extra: SignedExtra = (
+        system::CheckSpecVersion::<Runtime>::new(),
+        system::CheckTxVersion::<Runtime>::new(),
+        system::CheckGenesis::<Runtime>::new(),
+        system::CheckEra::<Runtime>::from(generic::Era::mortal(period, current_block)),
+        system::CheckNonce::<Runtime>::from(nonce),
+        system::CheckWeight::<Runtime>::new(),
+        pallet_transaction_payment::ChargeTransactionPayment::<Runtime>::from(tip),
+        pallet_grandpa::ValidateEquivocationReport::<Runtime>::new(),
+    );
+    let raw_payload = SignedPayload::new(call, extra)
+        .map_err(|e| {
+            debug::warn!("Unable to create signed payload: {:?}", e);
+        })
+        .ok()?;
+    let signature = raw_payload.using_encoded(|payload| C::sign(payload, public))?;
+    let address = Indices::unlookup(account);
+    let (call, extra, _) = raw_payload.deconstruct();
+    Some((call, (address, signature, extra)))
+}

+ 5 - 49
runtime/src/lib.rs

@@ -20,14 +20,13 @@ mod primitives;
 #[cfg(test)]
 mod tests; // Runtime integration tests
 
-use codec::Encode;
 use frame_support::inherent::{CheckInherentsResult, InherentData};
 use frame_support::traits::KeyOwnerProofSystem;
 use frame_support::weights::{
     constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight},
-    Weight, WeightToFeeCoefficients, WeightToFeePolynomial,
+    Weight,
 };
-use frame_support::{construct_runtime, debug, parameter_types, traits::Randomness};
+use frame_support::{construct_runtime, parameter_types, traits::Randomness};
 use pallet_contracts_rpc_runtime_api::ContractExecResult;
 use pallet_grandpa::fg_primitives;
 use pallet_grandpa::{AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList};
@@ -40,14 +39,13 @@ use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId;
 use sp_core::crypto::KeyTypeId;
 use sp_core::OpaqueMetadata;
 use sp_runtime::curve::PiecewiseLinear;
-use sp_runtime::generic::SignedPayload;
 use sp_runtime::traits::{
     BlakeTwo256, Block as BlockT, NumberFor, OpaqueKeys, Saturating, StaticLookup,
 };
 use sp_runtime::transaction_validity::{TransactionSource, TransactionValidity};
 use sp_runtime::{
     create_runtime_str, generic, impl_opaque_keys, ApplyExtrinsicResult, FixedPointNumber, Perbill,
-    Perquintill, SaturatedConversion,
+    Perquintill,
 };
 use sp_std::boxed::Box;
 use sp_std::vec::Vec;
@@ -180,36 +178,7 @@ where
         Call,
         <UncheckedExtrinsic as sp_runtime::traits::Extrinsic>::SignaturePayload,
     )> {
-        // take the biggest period possible.
-        let period = BlockHashCount::get()
-            .checked_next_power_of_two()
-            .map(|c| c / 2)
-            .unwrap_or(2) as u64;
-        let current_block = System::block_number()
-            .saturated_into::<u64>()
-            // The `System::block_number` is initialized with `n+1`,
-            // so the actual block number is `n`.
-            .saturating_sub(1);
-        let tip = 0;
-        let extra: SignedExtra = (
-            system::CheckSpecVersion::<Runtime>::new(),
-            system::CheckTxVersion::<Runtime>::new(),
-            system::CheckGenesis::<Runtime>::new(),
-            system::CheckEra::<Runtime>::from(generic::Era::mortal(period, current_block)),
-            system::CheckNonce::<Runtime>::from(nonce),
-            system::CheckWeight::<Runtime>::new(),
-            pallet_transaction_payment::ChargeTransactionPayment::<Runtime>::from(tip),
-            pallet_grandpa::ValidateEquivocationReport::<Runtime>::new(),
-        );
-        let raw_payload = SignedPayload::new(call, extra)
-            .map_err(|e| {
-                debug::warn!("Unable to create signed payload: {:?}", e);
-            })
-            .ok()?;
-        let signature = raw_payload.using_encoded(|payload| C::sign(payload, public))?;
-        let address = Indices::unlookup(account);
-        let (call, extra, _) = raw_payload.deconstruct();
-        Some((call, (address, signature, extra)))
+        integration::transactions::create_transaction::<C>(call, public, account, nonce)
     }
 }
 
@@ -273,23 +242,10 @@ impl pallet_transaction_payment::Trait for Runtime {
     type Currency = Balances;
     type OnTransactionPayment = ();
     type TransactionByteFee = TransactionByteFee;
-    type WeightToFee = NoWeights; // TODO: adjust weight
+    type WeightToFee = integration::transactions::NoWeights; // TODO: adjust weight
     type FeeMultiplierUpdate = (); // TODO: adjust fee
 }
 
-pub struct NoWeights;
-impl WeightToFeePolynomial for NoWeights {
-    type Balance = Balance;
-
-    fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
-        Default::default()
-    }
-
-    fn calc(_weight: &u64) -> Self::Balance {
-        Default::default()
-    }
-}
-
 impl pallet_sudo::Trait for Runtime {
     type Event = Event;
     type Call = Call;