Offchain-worker APIs stubs (#2615)

* WiP: HTTP Apis.

* Working on the API.

* Add docs, clean up the API.

* Expose ext_ stuff as well.

* Implement HTTP helpers for offchain sr-io.

* Remove HTTP stuff.

* Revert "Remove HTTP stuff."

This reverts commit 7cca029d6ae93c5849b50edfcc6d2c313ba3e5bf.

* HTTP apis.

* Additional offchain methods.

* Make it compile.

* Implement wasm-ext boundary of offchain methods.

* Add stubs for offchain stuff to prevent panics.

* Fix tests.

* Addres some more issues.

* Introduce typedef, use unsafe from_utf8

* Bump runtime version.

* Introduce error to distinguish deadline and io errors.

* Add local_storage_cas

* Some tests for offchain stuff.

* Address more grumbles.

* Fix tests compilation.

* Fix borked merge.

* Improve docs for expected return values from ext functions.

* Adding new sign/enrypt/decrypt APIs.
This commit is contained in:
Tomasz Drwięga
2019-05-31 09:33:44 +02:00
committed by Gavin Wood
parent 80b18c8531
commit 308ab4f269
26 changed files with 2648 additions and 120 deletions
+121 -4
View File
@@ -16,9 +16,13 @@
use std::sync::Arc;
use futures::{Stream, Future, sync::mpsc};
use log::{info, debug, warn};
use log::{info, debug, warn, error};
use parity_codec::Decode;
use primitives::OffchainExt;
use primitives::offchain::{
Timestamp, HttpRequestId, HttpRequestStatus, HttpError,
Externalities as OffchainExt,
CryptoKind, CryptoKeyId,
};
use runtime_primitives::{
generic::BlockId,
traits::{self, Extrinsic},
@@ -35,9 +39,122 @@ enum ExtMessage {
/// NOTE this is done to prevent recursive calls into the runtime (which are not supported currently).
pub(crate) struct AsyncApi(mpsc::UnboundedSender<ExtMessage>);
fn unavailable_yet<R: Default>(name: &str) -> R {
error!("This {:?} API is not available for offchain workers yet. Follow
https://github.com/paritytech/substrate/issues/1458 for details", name);
Default::default()
}
impl OffchainExt for AsyncApi {
fn submit_extrinsic(&mut self, ext: Vec<u8>) {
let _ = self.0.unbounded_send(ExtMessage::SubmitExtrinsic(ext));
fn submit_transaction(&mut self, ext: Vec<u8>) -> Result<(), ()> {
self.0.unbounded_send(ExtMessage::SubmitExtrinsic(ext))
.map(|_| ())
.map_err(|_| ())
}
fn new_crypto_key(&mut self, _crypto: CryptoKind) -> Result<CryptoKeyId, ()> {
unavailable_yet::<()>("new_crypto_key");
Err(())
}
fn encrypt(&mut self, _key: Option<CryptoKeyId>, _data: &[u8]) -> Result<Vec<u8>, ()> {
unavailable_yet::<()>("encrypt");
Err(())
}
fn decrypt(&mut self, _key: Option<CryptoKeyId>, _data: &[u8]) -> Result<Vec<u8>, ()> {
unavailable_yet::<()>("decrypt");
Err(())
}
fn sign(&mut self, _key: Option<CryptoKeyId>, _data: &[u8]) -> Result<Vec<u8>, ()> {
unavailable_yet::<()>("sign");
Err(())
}
fn verify(&mut self, _key: Option<CryptoKeyId>, _msg: &[u8], _signature: &[u8]) -> Result<bool, ()> {
unavailable_yet::<()>("verify");
Err(())
}
fn timestamp(&mut self) -> Timestamp {
unavailable_yet("timestamp")
}
fn sleep_until(&mut self, _deadline: Timestamp) {
unavailable_yet::<()>("sleep_until")
}
fn random_seed(&mut self) -> [u8; 32] {
unavailable_yet("random_seed")
}
fn local_storage_set(&mut self, _key: &[u8], _value: &[u8]) {
unavailable_yet("local_storage_set")
}
fn local_storage_compare_and_set(&mut self, _key: &[u8], _old_value: &[u8], _new_value: &[u8]) {
unavailable_yet("local_storage_compare_and_set")
}
fn local_storage_get(&mut self, _key: &[u8]) -> Option<Vec<u8>> {
unavailable_yet("local_storage_get")
}
fn http_request_start(
&mut self,
_method: &str,
_uri: &str,
_meta: &[u8]
) -> Result<HttpRequestId, ()> {
unavailable_yet::<()>("http_request_start");
Err(())
}
fn http_request_add_header(
&mut self,
_request_id: HttpRequestId,
_name: &str,
_value: &str
) -> Result<(), ()> {
unavailable_yet::<()>("http_request_add_header");
Err(())
}
fn http_request_write_body(
&mut self,
_request_id: HttpRequestId,
_chunk: &[u8],
_deadline: Option<Timestamp>
) -> Result<(), HttpError> {
unavailable_yet::<()>("http_request_write_body");
Err(HttpError::IoError)
}
fn http_response_wait(
&mut self,
ids: &[HttpRequestId],
_deadline: Option<Timestamp>
) -> Vec<HttpRequestStatus> {
unavailable_yet::<()>("http_response_wait");
ids.iter().map(|_| HttpRequestStatus::Unknown).collect()
}
fn http_response_headers(
&mut self,
_request_id: HttpRequestId
) -> Vec<(Vec<u8>, Vec<u8>)> {
unavailable_yet("http_response_headers")
}
fn http_response_read_body(
&mut self,
_request_id: HttpRequestId,
_buffer: &mut [u8],
_deadline: Option<Timestamp>
) -> Result<usize, HttpError> {
unavailable_yet::<()>("http_response_read_body");
Err(HttpError::IoError)
}
}