Initial: Offchain Workers (#1942)

* Refactor state-machine stuff.

* Fix tests.

* WiP

* WiP2

* Service support for offchain workers.

* Service support for offchain workers.

* Testing offchain worker.

* Initial version working.

* Pass side effects in call.

* Pass OffchainExt in context.

* Submit extrinsics to the pool.

* Support inherents.

* Insert to inherents pool.

* Inserting to the pool asynchronously.

* Add test to offchain worker.

* Implement convenience syntax for modules.

* Dispatching offchain worker through executive.

* Fix offchain test.

* Remove offchain worker from timestamp.

* Update Cargo.lock.

* Address review comments.

* Use latest patch version for futures.

* Add CLI parameter for offchain worker.

* Fix compilation.

* Fix test.

* Fix extrinsics format for tests.

* Fix RPC test.

* Bump spec version.

* Fix executive.

* Fix support macro.

* Address grumbles.

* Bump runtime
This commit is contained in:
Tomasz Drwięga
2019-03-25 23:22:11 +01:00
committed by Gav Wood
parent 3ee0e69463
commit e2f5e40876
58 changed files with 1158 additions and 178 deletions
-15
View File
@@ -393,21 +393,6 @@ impl From<sr25519::Signature> for AnySignature {
}
}
/// Context for executing a call into the runtime.
#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug, Serialize))]
#[repr(u8)]
pub enum ExecutionContext {
/// Context for general importing (including own blocks).
Importing,
/// Context used when syncing the blockchain.
Syncing,
/// Context used for block construction.
BlockConstruction,
/// Context used for other calls.
Other,
}
#[derive(Eq, PartialEq, Clone, Copy, Decode)]
#[cfg_attr(feature = "std", derive(Debug, Serialize))]
#[repr(u8)]
@@ -270,6 +270,24 @@ pub trait OnInitialise<BlockNumber> {
impl<N> OnInitialise<N> for () {}
/// Off-chain computation trait.
///
/// Implementing this trait on a module allows you to perform a long-running tasks
/// that make validators generate extrinsics (either transactions or inherents)
/// with results of those long-running computations.
///
/// NOTE: This function runs off-chain, so it can access the block state,
/// but cannot preform any alterations.
pub trait OffchainWorker<BlockNumber> {
/// This function is being called on every block.
///
/// Implement this and use special `extern`s to generate transactions or inherents.
/// Any state alterations are lost and are not persisted.
fn generate_extrinsics(_n: BlockNumber) {}
}
impl<N> OffchainWorker<N> for () {}
macro_rules! tuple_impl {
($one:ident,) => {
impl<Number: Copy, $one: OnFinalise<Number>> OnFinalise<Number> for ($one,) {
@@ -282,6 +300,11 @@ macro_rules! tuple_impl {
$one::on_initialise(n);
}
}
impl<Number: Copy, $one: OffchainWorker<Number>> OffchainWorker<Number> for ($one,) {
fn generate_extrinsics(n: Number) {
$one::generate_extrinsics(n);
}
}
};
($first:ident, $($rest:ident,)+) => {
impl<
@@ -304,6 +327,16 @@ macro_rules! tuple_impl {
$($rest::on_initialise(n);)+
}
}
impl<
Number: Copy,
$first: OffchainWorker<Number>,
$($rest: OffchainWorker<Number>),+
> OffchainWorker<Number> for ($first, $($rest),+) {
fn generate_extrinsics(n: Number) {
$first::generate_extrinsics(n);
$($rest::generate_extrinsics(n);)+
}
}
tuple_impl!($($rest,)+);
}
}