Trait sp_runtime::offchain::Externalities
source · [−]pub trait Externalities: Send {
Show 16 methods
fn is_validator(&self) -> bool;
fn network_state(&self) -> Result<OpaqueNetworkState, ()>;
fn timestamp(&mut self) -> Timestamp;
fn sleep_until(&mut self, deadline: Timestamp);
fn random_seed(&mut self) -> [u8; 32];
fn local_storage_set(&mut self, kind: StorageKind, key: &[u8], value: &[u8]);
fn local_storage_clear(&mut self, kind: StorageKind, key: &[u8]);
fn local_storage_compare_and_set(
&mut self,
kind: StorageKind,
key: &[u8],
old_value: Option<&[u8]>,
new_value: &[u8]
) -> bool;
fn local_storage_get(
&mut self,
kind: StorageKind,
key: &[u8]
) -> Option<Vec<u8, Global>>;
fn http_request_start(
&mut self,
method: &str,
uri: &str,
meta: &[u8]
) -> Result<HttpRequestId, ()>;
fn http_request_add_header(
&mut self,
request_id: HttpRequestId,
name: &str,
value: &str
) -> Result<(), ()>;
fn http_request_write_body(
&mut self,
request_id: HttpRequestId,
chunk: &[u8],
deadline: Option<Timestamp>
) -> Result<(), HttpError>;
fn http_response_wait(
&mut self,
ids: &[HttpRequestId],
deadline: Option<Timestamp>
) -> Vec<HttpRequestStatus, Global>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
;
fn http_response_headers(
&mut self,
request_id: HttpRequestId
) -> Vec<(Vec<u8, Global>, Vec<u8, Global>), Global>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
;
fn http_response_read_body(
&mut self,
request_id: HttpRequestId,
buffer: &mut [u8],
deadline: Option<Timestamp>
) -> Result<usize, HttpError>;
fn set_authorized_nodes(
&mut self,
nodes: Vec<OpaquePeerId, Global>,
authorized_only: bool
);
}
Expand description
An extended externalities for offchain workers.
Required methods
fn is_validator(&self) -> bool
fn is_validator(&self) -> bool
Returns if the local node is a potential validator.
Even if this function returns true
, it does not mean that any keys are configured
and that the validator is registered in the chain.
fn network_state(&self) -> Result<OpaqueNetworkState, ()>
fn network_state(&self) -> Result<OpaqueNetworkState, ()>
Returns information about the local node’s network state.
fn sleep_until(&mut self, deadline: Timestamp)
fn sleep_until(&mut self, deadline: Timestamp)
Pause the execution until deadline
is reached.
fn random_seed(&mut self) -> [u8; 32]
fn random_seed(&mut self) -> [u8; 32]
Returns a random seed.
This is a truly random non deterministic seed generated by host environment. Obviously fine in the off-chain worker context.
fn local_storage_set(&mut self, kind: StorageKind, key: &[u8], value: &[u8])
fn local_storage_set(&mut self, kind: StorageKind, key: &[u8], value: &[u8])
Sets a value in the local storage.
Note this storage is not part of the consensus, it’s only accessible by offchain worker tasks running on the same machine. It is persisted between runs.
fn local_storage_clear(&mut self, kind: StorageKind, key: &[u8])
fn local_storage_clear(&mut self, kind: StorageKind, key: &[u8])
Removes a value in the local storage.
Note this storage is not part of the consensus, it’s only accessible by offchain worker tasks running on the same machine. It is persisted between runs.
Sets a value in the local storage if it matches current value.
Since multiple offchain workers may be running concurrently, to prevent data races use CAS to coordinate between them.
Returns true
if the value has been set, false
otherwise.
Note this storage is not part of the consensus, it’s only accessible by offchain worker tasks running on the same machine. It is persisted between runs.
fn local_storage_get(
&mut self,
kind: StorageKind,
key: &[u8]
) -> Option<Vec<u8, Global>>
fn local_storage_get(
&mut self,
kind: StorageKind,
key: &[u8]
) -> Option<Vec<u8, Global>>
Gets a value from the local storage.
If the value does not exist in the storage None
will be returned.
Note this storage is not part of the consensus, it’s only accessible by
offchain worker tasks running on the same machine. It is persisted between runs.
fn http_request_start(
&mut self,
method: &str,
uri: &str,
meta: &[u8]
) -> Result<HttpRequestId, ()>
fn http_request_start(
&mut self,
method: &str,
uri: &str,
meta: &[u8]
) -> Result<HttpRequestId, ()>
Initiates a http request given HTTP verb and the URL.
Meta is a future-reserved field containing additional, parity-scale-codec encoded parameters. Returns the id of newly started request.
Returns an error if:
- No new request identifier could be allocated.
- The method or URI contain invalid characters.
fn http_request_add_header(
&mut self,
request_id: HttpRequestId,
name: &str,
value: &str
) -> Result<(), ()>
fn http_request_add_header(
&mut self,
request_id: HttpRequestId,
name: &str,
value: &str
) -> Result<(), ()>
Append header to the request.
Calling this function multiple times with the same header name continues appending new headers. In other words, headers are never replaced.
Returns an error if:
- The request identifier is invalid.
- You have called
http_request_write_body
on that request. - The name or value contain invalid characters.
An error doesn’t poison the request, and you can continue as if the call had never been made.
Write a chunk of request body.
Calling this function with a non-empty slice may or may not start the
HTTP request. Calling this function with an empty chunks finalizes the
request and always starts it. It is no longer valid to write more data
afterwards.
Passing None
as deadline blocks forever.
Returns an error if:
- The request identifier is invalid.
http_response_wait
has already been called on this request.- The deadline is reached.
- An I/O error has happened, for example the remote has closed our request. The request is then considered invalid.
Block and wait for the responses for given requests.
Returns a vector of request statuses (the len is the same as ids).
Note that if deadline is not provided the method will block indefinitely,
otherwise unready responses will produce DeadlineReached
status.
If a response returns an IoError
, it is then considered destroyed.
Its id is then invalid.
Passing None
as deadline blocks forever.
Read all response headers.
Returns a vector of pairs (HeaderKey, HeaderValue)
.
Dispatches the request if it hasn’t been done yet. It is no longer valid to modify the headers or write data to the request.
Returns an empty list if the identifier is unknown/invalid, hasn’t received a response, or has finished.
Read a chunk of body response to given buffer.
Dispatches the request if it hasn’t been done yet. It is no longer valid to modify the headers or write data to the request.
Returns the number of bytes written or an error in case a deadline
is reached or server closed the connection.
Passing None
as a deadline blocks forever.
If Ok(0)
or Err(IoError)
is returned, the request is considered
destroyed. Doing another read or getting the response’s headers, for
example, is then invalid.
Returns an error if:
- The request identifier is invalid.
- The deadline is reached.
- An I/O error has happened, for example the remote has closed our request. The request is then considered invalid.
Set the authorized nodes from runtime.
In a permissioned network, the connections between nodes need to reach a consensus between participants.
nodes
: a set of nodes which are allowed to connect for the local node. each one is identified with anOpaquePeerId
, here it just use plain bytes without any encoding. InvalidOpaquePeerId
s are silently ignored.authorized_only
: if true, only the authorized nodes are allowed to connect, otherwise unauthorized nodes can also be connected through other mechanism.
Implementations on Foreign Types
sourceimpl<T> Externalities for Box<T, Global> where
T: Externalities + ?Sized,
impl<T> Externalities for Box<T, Global> where
T: Externalities + ?Sized,
pub fn is_validator(&self) -> bool
pub fn network_state(&self) -> Result<OpaqueNetworkState, ()>
pub fn timestamp(&mut self) -> Timestamp
pub fn sleep_until(&mut self, deadline: Timestamp)
pub fn random_seed(&mut self) -> [u8; 32]
pub fn local_storage_set(&mut self, kind: StorageKind, key: &[u8], value: &[u8])
pub fn local_storage_clear(&mut self, kind: StorageKind, key: &[u8])
pub fn local_storage_compare_and_set(
&mut self,
kind: StorageKind,
key: &[u8],
old_value: Option<&[u8]>,
new_value: &[u8]
) -> bool
pub fn local_storage_get(
&mut self,
kind: StorageKind,
key: &[u8]
) -> Option<Vec<u8, Global>>
pub fn http_request_start(
&mut self,
method: &str,
uri: &str,
meta: &[u8]
) -> Result<HttpRequestId, ()>
pub fn http_request_add_header(
&mut self,
request_id: HttpRequestId,
name: &str,
value: &str
) -> Result<(), ()>
pub fn http_request_write_body(
&mut self,
request_id: HttpRequestId,
chunk: &[u8],
deadline: Option<Timestamp>
) -> Result<(), HttpError>
pub fn http_response_wait(
&mut self,
ids: &[HttpRequestId],
deadline: Option<Timestamp>
) -> Vec<HttpRequestStatus, Global>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
A: Allocator,
pub fn http_response_headers(
&mut self,
request_id: HttpRequestId
) -> Vec<(Vec<u8, Global>, Vec<u8, Global>), Global>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
A: Allocator,