Trait sp_runtime::offchain::Externalities[][src]

pub trait Externalities: Send {
    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>;
pub fn http_response_headers(
        &mut self,
        request_id: HttpRequestId
    ) -> Vec<(Vec<u8, Global>, Vec<u8, Global>), Global>;
pub fn http_response_read_body(
        &mut self,
        request_id: HttpRequestId,
        buffer: &mut [u8],
        deadline: Option<Timestamp>
    ) -> Result<usize, HttpError>;
pub fn set_authorized_nodes(
        &mut self,
        nodes: Vec<OpaquePeerId, Global>,
        authorized_only: bool
    ); }

An extended externalities for offchain workers.

Required methods

pub fn is_validator(&self) -> bool[src]

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.

pub fn network_state(&self) -> Result<OpaqueNetworkState, ()>[src]

Returns information about the local node’s network state.

pub fn timestamp(&mut self) -> Timestamp[src]

Returns current UNIX timestamp (in millis)

pub fn sleep_until(&mut self, deadline: Timestamp)[src]

Pause the execution until deadline is reached.

pub fn random_seed(&mut self) -> [u8; 32][src]

Returns a random seed.

This is a truly random non deterministic seed generated by host environment. Obviously fine in the off-chain worker context.

pub fn local_storage_set(&mut self, kind: StorageKind, key: &[u8], value: &[u8])[src]

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.

pub fn local_storage_clear(&mut self, kind: StorageKind, key: &[u8])[src]

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.

pub fn local_storage_compare_and_set(
    &mut self,
    kind: StorageKind,
    key: &[u8],
    old_value: Option<&[u8]>,
    new_value: &[u8]
) -> bool
[src]

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.

pub fn local_storage_get(
    &mut self,
    kind: StorageKind,
    key: &[u8]
) -> Option<Vec<u8, Global>>
[src]

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.

pub fn http_request_start(
    &mut self,
    method: &str,
    uri: &str,
    meta: &[u8]
) -> Result<HttpRequestId, ()>
[src]

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.

pub fn http_request_add_header(
    &mut self,
    request_id: HttpRequestId,
    name: &str,
    value: &str
) -> Result<(), ()>
[src]

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.

pub fn http_request_write_body(
    &mut self,
    request_id: HttpRequestId,
    chunk: &[u8],
    deadline: Option<Timestamp>
) -> Result<(), HttpError>
[src]

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.

pub fn http_response_wait(
    &mut self,
    ids: &[HttpRequestId],
    deadline: Option<Timestamp>
) -> Vec<HttpRequestStatus, Global>
[src]

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.

pub fn http_response_headers(
    &mut self,
    request_id: HttpRequestId
) -> Vec<(Vec<u8, Global>, Vec<u8, Global>), Global>
[src]

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.

pub fn http_response_read_body(
    &mut self,
    request_id: HttpRequestId,
    buffer: &mut [u8],
    deadline: Option<Timestamp>
) -> Result<usize, HttpError>
[src]

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.

pub fn set_authorized_nodes(
    &mut self,
    nodes: Vec<OpaquePeerId, Global>,
    authorized_only: bool
)
[src]

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 an OpaquePeerId, here it just use plain bytes without any encoding. Invalid OpaquePeerIds 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.
Loading content...

Implementations on Foreign Types

impl<T> Externalities for Box<T, Global> where
    T: Externalities + ?Sized
[src]

Loading content...

Implementors

impl Externalities for TestOffchainExt[src]

impl<T> Externalities for LimitedExternalities<T> where
    T: Externalities
[src]

Loading content...