Move Externalities into its own crate (#3775)

* Move `Externalities` into `substrate-externalities`

- `Externalities` now support generic extensions
- Split of `primtives-storage` for storage primitive types

* Move the externalities scoping into `substrate-externalities`

* Fix compilation

* Review feedback

* Adds macro for declaring extensions

* Fix benchmarks

* Introduce `ExtensionStore` trait

* Last review comments

* Implement it for `ExtensionStore`
This commit is contained in:
Bastian Köcher
2019-10-09 15:50:30 +02:00
committed by GitHub
parent 984c6ac839
commit 8a39be474e
95 changed files with 1600 additions and 1420 deletions
+10 -104
View File
@@ -17,8 +17,7 @@
//! Offchain workers types
use codec::{Encode, Decode};
use rstd::prelude::{Vec, Box};
use rstd::convert::TryFrom;
use rstd::{prelude::{Vec, Box}, convert::TryFrom};
pub use crate::crypto::KeyTypeId;
@@ -663,110 +662,17 @@ impl<T: Externalities> Externalities for LimitedExternalities<T> {
}
}
/// An implementation of offchain extensions that should never be triggered.
pub enum NeverOffchainExt {}
impl NeverOffchainExt {
/// Create new offchain extensions.
pub fn new<'a>() -> Option<&'a mut Self> {
None
}
#[cfg(feature = "std")]
externalities::decl_extension! {
/// The offchain extension that will be registered at the Substrate externalities.
pub struct OffchainExt(Box<dyn Externalities>);
}
impl Externalities for NeverOffchainExt {
fn is_validator(&self) -> bool {
unreachable!()
}
fn submit_transaction(&mut self, _extrinsic: Vec<u8>) -> Result<(), ()> {
unreachable!()
}
fn network_state(
&self,
) -> Result<OpaqueNetworkState, ()> {
unreachable!()
}
fn timestamp(&mut self) -> Timestamp {
unreachable!()
}
fn sleep_until(&mut self, _deadline: Timestamp) {
unreachable!()
}
fn random_seed(&mut self) -> [u8; 32] {
unreachable!()
}
fn local_storage_set(&mut self, _kind: StorageKind, _key: &[u8], _value: &[u8]) {
unreachable!()
}
fn local_storage_compare_and_set(
&mut self,
_kind: StorageKind,
_key: &[u8],
_old_value: Option<&[u8]>,
_new_value: &[u8],
) -> bool {
unreachable!()
}
fn local_storage_get(&mut self, _kind: StorageKind, _key: &[u8]) -> Option<Vec<u8>> {
unreachable!()
}
fn http_request_start(
&mut self,
_method: &str,
_uri: &str,
_meta: &[u8]
) -> Result<HttpRequestId, ()> {
unreachable!()
}
fn http_request_add_header(
&mut self,
_request_id: HttpRequestId,
_name: &str,
_value: &str
) -> Result<(), ()> {
unreachable!()
}
fn http_request_write_body(
&mut self,
_request_id: HttpRequestId,
_chunk: &[u8],
_deadline: Option<Timestamp>
) -> Result<(), HttpError> {
unreachable!()
}
fn http_response_wait(
&mut self,
_ids: &[HttpRequestId],
_deadline: Option<Timestamp>
) -> Vec<HttpRequestStatus> {
unreachable!()
}
fn http_response_headers(
&mut self,
_request_id: HttpRequestId
) -> Vec<(Vec<u8>, Vec<u8>)> {
unreachable!()
}
fn http_response_read_body(
&mut self,
_request_id: HttpRequestId,
_buffer: &mut [u8],
_deadline: Option<Timestamp>
) -> Result<usize, HttpError> {
unreachable!()
#[cfg(feature = "std")]
impl OffchainExt {
/// Create a new instance of `Self`.
pub fn new<O: Externalities + 'static>(offchain: O) -> Self {
Self(Box::new(offchain))
}
}