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
+84
View File
@@ -117,6 +117,7 @@ macro_rules! decl_module {
{}
{}
{}
{}
[]
$($t)*
);
@@ -135,6 +136,7 @@ macro_rules! decl_module {
{}
{}
{}
{}
[]
$($t)*
);
@@ -147,6 +149,7 @@ macro_rules! decl_module {
{}
{ $( $on_initialise:tt )* }
{ $( $on_finalise:tt )* }
{ $( $offchain:tt )* }
[ $($t:tt)* ]
$(#[doc = $doc_attr:tt])*
$vis:vis fn deposit_event $(<$dpeg:ident $(, $dpeg_instance:ident)?>)* () = default;
@@ -159,6 +162,7 @@ macro_rules! decl_module {
{ $vis fn deposit_event $(<$dpeg $(, $dpeg_instance)?>)* () = default; }
{ $( $on_initialise )* }
{ $( $on_finalise )* }
{ $( $offchain )* }
[ $($t)* ]
$($rest)*
);
@@ -170,6 +174,7 @@ macro_rules! decl_module {
{}
{ $( $on_initialise:tt )* }
{ $( $on_finalise:tt )* }
{ $( $offchain:tt )* }
[ $($t:tt)* ]
$(#[doc = $doc_attr:tt])*
$vis:vis fn deposit_event $(<$dpeg:ident $(, $dpeg_instance:ident)?>)* (
@@ -184,6 +189,7 @@ macro_rules! decl_module {
{ $vis fn deposit_event $(<$dpeg $(, $dpeg_instance)?>)* ($( $param_name: $param ),* ) { $( $impl )* } }
{ $( $on_initialise )* }
{ $( $on_finalise )* }
{ $( $offchain )* }
[ $($t)* ]
$($rest)*
);
@@ -195,6 +201,7 @@ macro_rules! decl_module {
{ $( $deposit_event:tt )* }
{ $( $on_initialise:tt )* }
{}
{ $( $offchain:tt )* }
[ $($t:tt)* ]
$(#[doc = $doc_attr:tt])*
fn on_finalise($($param_name:ident : $param:ty),* ) { $( $impl:tt )* }
@@ -207,6 +214,7 @@ macro_rules! decl_module {
{ $( $deposit_event )* }
{ $( $on_initialise )* }
{ fn on_finalise( $( $param_name : $param ),* ) { $( $impl )* } }
{ $( $offchain )* }
[ $($t)* ]
$($rest)*
);
@@ -218,6 +226,7 @@ macro_rules! decl_module {
{ $( $deposit_event:tt )* }
{}
{ $( $on_finalise:tt )* }
{ $( $offchain:tt )* }
[ $($t:tt)* ]
$(#[doc = $doc_attr:tt])*
fn on_initialise($($param_name:ident : $param:ty),* ) { $( $impl:tt )* }
@@ -230,6 +239,32 @@ macro_rules! decl_module {
{ $( $deposit_event )* }
{ fn on_initialise( $( $param_name : $param ),* ) { $( $impl )* } }
{ $( $on_finalise )* }
{ $( $offchain )* }
[ $($t)* ]
$($rest)*
);
};
(@normalize
$(#[$attr:meta])*
pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident>
for enum $call_type:ident where origin: $origin_type:ty, system = $system:ident
{ $( $deposit_event:tt )* }
{ $( $on_initialise:tt )* }
{ $( $on_finalise:tt )* }
{ }
[ $($t:tt)* ]
$(#[doc = $doc_attr:tt])*
fn offchain_worker($($param_name:ident : $param:ty),* ) { $( $impl:tt )* }
$($rest:tt)*
) => {
decl_module!(@normalize
$(#[$attr])*
pub struct $mod_type<$trait_instance: $trait_name>
for enum $call_type where origin: $origin_type, system = $system
{ $( $deposit_event )* }
{ $( $on_initialise )* }
{ $( $on_finalise )* }
{ fn offchain_worker( $( $param_name : $param ),* ) { $( $impl )* } }
[ $($t)* ]
$($rest)*
);
@@ -241,6 +276,7 @@ macro_rules! decl_module {
{ $( $deposit_event:tt )* }
{ $( $on_initialise:tt )* }
{ $( $on_finalise:tt )* }
{ $( $offchain:tt )* }
[ $($t:tt)* ]
$(#[doc = $doc_attr:tt])*
$fn_vis:vis fn $fn_name:ident(
@@ -255,6 +291,7 @@ macro_rules! decl_module {
{ $( $deposit_event )* }
{ $( $on_initialise )* }
{ $( $on_finalise )* }
{ $( $offchain )* }
[
$($t)*
$(#[doc = $doc_attr])*
@@ -273,6 +310,7 @@ macro_rules! decl_module {
{ $( $deposit_event:tt )* }
{ $( $on_initialise:tt )* }
{ $( $on_finalise:tt )* }
{ $( $offchain:tt )* }
[ $($t:tt)* ]
$(#[doc = $doc_attr:tt])*
$fn_vis:vis fn $fn_name:ident(
@@ -293,6 +331,7 @@ macro_rules! decl_module {
{ $( $deposit_event:tt )* }
{ $( $on_initialise:tt )* }
{ $( $on_finalise:tt )* }
{ $( $offchain:tt )* }
[ $($t:tt)* ]
$(#[doc = $doc_attr:tt])*
$fn_vis:vis fn $fn_name:ident(
@@ -313,6 +352,7 @@ macro_rules! decl_module {
{ $( $deposit_event:tt )* }
{ $( $on_initialise:tt )* }
{ $( $on_finalise:tt )* }
{ $( $offchain:tt )* }
[ $($t:tt)* ]
$(#[doc = $doc_attr:tt])*
$fn_vis:vis fn $fn_name:ident(
@@ -327,6 +367,7 @@ macro_rules! decl_module {
{ $( $deposit_event )* }
{ $( $on_initialise )* }
{ $( $on_finalise )* }
{ $( $offchain )* }
[
$($t)*
$(#[doc = $doc_attr])*
@@ -345,6 +386,7 @@ macro_rules! decl_module {
{ $( $deposit_event:tt )* }
{ $( $on_initialise:tt )* }
{ $( $on_finalise:tt )* }
{ $( $offchain:tt )* }
[ $($t:tt)* ]
) => {
decl_module!(@imp
@@ -356,6 +398,7 @@ macro_rules! decl_module {
{ $( $deposit_event )* }
{ $( $on_initialise )* }
{ $( $on_finalise )* }
{ $( $offchain )* }
);
};
@@ -477,6 +520,39 @@ macro_rules! decl_module {
}
};
(@impl_offchain
$module:ident<$trait_instance:ident: $trait_name:ident$(<I>, $instance:ident: $instantiable:path)?>;
fn offchain_worker() { $( $impl:tt )* }
) => {
impl<$trait_instance: $trait_name$(<I>, $instance: $instantiable)?>
$crate::runtime_primitives::traits::OffchainWorker<$trait_instance::BlockNumber>
for $module<$trait_instance$(, $instance)?>
{
fn generate_extrinsics(_block_number_not_used: $trait_instance::BlockNumber) { $( $impl )* }
}
};
(@impl_offchain
$module:ident<$trait_instance:ident: $trait_name:ident$(<I>, $instance:ident: $instantiable:path)?>;
fn offchain_worker($param:ident : $param_ty:ty) { $( $impl:tt )* }
) => {
impl<$trait_instance: $trait_name$(<I>, $instance: $instantiable)?>
$crate::runtime_primitives::traits::OffchainWorker<$trait_instance::BlockNumber>
for $module<$trait_instance$(, $instance)?>
{
fn generate_extrinsics($param: $param_ty) { $( $impl )* }
}
};
(@impl_offchain
$module:ident<$trait_instance:ident: $trait_name:ident$(<I>, $instance:ident: $instantiable:path)?>;
) => {
impl<$trait_instance: $trait_name$(<I>, $instance: $instantiable)?>
$crate::runtime_primitives::traits::OffchainWorker<$trait_instance::BlockNumber>
for $module<$trait_instance$(, $instance)?>
{}
};
(@impl_function
$module:ident<$trait_instance:ident: $trait_name:ident$(<I>, $instance:ident: $instantiable:path)?>;
$origin_ty:ty;
@@ -556,6 +632,7 @@ macro_rules! decl_module {
{ $( $deposit_event:tt )* }
{ $( $on_initialise:tt )* }
{ $( $on_finalise:tt )* }
{ $( $offchain:tt )* }
) => {
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Clone, Copy, PartialEq, Eq)]
@@ -584,6 +661,12 @@ macro_rules! decl_module {
$( $on_finalise )*
}
decl_module! {
@impl_offchain
$mod_type<$trait_instance: $trait_name $(<I>, $instance: $instantiable)?>;
$( $offchain )*
}
decl_module! {
@impl_deposit_event
$mod_type<$trait_instance: $trait_name $(<I>, $instance: $instantiable)?>;
@@ -1086,6 +1169,7 @@ mod tests {
fn on_initialise(n: T::BlockNumber) { if n.into() == 42 { panic!("on_initialise") } }
fn on_finalise(n: T::BlockNumber) { if n.into() == 42 { panic!("on_finalise") } }
fn offchain_worker() {}
}
}