Trait sp_runtime::offchain::Externalities
source · pub trait Externalities: Send {
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 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> ⓘ;
fn http_response_headers(
&mut self,
request_id: HttpRequestId
) -> Vec<(Vec<u8, Global>, Vec<u8, Global>), Global> ⓘ;
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§
sourcefn 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.
sourcefn network_state(&self) -> Result<OpaqueNetworkState, ()>
fn network_state(&self) -> Result<OpaqueNetworkState, ()>
Returns information about the local node’s network state.
sourcefn sleep_until(&mut self, deadline: Timestamp)
fn sleep_until(&mut self, deadline: Timestamp)
Pause the execution until deadline
is reached.
sourcefn 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.
sourcefn 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.
sourcefn 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.
sourcefn http_request_write_body(
&mut self,
request_id: HttpRequestId,
chunk: &[u8],
deadline: Option<Timestamp>
) -> Result<(), HttpError>
fn http_request_write_body(
&mut self,
request_id: HttpRequestId,
chunk: &[u8],
deadline: Option<Timestamp>
) -> Result<(), HttpError>
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.
sourcefn http_response_wait(
&mut self,
ids: &[HttpRequestId],
deadline: Option<Timestamp>
) -> Vec<HttpRequestStatus, Global> ⓘ
fn http_response_wait(
&mut self,
ids: &[HttpRequestId],
deadline: Option<Timestamp>
) -> Vec<HttpRequestStatus, Global> ⓘ
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.
sourcefn http_response_headers(
&mut self,
request_id: HttpRequestId
) -> Vec<(Vec<u8, Global>, Vec<u8, Global>), Global> ⓘ
fn http_response_headers(
&mut self,
request_id: HttpRequestId
) -> Vec<(Vec<u8, Global>, Vec<u8, Global>), Global> ⓘ
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.
sourcefn http_response_read_body(
&mut self,
request_id: HttpRequestId,
buffer: &mut [u8],
deadline: Option<Timestamp>
) -> Result<usize, HttpError>
fn http_response_read_body(
&mut self,
request_id: HttpRequestId,
buffer: &mut [u8],
deadline: Option<Timestamp>
) -> Result<usize, HttpError>
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.