mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 10:01:17 +00:00
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:
@@ -31,6 +31,7 @@
|
||||
//! information on how that is done.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
#![warn(missing_docs)]
|
||||
|
||||
use parity_codec as codec;
|
||||
use codec::{Encode, Decode};
|
||||
@@ -43,6 +44,9 @@ use parking_lot::RwLock;
|
||||
#[cfg(feature = "std")]
|
||||
use std::{sync::Arc, format};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub mod pool;
|
||||
|
||||
pub use runtime_primitives::RuntimeString;
|
||||
|
||||
/// An identifier for an inherent.
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Inherents Pool
|
||||
|
||||
use std::{fmt, mem, vec};
|
||||
use parking_lot::Mutex;
|
||||
|
||||
/// Inherents Pool
|
||||
///
|
||||
/// The pool is responsible to collect inherents asynchronously generated
|
||||
/// by some other parts of the code and make them ready for the next block production.
|
||||
pub struct InherentsPool<T> {
|
||||
data: Mutex<Vec<T>>,
|
||||
}
|
||||
|
||||
impl<T> Default for InherentsPool<T> {
|
||||
fn default() -> Self {
|
||||
InherentsPool {
|
||||
data: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: fmt::Debug> fmt::Debug for InherentsPool<T> {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
let mut builder = fmt.debug_struct("InherentsPool");
|
||||
if let Some(data) = self.data.try_lock() {
|
||||
builder.field("data", &*data);
|
||||
}
|
||||
builder.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> InherentsPool<T> {
|
||||
/// Add inherent extrinsic to the pool.
|
||||
///
|
||||
/// This inherent will be appended to the next produced block.
|
||||
pub fn add(&self, extrinsic: T) {
|
||||
self.data.lock().push(extrinsic);
|
||||
}
|
||||
|
||||
/// Drain all currently queued inherents.
|
||||
pub fn drain(&self) -> Vec<T> {
|
||||
mem::replace(&mut *self.data.lock(), vec![])
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn should_drain_inherents_to_given_data() {
|
||||
let pool = InherentsPool::default();
|
||||
pool.add(5);
|
||||
pool.add(7);
|
||||
|
||||
assert_eq!(pool.drain(), vec![5, 7]);
|
||||
assert_eq!(pool.drain(), vec![]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user