Offchain execution extensions (#4145)

* Pass Extensions instead of individual objects.

* Move TransactionPool to a separate ExternalitiesExtension.

* Fix compilation.?

* Clean up.

* Refactor testing utilities.

* Add docs, fix tests.

* Fix doctest.

* Fix formatting and add some logs.

* Add some docs.

* Remove unused files.
This commit is contained in:
Tomasz Drwięga
2019-11-22 17:10:23 +01:00
committed by Gavin Wood
parent f000392cc0
commit 86b6ac5571
39 changed files with 554 additions and 360 deletions
+13 -77
View File
@@ -21,9 +21,9 @@ use std::{
thread::sleep,
};
use client_api::OffchainStorage;
use futures::{StreamExt as _, Future, FutureExt as _, future, channel::mpsc};
use log::{info, debug, warn, error};
use primitives::offchain::OffchainStorage;
use futures::Future;
use log::error;
use network::{PeerId, Multiaddr, NetworkStateInfo};
use codec::{Encode, Decode};
use primitives::offchain::{
@@ -31,8 +31,6 @@ use primitives::offchain::{
OpaqueNetworkState, OpaquePeerId, OpaqueMultiaddr, StorageKind,
};
pub use offchain_primitives::STORAGE_PREFIX;
use sr_primitives::{generic::BlockId, traits::{self, Extrinsic}};
use transaction_pool::txpool::{Pool, ChainApi};
#[cfg(not(target_os = "unknown"))]
mod http;
@@ -44,19 +42,14 @@ mod http_dummy;
mod timestamp;
/// A message between the offchain extension and the processing thread.
enum ExtMessage {
SubmitExtrinsic(Vec<u8>),
}
/// Asynchronous offchain API.
///
/// NOTE this is done to prevent recursive calls into the runtime (which are not supported currently).
pub(crate) struct Api<Storage, Block: traits::Block> {
sender: mpsc::UnboundedSender<ExtMessage>,
pub(crate) struct Api<Storage> {
/// Offchain Workers database.
db: Storage,
/// A NetworkState provider.
network_state: Arc<dyn NetworkStateInfo + Send + Sync>,
_at: BlockId<Block>,
/// Is this node a potential validator?
is_validator: bool,
/// Everything HTTP-related is handled by a different struct.
@@ -73,22 +66,11 @@ fn unavailable_yet<R: Default>(name: &str) -> R {
const LOCAL_DB: &str = "LOCAL (fork-aware) DB";
impl<Storage, Block> OffchainExt for Api<Storage, Block>
where
Storage: OffchainStorage,
Block: traits::Block,
{
impl<Storage: OffchainStorage> OffchainExt for Api<Storage> {
fn is_validator(&self) -> bool {
self.is_validator
}
fn submit_transaction(&mut self, ext: Vec<u8>) -> Result<(), ()> {
self.sender
.unbounded_send(ExtMessage::SubmitExtrinsic(ext))
.map(|_| ())
.map_err(|_| ())
}
fn network_state(&self) -> Result<OpaqueNetworkState, ()> {
let external_addresses = self.network_state.external_addresses();
@@ -260,40 +242,28 @@ impl TryFrom<OpaqueNetworkState> for NetworkState {
/// Offchain extensions implementation API
///
/// This is the asynchronous processing part of the API.
pub(crate) struct AsyncApi<A: ChainApi> {
receiver: Option<mpsc::UnboundedReceiver<ExtMessage>>,
transaction_pool: Arc<Pool<A>>,
at: BlockId<A::Block>,
pub(crate) struct AsyncApi {
/// Everything HTTP-related is handled by a different struct.
http: Option<http::HttpWorker>,
}
impl<A: ChainApi> AsyncApi<A> {
impl AsyncApi {
/// Creates new Offchain extensions API implementation an the asynchronous processing part.
pub fn new<S: OffchainStorage>(
transaction_pool: Arc<Pool<A>>,
db: S,
at: BlockId<A::Block>,
network_state: Arc<dyn NetworkStateInfo + Send + Sync>,
is_validator: bool,
) -> (Api<S, A::Block>, AsyncApi<A>) {
let (sender, rx) = mpsc::unbounded();
) -> (Api<S>, AsyncApi) {
let (http_api, http_worker) = http::http();
let api = Api {
sender,
db,
network_state,
_at: at,
is_validator,
http: http_api,
};
let async_api = AsyncApi {
receiver: Some(rx),
transaction_pool,
at,
http: Some(http_worker),
};
@@ -302,35 +272,9 @@ impl<A: ChainApi> AsyncApi<A> {
/// Run a processing task for the API
pub fn process(mut self) -> impl Future<Output = ()> {
let receiver = self.receiver.take().expect("Take invoked only once.");
let http = self.http.take().expect("Take invoked only once.");
let extrinsics = receiver.for_each(move |msg| {
match msg {
ExtMessage::SubmitExtrinsic(ext) => self.submit_extrinsic(ext),
}
});
future::join(extrinsics, http)
.map(|((), ())| ())
}
fn submit_extrinsic(&mut self, ext: Vec<u8>) -> impl Future<Output = ()> {
let xt = match <A::Block as traits::Block>::Extrinsic::decode(&mut &*ext) {
Ok(xt) => xt,
Err(e) => {
warn!("Unable to decode extrinsic: {:?}: {}", ext, e.what());
return future::Either::Left(future::ready(()))
},
};
info!("Submitting transaction to the pool: {:?} (isSigned: {:?})", xt, xt.is_signed());
future::Either::Right(self.transaction_pool
.submit_one(&self.at, xt.clone())
.map(|result| match result {
Ok(hash) => { debug!("[{:?}] Offchain transaction added to the pool.", hash); },
Err(e) => { warn!("Couldn't submit offchain transaction: {:?}", e); },
}))
http
}
}
@@ -338,10 +282,8 @@ impl<A: ChainApi> AsyncApi<A> {
mod tests {
use super::*;
use std::{convert::{TryFrom, TryInto}, time::SystemTime};
use sr_primitives::traits::Zero;
use client_db::offchain::LocalStorage;
use network::PeerId;
use test_client::runtime::Block;
struct MockNetworkStateInfo();
@@ -355,19 +297,13 @@ mod tests {
}
}
fn offchain_api() -> (Api<LocalStorage, Block>, AsyncApi<impl ChainApi>) {
fn offchain_api() -> (Api<LocalStorage>, AsyncApi) {
let _ = env_logger::try_init();
let db = LocalStorage::new_test();
let client = Arc::new(test_client::new());
let pool = Arc::new(
Pool::new(Default::default(), transaction_pool::FullChainApi::new(client.clone()))
);
let mock = Arc::new(MockNetworkStateInfo());
AsyncApi::new(
pool,
db,
BlockId::Number(Zero::zero()),
mock,
false,
)
+12 -12
View File
@@ -41,15 +41,11 @@ use sr_api::ApiExt;
use futures::future::Future;
use log::{debug, warn};
use network::NetworkStateInfo;
use primitives::{offchain, ExecutionContext};
use primitives::{offchain::{self, OffchainStorage}, ExecutionContext};
use sr_primitives::{generic::BlockId, traits::{self, ProvideRuntimeApi}};
use transaction_pool::txpool::{Pool, ChainApi};
use client_api::{OffchainStorage};
mod api;
pub mod testing;
pub use offchain_primitives::{OffchainWorkerApi, STORAGE_PREFIX};
/// An offchain workers manager.
@@ -94,13 +90,12 @@ impl<Client, Storage, Block> OffchainWorkers<
{
/// Start the offchain workers after given block.
#[must_use]
pub fn on_block_imported<A>(
pub fn on_block_imported(
&self,
number: &<Block::Header as traits::Header>::Number,
pool: &Arc<Pool<A>>,
network_state: Arc<dyn NetworkStateInfo + Send + Sync>,
is_validator: bool,
) -> impl Future<Output = ()> where A: ChainApi<Block=Block> + 'static {
) -> impl Future<Output = ()> {
let runtime = self.client.runtime_api();
let at = BlockId::number(*number);
let has_api = runtime.has_api::<dyn OffchainWorkerApi<Block, Error = ()>>(&at);
@@ -108,9 +103,7 @@ impl<Client, Storage, Block> OffchainWorkers<
if has_api.unwrap_or(false) {
let (api, runner) = api::AsyncApi::new(
pool.clone(),
self.db.clone(),
at.clone(),
network_state.clone(),
is_validator,
);
@@ -153,6 +146,8 @@ impl<Client, Storage, Block> OffchainWorkers<
mod tests {
use super::*;
use network::{Multiaddr, PeerId};
use std::sync::Arc;
use transaction_pool::txpool::Pool;
struct MockNetworkStateInfo();
@@ -171,13 +166,18 @@ mod tests {
// given
let _ = env_logger::try_init();
let client = Arc::new(test_client::new());
let pool = Arc::new(Pool::new(Default::default(), transaction_pool::FullChainApi::new(client.clone())));
let pool = Arc::new(Pool::new(
Default::default(),
transaction_pool::FullChainApi::new(client.clone())
));
client.execution_extensions()
.register_transaction_pool(Arc::downgrade(&pool.clone()) as _);
let db = client_db::offchain::LocalStorage::new_test();
let network_state = Arc::new(MockNetworkStateInfo());
// when
let offchain = OffchainWorkers::new(client, db);
futures::executor::block_on(offchain.on_block_imported(&0u64, &pool, network_state, false));
futures::executor::block_on(offchain.on_block_imported(&0u64, network_state, false));
// then
assert_eq!(pool.status().ready, 1);
-307
View File
@@ -1,307 +0,0 @@
// 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/>.
//! Offchain Externalities implementation for tests.
use std::{
collections::BTreeMap,
sync::Arc,
};
use client_api::{OffchainStorage, InMemOffchainStorage};
use parking_lot::RwLock;
use primitives::offchain::{
self,
HttpError,
HttpRequestId as RequestId,
HttpRequestStatus as RequestStatus,
Timestamp,
StorageKind,
OpaqueNetworkState,
};
/// Pending request.
#[derive(Debug, Default, PartialEq, Eq)]
pub struct PendingRequest {
/// HTTP method
pub method: String,
/// URI
pub uri: String,
/// Encoded Metadata
pub meta: Vec<u8>,
/// Request headers
pub headers: Vec<(String, String)>,
/// Request body
pub body: Vec<u8>,
/// Has the request been sent already.
pub sent: bool,
/// Response body
pub response: Option<Vec<u8>>,
/// Number of bytes already read from the response body.
pub read: usize,
/// Response headers
pub response_headers: Vec<(String, String)>,
}
/// Internal state of the externalities.
///
/// This can be used in tests to respond or assert stuff about interactions.
#[derive(Debug, Default)]
pub struct State {
/// A list of pending requests.
pub requests: BTreeMap<RequestId, PendingRequest>,
expected_requests: BTreeMap<RequestId, PendingRequest>,
/// Persistent local storage
pub persistent_storage: InMemOffchainStorage,
/// Local storage
pub local_storage: InMemOffchainStorage,
/// A vector of transactions submitted from the runtime.
pub transactions: Vec<Vec<u8>>,
}
impl State {
/// Asserts that pending request has been submitted and fills it's response.
pub fn fulfill_pending_request(
&mut self,
id: u16,
expected: PendingRequest,
response: impl Into<Vec<u8>>,
response_headers: impl IntoIterator<Item=(String, String)>,
) {
match self.requests.get_mut(&RequestId(id)) {
None => {
panic!("Missing pending request: {:?}.\n\nAll: {:?}", id, self.requests);
}
Some(req) => {
assert_eq!(
*req,
expected,
);
req.response = Some(response.into());
req.response_headers = response_headers.into_iter().collect();
}
}
}
fn fulfill_expected(&mut self, id: u16) {
if let Some(mut req) = self.expected_requests.remove(&RequestId(id)) {
let response = req.response.take().expect("Response checked while added.");
let headers = std::mem::replace(&mut req.response_headers, vec![]);
self.fulfill_pending_request(id, req, response, headers);
}
}
/// Add expected HTTP request.
///
/// This method can be used to initialize expected HTTP requests and their responses
/// before running the actual code that utilizes them (for instance before calling into runtime).
/// Expected request has to be fulfilled before this struct is dropped,
/// the `response` and `response_headers` fields will be used to return results to the callers.
pub fn expect_request(&mut self, id: u16, expected: PendingRequest) {
if expected.response.is_none() {
panic!("Expected request needs to have a response.");
}
self.expected_requests.insert(RequestId(id), expected);
}
}
impl Drop for State {
fn drop(&mut self) {
// If we panic! while we are already in a panic, the test dies with an illegal instruction.
if !self.expected_requests.is_empty() && !std::thread::panicking() {
panic!("Unfulfilled expected requests: {:?}", self.expected_requests);
}
}
}
/// Implementation of offchain externalities used for tests.
#[derive(Clone, Default, Debug)]
pub struct TestOffchainExt(pub Arc<RwLock<State>>);
impl TestOffchainExt {
/// Create new `TestOffchainExt` and a reference to the internal state.
pub fn new() -> (Self, Arc<RwLock<State>>) {
let ext = Self::default();
let state = ext.0.clone();
(ext, state)
}
}
impl offchain::Externalities for TestOffchainExt {
fn is_validator(&self) -> bool {
unimplemented!("not needed in tests so far")
}
fn submit_transaction(&mut self, ex: Vec<u8>) -> Result<(), ()> {
let mut state = self.0.write();
state.transactions.push(ex);
Ok(())
}
fn network_state(&self) -> Result<OpaqueNetworkState, ()> {
Ok(OpaqueNetworkState {
peer_id: Default::default(),
external_addresses: vec![],
})
}
fn timestamp(&mut self) -> Timestamp {
unimplemented!("not needed in tests so far")
}
fn sleep_until(&mut self, _deadline: Timestamp) {
unimplemented!("not needed in tests so far")
}
fn random_seed(&mut self) -> [u8; 32] {
unimplemented!("not needed in tests so far")
}
fn local_storage_set(&mut self, kind: StorageKind, key: &[u8], value: &[u8]) {
let mut state = self.0.write();
match kind {
StorageKind::LOCAL => &mut state.local_storage,
StorageKind::PERSISTENT => &mut state.persistent_storage,
}.set(b"", key, value);
}
fn local_storage_compare_and_set(
&mut self,
kind: StorageKind,
key: &[u8],
old_value: Option<&[u8]>,
new_value: &[u8]
) -> bool {
let mut state = self.0.write();
match kind {
StorageKind::LOCAL => &mut state.local_storage,
StorageKind::PERSISTENT => &mut state.persistent_storage,
}.compare_and_set(b"", key, old_value, new_value)
}
fn local_storage_get(&mut self, kind: StorageKind, key: &[u8]) -> Option<Vec<u8>> {
let state = self.0.read();
match kind {
StorageKind::LOCAL => &state.local_storage,
StorageKind::PERSISTENT => &state.persistent_storage,
}.get(b"", key)
}
fn http_request_start(&mut self, method: &str, uri: &str, meta: &[u8]) -> Result<RequestId, ()> {
let mut state = self.0.write();
let id = RequestId(state.requests.len() as u16);
state.requests.insert(id.clone(), PendingRequest {
method: method.into(),
uri: uri.into(),
meta: meta.into(),
..Default::default()
});
Ok(id)
}
fn http_request_add_header(
&mut self,
request_id: RequestId,
name: &str,
value: &str,
) -> Result<(), ()> {
let mut state = self.0.write();
if let Some(req) = state.requests.get_mut(&request_id) {
req.headers.push((name.into(), value.into()));
Ok(())
} else {
Err(())
}
}
fn http_request_write_body(
&mut self,
request_id: RequestId,
chunk: &[u8],
_deadline: Option<Timestamp>
) -> Result<(), HttpError> {
let mut state = self.0.write();
let sent = {
let req = state.requests.get_mut(&request_id).ok_or(HttpError::IoError)?;
req.body.extend(chunk);
if chunk.is_empty() {
req.sent = true;
}
req.sent
};
if sent {
state.fulfill_expected(request_id.0);
}
Ok(())
}
fn http_response_wait(
&mut self,
ids: &[RequestId],
_deadline: Option<Timestamp>,
) -> Vec<RequestStatus> {
let state = self.0.read();
ids.iter().map(|id| match state.requests.get(id) {
Some(req) if req.response.is_none() =>
panic!("No `response` provided for request with id: {:?}", id),
None => RequestStatus::Invalid,
_ => RequestStatus::Finished(200),
}).collect()
}
fn http_response_headers(&mut self, request_id: RequestId) -> Vec<(Vec<u8>, Vec<u8>)> {
let state = self.0.read();
if let Some(req) = state.requests.get(&request_id) {
req.response_headers
.clone()
.into_iter()
.map(|(k, v)| (k.into_bytes(), v.into_bytes()))
.collect()
} else {
Default::default()
}
}
fn http_response_read_body(
&mut self,
request_id: RequestId,
buffer: &mut [u8],
_deadline: Option<Timestamp>
) -> Result<usize, HttpError> {
let mut state = self.0.write();
if let Some(req) = state.requests.get_mut(&request_id) {
let response = req.response
.as_mut()
.expect(&format!("No response provided for request: {:?}", request_id));
if req.read >= response.len() {
// Remove the pending request as per spec.
state.requests.remove(&request_id);
Ok(0)
} else {
let read = std::cmp::min(buffer.len(), response[req.read..].len());
buffer[0..read].copy_from_slice(&response[req.read..read]);
req.read += read;
Ok(read)
}
} else {
Err(HttpError::IoError)
}
}
}