[][src]Struct exonum_rust_runtime::Broadcaster

pub struct Broadcaster { /* fields omitted */ }

Transaction broadcaster.

Transaction broadcast allows a service to create transactions in the after_commit handler or the HTTP API handlers and broadcast them to the connected Exonum nodes. The transactions are addressed to the executing service instance and are signed by the service keypair of the node.

Broadcasting functionality is primarily useful for services that receive information from outside the blockchain and need to translate it to transactions. As an example, a time oracle service may broadcast local node time and build the blockchain-wide time by processing corresponding transactions.

Examples

Using Broadcaster in service logic:

use exonum::runtime::{ExecutionContext, ExecutionError};
use exonum_rust_runtime::{AfterCommitContext, Service};

#[exonum_interface]
trait MyInterface<Ctx> {
    type Output;
    #[interface_method(id = 0)]
    fn publish_string(&self, ctx: Ctx, value: String) -> Self::Output;
}

#[derive(Debug, ServiceDispatcher, ServiceFactory)]
#[service_dispatcher(implements("MyInterface"))]
struct MyService;

impl MyInterface<ExecutionContext<'_>> for MyService {
    // implementation skipped...
}

impl Service for MyService {
    fn after_commit(&self, ctx: AfterCommitContext<'_>) {
        if let Some(broadcaster) = ctx.broadcaster() {
            // Broadcast a `do_something` transaction with
            // the specified payload. We swallow an error in this case
            // (in a more thorough setup, it could be logged).
            broadcaster.blocking().publish_string((), "!".to_owned()).ok();
        }
    }
}

Methods

impl Broadcaster[src]

pub fn blocking(self) -> BlockingBroadcaster[src]

Returns a synchronous broadcaster that blocks the current thread to broadcast transaction.

Trait Implementations

impl Clone for Broadcaster[src]

impl Debug for Broadcaster[src]

impl GenericCall<()> for Broadcaster[src]

Signs and asynchronous broadcasts a transaction to the other nodes in the network.

The transaction is signed by the service keypair of the node. The same input transaction will lead to the identical transaction being broadcast. If this is undesired, add a nonce field to the input transaction (e.g., a u64) and change it between the calls.

Return value

Returns the hash of the created transaction, or an error if the transaction cannot be broadcast. An error means that the node is being shut down.

type Output = BoxFuture<'static, Result<Hash, SendError>>

Type of values output by the stub.

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,