Refactors the offchain worker api (#3150)

* Update offchain primitives.

* Update offchain worker.

* Update im-online.

* Update service.

* Update node and node-template.

* Update runtime version.

* Fix build.

* Fix offchain worker tests.

* Generalize authority_pubkey.

* Add test.

* Update lib.rs
This commit is contained in:
David Craven
2019-07-22 11:20:57 +02:00
committed by Gavin Wood
parent 2edeef5825
commit a3d19baea3
17 changed files with 509 additions and 327 deletions
+35 -14
View File
@@ -60,9 +60,17 @@ pub mod testing;
pub use offchain_primitives::OffchainWorkerApi;
/// Provides currently configured authority key.
pub trait AuthorityKeyProvider: Clone + 'static {
pub trait AuthorityKeyProvider<Block: traits::Block>: Clone + 'static {
/// The crypto used by the block authoring algorithm.
type ConsensusPair: crypto::Pair;
/// The crypto used by the finality gadget.
type FinalityPair: crypto::Pair;
/// Returns currently configured authority key.
fn authority_key<TPair: crypto::Pair>(&self) -> Option<TPair>;
fn authority_key(&self, block_id: &BlockId<Block>) -> Option<Self::ConsensusPair>;
/// Returns currently configured finality gadget authority key.
fn fg_authority_key(&self, block_id: &BlockId<Block>) -> Option<Self::FinalityPair>;
}
/// An offchain workers manager.
@@ -122,7 +130,7 @@ impl<Client, Storage, KeyProvider, Block> OffchainWorkers<
Block: traits::Block,
Client: ProvideRuntimeApi,
Client::Api: OffchainWorkerApi<Block>,
KeyProvider: AuthorityKeyProvider,
KeyProvider: AuthorityKeyProvider<Block>,
Storage: client::backend::OffchainStorage + 'static,
{
/// Start the offchain workers after given block.
@@ -163,8 +171,7 @@ impl<Client, Storage, KeyProvider, Block> OffchainWorkers<
mod tests {
use super::*;
use futures::Future;
use primitives::{ed25519, sr25519, crypto::{TypedKey, Pair}};
use std::collections::HashSet;
use primitives::{ed25519, sr25519};
use network::{Multiaddr, PeerId};
struct MockNetworkStateInfo();
@@ -179,19 +186,33 @@ mod tests {
}
}
#[derive(Clone, Default)]
pub(crate) struct TestProvider {
#[derive(Clone)]
pub(crate) struct TestProvider<Block> {
_marker: PhantomData<Block>,
pub(crate) sr_key: Option<sr25519::Pair>,
pub(crate) ed_key: Option<ed25519::Pair>,
}
impl AuthorityKeyProvider for TestProvider {
fn authority_key<TPair: crypto::Pair>(&self) -> Option<TPair> {
TPair::from_seed_slice(&match TPair::KEY_TYPE {
sr25519::Pair::KEY_TYPE => self.sr_key.as_ref().map(|key| key.to_raw_vec()),
ed25519::Pair::KEY_TYPE => self.ed_key.as_ref().map(|key| key.to_raw_vec()),
_ => None,
}?).ok()
impl<Block: traits::Block> Default for TestProvider<Block> {
fn default() -> Self {
Self {
_marker: PhantomData,
sr_key: None,
ed_key: None,
}
}
}
impl<Block: traits::Block> AuthorityKeyProvider<Block> for TestProvider<Block> {
type ConsensusPair = ed25519::Pair;
type FinalityPair = sr25519::Pair;
fn authority_key(&self, _: &BlockId<Block>) -> Option<Self::ConsensusPair> {
self.ed_key.clone()
}
fn fg_authority_key(&self, _: &BlockId<Block>) -> Option<Self::FinalityPair> {
self.sr_key.clone()
}
}