Add documentation to SubmitSignedTransaction and actually make it work (#4200)

* Add documentation to signed transactions and actually make them work.

* Fix naming and bounds.

* Forgotten import.

* Remove warning.

* Make accounts optional, fix logic.

* Split the method to avoid confusing type error message.

* Move executor tests to integration.

* Add submit transactions tests.

* Make `submit_transaction` tests compile

* Remove a file that was accidently committed

* Add can_sign helper function.

* Fix compilation.

* Add a key to keystore.

* Fix the tests.

* Remove env_logger.

* Fix sending multiple transactions.

* Remove commented code.

* Bring back criterion.

* Remove stray debug log.

* Apply suggestions from code review

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Make sure to initialize block correctly.

* Initialize block for offchain workers.

* Add test for transaction validity.

* Fix tests.

* Review suggestions.

* Remove redundant comment.

* Make sure to use correct block number of authoring.

* Change the runtime API.

* Support both versions.

* Bump spec version, fix RPC test.

Co-authored-by: Hernando Castano <HCastano@users.noreply.github.com>
Co-authored-by: Gavin Wood <github@gavwood.com>
Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Tomasz Drwięga
2020-01-10 01:46:55 +01:00
committed by Gavin Wood
parent a1e0076aa8
commit 74d6e660c6
29 changed files with 2096 additions and 1413 deletions
+17 -3
View File
@@ -571,14 +571,22 @@ mod tests {
inner: vec![seal_header(create_header(0, Default::default(), Default::default()), 999)],
};
let initialize_block = |number, hash: H256| System::initialize(
&number,
&hash,
&Default::default(),
&Default::default(),
Default::default()
);
for number in 1..8 {
System::initialize(&number, &canon_chain.best_hash(), &Default::default(), &Default::default());
initialize_block(number, canon_chain.best_hash());
let header = seal_header(System::finalize(), author_a);
canon_chain.push(header);
}
// initialize so system context is set up correctly.
System::initialize(&8, &canon_chain.best_hash(), &Default::default(), &Default::default());
initialize_block(8, canon_chain.best_hash());
// 2 of the same uncle at once
{
@@ -663,7 +671,13 @@ mod tests {
);
header.digest_mut().pop(); // pop the seal off.
System::initialize(&1, &Default::default(), &Default::default(), header.digest());
System::initialize(
&1,
&Default::default(),
&Default::default(),
header.digest(),
Default::default(),
);
assert_eq!(Authorship::author(), author);
});
+7 -1
View File
@@ -81,7 +81,13 @@ fn first_block_epoch_zero_start() {
);
assert_eq!(Babe::genesis_slot(), 0);
System::initialize(&1, &Default::default(), &Default::default(), &pre_digest);
System::initialize(
&1,
&Default::default(),
&Default::default(),
&pre_digest,
Default::default(),
);
// see implementation of the function for details why: we issue an
// epoch-change digest but don't do it via the normal session mechanism.
+23 -13
View File
@@ -861,6 +861,16 @@ fn storage_size() {
});
}
fn initialize_block(number: u64) {
System::initialize(
&number,
&[0u8; 32].into(),
&[0u8; 32].into(),
&Default::default(),
Default::default(),
);
}
#[test]
fn deduct_blocks() {
let (wasm, code_hash) = compile_module::<Test>(CODE_SET_RENT).unwrap();
@@ -881,7 +891,7 @@ fn deduct_blocks() {
assert_eq!(bob_contract.rent_allowance, 1_000);
// Advance 4 blocks
System::initialize(&5, &[0u8; 32].into(), &[0u8; 32].into(), &Default::default());
initialize_block(5);
// Trigger rent through call
assert_ok!(Contract::call(Origin::signed(ALICE), BOB, 0, 100_000, call::null()));
@@ -896,7 +906,7 @@ fn deduct_blocks() {
assert_eq!(Balances::free_balance(BOB), 30_000 - rent);
// Advance 7 blocks more
System::initialize(&12, &[0u8; 32].into(), &[0u8; 32].into(), &Default::default());
initialize_block(12);
// Trigger rent through call
assert_ok!(Contract::call(Origin::signed(ALICE), BOB, 0, 100_000, call::null()));
@@ -971,7 +981,7 @@ fn claim_surcharge(blocks: u64, trigger_call: impl Fn() -> bool, removes: bool)
));
// Advance blocks
System::initialize(&blocks, &[0u8; 32].into(), &[0u8; 32].into(), &Default::default());
initialize_block(blocks);
// Trigger rent through call
assert!(trigger_call());
@@ -1011,7 +1021,7 @@ fn removals(trigger_call: impl Fn() -> bool) {
assert_eq!(Balances::free_balance(&BOB), 100);
// Advance blocks
System::initialize(&10, &[0u8; 32].into(), &[0u8; 32].into(), &Default::default());
initialize_block(10);
// Trigger rent through call
assert!(trigger_call());
@@ -1019,7 +1029,7 @@ fn removals(trigger_call: impl Fn() -> bool) {
assert_eq!(Balances::free_balance(&BOB), subsistence_threshold);
// Advance blocks
System::initialize(&20, &[0u8; 32].into(), &[0u8; 32].into(), &Default::default());
initialize_block(20);
// Trigger rent must have no effect
assert!(trigger_call());
@@ -1045,7 +1055,7 @@ fn removals(trigger_call: impl Fn() -> bool) {
assert_eq!(Balances::free_balance(&BOB), 1_000);
// Advance blocks
System::initialize(&10, &[0u8; 32].into(), &[0u8; 32].into(), &Default::default());
initialize_block(10);
// Trigger rent through call
assert!(trigger_call());
@@ -1054,7 +1064,7 @@ fn removals(trigger_call: impl Fn() -> bool) {
assert_eq!(Balances::free_balance(&BOB), 900);
// Advance blocks
System::initialize(&20, &[0u8; 32].into(), &[0u8; 32].into(), &Default::default());
initialize_block(20);
// Trigger rent must have no effect
assert!(trigger_call());
@@ -1085,7 +1095,7 @@ fn removals(trigger_call: impl Fn() -> bool) {
assert_eq!(Balances::free_balance(&BOB), Balances::minimum_balance());
// Advance blocks
System::initialize(&10, &[0u8; 32].into(), &[0u8; 32].into(), &Default::default());
initialize_block(10);
// Trigger rent through call
assert!(trigger_call());
@@ -1093,7 +1103,7 @@ fn removals(trigger_call: impl Fn() -> bool) {
assert_eq!(Balances::free_balance(&BOB), Balances::minimum_balance());
// Advance blocks
System::initialize(&20, &[0u8; 32].into(), &[0u8; 32].into(), &Default::default());
initialize_block(20);
// Trigger rent must have no effect
assert!(trigger_call());
@@ -1122,7 +1132,7 @@ fn call_removed_contract() {
assert_ok!(Contract::call(Origin::signed(ALICE), BOB, 0, 100_000, call::null()));
// Advance blocks
System::initialize(&10, &[0u8; 32].into(), &[0u8; 32].into(), &Default::default());
initialize_block(10);
// Calling contract should remove contract and fail.
assert_err!(
@@ -1209,7 +1219,7 @@ fn default_rent_allowance_on_instantiate() {
assert_eq!(bob_contract.rent_allowance, <BalanceOf<Test>>::max_value());
// Advance blocks
System::initialize(&5, &[0u8; 32].into(), &[0u8; 32].into(), &Default::default());
initialize_block(5);
// Trigger rent through call
assert_ok!(Contract::call(Origin::signed(ALICE), BOB, 0, 100_000, call::null()));
@@ -1355,7 +1365,7 @@ fn restoration(test_different_storage: bool, test_restore_to_with_dirty_storage:
}
// Advance 4 blocks, to the 5th.
System::initialize(&5, &[0u8; 32].into(), &[0u8; 32].into(), &Default::default());
initialize_block(5);
// Call `BOB`, which makes it pay rent. Since the rent allowance is set to 0
// we expect that it will get removed leaving tombstone.
@@ -1384,7 +1394,7 @@ fn restoration(test_different_storage: bool, test_restore_to_with_dirty_storage:
if !test_restore_to_with_dirty_storage {
// Advance 1 block, to the 6th.
System::initialize(&6, &[0u8; 32].into(), &[0u8; 32].into(), &Default::default());
initialize_block(6);
}
// Perform a call to `DJANGO`. This should either perform restoration successfully or
+9 -9
View File
@@ -5,13 +5,13 @@ authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[dependencies]
serde = { version = "1.0.101", optional = true }
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] }
sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" }
sp-io ={ path = "../../primitives/io", default-features = false }
sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" }
frame-support = { version = "2.0.0", default-features = false, path = "../support" }
frame-system = { version = "2.0.0", default-features = false, path = "../system" }
serde = { version = "1.0.101", optional = true }
sp-io ={ path = "../../primitives/io", default-features = false }
sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" }
sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" }
[dev-dependencies]
hex-literal = "0.2.1"
@@ -23,11 +23,11 @@ pallet-transaction-payment = { version = "2.0.0", path = "../transaction-payment
[features]
default = ["std"]
std = [
"sp-std/std",
"frame-support/std",
"serde",
"codec/std",
"sp-runtime/std",
"sp-io/std",
"frame-support/std",
"frame-system/std",
"serde",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
]
+43 -7
View File
@@ -82,7 +82,7 @@ use sp_runtime::{
generic::Digest, ApplyExtrinsicResult,
traits::{
self, Header, Zero, One, Checkable, Applyable, CheckEqual, OnFinalize, OnInitialize,
NumberFor, Block as BlockT, OffchainWorker, Dispatchable,
NumberFor, Block as BlockT, OffchainWorker, Dispatchable, Saturating,
},
transaction_validity::TransactionValidity,
};
@@ -154,9 +154,23 @@ where
{
/// Start the execution of a particular block.
pub fn initialize_block(header: &System::Header) {
let mut digests = <DigestOf<System>>::default();
header.digest().logs().iter().for_each(|d| if d.as_pre_runtime().is_some() { digests.push(d.clone()) });
Self::initialize_block_impl(header.number(), header.parent_hash(), header.extrinsics_root(), &digests);
let digests = Self::extract_pre_digest(&header);
Self::initialize_block_impl(
header.number(),
header.parent_hash(),
header.extrinsics_root(),
&digests
);
}
fn extract_pre_digest(header: &System::Header) -> DigestOf<System> {
let mut digest = <DigestOf<System>>::default();
header.digest().logs()
.iter()
.for_each(|d| if d.as_pre_runtime().is_some() {
digest.push(d.clone())
});
digest
}
fn initialize_block_impl(
@@ -165,7 +179,13 @@ where
extrinsics_root: &System::Hash,
digest: &Digest<System::Hash>,
) {
<frame_system::Module<System>>::initialize(block_number, parent_hash, extrinsics_root, digest);
<frame_system::Module<System>>::initialize(
block_number,
parent_hash,
extrinsics_root,
digest,
frame_system::InitKind::Full,
);
<AllModules as OnInitialize<System::BlockNumber>>::on_initialize(*block_number);
<frame_system::Module<System>>::register_extra_weight_unchecked(
<AllModules as WeighBlock<System::BlockNumber>>::on_initialize(*block_number)
@@ -310,8 +330,24 @@ where
}
/// Start an offchain worker and generate extrinsics.
pub fn offchain_worker(n: System::BlockNumber) {
<AllModules as OffchainWorker<System::BlockNumber>>::offchain_worker(n)
pub fn offchain_worker(header: &System::Header) {
// We need to keep events available for offchain workers,
// hence we initialize the block manually.
// OffchainWorker RuntimeApi should skip initialization.
let digests = Self::extract_pre_digest(header);
<frame_system::Module<System>>::initialize(
header.number(),
header.parent_hash(),
header.extrinsics_root(),
&digests,
frame_system::InitKind::Inspection,
);
<AllModules as OffchainWorker<System::BlockNumber>>::offchain_worker(
// to maintain backward compatibility we call module offchain workers
// with parent block number.
header.number().saturating_sub(1.into())
)
}
}
+14 -2
View File
@@ -291,7 +291,13 @@ mod tests {
TestExternalities::new(t).execute_with(|| {
let mut parent_hash = System::parent_hash();
for i in 2..106 {
System::initialize(&i, &parent_hash, &Default::default(), &Default::default());
System::initialize(
&i,
&parent_hash,
&Default::default(),
&Default::default(),
Default::default()
);
FinalityTracker::on_finalize(i);
let hdr = System::finalize();
parent_hash = hdr.hash();
@@ -310,7 +316,13 @@ mod tests {
TestExternalities::new(t).execute_with(|| {
let mut parent_hash = System::parent_hash();
for i in 2..106 {
System::initialize(&i, &parent_hash, &Default::default(), &Default::default());
System::initialize(
&i,
&parent_hash,
&Default::default(),
&Default::default(),
Default::default(),
);
assert_ok!(FinalityTracker::dispatch(
Call::final_hint(i-1),
Origin::NONE,
+29 -19
View File
@@ -18,17 +18,27 @@
#![cfg(test)]
use sp_runtime::{testing::Digest, traits::{Header, OnFinalize}};
use sp_runtime::{testing::{H256, Digest}, traits::{Header, OnFinalize}};
use crate::mock::*;
use frame_system::{EventRecord, Phase};
use codec::{Decode, Encode};
use fg_primitives::ScheduledChange;
use super::*;
fn initialize_block(number: u64, parent_hash: H256) {
System::initialize(
&number,
&parent_hash,
&Default::default(),
&Default::default(),
Default::default(),
);
}
#[test]
fn authorities_change_logged() {
new_test_ext(vec![(1, 1), (2, 1), (3, 1)]).execute_with(|| {
System::initialize(&1, &Default::default(), &Default::default(), &Default::default());
initialize_block(1, Default::default());
Grandpa::schedule_change(to_authorities(vec![(4, 1), (5, 1), (6, 1)]), 0, None).unwrap();
System::note_finished_extrinsics();
@@ -56,7 +66,7 @@ fn authorities_change_logged() {
#[test]
fn authorities_change_logged_after_delay() {
new_test_ext(vec![(1, 1), (2, 1), (3, 1)]).execute_with(|| {
System::initialize(&1, &Default::default(), &Default::default(), &Default::default());
initialize_block(1, Default::default());
Grandpa::schedule_change(to_authorities(vec![(4, 1), (5, 1), (6, 1)]), 1, None).unwrap();
Grandpa::on_finalize(1);
let header = System::finalize();
@@ -71,7 +81,7 @@ fn authorities_change_logged_after_delay() {
// no change at this height.
assert_eq!(System::events(), vec![]);
System::initialize(&2, &header.hash(), &Default::default(), &Default::default());
initialize_block(2, header.hash());
System::note_finished_extrinsics();
Grandpa::on_finalize(2);
@@ -89,7 +99,7 @@ fn authorities_change_logged_after_delay() {
#[test]
fn cannot_schedule_change_when_one_pending() {
new_test_ext(vec![(1, 1), (2, 1), (3, 1)]).execute_with(|| {
System::initialize(&1, &Default::default(), &Default::default(), &Default::default());
initialize_block(1, Default::default());
Grandpa::schedule_change(to_authorities(vec![(4, 1), (5, 1), (6, 1)]), 1, None).unwrap();
assert!(<PendingChange<Test>>::exists());
assert!(Grandpa::schedule_change(to_authorities(vec![(5, 1)]), 1, None).is_err());
@@ -97,14 +107,14 @@ fn cannot_schedule_change_when_one_pending() {
Grandpa::on_finalize(1);
let header = System::finalize();
System::initialize(&2, &header.hash(), &Default::default(), &Default::default());
initialize_block(2, header.hash());
assert!(<PendingChange<Test>>::exists());
assert!(Grandpa::schedule_change(to_authorities(vec![(5, 1)]), 1, None).is_err());
Grandpa::on_finalize(2);
let header = System::finalize();
System::initialize(&3, &header.hash(), &Default::default(), &Default::default());
initialize_block(3, header.hash());
assert!(!<PendingChange<Test>>::exists());
assert!(Grandpa::schedule_change(to_authorities(vec![(5, 1)]), 1, None).is_ok());
@@ -132,7 +142,7 @@ fn new_decodes_from_old() {
#[test]
fn dispatch_forced_change() {
new_test_ext(vec![(1, 1), (2, 1), (3, 1)]).execute_with(|| {
System::initialize(&1, &Default::default(), &Default::default(), &Default::default());
initialize_block(1, Default::default());
Grandpa::schedule_change(
to_authorities(vec![(4, 1), (5, 1), (6, 1)]),
5,
@@ -146,7 +156,7 @@ fn dispatch_forced_change() {
let mut header = System::finalize();
for i in 2..7 {
System::initialize(&i, &header.hash(), &Default::default(), &Default::default());
initialize_block(i, header.hash());
assert!(<PendingChange<Test>>::get().unwrap().forced.is_some());
assert_eq!(Grandpa::next_forced(), Some(11));
assert!(Grandpa::schedule_change(to_authorities(vec![(5, 1)]), 1, None).is_err());
@@ -159,7 +169,7 @@ fn dispatch_forced_change() {
// change has been applied at the end of block 6.
// add a normal change.
{
System::initialize(&7, &header.hash(), &Default::default(), &Default::default());
initialize_block(7, header.hash());
assert!(!<PendingChange<Test>>::exists());
assert_eq!(Grandpa::grandpa_authorities(), to_authorities(vec![(4, 1), (5, 1), (6, 1)]));
assert!(Grandpa::schedule_change(to_authorities(vec![(5, 1)]), 1, None).is_ok());
@@ -169,7 +179,7 @@ fn dispatch_forced_change() {
// run the normal change.
{
System::initialize(&8, &header.hash(), &Default::default(), &Default::default());
initialize_block(8, header.hash());
assert!(<PendingChange<Test>>::exists());
assert_eq!(Grandpa::grandpa_authorities(), to_authorities(vec![(4, 1), (5, 1), (6, 1)]));
assert!(Grandpa::schedule_change(to_authorities(vec![(5, 1)]), 1, None).is_err());
@@ -180,7 +190,7 @@ fn dispatch_forced_change() {
// normal change applied. but we can't apply a new forced change for some
// time.
for i in 9..11 {
System::initialize(&i, &header.hash(), &Default::default(), &Default::default());
initialize_block(i, header.hash());
assert!(!<PendingChange<Test>>::exists());
assert_eq!(Grandpa::grandpa_authorities(), to_authorities(vec![(5, 1)]));
assert_eq!(Grandpa::next_forced(), Some(11));
@@ -190,7 +200,7 @@ fn dispatch_forced_change() {
}
{
System::initialize(&11, &header.hash(), &Default::default(), &Default::default());
initialize_block(11, header.hash());
assert!(!<PendingChange<Test>>::exists());
assert!(Grandpa::schedule_change(to_authorities(vec![(5, 1), (6, 1), (7, 1)]), 5, Some(0)).is_ok());
assert_eq!(Grandpa::next_forced(), Some(21));
@@ -205,7 +215,7 @@ fn dispatch_forced_change() {
fn schedule_pause_only_when_live() {
new_test_ext(vec![(1, 1), (2, 1), (3, 1)]).execute_with(|| {
// we schedule a pause at block 1 with delay of 1
System::initialize(&1, &Default::default(), &Default::default(), &Default::default());
initialize_block(1, Default::default());
Grandpa::schedule_pause(1).unwrap();
// we've switched to the pending pause state
@@ -220,7 +230,7 @@ fn schedule_pause_only_when_live() {
Grandpa::on_finalize(1);
let _ = System::finalize();
System::initialize(&2, &Default::default(), &Default::default(), &Default::default());
initialize_block(2, Default::default());
// signaling a pause now should fail
assert!(Grandpa::schedule_pause(1).is_err());
@@ -239,7 +249,7 @@ fn schedule_pause_only_when_live() {
#[test]
fn schedule_resume_only_when_paused() {
new_test_ext(vec![(1, 1), (2, 1), (3, 1)]).execute_with(|| {
System::initialize(&1, &Default::default(), &Default::default(), &Default::default());
initialize_block(1, Default::default());
// the set is currently live, resuming it is an error
assert!(Grandpa::schedule_resume(1).is_err());
@@ -260,16 +270,16 @@ fn schedule_resume_only_when_paused() {
);
// we schedule the set to go back live in 2 blocks
System::initialize(&2, &Default::default(), &Default::default(), &Default::default());
initialize_block(2, Default::default());
Grandpa::schedule_resume(2).unwrap();
Grandpa::on_finalize(2);
let _ = System::finalize();
System::initialize(&3, &Default::default(), &Default::default(), &Default::default());
initialize_block(3, Default::default());
Grandpa::on_finalize(3);
let _ = System::finalize();
System::initialize(&4, &Default::default(), &Default::default(), &Default::default());
initialize_block(4, Default::default());
Grandpa::on_finalize(4);
let _ = System::finalize();
+8 -4
View File
@@ -171,10 +171,14 @@ pub type AuthIndex = u32;
pub struct Heartbeat<BlockNumber>
where BlockNumber: PartialEq + Eq + Decode + Encode,
{
block_number: BlockNumber,
network_state: OpaqueNetworkState,
session_index: SessionIndex,
authority_index: AuthIndex,
/// Block number at the time heartbeat is created..
pub block_number: BlockNumber,
/// A state of local network (peer id and external addresses)
pub network_state: OpaqueNetworkState,
/// Index of the current session.
pub session_index: SessionIndex,
/// An index of the authority on the list of validators.
pub authority_index: AuthIndex,
}
pub trait Trait: frame_system::Trait + pallet_session::historical::Trait {
@@ -210,7 +210,13 @@ mod tests {
let mut parent_hash = System::parent_hash();
for i in 1 .. (blocks + 1) {
System::initialize(&i, &parent_hash, &Default::default(), &Default::default());
System::initialize(
&i,
&parent_hash,
&Default::default(),
&Default::default(),
frame_system::InitKind::Full,
);
CollectiveFlip::on_initialize(i);
let header = System::finalize();
+44 -5
View File
@@ -531,6 +531,27 @@ pub fn ensure_none<OuterOrigin, AccountId>(o: OuterOrigin) -> Result<(), BadOrig
}
}
/// A type of block initialization to perform.
pub enum InitKind {
/// Leave inspectable storage entries in state.
///
/// i.e. `Events` are not being reset.
/// Should only be used for off-chain calls,
/// regular block execution should clear those.
Inspection,
/// Reset also inspectable storage entries.
///
/// This should be used for regular block execution.
Full,
}
impl Default for InitKind {
fn default() -> Self {
InitKind::Full
}
}
impl<T: Trait> Module<T> {
/// Deposits an event into this block's event record.
pub fn deposit_event(event: impl Into<T::Event>) {
@@ -633,6 +654,7 @@ impl<T: Trait> Module<T> {
parent_hash: &T::Hash,
txs_root: &T::Hash,
digest: &DigestOf<T>,
kind: InitKind,
) {
// populate environment
storage::unhashed::put(well_known_keys::EXTRINSIC_INDEX, &0u32);
@@ -641,9 +663,12 @@ impl<T: Trait> Module<T> {
<ParentHash<T>>::put(parent_hash);
<BlockHash<T>>::insert(*number - One::one(), parent_hash);
<ExtrinsicsRoot<T>>::put(txs_root);
<Events<T>>::kill();
EventCount::kill();
<EventTopics<T>>::remove_all();
if let InitKind::Full = kind {
<Events<T>>::kill();
EventCount::kill();
<EventTopics<T>>::remove_all();
}
}
/// Remove temporary "environment" entries in storage.
@@ -1220,7 +1245,13 @@ mod tests {
#[test]
fn deposit_event_should_work() {
new_test_ext().execute_with(|| {
System::initialize(&1, &[0u8; 32].into(), &[0u8; 32].into(), &Default::default());
System::initialize(
&1,
&[0u8; 32].into(),
&[0u8; 32].into(),
&Default::default(),
InitKind::Full,
);
System::note_finished_extrinsics();
System::deposit_event(1u16);
System::finalize();
@@ -1235,7 +1266,13 @@ mod tests {
]
);
System::initialize(&2, &[0u8; 32].into(), &[0u8; 32].into(), &Default::default());
System::initialize(
&2,
&[0u8; 32].into(),
&[0u8; 32].into(),
&Default::default(),
InitKind::Full,
);
System::deposit_event(42u16);
System::note_applied_extrinsic(&Ok(()), 0, Default::default());
System::note_applied_extrinsic(&Err(DispatchError::BadOrigin), 0, Default::default());
@@ -1264,6 +1301,7 @@ mod tests {
&[0u8; 32].into(),
&[0u8; 32].into(),
&Default::default(),
InitKind::Full,
);
System::note_finished_extrinsics();
@@ -1329,6 +1367,7 @@ mod tests {
&[n as u8 - 1; 32].into(),
&[0u8; 32].into(),
&Default::default(),
InitKind::Full,
);
System::finalize();
+250 -50
View File
@@ -14,49 +14,19 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Module helpers for offchain calls.
//! Module helpers for off-chain calls.
use codec::Encode;
use sp_runtime::app_crypto::{self, RuntimeAppPublic};
use sp_std::convert::TryInto;
use sp_std::prelude::Vec;
use sp_runtime::app_crypto::{RuntimeAppPublic, AppPublic, AppSignature};
use sp_runtime::traits::{Extrinsic as ExtrinsicT, IdentifyAccount};
use frame_support::debug;
/// A trait responsible for signing a payload using given account.
pub trait Signer<Public, Signature> {
/// Sign any encodable payload with given account and produce a signature.
///
/// Returns `Some` if signing succeeded and `None` in case the `account` couldn't be used.
fn sign<Payload: Encode>(public: Public, payload: &Payload) -> Option<Signature>;
}
/// A `Signer` implementation for any `AppPublic` type.
/// Creates runtime-specific signed transaction.
///
/// This implementation additionaly supports conversion to/from multi-signature/multi-signer
/// wrappers.
/// If the wrapped crypto doesn't match `AppPublic`s crypto `None` is returned.
impl<Public, Signature, AppPublic> Signer<Public, Signature> for AppPublic where
AppPublic: RuntimeAppPublic
+ app_crypto::AppPublic
+ From<<AppPublic as app_crypto::AppPublic>::Generic>,
<AppPublic as RuntimeAppPublic>::Signature: app_crypto::AppSignature,
Signature: From<
<<AppPublic as RuntimeAppPublic>::Signature as app_crypto::AppSignature>::Generic
>,
Public: sp_std::convert::TryInto<<AppPublic as app_crypto::AppPublic>::Generic>
{
fn sign<Payload: Encode>(public: Public, raw_payload: &Payload) -> Option<Signature> {
raw_payload.using_encoded(|payload| {
let public = public.try_into().ok()?;
AppPublic::from(public).sign(&payload)
.map(
<<AppPublic as RuntimeAppPublic>::Signature as app_crypto::AppSignature>
::Generic::from
)
.map(Signature::from)
})
}
}
/// Creates a runtime-specific signed transaction.
/// This trait should be implemented by your `Runtime` to be able
/// to submit `SignedTransaction`s` to the pool from off-chain code.
pub trait CreateTransaction<T: crate::Trait, Extrinsic: ExtrinsicT> {
/// A `Public` key representing a particular `AccountId`.
type Public: IdentifyAccount<AccountId=T::AccountId> + Clone;
@@ -77,15 +47,67 @@ pub trait CreateTransaction<T: crate::Trait, Extrinsic: ExtrinsicT> {
) -> Option<(Extrinsic::Call, Extrinsic::SignaturePayload)>;
}
type PublicOf<T, Call, X> = <
<X as SubmitSignedTransaction<T, Call>>::CreateTransaction as CreateTransaction<
T,
<X as SubmitSignedTransaction<T, Call>>::Extrinsic,
>
/// A trait responsible for signing a payload using given account.
///
/// This trait is usually going to represent a local public key
/// that has ability to sign arbitrary `Payloads`.
///
/// NOTE: Most likely you don't need to implement this trait manually.
/// It has a blanket implementation for all `RuntimeAppPublic` types,
/// so it's enough to pass an application-specific crypto type.
///
/// To easily create `SignedTransaction`s have a look at the
/// [`TransactionSubmitter`] type.
pub trait Signer<Public, Signature> {
/// Sign any encodable payload with given account and produce a signature.
///
/// Returns `Some` if signing succeeded and `None` in case the `account` couldn't
/// be used (for instance we couldn't convert it to required application specific crypto).
fn sign<Payload: Encode>(public: Public, payload: &Payload) -> Option<Signature>;
}
/// A `Signer` implementation for any `AppPublic` type.
///
/// This implementation additionaly supports conversion to/from multi-signature/multi-signer
/// wrappers.
/// If the wrapped crypto doesn't match `AppPublic`s crypto `None` is returned.
impl<Public, Signature, TAnyAppPublic> Signer<Public, Signature> for TAnyAppPublic where
TAnyAppPublic: RuntimeAppPublic
+ AppPublic
+ From<<TAnyAppPublic as AppPublic>::Generic>,
<TAnyAppPublic as RuntimeAppPublic>::Signature: AppSignature,
Signature: From<
<<TAnyAppPublic as RuntimeAppPublic>::Signature as AppSignature>::Generic
>,
Public: TryInto<<TAnyAppPublic as AppPublic>::Generic>
{
fn sign<Payload: Encode>(public: Public, raw_payload: &Payload) -> Option<Signature> {
raw_payload.using_encoded(|payload| {
let public = public.try_into().ok()?;
TAnyAppPublic::from(public).sign(&payload)
.map(
<<TAnyAppPublic as RuntimeAppPublic>::Signature as AppSignature>
::Generic::from
)
.map(Signature::from)
})
}
}
/// Retrieves a public key type for given `SignAndSubmitTransaction`.
pub type PublicOf<T, Call, X> = <
<X as SignAndSubmitTransaction<T, Call>>::CreateTransaction
as
CreateTransaction<T, <X as SignAndSubmitTransaction<T, Call>>::Extrinsic>
>::Public;
/// A trait to sign and submit transactions in offchain calls.
pub trait SubmitSignedTransaction<T: crate::Trait, Call> {
/// A trait to sign and submit transactions in off-chain calls.
///
/// NOTE: Most likely you should not implement this trait yourself.
/// There is an implementation for
/// [`TransactionSubmitter`] type, which
/// you should use.
pub trait SignAndSubmitTransaction<T: crate::Trait, Call> {
/// Unchecked extrinsic type.
type Extrinsic: ExtrinsicT<Call=Call> + codec::Encode;
@@ -107,15 +129,30 @@ pub trait SubmitSignedTransaction<T: crate::Trait, Call> {
let call = call.into();
let id = public.clone().into_account();
let expected = <crate::Module<T>>::account_nonce(&id);
debug::native::debug!(
target: "offchain",
"Creating signed transaction from account: {:?} (nonce: {:?})",
id,
expected,
);
let (call, signature_data) = Self::CreateTransaction
::create_transaction::<Self::Signer>(call, public, id, expected)
::create_transaction::<Self::Signer>(call, public, id.clone(), expected)
.ok_or(())?;
// increment the nonce. This is fine, since the code should always
// be running in off-chain context, so we NEVER persists data.
<crate::Module<T>>::inc_account_nonce(&id);
let xt = Self::Extrinsic::new(call, Some(signature_data)).ok_or(())?;
sp_io::offchain::submit_transaction(xt.encode())
}
}
/// A trait to submit unsigned transactions in off-chain calls.
///
/// NOTE: Most likely you should not implement this trait yourself.
/// There is an implementation for
/// [`TransactionSubmitter`] type, which
/// you should use.
pub trait SubmitUnsignedTransaction<T: crate::Trait, Call> {
/// Unchecked extrinsic type.
type Extrinsic: ExtrinsicT<Call=Call> + codec::Encode;
@@ -130,9 +167,114 @@ pub trait SubmitUnsignedTransaction<T: crate::Trait, Call> {
}
}
/// A utility trait to easily create signed transactions
/// from accounts in node's local keystore.
///
/// NOTE: Most likely you should not implement this trait yourself.
/// There is an implementation for
/// [`TransactionSubmitter`] type, which
/// you should use.
pub trait SubmitSignedTransaction<T: crate::Trait, Call> {
/// A `SignAndSubmitTransaction` implementation.
type SignAndSubmit: SignAndSubmitTransaction<T, Call>;
/// Find local keys that match given list of accounts.
///
/// Technically it finds an intersection between given list of `AccountId`s
/// and accounts that are represented by public keys in local keystore.
/// If `None` is passed it returns all accounts in the keystore.
///
/// Returns both public keys and `AccountId`s of accounts that are available.
/// Such accounts can later be used to sign a payload or send signed transactions.
fn find_local_keys(accounts: Option<impl IntoIterator<Item = T::AccountId>>) -> Vec<(
T::AccountId,
PublicOf<T, Call, Self::SignAndSubmit>,
)>;
/// Find all available local keys.
///
/// This is equivalent of calling `find_local_keys(None)`.
fn find_all_local_keys() -> Vec<(T::AccountId, PublicOf<T, Call, Self::SignAndSubmit>)> {
Self::find_local_keys(None as Option<Vec<_>>)
}
/// Check if there are keys for any of given accounts that could be used to send a transaction.
///
/// This check can be used as an early-exit condition to avoid doing too
/// much work, before we actually realise that there are no accounts that you
/// we could use for signing.
fn can_sign_with(accounts: Option<impl IntoIterator<Item = T::AccountId>>) -> bool {
!Self::find_local_keys(accounts).is_empty()
}
/// Check if there are any keys that could be used for signing.
///
/// This is equivalent of calling `can_sign_with(None)`.
fn can_sign() -> bool {
Self::can_sign_with(None as Option<Vec<_>>)
}
/// Create and submit signed transactions from supported accounts.
///
/// This method should intersect given list of accounts with the ones
/// supported locally and submit signed transaction containing given `Call`
/// with every of them.
///
/// Returns a vector of results and account ids that were supported.
#[must_use]
fn submit_signed_from(
call: impl Into<Call> + Clone,
accounts: impl IntoIterator<Item = T::AccountId>,
) -> Vec<(T::AccountId, Result<(), ()>)> {
let keys = Self::find_local_keys(Some(accounts));
keys.into_iter().map(|(account, pub_key)| {
let call = call.clone().into();
(
account,
Self::SignAndSubmit::sign_and_submit(call, pub_key)
)
}).collect()
}
/// Create and submit signed transactions from all local accounts.
///
/// This method submits a signed transaction from all local accounts
/// for given application crypto.
///
/// Returns a vector of results and account ids that were supported.
#[must_use]
fn submit_signed(
call: impl Into<Call> + Clone,
) -> Vec<(T::AccountId, Result<(), ()>)> {
let keys = Self::find_all_local_keys();
keys.into_iter().map(|(account, pub_key)| {
let call = call.clone().into();
(
account,
Self::SignAndSubmit::sign_and_submit(call, pub_key)
)
}).collect()
}
}
/// A default type used to submit transactions to the pool.
pub struct TransactionSubmitter<S, C, E> {
_signer: sp_std::marker::PhantomData<(S, C, E)>,
///
/// This is passed into each runtime as an opaque associated type that can have either of:
/// - [`SignAndSubmitTransaction`]
/// - [`SubmitUnsignedTransaction`]
/// - [`SubmitSignedTransaction`]
/// and used accordingly.
///
/// This struct should be constructed by providing the following generic parameters:
/// * `Signer` - Usually the application specific key type (see `app_crypto`).
/// * `CreateTransaction` - A type that is able to produce signed transactions,
/// usually it's going to be the entire `Runtime` object.
/// * `Extrinsic` - A runtime-specific type for in-block extrinsics.
///
/// If you only need the ability to submit unsigned transactions,
/// you may substitute both `Signer` and `CreateTransaction` with any type.
pub struct TransactionSubmitter<Signer, CreateTransaction, Extrinsic> {
_signer: sp_std::marker::PhantomData<(Signer, CreateTransaction, Extrinsic)>,
}
impl<S, C, E> Default for TransactionSubmitter<S, C, E> {
@@ -144,7 +286,7 @@ impl<S, C, E> Default for TransactionSubmitter<S, C, E> {
}
/// A blanket implementation to simplify creation of transaction signer & submitter in the runtime.
impl<T, E, S, C, Call> SubmitSignedTransaction<T, Call> for TransactionSubmitter<S, C, E> where
impl<T, E, S, C, Call> SignAndSubmitTransaction<T, Call> for TransactionSubmitter<S, C, E> where
T: crate::Trait,
C: CreateTransaction<T, E>,
S: Signer<<C as CreateTransaction<T, E>>::Public, <C as CreateTransaction<T, E>>::Signature>,
@@ -155,10 +297,68 @@ impl<T, E, S, C, Call> SubmitSignedTransaction<T, Call> for TransactionSubmitter
type Signer = S;
}
/// A blanket impl to use the same submitter for usigned transactions as well.
/// A blanket implementation to use the same submitter for unsigned transactions as well.
impl<T, E, S, C, Call> SubmitUnsignedTransaction<T, Call> for TransactionSubmitter<S, C, E> where
T: crate::Trait,
E: ExtrinsicT<Call=Call> + codec::Encode,
{
type Extrinsic = E;
}
/// A blanket implementation to support local keystore of application-crypto types.
impl<T, C, E, S, Call> SubmitSignedTransaction<T, Call> for TransactionSubmitter<S, C, E> where
T: crate::Trait,
C: CreateTransaction<T, E>,
E: ExtrinsicT<Call=Call> + codec::Encode,
S: Signer<<C as CreateTransaction<T, E>>::Public, <C as CreateTransaction<T, E>>::Signature>,
// Make sure we can unwrap the app crypto key.
S: RuntimeAppPublic + AppPublic + Into<<S as AppPublic>::Generic>,
// Make sure we can convert from wrapped crypto to public key (e.g. `MultiSigner`)
S::Generic: Into<PublicOf<T, Call, Self>>,
// For simplicity we require the same trait to implement `SignAndSubmitTransaction` too.
Self: SignAndSubmitTransaction<T, Call, Signer = S, Extrinsic = E, CreateTransaction = C>,
{
type SignAndSubmit = Self;
fn find_local_keys(accounts: Option<impl IntoIterator<Item = T::AccountId>>) -> Vec<(
T::AccountId,
PublicOf<T, Call, Self::SignAndSubmit>,
)> {
// Convert app-specific keys into generic ones.
let local_accounts_and_keys = S::all()
.into_iter()
.map(|app_key| {
// unwrap app-crypto
let generic_pub_key: <S as AppPublic>::Generic = app_key.into();
// convert to expected public key type (might be MultiSigner)
let signer_pub_key: PublicOf<T, Call, Self::SignAndSubmit> = generic_pub_key.into();
// lookup accountid for that pubkey
let account = signer_pub_key.clone().into_account();
(account, signer_pub_key)
}).collect::<Vec<_>>();
if let Some(accounts) = accounts {
let mut local_accounts_and_keys = local_accounts_and_keys;
// sort by accountId to allow bin-search.
local_accounts_and_keys.sort_by(|a, b| a.0.cmp(&b.0));
// get all the matching accounts
accounts.into_iter().filter_map(|acc| {
let idx = local_accounts_and_keys.binary_search_by(|a| a.0.cmp(&acc)).ok()?;
local_accounts_and_keys.get(idx).cloned()
}).collect()
} else {
// just return all account ids and keys
local_accounts_and_keys
}
}
fn can_sign_with(accounts: Option<impl IntoIterator<Item = T::AccountId>>) -> bool {
// early exit if we care about any account.
if accounts.is_none() {
!S::all().is_empty()
} else {
!Self::find_local_keys(accounts).is_empty()
}
}
}