Companion PR to #5182 - Substrate offchain signing (#985)

* Implement offchain::SendTransactionTypes

* Fix test-runtime

* Fix Kusama

* Add SendTransactiontypes to Kusama

* impl SendTransactiontypes for parachains and registrar

* Fix typos

* Provided extrinsic type

* Implement AppCrypto for signing transactions

* Add signing capabilities to runtime

* Fix test runtimes

* Implement CreateSignedtransaction for Test

* Implement signing types for test runtimes

* Fix test runtime create transaction impl

* Fix build

* Bump kusama's spec version

* Update substrate

* Use all_accounts
This commit is contained in:
Rakan Alhneiti
2020-04-21 18:50:32 +02:00
committed by GitHub
parent 765c456a10
commit 2e521d30ea
7 changed files with 346 additions and 243 deletions
+33 -18
View File
@@ -32,6 +32,7 @@ use runtime_common::{attestations, claims, parachains, registrar, slots,
NegativeImbalance, BlockHashCount, MaximumBlockWeight, AvailableBlockRatio,
MaximumBlockLength,
};
use sp_core::sr25519;
use sp_runtime::{
create_runtime_str, generic, impl_opaque_keys,
ApplyExtrinsicResult, KeyTypeId, Percent, Permill, Perbill, Perquintill, RuntimeDebug,
@@ -41,7 +42,7 @@ use sp_runtime::{
curve::PiecewiseLinear,
traits::{
BlakeTwo256, Block as BlockT, SignedExtension, OpaqueKeys, ConvertInto, IdentityLookup,
DispatchInfoOf, Extrinsic as ExtrinsicT, SaturatedConversion,
DispatchInfoOf, Extrinsic as ExtrinsicT, SaturatedConversion, Verify,
},
};
#[cfg(feature = "runtime-benchmarks")]
@@ -58,7 +59,6 @@ use frame_support::{
};
use im_online::sr25519::AuthorityId as ImOnlineId;
use authority_discovery_primitives::AuthorityId as AuthorityDiscoveryId;
use system::offchain::TransactionSubmitter;
use transaction_payment_rpc_runtime_api::RuntimeDispatchInfo;
use session::{historical as session_historical};
@@ -84,7 +84,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("kusama"),
impl_name: create_runtime_str!("parity-kusama"),
authoring_version: 2,
spec_version: 1059,
spec_version: 1060,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
@@ -322,7 +322,6 @@ impl staking::Trait for Runtime {
type NextNewSession = Session;
type ElectionLookahead = ElectionLookahead;
type Call = Call;
type SubmitTransaction = TransactionSubmitter<(), Runtime, UncheckedExtrinsic>;
type UnsignedPriority = StakingUnsignedPriority;
}
@@ -467,8 +466,6 @@ impl offences::Trait for Runtime {
impl authority_discovery::Trait for Runtime {}
type SubmitTransaction = TransactionSubmitter<ImOnlineId, Runtime, UncheckedExtrinsic>;
parameter_types! {
pub const SessionDuration: BlockNumber = EPOCH_DURATION_IN_BLOCKS as _;
}
@@ -481,8 +478,6 @@ parameter_types! {
impl im_online::Trait for Runtime {
type AuthorityId = ImOnlineId;
type Event = Event;
type Call = Call;
type SubmitTransaction = SubmitTransaction;
type ReportUnresponsiveness = Offences;
type SessionDuration = SessionDuration;
type UnsignedPriority = StakingUnsignedPriority;
@@ -522,6 +517,7 @@ parameter_types! {
}
impl parachains::Trait for Runtime {
type AuthorityId = FishermanAuthorityId;
type Origin = Origin;
type Call = Call;
type ParachainCurrency = Balances;
@@ -541,17 +537,22 @@ impl parachains::Trait for Runtime {
type IdentificationTuple = <Self::KeyOwnerProofSystem as KeyOwnerProofSystem<(KeyTypeId, Vec<u8>)>>::IdentificationTuple;
type ReportOffence = Offences;
type BlockHashConversion = sp_runtime::traits::Identity;
type SubmitSignedTransaction = TransactionSubmitter<parachain::FishermanId, Runtime, UncheckedExtrinsic>;
}
impl system::offchain::CreateTransaction<Runtime, UncheckedExtrinsic> for Runtime {
type Public = <primitives::Signature as sp_runtime::traits::Verify>::Signer;
type Signature = primitives::Signature;
pub struct FishermanAuthorityId;
impl system::offchain::AppCrypto<<Signature as Verify>::Signer, Signature> for FishermanAuthorityId {
type RuntimeAppPublic = parachain::FishermanId;
type GenericSignature = sr25519::Signature;
type GenericPublic = sr25519::Public;
}
fn create_transaction<TSigner: system::offchain::Signer<Self::Public, Self::Signature>>(
call: <UncheckedExtrinsic as ExtrinsicT>::Call,
public: Self::Public,
account: <Runtime as system::Trait>::AccountId,
impl<LocalCall> system::offchain::CreateSignedTransaction<LocalCall> for Runtime where
Call: From<LocalCall>,
{
fn create_transaction<C: system::offchain::AppCrypto<Self::Public, Self::Signature>>(
call: Call,
public: <Signature as Verify>::Signer,
account: AccountId,
nonce: <Runtime as system::Trait>::Index,
) -> Option<(Call, <UncheckedExtrinsic as ExtrinsicT>::SignaturePayload)> {
let period = BlockHashCount::get()
@@ -575,14 +576,28 @@ impl system::offchain::CreateTransaction<Runtime, UncheckedExtrinsic> for Runtim
parachains::ValidateDoubleVoteReports::<Runtime>::new(),
);
let raw_payload = SignedPayload::new(call, extra).map_err(|e| {
debug::warn!("Unable to create signed payload: {:?}", e)
debug::warn!("Unable to create signed payload: {:?}", e);
}).ok()?;
let signature = TSigner::sign(public, &raw_payload)?;
let signature = raw_payload.using_encoded(|payload| {
C::sign(payload, public)
})?;
let (call, extra, _) = raw_payload.deconstruct();
Some((call, (account, signature, extra)))
}
}
impl system::offchain::SigningTypes for Runtime {
type Public = <Signature as Verify>::Signer;
type Signature = Signature;
}
impl<C> system::offchain::SendTransactionTypes<C> for Runtime where
Call: From<C>,
{
type OverarchingCall = Call;
type Extrinsic = UncheckedExtrinsic;
}
parameter_types! {
pub const ParathreadDeposit: Balance = 5 * DOLLARS;
pub const QueueSize: usize = 2;